src/global/structs/item.rs
434 lines · 14573 bytes
1use crate::global::functions::{date_text, secs_display_string, viewcount_text};
2use invidious::structs::{
3 channel::Channel,
4 hidden::{
5 ChannelVideo, Playlist, PlaylistItem, PopularItem, SearchItem, SearchItemTransition,
6 TrendingVideo,
7 },
8 universal::Playlist as FullPlaylist,
9 video::Video,
10};
11use serde::{Deserialize, Serialize};
12use std::fmt::Display;
13
14use super::Errors;
15
16/// Items are things like a single video/playlist and channel
17// they are displayed by the item info widget in `iteminfo.rs`
18#[derive(Clone, Serialize, Deserialize)]
19#[serde(tag = "type", content = "data")]
20pub enum Item {
21 // minivideos are all videos that appeared without actually clicking into the video
22 MiniVideo(MiniVideoItem),
23 MiniPlaylist(MiniPlaylistItem),
24 MiniChannel(MiniChannelItem),
25 FullVideo(FullVideoItem),
26 FullPlaylist(FullPlaylistItem),
27 FullChannel(FullChannelItem),
28 Page(bool), // true: next false: prev
29 Unknown(SearchItemTransition),
30}
31
32/// stores information of a previewed video
33#[derive(Clone, Serialize, Deserialize)]
34pub struct MiniVideoItem {
35 pub title: String,
36 pub id: String,
37 pub thumbnail_url: String,
38 pub length: String,
39 pub views: Option<String>,
40 pub channel: String,
41 pub channel_id: String,
42 pub published: Option<String>,
43 pub description: Option<String>,
44}
45
46/// stores information of a previewed playlist
47#[derive(Clone, Serialize, Deserialize)]
48pub struct MiniPlaylistItem {
49 pub title: String,
50 pub id: String,
51 pub channel: String,
52 pub channel_id: String,
53 pub video_count: u32,
54 pub thumbnail_url: String,
55}
56
57/// stores information of a previewed channel
58#[derive(Clone, Serialize, Deserialize)]
59pub struct MiniChannelItem {
60 pub name: String,
61 pub id: String,
62 pub thumbnail_url: String,
63 pub sub_count: u32,
64 pub sub_count_text: String,
65 pub video_count: u32,
66 pub description: String,
67}
68
69/// stores information of a viewed video
70#[derive(Clone, Serialize, Deserialize)]
71pub struct FullVideoItem {
72 pub title: String,
73 pub id: String,
74 pub thumbnail_url: String,
75 pub length: String,
76 pub views: String,
77 pub channel: String,
78 pub channel_id: String,
79 pub sub_count: String,
80 pub published: String,
81 pub description: String,
82 pub likes: String,
83 // pub dislikes: Option<String>, TODO
84 pub genre: String,
85}
86
87/// stores information of a viewed playlist
88#[derive(Clone, Serialize, Deserialize)]
89pub struct FullPlaylistItem {
90 pub title: String,
91 pub id: String,
92 pub channel: String,
93 pub channel_id: String,
94 pub video_count: u32,
95 pub description: String,
96 pub views: String,
97 pub thumbnail_url: String,
98 pub videos: Vec<Item>,
99}
100
101/// stores information of a viewed channel
102#[derive(Clone, Serialize, Deserialize)]
103pub struct FullChannelItem {
104 pub name: String,
105 pub id: String,
106 pub thumbnail_url: String,
107 pub sub_count: u32,
108 pub sub_count_text: String,
109 pub total_views: String,
110 pub created: String,
111 pub autogenerated: bool,
112 pub description: String,
113}
114
115impl Display for Item {
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 f.write_str(match self {
118 Self::MiniVideo(video) => &video.title,
119 Self::MiniPlaylist(playlist) => &playlist.title,
120 Self::MiniChannel(channel) => &channel.name,
121 Self::FullVideo(video) => &video.title,
122 Self::FullPlaylist(playlist) => &playlist.title,
123 Self::FullChannel(channel) => &channel.name,
124 Self::Page(b) => {
125 if *b {
126 "Next page"
127 } else {
128 "Previous page"
129 }
130 }
131 Self::Unknown(_) => "Unknown item",
132 })
133 }
134}
135
136impl Item {
137 /// returns the id of an item, `None` is self is Unknown
138 pub fn id(&self) -> Option<&str> {
139 match self {
140 Self::MiniVideo(MiniVideoItem { id, .. })
141 | Self::MiniChannel(MiniChannelItem { id, .. })
142 | Self::MiniPlaylist(MiniPlaylistItem { id, .. })
143 | Self::FullVideo(FullVideoItem { id, .. })
144 | Self::FullChannel(FullChannelItem { id, .. })
145 | Self::FullPlaylist(FullPlaylistItem { id, .. }) => Some(id),
146 Self::Unknown(_) | Self::Page(_) => None,
147 }
148 }
149
150 /// check if self is Self::Unknown
151 pub fn is_unknown(&self) -> bool {
152 matches!(self, Self::Unknown(_))
153 }
154}
155
156impl Item {
157 /// stript self into a FullVideoItem
158 pub fn fullvideo(&self) -> Result<&FullVideoItem, Errors> {
159 match self {
160 Self::FullVideo(fullvideo) => Ok(fullvideo),
161 _ => Err(Errors::StrError("not a full video")),
162 }
163 }
164
165 /// stript self into a MiniVideoItem
166 pub fn minivideo(&self) -> Result<&MiniVideoItem, Errors> {
167 match self {
168 Self::MiniVideo(minivideo) => Ok(minivideo),
169 _ => Err(Errors::StrError("not a mini video")),
170 }
171 }
172
173 /// stript self into a MiniPlaylistItem
174 pub fn miniplaylist(&self) -> Result<&MiniPlaylistItem, Errors> {
175 match self {
176 Self::MiniPlaylist(miniplaylist) => Ok(miniplaylist),
177 _ => Err(Errors::StrError("not a mini playlist")),
178 }
179 }
180
181 /// stript self into a FullPlaylistItem
182 pub fn fullplaylist(&self) -> Result<&FullPlaylistItem, Errors> {
183 match self {
184 Self::FullPlaylist(fullplaylist) => Ok(fullplaylist),
185 _ => Err(Errors::StrError("not a full playlist")),
186 }
187 }
188
189 /// stript self into a MiniChannelItem
190 pub fn minichannel(&self) -> Result<&MiniChannelItem, Errors> {
191 match self {
192 Self::MiniChannel(minichannel) => Ok(minichannel),
193 _ => Err(Errors::StrError("not a mini channel")),
194 }
195 }
196
197 pub fn fullchannel(&self) -> Result<&FullChannelItem, Errors> {
198 match self {
199 Self::FullChannel(fullchannel) => Ok(fullchannel),
200 _ => Err(Errors::StrError("not a full channel")),
201 }
202 }
203}
204
205impl Item {
206 /// returns the file save path for thumbnails (which is by their id)
207 /// returns `invalid` if the item does not have a thumbnail
208 pub fn thumbnail_id(&self) -> &str {
209 match self {
210 Self::MiniVideo(video) => &video.id,
211 Self::MiniPlaylist(playlist) => &playlist.id,
212 Self::MiniChannel(channel) => &channel.id,
213 Self::FullVideo(video) => &video.id,
214 Self::FullPlaylist(playlist) => &playlist.id,
215 Self::FullChannel(channel) => &channel.id,
216 Self::Unknown(_) | Self::Page(_) => "invalid",
217 }
218 }
219
220 /// parse `TrendingVideo` into `Self`
221 pub fn from_trending_video(original: TrendingVideo, image_index: usize) -> Self {
222 Self::MiniVideo(MiniVideoItem {
223 title: original.title,
224 id: original.id,
225 thumbnail_url: original.thumbmails[image_index].url.clone(),
226 length: secs_display_string(original.length),
227 views: Some(viewcount_text(original.views)),
228 channel: original.author,
229 channel_id: original.author_id,
230 published: Some(format!(
231 "{} [{}]",
232 original.published_text,
233 date_text(original.published)
234 )),
235 description: Some(original.description),
236 })
237 }
238
239 /// parse `PopularItem` into `Self`
240 pub fn from_popular_item(original: PopularItem, image_index: usize) -> Self {
241 Self::MiniVideo(MiniVideoItem {
242 title: original.title,
243 id: original.id,
244 thumbnail_url: original.thumbnails[image_index].url.clone(),
245 length: secs_display_string(original.length),
246 views: Some(viewcount_text(original.views)),
247 channel: original.author,
248 channel_id: original.author_id,
249 published: Some(format!(
250 "{} [{}]",
251 original.published_text,
252 date_text(original.published)
253 )),
254 description: None,
255 })
256 }
257
258 /// parse `Playlist` into `Self`
259 pub fn from_mini_playlist(original: Playlist) -> Self {
260 Self::MiniPlaylist(MiniPlaylistItem {
261 title: original.title,
262 id: original.id,
263 channel: original.author,
264 channel_id: original.author_id,
265 video_count: original.video_count,
266 thumbnail_url: original.thumbnail,
267 })
268 }
269
270 /// parse `SearchItem` into `Self`
271 pub fn from_search_item(original: SearchItem, image_index: usize) -> Self {
272 match original {
273 SearchItem::Video {
274 title,
275 id,
276 author,
277 author_id,
278 length,
279 thumbnails,
280 description,
281 views,
282 published,
283 published_text,
284 ..
285 } => Self::MiniVideo(MiniVideoItem {
286 title,
287 id,
288 thumbnail_url: thumbnails[image_index].url.clone(),
289 length: secs_display_string(length as u32),
290 views: Some(viewcount_text(views)),
291 channel: author,
292 channel_id: author_id,
293 published: Some(format!("{} [{}]", published_text, date_text(published))),
294 description: Some(description),
295 }),
296 SearchItem::Playlist {
297 title,
298 id,
299 author,
300 author_id,
301 video_count,
302 thumbnail,
303 ..
304 } => Self::MiniPlaylist(MiniPlaylistItem {
305 title,
306 id,
307 channel: author,
308 channel_id: author_id,
309 video_count,
310 thumbnail_url: thumbnail,
311 }),
312 SearchItem::Channel {
313 name,
314 id,
315 thumbnails,
316 subscribers,
317 video_count,
318 description,
319 ..
320 } => Self::MiniChannel(MiniChannelItem {
321 name,
322 id,
323 thumbnail_url: format!("https://{}", thumbnails[image_index].url),
324 video_count,
325 sub_count: subscribers,
326 sub_count_text: viewcount_text(subscribers as u64),
327 description,
328 }),
329 SearchItem::Unknown(searchitem_transitional) => Self::Unknown(searchitem_transitional),
330 }
331 }
332
333 /// parse `Video` into `Self`
334 pub fn from_full_video(original: Video, image_index: usize) -> Self {
335 Self::FullVideo(FullVideoItem {
336 title: original.title,
337 id: original.id,
338 thumbnail_url: original.thumbnails[image_index].url.clone(),
339 length: secs_display_string(original.length),
340 views: viewcount_text(original.views),
341 channel: original.author,
342 channel_id: original.author_id,
343 sub_count: original.sub_count_text,
344 published: format!(
345 "{} [{}]",
346 original.published_text,
347 date_text(original.published)
348 ),
349 description: original.description,
350 likes: viewcount_text(original.likes as u64),
351 genre: original.genre,
352 })
353 }
354
355 /// parse `FullPlaylist` into `Self`
356 pub fn from_full_playlist(original: FullPlaylist, image_index: usize) -> Self {
357 Self::FullPlaylist(FullPlaylistItem {
358 title: original.title,
359 id: original.id,
360 channel: original.author,
361 channel_id: original.author_id,
362 video_count: original.video_count,
363 description: original.description,
364 views: viewcount_text(original.views),
365 thumbnail_url: original.thumbnail,
366 videos: original
367 .videos
368 .into_iter()
369 .map(|video| Self::from_playlist_item(video, image_index))
370 .collect(),
371 })
372 }
373
374 /// parse `PlaylistItem` into `Self`
375 pub fn from_playlist_item(original: PlaylistItem, image_index: usize) -> Self {
376 Self::MiniVideo(MiniVideoItem {
377 title: original.title,
378 id: original.id,
379 thumbnail_url: original.thumbnails[image_index].url.clone(),
380 length: secs_display_string(original.length),
381 views: None,
382 channel: original.author,
383 channel_id: original.author_id,
384 published: None,
385 description: None,
386 })
387 }
388
389 /// parse `Channel` into `Self`
390 pub fn from_full_channel(original: Channel, image_index: usize) -> Self {
391 Self::FullChannel(FullChannelItem {
392 name: original.name,
393 id: original.id,
394 thumbnail_url: original.thumbnails[image_index].url.clone(),
395 sub_count: original.sub_count,
396 sub_count_text: viewcount_text(original.sub_count as u64),
397 total_views: viewcount_text(original.total_views),
398 created: date_text(original.joined),
399 autogenerated: original.auto_generated,
400 description: original.description,
401 })
402 }
403
404 /// parse `ChannelVideo` into `Self`
405 pub fn from_channel_video(original: ChannelVideo, image_index: usize) -> Self {
406 Self::MiniVideo(MiniVideoItem {
407 title: original.title,
408 id: original.id,
409 thumbnail_url: original.thumbnails[image_index].url.clone(),
410 length: secs_display_string(original.length),
411 views: Some(viewcount_text(original.view_count)),
412 channel: original.author,
413 channel_id: original.author_id,
414 published: Some(format!(
415 "{} [{}]",
416 original.published_text,
417 date_text(original.published)
418 )),
419 description: Some(original.description),
420 })
421 }
422
423 /// parse `Playlist` into `Self`
424 pub fn from_channel_playlist(original: Playlist) -> Self {
425 Self::MiniPlaylist(MiniPlaylistItem {
426 title: original.title,
427 id: original.id,
428 channel: original.author,
429 channel_id: original.author_id,
430 video_count: original.video_count,
431 thumbnail_url: original.thumbnail,
432 })
433 }
434}