tytd-server/TYTD.Api/Server/Models/InfomationQueueItem.cs

314 lines
14 KiB
C#
Raw Normal View History

2021-08-26 09:04:57 +00:00
using Dasync.Collections;
using Newtonsoft.Json;
2021-06-25 08:30:45 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2021-12-08 01:07:43 +00:00
2021-06-25 08:30:45 +00:00
using YoutubeExplode.Channels;
using YoutubeExplode.Playlists;
using YoutubeExplode.Videos;
2021-12-08 01:07:43 +00:00
namespace TYTD.Server.Models
2021-06-25 08:30:45 +00:00
{
public class InfomationQueueItem
{
public InfomationQueueItem(VideoId id,Resolution res,bool download = true)
{
Data = id.Value;
DownloadActualDataAfterwards = download;
Type = InfoType.Video;
this.res = res;
}
public InfomationQueueItem(VideoId id)
{
Data = id.Value;
DownloadActualDataAfterwards = false;
Type = InfoType.ClosedCaptions;
this.res = 0;
}
public InfomationQueueItem(IDResolutionTypeTriplet triplet)
{
Data = triplet.Id;
DownloadActualDataAfterwards = true;
Type = triplet.Type;
this.res = triplet.Resolution;
}
2021-06-25 08:30:45 +00:00
public InfomationQueueItem(PlaylistId id,Resolution res,bool download = true)
{
Data = id.Value;
DownloadActualDataAfterwards = download;
Type = InfoType.Playlist;
this.res = res;
}
public InfomationQueueItem(Uri url)
{
Data = url.ToString();
DownloadActualDataAfterwards = true;
Type = InfoType.FileDownload;
this.res = 0;
}
2021-06-25 08:30:45 +00:00
public InfomationQueueItem(ChannelId id, Resolution res, bool download = true)
{
Data = id.Value;
DownloadActualDataAfterwards = download;
Type = InfoType.Channel;
this.res = res;
}
public InfomationQueueItem(UserName id, Resolution res, bool download = true)
{
Data = id.Value;
DownloadActualDataAfterwards = download;
Type = InfoType.User;
this.res = res;
}
Resolution res;
/// <summary>
/// Download it afterwards
/// </summary>
public bool DownloadActualDataAfterwards { get; set; }
/// <summary>
/// Kind
/// </summary>
public InfoType Type { get; set; }
/// <summary>
/// ID for item
/// </summary>
public string Data { get; set; }
public async Task<SavedVideoObject[]> DownloadData()
{
switch (Type)
{
case InfoType.FileDownload:
return new SavedVideoObject[] { new SavedVideoObject(Data) };
case InfoType.ClosedCaptions:
string path = Functions.Downloader.DL.GetPath(true, "ClosedCaptions", Data);
if (!Directory.Exists(path))
{
var cc0 = await Functions.Downloader.DL.ytc.Videos.ClosedCaptions.GetManifestAsync(Data);
string trackInfo = Functions.Downloader.DL.GetPath(true, "ClosedCaptions", Data,"track_info.json");
File.WriteAllText(trackInfo, JsonConvert.SerializeObject(cc0.Tracks));
foreach (var track in cc0.Tracks)
{
string trackType = track.IsAutoGenerated ? "auto" : "manu";
string langFileName = $"{trackType}_{track.Language.Code}.srt";
string track2 = Functions.Downloader.DL.GetPath(true, "ClosedCaptions", Data,langFileName);
await Functions.Downloader.DL.ytc.Videos.ClosedCaptions.DownloadAsync(track, track2);
}
}
break;
2021-06-25 08:30:45 +00:00
case InfoType.Video:
{
string infPath = Functions.Downloader.DL.GetPath(true, "Info", Data + ".json");
SavedVideoObject sv;
bool exist = File.Exists(infPath);
if (exist)
{
sv = new SavedVideoObject();
sv.Resolution = res;
sv.Video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(infPath));
2021-08-03 19:39:03 +00:00
foreach(var tn in sv.Video.Thumbnails)
{
Functions.Downloader.DL._DownloadThumbnail2(tn.Item1, tn.Item2, sv.Video.Id, tn.Item3);
}
2021-06-25 08:30:45 +00:00
}
2021-08-03 19:39:03 +00:00
else{
2021-06-25 08:30:45 +00:00
var vinfo = await Functions.Downloader.DL.ytc.Videos.GetAsync(Data);
sv = SavedVideo.CreateFrom(res, vinfo, Functions.Downloader.DL._DownloadThumbnail);
}
if (!exist)
{
File.WriteAllText(infPath, JsonConvert.SerializeObject(sv.Video));
}
return new SavedVideoObject[] { sv };
}
case InfoType.Playlist:
2021-06-26 03:17:59 +00:00
{ List<SavedVideoObject> video2 = new List<SavedVideoObject>();
2021-06-25 08:30:45 +00:00
List<string> vo = new List<string>();
SavedPlaylist pl = await SavedPlaylist.FromPlaylistId(res, Functions.Downloader.DL.ytc, Data, (e,f) => { vo.Add(e); }, Functions.Downloader.DL._DownloadThumbnail);
string infpath = Functions.Downloader.DL.GetPath(true, "Playlist", Data + ".json");
File.WriteAllText(infpath, JsonConvert.SerializeObject(pl));
foreach(var str in vo)
{
string infPath = Functions.Downloader.DL.GetPath(true, "Info", str + ".json");
SavedVideoObject sv;
bool exist = File.Exists(infPath);
if (exist)
{
sv = new SavedVideoObject();
sv.Resolution = res;
sv.Video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(infPath));
2021-08-03 19:39:03 +00:00
foreach (var tn in sv.Video.Thumbnails)
{
Functions.Downloader.DL._DownloadThumbnail2(tn.Item1, tn.Item2, sv.Video.Id, tn.Item3);
}
2021-06-25 08:30:45 +00:00
}
else
{
try
{
var vinfo = await Functions.Downloader.DL.ytc.Videos.GetAsync(str);
sv = SavedVideo.CreateFrom(res, vinfo, Functions.Downloader.DL._DownloadThumbnail);
}catch(Exception ex)
{
2021-06-26 03:22:31 +00:00
Console.WriteLine(ex.Message);
2021-06-25 08:30:45 +00:00
sv = null;
_ = ex;
}
}
if (!exist)
{
File.WriteAllText(infPath, JsonConvert.SerializeObject(sv.Video));
}
if(sv != null)
{
video2.Add(sv);
}
}
return video2.ToArray();
}
case InfoType.Channel:
{
List<SavedVideoObject> video = new List<SavedVideoObject>();
try
{
var c = Functions.Downloader.DL.ytc.Channels.GetAsync(Data).GetAwaiter().GetResult();
SavedChannel c2 = SavedChannel.FromChannel(c, Functions.Downloader.DL._DownloadThumbnail);
string infpath = Functions.Downloader.DL.GetPath(true, "Channel", Data + ".json");
File.WriteAllText(infpath, JsonConvert.SerializeObject(c2));
try
{
Functions.Downloader.DL.ytc.Channels.GetUploadsAsync(c.Id).ForEachAsync(async (v) =>
{
string infPath = Functions.Downloader.DL.GetPath(true, "Info", v.Id + ".json");
bool exist = File.Exists(infPath);
if (exist)
{
SavedVideoObject sv = new SavedVideoObject();
sv.Resolution = res;
sv.Video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(infPath));
2021-08-03 19:39:03 +00:00
foreach (var tn in sv.Video.Thumbnails)
{
Functions.Downloader.DL._DownloadThumbnail2(tn.Item1, tn.Item2, sv.Video.Id, tn.Item3);
}
2021-06-25 08:30:45 +00:00
video.Add(sv);
}
else
{
try
{
var vinfo = await Functions.Downloader.DL.ytc.Videos.GetAsync(v.Id);
SavedVideoObject sv = SavedVideo.CreateFrom(res, vinfo, Functions.Downloader.DL._DownloadThumbnail);
video.Add(sv);
}
catch (Exception ex)
{
2021-06-26 03:22:31 +00:00
Console.WriteLine(ex.Message);
2021-06-25 08:30:45 +00:00
_ = ex;
}
}
}).Wait();
}
catch (Exception ex)
{
2021-06-26 03:22:31 +00:00
Console.WriteLine(ex.Message);
2021-06-25 08:30:45 +00:00
_ = ex;
}
}
catch (Exception ex2)
{
2021-06-26 03:22:31 +00:00
Console.WriteLine(ex2.Message);
2021-06-25 08:30:45 +00:00
_ = ex2;
}
return video.ToArray();
}
case InfoType.User:
{
List<SavedVideoObject> video = new List<SavedVideoObject>();
try
{
var c = Functions.Downloader.DL.ytc.Channels.GetByUserAsync(Data).GetAwaiter().GetResult();
SavedChannel c2 = SavedChannel.FromChannel(c, Functions.Downloader.DL._DownloadThumbnail);
string infpath = Functions.Downloader.DL.GetPath(true, "Channel", Data + ".json");
File.WriteAllText(infpath, JsonConvert.SerializeObject(c2));
try
{
Functions.Downloader.DL.ytc.Channels.GetUploadsAsync(c.Id).ForEachAsync(async (v) =>
{
string infPath = Functions.Downloader.DL.GetPath(true, "Info", v.Id + ".json");
bool exist = File.Exists(infPath);
if (exist)
{
SavedVideoObject sv = new SavedVideoObject();
sv.Resolution = res;
sv.Video = JsonConvert.DeserializeObject<SavedVideo>(File.ReadAllText(infPath));
2021-08-03 19:39:03 +00:00
foreach (var tn in sv.Video.Thumbnails)
{
Functions.Downloader.DL._DownloadThumbnail2(tn.Item1, tn.Item2, sv.Video.Id, tn.Item3);
}
2021-06-25 08:30:45 +00:00
video.Add(sv);
}
else
{
try
{
var vinfo = await Functions.Downloader.DL.ytc.Videos.GetAsync(v.Id);
SavedVideoObject sv = SavedVideo.CreateFrom(res, vinfo, Functions.Downloader.DL._DownloadThumbnail);
video.Add(sv);
}
catch (Exception ex)
{
2021-06-26 03:22:31 +00:00
Console.WriteLine(ex.Message);
2021-06-25 08:30:45 +00:00
_ = ex;
}
}
}).Wait();
}
catch (Exception ex)
{
2021-06-26 03:22:31 +00:00
Console.WriteLine(ex.Message);
2021-06-25 08:30:45 +00:00
_ = ex;
}
}
catch (Exception ex2)
{
2021-06-26 03:22:31 +00:00
Console.WriteLine(ex2.Message);
2021-06-25 08:30:45 +00:00
_ = ex2;
}
return video.ToArray();
}
}
return new SavedVideoObject[0];
}
}
}