tytd-server/Program.cs

276 lines
11 KiB
C#
Raw Normal View History

2021-06-25 08:30:45 +00:00
using System.Linq;
using SimpleHttp;
2021-06-24 01:10:20 +00:00
using System;
2021-06-24 02:55:41 +00:00
using System.Collections.Generic;
2021-06-24 01:10:20 +00:00
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;
2021-06-25 08:30:45 +00:00
using Newtonsoft.Json;
2021-08-26 10:44:54 +00:00
using Makaretu.Dns;
2021-09-25 06:18:12 +00:00
using System.Net;
using System.Threading.Tasks;
using youtube_downloader.Server.Models;
2021-06-24 01:10:20 +00:00
namespace youtube_downloader
{
2021-08-03 19:39:03 +00:00
2021-06-24 01:10:20 +00:00
class Program
{
2021-08-03 19:39:03 +00:00
2021-06-28 16:42:57 +00:00
static void Main(string[] arg)
2021-06-24 01:10:20 +00:00
{
2021-08-03 19:39:03 +00:00
Thread t = new Thread(new ThreadStart(() => {
2021-06-24 03:41:34 +00:00
Server.Functions.Downloader.DL.DownloadThread().GetAwaiter().GetResult();
}));
t.Start();
2021-06-25 08:30:45 +00:00
Thread t2 = new Thread(new ThreadStart(() => {
Server.Functions.Downloader.DL.ListenForQueueItem().GetAwaiter().GetResult();
}));
t2.Start();
2021-08-26 10:44:54 +00:00
2021-09-25 06:18:12 +00:00
2021-06-24 01:10:20 +00:00
// we need to get our app name so that
// we can create unique names for our mutex and our pipe
2021-06-25 08:30:45 +00:00
string webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite");
2021-08-25 09:11:08 +00:00
/*Route.Add("/api/endpoint/", (rq, rp, args) =>
{
rp.AsText(HandleRequest(rq.InputStream,rq.CurrentEncoding), "application/json");
},
"POST");*/
2021-08-26 08:38:28 +00:00
2021-08-25 09:11:08 +00:00
Route.Add("/api/upload/", (rq,rp,args) => {
var files = rq.ParseBody(args);
foreach (var f in files.Values)
f.Save(Path.Combine(webSitePath,f.FileName));
2021-08-26 08:38:28 +00:00
rp.AsText("uploaded", "text/plain");
2021-08-26 04:30:42 +00:00
},"POST");
2021-09-25 06:53:44 +00:00
Route.Add("/api/AddItems", (HttpAction)AddItems,"POST");
2021-08-03 19:39:03 +00:00
Route.Add("/", (rq, rp, args) =>
2021-06-24 07:31:55 +00:00
{
2021-06-25 08:30:45 +00:00
rp.AsFile(rq, Path.Combine(webSitePath, "index.html"));
});
2021-06-27 17:22:18 +00:00
Route.Add("/api/Search/{text}", (rq, rp, args) =>
{
2021-08-03 19:39:03 +00:00
string search = System.Web.HttpUtility.UrlDecode(args["text"]);
2021-06-27 17:22:18 +00:00
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search));
rp.AsText(json, "application/json");
2021-08-25 09:11:08 +00:00
});
Route.Add("/api/SearchOnly/{text}", (rq, rp, args) =>
{
string search = System.Web.HttpUtility.UrlDecode(args["text"]);
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search,false));
rp.AsText(json, "application/json");
2021-06-27 17:22:18 +00:00
});
2021-06-25 08:30:45 +00:00
Route.Add("/api/Storage/GetDirectories/{Path}", (rq, rp, args) =>
2021-06-24 01:10:20 +00:00
{
2021-06-25 08:30:45 +00:00
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
2021-06-24 03:41:34 +00:00
2021-06-25 08:30:45 +00:00
if (Directory.Exists(path))
2021-06-24 03:41:34 +00:00
{
2021-06-25 08:30:45 +00:00
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateDirectories(path).Select<string, string>((o) => { return Path.GetFileName(o); }));
rp.AsText(json, "application/json");
2021-06-24 03:41:34 +00:00
}
2021-06-25 08:30:45 +00:00
else
2021-06-24 03:41:34 +00:00
{
2021-06-25 08:30:45 +00:00
rp.AsText("[]", "application/json");
2021-06-24 03:41:34 +00:00
}
2021-06-25 08:30:45 +00:00
});
Route.Add("/api/Storage/GetFiles/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
2021-06-24 03:41:34 +00:00
2021-06-25 08:30:45 +00:00
if (Directory.Exists(path))
2021-06-24 01:10:20 +00:00
{
2021-06-25 08:30:45 +00:00
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateFiles(path).Select<string, string>((o) => { return Path.GetFileName(o); }));
rp.AsText(json, "application/json");
2021-06-24 01:10:20 +00:00
}
2021-06-25 08:30:45 +00:00
else
{
rp.AsText("[]", "application/json");
}
});
2021-08-03 19:39:03 +00:00
2021-06-25 08:30:45 +00:00
Route.Add("/api/Storage/FileExists/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
string json = File.Exists(path) ? "true" : "false";
rp.AsText(json, "text/plain");
2021-08-03 19:39:03 +00:00
2021-06-25 08:30:45 +00:00
});
Route.Add("/api/Storage/DirectoryExists/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
string json = Directory.Exists(path) ? "true" : "false";
rp.AsText(json, "text/plain");
2021-06-24 01:10:20 +00:00
2021-06-25 08:30:45 +00:00
});
Route.Add("/api/Storage/File/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
2021-08-03 19:39:03 +00:00
2021-06-25 08:30:45 +00:00
rp.AsFile(rq, path);
});
2021-06-25 10:55:27 +00:00
Route.Add("/api/AddVideo/{Id}", (rq, rp, args) =>
2021-08-03 19:39:03 +00:00
{
Server.Functions.Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
2021-06-25 10:55:27 +00:00
Route.Add("/api/AddVideoRes/{R}/{Id}", (rq, rp, args) =>
2021-06-25 08:30:45 +00:00
{
2021-06-25 10:55:27 +00:00
Server.Functions.Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddPlaylist/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadPlaylist(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddPlaylistRes/{R}/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadPlaylist(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddChannel/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadChannel(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddChannelRes/{R}/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadChannel(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddUser/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadUser(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddChannelOnly/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadChannelOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
2021-08-25 09:11:08 +00:00
Route.Add("/api/AddUserOnly/{Id}", (rq, rp, args) =>
2021-06-25 10:55:27 +00:00
{
Server.Functions.Downloader.DownloadUserOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
2021-08-25 09:11:08 +00:00
Route.Add("/api/AddPlaylistOnly/{Id}", (rq, rp, args) =>
{
2021-08-26 09:04:57 +00:00
Server.Functions.Downloader.DownloadPlaylistOnly(System.Web.HttpUtility.UrlDecode(args["Id"]),Resolution.NoConvert);
2021-08-25 09:11:08 +00:00
rp.AsRedirect("/");
});
Route.Add("/api/AddVideoInfo/{Id}", (rq, rp, args) =>
{
2021-08-26 09:04:57 +00:00
Server.Functions.Downloader.DownloadVideoInfo(System.Web.HttpUtility.UrlDecode(args["Id"]),Resolution.NoConvert);
2021-08-25 09:11:08 +00:00
rp.AsRedirect("/");
});
2021-06-25 10:55:27 +00:00
Route.Add("/api/AddUserRes/{R}/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadUser(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
2021-06-25 08:30:45 +00:00
rp.AsRedirect("/");
});
Route.Add("/api/QueueMove/{From}/{To}", (rq, rp, args) =>
{
2021-08-03 19:39:03 +00:00
Server.Functions.Downloader.ModQueue(args["To"], args["From"]);
2021-06-25 08:30:45 +00:00
rp.AsRedirect("/");
});
Route.Add("/api/QueueList", (rq, rp, args) =>
{
string json = Server.Functions.Downloader.GetQueue();
rp.AsText(json, "application/json");
});
Route.Add("/api/Progress", (rq, rp, args) =>
{
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress());
rp.AsText(json, "application/json");
});
2021-06-25 11:03:09 +00:00
Route.Add("/api/Watch/{VideoId}", (rq, rp, args) =>
{
var txt = Templating.RenderFile(Path.Combine(webSitePath, "watch_page.thtml"), args); //populate template
rp.AsText(txt);
});
2021-06-25 10:55:27 +00:00
Route.Add("/api/Redownload", (rq, rp, 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("/");
});
Route.Add("/api/Redownload/{R}", (rq, rp, 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("/");
});
2021-06-25 08:30:45 +00:00
Route.Add("/{Path}", (rq, rp, args) =>
{
2021-08-03 19:39:03 +00:00
string path = Path.Combine(webSitePath, args["Path"].Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[0]);
2021-06-24 01:10:20 +00:00
2021-06-25 08:30:45 +00:00
rp.AsFile(rq, path);
});
2021-08-26 10:44:54 +00:00
Console.WriteLine("Almost Ready To Listen");
2021-09-25 06:18:12 +00:00
2021-06-28 16:42:57 +00:00
if (arg.Length > 0)
{
2021-08-26 10:44:54 +00:00
2021-06-28 16:42:57 +00:00
HttpServer.ListenAsync(arg[0], CancellationToken.None, Route.OnHttpRequestAsync).Wait();
}
else
{
HttpServer.ListenAsync(3250, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
}
2021-06-24 01:10:20 +00:00
}
2021-09-25 06:18:12 +00:00
2021-09-25 06:53:44 +00:00
private static void AddItems(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
2021-08-25 09:11:08 +00:00
{
2021-09-25 06:53:44 +00:00
var f = request.ParseBody(arguments);
foreach(var file in f.Values)
2021-08-25 09:11:08 +00:00
{
2021-09-25 06:53:44 +00:00
using (var req = new StreamReader(file.Value))
{
List<IDResolutionTypeTriplet> tripletlst = JsonConvert.DeserializeObject<List<IDResolutionTypeTriplet>>( req.ReadToEnd());
Server.Functions.Downloader.DownloadItems(tripletlst);
response.Redirect("/");
}
2021-08-25 09:11:08 +00:00
}
2021-09-25 06:53:44 +00:00
2021-09-25 06:18:12 +00:00
2021-08-25 09:11:08 +00:00
}
2021-09-25 06:18:12 +00:00
/*
private static string HandleRequest(Stream inputStream, Encoding currentEncoding)
{
using (var streamReader = new StreamReader(inputStream, currentEncoding))
{
return Newtonsoft.Json.JsonConvert.SerializeObject(HandleRequest(Newtonsoft.Json.JsonConvert.DeserializeObject<YoutubeDownloaderRequest>(streamReader.ReadToEnd()));
}
}
private static YoutubeDownloaderResponse HandleRequest(YoutubeDownloaderRequest data)
{
2021-06-24 01:10:20 +00:00
2021-09-25 06:18:12 +00:00
}*/
2021-06-24 01:10:20 +00:00
}
}