From 1cd7375fe5207d14af5e17e0fb5f5bb29aa425d5 Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Tue, 10 May 2022 07:57:52 -0500 Subject: [PATCH] Added Subscriptions --- Tesses.YouTubeDownloader.Server/Class1.cs | 54 ++++++++++++--- .../Tesses.YouTubeDownloader.Server.csproj | 6 +- Tesses.YouTubeDownloader/IDownloader.cs | 5 ++ Tesses.YouTubeDownloader/SubscribedTo.cs | 27 ++++++-- Tesses.YouTubeDownloader/TYTDClient.cs | 65 ++++++++++++++++++- .../Tesses.YouTubeDownloader.csproj | 6 +- 6 files changed, 143 insertions(+), 20 deletions(-) diff --git a/Tesses.YouTubeDownloader.Server/Class1.cs b/Tesses.YouTubeDownloader.Server/Class1.cs index 119f931..ca84bfd 100644 --- a/Tesses.YouTubeDownloader.Server/Class1.cs +++ b/Tesses.YouTubeDownloader.Server/Class1.cs @@ -335,7 +335,23 @@ namespace Tesses.YouTubeDownloader.Server Add("/QueueList",QueueList); Add("/subscribe",Subscribe); Add("/resubscribe",Resubscribe); + Add("/unsubscribe",Unsubscribe); + Add("/subscriptions",Subscriptions); + } + public async Task Subscriptions(ServerContext ctx) + { + TYTDStorage storage = Downloader as TYTDStorage; + if(storage != null) + { + + + var sub=storage.GetLoadedSubscriptions(); + await ctx.SendJsonAsync(sub); + + + } + } public async Task Resubscribe(ServerContext ctx) { @@ -362,12 +378,7 @@ namespace Tesses.YouTubeDownloader.Server if(cid.HasValue) { - var sub=storage.GetSubscription(cid.Value); - if(sub != null) - { - sub.BellInfo = conf; - await storage.SaveSubscription(sub); - } + await storage.ResubscribeAsync(cid.Value,conf); } } } @@ -375,6 +386,33 @@ namespace Tesses.YouTubeDownloader.Server $"You Will Be Redirected in 5 Sec

You Will Be Redirected in 5 Sec

\n" ); } + + public async Task Unsubscribe(ServerContext ctx) + { + TYTDStorage storage = Downloader as TYTDStorage; + if(storage != null) + { + string id; + + if(ctx.QueryParams.TryGetFirst("id",out id)) + { + + + + ChannelId? cid=ChannelId.TryParse(WebUtility.UrlDecode(id)); + + if(cid.HasValue) + { + + storage.Unsubscribe(cid.Value); + + } + } + } + await ctx.SendTextAsync( + $"You Will Be Redirected in 5 Sec

You Will Be Redirected in 5 Sec

