src/structs/minivideo.rs

2737f153106b44565bbbab1d0b08ab3067071283
youtube-tui/src/structs/minivideo.rs history · blame · raw

107 lines · 4107 bytes

  1use chrono::{prelude::*, Utc};
  2use invidious::structs::hidden::{PopularItem, TrendingVideo, SearchItem};
  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}
 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: viewCount.separate_with_commas(),
 85                    author: author,
 86                    author_url: authorUrl,
 87                    published: 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: description,
101                }
102            }
103
104            _ => unreachable!(),
105        }
106    }
107}