src/global/structs/item.rs

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