346 lines
15 KiB
C#
346 lines
15 KiB
C#
using System.Linq;
|
|
using SimpleHttp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Pipes;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using TessesYoutubeDownloader.Server.Models;
|
|
using Newtonsoft.Json;
|
|
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
using youtube_downloader.Server.Models;
|
|
|
|
namespace youtube_downloader
|
|
{
|
|
|
|
class Program
|
|
{
|
|
static string webSitePath;
|
|
static void Main(string[] arg)
|
|
{
|
|
|
|
Thread t = new Thread(new ThreadStart(() => {
|
|
Server.Functions.Downloader.DL.DownloadThread().GetAwaiter().GetResult();
|
|
}));
|
|
t.Start();
|
|
Thread t2 = new Thread(new ThreadStart(() => {
|
|
Server.Functions.Downloader.DL.ListenForQueueItem().GetAwaiter().GetResult();
|
|
}));
|
|
t2.Start();
|
|
|
|
|
|
// we need to get our app name so that
|
|
// we can create unique names for our mutex and our pipe
|
|
webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite");
|
|
|
|
|
|
|
|
/* Generic */
|
|
Route.Add("/api/AddItems", (HttpAction)AddItems, "POST");
|
|
Route.Add("/api/AddFile/{Url}", (HttpAction)AddFile);
|
|
Route.Add("/api/AddCaptions/{Id}", (HttpAction)AddCaptions);
|
|
|
|
/* Videos */
|
|
Route.Add("/api/AddVideoInfo/{Id}", (HttpAction)AddVideoInfo);
|
|
Route.Add("/api/AddVideo/{Id}", (HttpAction)AddVideo);
|
|
Route.Add("/api/AddVideoRes/{R}/{Id}", (HttpAction)AddVideoRes);
|
|
Route.Add("/api/Redownload", (HttpAction)Redownload);
|
|
Route.Add("/api/Redownload/{R}", (HttpAction)RedownloadRes);
|
|
Route.Add("/api/Watch/{VideoId}", (HttpAction)Watch);
|
|
|
|
/* Playlist */
|
|
Route.Add("/api/AddPlaylistOnly/{Id}", (HttpAction)AddPlaylistOnly);
|
|
Route.Add("/api/AddPlaylist/{Id}", (HttpAction)AddPlaylist);
|
|
Route.Add("/api/AddPlaylistRes/{R}/{Id}", (HttpAction)AddPlaylistRes);
|
|
|
|
/* Search */
|
|
Route.Add("/api/SearchOnly/{text}", (HttpAction)SearchOnly);
|
|
Route.Add("/api/Search/{text}", (HttpAction)Search);
|
|
|
|
/* Channel */
|
|
Route.Add("/api/AddChannelOnly/{Id}", (HttpAction)AddChannelOnly);
|
|
Route.Add("/api/AddChannel/{Id}", (HttpAction)AddChannel);
|
|
Route.Add("/api/AddChannelRes/{R}/{Id}", (HttpAction)AddChannelRes);
|
|
|
|
/* User */
|
|
Route.Add("/api/AddUserOnly/{Id}", (HttpAction)AddUserOnly);
|
|
Route.Add("/api/AddUser/{Id}", (HttpAction)AddUser);
|
|
Route.Add("/api/AddUserRes/{R}/{Id}", (HttpAction)AddUserRes);
|
|
|
|
/* Queue and Progress */
|
|
Route.Add("/api/QueueList", (HttpAction)QueueList);
|
|
Route.Add("/api/QueueMove/{From}/{To}", (HttpAction)QueueMove);
|
|
Route.Add("/api/QueueMove2/{To}/{Id}", (HttpAction)QueueMove2);
|
|
Route.Add("/api/Progress", (HttpAction)VideoProgress);
|
|
|
|
/* Storage */
|
|
Route.Add("/api/Storage/GetDirectories/{Path}", (HttpAction)StorageGetDirectories);
|
|
Route.Add("/api/Storage/GetFiles/{Path}", (HttpAction)StorageGetFiles);
|
|
Route.Add("/api/Storage/DirectoryExists/{Path}", (HttpAction)StorageDirectoryExists);
|
|
Route.Add("/api/Storage/FileExists/{Path}", (HttpAction)StorageFileExists);
|
|
Route.Add("/api/Storage/File/{Path}", (HttpAction)StorageFile);
|
|
Route.Add("/api/upload/", (HttpAction)UploadFiles, "POST");
|
|
|
|
/* Other */
|
|
Route.Add("/", (HttpAction)Index);
|
|
Route.Add("/{Path}", (HttpAction)RootPath);
|
|
Console.WriteLine("Almost Ready To Listen");
|
|
|
|
if (arg.Length > 0)
|
|
{
|
|
|
|
HttpServer.ListenAsync(arg[0], CancellationToken.None, Route.OnHttpRequestAsync).Wait();
|
|
|
|
}
|
|
else
|
|
{
|
|
HttpServer.ListenAsync(3250, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
|
|
}
|
|
}
|
|
|
|
#region Generic
|
|
private static void AddItems(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
{
|
|
var f = request.ParseBody(arguments);
|
|
foreach (var file in f.Values)
|
|
{
|
|
|
|
using (var req = new StreamReader(file.Value))
|
|
{
|
|
List<IDResolutionTypeTriplet> tripletlst = JsonConvert.DeserializeObject<List<IDResolutionTypeTriplet>>(req.ReadToEnd());
|
|
Server.Functions.Downloader.DownloadItems(tripletlst);
|
|
response.Redirect("/");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
public static void AddFile(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
{
|
|
Server.Functions.Downloader.DownloadFile(arguments["Url"]);
|
|
}
|
|
public static void AddCaptions(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
{
|
|
Server.Functions.Downloader.DownloadCaptions(arguments["Id"]);
|
|
}
|
|
#endregion
|
|
#region Video
|
|
public static void AddVideoInfo(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadVideoInfo(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddVideo(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
|
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddVideoRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void Redownload(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
foreach (var item in Directory.GetFiles(Server.Functions.Downloader.DL.GetPath(true, "Info"), "*.json"))
|
|
{
|
|
string id = System.IO.Path.GetFileNameWithoutExtension(item);
|
|
Server.Functions.Downloader.DownloadVideo(id, Resolution.NoConvert);
|
|
}
|
|
rp.AsRedirect("/");
|
|
}
|
|
|
|
public static void RedownloadRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
foreach (var item in Directory.GetFiles(Server.Functions.Downloader.DL.GetPath(true, "Info"), "*.json"))
|
|
{
|
|
string id = System.IO.Path.GetFileNameWithoutExtension(item);
|
|
Server.Functions.Downloader.DownloadVideo(id, (Resolution)int.Parse(args["R"]));
|
|
}
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void Watch(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
var txt = Templating.RenderFile(Path.Combine(webSitePath, "watch_page.thtml"), args); //populate template
|
|
rp.AsText(txt);
|
|
}
|
|
#endregion
|
|
#region Playlist
|
|
public static void AddPlaylistOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadPlaylistOnly(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddPlaylist(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadPlaylist(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
|
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddPlaylistRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadPlaylist(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
|
rp.AsRedirect("/");
|
|
}
|
|
#endregion
|
|
#region Search
|
|
public static void SearchOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string search = System.Web.HttpUtility.UrlDecode(args["text"]);
|
|
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search, false));
|
|
rp.AsText(json, "application/json");
|
|
|
|
}
|
|
public static void Search(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string search = System.Web.HttpUtility.UrlDecode(args["text"]);
|
|
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search));
|
|
rp.AsText(json, "application/json");
|
|
|
|
}
|
|
#endregion
|
|
#region Channel
|
|
public static void AddChannelOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadChannelOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
|
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddChannel(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadChannel(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
|
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddChannelRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadChannel(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
|
rp.AsRedirect("/");
|
|
}
|
|
#endregion
|
|
#region User
|
|
public static void AddUserOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadUserOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddUser(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadUser(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
|
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void AddUserRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.DownloadUser(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
|
rp.AsRedirect("/");
|
|
}
|
|
#endregion
|
|
#region Queue And Progress
|
|
public static void QueueList(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string json = Server.Functions.Downloader.GetQueue();
|
|
rp.AsText(json, "application/json");
|
|
}
|
|
public static void QueueMove(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
Server.Functions.Downloader.ModQueue(args["To"], args["From"]);
|
|
rp.AsRedirect("/");
|
|
}
|
|
public static void QueueMove2(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
{
|
|
Server.Functions.Downloader.ModQueue2(arguments["To"], arguments["Id"]);
|
|
response.AsRedirect("/");
|
|
}
|
|
public static void VideoProgress(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress());
|
|
rp.AsText(json, "application/json");
|
|
}
|
|
#endregion
|
|
#region Storage
|
|
public static void StorageGetDirectories(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
|
|
|
|
if (Directory.Exists(path))
|
|
{
|
|
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateDirectories(path).Select<string, string>((o) => { return Path.GetFileName(o); }));
|
|
rp.AsText(json, "application/json");
|
|
}
|
|
else
|
|
{
|
|
rp.AsText("[]", "application/json");
|
|
}
|
|
}
|
|
public static void StorageGetFiles(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
|
|
|
|
if (Directory.Exists(path))
|
|
{
|
|
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateFiles(path).Select<string, string>((o) => { return Path.GetFileName(o); }));
|
|
rp.AsText(json, "application/json");
|
|
}
|
|
else
|
|
{
|
|
rp.AsText("[]", "application/json");
|
|
}
|
|
}
|
|
public static void StorageDirectoryExists(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
|
|
string json = Directory.Exists(path) ? "true" : "false";
|
|
rp.AsText(json, "text/plain");
|
|
|
|
}
|
|
public static void StorageFileExists(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
|
|
string json = File.Exists(path) ? "true" : "false";
|
|
rp.AsText(json, "text/plain");
|
|
|
|
}
|
|
public static void StorageFile(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
if (args["path"].StartsWith("config/"))
|
|
{
|
|
rp.AsText("Access denied");
|
|
}
|
|
else
|
|
{
|
|
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
|
|
|
|
rp.AsFile(rq, path);
|
|
}
|
|
}
|
|
public static void UploadFiles(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
var files = rq.ParseBody(args);
|
|
foreach (var f in files.Values)
|
|
f.Save(Path.Combine(webSitePath, f.FileName));
|
|
rp.AsText("uploaded", "text/plain");
|
|
}
|
|
#endregion
|
|
#region Other
|
|
public static void Index(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
rp.AsFile(rq, Path.Combine(webSitePath, "index.html"));
|
|
}
|
|
public static void RootPath(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
{
|
|
string path = Path.Combine(webSitePath, args["Path"].Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[0]);
|
|
|
|
rp.AsFile(rq, path);
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|