fixed some bugs

This commit is contained in:
Mike Nolan 2022-02-24 17:37:01 -06:00
parent 17868d9708
commit 2155d045ef
8 changed files with 123 additions and 39 deletions

View File

@ -22,6 +22,8 @@ using PlaylistsNET.Content;
using PlaylistsNET.Models;
using PlaylistsNET.Utils;
using YoutubeExplode.Playlists;
using Dasync.Collections;
using YoutubeExplode.Search;
namespace TYTD
{
@ -67,7 +69,7 @@ namespace TYTD
Route.Add("/api/AddCaptions/{Id}", (HttpAction)AddCaptions);
/* Videos */
Route.Add("/api/AddVideoInfo/{Id}", (HttpAction)AddVideoInfo);
Route.Add("/api/AddVideoInfo/{Id}", AddVideoInfo);
Route.Add("/api/AddVideo/{Id}", (HttpAction)AddVideo);
Route.Add("/api/AddVideoRes/{R}/{Id}", (HttpAction)AddVideoRes);
Route.Add("/api/Redownload", (HttpAction)Redownload);
@ -75,28 +77,31 @@ namespace TYTD
Route.Add("/api/Watch/{VideoId}", (HttpAction)Watch);
Route.Add("/api/VideoInfo/{Id}",(HttpAction)VideoInfo);
/* Playlist */
Route.Add("/api/AddPlaylistOnly/{Id}", (HttpAction)AddPlaylistOnly);
Route.Add("/api/AddPlaylistOnly/{Id}", AddPlaylistOnly);
Route.Add("/api/AddPlaylist/{Id}", (HttpAction)AddPlaylist);
Route.Add("/api/AddPlaylistRes/{R}/{Id}", (HttpAction)AddPlaylistRes);
Route.Add("/api/PersonalPlaylist/{PlaylistName}",(HttpAction)PersonalPlaylist);
Route.Add("/api/CreatePlaylist/{Ids}/playlist.{extension}", (HttpAction)CreatePlaylist);
Route.Add("/api/CreatePlaylistRes/{Ids}/playlist.{extension}", (HttpAction)CreatePlaylistRes);
Route.Add("/api/PlaylistInfo/{Id}", (HttpAction)PlaylistInfo);
Route.Add("/api/ListPlaylists.html", (HttpAction)ListPlaylists);
Route.Add("/api/ListPlaylists/", (HttpAction)ListPlaylists);
/* Search */
Route.Add("/api/SearchOnly/{text}", (HttpAction)SearchOnly);
Route.Add("/api/Search/{text}", (HttpAction)Search);
Route.Add("/api/SearchPage/", SearchPage);
Route.Add("/api/SearchPage/{query}",SearchPage);
Route.Add("/api/SearchPage/",SearchPage,"POST");
Route.Add("/api/SearchVideos/", (HttpAction)SearchVideos,"POST");
Route.Add("/api/SearchVideos/{query}", (HttpAction)SearchVideos, "GET");
Route.Add("/api/SearchVideos/", (HttpAction)SearchVideos, "GET");
/* Channel */
Route.Add("/api/AddChannelOnly/{Id}", (HttpAction)AddChannelOnly);
Route.Add("/api/AddChannelOnly/{Id}", AddChannelOnly);
Route.Add("/api/AddChannel/{Id}", (HttpAction)AddChannel);
Route.Add("/api/AddChannelRes/{R}/{Id}", (HttpAction)AddChannelRes);
/* User */
Route.Add("/api/AddUserOnly/{Id}", (HttpAction)AddUserOnly);
Route.Add("/api/AddUserOnly/{Id}", AddUserOnly);
Route.Add("/api/AddUser/{Id}", (HttpAction)AddUser);
Route.Add("/api/AddUserRes/{R}/{Id}", (HttpAction)AddUserRes);
@ -224,7 +229,7 @@ namespace TYTD
videos.AddEscapedHtml("Description", item.Description);
videos.AddEscapedHtml("UploadDate", DateTime.Parse(item.UploadDate).ToShortDateString());
string res=ApiLoader.RenderFileOrDefault("WebSite/err/video_list/VideoInfo.html", "<!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>Information about {Title}</title></head><body><h1>Video Info for {Title}</h1>Playback: <a href=\"../../api/Storage/File/NotConverted/{Id}.mp4\">SD</a> <a href=\"../../api/Storage/File/Converted/{Id}.mp4\">HD</a> <a href=\"../../api/Storage/File/Audio/{Id}.mp4\">Audio</a><br>Download: <a href=\"../../api/Storage/VideoRes/1/{Id}\">SD</a> <a href=\"../../api/Storage/VideoRes/0/{Id}\">HD</a> <a href=\"../../api/Storage/VideoRes/2/{Id}\">Audio</a> <br><h3>Video Id: {Id}</h3><h3>Video Channel: {AuthorTitle}</h3><h3>Video Channel Id: {AuthorChannelId}</h3><h3>Likes: {Likes}, Dislikes: {Dislikes}, Views: {Views}</h3><h3>Upload Date: {UploadDate}</h3><h3>Duration: {DurationStringLong}</h3><h3>Description:</h3><p>{Description}</p></body></html>", videos);
string res=ApiLoader.RenderFileOrDefault("WebSite/err/video_list/VideoInfo.html", "<!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>Information about {Title}</title></head><body><h1>Video Info for {Title}</h1>Playback: <a href=\"../../api/Storage/File/NotConverted/{Id}.mp4\">SD</a> <a href=\"../../api/Storage/File/Converted/{Id}.mp4\">HD</a> <a href=\"../../api/Storage/File/Audio/{Id}.mp4\">Audio</a><br>Download: <a href=\"../../api/Storage/VideoRes/1/{Id}\">SD</a> <a href=\"../../api/Storage/VideoRes/0/{Id}\">HD</a> <a href=\"../../api/Storage/VideoRes/2/{Id}\">Audio</a><br>Add To Server: <a href=\"../../api/AddVideoRes/1/{Id}\">SD</a> <a href=\"../../api/AddVideoRes/0/{Id}\">HD</a> <a href=\"../../api/AddVideoRes/2/{Id}\">Audio</a><br><h3>Video Id: {Id}</h3><h3>Video Channel: {AuthorTitle}</h3><h3>Video Channel Id: {AuthorChannelId}</h3><h3>Likes: {Likes}, Dislikes: {Dislikes}, Views: {Views}</h3><h3>Upload Date: {UploadDate}</h3><h3>Duration: {DurationStringLong}</h3><h3>Description:</h3><p>{Description}</p></body></html>", videos);
response.AsText(res);
}
else
@ -234,9 +239,9 @@ namespace TYTD
}
public static void AddVideoInfo(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
public static async Task AddVideoInfo(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
Downloader.DownloadVideoInfo(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
await Downloader.DownloadVideoInfo(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
rq.RedirectIt(rp);
}
@ -282,9 +287,9 @@ namespace TYTD
}
#endregion
#region Playlist
public static void AddPlaylistOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
public static async Task AddPlaylistOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
Downloader.DownloadPlaylistOnly(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
await Downloader.DownloadPlaylistOnly(System.Web.HttpUtility.UrlDecode(args["Id"]), Resolution.NoConvert);
rq.RedirectIt(rp);
}
@ -429,7 +434,7 @@ namespace TYTD
}
public static void ListPlaylists(HttpListenerRequest request,HttpListenerResponse resp,Dictionary<string,string> args)
{
string htmlBeforeProcessed = ApiLoader.ReadAllTextOrDefault("WebSite/err/playlist_list/element.html", "<tr><td><img src=\"../api/Storage/File/Thumbnails/120x90/{FirstVideoId}.jpg\" alt=\"\" width=\"120\" height=\"90\"></td><td><a href=\"../api/PlaylistInfo/{Id}\">{Title}</a><h5>{AuthorTitle}</h5></td></tr>");
string htmlBeforeProcessed = ApiLoader.ReadAllTextOrDefault("WebSite/err/playlist_list/element.html", "<tr><td><img src=\"../Storage/File/Thumbnails/120x90/{FirstVideoId}.jpg\" alt=\"\" width=\"120\" height=\"90\"></td><td><a href=\"../PlaylistInfo/{Id}\">{Title}</a><h5>{AuthorTitle}</h5></td></tr>");
StringBuilder builder = new StringBuilder();
foreach (var f in Directory.EnumerateFiles("Playlist","*.json"))
{
@ -472,9 +477,14 @@ namespace TYTD
StringBuilder builder = new StringBuilder();
foreach (var v in plitem.Videos)
{
string path = Path.Combine("Info", $"{v}.json");
if (!File.Exists(path))
{
continue;
}
try
{
var item = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(Path.Combine("Info", $"{v}.json")));
var item = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(path));
Dictionary<string, string> videos = new Dictionary<string, string>();
videos.AddEscapedHtml("Title", item.Title);
videos.AddEscapedHtml("Id", item.Id);
@ -528,6 +538,86 @@ namespace TYTD
rp.AsText(json, "application/json");
}
public static async Task SearchPage(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
if (rq.HttpMethod == "POST")
{
rq.ParseBody(args);
}
string search = "";
if (args.ContainsKey("query"))
{
search = System.Web.HttpUtility.UrlDecode(args["query"]);
}
try
{
string htmlBeforeProcessedPlaylist = ApiLoader.ReadAllTextOrDefault("WebSite/err/playlist_list/element.html", "<tr><td><img src=\"../Storage/File/Thumbnails/120x90/{FirstVideoId}.jpg\" alt=\"\" width=\"120\" height=\"90\"></td><td><a href=\"../PlaylistInfo/{Id}\">{Title}</a><h5>{AuthorTitle}</h5></td></tr>");
string htmlBeforeProcessedVideo = ApiLoader.ReadAllTextOrDefault("WebSite/err/video_list/element.html", "<tr><td><img src=\"../api/Storage/File/Thumbnails/120x90/{Id}.jpg\" alt=\"\" width=\"120\" height=\"90\"></td><td><a href=\"../VideoInfo/{Id}\">{Title}</a><h5>{AuthorTitle}</h5></td></tr>");
StringBuilder builder = new StringBuilder();
await Downloader.GetClient().Search.GetResultsAsync(search).ForEachAsync(async (e) =>
{
var video = e as VideoSearchResult;
var playlist = e as PlaylistSearchResult;
var channel = e as ChannelSearchResult;
if (video != null)
{
await Downloader.DownloadVideoInfo(video.Id, Resolution.NoConvert);
string path = Path.Combine("Info", $"{video.Id.Value}.json");
var item = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(path));
Dictionary<string, string> videos = new Dictionary<string, string>();
videos.AddEscapedHtml("Title", item.Title);
videos.AddEscapedHtml("Id", item.Id);
videos.AddEscapedHtml("AuthorTitle", item.AuthorTitle);
videos.AddEscapedHtml("AuthorChannelId", item.AuthorChannelId);
videos.AddEscapedHtml("DurationStringLong", TimeSpan.FromSeconds(item.Duration).ToString());
videos.AddEscapedHtml("DurationNumber", item.Duration.ToString());
videos.AddEscapedHtml("Likes", item.Likes.ToString());
videos.AddEscapedHtml("Dislikes", item.Dislikes.ToString());
videos.AddEscapedHtml("Views", item.Views.ToString());
videos.AddEscapedHtml("Description", item.Description);
videos.AddEscapedHtml("UploadDate", DateTime.Parse(item.UploadDate).ToShortDateString());
builder.Append(Templating.RenderString(htmlBeforeProcessedVideo, videos));
}
if (playlist != null)
{
await Downloader.DownloadPlaylistOnly(playlist.Id, Resolution.NoConvert);
Dictionary<string, string> playlist0 = new Dictionary<string, string>();
var plitem = JsonConvert.DeserializeObject<SavedPlaylist>(File.ReadAllText(Path.Combine("Playlist", $"{playlist.Id.Value}.json")));
playlist0.AddEscapedHtml("Title", plitem.Title);
playlist0.AddEscapedHtml("Id", plitem.Id);
playlist0.AddEscapedHtml("AuthorTitle", plitem.AuthorTitle);
playlist0.AddEscapedHtml("AuthorChannelId", plitem.AuthorChannelId);
playlist0.AddEscapedHtml("Description", plitem.Description);
playlist0.AddEscapedHtml("FirstVideoId", plitem.Videos[0]);
string res0 = Templating.RenderString(htmlBeforeProcessedPlaylist, playlist0);
builder.Append(res0);
}
if(channel !=null)
{
await Downloader.DownloadChannelOnly(channel.Id);
}
});
//string res = ApiLoader.RenderFileOrDefault(, "<!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>All Playlists</title></head><body><h1>All Playlists</h1><table><thead><tr><th></th><th></th></tr></thead><tbody>{Elements}</tbody></table></body></html>", main);
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("Elements", builder.ToString());
string html = ApiLoader.RenderFileOrDefault("WebSite/err/search_main.html", "<!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>Search Online</title></head><body><form action=\"../SearchPage/\" method=\"POST\"><input type=\"text\" name=\"query\"><input type=\"submit\" value=\"Search\"></form><table><thead><tr><th></th><th></th></tr></thead><tbody>{Elements}</tbody></table>\n</body></html>", data);
rp.AsText(html, "text/html");
}
catch (Exception ex)
{
_ = ex;
}
}
public static void SearchVideos(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
if(rq.HttpMethod == "POST")
@ -537,11 +627,11 @@ namespace TYTD
string search = "*";
if (args.ContainsKey("query"))
{
System.Web.HttpUtility.UrlDecode(args["query"]);
search= System.Web.HttpUtility.UrlDecode(args["query"]);
}
StringBuilder innerHtml = new StringBuilder();
string htmlBeforeProcessed = ApiLoader.ReadAllTextOrDefault("WebSite/err/video_list/element.html", "<tr><td><img src=\"../File/Thumbnails/120x90/{Id}.jpg\" alt=\"\" width=\"120\" height=\"90\"></td><td><a href=\"../VideoInfo/{Id}\">{Title}</a><h5>{AuthorTitle}</h5></td></tr>");
string htmlBeforeProcessed = ApiLoader.ReadAllTextOrDefault("WebSite/err/video_list/element.html", "<tr><td><img src=\"../Storage/File/Thumbnails/120x90/{Id}.jpg\" alt=\"\" width=\"120\" height=\"90\"></td><td><a href=\"../VideoInfo/{Id}\">{Title}</a><h5>{AuthorTitle}</h5></td></tr>");
long i=0;
foreach (var item in Downloader.SearchFor(search))
{
@ -573,9 +663,9 @@ namespace TYTD
}
#endregion
#region Channel
public static void AddChannelOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
public static async Task AddChannelOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
Downloader.DownloadChannelOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
await Downloader.DownloadChannelOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
rq.RedirectIt(rp);
@ -595,9 +685,9 @@ namespace TYTD
}
#endregion
#region User
public static void AddUserOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
public static async Task AddUserOnly(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
Downloader.DownloadUserOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
await Downloader.DownloadUserOnly(System.Web.HttpUtility.UrlDecode(args["Id"]));
rq.RedirectIt(rp);
}
@ -910,7 +1000,7 @@ namespace TYTD
#region Other
public static void Index(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
string r = ApiLoader.ReadAllTextOrDefault(Path.Combine(webSitePath, "index.html"), "<!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>TYTD</title></head><body><h1>TYTD</h1><form action=\"./api/endpoint\" method=\"POST\"><input type=\"text\" name=\"url\"><select name=\"resolution\"><option value=\"1\" selected>SD</option><option value=\"0\">HD</option><option value=\"2\">Audio</option></select><input type=\"submit\" value=\"Add To Downloader\"></form>Existing Videos: <form action=\"./api/SearchVideos/\" method=\"POST\"><input type=\"search\" name=\"query\"><input type=\"submit\" value=\"Search\"></form><br><a href=\"./api/Progress.html\">Get Progress</a><br><a href=\"./api/QueueListPage/\">List Queue</a><br><a href=\"./api/ListPlaylists.html\">List Playlists</a><br></body></html>");
string r = ApiLoader.ReadAllTextOrDefault(Path.Combine(webSitePath, "index.html"), "<!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>TYTD</title></head><body><h1>TYTD</h1><form action=\"./api/endpoint\" method=\"POST\"><input type=\"text\" name=\"url\"><select name=\"resolution\"><option value=\"1\" selected>SD</option><option value=\"0\">HD</option><option value=\"2\">Audio</option></select><input type=\"submit\" value=\"Add To Downloader\"></form>Existing Videos: <form action=\"./api/SearchVideos/\" method=\"POST\"><input type=\"search\" name=\"query\"><input type=\"submit\" value=\"Search\"></form><br><a href=\"./api/Progress.html\">Get Progress</a><br><a href=\"./api/QueueListPage/\">List Queue</a><br><a href=\"./api/ListPlaylists/\">List Playlists</a><br></body></html>");
rp.AsText( r);
}

View File

@ -149,6 +149,11 @@ namespace TYTD.Server.Functions
static HttpClient Http;
internal YoutubeClient ytc = CreateYoutubeClient();
public static YoutubeClient GetClient()
{
return DL.ytc;
}
static VideoDownloadProgress P = new VideoDownloadProgress();
const int NUM_WAITS = 5;
static int WAITS = 0;
@ -391,16 +396,13 @@ namespace TYTD.Server.Functions
}
}
public static void DownloadChannelOnly(string id)
public static async Task DownloadChannelOnly(string id)
{
ChannelId? v = ChannelId.TryParse(id);
if (v.HasValue)
{
InfomationQueueItem item = new InfomationQueueItem(v.Value, Resolution.NoConvert, false);
lock (DL.infoQueue)
{
DL.infoQueue.Insert(0, item);
}
await item.DownloadData();
}
}
public static void DownloadItems(List<IDResolutionTypeTriplet> id)
@ -423,16 +425,13 @@ namespace TYTD.Server.Functions
}
}
}
public static void DownloadUserOnly(string id)
public static async Task DownloadUserOnly(string id)
{
UserName? v = UserName.TryParse(id);
if (v.HasValue)
{
InfomationQueueItem item = new InfomationQueueItem(v.Value, Resolution.NoConvert, false);
lock (DL.infoQueue)
{
DL.infoQueue.Insert(0, item);
}
await item.DownloadData();
}
}
@ -1221,16 +1220,13 @@ namespace TYTD.Server.Functions
}
}
}
public static void DownloadVideoInfo(string id, Resolution res)
public static async Task DownloadVideoInfo(string id, Resolution res)
{
VideoId? v = VideoId.TryParse(id);
if (v.HasValue)
{
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, false);
lock (DL.infoQueue)
{
DL.infoQueue.Insert(0, item);
}
await item.DownloadData();
}
}
@ -1262,16 +1258,14 @@ namespace TYTD.Server.Functions
}
}
}
public static void DownloadPlaylistOnly(string id, Resolution res)
public static async Task DownloadPlaylistOnly(string id, Resolution res)
{
PlaylistId? v = PlaylistId.TryParse(id);
if (v.HasValue)
{
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, false);
lock (DL.infoQueue)
{
DL.infoQueue.Insert(0, item);
}
await item.DownloadData();
}
}
public static void DownloadPlaylist(string id)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.