src/structs/minivideo.rs

641938ba1f255069a13382c2a2903a32612e3873
youtube-tui/src/structs/minivideo.rs history · blame · raw

72 lines · 2567 bytes

 1use thousands::Separable;
 2use chrono::{prelude::*, Utc};
 3use invidious::structs::hidden::{PopularItem, TrendingVideo};
 4
 5#[derive(Debug, Clone)]
 6pub struct MiniVideo {
 7    pub title: String,
 8    pub video_id: String,
 9    pub video_thumbnail: String,
10    pub length: String,
11    pub view_count: String,
12    pub author: String,
13    pub author_url: String,
14    pub published: String,
15    pub description: String,
16}
17
18impl From<TrendingVideo> for MiniVideo {
19    fn from(original: TrendingVideo) -> Self {
20        MiniVideo {
21            title: original.title,
22            video_id: original.videoId,
23            video_thumbnail: original.videoThumbnails[4].url.clone(),
24            length: hrtime::from_sec_padded(original.lengthSeconds as u64),
25            view_count: original.viewCount.separate_with_commas(),
26            author: original.author,
27            author_url: original.authorUrl,
28            published: format!(
29                "{}{}",
30                original.publishedText,
31                if Utc::now().timestamp() - original.published as i64 > 86400 {
32                    let datetime: DateTime<Utc> = DateTime::from_utc(
33                        NaiveDateTime::from_timestamp(original.published as i64, 0),
34                        Utc,
35                    );
36                    format!(" [{}]", datetime.format("%Y/%m/%d"))
37                } else {
38                    String::new()
39                }
40            ),
41            description: original.description,
42        }
43    }
44}
45
46impl From<PopularItem> for MiniVideo {
47    fn from(original: PopularItem) -> Self {
48        MiniVideo {
49            title: original.title,
50            video_id: original.videoId,
51            video_thumbnail: original.videoThumbnails[5].url.clone(),
52            length: hrtime::from_sec_padded(original.lengthSeconds as u64),
53            view_count: original.viewCount.separate_with_commas(),
54            author: original.author,
55            author_url: original.authorUrl,
56            published: format!(
57                "{}{}",
58                original.publishedText,
59                if Utc::now().timestamp() - original.published as i64 > 86400 {
60                    let datetime: DateTime<Utc> = DateTime::from_utc(
61                        NaiveDateTime::from_timestamp(original.published as i64, 0),
62                        Utc,
63                    );
64                    format!("[{}]", datetime.format("%Y/%m/%d"))
65                } else {
66                    String::new()
67                }
68            ),
69            description: String::new(),
70        }
71    }
72}