src/global/structs/item.rs
433 lines · 14532 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 pub fn fullchannel(&self) -> Result<&FullChannelItem, Errors> {
197 match self {
198 Self::FullChannel(fullchannel) => Ok(fullchannel),
199 _ => Err(Errors::StrError("not a full channel")),
200 }
201 }
202}
203
204impl Item {
205 /// returns the file save path for thumbnails (which is by their id)
206 /// returns `invalid` if the item does not have a thumbnail
207 pub fn thumbnail_id(&self) -> &str {
208 match self {
209 Self::MiniVideo(video) => &video.id,
210 Self::MiniPlaylist(playlist) => &playlist.id,
211 Self::MiniChannel(channel) => &channel.id,
212 Self::FullVideo(video) => &video.id,
213 Self::FullPlaylist(playlist) => &playlist.id,
214 Self::FullChannel(channel) => &channel.id,
215 Self::Unknown(_) | Self::Page(_) => "invalid",
216 }
217 }
218
219 /// parse `TrendingVideo` into `Self`
220 pub fn from_trending_video(original: TrendingVideo, image_index: usize) -> Self {
221 Self::MiniVideo(MiniVideoItem {
222 title: original.title,
223 id: original.id,
224 thumbnail_url: original.thumbmails[image_index].url.clone(),
225 length: secs_display_string(original.length),
226 views: Some(viewcount_text(original.views)),
227 channel: original.author,
228 channel_id: original.author_id,
229 published: Some(format!(
230 "{} [{}]",
231 original.published_text,
232 date_text(original.published)
233 )),
234 description: Some(original.description),
235 })
236 }
237
238 /// parse `PopularItem` into `Self`
239 pub fn from_popular_item(original: PopularItem, image_index: usize) -> Self {
240 Self::MiniVideo(MiniVideoItem {
241 title: original.title,
242 id: original.id,
243 thumbnail_url: original.thumbnails[image_index].url.clone(),
244 length: secs_display_string(original.length),
245 views: Some(viewcount_text(original.views)),
246 channel: original.author,
247 channel_id: original.author_id,
248 published: Some(format!(
249 "{} [{}]",
250 original.published_text,
251 date_text(original.published)
252 )),
253 description: None,
254 })
255 }
256
257 /// parse `Playlist` into `Self`
258 pub fn from_mini_playlist(original: Playlist) -> Self {
259 Self::MiniPlaylist(MiniPlaylistItem {
260 title: original.title,
261 id: original.id,
262 channel: original.author,
263 channel_id: original.author_id,
264 video_count: original.video_count,
265 thumbnail_url: original.thumbnail,
266 })
267 }
268
269 /// parse `SearchItem` into `Self`
270 pub fn from_search_item(original: SearchItem, image_index: usize) -> Self {
271 match original {
272 SearchItem::Video {
273 title,
274 id,
275 author,
276 author_id,
277 length,
278 thumbnails,
279 description,
280 views,
281 published,
282 published_text,
283 ..
284 } => Self::MiniVideo(MiniVideoItem {
285 title,
286 id,
287 thumbnail_url: thumbnails[image_index].url.clone(),
288 length: secs_display_string(length as u32),
289 views: Some(viewcount_text(views)),
290 channel: author,
291 channel_id: author_id,
292 published: Some(format!("{} [{}]", published_text, date_text(published))),
293 description: Some(description),
294 }),
295 SearchItem::Playlist {
296 title,
297 id,
298 author,
299 author_id,
300 video_count,
301 thumbnail,
302 ..
303 } => Self::MiniPlaylist(MiniPlaylistItem {
304 title,
305 id,
306 channel: author,
307 channel_id: author_id,
308 video_count,
309 thumbnail_url: thumbnail,
310 }),
311 SearchItem::Channel {
312 name,
313 id,
314 thumbnails,
315 subscribers,
316 video_count,
317 description,
318 ..
319 } => Self::MiniChannel(MiniChannelItem {
320 name,
321 id,
322 thumbnail_url: format!("https://{}", thumbnails[image_index].url),
323 video_count,
324 sub_count: subscribers,
325 sub_count_text: viewcount_text(subscribers as u64),
326 description,
327 }),
328 SearchItem::Unknown(searchitem_transitional) => Self::Unknown(searchitem_transitional),
329 }
330 }
331
332 /// parse `Video` into `Self`
333 pub fn from_full_video(original: Video, image_index: usize) -> Self {
334 Self::FullVideo(FullVideoItem {
335 title: original.title,
336 id: original.id,
337 thumbnail_url: original.thumbnails[image_index].url.clone(),
338 length: secs_display_string(original.length),
339 views: viewcount_text(original.views),
340 channel: original.author,
341 channel_id: original.author_id,
342 sub_count: original.sub_count_text,
343 published: format!(
344 "{} [{}]",
345 original.published_text,
346 date_text(original.published)
347 ),
348 description: original.description,
349 likes: viewcount_text(original.likes as u64),
350 genre: original.genre,
351 })
352 }
353
354 /// parse `FullPlaylist` into `Self`
355 pub fn from_full_playlist(original: FullPlaylist, image_index: usize) -> Self {
356 Self::FullPlaylist(FullPlaylistItem {
357 title: original.title,
358 id: original.id,
359 channel: original.author,
360 channel_id: original.author_id,
361 video_count: original.video_count,
362 description: original.description,
363 views: viewcount_text(original.views),
364 thumbnail_url: original.thumbnail,
365 videos: original
366 .videos
367 .into_iter()
368 .map(|video| Self::from_playlist_item(video, image_index))
369 .collect(),
370 })
371 }
372
373 /// parse `PlaylistItem` into `Self`
374 pub fn from_playlist_item(original: PlaylistItem, image_index: usize) -> Self {
375 Self::MiniVideo(MiniVideoItem {
376 title: original.title,
377 id: original.id,
378 thumbnail_url: original.thumbnails[image_index].url.clone(),
379 length: secs_display_string(original.length),
380 views: None,
381 channel: original.author,
382 channel_id: original.author_id,
383 published: None,
384 description: None,
385 })
386 }
387
388 /// parse `Channel` into `Self`
389 pub fn from_full_channel(original: Channel, image_index: usize) -> Self {
390 Self::FullChannel(FullChannelItem {
391 name: original.name,
392 id: original.id,
393 thumbnail_url: original.thumbnails[image_index].url.clone(),
394 sub_count: original.sub_count,
395 sub_count_text: viewcount_text(original.sub_count as u64),
396 total_views: viewcount_text(original.total_views),
397 created: date_text(original.joined),
398 autogenerated: original.auto_generated,
399 description: original.description,
400 })
401 }
402
403 /// parse `ChannelVideo` into `Self`
404 pub fn from_channel_video(original: ChannelVideo, image_index: usize) -> Self {
405 Self::MiniVideo(MiniVideoItem {
406 title: original.title,
407 id: original.id,
408 thumbnail_url: original.thumbnails[image_index].url.clone(),
409 length: secs_display_string(original.length),
410 views: Some(viewcount_text(original.view_count)),
411 channel: original.author,
412 channel_id: original.author_id,
413 published: Some(format!(
414 "{} [{}]",
415 original.published_text,
416 date_text(original.published)
417 )),
418 description: Some(original.description),
419 })
420 }
421
422 /// parse `Playlist` into `Self`
423 pub fn from_channel_playlist(original: Playlist) -> Self {
424 Self::MiniPlaylist(MiniPlaylistItem {
425 title: original.title,
426 id: original.id,
427 channel: original.author,
428 channel_id: original.author_id,
429 video_count: original.video_count,
430 thumbnail_url: original.thumbnail,
431 })
432 }
433}