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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::{youtube::player_response::PlayerResponse, Client, Stream, Thumbnail};
use serde_json::Value;
use std::{sync::Arc, time::Duration};
#[derive(Debug)]
pub struct Video {
initial_data: Value,
player_response: PlayerResponse,
client: Arc<Client>,
}
impl Video {
pub(crate) async fn get(client: Arc<Client>, id: Id) -> crate::Result<Self> {
let player_response = client.api.player(id).await?.into_std()?;
Ok(Self {
initial_data: client.api.next(id).await?,
client,
player_response,
})
}
pub fn title(&self) -> &str {
&self.player_response.video_details.title
}
pub fn id(&self) -> Id {
self.player_response.video_details.video_id
}
pub fn duration(&self) -> Duration {
self.player_response.video_details.length_seconds
}
pub fn keywords(&self) -> &Vec<String> {
&self.player_response.video_details.keywords
}
pub fn channel(&self) -> Channel<'_> {
Channel {
client: Arc::clone(&self.client),
id: self.player_response.video_details.channel_id,
name: &self.player_response.video_details.author,
}
}
pub fn description(&self) -> &str {
&self.player_response.video_details.short_description
}
pub fn views(&self) -> u64 {
self.player_response.video_details.view_count
}
pub fn ratings(&self) -> Ratings {
if self.player_response.video_details.allow_ratings {
let fixed_tooltip = self.initial_data["contents"]["twoColumnWatchNextResults"]
["results"]["results"]["contents"]
.as_array()
.expect("InitialData contents was not an array")
.iter()
.find_map(|v| v.get("videoPrimaryInfoRenderer"))
.expect("InitialData contents did not have a videoPrimaryInfoRenderer")
["sentimentBar"]["sentimentBarRenderer"]["tooltip"]
.as_str()
.expect("sentimentBar tooltip was not a string")
.replace(',', "");
let (likes, dislikes) = fixed_tooltip
.split_once(" / ")
.expect("sentimentBar tooltip did not have a '/'");
let likes = likes
.parse()
.expect("Likes we not parsable as a unsigned integer");
let dislikes = dislikes
.parse()
.expect("Dislikes we not parsable as a unsigned integer");
Ratings::Allowed { likes, dislikes }
} else {
Ratings::NotAllowed
}
}
pub fn live(&self) -> bool {
self.player_response.video_details.is_live_content
}
pub fn thumbnails(&self) -> &Vec<Thumbnail> {
&self.player_response.video_details.thumbnail.thumbnails
}
pub fn age_restricted(&self) -> bool {
!self.family_safe()
}
fn microformat(&self) -> &crate::youtube::player_response::PlayerMicroformatRenderer {
&self.player_response.microformat.player_microformat_renderer
}
pub fn family_safe(&self) -> bool {
self.microformat().is_family_safe
}
pub fn unlisted(&self) -> bool {
self.microformat().is_unlisted
}
pub fn category(&self) -> &str {
&self.microformat().category
}
pub fn publish_date(&self) -> chrono::NaiveDate {
self.microformat().publish_date
}
pub fn upload_date(&self) -> chrono::NaiveDate {
self.microformat().upload_date
}
pub async fn streams(&self) -> crate::Result<impl Iterator<Item = Stream>> {
crate::stream::get(
Arc::clone(&self.client),
self.id(),
self.player_response.streaming_data.clone(),
)
.await
}
}
pub struct Channel<'a> {
client: Arc<Client>,
id: crate::channel::Id,
name: &'a str,
}
impl<'a> Channel<'a> {
pub fn id(&self) -> crate::channel::Id {
self.id
}
pub fn name(&self) -> &str {
self.name
}
pub async fn upgrade(&self) -> crate::Result<crate::Channel> {
self.client.channel(self.id).await
}
}
impl<'a> std::fmt::Debug for Channel<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Channel")
.field("id", &self.id)
.field("name", &self.name)
.finish()
}
}
#[derive(Debug)]
pub enum Ratings {
Allowed {
likes: u64,
dislikes: u64,
},
NotAllowed,
}
define_id! {
11,
"An Id describing a [`Video`]",
[
"https://www.youtube.com/watch?v=",
"https://youtu.be/",
"https://www.youtube.com/embed/",
]
}