-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTitle.cs
109 lines (88 loc) · 3.82 KB
/
AutoTitle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace YTPlayListDownloader.Collectors;
using System.Diagnostics;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
class AutoTitle : Collector
{
public AutoTitle(string url) : base(url)
{
}
private async Task<IEnumerable<Video>> Update(HashSet<Video> videos)
{
var playList = await yt.Playlists.GetVideosAsync(url).ToListAsync();
var newVideos = playList.Where(playList => !videos.Any(video => video.Id == playList.Id))
.Select(video =>
{
var Thumbnails = video.Thumbnails.OrderBy(thumbnail => thumbnail.Resolution.Width).ToList();
var url = Thumbnails.LastOrDefault()!.Url;
return new Video(video.Id, video.Title, "", url);
})
.Concat(videos);
Console.WriteLine($"origin = {videos.Count}, updated = {newVideos.Count()}");
return newVideos;
}
private async Task<HashSet<Video>> UpdatePlayListInfo(IEnumerable<Video> videos)
{
var playList = await yt.Playlists.GetVideosAsync(url).ToListAsync();
var PlaylistInfo = await yt.Playlists.GetAsync(url);
string[] special = new string[] { "ry3Tupx4BL4", "87moOXPTtSk", "4QXCPuwBz2E", "0_pfGRDugxg" };
return videos.Select(v =>
{
var InPlaylist = playList.FirstOrDefault((playList) => playList.Id == v.Id);
if (InPlaylist != null)
{
if (special.Contains(v.Id))
{
Console.WriteLine($"{playList.IndexOf(InPlaylist) + 1} - {v.Title}");
}
var check = v.Playlists.FirstOrDefault((playList) => playList.Id == PlaylistInfo.Id);
if (check != null)
{
v.Playlists = v.Playlists.Where(playList => playList.Id == PlaylistInfo.Id)
.Select(ytAlbum =>
{
ytAlbum.Position = playList.IndexOf(InPlaylist) + 1;
return ytAlbum;
})
.ToHashSet();
}
else
{
var id = PlaylistInfo.Id;
var owner = PlaylistInfo.Author?.ChannelTitle!.Replace("by", "").Trim();
var title = PlaylistInfo.Title;
var track = playList.IndexOf(InPlaylist) + 1;
v.Playlists.Add(new PlaylistInfo(id, owner, title, track));
}
}
return v;
})
.ToHashSet(); ;
}
public override async Task Invoke()
{
Console.WriteLine("updating...");
Stopwatch watch = new Stopwatch();
watch.Start();
string jsonFile =
await File.ReadAllTextAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./customTitle.json"));
var jsonContent = JsonSerializer.Deserialize<Videos>(
jsonFile,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
);
jsonContent!.items = await UpdatePlayListInfo(await Update(jsonContent.items));
if (jsonContent != null)
{
jsonContent.items = jsonContent.items.ToList().OrderBy(v => v.Title).ToHashSet();
var finalJson = JsonSerializer.Serialize<Videos>(
jsonContent,
new JsonSerializerOptions
{ WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }
);
await File.WriteAllTextAsync("./customTitle.json", finalJson);
}
watch.Stop();
Console.WriteLine($"update successful ☑ {watch.Elapsed}");
}
}