diff --git a/Creds.cs b/Creds.cs index 14bfbed..a889473 100644 --- a/Creds.cs +++ b/Creds.cs @@ -1,3 +1,5 @@ +using YoutubeExplode.Videos; + public static partial class Creds { public static string IP => _ip; @@ -10,4 +12,9 @@ public static partial class Creds public static int Port => _port; public static string AlreadyExistsUrl=> _alreadyExists; + + public static string GetRedirectUrl(VideoId id) + { + return _redir.Replace("%ID%",id.Value); + } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 9a37952..0dc7823 100644 --- a/Program.cs +++ b/Program.cs @@ -142,9 +142,35 @@ routeServer.Add("/queue.json", async(ctx)=>{ await ctx.SendJsonAsync(items); }); routeServer.Add("/add",async(ctx)=>{ - if(ctx.QueryParams.TryGetFirst("v",out var v)) + + + if(ctx.QueryParams.TryGetFirst("v",out var v) && ctx.QueryParams.TryGetFirst("mode",out var mode)) { - await AddItem(v); + if(mode == "add") + await AddItem(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("/"); }); diff --git a/README.md b/README.md index 44e3416..66b677b 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ To Configure you must add a file called Creds.priv.cs with the following content static string _password = "YOUR_SSH_PASSWORD"; static string _dir = "YOUR_SSH_PATH"; static int _port = 22; //if ssh is on different port change it here + static string _redir = "/not_found?v=%ID%"; //if a video does not exist redirect here static string _alreadyExists=""; //must be http(s) url such as https://tytdarchive.site.tesses.net/have.php?v= that the response must return "true" if it already exists or "false" if not } diff --git a/wwwroot/index.html b/wwwroot/index.html index 44cfc9e..78d2505 100644 --- a/wwwroot/index.html +++ b/wwwroot/index.html @@ -13,6 +13,7 @@
download
Add a video, playlist or channel to my TYTD
+

Use add to add video to queue, or download to download a video from my TYTD to your device

@@ -20,9 +21,10 @@