diff --git a/.vs/youtube-downloader/xs/UserPrefs.xml b/.vs/youtube-downloader/xs/UserPrefs.xml index cb30664..fed214a 100644 --- a/.vs/youtube-downloader/xs/UserPrefs.xml +++ b/.vs/youtube-downloader/xs/UserPrefs.xml @@ -1,14 +1,14 @@  - + - + - + diff --git a/Program.cs b/Program.cs index d8a8ff9..c3559e3 100644 --- a/Program.cs +++ b/Program.cs @@ -17,6 +17,42 @@ using youtube_downloader.Server.Models; namespace youtube_downloader { + public class SSE + { + public static SSE ServerSentEventItem = new SSE(); + List Streams = new List(); + public void RegisterStreamWriter(StreamWriter rp) + { + Streams.Add(rp); + } + public async Task PushEventAsync(string event_name,object data) + { + await PushEventAsync(event_name, JsonConvert.SerializeObject(data)); + } + public async Task PushEventAsync(string event_name,string data) + { + await PushEventRawAsync($"event: {event_name}\ndata: {data}"); + + } + public async Task PushEventRawAsync(string raw_data) + { + foreach (var items in Streams) + { + try + { + if(items.BaseStream != null) + { + items.Write(raw_data); + items.Write("\n\n"); + } + } + catch (Exception ex) + { + _ = ex; + } + } + } + } class Program { @@ -78,6 +114,7 @@ namespace youtube_downloader Route.Add("/api/QueueMove/{From}/{To}", (HttpAction)QueueMove); Route.Add("/api/QueueMove2/{To}/{Id}", (HttpAction)QueueMove2); Route.Add("/api/Progress", (HttpAction)VideoProgress); + Route.Add("/api/SSE", (HttpAction)ServerSentEvents); /* Storage */ Route.Add("/api/Storage/GetDirectories/{Path}", (HttpAction)StorageGetDirectories); @@ -267,6 +304,16 @@ namespace youtube_downloader { string json = JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress()); rp.AsText(json, "application/json"); + } + public static void ServerSentEvents(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary args) + { + rp.AddHeader("Cache-Control", "no-cache"); + rp.ContentType = "text/event-stream"; + StreamWriter w = new StreamWriter(rp.OutputStream, rp.ContentEncoding); + SSE.ServerSentEventItem.RegisterStreamWriter(w); + + + } #endregion #region Storage diff --git a/Server/Functions/Downloader.cs b/Server/Functions/Downloader.cs index 38491cd..986256d 100644 --- a/Server/Functions/Downloader.cs +++ b/Server/Functions/Downloader.cs @@ -56,8 +56,17 @@ namespace youtube_downloader.Server.Functions static HttpClient Http; internal YoutubeClient ytc = CreateYoutubeClient(); static VideoDownloadProgress P = new VideoDownloadProgress(); - Progress DownloadP = new Progress((e) => { P.Progress = (int)(e * 100.0); P.ProgressRaw = e; }); + const int NUM_WAITS = 10; + static int WAITS = 0; + Progress DownloadP = new Progress((e) => { P.Progress = (int)(e * 100.0); P.ProgressRaw = e; SendProgress(); }); + public static void SendProgress() + { + + if (WAITS++ < NUM_WAITS) + return; + SSE.ServerSentEventItem.PushEventAsync("progress", P).Wait(); + } public List Queue = new List(); internal static string GetQueue() @@ -409,6 +418,7 @@ namespace youtube_downloader.Server.Functions Console.WriteLine($"Download: {v.Video.Title}"); } + } else { @@ -418,10 +428,11 @@ namespace youtube_downloader.Server.Functions if (canDownload) { + try { if (v.RegularFile) { - + await SSE.ServerSentEventItem.PushEventAsync("start_dl_file", P); var req=await Http.GetAsync(v.Video.Id); long? Len=req.Content.Headers.ContentLength; Uri u = new Uri(v.Video.Id); @@ -478,6 +489,7 @@ namespace youtube_downloader.Server.Functions } else { + await SSE.ServerSentEventItem.PushEventAsync("start_dl_vid", P); switch (v.Resolution) { case Resolution.Convert: @@ -673,6 +685,7 @@ namespace youtube_downloader.Server.Functions } break; } + await SSE.ServerSentEventItem.PushEventAsync("done", v.Video); ffmpeg.on_video_done(v.Video.Id, (int)v.Resolution); } } catch (Exception ex) diff --git a/Server/Functions/ffmpeg.cs b/Server/Functions/ffmpeg.cs index 35aa49c..647c4e5 100644 --- a/Server/Functions/ffmpeg.cs +++ b/Server/Functions/ffmpeg.cs @@ -25,9 +25,11 @@ namespace youtube_downloader.Server.Functions string path_to_video_id = Path.Combine(Downloader.DL.StorageLocation,"Info",$"{id}.json"); Directory.CreateDirectory("config"); + string vdone= Path.Combine(Environment.CurrentDirectory, "config", "done"); try { + using (var p = new Process()) { p.StartInfo.FileName = vdone; diff --git a/bin/Release/youtube-downloader.exe b/bin/Release/youtube-downloader.exe index c5bc5d6..7c01e7f 100644 Binary files a/bin/Release/youtube-downloader.exe and b/bin/Release/youtube-downloader.exe differ diff --git a/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache b/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache index f49390d..01a8130 100644 Binary files a/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache and b/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache differ diff --git a/obj/x86/Release/youtube-downloader.exe b/obj/x86/Release/youtube-downloader.exe index c5bc5d6..7c01e7f 100644 Binary files a/obj/x86/Release/youtube-downloader.exe and b/obj/x86/Release/youtube-downloader.exe differ