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