refactor Program.cs, add file download support

This commit is contained in:
Michael Nolan 2021-09-25 17:30:16 -05:00
parent ec069264f1
commit 81da4967f5
9 changed files with 732 additions and 418 deletions

View File

@ -1,28 +1,27 @@
<Properties StartupConfiguration="{E26F8159-6B4B-4660-A7A4-D0333DFEF0DD}|Default" NuGet.AddPackagesDialog.IncludePrerelease="True"> <Properties StartupConfiguration="{E26F8159-6B4B-4660-A7A4-D0333DFEF0DD}|Default" NuGet.AddPackagesDialog.IncludePrerelease="True">
<MonoDevelop.Ide.Workbench> <MonoDevelop.Ide.Workbench ActiveDocument="Program.cs">
<Files> <Files>
<File FileName="Program.cs" Line="13" Column="1" /> <File FileName="Program.cs" Line="253" Column="7" />
<File FileName="Server/Models/InfoType.cs" Line="42" Column="60" /> <File FileName="Server/Models/InfoType.cs" Line="45" Column="39" />
<File FileName="Server/Models/YoutubeDownloaderResponse.cs" Line="1" Column="1" /> <File FileName="Server/Models/YoutubeDownloaderResponse.cs" Line="1" Column="1" />
<File FileName="Server/Functions/Downloader.cs" Line="43" Column="17" /> <File FileName="Server/Functions/Downloader.cs" Line="642" Column="24" />
<File FileName="Server/Functions/SavedMedia.cs" Line="1" Column="1" /> <File FileName="Server/Functions/SavedMedia.cs" Line="1" Column="1" />
<File FileName="Server/Models/SavedVideo.cs" Line="7" Column="29" />
<File FileName="Server/Models/InfomationQueueItem.cs" Line="25" Column="27" />
<File FileName="Server/Functions/ffmpeg.cs" Line="1" Column="1" />
</Files> </Files>
<Pads> <Pads>
<Pad Id="ProjectPad"> <Pad Id="ProjectPad">
<State name="__root__"> <State name="__root__">
<Node name="youtube-downloader" expanded="True"> <Node name="youtube-downloader" expanded="True">
<Node name="youtube-downloader" expanded="True"> <Node name="youtube-downloader" expanded="True">
<Node name="References" expanded="True"> <Node name="References" expanded="True" />
<Node name="From Packages" expanded="True">
<Node name="CookiesTxtParser" selected="True" />
</Node>
</Node>
<Node name="Packages" expanded="True" />
<Node name="Properties" expanded="True" /> <Node name="Properties" expanded="True" />
<Node name="Server" expanded="True"> <Node name="Server" expanded="True">
<Node name="Functions" expanded="True" /> <Node name="Functions" expanded="True" />
<Node name="Models" expanded="True" /> <Node name="Models" expanded="True" />
</Node> </Node>
<Node name="Program.cs" selected="True" />
</Node> </Node>
</Node> </Node>
</State> </State>
@ -33,9 +32,7 @@
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
<MonoDevelop.Ide.ItemProperties.youtube-downloader PreferredExecutionTarget="MonoDevelop.Default" /> <MonoDevelop.Ide.ItemProperties.youtube-downloader PreferredExecutionTarget="MonoDevelop.Default" />
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore> <BreakpointStore />
<Breakpoint file="/home/ddlovato/site/Server/Functions/Downloader.cs" relfile="Server/Functions/Downloader.cs" line="34" column="1" />
</BreakpointStore>
</MonoDevelop.Ide.DebuggingService.Breakpoints> </MonoDevelop.Ide.DebuggingService.Breakpoints>
<MultiItemStartupConfigurations /> <MultiItemStartupConfigurations />
</Properties> </Properties>

View File

