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