BringTYTDBack/Program.cs

230 lines
6.1 KiB
C#

using System.Collections.Concurrent;
using Renci.SshNet;
using YoutubeExplode;
using YoutubeExplode.Videos;
using Tesses.WebServer;
using static Creds;
using System.Diagnostics.CodeAnalysis;
using YoutubeExplode.Playlists;
using YoutubeExplode.Channels;
using System.Linq;
HttpClient http=new HttpClient();
YoutubeClient ytc=new YoutubeClient(http);
ConcurrentStack<string> ids=new ConcurrentStack<string>();
bool isRunning=true;
Box<ulong> videosDownloaded=0;
async Task NewVideoDL()
{
ulong dl;
lock(videosDownloaded)
{
unchecked {
dl = videosDownloaded.Item++;
}
}
if((dl % 4) == 3) await Task.Delay(45000);
}
async Task AddVideo(VideoId id)
{
if(!string.IsNullOrWhiteSpace(AlreadyExistsUrl))
{
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;
}
}catch(Exception)
{
}
}
using(SshClient client = new SshClient(IP,Port,Username,Password)){
await client.ConnectAsync(default);
using(var res=client.CreateCommand($"echo {DirectoryOnServer}/*-{id.Value}.mp4"))
{
await res.ExecuteAsync();
if(res.Result == $"{DirectoryOnServer}/*-{id.Value}.mp4\n")
{
using(var res2=client.CreateCommand($"cd {DirectoryOnServer} && tytd {id.Value}"))
{
await res2.ExecuteAsync();
Console.WriteLine($"Downloaded: https://www.youtube.com/watch?v={id.Value}");
_=res2.Result;
}
}
else
{
Console.WriteLine($"Video already exists: https://www.youtube.com/watch?v={id.Value}");
}
}
client.Disconnect();
await NewVideoDL();
}
}
async Task AddItem(string url)
{
VideoId? vId = VideoId.TryParse(url);
PlaylistId? pId = PlaylistId.TryParse(url);
ChannelId? cId = ChannelId.TryParse(url);
UserName? username = UserName.TryParse(url);
ChannelSlug? slug = ChannelSlug.TryParse(url);
ChannelHandle? handle = ChannelHandle.TryParse(url);
if(url.Length == 11 && vId.HasValue)
{
await AddVideo(vId.Value);
}
else if(pId.HasValue)
{
await foreach(var v in ytc.Playlists.GetVideosAsync(pId.Value))
{
await AddVideo(v.Id);
}
}
else if(vId.HasValue)
{
await AddVideo(vId.Value);
}
else if(cId.HasValue)
{
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
{
await AddVideo(c.Id);
}
}
else if(username.HasValue)
{
cId = (await ytc.Channels.GetByUserAsync(username.Value)).Id;
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
{
await AddVideo(c.Id);
}
}
else if(slug.HasValue)
{
cId = (await ytc.Channels.GetBySlugAsync(slug.Value)).Id;
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
{
await AddVideo(c.Id);
}
}
else if(handle.HasValue)
{
cId = (await ytc.Channels.GetByHandleAsync(handle.Value)).Id;
await foreach(var c in ytc.Channels.GetUploadsAsync(cId.Value))
{
await AddVideo(c.Id);
}
}
}
Task.Factory.StartNew(async()=>{
while(isRunning)
{
if(ids.TryPop(out var res))
{
await AddItem(res);
}
}
},TaskCreationOptions.LongRunning).Wait(0);
MountableServer msvr=new MountableServer(new StaticServer("wwwroot"));
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(ids.Count);
});
routeServer.Add("/queue.json", async(ctx)=>{
List<string> items = new List<string>();
foreach(var item in ids)
{
items.Add(item);
}
await ctx.SendJsonAsync(items);
});
routeServer.Add("/add",async(ctx)=>{
if(ctx.QueryParams.TryGetFirst("v",out var v) && ctx.QueryParams.TryGetFirst("mode",out var mode))
{
if(mode == "add")
ids.Push(v);
else
{
var id = VideoId.TryParse(v);
if(id.HasValue)
{
using(SftpClient sftpClient=new SftpClient(IP,Port,Username,Password))
{
await sftpClient.ConnectAsync(default);
await foreach(var item in sftpClient.ListDirectoryAsync(DirectoryOnServer,default))
{
if(item.Name.EndsWith($"-{id.Value.Value}.mp4"))
{
await ctx.WithFileName(item.Name,false).SendStreamAsync(sftpClient.OpenRead(item.FullName));
sftpClient.Disconnect();
return;
}
}
sftpClient.Disconnect();
}
await ctx.SendRedirectAsync(GetRedirectUrl(id.Value));
return;
}
}
}
await ctx.SendRedirectAsync("/");
});
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<string> stk;
public TYTDServer(bool onlyVids,ConcurrentStack<string> stk)
{
this.onlyVideos=onlyVids;
this.stk = stk;
}
public override async Task GetAsync(ServerContext ctx)
{
if(onlyVideos)
{
string url=ctx.UrlAndQuery.Substring(1);
stk?.Push(url);
await ctx.SendTextAsync("<script>window.history.back();</script>");
}
}
}