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