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

143 lines
4.5 KiB
C#
Raw Permalink Normal View History

2021-06-24 01:10:20 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2021-12-08 01:07:43 +00:00
using TYTD.Server.Functions;
using YoutubeExplode.Videos;
2021-12-08 22:02:30 +00:00
2021-12-08 01:07:43 +00:00
namespace TYTD.Server.Models
2021-06-24 01:10:20 +00:00
{
public enum Resolution
{
Convert=0,
NoConvert=1,
Audio=2
}
public struct ClosedCaptions
{
public ClosedCaptions(VideoId id)
{
VideoId = id;
}
public VideoId VideoId;
}
2021-12-08 01:07:43 +00:00
public class Chapter
{
public Chapter(TimeSpan ts, string name)
{
Offset = ts;
ChapterName = name;
}
public TimeSpan Offset { get; set; }
public string ChapterName { get; set; }
}
2021-06-24 01:10:20 +00:00
public class SavedVideoObject
{
public SavedVideoObject()
{
Video = new SavedVideo();
RegularFile = false;
}
public SavedVideoObject(string url)
{
RegularFile = true;
Video = new SavedVideo() { Id = url };
}
public bool RegularFile { get; set; }
2021-06-24 01:10:20 +00:00
public SavedVideo Video { get; set; }
public Resolution Resolution { get; set; }
}
public class SavedVideo
{
public SavedVideo()
{
Id = "";
Title = "";
AuthorChannelId = "";
AuthorTitle = "";
Description = "";
Keywords = new string[0];
Likes = 0;
Dislikes = 0;
Views = 0;
Duration = 0.0;
}
2021-12-08 01:07:43 +00:00
public List<Chapter> GetChapters()
{
List<Chapter> chapters = new List<Chapter>();
//a line should start with time then be followed by info
bool found_0 = false;
foreach (var line in Description.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] splitLine = line.Split(new char[] { ' ' }, 2);
if (splitLine.Length == 2)
{
if (found_0)
{
TimeSpan time;
if (TimeSpan.TryParse(splitLine[0], out time))
{
chapters.Add(new Chapter(time, splitLine[1]));
}
}
else
if (splitLine[0] == "0:00")
{
found_0 = true;
chapters.Add(new Chapter(TimeSpan.FromSeconds(0), splitLine[1]));
}
}
}
return chapters;
}
2021-06-24 01:10:20 +00:00
public static SavedVideoObject CreateFrom(Resolution res,YoutubeExplode.Videos.Video v,Action<int,int,string,string> downloadThumbnail)
{
2021-08-03 19:39:03 +00:00
2021-06-24 01:10:20 +00:00
SavedVideo sv = new SavedVideo();
2021-08-03 19:39:03 +00:00
sv.Thumbnails = new List<Tuple<int, int, string>>();
2021-06-24 01:10:20 +00:00
sv.Title = v.Title;
sv.UploadDate = v.UploadDate.ToString();
sv.Id = v.Id;
sv.AuthorChannelId = v.Author.ChannelId;
sv.AuthorTitle = v.Author.Title;
sv.Description = v.Description;
sv.Dislikes = v.Engagement.DislikeCount;
sv.Duration = v.Duration.GetValueOrDefault().TotalSeconds;
sv.Keywords = v.Keywords.ToArray();
sv.Likes = v.Engagement.LikeCount;
sv.Views = v.Engagement.ViewCount;
2021-08-03 19:39:03 +00:00
sv.Thumbnails = v.Thumbnails.Select<YoutubeExplode.Common.Thumbnail,Tuple<int,int,string>>((e) => { return new Tuple<int, int, string>(e.Resolution.Width, e.Resolution.Height, e.Url); }).ToList();
2021-06-24 01:10:20 +00:00
foreach(var thumb in v.Thumbnails)
{
downloadThumbnail(thumb.Resolution.Width, thumb.Resolution.Height,v.Id, thumb.Url);
}
SavedVideoObject obj = new SavedVideoObject();
obj.Video = sv;
obj.Resolution = res;
return obj;
}
public string Title { get; set; }
public string UploadDate { get; set; }
public string[] Keywords { get; set; }
public string Id { get; set; }
public string AuthorTitle { get; set; }
public string AuthorChannelId { get; set; }
public string Description { get; set; }
public double Duration { get; set; }
public long Views { get; set; }
public long Likes { get; set; }
public long Dislikes { get; set; }
2021-08-03 19:39:03 +00:00
public List<Tuple<int,int,string>> Thumbnails { get; set; }
2021-06-24 01:10:20 +00:00
}
}