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;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
using TYTD.Server.Models;
|
2021-06-25 08:30:45 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2021-09-25 19:33:10 +00:00
|
|
|
|
|
2021-09-25 06:18:12 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
using TYTD.Server.Functions;
|
2021-12-08 22:02:30 +00:00
|
|
|
|
using YoutubeExplode.Videos;
|
|
|
|
|
using YoutubeExplode;
|
|
|
|
|
using YoutubeExplode.Videos.Streams;
|
2022-01-03 16:35:57 +00:00
|
|
|
|
using PlaylistsNET;
|
|
|
|
|
using PlaylistsNET.Content;
|
|
|
|
|
using PlaylistsNET.Models;
|
|
|
|
|
using PlaylistsNET.Utils;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
namespace TYTD
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-08-03 19:39:03 +00:00
|
|
|
|
|
2022-01-03 16:35:57 +00:00
|
|
|
|
static class Program
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
public static void RemoveRange<T>(this List<T> list,IEnumerable<T> items)
|
|
|
|
|
{
|
|
|
|
|
foreach(var item in items)
|
|
|
|
|
{
|
|
|
|
|
list.Remove(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
static string webSitePath;
|
2021-06-28 16:42:57 +00:00
|
|
|
|
static void Main(string[] arg)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-12-08 22:02:30 +00:00
|
|
|
|
Downloader.GetManifest = GetManifest;
|
2021-08-03 19:39:03 +00:00
|
|
|
|
Thread t = new Thread(new ThreadStart(() => {
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DL.DownloadThread().GetAwaiter().GetResult();
|
2021-06-24 03:41:34 +00:00
|
|
|
|
}));
|
|
|
|
|
t.Start();
|
2021-06-25 08:30:45 +00:00
|
|
|
|
Thread t2 = new Thread(new ThreadStart(() => {
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DL.ListenForQueueItem().GetAwaiter().GetResult();
|
2021-06-25 08:30:45 +00:00
|
|
|
|
}));
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-25 08:30:45 +00:00
|
|
|
|
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-12-08 01:07:43 +00:00
|
|
|
|
webSitePath = Downloader.DL.GetPath(true, "WebSite");
|
2021-08-25 09:11:08 +00:00
|
|
|
|
|
2021-10-24 12:52:24 +00:00
|
|
|
|
Route.Before += Route_Before;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Generic */
|
|
|
|
|
Route.Add("/api/AddItems", (HttpAction)AddItems, "POST");
|
2021-11-14 12:57:02 +00:00
|
|
|
|
Route.Add("/api/AddItem/{Id}", (HttpAction)AddItem);
|
|
|
|
|
Route.Add("/api/AddItemRes/{R}/{Id}", (HttpAction)AddItemRes);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
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);
|
2022-01-03 16:35:57 +00:00
|
|
|
|
Route.Add("/api/PersonalPlaylist/{PlaylistName}",(HttpAction)PersonalPlaylist);
|
|
|
|
|
Route.Add("/api/CreatePlaylist/{Ids}/playlist.{extension}", (HttpAction)CreatePlaylist);
|
|
|
|
|
Route.Add("/api/CreatePlaylistRes/{Ids}/playlist.{extension}", (HttpAction)CreatePlaylistRes);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
/* Search */
|
|
|
|
|
Route.Add("/api/SearchOnly/{text}", (HttpAction)SearchOnly);
|
|
|
|
|
Route.Add("/api/Search/{text}", (HttpAction)Search);
|
2021-08-26 08:38:28 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
/* Channel */
|
|
|
|
|
Route.Add("/api/AddChannelOnly/{Id}", (HttpAction)AddChannelOnly);
|
|
|
|
|
Route.Add("/api/AddChannel/{Id}", (HttpAction)AddChannel);
|
|
|
|
|
Route.Add("/api/AddChannelRes/{R}/{Id}", (HttpAction)AddChannelRes);
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
/* User */
|
|
|
|
|
Route.Add("/api/AddUserOnly/{Id}", (HttpAction)AddUserOnly);
|
|
|
|
|
Route.Add("/api/AddUser/{Id}", (HttpAction)AddUser);
|
|
|
|
|
Route.Add("/api/AddUserRes/{R}/{Id}", (HttpAction)AddUserRes);
|
2021-08-25 09:11:08 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
/* 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);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Route.Add("/api/Redo", (HttpAction)Redo);
|
|
|
|
|
Route.Add("/api/Cancel", (HttpAction)Cancel);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
|
|
|
|
/* 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);
|
2021-10-24 12:52:24 +00:00
|
|
|
|
Route.Add("/api/Storage/Video/{Id}",(HttpAction)Video);
|
|
|
|
|
Route.Add("/api/Storage/VideoRes/{Res}/{Id}",(HttpAction)VideoRes);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
Route.Add("/api/upload/", (HttpAction)UploadFiles, "POST");
|
2021-06-24 03:41:34 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
Route.Add("/api/endpoint", (HttpAction)Endpoint,"POST");
|
2022-01-03 16:35:57 +00:00
|
|
|
|
|
2021-12-08 01:16:23 +00:00
|
|
|
|
ApiLoader.Init();
|
2021-09-25 22:30:16 +00:00
|
|
|
|
/* Other */
|
|
|
|
|
Route.Add("/", (HttpAction)Index);
|
2021-12-08 07:24:25 +00:00
|
|
|
|
Route.Add("/extensions.html", (HttpAction)Extensions);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Route.Add("/{Path}", (HttpAction)RootPath);
|
2021-12-12 10:04:16 +00:00
|
|
|
|
Route.Add("/{Path}",(HttpAction)UploadFilePut,"PUT");
|
2022-01-03 16:35:57 +00:00
|
|
|
|
Console.CancelKeyPress += (sender, e) => { ApiLoader.Dispose();var date = DateTime.Now.ToString("yyyyMMdd_HHmmss");File.WriteAllText(Path.Combine("config","queues-close",$"{date}.json"), Downloader.GetQueue()); e.Cancel = false; };
|
2021-12-08 01:16:23 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
Console.WriteLine("Almost Ready To Listen");
|
|
|
|
|
|
|
|
|
|
if (arg.Length > 0)
|
2021-06-25 08:30:45 +00:00
|
|
|
|
{
|
2021-06-24 03:41:34 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
HttpServer.ListenAsync(arg[0], CancellationToken.None, Route.OnHttpRequestAsync).Wait();
|
2021-08-03 19:39:03 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2021-06-25 08:30:45 +00:00
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
HttpServer.ListenAsync(3250, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-03 19:39:03 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
|
2021-12-08 22:02:30 +00:00
|
|
|
|
private static async Task<StreamManifest> GetManifest(YoutubeClient arg1, VideoId arg2)
|
|
|
|
|
{
|
2021-12-31 15:28:34 +00:00
|
|
|
|
return await arg1.Videos.Streams.GetManifestAsync(arg2);
|
2021-12-08 22:02:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
#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)
|
2021-06-25 08:30:45 +00:00
|
|
|
|
{
|
2021-06-24 01:10:20 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
using (var req = new StreamReader(file.Value))
|
2021-09-25 19:33:10 +00:00
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
List<IDResolutionTypeTriplet> tripletlst = JsonConvert.DeserializeObject<List<IDResolutionTypeTriplet>>(req.ReadToEnd());
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadItems(tripletlst);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
response.Redirect("/");
|
2021-09-25 19:33:10 +00:00
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-06-25 08:30:45 +00:00
|
|
|
|
|
2021-08-03 19:39:03 +00:00
|
|
|
|
|
2021-11-14 12:57:02 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddItem(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadItem(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-11-14 12:57:02 +00:00
|
|
|
|
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-11-14 12:57:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddItemRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadItem(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddFile(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadFile(arguments["Url"]);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddCaptions(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadCaptions(arguments["Id"]);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region Video
|
|
|
|
|
public static void AddVideoInfo(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadVideoInfo(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddVideo(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-06-25 10:55:27 +00:00
|
|
|
|
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-10-24 12:52:24 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public static void AddVideoRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void Redownload(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
foreach (var item in Directory.GetFiles(Downloader.DL.GetPath(true, "Info"), "*.json"))
|
2021-06-25 10:55:27 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
string id =Path.GetFileNameWithoutExtension(item);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadVideo(id, Resolution.NoConvert);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RedownloadRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
foreach (var item in Directory.GetFiles(Downloader.DL.GetPath(true, "Info"), "*.json"))
|
2021-06-25 10:55:27 +00:00
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
string id = System.IO.Path.GetFileNameWithoutExtension(item);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadVideo(id, (Resolution)int.Parse(args["R"]));
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadPlaylistOnly(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddPlaylist(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadPlaylist(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-06-25 10:55:27 +00:00
|
|
|
|
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddPlaylistRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadPlaylist(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
|
|
|
|
}
|
|
|
|
|
public static void PersonalPlaylist(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
|
|
|
|
string playlistName=System.Web.HttpUtility.UrlDecode(args["PlaylistName"]);
|
|
|
|
|
string playlistNameWithoutExtension =Path.GetFileNameWithoutExtension(playlistName);
|
|
|
|
|
string path= Path.Combine("PersonalPlaylist", $"{playlistNameWithoutExtension}.json");
|
|
|
|
|
string extension = Path.GetExtension(playlistName);
|
|
|
|
|
List<(string Id, Resolution Resolution)> list = JsonConvert.DeserializeObject<List<(string Id, Resolution Resolution)>>(File.ReadAllText(path));
|
|
|
|
|
string mimeType;
|
|
|
|
|
rp.AsText(CreatePlaylistString(rq, list, extension, out mimeType),mimeType);
|
|
|
|
|
}
|
|
|
|
|
public static void CreatePlaylist(HttpListenerRequest rq,HttpListenerResponse rp,Dictionary<string,string> args)
|
|
|
|
|
{
|
|
|
|
|
// Route.Add("/api/PersonalPlaylist/{PlaylistName}", (HttpAction)PersonalPlaylist);
|
|
|
|
|
//Route.Add("/api/CreatePlaylist/{Ids}/playlist.{extension}", (HttpAction)CreatePlaylist);
|
|
|
|
|
//Route.Add("/api/CreatePlaylistRes/{Ids}/playlist.{extension}", (HttpAction)CreatePlaylistRes);
|
|
|
|
|
|
|
|
|
|
string[] ids= System.Web.HttpUtility.UrlDecode(args["Ids"]).Split(',');
|
|
|
|
|
var list = new List<(string Id, Resolution Resolution)>();
|
|
|
|
|
foreach (var id in ids)
|
|
|
|
|
{
|
|
|
|
|
list.Add((id, Resolution.NoConvert));
|
|
|
|
|
}
|
|
|
|
|
string mimeType;
|
|
|
|
|
rp.AsText(CreatePlaylistString(rq, list, args["extension"],out mimeType),mimeType);
|
|
|
|
|
}
|
|
|
|
|
public static void CreatePlaylistRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(args["Ids"]).Split(',');
|
|
|
|
|
var list = new List<(string Id, Resolution Resolution)>();
|
|
|
|
|
for (int i=0;i<list.Count;i+=2)
|
|
|
|
|
{
|
|
|
|
|
string id = ids[i];
|
|
|
|
|
string res = ids[i + 1];
|
|
|
|
|
list.Add((id,(Resolution)int.Parse(res)));
|
|
|
|
|
}
|
|
|
|
|
string mimeType;
|
|
|
|
|
rp.AsText(CreatePlaylistString(rq, list, args["extension"], out mimeType),mimeType);
|
|
|
|
|
}
|
|
|
|
|
public static string CreatePlaylistString(HttpListenerRequest req, List<(string Id, Resolution Resolution)> videoIds, string ext,out string mimeType)
|
|
|
|
|
{
|
|
|
|
|
string[] _res = { "Converted", "NotConverted", "Audio" };
|
|
|
|
|
string UrlBegin = GetServerRoot(req);
|
|
|
|
|
string ext2 = ext.TrimStart('.');
|
|
|
|
|
if (ext2 == "m3u" || ext2 == "m3u8")
|
|
|
|
|
{
|
|
|
|
|
M3uPlaylist playlist = new M3uPlaylist();
|
|
|
|
|
playlist.IsExtended = true;
|
|
|
|
|
|
|
|
|
|
foreach (var item in videoIds)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
M3uPlaylistEntry entry = new M3uPlaylistEntry();
|
|
|
|
|
entry.Path = UrlBegin.TrimEnd('/') + $"/api/Storage/File/{_res[(int)item.Resolution]}/{item.Id}.mp4";
|
|
|
|
|
var video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(Path.Combine("Info", $"{item.Id}.json")));
|
|
|
|
|
entry.Title = video.Title;
|
|
|
|
|
entry.AlbumArtist = video.AuthorTitle;
|
|
|
|
|
entry.Duration = TimeSpan.FromSeconds(video.Duration);
|
|
|
|
|
playlist.PlaylistEntries.Add(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
M3uContent content = new M3uContent();
|
|
|
|
|
mimeType = "audio/x-mpegurl";
|
|
|
|
|
return content.ToText(playlist);
|
|
|
|
|
} else if (ext2 == "wpl")
|
|
|
|
|
{
|
|
|
|
|
WplPlaylist playlist = new WplPlaylist();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in videoIds)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
WplPlaylistEntry entry = new WplPlaylistEntry();
|
|
|
|
|
entry.Path = UrlBegin.TrimEnd('/') + $"/api/Storage/File/{_res[(int)item.Resolution]}/{item.Id}.mp4";
|
|
|
|
|
var video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(Path.Combine("Info", $"{item.Id}.json")));
|
|
|
|
|
entry.TrackTitle = video.Title;
|
|
|
|
|
entry.TrackArtist = video.AuthorTitle;
|
|
|
|
|
entry.Duration = TimeSpan.FromSeconds(video.Duration);
|
|
|
|
|
playlist.PlaylistEntries.Add(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WplContent content = new WplContent();
|
|
|
|
|
mimeType = "application/vnd.ms-wpl";
|
|
|
|
|
return content.ToText(playlist);
|
|
|
|
|
} else if (ext2 == "pls")
|
|
|
|
|
{
|
|
|
|
|
PlsPlaylist playlist = new PlsPlaylist();
|
|
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
|
foreach (var item in videoIds)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
PlsPlaylistEntry entry = new PlsPlaylistEntry();
|
|
|
|
|
entry.Path = UrlBegin.TrimEnd('/') + $"/api/Storage/File/{_res[(int)item.Resolution]}/{item.Id}.mp4";
|
|
|
|
|
var video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(Path.Combine("Info", $"{item.Id}.json")));
|
|
|
|
|
entry.Title = video.Title;
|
|
|
|
|
entry.Nr = i++;
|
|
|
|
|
|
|
|
|
|
playlist.PlaylistEntries.Add(entry);
|
|
|
|
|
}
|
|
|
|
|
mimeType = "audio/x-scpls";
|
|
|
|
|
PlsContent content = new PlsContent();
|
|
|
|
|
return content.ToText(playlist);
|
|
|
|
|
} else if (ext2 == "zpl")
|
|
|
|
|
{
|
|
|
|
|
ZplPlaylist playlist = new ZplPlaylist();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in videoIds)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ZplPlaylistEntry entry = new ZplPlaylistEntry();
|
|
|
|
|
entry.Path = UrlBegin.TrimEnd('/') + $"/api/Storage/File/{_res[(int)item.Resolution]}/{item.Id}.mp4";
|
|
|
|
|
var video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(Path.Combine("Info", $"{item.Id}.json")));
|
|
|
|
|
entry.TrackTitle = video.Title;
|
|
|
|
|
entry.TrackArtist = video.AuthorTitle;
|
|
|
|
|
entry.Duration = TimeSpan.FromSeconds(video.Duration);
|
|
|
|
|
|
|
|
|
|
playlist.PlaylistEntries.Add(entry);
|
|
|
|
|
}
|
|
|
|
|
mimeType = "application/vnd.ms-zpl";
|
|
|
|
|
ZplContent content = new ZplContent();
|
|
|
|
|
return content.ToText(playlist);
|
|
|
|
|
}
|
|
|
|
|
mimeType = "text/plain";
|
|
|
|
|
return "Invalid";
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2022-01-03 16:35:57 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
#endregion
|
|
|
|
|
#region Search
|
|
|
|
|
public static void SearchOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
|
|
|
|
string search = System.Web.HttpUtility.UrlDecode(args["text"]);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
string json = JsonConvert.SerializeObject(Downloader.Search(search, false));
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsText(json, "application/json");
|
2021-06-25 10:55:27 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void Search(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
|
|
|
|
string search = System.Web.HttpUtility.UrlDecode(args["text"]);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
string json = JsonConvert.SerializeObject(Downloader.Search(search));
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsText(json, "application/json");
|
2021-06-25 10:55:27 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region Channel
|
|
|
|
|
public static void AddChannelOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadChannelOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-06-25 11:03:09 +00:00
|
|
|
|
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddChannel(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadChannel(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-06-24 01:10:20 +00:00
|
|
|
|
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddChannelRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadChannel(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region User
|
|
|
|
|
public static void AddUserOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadUserOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddUser(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadUser(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-09-25 06:18:12 +00:00
|
|
|
|
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void AddUserRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.DownloadUser(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region Queue And Progress
|
|
|
|
|
public static void QueueList(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
string json = Downloader.GetQueue();
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsText(json, "application/json");
|
|
|
|
|
}
|
|
|
|
|
public static void QueueMove(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.ModQueue(args["To"], args["From"]);
|
2022-01-03 16:35:57 +00:00
|
|
|
|
rp.AsText("<script>history.back();</script>");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void QueueMove2(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
Downloader.ModQueue2(arguments["To"], arguments["Id"]);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
response.AsRedirect("/");
|
|
|
|
|
}
|
|
|
|
|
public static void VideoProgress(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
string json = JsonConvert.SerializeObject(Downloader.GetProgress());
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsText(json, "application/json");
|
2021-11-08 01:42:56 +00:00
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static void Redo(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
2021-11-08 01:42:56 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
lock (Downloader.DL.cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
Downloader.RedownloadIt = true;
|
|
|
|
|
Downloader.DL.cancelSrc.Item.Cancel();
|
|
|
|
|
}
|
2021-11-08 01:42:56 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
public static void Cancel(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
lock (Downloader.DL.cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
Downloader.RedownloadIt = false;
|
|
|
|
|
Downloader.DL.cancelSrc.Item.Cancel();
|
|
|
|
|
}
|
2021-11-08 01:42:56 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region Storage
|
|
|
|
|
public static void StorageGetDirectories(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, System.Web.HttpUtility.UrlDecode(args["Path"]));
|
2021-08-26 10:44:54 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, System.Web.HttpUtility.UrlDecode(args["Path"]));
|
2021-06-28 16:42:57 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
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");
|
2021-06-28 16:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsText("[]", "application/json");
|
2021-06-28 16:42:57 +00:00
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public static void StorageDirectoryExists(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, System.Web.HttpUtility.UrlDecode(args["Path"]));
|
2021-09-25 22:30:16 +00:00
|
|
|
|
string json = Directory.Exists(path) ? "true" : "false";
|
|
|
|
|
rp.AsText(json, "text/plain");
|
2021-09-25 06:18:12 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
public static void StorageFileExists(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
2021-08-25 09:11:08 +00:00
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, System.Web.HttpUtility.UrlDecode(args["Path"]));
|
2021-09-25 22:30:16 +00:00
|
|
|
|
string json = File.Exists(path) ? "true" : "false";
|
|
|
|
|
rp.AsText(json, "text/plain");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public static void StorageFile(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2021-09-27 05:52:31 +00:00
|
|
|
|
if (args["Path"].StartsWith("config/"))
|
2021-08-25 09:11:08 +00:00
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsText("Access denied");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, System.Web.HttpUtility.UrlDecode(args["Path"]));
|
2021-12-12 10:04:16 +00:00
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
string indexHtml = Path.Combine(path, "index.html");
|
|
|
|
|
if (File.Exists(indexHtml))
|
|
|
|
|
{
|
|
|
|
|
path = indexHtml;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string dir = Path.Combine(webSitePath, "err", "dir.html");
|
|
|
|
|
StringBuilder b = new StringBuilder();
|
|
|
|
|
var f = Directory.GetLastWriteTime(Path.GetDirectoryName(path));
|
|
|
|
|
string parentModified = $"{f.ToShortDateString()} {f.ToShortTimeString()}";
|
|
|
|
|
b.Append($"<tr><td><a href=\"..\">Up</a></td><td>{parentModified}</td><td>DIR</td></tr>");
|
|
|
|
|
foreach (var file in Directory.GetDirectories(path))
|
|
|
|
|
{
|
|
|
|
|
string name = Path.GetFileName(file);
|
|
|
|
|
string nameUrled = System.Web.HttpUtility.UrlEncode(name);
|
|
|
|
|
string nameHtmled = System.Web.HttpUtility.HtmlEncode(name);
|
|
|
|
|
f = Directory.GetLastWriteTime(file);
|
|
|
|
|
string dateModifed = $"{f.ToShortDateString()} {f.ToShortTimeString()}";
|
|
|
|
|
b.Append($"<tr><td><a href=\"{nameUrled}\">{nameHtmled}</a></td><td>{dateModifed}</td><td>DIR</td></tr>");
|
|
|
|
|
}
|
|
|
|
|
foreach (var file in Directory.GetFiles(path))
|
|
|
|
|
{
|
|
|
|
|
string name = Path.GetFileName(file);
|
|
|
|
|
string nameUrled = System.Web.HttpUtility.UrlEncode(name);
|
|
|
|
|
string nameHtmled = System.Web.HttpUtility.HtmlEncode(name);
|
|
|
|
|
f = File.GetLastWriteTime(file);
|
|
|
|
|
|
|
|
|
|
string dateModifed = $"{f.ToShortDateString()} {f.ToShortTimeString()}";
|
|
|
|
|
b.Append($"<tr><td><a href=\"{nameUrled}\">{nameHtmled}</a></td><td>{dateModifed}</td><td>FILE</td></tr>");
|
|
|
|
|
}
|
|
|
|
|
Dictionary<string, string> templating = new Dictionary<string, string>();
|
|
|
|
|
templating.Add("Items", b.ToString());
|
|
|
|
|
rp.AsText(Templating.RenderFile(dir, templating));
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsFile(rq, path);
|
2021-08-25 09:11:08 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-10-24 12:52:24 +00:00
|
|
|
|
public static void Video(HttpListenerRequest rq,HttpListenerResponse rp,Dictionary<string,string> args)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
VideoId? vid = VideoId.TryParse(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-10-24 12:52:24 +00:00
|
|
|
|
if (vid.HasValue)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, "NotConverted",vid.Value +".mp4");
|
2021-10-24 12:52:24 +00:00
|
|
|
|
rp.AddHeader("Content-Disposition", GetVideoContentDisposition(vid.Value).ToString());
|
|
|
|
|
rp.AsFile(rq, path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rp.WithCode(HttpStatusCode.BadRequest);
|
|
|
|
|
rp.AsText("Invalid Video ID or URL", "text/plain");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void VideoRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
VideoId? vid = VideoId.TryParse(System.Web.HttpUtility.UrlDecode(args["Id"]));
|
2021-10-24 12:52:24 +00:00
|
|
|
|
if (vid.HasValue)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
if(int.TryParse(args["Res"],out res))
|
|
|
|
|
{
|
|
|
|
|
if (res > 2 || res < 0)
|
|
|
|
|
{
|
|
|
|
|
rp.WithCode(HttpStatusCode.BadRequest);
|
|
|
|
|
rp.AsText($"Invalid Resolution Number must be either 0, 1 or 2", "text/plain");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string[] m = new string[] { "Converted", "NotConverted", "Audio" };
|
2021-12-08 01:07:43 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, m[res], vid.Value + ".mp4");
|
2021-10-24 12:52:24 +00:00
|
|
|
|
rp.AddHeader("Content-Disposition", GetVideoContentDisposition(vid.Value).ToString());
|
|
|
|
|
rp.AsFile(rq, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rp.WithCode(HttpStatusCode.BadRequest);
|
|
|
|
|
rp.AsText("Res is not a number", "text/plain");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rp.WithCode(HttpStatusCode.BadRequest);
|
|
|
|
|
rp.AsText("Invalid Video ID or URL", "text/plain");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static System.Net.Mime.ContentDisposition GetVideoContentDisposition(string id)
|
|
|
|
|
{
|
|
|
|
|
var cd = new System.Net.Mime.ContentDisposition();
|
|
|
|
|
string filename = GetVideoName(id);
|
|
|
|
|
cd.FileName = filename;
|
|
|
|
|
|
|
|
|
|
return cd;
|
|
|
|
|
}
|
|
|
|
|
public static string GetVideoName(string id)
|
|
|
|
|
{
|
|
|
|
|
string name = id + ".mp4";
|
2021-12-08 01:07:43 +00:00
|
|
|
|
string path = Downloader.DL.GetPath(true, "Info", id + ".json");
|
2021-10-24 12:52:24 +00:00
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
string info=File.ReadAllText(path);
|
|
|
|
|
name= JsonConvert.DeserializeObject<SavedVideo>(info).Title + ".mp4";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string asAscii = Encoding.ASCII.GetString(
|
|
|
|
|
Encoding.Convert(
|
|
|
|
|
Encoding.UTF8,
|
|
|
|
|
Encoding.GetEncoding(
|
|
|
|
|
Encoding.ASCII.EncodingName,
|
|
|
|
|
new EncoderReplacementFallback(string.Empty),
|
|
|
|
|
new DecoderExceptionFallback()
|
|
|
|
|
),
|
|
|
|
|
Encoding.UTF8.GetBytes(name)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
return asAscii;
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
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"));
|
|
|
|
|
}
|
2021-12-08 07:09:16 +00:00
|
|
|
|
public static void Extensions(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
|
|
|
|
rp.AsText(ApiLoader.Page);
|
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
private static void UploadFilePut(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
string p = System.Web.HttpUtility.UrlDecode(arguments["Path"]).Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[0];
|
2021-12-12 10:04:16 +00:00
|
|
|
|
string path = Path.Combine(webSitePath, p);
|
|
|
|
|
bool exists = File.Exists(path);
|
|
|
|
|
using (var instr = request.InputStream)
|
|
|
|
|
{
|
|
|
|
|
using (var outStr = File.Create(path))
|
|
|
|
|
{
|
|
|
|
|
instr.CopyTo(outStr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (exists)
|
|
|
|
|
{
|
|
|
|
|
response.WithCode(HttpStatusCode.NoContent);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.WithCode(HttpStatusCode.Created);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Endpoint(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
//MultiDictionary<string, string> multi = new MultiDictionary<string, string>();
|
2021-12-12 10:04:16 +00:00
|
|
|
|
request.ParseBody(arguments);
|
|
|
|
|
List<string> print = new List<string>();
|
|
|
|
|
bool hasOtherResponse=false;
|
|
|
|
|
bool success = true;
|
|
|
|
|
if(arguments.ContainsKey("operation"))
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
if (arguments["operation"] == "create_personal_playlist")
|
|
|
|
|
{
|
|
|
|
|
string myName = arguments["name"];
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "PersonalPlaylist", $"{myName}.json");
|
|
|
|
|
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(arguments["ids"]).Split(',');
|
|
|
|
|
var list = new List<(string Id, Resolution Resolution)>();
|
|
|
|
|
list.AddRange(ids.Select(e => (e, Resolution.NoConvert)));
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(list));
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "create_personal_playlist_res")
|
|
|
|
|
{
|
|
|
|
|
string myName = arguments["name"];
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "PersonalPlaylist", $"{myName}.json");
|
|
|
|
|
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(arguments["ids"]).Split(',');
|
|
|
|
|
var list = new List<(string Id, Resolution Resolution)>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < list.Count; i += 2)
|
|
|
|
|
{
|
|
|
|
|
string id = ids[i];
|
|
|
|
|
string res = ids[i + 1];
|
|
|
|
|
list.Add((id, (Resolution)int.Parse(res)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(list));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "add_to_personal_playlist")
|
|
|
|
|
{
|
|
|
|
|
string myName = arguments["name"];
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "PersonalPlaylist", $"{myName}.json");
|
|
|
|
|
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(arguments["ids"]).Split(',');
|
|
|
|
|
var list = JsonConvert.DeserializeObject<List<(string Id, Resolution Resolution)>>(File.ReadAllText(path));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
list.AddRange(ids.Select(e => (e, Resolution.NoConvert)));
|
|
|
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(list));
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "add_to_personal_playlist_res")
|
|
|
|
|
{
|
|
|
|
|
string myName = arguments["name"];
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "PersonalPlaylist", $"{myName}.json");
|
|
|
|
|
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(arguments["ids"]).Split(',');
|
|
|
|
|
var list = JsonConvert.DeserializeObject<List<(string Id, Resolution Resolution)>>(File.ReadAllText(path));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < list.Count; i += 2)
|
|
|
|
|
{
|
|
|
|
|
string id = ids[i];
|
|
|
|
|
string res = ids[i + 1];
|
|
|
|
|
list.Add((id, (Resolution)int.Parse(res)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(list));
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "insert_to_personal_playlist")
|
|
|
|
|
{
|
|
|
|
|
string myName = arguments["name"];
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "PersonalPlaylist", $"{myName}.json");
|
|
|
|
|
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(arguments["ids"]).Split(',');
|
|
|
|
|
var list = JsonConvert.DeserializeObject<List<(string Id, Resolution Resolution)>>(File.ReadAllText(path));
|
|
|
|
|
int offset = 0;
|
|
|
|
|
if (arguments.ContainsKey("offset"))
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(arguments["offset"], out offset))
|
|
|
|
|
{
|
|
|
|
|
offset = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
list.InsertRange(offset, ids.Select(e => (e, Resolution.NoConvert)));
|
|
|
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(list));
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "remove_from_personal_playlist")
|
|
|
|
|
{
|
|
|
|
|
string myName = arguments["name"];
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "PersonalPlaylist", $"{myName}.json");
|
|
|
|
|
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(arguments["ids"]).Split(',');
|
|
|
|
|
var list = JsonConvert.DeserializeObject<List<(string Id, Resolution Resolution)>>(File.ReadAllText(path));
|
|
|
|
|
List<(string, Resolution)> todel = new List<(string, Resolution)>();
|
|
|
|
|
foreach(var item in list)
|
|
|
|
|
{
|
|
|
|
|
if(ids.Contains(item.Id))
|
|
|
|
|
{
|
|
|
|
|
todel.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
list.RemoveRange(todel);
|
|
|
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(list));
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "insert_to_personal_playlist_res")
|
|
|
|
|
{
|
|
|
|
|
string myName = arguments["name"];
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "PersonalPlaylist", $"{myName}.json");
|
|
|
|
|
|
|
|
|
|
string[] ids = System.Web.HttpUtility.UrlDecode(arguments["ids"]).Split(',');
|
|
|
|
|
var list = JsonConvert.DeserializeObject<List<(string Id, Resolution Resolution)>>(File.ReadAllText(path));
|
|
|
|
|
int offset = 0;
|
|
|
|
|
if (arguments.ContainsKey("offset"))
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(arguments["offset"], out offset))
|
|
|
|
|
{
|
|
|
|
|
offset = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < list.Count; i += 2)
|
|
|
|
|
{
|
|
|
|
|
string id = ids[i];
|
|
|
|
|
string res = ids[i + 1];
|
|
|
|
|
list.Insert(offset++,(id, (Resolution)int.Parse(res)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(list));
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "server_download")
|
2021-12-12 10:04:16 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.ContainsKey("url"))
|
|
|
|
|
{
|
|
|
|
|
string myUrl = arguments["url"];
|
|
|
|
|
if (arguments.ContainsKey("resolution"))
|
|
|
|
|
{
|
|
|
|
|
uint res;
|
|
|
|
|
if (uint.TryParse(arguments["resolution"], out res) && res <= 2)
|
|
|
|
|
{
|
|
|
|
|
Downloader.DownloadItem(myUrl, (Resolution)res);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
print.Add("WARNING: argument resolution shall not be greater than 3 or less than 0");
|
|
|
|
|
Downloader.DownloadItem(myUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Downloader.DownloadItem(myUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "download")
|
|
|
|
|
{
|
|
|
|
|
if (arguments.ContainsKey("url"))
|
|
|
|
|
{
|
|
|
|
|
VideoId? vid = VideoId.TryParse(arguments["url"]);
|
|
|
|
|
if (vid.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (arguments.ContainsKey("resolution"))
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
if (int.TryParse(arguments["resolution"], out res))
|
|
|
|
|
{
|
|
|
|
|
if (res > 2 || res < 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
print.Add("Invalid Resolution Number must be either 0, 1 or 2");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string[] m = new string[] { "Converted", "NotConverted", "Audio" };
|
|
|
|
|
string path = Downloader.DL.GetPath(true, m[res], vid.Value + ".mp4");
|
|
|
|
|
response.AddHeader("Content-Disposition", GetVideoContentDisposition(vid.Value).ToString());
|
|
|
|
|
response.AsFile(request, path);
|
|
|
|
|
hasOtherResponse = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
print.Add("Res is not a number");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
string path = Downloader.DL.GetPath(true, "NotConverted", vid.Value + ".mp4");
|
|
|
|
|
response.AddHeader("Content-Disposition", GetVideoContentDisposition(vid.Value).ToString());
|
|
|
|
|
response.AsFile(request, path);
|
|
|
|
|
hasOtherResponse = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
success = false;
|
|
|
|
|
print.Add("Invalid Video ID or URL");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "enumerate_queue")
|
|
|
|
|
{
|
|
|
|
|
string json = Downloader.GetQueue();
|
|
|
|
|
response.AsText(json, "application/json");
|
|
|
|
|
hasOtherResponse = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (arguments["operation"] == "progress")
|
|
|
|
|
{
|
|
|
|
|
string json = JsonConvert.SerializeObject(Downloader.GetProgress());
|
|
|
|
|
response.AsText(json, "application/json");
|
|
|
|
|
hasOtherResponse = true;
|
|
|
|
|
|
|
|
|
|
}
|
2022-01-03 16:35:57 +00:00
|
|
|
|
else if (arguments["operation"] == "queue_move")
|
2021-12-12 10:04:16 +00:00
|
|
|
|
{
|
|
|
|
|
bool containsId = arguments.ContainsKey("id");
|
|
|
|
|
bool containsIndex = arguments.ContainsKey("index");
|
|
|
|
|
if (containsId ^ containsIndex)
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
print.Add("You cant use both id and index");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (arguments.ContainsKey("to"))
|
|
|
|
|
{
|
|
|
|
|
string to_loc = arguments["to"];
|
2022-01-03 16:35:57 +00:00
|
|
|
|
if (containsId)
|
2021-12-12 10:04:16 +00:00
|
|
|
|
Downloader.ModQueue2(to_loc, arguments["id"]);
|
|
|
|
|
|
|
|
|
|
if (containsIndex)
|
|
|
|
|
Downloader.ModQueue(to_loc, arguments["index"]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
print.Add("You must have the "to" variable set");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(success)
|
|
|
|
|
{
|
|
|
|
|
print.Add("Your request was delt with successfully");
|
|
|
|
|
}
|
|
|
|
|
else if(!hasOtherResponse)
|
|
|
|
|
{
|
|
|
|
|
response.WithCode(HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
|
|
|
|
if (!hasOtherResponse)
|
|
|
|
|
{
|
|
|
|
|
response.AsText(string.Join("<br>", print));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public static void RootPath(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
|
|
|
|
|
{
|
2022-01-03 16:35:57 +00:00
|
|
|
|
string p = System.Web.HttpUtility.UrlDecode(args["Path"]).Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[0];
|
2021-12-12 10:04:16 +00:00
|
|
|
|
string path = Path.Combine(webSitePath, p);
|
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
string indexHtml = Path.Combine(path, "index.html");
|
|
|
|
|
if(File.Exists(indexHtml))
|
|
|
|
|
{
|
|
|
|
|
path = indexHtml;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string dir = Path.Combine(webSitePath,"err", "dir.html");
|
|
|
|
|
StringBuilder b = new StringBuilder();
|
|
|
|
|
var f= Directory.GetLastWriteTime(Path.GetDirectoryName(path));
|
|
|
|
|
string parentModified = $"{f.ToShortDateString()} {f.ToShortTimeString()}";
|
|
|
|
|
b.Append($"<tr><td><a href=\"..\">Up</a></td><td>{parentModified}</td><td>DIR</td></tr>");
|
|
|
|
|
foreach (var file in Directory.GetDirectories(path))
|
|
|
|
|
{
|
|
|
|
|
string name = Path.GetFileName(file);
|
|
|
|
|
string nameUrled = System.Web.HttpUtility.UrlEncode(name);
|
|
|
|
|
string nameHtmled = System.Web.HttpUtility.HtmlEncode(name);
|
|
|
|
|
f= Directory.GetLastWriteTime(file);
|
|
|
|
|
string dateModifed =$"{f.ToShortDateString()} {f.ToShortTimeString()}";
|
|
|
|
|
b.Append($"<tr><td><a href=\"{nameUrled}\">{nameHtmled}</a></td><td>{dateModifed}</td><td>DIR</td></tr>");
|
|
|
|
|
}
|
|
|
|
|
foreach (var file in Directory.GetFiles(path))
|
|
|
|
|
{
|
|
|
|
|
string name = Path.GetFileName(file);
|
|
|
|
|
string nameUrled = System.Web.HttpUtility.UrlEncode(name);
|
|
|
|
|
string nameHtmled = System.Web.HttpUtility.HtmlEncode(name);
|
|
|
|
|
f = File.GetLastWriteTime(file);
|
|
|
|
|
|
|
|
|
|
string dateModifed = $"{f.ToShortDateString()} {f.ToShortTimeString()}";
|
|
|
|
|
b.Append($"<tr><td><a href=\"{nameUrled}\">{nameHtmled}</a></td><td>{dateModifed}</td><td>FILE</td></tr>");
|
|
|
|
|
}
|
|
|
|
|
Dictionary<string, string> templating = new Dictionary<string, string>();
|
|
|
|
|
templating.Add("Items", b.ToString());
|
|
|
|
|
rp.AsText(Templating.RenderFile(dir, templating));
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-25 06:18:12 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
rp.AsFile(rq, path);
|
2021-08-25 09:11:08 +00:00
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
#endregion
|
2021-06-24 01:10:20 +00:00
|
|
|
|
|
2021-10-24 12:52:24 +00:00
|
|
|
|
public static bool Route_Before(HttpListenerRequest request, HttpListenerResponse response)
|
|
|
|
|
{
|
|
|
|
|
response.WithCORS();
|
2021-10-24 13:38:14 +00:00
|
|
|
|
return false;
|
2021-10-24 12:52:24 +00:00
|
|
|
|
}
|
2022-01-03 16:35:57 +00:00
|
|
|
|
public static string GetServerRoot(HttpListenerRequest req)
|
|
|
|
|
{
|
|
|
|
|
if(req.Headers.AllKeys.Contains("ServerRoot"))
|
|
|
|
|
{
|
|
|
|
|
return req.Headers["ServerRoot"];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return $"http://{req.LocalEndPoint.ToString()}/";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-10-24 12:52:24 +00:00
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|