Added SSE
This commit is contained in:
parent
beb3ec5426
commit
9570287c11
|
@ -1,14 +1,14 @@
|
|||
<Properties StartupConfiguration="{E26F8159-6B4B-4660-A7A4-D0333DFEF0DD}|Default" NuGet.AddPackagesDialog.IncludePrerelease="True">
|
||||
<MonoDevelop.Ide.Workbench ActiveDocument="Program.cs">
|
||||
<Files>
|
||||
<File FileName="Program.cs" Line="42" Column="11" />
|
||||
<File FileName="Program.cs" Line="81" Column="13" />
|
||||
<File FileName="Server/Models/InfoType.cs" Line="45" Column="39" />
|
||||
<File FileName="Server/Models/YoutubeDownloaderResponse.cs" Line="1" Column="1" />
|
||||
<File FileName="Server/Functions/Downloader.cs" Line="675" Column="30" />
|
||||
<File FileName="Server/Functions/Downloader.cs" Line="888" Column="8" />
|
||||
<File FileName="Server/Functions/SavedMedia.cs" Line="1" Column="1" />
|
||||
<File FileName="Server/Models/SavedVideo.cs" Line="35" Column="10" />
|
||||
<File FileName="Server/Models/InfomationQueueItem.cs" Line="25" Column="27" />
|
||||
<File FileName="Server/Functions/ffmpeg.cs" Line="30" Column="86" />
|
||||
<File FileName="Server/Functions/ffmpeg.cs" Line="30" Column="14" />
|
||||
<File FileName="../Projects/tytdtool/tytdtool/Program.cs" Line="1" Column="1" />
|
||||
<File FileName="../../../usr/lib/mono/msbuild/15.0/bin/Microsoft.Common.CurrentVersion.targets" Line="2101" Column="5" />
|
||||
</Files>
|
||||
|
|
47
Program.cs
47
Program.cs
|
@ -17,6 +17,42 @@ using youtube_downloader.Server.Models;
|
|||
|
||||
namespace youtube_downloader
|
||||
{
|
||||
public class SSE
|
||||
{
|
||||
public static SSE ServerSentEventItem = new SSE();
|
||||
List<StreamWriter> Streams = new List<StreamWriter>();
|
||||
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<string, string> 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
|
||||
|
|
|
@ -56,8 +56,17 @@ namespace youtube_downloader.Server.Functions
|
|||
static HttpClient Http;
|
||||
internal YoutubeClient ytc = CreateYoutubeClient();
|
||||
static VideoDownloadProgress P = new VideoDownloadProgress();
|
||||
Progress<double> DownloadP = new Progress<double>((e) => { P.Progress = (int)(e * 100.0); P.ProgressRaw = e; });
|
||||
const int NUM_WAITS = 10;
|
||||
static int WAITS = 0;
|
||||
Progress<double> DownloadP = new Progress<double>((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<SavedVideoObject> Queue = new List<SavedVideoObject>();
|
||||
|
||||
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)
|
||||
|
|
|
@ -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;
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue