Throttle YT
This commit is contained in:
		
							parent
							
								
									947c149b40
								
							
						
					
					
						commit
						4eb87e05d7
					
				
							
								
								
									
										84
									
								
								Program.cs
								
								
								
								
							
							
						
						
									
										84
									
								
								Program.cs
								
								
								
								
							|  | @ -10,19 +10,19 @@ using YoutubeExplode.Channels; | |||
| using System.Linq; | ||||
| HttpClient http=new HttpClient(); | ||||
| YoutubeClient ytc=new YoutubeClient(http); | ||||
| ConcurrentStack<VideoId> videoIds=new ConcurrentStack<VideoId>(); | ||||
| ConcurrentStack<string> ids=new ConcurrentStack<string>(); | ||||
| bool isRunning=true; | ||||
| long videosDownloaded=0; | ||||
| Box<long> videosDownloaded=0; | ||||
| 
 | ||||
| async Task NewVideoDL() | ||||
| { | ||||
|     long dl; | ||||
|     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) | ||||
|  | @ -80,24 +80,24 @@ async Task AddItem(string url) | |||
| 
 | ||||
|     if(url.Length == 11 && vId.HasValue)     | ||||
|     { | ||||
|         videoIds.Push(vId.Value); | ||||
|         await AddVideo(vId.Value); | ||||
|     } | ||||
|     else if(pId.HasValue) | ||||
|     { | ||||
|         await foreach(var v in ytc.Playlists.GetVideosAsync(pId.Value)) | ||||
|         { | ||||
|             videoIds.Push(v.Id); | ||||
|             await AddVideo(v.Id); | ||||
|         } | ||||
|     } | ||||
|     else if(vId.HasValue) | ||||
|     { | ||||
|         videoIds.Push(vId.Value); | ||||
|         await AddVideo(vId.Value); | ||||
|     } | ||||
|     else if(cId.HasValue) | ||||
|     { | ||||
|         await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) | ||||
|         { | ||||
|             videoIds.Push(c.Id); | ||||
|             await AddVideo(c.Id); | ||||
|         } | ||||
|     } | ||||
|     else if(username.HasValue) | ||||
|  | @ -105,7 +105,7 @@ async Task AddItem(string url) | |||
|         cId = (await ytc.Channels.GetByUserAsync(username.Value)).Id; | ||||
|         await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) | ||||
|         { | ||||
|             videoIds.Push(c.Id); | ||||
|             await AddVideo(c.Id); | ||||
|         } | ||||
|     } | ||||
|     else if(slug.HasValue) | ||||
|  | @ -113,7 +113,7 @@ async Task AddItem(string url) | |||
|         cId = (await ytc.Channels.GetBySlugAsync(slug.Value)).Id; | ||||
|         await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) | ||||
|         { | ||||
|             videoIds.Push(c.Id); | ||||
|             await AddVideo(c.Id); | ||||
|         } | ||||
|     } | ||||
|     else if(handle.HasValue) | ||||
|  | @ -121,36 +121,37 @@ async Task AddItem(string url) | |||
|         cId = (await ytc.Channels.GetByHandleAsync(handle.Value)).Id; | ||||
|         await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value)) | ||||
|         { | ||||
|             videoIds.Push(c.Id); | ||||
|             await AddVideo(c.Id); | ||||
|         } | ||||
|     } | ||||
|      | ||||
| } | ||||
| 
 | ||||
| Task.Factory.StartNew(async()=>{ | ||||
|     while(isRunning) | ||||
|     { | ||||
|         if(videoIds.TryPop(out var res)) | ||||
|         if(ids.TryPop(out var res)) | ||||
|         { | ||||
|             await AddVideo(res); | ||||
|             await AddItem(res); | ||||
|         } | ||||
|     } | ||||
| },TaskCreationOptions.LongRunning).Wait(0); | ||||
| 
 | ||||
| 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/AddVideoRes/1/",new TYTDServer(videoIds)); | ||||
| msvr.Mount("/api/AddVideo/",new TYTDServer(videoIds)); | ||||
| msvr.Mount("/api/AddItemRes/1/",new TYTDServer(false,ids)); | ||||
| msvr.Mount("/api/AddItem/",new TYTDServer(false,ids)); | ||||
| msvr.Mount("/api/AddVideoRes/1/",new TYTDServer(true,ids)); | ||||
| msvr.Mount("/api/AddVideo/",new TYTDServer(true,ids)); | ||||
| RouteServer routeServer=new RouteServer(msvr); | ||||
| routeServer.Add("/itemsInQueue",async(ctx)=>{ | ||||
|     await ctx.SendJsonAsync(videoIds.Count); | ||||
|     await ctx.SendJsonAsync(ids.Count); | ||||
| }); | ||||
| routeServer.Add("/queue.json", async(ctx)=>{ | ||||
|     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); | ||||
| }); | ||||
|  | @ -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(mode == "add") | ||||
|             await AddItem(v); | ||||
|             ids.Push(v); | ||||
|         else | ||||
|         { | ||||
|             var id = VideoId.TryParse(v); | ||||
|  | @ -189,22 +190,28 @@ routeServer.Add("/add",async(ctx)=>{ | |||
| }); | ||||
| 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 | ||||
| { | ||||
|     bool onlyVideos; | ||||
|     ConcurrentStack<VideoId>? stk; | ||||
|     | ||||
|     Func<string,Task>? cb; | ||||
|     public TYTDServer(ConcurrentStack<VideoId> stk) | ||||
|     { | ||||
|         onlyVideos=true; | ||||
|         this.stk = stk; | ||||
|     } | ||||
|     ConcurrentStack<string> stk; | ||||
| 
 | ||||
|     public TYTDServer(Func<string,Task> cb) | ||||
|     public TYTDServer(bool onlyVids,ConcurrentStack<string> stk) | ||||
|     { | ||||
|         onlyVideos=false; | ||||
|         this.cb = cb; | ||||
|         this.onlyVideos=onlyVids; | ||||
|         this.stk = stk; | ||||
|     } | ||||
|     public override async Task GetAsync(ServerContext ctx) | ||||
|     { | ||||
|  | @ -217,18 +224,5 @@ public class TYTDServer : Server | |||
|             stk?.Push(url); | ||||
|             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>"); | ||||
| 
 | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue