113 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C#
		
	
	
	
/*
 | 
						|
    TYTDDownloadWebUI a YouTube Search webui for TYTD (Note this program is seperate from the main TYTD Console App but depends on nuget package Tesses.YouTubeDownloader)
 | 
						|
    Copyright (C) 2023  Mike Nolan (Tesses)
 | 
						|
 | 
						|
    This program is free software: you can redistribute it and/or modify
 | 
						|
    it under the terms of the GNU General Public License as published by
 | 
						|
    the Free Software Foundation, either version 3 of the License, or
 | 
						|
    (at your option) any later version.
 | 
						|
 | 
						|
    This program is distributed in the hope that it will be useful,
 | 
						|
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
						|
    GNU General Public License for more details.
 | 
						|
 | 
						|
    You should have received a copy of the GNU General Public License
 | 
						|
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
						|
*/
 | 
						|
using System.Text;
 | 
						|
using Tesses.WebServer;
 | 
						|
using Tesses.YouTubeDownloader;
 | 
						|
using YoutubeExplode;
 | 
						|
using YoutubeExplode.Channels;
 | 
						|
using YoutubeExplode.Playlists;
 | 
						|
using YoutubeExplode.Search;
 | 
						|
using YoutubeExplode.Videos;
 | 
						|
 | 
						|
/*
 | 
						|
    Configuration
 | 
						|
*/
 | 
						|
string myIP = "http://192.168.0.142:3252/"; //change me for your downloader
 | 
						|
int myPort = 49204; //if you change me you also have to change this port in Dockerfile
 | 
						|
 | 
						|
/*
 | 
						|
    Code
 | 
						|
*/
 | 
						|
string redirstr = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>OK Added!</title></head><body><h1>OK Added!</h1><p>Please use back button in browser</p></body></html>";
 | 
						|
string failStr = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Failed To Add</title></head><body><h1>Failed To Add</h1><p>Please use back button in browser</p></body></html>";
 | 
						|
TYTDClient hclient = new TYTDClient(myIP);
 | 
						|
YoutubeClient sclient = new YoutubeClient();
 | 
						|
RouteServer routeServer=new RouteServer();
 | 
						|
routeServer.Add("/",async(ctx)=>{
 | 
						|
    StringBuilder builder=new StringBuilder();
 | 
						|
    builder.Append("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Download YouTube Videos</title></head><body><h1>Download YouTube Videos</h1><form action=\"./\" method=\"get\"><input type=\"search\" name=\"q\"><input type=\"submit\" value=\"Search\"></form><hr>");
 | 
						|
    builder.Append("<ul>");
 | 
						|
    if(ctx.QueryParams.TryGetFirst("q",out var query))
 | 
						|
    {
 | 
						|
        await foreach(var item in sclient.Search.GetResultsAsync(query))
 | 
						|
        {
 | 
						|
            var video = item as VideoSearchResult;
 | 
						|
            var playlist = item as PlaylistSearchResult;
 | 
						|
            var channel = item as ChannelSearchResult;
 | 
						|
 | 
						|
            if(video != null)
 | 
						|
            {
 | 
						|
                builder.Append($"<li><a href=\"./video?v={video.Id}\">[VIDEO] {System.Net.WebUtility.HtmlEncode(video.Title)} With Id: {video.Id}</a></li>");
 | 
						|
            }
 | 
						|
            if(playlist != null)
 | 
						|
            {
 | 
						|
                builder.Append($"<li><a href=\"./playlist?id={playlist.Id}\">[PLAYLIST] {System.Net.WebUtility.HtmlEncode(playlist.Title)} With Id: {playlist.Id}</a></li>");
 | 
						|
            }
 | 
						|
            if(channel != null)
 | 
						|
            {
 | 
						|
                builder.Append($"<li><a href=\"./channel?id={channel.Id}\">[CHANNEL] {System.Net.WebUtility.HtmlEncode(channel.Title)} With Id: {channel.Id}</a></li>");
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
    builder.Append("</ul></body></html>");
 | 
						|
    await ctx.SendTextAsync(builder.ToString());
 | 
						|
});
 | 
						|
routeServer.Add("/video",async(ctx)=>{
 | 
						|
    if(ctx.QueryParams.TryGetFirst("v",out var vid))
 | 
						|
    {
 | 
						|
        var id = VideoId.TryParse(vid);
 | 
						|
        if(id.HasValue)
 | 
						|
        {
 | 
						|
            await hclient.AddVideoAsync(id.Value);
 | 
						|
            await ctx.SendTextAsync(redirstr);
 | 
						|
            return;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    await ctx.SendTextAsync(failStr);
 | 
						|
    
 | 
						|
});
 | 
						|
routeServer.Add("/playlist",async(ctx)=>{
 | 
						|
    if(ctx.QueryParams.TryGetFirst("id",out var vid))
 | 
						|
    {
 | 
						|
        var id = PlaylistId.TryParse(vid);
 | 
						|
        if(id.HasValue)
 | 
						|
        {
 | 
						|
            await hclient.AddPlaylistAsync(id.Value);
 | 
						|
            await ctx.SendTextAsync(redirstr);
 | 
						|
            return;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    await ctx.SendTextAsync(failStr);
 | 
						|
    
 | 
						|
});
 | 
						|
routeServer.Add("/channel",async(ctx)=>{
 | 
						|
    if(ctx.QueryParams.TryGetFirst("id",out var vid))
 | 
						|
    {
 | 
						|
        var id = ChannelId.TryParse(vid);
 | 
						|
        if(id.HasValue)
 | 
						|
        {
 | 
						|
            await hclient.AddChannelAsync(id.Value);
 | 
						|
            await ctx.SendTextAsync(redirstr);
 | 
						|
            return;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    await ctx.SendTextAsync(failStr);
 | 
						|
    
 | 
						|
});
 | 
						|
routeServer.StartServer(myPort);
 |