src/global/structs/item.rs

428 lines · 14392 bytes

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