-
Notifications
You must be signed in to change notification settings - Fork 20
/
PlaylistManager.cs
242 lines (202 loc) · 9.66 KB
/
PlaylistManager.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using InnerTube;
using InnerTube.Models;
using InnerTube.Protobuf;
using InnerTube.Renderers;
using LightTube.Database.Models;
using LightTube.Localization;
using MongoDB.Driver;
using Newtonsoft.Json.Linq;
namespace LightTube.Database;
public class PlaylistManager(
IMongoCollection<DatabasePlaylist> playlistCollection,
IMongoCollection<DatabaseVideo> videoCacheCollection)
{
private const string INNERTUBE_PLAYLIST_PANEL_VIDEO_RENDERER_TEMPLATE = "{\"title\":{\"simpleText\":\"%%TITLE%%\"},\"thumbnail\":{\"thumbnails\":[{\"url\":\"%%THUMBNAIL%%\",\"width\":0,\"height\":0}]},\"lengthText\":{\"simpleText\":\"%%DURATION%%\"},\"indexText\":{\"simpleText\":\"%%INDEX%%\"},\"selected\":%%SELECTED%%,\"navigationEndpoint\":{\"watchEndpoint\":{\"params\":\"OAE%3D\"}},\"videoId\":\"%%ID%%\",\"shortBylineText\":{\"runs\":[{\"text\":\"%%CHANNEL_TITLE%%\",\"navigationEndpoint\":{\"browseEndpoint\":{\"browseId\":\"%%CHANNEL_ID%%\"}}}]}}";
public IMongoCollection<DatabasePlaylist> PlaylistCollection { get; } = playlistCollection;
public IMongoCollection<DatabaseVideo> VideoCacheCollection { get; } = videoCacheCollection;
public DatabasePlaylist? GetPlaylist(string id) => PlaylistCollection.FindSync(x => x.Id == id).FirstOrDefault();
public IEnumerable<DatabasePlaylist> GetUserPlaylists(string userId, PlaylistVisibility minVisibility)
{
IAsyncCursor<DatabasePlaylist> unfiltered = PlaylistCollection.FindSync(x => x.Author == userId);
return unfiltered.ToList().Where(x => x.Visibility >= minVisibility);
}
public IEnumerable<RendererContainer> GetPlaylistVideoRenderers(string id, bool editable, LocalizationManager localization)
{
DatabasePlaylist? pl = GetPlaylist(id);
if (pl == null) return [];
List<RendererContainer> renderers = [];
for (int i = 0; i < pl.VideoIds.Count; i++)
{
string videoId = pl.VideoIds[i];
DatabaseVideo? video = VideoCacheCollection.FindSync(x => x.Id == videoId).FirstOrDefault();
RendererContainer container = new()
{
Type = "video",
OriginalType = "playlistVideoContainer",
Data = new PlaylistVideoRendererData
{
VideoId = editable ? videoId + "!" : videoId,
Title = video?.Title.Replace("\"", "\\\"") ?? localization.GetRawString("playlist.video.uncached"),
Thumbnails =
[
new Thumbnail
{
Url = video?.Thumbnails.LastOrDefault()?.Url.ToString() ?? "https://i.ytimg.com/vi//hqdefault.jpg",
Width = 480,
Height = 360
}
],
Author = video?.Channel.Id != null
? new Channel(
video.Channel.Id,
video?.Channel.Name.Replace("\"", "\\\"") ?? "???",
null,
null,
null,
null
)
: null,
Duration = InnerTube.Utils.ParseDuration(video?.Duration ?? "00:00"),
PublishedText = video?.UploadedAt,
ViewCountText = (video?.Views ?? 0).ToString(),
Badges = [],
Description = null,
VideoIndexText = (i + 1).ToString()
}
};
renderers.Add(container);
}
return renderers;
}
public List<DatabaseVideo> GetPlaylistVideos(string playlistId, LocalizationManager localization)
{
DatabasePlaylist? pl = GetPlaylist(playlistId);
return pl == null
? []
: pl.VideoIds.Select(id => VideoCacheCollection.FindSync(x => x.Id == id).FirstOrDefault()).ToList();
}
public string GetPlaylistPanelVideosJson(string id, string currentVideoId)
{
DatabasePlaylist? pl = GetPlaylist(id);
if (pl == null) return "";
List<string> renderers = [];
for (int i = 0; i < pl.VideoIds.Count; i++)
{
string videoId = pl.VideoIds[i];
DatabaseVideo? video = VideoCacheCollection.FindSync(x => x.Id == videoId).FirstOrDefault();
string json = $"{{\"playlistPanelVideoRenderer\":{INNERTUBE_PLAYLIST_PANEL_VIDEO_RENDERER_TEMPLATE}}}"
.Replace("%%ID%%", videoId)
.Replace("%%SELECTED%%", (currentVideoId == videoId).ToString().ToLower())
.Replace("%%INDEX%%", currentVideoId == videoId ? "▶" : (i + 1).ToString())
.Replace("%%TITLE%%", video?.Title.Replace("\"", "\\\"") ?? "Uncached video. Click to fix")
.Replace("%%THUMBNAIL%%", video?.Thumbnails.LastOrDefault()?.Url.ToString() ?? "https://i.ytimg.com/vi//hqdefault.jpg")
.Replace("%%DURATION%%", video?.Duration ?? "00:00")
.Replace("%%CHANNEL_TITLE%%", video?.Channel.Name.Replace("\"", "\\\"") ?? "???")
.Replace("%%CHANNEL_ID%%", video?.Channel.Id ?? "???");
renderers.Add(json);
}
return string.Join(",", renderers);
}
public async Task<DatabasePlaylist> CreatePlaylist(string token, string title, string description, PlaylistVisibility visibility)
{
DatabaseUser? u = await DatabaseManager.Users.GetUserFromToken(token);
if (u is null)
throw new UnauthorizedAccessException("Unauthorized");
DatabasePlaylist pl = new()
{
Id = DatabasePlaylist.GenerateId(),
Name = title,
Description = description,
Visibility = visibility,
VideoIds = [],
Author = u.UserID,
LastUpdated = DateTimeOffset.UtcNow
};
await PlaylistCollection.InsertOneAsync(pl);
return pl;
}
public async Task AddVideoToPlaylist(string token, string playlistId, InnerTubePlayer video)
{
DatabaseUser? u = await DatabaseManager.Users.GetUserFromToken(token);
DatabasePlaylist? playlist = GetPlaylist(playlistId);
if (u is null)
throw new UnauthorizedAccessException("Unauthorized");
if (playlist is null)
throw new KeyNotFoundException("Playlist not found");
if (u.UserID != playlist.Author)
throw new UnauthorizedAccessException("Unauthorized");
if (!playlist.VideoIds.Contains(video.Details.Id))
playlist.VideoIds.Add(video.Details.Id);
playlist.LastUpdated = DateTimeOffset.UtcNow;
await PlaylistCollection.ReplaceOneAsync(x => x.Id == playlistId, playlist);
await DatabaseManager.Cache.AddVideo(new DatabaseVideo
{
Id = video.Details.Id,
Title = video.Details.Title,
Thumbnails = [
new Thumbnail
{
Url = $"https://i.ytimg.com/vi/{video.Details.Id}/hqdefault.jpg",
Width = 480,
Height = 360
}
],
Views = 0,
Channel = new DatabaseVideoAuthor
{
Id = video.Details.Author.Id!,
Name = video.Details.Author.Title,
Avatars =
[
new Thumbnail
{
Url = video.Details.Author.Avatar!.First().Url
}
]
},
Duration = video.Details.Length.ToDurationString()
});
}
public async Task RemoveVideoFromPlaylist(string token, string playlistId, string videoId)
{
DatabaseUser? u = await DatabaseManager.Users.GetUserFromToken(token);
DatabasePlaylist? playlist = GetPlaylist(playlistId);
if (u is null)
throw new UnauthorizedAccessException("Unauthorized");
if (playlist is null)
throw new KeyNotFoundException("Playlist not found");
if (u.UserID != playlist.Author)
throw new UnauthorizedAccessException("Unauthorized");
playlist.VideoIds.Remove(videoId);
playlist.LastUpdated = DateTimeOffset.UtcNow;
await PlaylistCollection.ReplaceOneAsync(x => x.Id == playlistId, playlist);
}
public async Task EditPlaylist(string token, string id, string title, string description, PlaylistVisibility visibility)
{
DatabaseUser? u = await DatabaseManager.Users.GetUserFromToken(token);
DatabasePlaylist? playlist = GetPlaylist(id);
if (u is null)
throw new UnauthorizedAccessException("Unauthorized");
if (playlist is null)
throw new KeyNotFoundException("Playlist not found");
if (u.UserID != playlist.Author)
throw new UnauthorizedAccessException("Unauthorized");
playlist.Name = title;
playlist.Description = description;
playlist.Visibility = visibility;
playlist.LastUpdated = DateTimeOffset.UtcNow;
await PlaylistCollection.ReplaceOneAsync(x => x.Id == id, playlist);
}
public async Task DeletePlaylist(string token, string id)
{
DatabaseUser? u = await DatabaseManager.Users.GetUserFromToken(token);
DatabasePlaylist? playlist = GetPlaylist(id);
if (u is null)
throw new UnauthorizedAccessException("Unauthorized");
if (playlist is null)
throw new KeyNotFoundException("Playlist not found");
if (u.UserID != playlist.Author)
throw new UnauthorizedAccessException("Unauthorized");
await PlaylistCollection.DeleteOneAsync(x => x.Id == id);
}
}