src/structs/minivideo.rs
73 lines · 2629 bytes
1use chrono::{prelude::*, Utc};
2use invidious::structs::hidden::{PopularItem, TrendingVideo};
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: String,
13 pub author: String,
14 pub author_url: String,
15 pub published: String,
16 pub description: 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: original.viewCount.separate_with_commas(),
27 author: original.author,
28 author_url: original.authorUrl,
29 published: 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: 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: original.viewCount.separate_with_commas(),
55 author: original.author,
56 author_url: original.authorUrl,
57 published: 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: String::new(),
71 }
72 }
73}