\n" + ); + } public async Task Subscribe(ServerContext ctx) { TYTDStorage storage = Downloader as TYTDStorage; @@ -408,10 +446,10 @@ namespace Tesses.YouTubeDownloader.Server if(cid.HasValue) { - await storage.Subscribe(cid.Value,getinfo,conf); + await storage.SubscribeAsync(cid.Value,getinfo,conf); }else{ UserName? uname=UserName.TryParse(WebUtility.UrlDecode(id)); - await storage.Subscribe(uname.Value,conf); + await storage.SubscribeAsync(uname.Value,conf); } } diff --git a/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj b/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj index 9553270..f8f152f 100644 --- a/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj +++ b/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj @@ -15,9 +15,9 @@ Tesses.YouTubeDownloader.Server Mike Nolan Tesses - 1.0.2.0 - 1.0.2.0 - 1.0.2.0 + 1.0.2.1 + 1.0.2.1 + 1.0.2.1 Adds WebServer to TYTD LGPL-3.0-only true diff --git a/Tesses.YouTubeDownloader/IDownloader.cs b/Tesses.YouTubeDownloader/IDownloader.cs index e6b7ecb..a28e0ae 100644 --- a/Tesses.YouTubeDownloader/IDownloader.cs +++ b/Tesses.YouTubeDownloader/IDownloader.cs @@ -20,5 +20,10 @@ namespace Tesses.YouTubeDownloader IReadOnlyList<(SavedVideo Video,Resolution Resolution)> GetQueueList(); SavedVideoProgress GetProgress(); + IAsyncEnumerable GetSubscriptionsAsync(); + Task UnsubscribeAsync(ChannelId id); + Task SubscribeAsync(ChannelId id,bool downloadChannelInfo=false,ChannelBellInfo bellInfo = ChannelBellInfo.NotifyAndDownload); + Task SubscribeAsync(UserName name,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload); + Task ResubscribeAsync(ChannelId id,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload); } } \ No newline at end of file diff --git a/Tesses.YouTubeDownloader/SubscribedTo.cs b/Tesses.YouTubeDownloader/SubscribedTo.cs index f8a6f6f..d9c360d 100644 --- a/Tesses.YouTubeDownloader/SubscribedTo.cs +++ b/Tesses.YouTubeDownloader/SubscribedTo.cs @@ -66,7 +66,22 @@ namespace Tesses.YouTubeDownloader } - + + public async Task UnsubscribeAsync(ChannelId id) + { + await Task.Run(()=>{Unsubscribe(id);}); + } + + public async Task ResubscribeAsync(ChannelId id,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload) + { + var sub=GetSubscription(id); + if(sub != null) + { + sub.BellInfo = info; + await SaveSubscription(sub); + } + } + public DateTime LastSubscriptionTime = DateTime.MinValue; public async Task HandleSubscriptions() { @@ -99,7 +114,7 @@ namespace Tesses.YouTubeDownloader /// /// Subscribe to creator /// - public async Task Subscribe(ChannelId id, bool downloadChannelInfo=false,ChannelBellInfo bellInfo = ChannelBellInfo.NotifyAndDownload) + public async Task SubscribeAsync(ChannelId id, bool downloadChannelInfo=false,ChannelBellInfo bellInfo = ChannelBellInfo.NotifyAndDownload) { if(downloadChannelInfo) { @@ -117,11 +132,15 @@ namespace Tesses.YouTubeDownloader { await WriteAllTextAsync($"Subscriptions/{sub.Id}",JsonConvert.SerializeObject(sub)); } - public async Task Subscribe(UserName name,ChannelBellInfo bellInfo=ChannelBellInfo.NotifyAndDownload) + public async Task SubscribeAsync(UserName name,ChannelBellInfo bellInfo=ChannelBellInfo.NotifyAndDownload) { ChannelMediaContext context=new ChannelMediaContext(name,Resolution.NoDownload); var c=await context.GetChannel(this); - await Subscribe(ChannelId.Parse(c.Id),false,bellInfo); + await SubscribeAsync(ChannelId.Parse(c.Id),false,bellInfo); + } + public IReadOnlyList GetLoadedSubscriptions() + { + return Subscriptions; } public void Unsubscribe(ChannelId id) { diff --git a/Tesses.YouTubeDownloader/TYTDClient.cs b/Tesses.YouTubeDownloader/TYTDClient.cs index d2cbdd0..93b66bd 100644 --- a/Tesses.YouTubeDownloader/TYTDClient.cs +++ b/Tesses.YouTubeDownloader/TYTDClient.cs @@ -11,6 +11,7 @@ using System.IO; using YoutubeExplode.Channels; using YoutubeExplode.Playlists; using System.Net.Http; +using System.Net; namespace Tesses.YouTubeDownloader { @@ -111,7 +112,67 @@ namespace Tesses.YouTubeDownloader } } } - + public async IAsyncEnumerable GetSubscriptionsAsync() + { + string v="[]"; + try{ + v=await client.GetStringAsync("/api/v2/subscriptions"); + }catch(Exception ex) + { + _=ex; + } + foreach(var item in JsonConvert.DeserializeObject>(v)) + { + yield return await Task.FromResult(item); + } + + + } + public async Task UnsubscribeAsync(ChannelId id) + { + try{ + string v=await client.GetStringAsync($"/api/v2/unsubscribe?id={id.Value}"); + + }catch(Exception ex) + { + _=ex; + } + } + public async Task SubscribeAsync(ChannelId id,bool downloadChannelInfo=false,ChannelBellInfo bellInfo = ChannelBellInfo.NotifyAndDownload) + { + try{ + string dlcid=downloadChannelInfo ? "true" : "false"; + string v=await client.GetStringAsync($"/api/v2/subscribe?id={id.Value}&conf={bellInfo.ToString()}&getinfo={dlcid}"); + + }catch(Exception ex) + { + _=ex; + } + } + public async Task SubscribeAsync(UserName name,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload) + { + try{ + + + string v=await client.GetStringAsync($"/api/v2/subscribe?id={ WebUtility.UrlEncode(name.Value)}&conf={info.ToString()}"); + + }catch(Exception ex) + { + _=ex; + } + } + public async Task ResubscribeAsync(ChannelId id,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload) + { + try{ + + + string v=await client.GetStringAsync($"/api/v2/resubscribe?id={id.Value}&conf={info.ToString()}"); + + }catch(Exception ex) + { + _=ex; + } + } public async override IAsyncEnumerable EnumerateFilesAsync(string path) { List items=null; @@ -208,6 +269,6 @@ namespace Tesses.YouTubeDownloader return Stream.Null; } - + } } \ No newline at end of file diff --git a/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj b/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj index 877afac..aff957d 100644 --- a/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj +++ b/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj @@ -7,9 +7,9 @@ Tesses.YouTubeDownloader Mike Nolan Tesses - 1.0.3.0 - 1.0.3.0 - 1.0.3.0 + 1.0.3.1 + 1.0.3.1 + 1.0.3.1 A YouTube Downloader LGPL-3.0-only true