src/global/structs/item.rs

443 lines · 14750 bytes

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