@ -20,7 +20,7 @@ namespace youtube_downloader
class Program class Program
{ {
static string webSitePath;
static void Main(string[] arg) static void Main(string[] arg)
{ {
@ -36,205 +36,59 @@ namespace youtube_downloader
// we need to get our app name so that // we need to get our app name so that
// we can create unique names for our mutex and our pipe // we can create unique names for our mutex and our pipe
string webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite"); webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite");
/*Route.Add("/api/endpoint/", (rq, rp, args) =>
{
rp.AsText(HandleRequest(rq.InputStream,rq.CurrentEncoding), "application/json");
},
"POST");*/
Route.Add("/api/upload/", (rq,rp,args) => {
var files = rq.ParseBody(args); /* Generic */
foreach (var f in files.Values)
f.Save(Path.Combine(webSitePath,f.FileName));
rp.AsText("uploaded", "text/plain");
},"POST");
Route.Add("/api/AddItems", (HttpAction)AddItems, "POST"); Route.Add("/api/AddItems", (HttpAction)AddItems, "POST");
Route.Add("/", (rq, rp, args) => Route.Add("/api/AddFile/{Url}", (HttpAction)AddFile);
{ Route.Add("/api/AddCaptions/{Id}", (HttpAction)AddCaptions);
rp.AsFile(rq, Path.Combine(webSitePath, "index.html"));
});
Route.Add("/api/Search/{text}", (rq, rp, args) =>
{
string search = System.Web.HttpUtility.UrlDecode(args["text"]);
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search));
rp.AsText(json, "application/json");
}); /* Videos */
Route.Add("/api/SearchOnly/{text}", (rq, rp, args) => Route.Add("/api/AddVideoInfo/{Id}", (HttpAction)AddVideoInfo);
{ Route.Add("/api/AddVideo/{Id}", (HttpAction)AddVideo);
string search = System.Web.HttpUtility.UrlDecode(args["text"]); Route.Add("/api/AddVideoRes/{R}/{Id}", (HttpAction)AddVideoRes);
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search,false)); Route.Add("/api/Redownload", (HttpAction)Redownload);
rp.AsText(json, "application/json"); Route.Add("/api/Redownload/{R}", (HttpAction)RedownloadRes);
Route.Add("/api/Watch/{VideoId}", (HttpAction)Watch);
}); /* Playlist */
Route.Add("/api/Storage/GetDirectories/{Path}", (rq, rp, args) => Route.Add("/api/AddPlaylistOnly/{Id}", (HttpAction)AddPlaylistOnly);
{ Route.Add("/api/AddPlaylist/{Id}", (HttpAction)AddPlaylist);
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]); Route.Add("/api/AddPlaylistRes/{R}/{Id}", (HttpAction)AddPlaylistRes);
if (Directory.Exists(path)) /* Search */
{ Route.Add("/api/SearchOnly/{text}", (HttpAction)SearchOnly);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateDirectories(path).Select<string, string>((o) => { return Path.GetFileName(o); })); Route.Add("/api/Search/{text}", (HttpAction)Search);
rp.AsText(json, "application/json");
}
else
{
rp.AsText("[]", "application/json");
}
});
Route.Add("/api/Storage/GetFiles/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
if (Directory.Exists(path)) /* Channel */
{ Route.Add("/api/AddChannelOnly/{Id}", (HttpAction)AddChannelOnly);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateFiles(path).Select<string, string>((o) => { return Path.GetFileName(o); })); Route.Add("/api/AddChannel/{Id}", (HttpAction)AddChannel);
rp.AsText(json, "application/json"); Route.Add("/api/AddChannelRes/{R}/{Id}", (HttpAction)AddChannelRes);
}
else
{
rp.AsText("[]", "application/json");
}
});
Route.Add("/api/Storage/FileExists/{Path}", (rq, rp, args) => /* User */
{ Route.Add("/api/AddUserOnly/{Id}", (HttpAction)AddUserOnly);
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]); Route.Add("/api/AddUser/{Id}", (HttpAction)AddUser);
string json = File.Exists(path) ? "true" : "false"; Route.Add("/api/AddUserRes/{R}/{Id}", (HttpAction)AddUserRes);
rp.AsText(json, "text/plain");
}); /* Queue and Progress */
Route.Add("/api/Storage/DirectoryExists/{Path}", (rq, rp, args) => Route.Add("/api/QueueList", (HttpAction)QueueList);
{ Route.Add("/api/QueueMove/{From}/{To}", (HttpAction)QueueMove);
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]); Route.Add("/api/QueueMove2/{To}/{Id}", (HttpAction)QueueMove2);
string json = Directory.Exists(path) ? "true" : "false"; Route.Add("/api/Progress", (HttpAction)VideoProgress);
rp.AsText(json, "text/plain");
}); /* Storage */
Route.Add("/api/Storage/File/{Path}", (rq, rp, args) => Route.Add("/api/Storage/GetDirectories/{Path}", (HttpAction)StorageGetDirectories);
{ Route.Add("/api/Storage/GetFiles/{Path}", (HttpAction)StorageGetFiles);
if (args["path"].StartsWith("config/")) Route.Add("/api/Storage/DirectoryExists/{Path}", (HttpAction)StorageDirectoryExists);
{ Route.Add("/api/Storage/FileExists/{Path}", (HttpAction)StorageFileExists);
rp.AsText("Access denied"); Route.Add("/api/Storage/File/{Path}", (HttpAction)StorageFile);
} Route.Add("/api/upload/", (HttpAction)UploadFiles, "POST");
else
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
rp.AsFile(rq, path); /* Other */
} Route.Add("/", (HttpAction)Index);
}); Route.Add("/{Path}", (HttpAction)RootPath);
Route.Add("/api/AddVideo/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddVideoRes/{R}/{Id}", (rq, rp, args) =>
{
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("/");
});
Route.Add("/api/AddUserOnly/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadUserOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddPlaylistOnly/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadPlaylistOnly(System.Web.HttpUtility.UrlDecode(args["Id"]),Resolution.NoConvert);
rp.AsRedirect("/");
});
Route.Add("/api/AddVideoInfo/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadVideoInfo(System.Web.HttpUtility.UrlDecode(args["Id"]),Resolution.NoConvert);
rp.AsRedirect("/");
});
Route.Add("/api/AddUserRes/{R}/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.DownloadUser(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
rp.AsRedirect("/");
});
Route.Add("/api/QueueMove/{From}/{To}", (rq, rp, args) =>
{
Server.Functions.Downloader.ModQueue(args["To"], args["From"]);
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");
});
Route.Add("/api/Watch/{VideoId}", (rq, rp, args) =>
{
var txt = Templating.RenderFile(Path.Combine(webSitePath, "watch_page.thtml"), args); //populate template
rp.AsText(txt);
});
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("/");
});
Route.Add("/{Path}", (rq, rp, args) =>
{
string path = Path.Combine(webSitePath, args["Path"].Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[0]);
rp.AsFile(rq, path);
});
Console.WriteLine("Almost Ready To Listen"); Console.WriteLine("Almost Ready To Listen");
if (arg.Length > 0) if (arg.Length > 0)
@ -249,6 +103,7 @@ namespace youtube_downloader
} }
} }
#region Generic
private static void AddItems(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments) private static void AddItems(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
{ {
var f = request.ParseBody(arguments); var f = request.ParseBody(arguments);
@ -265,17 +120,226 @@ namespace youtube_downloader
} }
/* public static void AddFile(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
private static string HandleRequest(Stream inputStream, Encoding currentEncoding)
{ {
using (var streamReader = new StreamReader(inputStream, currentEncoding)) Server.Functions.Downloader.DownloadFile(arguments["Url"]);
{
return Newtonsoft.Json.JsonConvert.SerializeObject(HandleRequest(Newtonsoft.Json.JsonConvert.DeserializeObject<YoutubeDownloaderRequest>(streamReader.ReadToEnd()));
} }
} public static void AddCaptions(HttpListenerRequest request, HttpListenerResponse response, Dictionary<string, string> arguments)
private static YoutubeDownloaderResponse HandleRequest(YoutubeDownloaderRequest data)
{ {
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
}*/
} }
} }

