src/structs/minivideo.rs
124 lines · 4702 bytes
1use chrono::{prelude::*, Utc};
2use invidious::structs::hidden::{PopularItem, TrendingVideo, SearchItem, PlaylistItem};
3use thousands::Separable;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct MiniVideo {
8 pub title: String,
9 pub video_id: String,
10 pub video_thumbnail: String,
11 pub length: String,
12 pub view_count: Option<String>,
13 pub author: String,
14 pub author_url: String,
15 pub published: Option<String>,
16 pub description: Option<String>,
17}
18
19impl From<TrendingVideo> for MiniVideo {
20 fn from(original: TrendingVideo) -> Self {
21 MiniVideo {
22 title: original.title,
23 video_id: original.videoId,
24 video_thumbnail: original.videoThumbnails[4].url.clone(),
25 length: hrtime::from_sec_padded(original.lengthSeconds as u64),
26 view_count: Some(original.viewCount.separate_with_commas()),
27 author: original.author,
28 author_url: original.authorUrl,
29 published: Some(format!(
30 "{}{}",
31 original.publishedText,
32 if Utc::now().timestamp() - original.published as i64 > 86400 {
33 let datetime: DateTime<Utc> = DateTime::from_utc(
34 NaiveDateTime::from_timestamp(original.published as i64, 0),
35 Utc,
36 );
37 format!(" [{}]", datetime.format("%Y/%m/%d"))
38 } else {
39 String::new()
40 }
41 )),
42 description: Some(original.description),
43 }
44 }
45}
46
47impl From<PopularItem> for MiniVideo {
48 fn from(original: PopularItem) -> Self {
49 MiniVideo {
50 title: original.title,
51 video_id: original.videoId,
52 video_thumbnail: original.videoThumbnails[5].url.clone(),
53 length: hrtime::from_sec_padded(original.lengthSeconds as u64),
54 view_count: Some(original.viewCount.separate_with_commas()),
55 author: original.author,
56 author_url: original.authorUrl,
57 published: Some(format!(
58 "{}{}",
59 original.publishedText,
60 if Utc::now().timestamp() - original.published as i64 > 86400 {
61 let datetime: DateTime<Utc> = DateTime::from_utc(
62 NaiveDateTime::from_timestamp(original.published as i64, 0),
63 Utc,
64 );
65 format!(" [{}]", datetime.format("%Y/%m/%d"))
66 } else {
67 String::new()
68 }
69 )),
70 description: None,
71 }
72 }
73}
74
75impl From<SearchItem> for MiniVideo {
76 fn from(original: SearchItem) -> Self {
77 match original {
78 SearchItem::Video { title, videoId, author, authorId: _, authorUrl, lengthSeconds, videoThumbnails, description, descriptionHtml: _, viewCount, published, publishedText, liveNow: _, paid: _, premium: _ } => {
79 MiniVideo {
80 title: title,
81 video_id: videoId,
82 video_thumbnail: videoThumbnails[0].url.clone(),
83 length: hrtime::from_sec_padded(lengthSeconds as u64),
84 view_count: Some(viewCount.separate_with_commas()),
85 author: author,
86 author_url: authorUrl,
87 published: Some(format!(
88 "{}{}",
89 publishedText,
90 if Utc::now().timestamp() - published as i64 > 86400 {
91 let datetime: DateTime<Utc> = DateTime::from_utc(
92 NaiveDateTime::from_timestamp(published as i64, 0),
93 Utc,
94 );
95 format!(" [{}]", datetime.format("%Y/%m/%d"))
96 } else {
97 String::new()
98 }
99 )),
100 description: Some(description),
101 }
102 }
103
104 _ => unreachable!(),
105 }
106 }
107}
108
109impl From<PlaylistItem> for MiniVideo {
110 fn from(original: PlaylistItem) -> Self {
111 MiniVideo {
112 title: original.title,
113 video_id: original.videoId,
114 video_thumbnail: original.videoThumbnails[4].url.clone(),
115 length: hrtime::from_sec_padded(original.lengthSeconds as u64),
116 view_count: None,
117 author: original.author,
118 author_url: original.authorUrl,
119 published: None,
120 description: None,
121
122 }
123 }
124}