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

127 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TYTD.Server.Functions;
using YoutubeExplode.Videos;
namespace TYTD.Server.Models
{
public enum Resolution
{
Convert=0,
NoConvert=1,
Audio=2
}
public struct ClosedCaptions
{
public ClosedCaptions(VideoId id)
{
VideoId = id;
}
public VideoId VideoId;
}
public class Chapter
{
public Chapter(TimeSpan ts, string name)
{
Offset = ts;
ChapterName = name;
}
public TimeSpan Offset { get; set; }
public string ChapterName { get; set; }
}
public class SavedVideoObject
{
public SavedVideoObject()
{
RegularFile = false;
}
public SavedVideoObject(string url)
{
RegularFile = true;
Video = new SavedVideo() { Id = url };
}
public bool RegularFile { get; set; }
public SavedVideo Video { get; set; }
public Resolution Resolution { get; set; }
}
public class SavedVideo
{
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;
}
public static SavedVideoObject CreateFrom(Resolution res,YoutubeExplode.Videos.Video v,Action<int,int,string,string> downloadThumbnail)
{
SavedVideo sv = new SavedVideo();
sv.Thumbnails = new List<Tuple<int, int, string>>();
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;
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();
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; }
public List<Tuple<int,int,string>> Thumbnails { get; set; }
}
}