1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Eq, PartialEq)]
pub enum MediaType {
Photo,
Gif,
Video,
Unknown(String),
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Media {
pub display_url: String,
pub expanded_url: String,
pub id: u64,
pub id_str: String,
pub indices: Vec<u32>,
pub media_url: String,
pub media_url_https: String,
pub sizes: Sizes,
pub source_status_id: Option<u64>,
pub source_status_id_str: Option<String>,
#[serde(rename="type")]
pub kind: MediaType,
pub url: String,
pub video_info: Option<VideoInfo>,
pub additional_media_info: Option<AdditionalMediaInfo>,
}
impl Media {
pub fn url(&self) -> Option<String> {
if self.kind == MediaType::Photo {
return Some(self.media_url_https.clone());
}
if let Some(vi) = &self.video_info {
let max = vi.variants.iter().max_by(|a, b| a.bitrate.cmp(&b.bitrate));
if let Some(var) = max {
return Some(var.url.clone());
}
}
return None;
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct VideoInfo {
pub aspect_ratio: (u32, u32),
pub duration_millis: Option<u32>,
pub variants: Vec<Variant>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Variant {
pub bitrate: Option<u32>,
pub content_type: String,
pub url: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct AdditionalMediaInfo {
pub title: Option<String>,
pub description: Option<String>,
pub embeddable: Option<bool>,
pub monetizable: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Sizes {
pub thumb: Size,
pub large: Size,
pub medium: Size,
pub small: Size,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Size {
pub w: u32,
pub h: u32,
pub resize: String,
}