Added Subscriptions

This commit is contained in:
Mike Nolan 2022-05-10 07:57:52 -05:00
parent 728ea65613
commit 1cd7375fe5
6 changed files with 143 additions and 20 deletions

View File

@ -335,7 +335,23 @@ namespace Tesses.YouTubeDownloader.Server
Add("/QueueList",QueueList); Add("/QueueList",QueueList);
Add("/subscribe",Subscribe); Add("/subscribe",Subscribe);
Add("/resubscribe",Resubscribe); 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) public async Task Resubscribe(ServerContext ctx)
{ {
@ -362,12 +378,7 @@ namespace Tesses.YouTubeDownloader.Server
if(cid.HasValue) if(cid.HasValue)
{ {
var sub=storage.GetSubscription(cid.Value); await storage.ResubscribeAsync(cid.Value,conf);
if(sub != null)
{
sub.BellInfo = conf;
await storage.SaveSubscription(sub);
}
} }
} }
} }
@ -375,6 +386,33 @@ namespace Tesses.YouTubeDownloader.Server
$"<html><head><title>You Will Be Redirected in 5 Sec</title><meta http-equiv=\"Refresh\" content=\"5; url='../../'\" /></head><body><h1>You Will Be Redirected in 5 Sec</h1></body></html>\n" $"<html><head><title>You Will Be Redirected in 5 Sec</title><meta http-equiv=\"Refresh\" content=\"5; url='../../'\" /></head><body><h1>You Will Be Redirected in 5 Sec</h1></body></html>\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(
$"<html><head><title>You Will Be Redirected in 5 Sec</title><meta http-equiv=\"Refresh\" content=\"5; url='../../'\" /></head><body><h1>You Will Be Redirected in 5 Sec</h1></body></html>\n"
);
}
public async Task Subscribe(ServerContext ctx) public async Task Subscribe(ServerContext ctx)
{ {
TYTDStorage storage = Downloader as TYTDStorage; TYTDStorage storage = Downloader as TYTDStorage;
@ -408,10 +446,10 @@ namespace Tesses.YouTubeDownloader.Server
if(cid.HasValue) if(cid.HasValue)
{ {
await storage.Subscribe(cid.Value,getinfo,conf); await storage.SubscribeAsync(cid.Value,getinfo,conf);
}else{ }else{
UserName? uname=UserName.TryParse(WebUtility.UrlDecode(id)); UserName? uname=UserName.TryParse(WebUtility.UrlDecode(id));
await storage.Subscribe(uname.Value,conf); await storage.SubscribeAsync(uname.Value,conf);
} }
} }

View File

@ -15,9 +15,9 @@
<PackageId>Tesses.YouTubeDownloader.Server</PackageId> <PackageId>Tesses.YouTubeDownloader.Server</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>1.0.2.0</Version> <Version>1.0.2.1</Version>
<AssemblyVersion>1.0.2.0</AssemblyVersion> <AssemblyVersion>1.0.2.1</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion> <FileVersion>1.0.2.1</FileVersion>
<Description>Adds WebServer to TYTD</Description> <Description>Adds WebServer to TYTD</Description>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression> <PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

View File

@ -20,5 +20,10 @@ namespace Tesses.YouTubeDownloader
IReadOnlyList<(SavedVideo Video,Resolution Resolution)> GetQueueList(); IReadOnlyList<(SavedVideo Video,Resolution Resolution)> GetQueueList();
SavedVideoProgress GetProgress(); SavedVideoProgress GetProgress();
IAsyncEnumerable<Subscription> 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);
} }
} }

View File

@ -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 DateTime LastSubscriptionTime = DateTime.MinValue;
public async Task HandleSubscriptions() public async Task HandleSubscriptions()
{ {
@ -99,7 +114,7 @@ namespace Tesses.YouTubeDownloader
/// <summary> /// <summary>
/// Subscribe to creator /// Subscribe to creator
/// </summary> /// </summary>
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) if(downloadChannelInfo)
{ {
@ -117,11 +132,15 @@ namespace Tesses.YouTubeDownloader
{ {
await WriteAllTextAsync($"Subscriptions/{sub.Id}",JsonConvert.SerializeObject(sub)); 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); ChannelMediaContext context=new ChannelMediaContext(name,Resolution.NoDownload);
var c=await context.GetChannel(this); var c=await context.GetChannel(this);
await Subscribe(ChannelId.Parse(c.Id),false,bellInfo); await SubscribeAsync(ChannelId.Parse(c.Id),false,bellInfo);
}
public IReadOnlyList<Subscription> GetLoadedSubscriptions()
{
return Subscriptions;
} }
public void Unsubscribe(ChannelId id) public void Unsubscribe(ChannelId id)
{ {

View File

@ -11,6 +11,7 @@ using System.IO;
using YoutubeExplode.Channels; using YoutubeExplode.Channels;
using YoutubeExplode.Playlists; using YoutubeExplode.Playlists;
using System.Net.Http; using System.Net.Http;
using System.Net;
namespace Tesses.YouTubeDownloader namespace Tesses.YouTubeDownloader
{ {
@ -111,7 +112,67 @@ namespace Tesses.YouTubeDownloader
} }
} }
} }
public async IAsyncEnumerable<Subscription> GetSubscriptionsAsync()
{
string v="[]";
try{
v=await client.GetStringAsync("/api/v2/subscriptions");
}catch(Exception ex)
{
_=ex;
}
foreach(var item in JsonConvert.DeserializeObject<List<Subscription>>(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<string> EnumerateFilesAsync(string path) public async override IAsyncEnumerable<string> EnumerateFilesAsync(string path)
{ {
List<string> items=null; List<string> items=null;
@ -208,6 +269,6 @@ namespace Tesses.YouTubeDownloader
return Stream.Null; return Stream.Null;
} }
} }
} }

View File

@ -7,9 +7,9 @@
<PackageId>Tesses.YouTubeDownloader</PackageId> <PackageId>Tesses.YouTubeDownloader</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>1.0.3.0</Version> <Version>1.0.3.1</Version>
<AssemblyVersion>1.0.3.0</AssemblyVersion> <AssemblyVersion>1.0.3.1</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion> <FileVersion>1.0.3.1</FileVersion>
<Description>A YouTube Downloader</Description> <Description>A YouTube Downloader</Description>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression> <PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>