Add enumerate video ids

This commit is contained in:
Mike Nolan 2024-12-02 16:36:27 +00:00
parent 55f62811c7
commit f8deea2c78
1 changed files with 194 additions and 185 deletions

View File

@ -1,186 +1,195 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Renci.SshNet; using Renci.SshNet;
using YoutubeExplode; using YoutubeExplode;
using YoutubeExplode.Videos; using YoutubeExplode.Videos;
using Tesses.WebServer; using Tesses.WebServer;
using static Creds; using static Creds;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using YoutubeExplode.Playlists; using YoutubeExplode.Playlists;
using YoutubeExplode.Channels; using YoutubeExplode.Channels;
using System.Linq;
HttpClient http=new HttpClient();
YoutubeClient ytc=new YoutubeClient(http); HttpClient http=new HttpClient();
ConcurrentStack<VideoId> videoIds=new ConcurrentStack<VideoId>(); YoutubeClient ytc=new YoutubeClient(http);
bool isRunning=true; ConcurrentStack<VideoId> videoIds=new ConcurrentStack<VideoId>();
bool isRunning=true;
async Task AddVideo(VideoId id)
{ async Task AddVideo(VideoId id)
if(!string.IsNullOrWhiteSpace(AlreadyExistsUrl)) {
{ if(!string.IsNullOrWhiteSpace(AlreadyExistsUrl))
try{ {
if(await http.GetStringAsync($"{AlreadyExistsUrl}{id.Value}") == "true") try{
{ if(await http.GetStringAsync($"{AlreadyExistsUrl}{id.Value}") == "true")
Console.WriteLine($"{AlreadyExistsUrl}{id.Value} returned true skipping: https://www.youtube.com/watch?v={id.Value}"); {
return; Console.WriteLine($"{AlreadyExistsUrl}{id.Value} returned true skipping: https://www.youtube.com/watch?v={id.Value}");
} return;
}catch(Exception) }
{ }catch(Exception)
{
}
} }
}
using(SshClient client = new SshClient(IP,Port,Username,Password)){
await client.ConnectAsync(default); using(SshClient client = new SshClient(IP,Port,Username,Password)){
using(var res=client.CreateCommand($"echo {DirectoryOnServer}/*-{id.Value}.mp4")) await client.ConnectAsync(default);
{ using(var res=client.CreateCommand($"echo {DirectoryOnServer}/*-{id.Value}.mp4"))
await res.ExecuteAsync(); {
await res.ExecuteAsync();
if(res.Result == $"{DirectoryOnServer}/*-{id.Value}.mp4\n")
{ if(res.Result == $"{DirectoryOnServer}/*-{id.Value}.mp4\n")
using(var res2=client.CreateCommand($"cd {DirectoryOnServer} && tytd {id.Value}")) {
{ using(var res2=client.CreateCommand($"cd {DirectoryOnServer} && tytd {id.Value}"))
await res2.ExecuteAsync(); {
Console.WriteLine($"Downloaded: https://www.youtube.com/watch?v={id.Value}"); await res2.ExecuteAsync();
_=res2.Result; Console.WriteLine($"Downloaded: https://www.youtube.com/watch?v={id.Value}");
} _=res2.Result;
} }
else }
{ else
Console.WriteLine($"Video already exists: https://www.youtube.com/watch?v={id.Value}"); {
} Console.WriteLine($"Video already exists: https://www.youtube.com/watch?v={id.Value}");
}
}
client.Disconnect(); }
} client.Disconnect();
} }
}
async Task AddItem(string url)
{ async Task AddItem(string url)
VideoId? vId = VideoId.TryParse(url); {
PlaylistId? pId = PlaylistId.TryParse(url); VideoId? vId = VideoId.TryParse(url);
ChannelId? cId = ChannelId.TryParse(url); PlaylistId? pId = PlaylistId.TryParse(url);
UserName? username = UserName.TryParse(url); ChannelId? cId = ChannelId.TryParse(url);
ChannelSlug? slug = ChannelSlug.TryParse(url); UserName? username = UserName.TryParse(url);
ChannelHandle? handle = ChannelHandle.TryParse(url); ChannelSlug? slug = ChannelSlug.TryParse(url);
ChannelHandle? handle = ChannelHandle.TryParse(url);
if(url.Length == 11 && vId.HasValue)
{ if(url.Length == 11 && vId.HasValue)
videoIds.Push(vId.Value); {
} videoIds.Push(vId.Value);
else if(pId.HasValue) }
{ else if(pId.HasValue)
await foreach(var v in ytc.Playlists.GetVideosAsync(pId.Value)) {
{ await foreach(var v in ytc.Playlists.GetVideosAsync(pId.Value))
videoIds.Push(v.Id); {
} videoIds.Push(v.Id);
} }
else if(vId.HasValue) }
{ else if(vId.HasValue)
videoIds.Push(vId.Value); {
} videoIds.Push(vId.Value);
else if(cId.HasValue) }
{ else if(cId.HasValue)
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) {
{ await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
videoIds.Push(c.Id); {
} videoIds.Push(c.Id);
} }
else if(username.HasValue) }
{ else if(username.HasValue)
cId = (await ytc.Channels.GetByUserAsync(username.Value)).Id; {
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) cId = (await ytc.Channels.GetByUserAsync(username.Value)).Id;
{ await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
videoIds.Push(c.Id); {
} videoIds.Push(c.Id);
} }
else if(slug.HasValue) }
{ else if(slug.HasValue)
cId = (await ytc.Channels.GetBySlugAsync(slug.Value)).Id; {
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) cId = (await ytc.Channels.GetBySlugAsync(slug.Value)).Id;
{ await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
videoIds.Push(c.Id); {
} videoIds.Push(c.Id);
} }
else if(handle.HasValue) }
{ else if(handle.HasValue)
cId = (await ytc.Channels.GetByHandleAsync(handle.Value)).Id; {
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) cId = (await ytc.Channels.GetByHandleAsync(handle.Value)).Id;
{ await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
videoIds.Push(c.Id); {
} videoIds.Push(c.Id);
} }
} }
}
Task.Factory.StartNew(async()=>{
while(isRunning) Task.Factory.StartNew(async()=>{
{ while(isRunning)
if(videoIds.TryPop(out var res)) {
{ if(videoIds.TryPop(out var res))
await AddVideo(res); {
} await AddVideo(res);
} }
},TaskCreationOptions.LongRunning).Wait(0); }
},TaskCreationOptions.LongRunning).Wait(0);
MountableServer msvr=new MountableServer(new StaticServer("wwwroot"));
MountableServer msvr=new MountableServer(new StaticServer("wwwroot"));
msvr.Mount("/api/AddItemRes/1/",new TYTDServer(AddItem));
msvr.Mount("/api/AddItem/",new TYTDServer(AddItem)); msvr.Mount("/api/AddItemRes/1/",new TYTDServer(AddItem));
msvr.Mount("/api/AddVideoRes/1/",new TYTDServer(videoIds)); msvr.Mount("/api/AddItem/",new TYTDServer(AddItem));
msvr.Mount("/api/AddVideo/",new TYTDServer(videoIds)); msvr.Mount("/api/AddVideoRes/1/",new TYTDServer(videoIds));
RouteServer routeServer=new RouteServer(msvr); msvr.Mount("/api/AddVideo/",new TYTDServer(videoIds));
routeServer.Add("/itemsInQueue",async(ctx)=>{ RouteServer routeServer=new RouteServer(msvr);
await ctx.SendJsonAsync(videoIds.Count); routeServer.Add("/itemsInQueue",async(ctx)=>{
}); await ctx.SendJsonAsync(videoIds.Count);
routeServer.Add("/add",async(ctx)=>{ });
if(ctx.QueryParams.TryGetFirst("v",out var v)) routeServer.Add("/queue.json", async(ctx)=>{
{ List<string> items = new List<string>();
await AddItem(v); foreach(var item in videoIds)
} {
await ctx.SendRedirectAsync("/"); items.Add(item.Value);
}); }
routeServer.StartServer(3252); await ctx.SendJsonAsync(items);
});
public class TYTDServer : Server routeServer.Add("/add",async(ctx)=>{
{ if(ctx.QueryParams.TryGetFirst("v",out var v))
bool onlyVideos; {
ConcurrentStack<VideoId>? stk; await AddItem(v);
}
Func<string,Task>? cb; await ctx.SendRedirectAsync("/");
public TYTDServer(ConcurrentStack<VideoId> stk) });
{ routeServer.StartServer(3252);
onlyVideos=true;
this.stk = stk; public class TYTDServer : Server
} {
bool onlyVideos;
public TYTDServer(Func<string,Task> cb) ConcurrentStack<VideoId>? stk;
{
onlyVideos=false; Func<string,Task>? cb;
this.cb = cb; public TYTDServer(ConcurrentStack<VideoId> stk)
} {
public override async Task GetAsync(ServerContext ctx) onlyVideos=true;
{ this.stk = stk;
}
if(onlyVideos)
{ public TYTDServer(Func<string,Task> cb)
{
string url=ctx.UrlAndQuery.Substring(1); onlyVideos=false;
this.cb = cb;
stk?.Push(url); }
await ctx.SendTextAsync("<script>window.history.back();</script>"); public override async Task GetAsync(ServerContext ctx)
} {
else
{ if(onlyVideos)
if(cb == null) { {
await ctx.SendTextAsync("<script>window.history.back();</script>");
string url=ctx.UrlAndQuery.Substring(1);
return;
} stk?.Push(url);
string url=ctx.UrlAndQuery.Substring(1); await ctx.SendTextAsync("<script>window.history.back();</script>");
}
await cb(url); else
await ctx.SendTextAsync("<script>window.history.back();</script>"); {
if(cb == null) {
} await ctx.SendTextAsync("<script>window.history.back();</script>");
}
return;
}
string url=ctx.UrlAndQuery.Substring(1);
await cb(url);
await ctx.SendTextAsync("<script>window.history.back();</script>");
}
}
} }