finished client refactor, client2 -> client
This commit is contained in:
parent
05f609e247
commit
8548bc81e9
39 changed files with 1484 additions and 11468 deletions
|
|
@ -1,41 +0,0 @@
|
|||
use anyhow::Result;
|
||||
use reqwest::Method;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{response, ClientType, ContextYT, RustyTube};
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct QChannel {
|
||||
context: ContextYT,
|
||||
browse_id: String,
|
||||
params: String,
|
||||
}
|
||||
|
||||
impl RustyTube {
|
||||
async fn get_channel_response(&self, channel_id: &str) -> Result<response::Channel> {
|
||||
let client = self.get_ytclient(ClientType::Desktop);
|
||||
let context = client.get_context(true).await;
|
||||
|
||||
let request_body = QChannel {
|
||||
context,
|
||||
browse_id: channel_id.to_owned(),
|
||||
params: "EgZ2aWRlb3PyBgQKAjoA".to_owned(),
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "browse")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
Ok(resp.json::<response::Channel>().await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
}
|
||||
1332
src/client/mod.rs
1332
src/client/mod.rs
File diff suppressed because it is too large
Load diff
|
|
@ -1,20 +1,28 @@
|
|||
use std::{
|
||||
borrow::Cow,
|
||||
collections::{BTreeMap, HashMap},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
|
||||
use fancy_regex::Regex;
|
||||
use log::{error, warn};
|
||||
use once_cell::sync::Lazy;
|
||||
use reqwest::Method;
|
||||
use reqwest::{Method, Url};
|
||||
use serde::Serialize;
|
||||
use url::Url;
|
||||
|
||||
use super::{response, ClientType, ContextYT, RustyTube, YTClient};
|
||||
use crate::{client::response::player, deobfuscate::Deobfuscator, model::*, util};
|
||||
use crate::{
|
||||
deobfuscate::Deobfuscator,
|
||||
model::{
|
||||
AudioCodec, AudioFormat, AudioStream, AudioTrack, Channel, Language, Subtitle, VideoCodec,
|
||||
VideoFormat, VideoInfo, VideoPlayer, VideoStream,
|
||||
},
|
||||
util,
|
||||
};
|
||||
|
||||
use super::{
|
||||
response::{self, player},
|
||||
ClientType, ContextYT, MapResponse, MapResult, RustyPipeQuery,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
|
@ -49,58 +57,211 @@ struct QContentPlaybackContext {
|
|||
referer: String,
|
||||
}
|
||||
|
||||
impl RustyTube {
|
||||
pub async fn get_player(&self, video_id: &str, client_type: ClientType) -> Result<VideoPlayer> {
|
||||
let client = self.get_ytclient(client_type);
|
||||
let (context, deobf) = tokio::join!(
|
||||
client.get_context(false),
|
||||
Deobfuscator::from_fetched_info(client.http_client(), self.cache.clone())
|
||||
);
|
||||
let deobf = deobf?;
|
||||
let request_body = build_request_body(client.clone(), &deobf, context, video_id);
|
||||
impl RustyPipeQuery {
|
||||
pub async fn get_player(self, video_id: &str, client_type: ClientType) -> Result<VideoPlayer> {
|
||||
let q1 = self.clone();
|
||||
let t_context = tokio::spawn(async move { q1.get_context(client_type, false).await });
|
||||
let q2 = self.clone();
|
||||
let t_deobf = tokio::spawn(async move { q2.get_deobf().await });
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "player")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
let (context, deobf) = tokio::join!(t_context, t_deobf);
|
||||
let context = context.unwrap();
|
||||
let deobf = deobf.unwrap()?;
|
||||
|
||||
let player_response = resp.json::<response::Player>().await?;
|
||||
map_player_data(player_response, &deobf)
|
||||
let request_body = if client_type.is_web() {
|
||||
QPlayer {
|
||||
context,
|
||||
playback_context: Some(QPlaybackContext {
|
||||
content_playback_context: QContentPlaybackContext {
|
||||
signature_timestamp: deobf.get_sts(),
|
||||
referer: format!("https://www.youtube.com/watch?v={}", video_id),
|
||||
},
|
||||
}),
|
||||
cpn: None,
|
||||
video_id: video_id.to_owned(),
|
||||
content_check_ok: true,
|
||||
racy_check_ok: true,
|
||||
}
|
||||
} else {
|
||||
QPlayer {
|
||||
context,
|
||||
playback_context: None,
|
||||
cpn: Some(util::generate_content_playback_nonce()),
|
||||
video_id: video_id.to_owned(),
|
||||
content_check_ok: true,
|
||||
racy_check_ok: true,
|
||||
}
|
||||
};
|
||||
|
||||
self.execute_request_deobf::<response::Player, _, _>(
|
||||
client_type,
|
||||
"get_player",
|
||||
Method::POST,
|
||||
"player",
|
||||
video_id,
|
||||
&request_body,
|
||||
Some(&deobf),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
fn build_request_body(
|
||||
client: Arc<dyn YTClient>,
|
||||
deobf: &Deobfuscator,
|
||||
context: ContextYT,
|
||||
video_id: &str,
|
||||
) -> QPlayer {
|
||||
if client.get_type().is_web() {
|
||||
QPlayer {
|
||||
context,
|
||||
playback_context: Some(QPlaybackContext {
|
||||
content_playback_context: QContentPlaybackContext {
|
||||
signature_timestamp: deobf.get_sts(),
|
||||
referer: format!("https://www.youtube.com/watch?v={}", video_id),
|
||||
},
|
||||
}),
|
||||
cpn: None,
|
||||
video_id: video_id.to_owned(),
|
||||
content_check_ok: true,
|
||||
racy_check_ok: true,
|
||||
impl MapResponse<VideoPlayer> for response::Player {
|
||||
fn map_response(
|
||||
self,
|
||||
id: &str,
|
||||
_lang: Language,
|
||||
deobf: Option<&Deobfuscator>,
|
||||
) -> Result<super::MapResult<VideoPlayer>> {
|
||||
let deobf = deobf.unwrap();
|
||||
let mut warnings = vec![];
|
||||
|
||||
// Check playability status
|
||||
match self.playability_status {
|
||||
response::player::PlayabilityStatus::Ok { live_streamability } => {
|
||||
if live_streamability.is_some() {
|
||||
bail!("Active livestreams are not supported")
|
||||
}
|
||||
}
|
||||
response::player::PlayabilityStatus::Unplayable { reason } => {
|
||||
bail!("Video is unplayable. Reason: {}", reason)
|
||||
}
|
||||
response::player::PlayabilityStatus::LoginRequired { reason } => {
|
||||
bail!("Playback requires login. Reason: {}", reason)
|
||||
}
|
||||
response::player::PlayabilityStatus::LiveStreamOffline { reason } => {
|
||||
bail!("Livestream is offline. Reason: {}", reason)
|
||||
}
|
||||
response::player::PlayabilityStatus::Error { reason } => {
|
||||
bail!("Video was deleted. Reason: {}", reason)
|
||||
}
|
||||
};
|
||||
|
||||
let mut streaming_data = some_or_bail!(
|
||||
self.streaming_data,
|
||||
Err(anyhow!("No streaming data was returned"))
|
||||
);
|
||||
let video_details = some_or_bail!(
|
||||
self.video_details,
|
||||
Err(anyhow!("No video details were returned"))
|
||||
);
|
||||
let microformat = self.microformat.map(|m| m.player_microformat_renderer);
|
||||
let (publish_date, category, tags, is_family_safe) =
|
||||
microformat.map_or((None, None, None, None), |m| {
|
||||
(
|
||||
Local
|
||||
.from_local_datetime(&NaiveDateTime::new(
|
||||
m.publish_date,
|
||||
NaiveTime::from_hms(0, 0, 0),
|
||||
))
|
||||
.single(),
|
||||
Some(m.category),
|
||||
m.tags,
|
||||
Some(m.is_family_safe),
|
||||
)
|
||||
});
|
||||
|
||||
if video_details.video_id != id {
|
||||
bail!(
|
||||
"got wrong video id {}, expected {}",
|
||||
video_details.video_id,
|
||||
id
|
||||
);
|
||||
}
|
||||
} else {
|
||||
QPlayer {
|
||||
context,
|
||||
playback_context: None,
|
||||
cpn: Some(util::generate_content_playback_nonce()),
|
||||
video_id: video_id.to_owned(),
|
||||
content_check_ok: true,
|
||||
racy_check_ok: true,
|
||||
|
||||
let video_info = VideoInfo {
|
||||
id: video_details.video_id,
|
||||
title: video_details.title,
|
||||
description: video_details.short_description,
|
||||
length: video_details.length_seconds,
|
||||
thumbnails: video_details.thumbnail.unwrap_or_default().into(),
|
||||
channel: Channel {
|
||||
id: video_details.channel_id,
|
||||
name: video_details.author,
|
||||
},
|
||||
publish_date,
|
||||
view_count: video_details.view_count,
|
||||
keywords: match video_details.keywords {
|
||||
Some(keywords) => keywords,
|
||||
None => tags.unwrap_or_default(),
|
||||
},
|
||||
category,
|
||||
is_live_content: video_details.is_live_content,
|
||||
is_family_safe,
|
||||
};
|
||||
|
||||
let mut formats = streaming_data.formats.c;
|
||||
formats.append(&mut streaming_data.adaptive_formats.c);
|
||||
|
||||
warnings.append(&mut streaming_data.formats.warnings);
|
||||
warnings.append(&mut streaming_data.adaptive_formats.warnings);
|
||||
|
||||
let mut last_nsig: [String; 2] = ["".to_owned(), "".to_owned()];
|
||||
|
||||
let mut video_streams: Vec<VideoStream> = Vec::new();
|
||||
let mut video_only_streams: Vec<VideoStream> = Vec::new();
|
||||
let mut audio_streams: Vec<AudioStream> = Vec::new();
|
||||
|
||||
for f in formats {
|
||||
if f.format_type == player::FormatType::FormatStreamTypeOtf {
|
||||
continue;
|
||||
}
|
||||
|
||||
match (f.is_video(), f.is_audio()) {
|
||||
(true, true) => {
|
||||
let mut map_res = map_video_stream(f, deobf, &mut last_nsig);
|
||||
warnings.append(&mut map_res.warnings);
|
||||
if let Some(c) = map_res.c {
|
||||
video_streams.push(c);
|
||||
};
|
||||
}
|
||||
(true, false) => {
|
||||
let mut map_res = map_video_stream(f, deobf, &mut last_nsig);
|
||||
warnings.append(&mut map_res.warnings);
|
||||
if let Some(c) = map_res.c {
|
||||
video_only_streams.push(c);
|
||||
};
|
||||
}
|
||||
(false, true) => {
|
||||
let mut map_res = map_audio_stream(f, deobf, &mut last_nsig);
|
||||
warnings.append(&mut map_res.warnings);
|
||||
if let Some(c) = map_res.c {
|
||||
audio_streams.push(c);
|
||||
};
|
||||
}
|
||||
(false, false) => warnings.push(format!("invalid stream: itag {}", f.itag)),
|
||||
}
|
||||
}
|
||||
|
||||
video_streams.sort();
|
||||
video_only_streams.sort();
|
||||
audio_streams.sort();
|
||||
|
||||
let mut subtitles = vec![];
|
||||
if let Some(captions) = self.captions {
|
||||
for c in captions.player_captions_tracklist_renderer.caption_tracks {
|
||||
let lang_auto = c.name.strip_suffix(" (auto-generated)");
|
||||
|
||||
subtitles.push(Subtitle {
|
||||
url: c.base_url,
|
||||
lang: c.language_code,
|
||||
lang_name: lang_auto.unwrap_or(&c.name).to_owned(),
|
||||
auto_generated: lang_auto.is_some(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ok(MapResult {
|
||||
c: VideoPlayer {
|
||||
info: video_info,
|
||||
video_streams,
|
||||
video_only_streams,
|
||||
audio_streams,
|
||||
subtitles,
|
||||
expires_in_seconds: streaming_data.expires_in_seconds,
|
||||
},
|
||||
warnings,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -136,7 +297,7 @@ fn deobf_nsig(
|
|||
let nsig: String;
|
||||
match url_params.get("n") {
|
||||
Some(n) => {
|
||||
nsig = if n.to_owned() == last_nsig[0] {
|
||||
nsig = if n == &last_nsig[0] {
|
||||
last_nsig[1].to_owned()
|
||||
} else {
|
||||
let nsig = deobf.deobfuscate_nsig(n)?;
|
||||
|
|
@ -157,108 +318,192 @@ fn map_url(
|
|||
signature_cipher: &Option<String>,
|
||||
deobf: &Deobfuscator,
|
||||
last_nsig: &mut [String; 2],
|
||||
) -> Option<(String, bool)> {
|
||||
) -> MapResult<Option<(String, bool)>> {
|
||||
let (url_base, mut url_params) = match url {
|
||||
Some(url) => ok_or_bail!(util::url_to_params(url), None),
|
||||
Some(url) => ok_or_bail!(
|
||||
util::url_to_params(url),
|
||||
MapResult {
|
||||
c: None,
|
||||
warnings: vec![format!("Could not parse url `{}`", url)]
|
||||
}
|
||||
),
|
||||
None => match signature_cipher {
|
||||
Some(signature_cipher) => match cipher_to_url_params(signature_cipher, deobf) {
|
||||
Ok(res) => res,
|
||||
Err(e) => {
|
||||
error!("Could not deobfuscate signatureCipher: {}", e);
|
||||
return None;
|
||||
return MapResult {
|
||||
c: None,
|
||||
warnings: vec![format!(
|
||||
"Could not deobfuscate signatureCipher `{}`: {}",
|
||||
signature_cipher, e
|
||||
)],
|
||||
};
|
||||
}
|
||||
},
|
||||
None => return None,
|
||||
None => {
|
||||
return MapResult {
|
||||
c: None,
|
||||
warnings: vec!["stream contained neither url nor cipher".to_owned()],
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let mut warnings = vec![];
|
||||
let mut throttled = false;
|
||||
deobf_nsig(&mut url_params, deobf, last_nsig).unwrap_or_else(|e| {
|
||||
warn!("Could not deobfuscate nsig: {}", e);
|
||||
warnings.push(format!(
|
||||
"Could not deobfuscate nsig (params: {:?}): {}",
|
||||
url_params, e
|
||||
));
|
||||
throttled = true;
|
||||
});
|
||||
|
||||
Some((
|
||||
ok_or_bail!(
|
||||
Url::parse_with_params(url_base.as_str(), url_params.iter()),
|
||||
None
|
||||
)
|
||||
.to_string(),
|
||||
throttled,
|
||||
))
|
||||
MapResult {
|
||||
c: Some((
|
||||
ok_or_bail!(
|
||||
Url::parse_with_params(url_base.as_str(), url_params.iter()),
|
||||
MapResult {
|
||||
c: None,
|
||||
warnings: vec![format!(
|
||||
"url could not be joined. url: `{}` params: {:?}",
|
||||
url_base, url_params
|
||||
)],
|
||||
}
|
||||
)
|
||||
.to_string(),
|
||||
throttled,
|
||||
)),
|
||||
warnings,
|
||||
}
|
||||
}
|
||||
|
||||
fn map_video_stream(
|
||||
f: &player::Format,
|
||||
f: player::Format,
|
||||
deobf: &Deobfuscator,
|
||||
last_nsig: &mut [String; 2],
|
||||
) -> Option<VideoStream> {
|
||||
let (mtype, codecs) = some_or_bail!(parse_mime(&f.mime_type), None);
|
||||
let (url, throttled) =
|
||||
some_or_bail!(map_url(&f.url, &f.signature_cipher, deobf, last_nsig), None);
|
||||
) -> MapResult<Option<VideoStream>> {
|
||||
let (mtype, codecs) = some_or_bail!(
|
||||
parse_mime(&f.mime_type),
|
||||
MapResult {
|
||||
c: None,
|
||||
warnings: vec![format!(
|
||||
"Invalid mime type `{}` in video format {:?}",
|
||||
&f.mime_type, &f
|
||||
)]
|
||||
}
|
||||
);
|
||||
let map_res = map_url(&f.url, &f.signature_cipher, deobf, last_nsig);
|
||||
|
||||
Some(VideoStream {
|
||||
url,
|
||||
itag: f.itag,
|
||||
bitrate: f.bitrate,
|
||||
average_bitrate: f.average_bitrate,
|
||||
size: f.content_length,
|
||||
index_range: f.index_range.clone(),
|
||||
init_range: f.init_range.clone(),
|
||||
width: some_or_bail!(f.width, None),
|
||||
height: some_or_bail!(f.height, None),
|
||||
fps: some_or_bail!(f.fps, None),
|
||||
quality: some_or_bail!(f.quality_label.clone(), None),
|
||||
hdr: f.color_info.clone().unwrap_or_default().primaries
|
||||
== player::Primaries::ColorPrimariesBt2020,
|
||||
mime: f.mime_type.to_owned(),
|
||||
format: some_or_bail!(get_video_format(mtype), None),
|
||||
codec: get_video_codec(codecs),
|
||||
throttled,
|
||||
})
|
||||
match map_res.c {
|
||||
Some((url, throttled)) => MapResult {
|
||||
c: Some(VideoStream {
|
||||
url,
|
||||
itag: f.itag,
|
||||
bitrate: f.bitrate,
|
||||
average_bitrate: f.average_bitrate.unwrap_or(f.bitrate),
|
||||
size: f.content_length,
|
||||
index_range: f.index_range,
|
||||
init_range: f.init_range,
|
||||
// Note that the format has already been verified using
|
||||
// is_video(), so these unwraps are safe
|
||||
width: f.width.unwrap(),
|
||||
height: f.height.unwrap(),
|
||||
fps: f.fps.unwrap(),
|
||||
quality: f.quality_label.unwrap(),
|
||||
hdr: f.color_info.unwrap_or_default().primaries
|
||||
== player::Primaries::ColorPrimariesBt2020,
|
||||
mime: f.mime_type.to_owned(),
|
||||
format: some_or_bail!(
|
||||
get_video_format(mtype),
|
||||
MapResult {
|
||||
c: None,
|
||||
warnings: vec![format!("invalid video format. itag: {}", f.itag)]
|
||||
}
|
||||
),
|
||||
codec: get_video_codec(codecs),
|
||||
throttled,
|
||||
}),
|
||||
warnings: map_res.warnings,
|
||||
},
|
||||
None => MapResult {
|
||||
c: None,
|
||||
warnings: map_res.warnings,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn map_audio_stream(
|
||||
f: &player::Format,
|
||||
f: player::Format,
|
||||
deobf: &Deobfuscator,
|
||||
last_nsig: &mut [String; 2],
|
||||
) -> Option<AudioStream> {
|
||||
) -> MapResult<Option<AudioStream>> {
|
||||
static LANG_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r#"^([a-z]{2})\."#).unwrap());
|
||||
|
||||
let (mtype, codecs) = some_or_bail!(parse_mime(&f.mime_type), None);
|
||||
let (url, throttled) =
|
||||
some_or_bail!(map_url(&f.url, &f.signature_cipher, deobf, last_nsig), None);
|
||||
let (mtype, codecs) = some_or_bail!(
|
||||
parse_mime(&f.mime_type),
|
||||
MapResult {
|
||||
c: None,
|
||||
warnings: vec![format!(
|
||||
"Invalid mime type `{}` in video format {:?}",
|
||||
&f.mime_type, &f
|
||||
)]
|
||||
}
|
||||
);
|
||||
let map_res = map_url(&f.url, &f.signature_cipher, deobf, last_nsig);
|
||||
|
||||
Some(AudioStream {
|
||||
url,
|
||||
itag: f.itag,
|
||||
bitrate: f.bitrate,
|
||||
average_bitrate: f.average_bitrate,
|
||||
size: f.content_length,
|
||||
index_range: f.index_range.to_owned(),
|
||||
init_range: f.init_range.to_owned(),
|
||||
mime: f.mime_type.to_owned(),
|
||||
format: some_or_bail!(get_audio_format(mtype), None),
|
||||
codec: get_audio_codec(codecs),
|
||||
throttled,
|
||||
track: f.audio_track.as_ref().map(|t| AudioTrack {
|
||||
id: t.id.to_owned(),
|
||||
lang: LANG_PATTERN
|
||||
.captures(&t.id)
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|m| m.get(1).unwrap().as_str().to_owned()),
|
||||
lang_name: t.display_name.to_owned(),
|
||||
is_default: t.audio_is_default,
|
||||
}),
|
||||
})
|
||||
match map_res.c {
|
||||
Some((url, throttled)) => MapResult {
|
||||
c: Some(AudioStream {
|
||||
url,
|
||||
itag: f.itag,
|
||||
bitrate: f.bitrate,
|
||||
average_bitrate: f.average_bitrate.unwrap_or(f.bitrate),
|
||||
size: f.content_length.unwrap(),
|
||||
index_range: f.index_range,
|
||||
init_range: f.init_range,
|
||||
mime: f.mime_type.to_owned(),
|
||||
format: some_or_bail!(
|
||||
get_audio_format(mtype),
|
||||
MapResult {
|
||||
c: None,
|
||||
warnings: vec![format!("invalid audio format. itag: {}", f.itag)]
|
||||
}
|
||||
),
|
||||
codec: get_audio_codec(codecs),
|
||||
throttled,
|
||||
track: match f.audio_track {
|
||||
Some(t) => {
|
||||
let lang = LANG_PATTERN
|
||||
.captures(&t.id)
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|m| m.get(1).unwrap().as_str().to_owned());
|
||||
|
||||
Some(AudioTrack {
|
||||
id: t.id,
|
||||
lang,
|
||||
lang_name: t.display_name,
|
||||
is_default: t.audio_is_default,
|
||||
})
|
||||
}
|
||||
None => None,
|
||||
},
|
||||
}),
|
||||
warnings: map_res.warnings,
|
||||
},
|
||||
None => MapResult {
|
||||
c: None,
|
||||
warnings: map_res.warnings,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_mime(mime: &str) -> Option<(&str, Vec<&str>)> {
|
||||
static PATTERN: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r#"(\w+/\w+);\scodecs="([a-zA-Z-0-9.,\s]*)""#).unwrap());
|
||||
|
||||
let captures = some_or_bail!(PATTERN.captures(&mime).ok().flatten(), None);
|
||||
let captures = some_or_bail!(PATTERN.captures(mime).ok().flatten(), None);
|
||||
Some((
|
||||
captures.get(1).unwrap().as_str(),
|
||||
captures
|
||||
|
|
@ -313,140 +558,16 @@ fn get_audio_codec(codecs: Vec<&str>) -> AudioCodec {
|
|||
AudioCodec::Unknown
|
||||
}
|
||||
|
||||
fn map_player_data(response: response::Player, deobf: &Deobfuscator) -> Result<VideoPlayer> {
|
||||
// Check playability status
|
||||
match response.playability_status {
|
||||
response::player::PlayabilityStatus::Ok { live_streamability } => {
|
||||
if live_streamability.is_some() {
|
||||
bail!("Active livestreams are not supported")
|
||||
}
|
||||
}
|
||||
response::player::PlayabilityStatus::Unplayable { reason } => {
|
||||
bail!("Video is unplayable. Reason: {}", reason)
|
||||
}
|
||||
response::player::PlayabilityStatus::LoginRequired { reason } => {
|
||||
bail!("Playback requires login. Reason: {}", reason)
|
||||
}
|
||||
response::player::PlayabilityStatus::LiveStreamOffline { reason } => {
|
||||
bail!("Livestream is offline. Reason: {}", reason)
|
||||
}
|
||||
response::player::PlayabilityStatus::Error { reason } => {
|
||||
bail!("Video was deleted. Reason: {}", reason)
|
||||
}
|
||||
};
|
||||
|
||||
let streaming_data = some_or_bail!(
|
||||
response.streaming_data,
|
||||
Err(anyhow!("No streaming data was returned"))
|
||||
);
|
||||
let video_details = some_or_bail!(
|
||||
response.video_details,
|
||||
Err(anyhow!("No video details were returned"))
|
||||
);
|
||||
let microformat = response.microformat.map(|m| m.player_microformat_renderer);
|
||||
|
||||
let video_info = VideoInfo {
|
||||
id: video_details.video_id,
|
||||
title: video_details.title,
|
||||
description: video_details.short_description,
|
||||
length: video_details.length_seconds,
|
||||
thumbnails: video_details
|
||||
.thumbnail
|
||||
.unwrap_or_default()
|
||||
.thumbnails
|
||||
.iter()
|
||||
.map(|t| Thumbnail {
|
||||
url: t.url.to_owned(),
|
||||
height: t.height,
|
||||
width: t.width,
|
||||
})
|
||||
.collect(),
|
||||
channel: Channel {
|
||||
id: video_details.channel_id,
|
||||
name: video_details.author,
|
||||
},
|
||||
publish_date: microformat.as_ref().map(|m| {
|
||||
let ndt = NaiveDateTime::new(m.publish_date, NaiveTime::from_hms(0, 0, 0));
|
||||
Local.from_local_datetime(&ndt).unwrap()
|
||||
}),
|
||||
view_count: video_details.view_count,
|
||||
keywords: video_details
|
||||
.keywords
|
||||
.or_else(|| microformat.as_ref().map_or(None, |mf| mf.tags.clone()))
|
||||
.unwrap_or_default(),
|
||||
category: microformat.as_ref().map(|m| m.category.to_owned()),
|
||||
is_live_content: video_details.is_live_content,
|
||||
is_family_safe: microformat.as_ref().map(|m| m.is_family_safe),
|
||||
};
|
||||
|
||||
let mut formats = streaming_data.formats.clone();
|
||||
formats.append(&mut streaming_data.adaptive_formats.clone());
|
||||
|
||||
let mut last_nsig: [String; 2] = ["".to_owned(), "".to_owned()];
|
||||
|
||||
let mut video_streams: Vec<VideoStream> = Vec::new();
|
||||
let mut video_only_streams: Vec<VideoStream> = Vec::new();
|
||||
let mut audio_streams: Vec<AudioStream> = Vec::new();
|
||||
|
||||
for f in formats {
|
||||
if f.format_type == player::FormatType::FormatStreamTypeOtf {
|
||||
continue;
|
||||
}
|
||||
|
||||
match (f.is_video(), f.is_audio()) {
|
||||
(true, true) => match map_video_stream(&f, deobf, &mut last_nsig) {
|
||||
Some(stream) => video_streams.push(stream),
|
||||
None => {}
|
||||
},
|
||||
(true, false) => match map_video_stream(&f, deobf, &mut last_nsig) {
|
||||
Some(stream) => video_only_streams.push(stream),
|
||||
None => {}
|
||||
},
|
||||
(false, true) => match map_audio_stream(&f, deobf, &mut last_nsig) {
|
||||
Some(stream) => audio_streams.push(stream),
|
||||
None => {}
|
||||
},
|
||||
(false, false) => {}
|
||||
}
|
||||
}
|
||||
|
||||
video_streams.sort();
|
||||
video_only_streams.sort();
|
||||
audio_streams.sort();
|
||||
|
||||
let subtitles = response.captions.map_or(vec![], |captions| {
|
||||
captions
|
||||
.player_captions_tracklist_renderer
|
||||
.caption_tracks
|
||||
.iter()
|
||||
.map(|caption| {
|
||||
let lang_auto = caption.name.strip_suffix(" (auto-generated)");
|
||||
|
||||
Subtitle {
|
||||
url: caption.base_url.to_owned(),
|
||||
lang: caption.language_code.to_owned(),
|
||||
lang_name: lang_auto.unwrap_or(&caption.name).to_owned(),
|
||||
auto_generated: lang_auto.is_some(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
|
||||
Ok(VideoPlayer {
|
||||
info: video_info,
|
||||
video_streams,
|
||||
video_only_streams,
|
||||
audio_streams,
|
||||
subtitles,
|
||||
expires_in_seconds: streaming_data.expires_in_seconds,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "yaml")]
|
||||
mod tests {
|
||||
use std::{fs::File, io::BufReader, path::Path};
|
||||
|
||||
use crate::{cache::DeobfData, client::CLIENT_TYPES};
|
||||
use crate::{
|
||||
client::{RustyPipe, CLIENT_TYPES},
|
||||
deobfuscate::DeobfData,
|
||||
report::TestFileReporter,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
use rstest::rstest;
|
||||
|
|
@ -465,8 +586,6 @@ mod tests {
|
|||
let tf_dir = Path::new("testfiles/player");
|
||||
let video_id = "pPvd8UxmSbQ";
|
||||
|
||||
let rt = RustyTube::new();
|
||||
|
||||
for client_type in CLIENT_TYPES {
|
||||
let mut json_path = tf_dir.to_path_buf();
|
||||
json_path.push(format!("{:?}_video.json", client_type).to_lowercase());
|
||||
|
|
@ -474,31 +593,20 @@ mod tests {
|
|||
continue;
|
||||
}
|
||||
|
||||
let client = rt.get_ytclient(client_type);
|
||||
let context = client.get_context(false).await;
|
||||
|
||||
let request_body = build_request_body(client.clone(), &DEOBFUSCATOR, context, video_id);
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "player")
|
||||
let reporter = TestFileReporter::new(json_path);
|
||||
let rp = RustyPipe::new(None, Some(Box::new(reporter)), None);
|
||||
rp.test_query()
|
||||
.report(true)
|
||||
.get_player(video_id, client_type)
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.error_for_status()
|
||||
.unwrap();
|
||||
|
||||
let mut file = File::create(json_path).unwrap();
|
||||
let mut content = std::io::Cursor::new(resp.bytes().await.unwrap());
|
||||
std::io::copy(&mut content, &mut file).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test_log::test(tokio::test)]
|
||||
async fn download_model_testfiles() {
|
||||
let tf_dir = Path::new("testfiles/player_model");
|
||||
let rt = RustyTube::new();
|
||||
let rp = RustyPipe::new_test();
|
||||
|
||||
for (name, id) in [("multilanguage", "tVWWp1PqDus"), ("hdr", "LXb3EKWsInQ")] {
|
||||
let mut json_path = tf_dir.to_path_buf();
|
||||
|
|
@ -507,7 +615,11 @@ mod tests {
|
|||
continue;
|
||||
}
|
||||
|
||||
let player_data = rt.get_player(id, ClientType::Desktop).await.unwrap();
|
||||
let player_data = rp
|
||||
.test_query()
|
||||
.get_player(id, ClientType::Desktop)
|
||||
.await
|
||||
.unwrap();
|
||||
let file = File::create(json_path).unwrap();
|
||||
serde_json::to_writer_pretty(file, &player_data).unwrap();
|
||||
}
|
||||
|
|
@ -525,10 +637,17 @@ mod tests {
|
|||
let json_file = File::open(json_path).unwrap();
|
||||
|
||||
let resp: response::Player = serde_json::from_reader(BufReader::new(json_file)).unwrap();
|
||||
let player_data = map_player_data(resp, &DEOBFUSCATOR).unwrap();
|
||||
let map_res = resp
|
||||
.map_response("pPvd8UxmSbQ", Language::En, Some(&DEOBFUSCATOR))
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
map_res.warnings.is_empty(),
|
||||
"deserialization/mapping warnings: {:?}",
|
||||
map_res.warnings
|
||||
);
|
||||
let is_desktop = name == "desktop" || name == "desktopmusic";
|
||||
insta::assert_yaml_snapshot!(format!("map_player_data_{}", name), player_data, {
|
||||
insta::assert_yaml_snapshot!(format!("map_player_data_{}", name), map_res.c, {
|
||||
".info.publish_date" => insta::dynamic_redaction(move |value, _path| {
|
||||
if is_desktop {
|
||||
assert!(value.as_str().unwrap().starts_with("2019-05-30T00:00:00"));
|
||||
|
|
@ -561,8 +680,12 @@ mod tests {
|
|||
#[case::ios(ClientType::Ios)]
|
||||
#[test_log::test(tokio::test)]
|
||||
async fn t_get_player(#[case] client_type: ClientType) {
|
||||
let rt = RustyTube::new();
|
||||
let player_data = rt.get_player("n4tK7LYFxI0", client_type).await.unwrap();
|
||||
let rp = RustyPipe::new_test();
|
||||
let player_data = rp
|
||||
.test_query()
|
||||
.get_player("n4tK7LYFxI0", client_type)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// dbg!(&player_data);
|
||||
|
||||
|
|
@ -584,7 +707,12 @@ mod tests {
|
|||
assert_eq!(player_data.info.is_live_content, false);
|
||||
|
||||
if client_type == ClientType::Desktop || client_type == ClientType::DesktopMusic {
|
||||
assert!(player_data.info.publish_date.unwrap().to_string().starts_with("2013-05-05 00:00:00"));
|
||||
assert!(player_data
|
||||
.info
|
||||
.publish_date
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.starts_with("2013-05-05 00:00:00"));
|
||||
assert_eq!(player_data.info.category.unwrap(), "Music");
|
||||
assert_eq!(player_data.info.is_family_safe.unwrap(), true);
|
||||
}
|
||||
|
|
@ -604,7 +732,7 @@ mod tests {
|
|||
// Bitrates may change between requests
|
||||
assert_approx(video.bitrate as f64, 1507068.0);
|
||||
assert_eq!(video.average_bitrate, 1345149);
|
||||
assert_eq!(video.size, 43553412);
|
||||
assert_eq!(video.size.unwrap(), 43553412);
|
||||
assert_eq!(video.width, 1280);
|
||||
assert_eq!(video.height, 720);
|
||||
assert_eq!(video.fps, 30);
|
||||
|
|
@ -634,7 +762,7 @@ mod tests {
|
|||
|
||||
assert_approx(video.bitrate as f64, 1340829.0);
|
||||
assert_approx(video.average_bitrate as f64, 1233444.0);
|
||||
assert_approx(video.size as f64, 39936630.0);
|
||||
assert_approx(video.size.unwrap() as f64, 39936630.0);
|
||||
assert_eq!(video.width, 1280);
|
||||
assert_eq!(video.height, 720);
|
||||
assert_eq!(video.fps, 30);
|
||||
|
|
@ -661,15 +789,20 @@ mod tests {
|
|||
fn t_cipher_to_url() {
|
||||
let signature_cipher = "s=w%3DAe%3DA6aDNQLkViKS7LOm9QtxZJHKwb53riq9qEFw-ecBWJCAiA%3DcEg0tn3dty9jEHszfzh4Ud__bg9CEHVx4ix-7dKsIPAhIQRw8JQ0qOA&sp=sig&url=https://rr5---sn-h0jelnez.googlevideo.com/videoplayback%3Fexpire%3D1659376413%26ei%3Dvb7nYvH5BMK8gAfBj7ToBQ%26ip%3D2003%253Ade%253Aaf06%253A6300%253Ac750%253A1b77%253Ac74a%253A80e3%26id%3Do-AB_BABwrXZJN428ZwDxq5ScPn2AbcGODnRlTVhCQ3mj2%26itag%3D251%26source%3Dyoutube%26requiressl%3Dyes%26mh%3DhH%26mm%3D31%252C26%26mn%3Dsn-h0jelnez%252Csn-4g5ednsl%26ms%3Dau%252Conr%26mv%3Dm%26mvi%3D5%26pl%3D37%26initcwndbps%3D1588750%26spc%3DlT-Khi831z8dTejFIRCvCEwx_6romtM%26vprv%3D1%26mime%3Daudio%252Fwebm%26ns%3Db_Mq_qlTFcSGlG9RpwpM9xQH%26gir%3Dyes%26clen%3D3781277%26dur%3D229.301%26lmt%3D1655510291473933%26mt%3D1659354538%26fvip%3D5%26keepalive%3Dyes%26fexp%3D24001373%252C24007246%26c%3DWEB%26rbqsm%3Dfr%26txp%3D4532434%26n%3Dd2g6G2hVqWIXxedQ%26sparams%3Dexpire%252Cei%252Cip%252Cid%252Citag%252Csource%252Crequiressl%252Cspc%252Cvprv%252Cmime%252Cns%252Cgir%252Cclen%252Cdur%252Clmt%26lsparams%3Dmh%252Cmm%252Cmn%252Cms%252Cmv%252Cmvi%252Cpl%252Cinitcwndbps%26lsig%3DAG3C_xAwRQIgCKCGJ1iu4wlaGXy3jcJyU3inh9dr1FIfqYOZEG_MdmACIQCbungkQYFk7EhD6K2YvLaHFMjKOFWjw001_tLb0lPDtg%253D%253D";
|
||||
let mut last_nsig: [String; 2] = ["".to_owned(), "".to_owned()];
|
||||
let (url, throttled) = map_url(
|
||||
let map_res = map_url(
|
||||
&None,
|
||||
&Some(signature_cipher.to_owned()),
|
||||
&DEOBFUSCATOR,
|
||||
&mut last_nsig,
|
||||
)
|
||||
.unwrap();
|
||||
);
|
||||
let (url, throttled) = map_res.c.unwrap();
|
||||
|
||||
assert_eq!(url, "https://rr5---sn-h0jelnez.googlevideo.com/videoplayback?c=WEB&clen=3781277&dur=229.301&ei=vb7nYvH5BMK8gAfBj7ToBQ&expire=1659376413&fexp=24001373%2C24007246&fvip=5&gir=yes&id=o-AB_BABwrXZJN428ZwDxq5ScPn2AbcGODnRlTVhCQ3mj2&initcwndbps=1588750&ip=2003%3Ade%3Aaf06%3A6300%3Ac750%3A1b77%3Ac74a%3A80e3&itag=251&keepalive=yes&lmt=1655510291473933&lsig=AG3C_xAwRQIgCKCGJ1iu4wlaGXy3jcJyU3inh9dr1FIfqYOZEG_MdmACIQCbungkQYFk7EhD6K2YvLaHFMjKOFWjw001_tLb0lPDtg%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&mh=hH&mime=audio%2Fwebm&mm=31%2C26&mn=sn-h0jelnez%2Csn-4g5ednsl&ms=au%2Conr&mt=1659354538&mv=m&mvi=5&n=XzXGSfGusw6OCQ&ns=b_Mq_qlTFcSGlG9RpwpM9xQH&pl=37&rbqsm=fr&requiressl=yes&sig=AOq0QJ8wRQIhAPIsKd7-xi4xVHEC9gb__dU4hzfzsHEj9ytd3nt0gEceAiACJWBcw-wFEq9qir35bwKHJZxtQ9mOL7SKiVkLQNDa6A%3D%3D&source=youtube&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&spc=lT-Khi831z8dTejFIRCvCEwx_6romtM&txp=4532434&vprv=1");
|
||||
assert_eq!(throttled, false);
|
||||
assert!(
|
||||
map_res.warnings.is_empty(),
|
||||
"deserialization/mapping warnings: {:?}",
|
||||
map_res.warnings
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
use anyhow::{anyhow, Context, Result};
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use reqwest::Method;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
deobfuscate::Deobfuscator,
|
||||
model::{Channel, Language, Playlist, Thumbnail, Video},
|
||||
serializer::text::{PageType, TextLink},
|
||||
timeago, util,
|
||||
};
|
||||
|
||||
use super::{response, ClientType, ContextYT, RustyTube};
|
||||
use super::{response, ClientType, ContextYT, MapResponse, MapResult, RustyPipeQuery};
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
|
@ -24,62 +25,44 @@ struct QPlaylistCont {
|
|||
continuation: String,
|
||||
}
|
||||
|
||||
impl RustyTube {
|
||||
pub async fn get_playlist(&self, playlist_id: &str) -> Result<Playlist> {
|
||||
let client = self.get_ytclient(ClientType::Desktop);
|
||||
let context = client.get_context(true).await;
|
||||
|
||||
impl RustyPipeQuery {
|
||||
pub async fn get_playlist(self, playlist_id: &str) -> Result<Playlist> {
|
||||
let context = self.get_context(ClientType::Desktop, true).await;
|
||||
let request_body = QPlaylist {
|
||||
context,
|
||||
browse_id: "VL".to_owned() + playlist_id,
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "browse")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
let resp_body = resp.text().await?;
|
||||
let playlist_response =
|
||||
serde_json::from_str::<response::Playlist>(&resp_body).context(resp_body)?;
|
||||
|
||||
map_playlist(&playlist_response, self.localization.language)
|
||||
self.execute_request::<response::Playlist, _, _>(
|
||||
ClientType::Desktop,
|
||||
"get_playlist",
|
||||
Method::POST,
|
||||
"browse",
|
||||
playlist_id,
|
||||
&request_body,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_playlist_cont(&self, playlist: &mut Playlist) -> Result<()> {
|
||||
pub async fn get_playlist_cont(self, playlist: &mut Playlist) -> Result<()> {
|
||||
match &playlist.ctoken {
|
||||
Some(ctoken) => {
|
||||
let client = self.get_ytclient(ClientType::Desktop);
|
||||
let context = client.get_context(true).await;
|
||||
|
||||
let context = self.get_context(ClientType::Desktop, true).await;
|
||||
let request_body = QPlaylistCont {
|
||||
context,
|
||||
continuation: ctoken.to_owned(),
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "browse")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
let cont_response = resp.json::<response::playlist::PlaylistCont>().await?;
|
||||
|
||||
let action = some_or_bail!(
|
||||
cont_response
|
||||
.on_response_received_actions
|
||||
.iter()
|
||||
.find(|a| a.append_continuation_items_action.target_id == playlist.id),
|
||||
Err(anyhow!("no continuation action"))
|
||||
);
|
||||
|
||||
let (mut videos, ctoken) =
|
||||
map_playlist_items(&action.append_continuation_items_action.continuation_items);
|
||||
let (mut videos, ctoken) = self
|
||||
.execute_request::<response::PlaylistCont, _, _>(
|
||||
ClientType::Desktop,
|
||||
"get_playlist_cont",
|
||||
Method::POST,
|
||||
"browse",
|
||||
&playlist.id,
|
||||
&request_body,
|
||||
)
|
||||
.await?;
|
||||
|
||||
playlist.videos.append(&mut videos);
|
||||
playlist.ctoken = ctoken;
|
||||
|
|
@ -95,149 +78,179 @@ impl RustyTube {
|
|||
}
|
||||
}
|
||||
|
||||
fn map_playlist(response: &response::Playlist, lang: Language) -> Result<Playlist> {
|
||||
let video_items = &some_or_bail!(
|
||||
some_or_bail!(
|
||||
impl MapResponse<Playlist> for response::Playlist {
|
||||
fn map_response(
|
||||
self,
|
||||
id: &str,
|
||||
lang: Language,
|
||||
_deobf: Option<&Deobfuscator>,
|
||||
) -> Result<MapResult<Playlist>> {
|
||||
let video_items = &some_or_bail!(
|
||||
some_or_bail!(
|
||||
response
|
||||
.contents
|
||||
.two_column_browse_results_renderer
|
||||
.contents
|
||||
.get(0),
|
||||
Err(anyhow!("twoColumnBrowseResultsRenderer empty"))
|
||||
some_or_bail!(
|
||||
self.contents
|
||||
.two_column_browse_results_renderer
|
||||
.contents
|
||||
.get(0),
|
||||
Err(anyhow!("twoColumnBrowseResultsRenderer empty"))
|
||||
)
|
||||
.tab_renderer
|
||||
.content
|
||||
.section_list_renderer
|
||||
.contents
|
||||
.get(0),
|
||||
Err(anyhow!("sectionListRenderer empty"))
|
||||
)
|
||||
.tab_renderer
|
||||
.content
|
||||
.section_list_renderer
|
||||
.item_section_renderer
|
||||
.contents
|
||||
.get(0),
|
||||
Err(anyhow!("sectionListRenderer empty"))
|
||||
Err(anyhow!("itemSectionRenderer empty"))
|
||||
)
|
||||
.item_section_renderer
|
||||
.contents
|
||||
.get(0),
|
||||
Err(anyhow!("itemSectionRenderer empty"))
|
||||
)
|
||||
.playlist_video_list_renderer
|
||||
.contents;
|
||||
.playlist_video_list_renderer
|
||||
.contents;
|
||||
|
||||
let (videos, ctoken) = map_playlist_items(video_items);
|
||||
let (videos, ctoken) = map_playlist_items(&video_items.c);
|
||||
|
||||
let (thumbnails, last_update_txt) = match &response.sidebar {
|
||||
Some(sidebar) => {
|
||||
let primary = some_or_bail!(
|
||||
sidebar.playlist_sidebar_renderer.items.get(0),
|
||||
Err(anyhow!("no primary sidebar"))
|
||||
);
|
||||
let (thumbnails, last_update_txt) = match &self.sidebar {
|
||||
Some(sidebar) => {
|
||||
let primary = some_or_bail!(
|
||||
sidebar.playlist_sidebar_renderer.items.get(0),
|
||||
Err(anyhow!("no primary sidebar"))
|
||||
);
|
||||
|
||||
(
|
||||
&primary
|
||||
.playlist_sidebar_primary_info_renderer
|
||||
.thumbnail_renderer
|
||||
.playlist_video_thumbnail_renderer
|
||||
.thumbnail
|
||||
.thumbnails,
|
||||
primary
|
||||
.playlist_sidebar_primary_info_renderer
|
||||
.stats
|
||||
.get(2)
|
||||
.map(|t| t.to_owned()),
|
||||
)
|
||||
}
|
||||
None => {
|
||||
let header_banner = some_or_bail!(
|
||||
&response
|
||||
(
|
||||
&primary
|
||||
.playlist_sidebar_primary_info_renderer
|
||||
.thumbnail_renderer
|
||||
.playlist_video_thumbnail_renderer
|
||||
.thumbnail
|
||||
.thumbnails,
|
||||
primary
|
||||
.playlist_sidebar_primary_info_renderer
|
||||
.stats
|
||||
.get(2)
|
||||
.map(|t| t.to_owned()),
|
||||
)
|
||||
}
|
||||
None => {
|
||||
let header_banner = some_or_bail!(
|
||||
&self.header.playlist_header_renderer.playlist_header_banner,
|
||||
Err(anyhow!("no thumbnail found"))
|
||||
);
|
||||
|
||||
let last_update_txt = self
|
||||
.header
|
||||
.playlist_header_renderer
|
||||
.playlist_header_banner,
|
||||
Err(anyhow!("no thumbnail found"))
|
||||
);
|
||||
.byline
|
||||
.get(1)
|
||||
.map(|b| b.playlist_byline_renderer.text.to_owned());
|
||||
|
||||
let last_update_txt = response
|
||||
.header
|
||||
.playlist_header_renderer
|
||||
.byline
|
||||
.get(1)
|
||||
.map(|b| b.playlist_byline_renderer.text.to_owned());
|
||||
(
|
||||
&header_banner
|
||||
.hero_playlist_thumbnail_renderer
|
||||
.thumbnail
|
||||
.thumbnails,
|
||||
last_update_txt,
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
&header_banner
|
||||
.hero_playlist_thumbnail_renderer
|
||||
.thumbnail
|
||||
.thumbnails,
|
||||
last_update_txt,
|
||||
)
|
||||
let thumbnails = thumbnails
|
||||
.iter()
|
||||
.map(|t| Thumbnail {
|
||||
url: t.url.to_owned(),
|
||||
width: t.width,
|
||||
height: t.height,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let n_videos = match ctoken {
|
||||
Some(_) => {
|
||||
ok_or_bail!(
|
||||
util::parse_numeric(&self.header.playlist_header_renderer.num_videos_text),
|
||||
Err(anyhow!("no video count"))
|
||||
)
|
||||
}
|
||||
None => videos.len() as u32,
|
||||
};
|
||||
|
||||
let playlist_id = self.header.playlist_header_renderer.playlist_id;
|
||||
if playlist_id != id {
|
||||
bail!("got wrong playlist id {}, expected {}", playlist_id, id);
|
||||
}
|
||||
};
|
||||
|
||||
let thumbnails = thumbnails
|
||||
.iter()
|
||||
.map(|t| Thumbnail {
|
||||
url: t.url.to_owned(),
|
||||
width: t.width,
|
||||
height: t.height,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let name = self.header.playlist_header_renderer.title;
|
||||
let description = self.header.playlist_header_renderer.description_text;
|
||||
|
||||
let n_videos = match ctoken {
|
||||
Some(_) => {
|
||||
ok_or_bail!(
|
||||
util::parse_numeric(&response.header.playlist_header_renderer.num_videos_text),
|
||||
Err(anyhow!("no video count"))
|
||||
)
|
||||
}
|
||||
None => videos.len() as u32,
|
||||
};
|
||||
|
||||
let id = response
|
||||
.header
|
||||
.playlist_header_renderer
|
||||
.playlist_id
|
||||
.to_owned();
|
||||
let name = response.header.playlist_header_renderer.title.to_owned();
|
||||
let description = response
|
||||
.header
|
||||
.playlist_header_renderer
|
||||
.description_text
|
||||
.to_owned();
|
||||
|
||||
let channel = match &response.header.playlist_header_renderer.owner_text {
|
||||
Some(owner_text) => match owner_text {
|
||||
TextLink::Browse {
|
||||
let channel = match self.header.playlist_header_renderer.owner_text {
|
||||
Some(TextLink::Browse {
|
||||
text,
|
||||
page_type,
|
||||
page_type: PageType::Channel,
|
||||
browse_id,
|
||||
} => match page_type {
|
||||
PageType::Channel => Some(Channel {
|
||||
id: browse_id.to_owned(),
|
||||
name: text.to_owned(),
|
||||
}),
|
||||
_ => None,
|
||||
},
|
||||
}) => Some(Channel {
|
||||
id: browse_id,
|
||||
name: text,
|
||||
}),
|
||||
_ => None,
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
};
|
||||
|
||||
Ok(Playlist {
|
||||
id,
|
||||
name,
|
||||
videos,
|
||||
n_videos,
|
||||
ctoken,
|
||||
thumbnails,
|
||||
description,
|
||||
channel,
|
||||
last_update: match &last_update_txt {
|
||||
Some(textual_date) => timeago::parse_textual_date_to_dt(lang, textual_date),
|
||||
let mut warnings = video_items.warnings.to_owned();
|
||||
let last_update = match &last_update_txt {
|
||||
Some(textual_date) => {
|
||||
let parsed = timeago::parse_textual_date_to_dt(lang, textual_date);
|
||||
if parsed.is_none() {
|
||||
warnings.push(format!("could not parse textual date `{}`", textual_date));
|
||||
}
|
||||
parsed
|
||||
}
|
||||
None => None,
|
||||
},
|
||||
last_update_txt,
|
||||
})
|
||||
};
|
||||
|
||||
Ok(MapResult {
|
||||
c: Playlist {
|
||||
id: playlist_id,
|
||||
name,
|
||||
videos,
|
||||
n_videos,
|
||||
ctoken,
|
||||
thumbnails,
|
||||
description,
|
||||
channel,
|
||||
last_update,
|
||||
last_update_txt,
|
||||
},
|
||||
warnings,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl MapResponse<(Vec<Video>, Option<String>)> for response::PlaylistCont {
|
||||
fn map_response(
|
||||
self,
|
||||
id: &str,
|
||||
_lang: Language,
|
||||
_deobf: Option<&Deobfuscator>,
|
||||
) -> Result<MapResult<(Vec<Video>, Option<String>)>> {
|
||||
let action = some_or_bail!(
|
||||
self.on_response_received_actions
|
||||
.iter()
|
||||
.find(|a| a.append_continuation_items_action.target_id == id),
|
||||
Err(anyhow!("no continuation action"))
|
||||
);
|
||||
|
||||
Ok(MapResult {
|
||||
c: map_playlist_items(&action.append_continuation_items_action.continuation_items.c),
|
||||
warnings: action
|
||||
.append_continuation_items_action
|
||||
.continuation_items
|
||||
.warnings
|
||||
.to_owned(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn map_playlist_items(
|
||||
items: &Vec<response::VideoListItem<response::playlist::PlaylistVideo>>,
|
||||
items: &[response::VideoListItem<response::playlist::PlaylistVideo>],
|
||||
) -> (Vec<Video>, Option<String>) {
|
||||
let mut ctoken: Option<String> = None;
|
||||
let videos = items
|
||||
|
|
@ -246,30 +259,27 @@ fn map_playlist_items(
|
|||
response::VideoListItem::GridVideoRenderer { video } => match &video.channel {
|
||||
TextLink::Browse {
|
||||
text,
|
||||
page_type,
|
||||
page_type: PageType::Channel,
|
||||
browse_id,
|
||||
} => match page_type {
|
||||
PageType::Channel => Some(Video {
|
||||
id: video.video_id.to_owned(),
|
||||
title: video.title.to_owned(),
|
||||
length: video.length_seconds,
|
||||
thumbnails: video
|
||||
.thumbnail
|
||||
.thumbnails
|
||||
.iter()
|
||||
.map(|t| Thumbnail {
|
||||
url: t.url.to_owned(),
|
||||
width: t.width,
|
||||
height: t.height,
|
||||
})
|
||||
.collect(),
|
||||
channel: Channel {
|
||||
id: browse_id.to_string(),
|
||||
name: text.to_owned(),
|
||||
},
|
||||
}),
|
||||
_ => None,
|
||||
},
|
||||
} => Some(Video {
|
||||
id: video.video_id.to_owned(),
|
||||
title: video.title.to_owned(),
|
||||
length: video.length_seconds,
|
||||
thumbnails: video
|
||||
.thumbnail
|
||||
.thumbnails
|
||||
.iter()
|
||||
.map(|t| Thumbnail {
|
||||
url: t.url.to_owned(),
|
||||
width: t.width,
|
||||
height: t.height,
|
||||
})
|
||||
.collect(),
|
||||
channel: Channel {
|
||||
id: browse_id.to_string(),
|
||||
name: text.to_owned(),
|
||||
},
|
||||
}),
|
||||
_ => None,
|
||||
},
|
||||
response::VideoListItem::ContinuationItemRenderer {
|
||||
|
|
@ -289,49 +299,10 @@ mod tests {
|
|||
|
||||
use rstest::rstest;
|
||||
|
||||
use crate::{client::RustyPipe, report::TestFileReporter};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test_log::test(tokio::test)]
|
||||
async fn download_testfiles() {
|
||||
let tf_dir = Path::new("testfiles/playlist");
|
||||
|
||||
let rt = RustyTube::new();
|
||||
|
||||
for (name, id) in [
|
||||
("short", "RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk"),
|
||||
("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ"),
|
||||
("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe"),
|
||||
] {
|
||||
let mut json_path = tf_dir.to_path_buf();
|
||||
json_path.push(format!("playlist_{}.json", name));
|
||||
if json_path.exists() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let client = rt.get_ytclient(ClientType::Desktop);
|
||||
let context = client.get_context(false).await;
|
||||
|
||||
let request_body = QPlaylist {
|
||||
context,
|
||||
browse_id: "VL".to_owned() + id,
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "browse")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.error_for_status()
|
||||
.unwrap();
|
||||
|
||||
let mut file = std::fs::File::create(json_path).unwrap();
|
||||
let mut content = std::io::Cursor::new(resp.bytes().await.unwrap());
|
||||
std::io::copy(&mut content, &mut file).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::long(
|
||||
"PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ",
|
||||
|
|
@ -368,8 +339,8 @@ mod tests {
|
|||
#[case] description: Option<String>,
|
||||
#[case] channel: Option<Channel>,
|
||||
) {
|
||||
let rt = RustyTube::new();
|
||||
let playlist = rt.get_playlist(id).await.unwrap();
|
||||
let rp = RustyPipe::new_test();
|
||||
let playlist = rp.test_query().get_playlist(id).await.unwrap();
|
||||
|
||||
assert_eq!(playlist.id, id);
|
||||
assert_eq!(playlist.name, name);
|
||||
|
|
@ -378,37 +349,70 @@ mod tests {
|
|||
assert!(playlist.n_videos > 10);
|
||||
assert_eq!(playlist.n_videos > 100, is_long);
|
||||
assert_eq!(playlist.description, description);
|
||||
assert_eq!(playlist.channel, channel);
|
||||
if channel.is_some() {
|
||||
assert_eq!(playlist.channel, channel);
|
||||
}
|
||||
assert!(!playlist.thumbnails.is_empty());
|
||||
}
|
||||
|
||||
#[test_log::test(tokio::test)]
|
||||
async fn download_testfiles() {
|
||||
let tf_dir = Path::new("testfiles/playlist");
|
||||
|
||||
for (name, id) in [
|
||||
("short", "RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk"),
|
||||
("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ"),
|
||||
("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe"),
|
||||
] {
|
||||
let mut json_path = tf_dir.to_path_buf();
|
||||
json_path.push(format!("playlist_{}.json", name));
|
||||
if json_path.exists() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let reporter = TestFileReporter::new(json_path);
|
||||
let rp = RustyPipe::new(None, Some(Box::new(reporter)), None);
|
||||
rp.test_query().report(true).get_playlist(id).await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::long("long")]
|
||||
#[case::short("short")]
|
||||
#[case::nomusic("nomusic")]
|
||||
fn t_map_playlist_data(#[case] name: &str) {
|
||||
#[case::short("short", "RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk")]
|
||||
#[case::long("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ")]
|
||||
#[case::nomusic("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe")]
|
||||
fn t_map_playlist_data(#[case] name: &str, #[case] id: &str) {
|
||||
let filename = format!("testfiles/playlist/playlist_{}.json", name);
|
||||
let json_path = Path::new(&filename);
|
||||
let json_file = File::open(json_path).unwrap();
|
||||
|
||||
let playlist: response::Playlist =
|
||||
serde_json::from_reader(BufReader::new(json_file)).unwrap();
|
||||
let playlist_data = map_playlist(&playlist, Language::En).unwrap();
|
||||
insta::assert_yaml_snapshot!(format!("map_playlist_data_{}", name), playlist_data, {
|
||||
let map_res = playlist.map_response(id, Language::En, None).unwrap();
|
||||
|
||||
assert!(
|
||||
map_res.warnings.is_empty(),
|
||||
"deserialization/mapping warnings: {:?}",
|
||||
map_res.warnings
|
||||
);
|
||||
insta::assert_yaml_snapshot!(format!("map_playlist_data_{}", name), map_res.c, {
|
||||
".last_update" => "[date]"
|
||||
});
|
||||
}
|
||||
|
||||
#[test_log::test(tokio::test)]
|
||||
async fn t_playlist_cont() {
|
||||
let rt = RustyTube::new();
|
||||
let mut playlist = rt
|
||||
let rp = RustyPipe::new_test();
|
||||
let mut playlist = rp
|
||||
.test_query()
|
||||
.get_playlist("PLbZIPy20-1pN7mqjckepWF78ndb6ci_qi")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
while playlist.ctoken.is_some() {
|
||||
rt.get_playlist_cont(&mut playlist).await.unwrap();
|
||||
rp.test_query()
|
||||
.get_playlist_cont(&mut playlist)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
assert!(playlist.videos.len() > 100);
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ use serde_with::VecSkipError;
|
|||
|
||||
use super::TimeOverlay;
|
||||
use super::{ContentRenderer, ContentsRenderer, Thumbnails, VideoListItem};
|
||||
use crate::serializer::text::Text;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
|
@ -63,11 +64,11 @@ pub struct GridRenderer {
|
|||
pub struct ChannelVideo {
|
||||
pub video_id: String,
|
||||
pub thumbnail: Thumbnails,
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub title: String,
|
||||
#[serde_as(as = "Option<crate::serializer::text::Text>")]
|
||||
#[serde_as(as = "Option<Text>")]
|
||||
pub published_time_text: Option<String>,
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub view_count_text: String,
|
||||
#[serde_as(as = "VecSkipError<_>")]
|
||||
pub thumbnail_overlays: Vec<TimeOverlay>,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ pub mod video;
|
|||
pub use channel::Channel;
|
||||
pub use player::Player;
|
||||
pub use playlist::Playlist;
|
||||
pub use playlist::PlaylistCont;
|
||||
pub use playlist_music::PlaylistMusic;
|
||||
pub use video::Video;
|
||||
pub use video::VideoComments;
|
||||
|
|
@ -15,7 +16,7 @@ pub use video::VideoRecommendations;
|
|||
use serde::Deserialize;
|
||||
use serde_with::{serde_as, DefaultOnError, VecSkipError};
|
||||
|
||||
use crate::serializer::text::TextLink;
|
||||
use crate::serializer::text::{Text, TextLink, TextLinks};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
|
@ -93,10 +94,10 @@ pub struct VideoOwner {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct VideoOwnerRenderer {
|
||||
#[serde_as(as = "crate::serializer::text::TextLink")]
|
||||
#[serde_as(as = "TextLink")]
|
||||
pub title: TextLink,
|
||||
pub thumbnail: Thumbnails,
|
||||
#[serde_as(as = "Option<crate::serializer::text::Text>")]
|
||||
#[serde_as(as = "Option<Text>")]
|
||||
pub subscriber_count_text: Option<String>,
|
||||
#[serde(default)]
|
||||
#[serde_as(as = "VecSkipError<_>")]
|
||||
|
|
@ -132,7 +133,7 @@ pub struct TimeOverlay {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TimeOverlayRenderer {
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub text: String,
|
||||
#[serde(default)]
|
||||
#[serde_as(deserialize_as = "DefaultOnError")]
|
||||
|
|
@ -198,7 +199,7 @@ pub struct MusicColumn {
|
|||
#[serde_as]
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct MusicColumnRenderer {
|
||||
#[serde_as(as = "crate::serializer::text::TextLinks")]
|
||||
#[serde_as(as = "TextLinks")]
|
||||
pub text: Vec<TextLink>,
|
||||
}
|
||||
|
||||
|
|
@ -213,3 +214,23 @@ pub struct MusicContinuation {
|
|||
pub struct MusicContinuationData {
|
||||
pub continuation: String,
|
||||
}
|
||||
|
||||
impl From<Thumbnail> for crate::model::Thumbnail {
|
||||
fn from(tn: Thumbnail) -> Self {
|
||||
crate::model::Thumbnail {
|
||||
url: tn.url,
|
||||
width: tn.width,
|
||||
height: tn.height,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Thumbnails> for Vec<crate::model::Thumbnail> {
|
||||
fn from(ts: Thumbnails) -> Self {
|
||||
let mut thumbnails = vec![];
|
||||
for t in ts.thumbnails {
|
||||
thumbnails.push(t.into());
|
||||
}
|
||||
thumbnails
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@ use std::ops::Range;
|
|||
use chrono::NaiveDate;
|
||||
use serde::Deserialize;
|
||||
use serde_with::serde_as;
|
||||
use serde_with::{json::JsonString, DefaultOnError, VecSkipError};
|
||||
use serde_with::{json::JsonString, DefaultOnError};
|
||||
|
||||
use super::Thumbnails;
|
||||
use crate::serializer::{text::Text, MapResult, VecLogError};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
|
@ -45,11 +46,11 @@ pub struct StreamingData {
|
|||
#[serde_as(as = "JsonString")]
|
||||
pub expires_in_seconds: u32,
|
||||
#[serde(default)]
|
||||
#[serde_as(as = "VecSkipError<_>")]
|
||||
pub formats: Vec<Format>,
|
||||
#[serde_as(as = "VecLogError<_>")]
|
||||
pub formats: MapResult<Vec<Format>>,
|
||||
#[serde(default)]
|
||||
#[serde_as(as = "VecSkipError<_>")]
|
||||
pub adaptive_formats: Vec<Format>,
|
||||
#[serde_as(as = "VecLogError<_>")]
|
||||
pub adaptive_formats: MapResult<Vec<Format>>,
|
||||
/// Only on livestreams
|
||||
pub dash_manifest_url: Option<String>,
|
||||
/// Only on livestreams
|
||||
|
|
@ -73,20 +74,20 @@ pub struct Format {
|
|||
pub width: Option<u32>,
|
||||
pub height: Option<u32>,
|
||||
|
||||
#[serde_as(as = "Option<crate::serializer::range::Range>")]
|
||||
#[serde_as(as = "Option<crate::serializer::Range>")]
|
||||
pub index_range: Option<Range<u32>>,
|
||||
#[serde_as(as = "Option<crate::serializer::range::Range>")]
|
||||
#[serde_as(as = "Option<crate::serializer::Range>")]
|
||||
pub init_range: Option<Range<u32>>,
|
||||
|
||||
#[serde_as(as = "JsonString")]
|
||||
pub content_length: u64,
|
||||
#[serde_as(as = "Option<JsonString>")]
|
||||
pub content_length: Option<u64>,
|
||||
|
||||
#[serde(default)]
|
||||
#[serde_as(deserialize_as = "DefaultOnError")]
|
||||
pub quality: Option<Quality>,
|
||||
pub fps: Option<u8>,
|
||||
pub quality_label: Option<String>,
|
||||
pub average_bitrate: u32,
|
||||
pub average_bitrate: Option<u32>,
|
||||
pub color_info: Option<ColorInfo>,
|
||||
|
||||
// Audio only
|
||||
|
|
@ -104,7 +105,9 @@ pub struct Format {
|
|||
|
||||
impl Format {
|
||||
pub fn is_audio(&self) -> bool {
|
||||
self.audio_quality.is_some() && self.audio_sample_rate.is_some()
|
||||
self.content_length.is_some()
|
||||
&& self.audio_quality.is_some()
|
||||
&& self.audio_sample_rate.is_some()
|
||||
}
|
||||
|
||||
pub fn is_video(&self) -> bool {
|
||||
|
|
@ -188,7 +191,7 @@ pub struct PlayerCaptionsTracklistRenderer {
|
|||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CaptionTrack {
|
||||
pub base_url: String,
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub name: String,
|
||||
pub language_code: String,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ use serde::Deserialize;
|
|||
use serde_with::serde_as;
|
||||
use serde_with::{json::JsonString, DefaultOnError, VecSkipError};
|
||||
|
||||
use crate::serializer::text::TextLink;
|
||||
use crate::serializer::text::{Text, TextLink};
|
||||
use crate::serializer::{MapResult, VecLogError};
|
||||
|
||||
use super::{ContentRenderer, ContentsRenderer, Thumbnails, ThumbnailsWrap, VideoListItem};
|
||||
|
||||
|
|
@ -56,8 +57,8 @@ pub struct PlaylistVideoListRenderer {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PlaylistVideoList {
|
||||
#[serde_as(as = "VecSkipError<_>")]
|
||||
pub contents: Vec<VideoListItem<PlaylistVideo>>,
|
||||
#[serde_as(as = "VecLogError<_>")]
|
||||
pub contents: MapResult<Vec<VideoListItem<PlaylistVideo>>>,
|
||||
}
|
||||
|
||||
#[serde_as]
|
||||
|
|
@ -66,10 +67,10 @@ pub struct PlaylistVideoList {
|
|||
pub struct PlaylistVideo {
|
||||
pub video_id: String,
|
||||
pub thumbnail: Thumbnails,
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub title: String,
|
||||
#[serde(rename = "shortBylineText")]
|
||||
#[serde_as(as = "crate::serializer::text::TextLink")]
|
||||
#[serde_as(as = "TextLink")]
|
||||
pub channel: TextLink,
|
||||
#[serde_as(as = "JsonString")]
|
||||
pub length_seconds: u32,
|
||||
|
|
@ -86,14 +87,14 @@ pub struct Header {
|
|||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HeaderRenderer {
|
||||
pub playlist_id: String,
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub title: String,
|
||||
#[serde(default)]
|
||||
#[serde_as(as = "DefaultOnError<Option<crate::serializer::text::Text>>")]
|
||||
#[serde_as(as = "DefaultOnError<Option<Text>>")]
|
||||
pub description_text: Option<String>,
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub num_videos_text: String,
|
||||
#[serde_as(as = "Option<crate::serializer::text::TextLink>")]
|
||||
#[serde_as(as = "Option<TextLink>")]
|
||||
pub owner_text: Option<TextLink>,
|
||||
|
||||
// Alternative layout
|
||||
|
|
@ -118,7 +119,7 @@ pub struct Byline {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BylineRenderer {
|
||||
#[serde_as(as = "crate::serializer::text::Text")]
|
||||
#[serde_as(as = "Text")]
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
|
|
@ -150,7 +151,7 @@ pub struct SidebarPrimaryInfoRenderer {
|
|||
// - `"495", " videos"`
|
||||
// - `"3,310,996 views"`
|
||||
// - `"Last updated on ", "Aug 7, 2022"`
|
||||
#[serde_as(as = "Vec<crate::serializer::text::Text>")]
|
||||
#[serde_as(as = "Vec<Text>")]
|
||||
pub stats: Vec<String>,
|
||||
}
|
||||
|
||||
|
|
@ -172,7 +173,7 @@ pub struct OnResponseReceivedAction {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AppendAction {
|
||||
#[serde_as(as = "VecSkipError<_>")]
|
||||
pub continuation_items: Vec<VideoListItem<PlaylistVideo>>,
|
||||
#[serde_as(as = "VecLogError<_>")]
|
||||
pub continuation_items: MapResult<Vec<VideoListItem<PlaylistVideo>>>,
|
||||
pub target_id: String,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
#![allow(clippy::enum_variant_names)]
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_with::serde_as;
|
||||
use serde_with::{DefaultOnError, VecSkipError};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/player.rs
|
||||
expression: player_data
|
||||
expression: map_res.c
|
||||
---
|
||||
info:
|
||||
id: pPvd8UxmSbQ
|
||||
|
|
@ -184,6 +184,22 @@ video_only_streams:
|
|||
format: mp4
|
||||
codec: av01
|
||||
throttled: false
|
||||
- url: "https://rr5---sn-h0jeenek.googlevideo.com/videoplayback?c=ANDROID&dur=163.096&ei=q1jpYtOPEYSBgQeHmqbwAQ&expire=1659481355&fexp=24001373%2C24007246&fvip=4&id=o-AEDMTCojVtwpIKOdhBaxEHE5s322qnAJHGqa2r1F46BM&initcwndbps=1527500&ip=2003%3Ade%3Aaf0e%3A2f00%3Ade47%3A297%3Aa6db%3A774e&itag=22&lmt=1580005750956837&lsig=AG3C_xAwRgIhAOiL-qJ04sA8FSOkEJfOYl3gFe4SzwYu_rAf3DMLHYigAiEA0Upi1HqqIu7NH_LTDL0jT1R5TTozQypL5FiSP9RoqtU%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&mh=mQ&mime=video%2Fmp4&mm=31%2C29&mn=sn-h0jeenek%2Csn-h0jelnez&ms=au%2Crdu&mt=1659459429&mv=m&mvi=5&pl=37&ratebypass=yes&rbqsm=fr&requiressl=yes&sig=AOq0QJ8wRAIgFlQZgR63Yz9UgY9gVqiyGDVkZmSmACRP3-MmKN7CRzQCIAMHAwZbHmWL1qNH4Nu3A0pXZwErXMVPzMIt-PyxeZqa&source=youtube&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cratebypass%2Cdur%2Clmt&txp=2211222&vprv=1"
|
||||
itag: 22
|
||||
bitrate: 1574434
|
||||
average_bitrate: 1574434
|
||||
size: ~
|
||||
index_range: ~
|
||||
init_range: ~
|
||||
width: 1280
|
||||
height: 720
|
||||
fps: 30
|
||||
quality: 720p
|
||||
hdr: false
|
||||
mime: "video/mp4; codecs=\"avc1.64001F, mp4a.40.2\""
|
||||
format: mp4
|
||||
codec: avc1
|
||||
throttled: false
|
||||
- url: "https://rr5---sn-h0jeenek.googlevideo.com/videoplayback?c=ANDROID&clen=22365208&dur=163.046&ei=q1jpYtOPEYSBgQeHmqbwAQ&expire=1659481355&fexp=24001373%2C24007246&fvip=4&gir=yes&id=o-AEDMTCojVtwpIKOdhBaxEHE5s322qnAJHGqa2r1F46BM&initcwndbps=1527500&ip=2003%3Ade%3Aaf0e%3A2f00%3Ade47%3A297%3Aa6db%3A774e&itag=398&keepalive=yes&lmt=1608048380553749&lsig=AG3C_xAwRgIhAOiL-qJ04sA8FSOkEJfOYl3gFe4SzwYu_rAf3DMLHYigAiEA0Upi1HqqIu7NH_LTDL0jT1R5TTozQypL5FiSP9RoqtU%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&mh=mQ&mime=video%2Fmp4&mm=31%2C29&mn=sn-h0jeenek%2Csn-h0jelnez&ms=au%2Crdu&mt=1659459429&mv=m&mvi=5&otfp=1&pl=37&rbqsm=fr&requiressl=yes&sig=AOq0QJ8wRAIgR6KqCOoig_FMl2tWKa7qHSmCjIZa9S7ABzEI16qdO2sCIFXccwql4bqV9CHlqXY4tgxyMFUsp7vW4XUjxs3AyG6H&source=youtube&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cotfp%2Cdur%2Clmt&txp=1311222&vprv=1"
|
||||
itag: 398
|
||||
bitrate: 1348419
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/player.rs
|
||||
expression: player_data
|
||||
expression: map_res.c
|
||||
---
|
||||
info:
|
||||
id: pPvd8UxmSbQ
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/player.rs
|
||||
expression: player_data
|
||||
expression: map_res.c
|
||||
---
|
||||
info:
|
||||
id: pPvd8UxmSbQ
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/player.rs
|
||||
expression: player_data
|
||||
expression: map_res.c
|
||||
---
|
||||
info:
|
||||
id: pPvd8UxmSbQ
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/player.rs
|
||||
expression: player_data
|
||||
expression: map_res.c
|
||||
---
|
||||
info:
|
||||
id: pPvd8UxmSbQ
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/playlist.rs
|
||||
expression: playlist_data
|
||||
expression: map_res.c
|
||||
---
|
||||
id: PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ
|
||||
name: Die schönsten deutschen Lieder | Beliebteste Lieder | Beste Deutsche Musik 2022
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/playlist.rs
|
||||
expression: playlist_data
|
||||
expression: map_res.c
|
||||
---
|
||||
id: PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe
|
||||
name: Minecraft SHINE
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: src/client/playlist.rs
|
||||
expression: playlist_data
|
||||
expression: map_res.c
|
||||
---
|
||||
id: RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk
|
||||
name: Easy Pop
|
||||
|
|
|
|||
|
|
@ -1,111 +0,0 @@
|
|||
use anyhow::Result;
|
||||
use reqwest::Method;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{response, ClientType, ContextYT, RustyTube};
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
struct QVideo {
|
||||
context: ContextYT,
|
||||
/// YouTube video ID
|
||||
video_id: String,
|
||||
/// Set to true to allow extraction of streams with sensitive content
|
||||
content_check_ok: bool,
|
||||
/// Probably refers to allowing sensitive content, too
|
||||
racy_check_ok: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
struct QVideoCont {
|
||||
context: ContextYT,
|
||||
continuation: String,
|
||||
}
|
||||
|
||||
impl RustyTube {
|
||||
async fn get_video_response(&self, video_id: &str) -> Result<response::Video> {
|
||||
let client = self.get_ytclient(ClientType::Desktop);
|
||||
let context = client.get_context(true).await;
|
||||
let request_body = QVideo {
|
||||
context,
|
||||
video_id: video_id.to_owned(),
|
||||
content_check_ok: true,
|
||||
racy_check_ok: true,
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "next")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
Ok(resp.json::<response::Video>().await?)
|
||||
}
|
||||
|
||||
async fn get_comments_response(&self, ctoken: &str) -> Result<response::VideoComments> {
|
||||
let client = self.get_ytclient(ClientType::Desktop);
|
||||
let context = client.get_context(true).await;
|
||||
let request_body = QVideoCont {
|
||||
context,
|
||||
continuation: ctoken.to_owned(),
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "next")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
Ok(resp.json::<response::VideoComments>().await?)
|
||||
}
|
||||
|
||||
async fn get_recommendations_response(
|
||||
&self,
|
||||
ctoken: &str,
|
||||
) -> Result<response::VideoRecommendations> {
|
||||
let client = self.get_ytclient(ClientType::Desktop);
|
||||
let context = client.get_context(true).await;
|
||||
let request_body = QVideoCont {
|
||||
context,
|
||||
continuation: ctoken.to_owned(),
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.request_builder(Method::POST, "next")
|
||||
.await
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
Ok(resp.json::<response::VideoRecommendations>().await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn t_get_video_response() {
|
||||
let rt = RustyTube::new();
|
||||
// rt.get_video("ZeerrnuLi5E").await.unwrap();
|
||||
dbg!(rt.get_video_response("iQfSvIgIs_M").await.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn t_get_comments_response() {
|
||||
let rt = RustyTube::new();
|
||||
// rt.get_comments("Eg0SC2lRZlN2SWdJc19NGAYyJSIRIgtpUWZTdklnSXNfTTAAeAJCEGNvbW1lbnRzLXNlY3Rpb24%3D").await.unwrap();
|
||||
dbg!(rt.get_comments_response("Eg0SC2lRZlN2SWdJc19NGAYychpFEhRVZ2lnVGJVTEZ6Qk5FWGdDb0FFQyICCAAqGFVDWFgwUldPSUJqdDRvM3ppSHUtNmE1QTILaVFmU3ZJZ0lzX01AAUgKQiljb21tZW50LXJlcGxpZXMtaXRlbS1VZ2lnVGJVTEZ6Qk5FWGdDb0FFQw%3D%3D").await.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn t_get_recommendations_response() {
|
||||
let rt = RustyTube::new();
|
||||
dbg!(rt.get_recommendations_response("CBQSExILaVFmU3ZJZ0lzX03AAQHIAQEYACqkBjJzNkw2d3pVQkFyUkJBb0Q4ajRBQ2c3Q1Bnc0lvWXlRejhLZnRZUGNBUW9EOGo0QUNnN0NQZ3NJeElEX2w0YjFtNnUtQVFvRDhqNEFDZzNDUGdvSXg5Ykx3WUNKenFwX0NnUHlQZ0FLRGNJLUNnaW83T2pqZzVPTHZEOEtBX0ktQUFvTndqNEtDTE9venZmQThybVhXd29EOGo0QUNnM0NQZ29JdzZETV9vSFk0cHRCQ2dQeVBnQUtEc0ktQ3dqbW9QbURpcHVPel80QkNnUHlQZ0FLRGNJLUNnalY4THpEazlfOTRCWUtBX0ktQUFvT3dqNExDTXVZNU9YZzE3ejV2d0VLQV9JLUFBb053ajRLQ1A3eHZiSGswTnVuYWdvRDhqNEFDZzdDUGdzSXFQYVU5ZGp2Ml96S0FRb0Q4ajRBQ2c3Q1Bnc0lfSW1acUtQOTlfQ09BUW9EOGo0QUNnM0NQZ29JeGRtNzlZS3prcUFqQ2dQeVBnQUtEY0ktQ2dpZ3FJMkg0UENRX2s0S0FfSS1BQW9Pd2o0TENQV0V5NV9ZeDhERl9nRUtBX0ktQUFvT3dqNExDTzJid3VuV3BPX3ppd0VLQV9JLUFBb2gwajRlQ2h4U1JFTk5WVU5ZV0RCU1YwOUpRbXAwTkc4emVtbElkUzAyWVRWQkNnUHlQZ0FLRGNJLUNnaXpqcXZwcDh5MWwwMEtBX0ktQUFvTndqNEtDTFhWbl83dHhfWDJOUW9EOGo0QUNnN0NQZ3NJNWR5ZWc1NjZyUGUwQVJJVUFBSUVCZ2dLREE0UUVoUVdHQm9jSGlBaUpDWWFCQWdBRUFFYUJBZ0NFQU1hQkFnRUVBVWFCQWdHRUFjYUJBZ0lFQWthQkFnS0VBc2FCQWdNRUEwYUJBZ09FQThhQkFnUUVCRWFCQWdTRUJNYUJBZ1VFQlVhQkFnV0VCY2FCQWdZRUJrYUJBZ2FFQnNhQkFnY0VCMGFCQWdlRUI4YUJBZ2dFQ0VhQkFnaUVDTWFCQWdrRUNVYUJBZ21FQ2NxRkFBQ0JBWUlDZ3dPRUJJVUZoZ2FIQjRnSWlRbWoPd2F0Y2gtbmV4dC1mZWVk").await.unwrap());
|
||||
}
|
||||
}
|
||||
Reference in a new issue