Throttle YT

This commit is contained in:
Mike Nolan 2025-02-12 09:17:16 -06:00
parent 947c149b40
commit 4eb87e05d7
1 changed files with 39 additions and 45 deletions

View File

@ -10,19 +10,19 @@ using YoutubeExplode.Channels;
using System.Linq; using System.Linq;
HttpClient http=new HttpClient(); HttpClient http=new HttpClient();
YoutubeClient ytc=new YoutubeClient(http); YoutubeClient ytc=new YoutubeClient(http);
ConcurrentStack<VideoId> videoIds=new ConcurrentStack<VideoId>(); ConcurrentStack<string> ids=new ConcurrentStack<string>();
bool isRunning=true; bool isRunning=true;
long videosDownloaded=0; Box<long> videosDownloaded=0;
async Task NewVideoDL() async Task NewVideoDL()
{ {
long dl; long dl;
lock(videosDownloaded) lock(videosDownloaded)
{ {
dl = videosDownloaded++; dl = videosDownloaded.Item++;
} }
if((dl % 6) == 5) await Task.Delay(60000); if((dl % 4) == 3) await Task.Delay(45000);
} }
async Task AddVideo(VideoId id) async Task AddVideo(VideoId id)
@ -80,24 +80,24 @@ async Task AddItem(string url)
if(url.Length == 11 && vId.HasValue) if(url.Length == 11 && vId.HasValue)
{ {
videoIds.Push(vId.Value); await AddVideo(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); await AddVideo(v.Id);
} }
} }
else if(vId.HasValue) else if(vId.HasValue)
{ {
videoIds.Push(vId.Value); await AddVideo(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); await AddVideo(c.Id);
} }
} }
else if(username.HasValue) else if(username.HasValue)
@ -105,7 +105,7 @@ async Task AddItem(string url)
cId = (await ytc.Channels.GetByUserAsync(username.Value)).Id; cId = (await ytc.Channels.GetByUserAsync(username.Value)).Id;
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
{ {
videoIds.Push(c.Id); await AddVideo(c.Id);
} }
} }
else if(slug.HasValue) else if(slug.HasValue)
@ -113,7 +113,7 @@ async Task AddItem(string url)
cId = (await ytc.Channels.GetBySlugAsync(slug.Value)).Id; cId = (await ytc.Channels.GetBySlugAsync(slug.Value)).Id;
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
{ {
videoIds.Push(c.Id); await AddVideo(c.Id);
} }
} }
else if(handle.HasValue) else if(handle.HasValue)
@ -121,36 +121,37 @@ async Task AddItem(string url)
cId = (await ytc.Channels.GetByHandleAsync(handle.Value)).Id; cId = (await ytc.Channels.GetByHandleAsync(handle.Value)).Id;
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
{ {
videoIds.Push(c.Id); await AddVideo(c.Id);
} }
} }
} }
Task.Factory.StartNew(async()=>{ Task.Factory.StartNew(async()=>{
while(isRunning) while(isRunning)
{ {
if(videoIds.TryPop(out var res)) if(ids.TryPop(out var res))
{ {
await AddVideo(res); await AddItem(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/AddItemRes/1/",new TYTDServer(false,ids));
msvr.Mount("/api/AddItem/",new TYTDServer(AddItem)); msvr.Mount("/api/AddItem/",new TYTDServer(false,ids));
msvr.Mount("/api/AddVideoRes/1/",new TYTDServer(videoIds)); msvr.Mount("/api/AddVideoRes/1/",new TYTDServer(true,ids));
msvr.Mount("/api/AddVideo/",new TYTDServer(videoIds)); msvr.Mount("/api/AddVideo/",new TYTDServer(true,ids));
RouteServer routeServer=new RouteServer(msvr); RouteServer routeServer=new RouteServer(msvr);
routeServer.Add("/itemsInQueue",async(ctx)=>{ routeServer.Add("/itemsInQueue",async(ctx)=>{
await ctx.SendJsonAsync(videoIds.Count); await ctx.SendJsonAsync(ids.Count);
}); });
routeServer.Add("/queue.json", async(ctx)=>{ routeServer.Add("/queue.json", async(ctx)=>{
List<string> items = new List<string>(); List<string> items = new List<string>();
foreach(var item in videoIds) foreach(var item in ids)
{ {
items.Add(item.Value); items.Add(item);
} }
await ctx.SendJsonAsync(items); await ctx.SendJsonAsync(items);
}); });
@ -160,7 +161,7 @@ routeServer.Add("/add",async(ctx)=>{
if(ctx.QueryParams.TryGetFirst("v",out var v) && ctx.QueryParams.TryGetFirst("mode",out var mode)) if(ctx.QueryParams.TryGetFirst("v",out var v) && ctx.QueryParams.TryGetFirst("mode",out var mode))
{ {
if(mode == "add") if(mode == "add")
await AddItem(v); ids.Push(v);
else else
{ {
var id = VideoId.TryParse(v); var id = VideoId.TryParse(v);
@ -189,23 +190,29 @@ routeServer.Add("/add",async(ctx)=>{
}); });
routeServer.StartServer(3252); routeServer.StartServer(3252);
public class Box<T>
{
public Box(T item) {
this.Item = item;
}
public T Item {get;set;}
public static implicit operator Box<T>(T item)
{
return new Box<T>(item);
}
}
public class TYTDServer : Server public class TYTDServer : Server
{ {
bool onlyVideos; bool onlyVideos;
ConcurrentStack<VideoId>? stk; ConcurrentStack<string> stk;
Func<string,Task>? cb; public TYTDServer(bool onlyVids,ConcurrentStack<string> stk)
public TYTDServer(ConcurrentStack<VideoId> stk)
{ {
onlyVideos=true; this.onlyVideos=onlyVids;
this.stk = stk; this.stk = stk;
} }
public TYTDServer(Func<string,Task> cb)
{
onlyVideos=false;
this.cb = cb;
}
public override async Task GetAsync(ServerContext ctx) public override async Task GetAsync(ServerContext ctx)
{ {
@ -217,18 +224,5 @@ public class TYTDServer : Server
stk?.Push(url); stk?.Push(url);
await ctx.SendTextAsync("<script>window.history.back();</script>"); await ctx.SendTextAsync("<script>window.history.back();</script>");
} }
else
{
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>");
}
} }
} }