View File

@ -56,7 +56,7 @@ namespace youtube_downloader.Server.Functions
static HttpClient Http; static HttpClient Http;
internal YoutubeClient ytc = CreateYoutubeClient(); internal YoutubeClient ytc = CreateYoutubeClient();
static VideoDownloadProgress P = new VideoDownloadProgress(); static VideoDownloadProgress P = new VideoDownloadProgress();
Progress<double> DownloadP =new Progress<double>( (e)=> { P.Progress = (int)(e * 100.0); }); Progress<double> DownloadP = new Progress<double>((e) => { P.Progress = (int)(e * 100.0); P.ProgressRaw = e; });
public List<SavedVideoObject> Queue = new List<SavedVideoObject>(); public List<SavedVideoObject> Queue = new List<SavedVideoObject>();
@ -106,6 +106,95 @@ namespace youtube_downloader.Server.Functions
} }
} while (true); } while (true);
} }
internal static void ModQueue2(string mvto, string index)
{
try
{
//?mv=up|down|top|bottom|remove,int&i=videoId;URL
lock (DL.Queue)
{
int index2 = -1;
for(int i=0;i<DL.Queue.Count;i++)
{
if(DL.Queue[i].Video.Id == index)
{
index2 = i;
break;
}
}
if(index2 < 0)
{
return;
}
if (mvto == "top")
{
var v = DL.Queue[index2];
DL.Queue.Remove(v);
DL.Queue.Insert(0, v);
}
else if (mvto == "bottom")
{
var v = DL.Queue[index2];
DL.Queue.Remove(v);
DL.Queue.Add(v);
}
else if (mvto == "remove")
{
var v = DL.Queue[index2];
DL.Queue.Remove(v);
}
else if (mvto == "up")
{
if (index2 > 0)
{
var v = DL.Queue[index2];
DL.Queue.Remove(v);
DL.Queue.Insert(index2 - 1, v);
}
}
else if (mvto == "down")
{
if (index2 < DL.Queue.Count - 1)
{
var v = DL.Queue[index2];
DL.Queue.Remove(v);
DL.Queue.Insert(index2 + 1, v);
}
}
else
{
int n1;
if (int.TryParse(mvto, out n1))
{
var v = DL.Queue[index2];
DL.Queue.Remove(v);
if (n1 > index2)
{
DL.Queue.Insert(n1 - 1, v);
}
else
{
DL.Queue.Insert(n1, v);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
_ = ex;
}
}
internal static void ModQueue(string mvto, string index) internal static void ModQueue(string mvto, string index)
{ {
try try
@ -286,6 +375,17 @@ namespace youtube_downloader.Server.Functions
} }
} }
} }
public bool FileExists(string nameSrc,ref string nameDest,ref int i)
{
if(i == 1)
{
i++;
return File.Exists(nameSrc);
}
i++;
nameDest= Path.Combine(Path.GetDirectoryName(nameSrc), $"{Path.GetFileNameWithoutExtension(nameSrc)} ({i}){Path.GetExtension(nameSrc)}");
return File.Exists(nameDest);
}
public async Task DownloadThread() public async Task DownloadThread()
{ {
do do
@ -300,8 +400,16 @@ namespace youtube_downloader.Server.Functions
v = Queue[0]; v = Queue[0];
Queue.RemoveAt(0); Queue.RemoveAt(0);
P.Saved = v.Video; P.Saved = v.Video;
if (v.RegularFile)
{
Console.WriteLine($"Download: {v.Video.Id}");
}
else
{
Console.WriteLine($"Download: {v.Video.Title}"); Console.WriteLine($"Download: {v.Video.Title}");
} }
}
else else
{ {
v = null; v = null;
@ -311,6 +419,64 @@ namespace youtube_downloader.Server.Functions
if (canDownload) if (canDownload)
{ {
try try
{
if (v.RegularFile) {
var req=await Http.GetAsync(v.Video.Id);
long? Len=req.Content.Headers.ContentLength;
Uri u = new Uri(v.Video.Id);
string abs = u.AbsolutePath;
string name = "file.bin";
if(abs.Contains("/"))
{
name=abs.Substring(abs.LastIndexOf('/') + 1);
}
if (req.Content.Headers.Contains("Content-Disposition"))
{
name=req.Content.Headers.ContentDisposition.FileName;
}
int fileI=1;
P.Saved.Title = name;
name=GetPath(true, "Download", name);
string filename=name;
while (FileExists(name, ref filename, ref fileI)) ;
long Len2 = long.MaxValue;
if(Len.HasValue)
{
if(Len.Value > 0)
{
Len2 = Len.Value;
}
}
P.Length = Len2;
long Pos = 0;
byte[] buffer = new byte[4096];
int Cycles = 0;
IProgress<double> p = DownloadP;
int CYCLES_BETWEEN_REPORT = 5;
using (var srcFile = await req.Content.ReadAsStreamAsync())
{
using (var destFile = File.Create(filename))
{
int read;
do
{
read = await srcFile.ReadAsync(buffer, 0, buffer.Length);
await destFile.WriteAsync(buffer, 0, read);
Pos += read;
Cycles++;
if (Cycles > CYCLES_BETWEEN_REPORT)
{
Cycles = 0;
p.Report(Pos / Len2);
}
} while (read > 0);
}
}
// DownloadP.
}
else
{ {
switch (v.Resolution) switch (v.Resolution)
{ {
@ -507,6 +673,7 @@ namespace youtube_downloader.Server.Functions
} }
break; break;
} }
}
} catch (Exception ex) } catch (Exception ex)
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
@ -603,8 +770,18 @@ namespace youtube_downloader.Server.Functions
{ {
return DL.Queue; return DL.Queue;
} }
public static void DownloadFile(string id)
{
InfomationQueueItem item = new InfomationQueueItem(new Uri(id));
lock (DL.infoQueue)
{
DL.infoQueue.Insert(0, item);
}
}
public static void DownloadVideo(string id, Resolution res) public static void DownloadVideo(string id, Resolution res)
{ {
VideoId? v = VideoId.TryParse(id); VideoId? v = VideoId.TryParse(id);
@ -634,6 +811,18 @@ namespace youtube_downloader.Server.Functions
{ {
DownloadVideo(v, Resolution.NoConvert); DownloadVideo(v, Resolution.NoConvert);
} }
public static void DownloadCaptions(string id)
{
VideoId? v = VideoId.TryParse(id);
if(v.HasValue)
{
InfomationQueueItem item = new InfomationQueueItem(v.Value);
lock (DL.infoQueue)
{
DL.infoQueue.Insert(0, item);
}
}
}
public static void DownloadPlaylist(string id,Resolution res) public static void DownloadPlaylist(string id,Resolution res)
{ {
PlaylistId? v = PlaylistId.TryParse(id); PlaylistId? v = PlaylistId.TryParse(id);

View File

@ -10,7 +10,8 @@ namespace youtube_downloader.Server.Models
Playlist=1, Playlist=1,
Channel=2, Channel=2,
User=3, User=3,
ClosedCaptions=4 ClosedCaptions=4,
FileDownload=5
} }
public class IDResolutionTypeTriplet public class IDResolutionTypeTriplet
{ {
@ -40,6 +41,9 @@ namespace youtube_downloader.Server.Models
UserName user = Id; UserName user = Id;
return new InfomationQueueItem(user, Resolution, download); return new InfomationQueueItem(user, Resolution, download);
case InfoType.ClosedCaptions:
VideoId vid2 = Id;
return new InfomationQueueItem(vid2);
} }
return null; return null;

View File

@ -22,6 +22,20 @@ namespace youtube_downloader.Server.Models
Type = InfoType.Video; Type = InfoType.Video;
this.res = res; this.res = res;
} }
public InfomationQueueItem(VideoId id)
{
Data = id.Value;
DownloadActualDataAfterwards = false;
Type = InfoType.ClosedCaptions;
this.res = 0;
}
public InfomationQueueItem(IDResolutionTypeTriplet triplet)
{
Data = triplet.Id;
DownloadActualDataAfterwards = true;
Type = triplet.Type;
this.res = triplet.Resolution;
}
public InfomationQueueItem(PlaylistId id,Resolution res,bool download = true) public InfomationQueueItem(PlaylistId id,Resolution res,bool download = true)
{ {
Data = id.Value; Data = id.Value;
@ -29,6 +43,13 @@ namespace youtube_downloader.Server.Models
Type = InfoType.Playlist; Type = InfoType.Playlist;
this.res = res; this.res = res;
} }
public InfomationQueueItem(Uri url)
{
Data = url.ToString();
DownloadActualDataAfterwards = true;
Type = InfoType.FileDownload;
this.res = 0;
}
public InfomationQueueItem(ChannelId id, Resolution res, bool download = true) public InfomationQueueItem(ChannelId id, Resolution res, bool download = true)
{ {
Data = id.Value; Data = id.Value;
@ -61,6 +82,25 @@ namespace youtube_downloader.Server.Models
{ {
switch (Type) switch (Type)
{ {
case InfoType.FileDownload:
return new SavedVideoObject[] { new SavedVideoObject(Data) };
case InfoType.ClosedCaptions:
string path = Functions.Downloader.DL.GetPath(true, "ClosedCaptions", Data);
if (!Directory.Exists(path))
{
var cc0 = await Functions.Downloader.DL.ytc.Videos.ClosedCaptions.GetManifestAsync(Data);
string trackInfo = Functions.Downloader.DL.GetPath(true, "ClosedCaptions", Data,"track_info.json");
File.WriteAllText(trackInfo, JsonConvert.SerializeObject(cc0.Tracks));
foreach (var track in cc0.Tracks)
{
string trackType = track.IsAutoGenerated ? "auto" : "manu";
string langFileName = $"{trackType}_{track.Language.Code}.srt";
string track2 = Functions.Downloader.DL.GetPath(true, "ClosedCaptions", Data,langFileName);
await Functions.Downloader.DL.ytc.Videos.ClosedCaptions.DownloadAsync(track, track2);
}
}
break;
case InfoType.Video: case InfoType.Video:
{ {
string infPath = Functions.Downloader.DL.GetPath(true, "Info", Data + ".json"); string infPath = Functions.Downloader.DL.GetPath(true, "Info", Data + ".json");

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using youtube_downloader.Server.Functions; using youtube_downloader.Server.Functions;
using YoutubeExplode.Videos;
namespace TessesYoutubeDownloader.Server.Models namespace TessesYoutubeDownloader.Server.Models
{ {
@ -14,8 +14,28 @@ namespace TessesYoutubeDownloader.Server.Models
NoConvert=1, NoConvert=1,
Audio=2 Audio=2
} }
public struct ClosedCaptions
{
public ClosedCaptions(VideoId id)
{
VideoId = id;
}
public VideoId VideoId;
}
public class SavedVideoObject public class SavedVideoObject
{ {
public SavedVideoObject()
{
RegularFile = false;
}
public SavedVideoObject(string url)
{
RegularFile = true;
Video = new SavedVideo() { Id = url };
}
public bool RegularFile { get; set; }
public SavedVideo Video { get; set; } public SavedVideo Video { get; set; }
public Resolution Resolution { get; set; } public Resolution Resolution { get; set; }
} }

Binary file not shown.