refactor: convert _or_bail macros to ok_or functions

This commit is contained in:
ThetaDev 2022-10-18 19:09:16 +02:00
parent 9aafb84e0f
commit b5f6b7a174
13 changed files with 274 additions and 282 deletions

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use serde::Serialize;
use url::Url;
@ -15,7 +17,7 @@ use super::{response, ClientType, MapResponse, RustyPipeQuery, YTContext};
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct QChannel<'a> {
context: YTContext,
context: YTContext<'a>,
browse_id: &'a str,
params: Params,
}
@ -257,11 +259,11 @@ fn map_vanity_url(url: &str, id: &str) -> Option<String> {
return None;
}
let mut parsed_url = ok_or_bail!(Url::parse(url), None);
// The vanity URL from YouTube is http for some reason
let _ = parsed_url.set_scheme("https");
Some(parsed_url.to_string())
Url::parse(url).ok().map(|mut parsed_url| {
// The vanity URL from YouTube is http for some reason
let _ = parsed_url.set_scheme("https");
parsed_url.to_string()
})
}
fn map_channel<T>(
@ -272,13 +274,17 @@ fn map_channel<T>(
id: &str,
lang: Language,
) -> Result<Channel<T>, ExtractionError> {
let header =
header.ok_or_else(|| ExtractionError::ContentUnavailable("channel not found".into()))?;
let header = header.ok_or(ExtractionError::ContentUnavailable(Cow::Borrowed(
"channel not found",
)))?;
let metadata = metadata
.ok_or_else(|| ExtractionError::ContentUnavailable("channel not found".into()))?
.ok_or(ExtractionError::ContentUnavailable(Cow::Borrowed(
"channel not found",
)))?
.channel_metadata_renderer;
let microformat = microformat
.ok_or_else(|| ExtractionError::ContentUnavailable("channel not found".into()))?;
let microformat = microformat.ok_or(ExtractionError::ContentUnavailable(Cow::Borrowed(
"channel not found",
)))?;
if metadata.external_id != id {
return Err(ExtractionError::WrongResult(format!(
@ -384,7 +390,9 @@ fn map_channel_content(
}
})
.next()
.ok_or_else(|| ExtractionError::InvalidData("could not extract content".into()))?;
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"could not extract content",
)))?;
if let Some(target_id) = target_id {
// YouTube falls back to the featured page if the channel does not have a "videos" tab.

View file

@ -15,8 +15,8 @@ mod video_details;
#[cfg_attr(docsrs, doc(cfg(feature = "rss")))]
mod channel_rss;
use std::fmt::Debug;
use std::sync::Arc;
use std::{borrow::Cow, fmt::Debug};
use chrono::{DateTime, Duration, Utc};
use fancy_regex::Regex;
@ -69,29 +69,29 @@ impl ClientType {
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct YTContext {
client: ClientInfo,
struct YTContext<'a> {
client: ClientInfo<'a>,
/// only used on desktop
#[serde(skip_serializing_if = "Option::is_none")]
request: Option<RequestYT>,
user: User,
/// only used for the embedded player
#[serde(skip_serializing_if = "Option::is_none")]
third_party: Option<ThirdParty>,
third_party: Option<ThirdParty<'a>>,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ClientInfo {
client_name: String,
client_version: String,
struct ClientInfo<'a> {
client_name: &'a str,
client_version: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
client_screen: Option<String>,
client_screen: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
device_model: Option<String>,
platform: String,
device_model: Option<&'a str>,
platform: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
original_url: Option<String>,
original_url: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
visitor_data: Option<String>,
hl: Language,
@ -124,21 +124,21 @@ struct User {
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ThirdParty {
embed_url: String,
struct ThirdParty<'a> {
embed_url: &'a str,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct QBrowse {
context: YTContext,
struct QBrowse<'a> {
context: YTContext<'a>,
browse_id: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct QContinuation<'a> {
context: YTContext,
context: YTContext<'a>,
continuation: &'a str,
}
@ -531,11 +531,11 @@ impl RustyPipe {
)
.await?;
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &swjs, 1).ok_or_else(|| {
Error::from(ExtractionError::InvalidData(
"Could not find desktop client version in sw.js".into(),
))
})
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &swjs, 1).ok_or(
Error::Extraction(ExtractionError::InvalidData(Cow::Borrowed(
"Could not find desktop client version in sw.js",
))),
)
};
let from_html = async {
@ -549,11 +549,11 @@ impl RustyPipe {
)
.await?;
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &html, 1).ok_or_else(|| {
Error::from(ExtractionError::InvalidData(
"Could not find desktop client version in sw.js".into(),
))
})
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &html, 1).ok_or(
Error::Extraction(ExtractionError::InvalidData(Cow::Borrowed(
"Could not find desktop client version on html page",
))),
)
};
match from_swjs.await {
@ -578,11 +578,11 @@ impl RustyPipe {
)
.await?;
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &swjs, 1).ok_or_else(|| {
Error::from(ExtractionError::InvalidData(
"Could not find music client version in sw.js".into(),
))
})
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &swjs, 1).ok_or(
Error::Extraction(ExtractionError::InvalidData(Cow::Borrowed(
"Could not find music client version in sw.js",
))),
)
};
let from_html = async {
@ -596,11 +596,11 @@ impl RustyPipe {
)
.await?;
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &html, 1).ok_or_else(|| {
Error::from(ExtractionError::InvalidData(
"Could not find music client version on html page".into(),
))
})
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &html, 1).ok_or(
Error::Extraction(ExtractionError::InvalidData(Cow::Borrowed(
"Could not find music client version on html page",
))),
)
};
match from_swjs.await {
@ -757,12 +757,12 @@ impl RustyPipeQuery {
match ctype {
ClientType::Desktop => YTContext {
client: ClientInfo {
client_name: "WEB".to_owned(),
client_version: self.client.get_desktop_client_version().await,
client_name: "WEB",
client_version: Cow::Owned(self.client.get_desktop_client_version().await),
client_screen: None,
device_model: None,
platform: "DESKTOP".to_owned(),
original_url: Some("https://www.youtube.com/".to_owned()),
platform: "DESKTOP",
original_url: Some("https://www.youtube.com/"),
visitor_data: None,
hl,
gl,
@ -773,12 +773,12 @@ impl RustyPipeQuery {
},
ClientType::DesktopMusic => YTContext {
client: ClientInfo {
client_name: "WEB_REMIX".to_owned(),
client_version: self.client.get_music_client_version().await,
client_name: "WEB_REMIX",
client_version: Cow::Owned(self.client.get_music_client_version().await),
client_screen: None,
device_model: None,
platform: "DESKTOP".to_owned(),
original_url: Some("https://music.youtube.com/".to_owned()),
platform: "DESKTOP",
original_url: Some("https://music.youtube.com/"),
visitor_data: None,
hl,
gl,
@ -789,11 +789,11 @@ impl RustyPipeQuery {
},
ClientType::TvHtml5Embed => YTContext {
client: ClientInfo {
client_name: "TVHTML5_SIMPLY_EMBEDDED_PLAYER".to_owned(),
client_version: TVHTML5_CLIENT_VERSION.to_owned(),
client_screen: Some("EMBED".to_owned()),
client_name: "TVHTML5_SIMPLY_EMBEDDED_PLAYER",
client_version: Cow::Borrowed(TVHTML5_CLIENT_VERSION),
client_screen: Some("EMBED"),
device_model: None,
platform: "TV".to_owned(),
platform: "TV",
original_url: None,
visitor_data: None,
hl,
@ -802,16 +802,16 @@ impl RustyPipeQuery {
request: Some(RequestYT::default()),
user: User::default(),
third_party: Some(ThirdParty {
embed_url: "https://www.youtube.com/".to_owned(),
embed_url: "https://www.youtube.com/",
}),
},
ClientType::Android => YTContext {
client: ClientInfo {
client_name: "ANDROID".to_owned(),
client_version: MOBILE_CLIENT_VERSION.to_owned(),
client_name: "ANDROID",
client_version: Cow::Borrowed(MOBILE_CLIENT_VERSION),
client_screen: None,
device_model: None,
platform: "MOBILE".to_owned(),
platform: "MOBILE",
original_url: None,
visitor_data: None,
hl,
@ -823,11 +823,11 @@ impl RustyPipeQuery {
},
ClientType::Ios => YTContext {
client: ClientInfo {
client_name: "IOS".to_owned(),
client_version: MOBILE_CLIENT_VERSION.to_owned(),
client_name: "IOS",
client_version: Cow::Borrowed(MOBILE_CLIENT_VERSION),
client_screen: None,
device_model: Some(IOS_DEVICE_MODEL.to_owned()),
platform: "MOBILE".to_owned(),
device_model: Some(IOS_DEVICE_MODEL),
platform: "MOBILE",
original_url: None,
visitor_data: None,
hl,

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use crate::error::{Error, ExtractionError};
use crate::model::{Comment, Paginator, PlaylistVideo, YouTubeItem};
use crate::param::ContinuationEndpoint;
@ -52,14 +54,13 @@ impl<T: TryFrom<YouTubeItem>> MapResponse<Paginator<T>> for response::Continuati
_deobf: Option<&crate::deobfuscate::Deobfuscator>,
) -> Result<MapResult<Paginator<T>>, ExtractionError> {
let mut actions = self.on_response_received_actions;
let items = some_or_bail!(
actions.try_swap_remove(0),
Err(ExtractionError::InvalidData(
"no item section renderer".into()
))
)
.append_continuation_items_action
.continuation_items;
let items = actions
.try_swap_remove(0)
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"no item section renderer",
)))?
.append_continuation_items_action
.continuation_items;
let mut mapper = response::YouTubeListMapper::<YouTubeItem>::new(lang);
mapper.map_response(items);

View file

@ -27,7 +27,7 @@ use super::{
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct QPlayer<'a> {
context: YTContext,
context: YTContext<'a>,
/// Website playback context
#[serde(skip_serializing_if = "Option::is_none")]
playback_context: Option<QPlaybackContext>,
@ -79,14 +79,11 @@ impl RustyPipeQuery {
video_id: &str,
client_type: ClientType,
) -> Result<VideoPlayer, Error> {
let q1 = self.clone();
let t_context = tokio::spawn(async move { q1.get_context(client_type, false).await });
let q2 = self.client.clone();
let t_deobf = tokio::spawn(async move { q2.get_deobf().await });
let (context, deobf) = tokio::join!(t_context, t_deobf);
let context = context.unwrap();
let deobf = deobf.unwrap()?;
let (context, deobf) = tokio::join!(
self.get_context(client_type, false),
self.client.get_deobf()
);
let deobf = deobf?;
let request_body = if client_type.is_web() {
QPlayer {
@ -168,14 +165,16 @@ impl MapResponse<VideoPlayer> for response::Player {
}
};
let mut streaming_data = some_or_bail!(
self.streaming_data,
Err(ExtractionError::InvalidData("no streaming data".into()))
);
let video_details = some_or_bail!(
self.video_details,
Err(ExtractionError::InvalidData("no video details".into()))
);
let mut streaming_data =
self.streaming_data
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"no streaming data",
)))?;
let video_details =
self.video_details
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"no video details",
)))?;
if video_details.video_id != id {
return Err(ExtractionError::WrongResult(format!(
@ -294,12 +293,11 @@ fn cipher_to_url_params(
// `sp`: Signature parameter
// `url`: URL that is missing the signature parameter
let sig = some_or_bail!(params.get("s"), Err(DeobfError::Extraction("s param")));
let sp = some_or_bail!(params.get("sp"), Err(DeobfError::Extraction("sp param")));
let raw_url = some_or_bail!(
params.get("url"),
Err(DeobfError::Extraction("no url param"))
);
let sig = params.get("s").ok_or(DeobfError::Extraction("s param"))?;
let sp = params.get("sp").ok_or(DeobfError::Extraction("sp param"))?;
let raw_url = params
.get("url")
.ok_or(DeobfError::Extraction("no url param"))?;
let (url_base, mut url_params) =
util::url_to_params(raw_url).or(Err(DeobfError::Extraction("url params")))?;

View file

@ -1,4 +1,4 @@
use std::convert::TryFrom;
use std::{borrow::Cow, convert::TryFrom};
use crate::{
deobfuscate::Deobfuscator,
@ -63,42 +63,40 @@ impl MapResponse<Playlist> for response::Playlist {
};
let mut tcbr_contents = contents.two_column_browse_results_renderer.contents;
let video_items = some_or_bail!(
some_or_bail!(
some_or_bail!(
tcbr_contents.try_swap_remove(0),
Err(ExtractionError::InvalidData(
"twoColumnBrowseResultsRenderer empty".into()
))
)
.tab_renderer
.content
.section_list_renderer
.contents
.try_swap_remove(0),
Err(ExtractionError::InvalidData(
"sectionListRenderer empty".into()
))
)
let video_items = tcbr_contents
.try_swap_remove(0)
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"twoColumnBrowseResultsRenderer empty",
)))?
.tab_renderer
.content
.section_list_renderer
.contents
.try_swap_remove(0)
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"sectionListRenderer empty",
)))?
.item_section_renderer
.contents
.try_swap_remove(0),
Err(ExtractionError::InvalidData(
"itemSectionRenderer empty".into()
))
)
.playlist_video_list_renderer
.contents;
.try_swap_remove(0)
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"itemSectionRenderer empty",
)))?
.playlist_video_list_renderer
.contents;
let (videos, ctoken) = map_playlist_items(video_items.c);
let (thumbnails, last_update_txt) = match self.sidebar {
Some(sidebar) => {
let mut sidebar_items = sidebar.playlist_sidebar_renderer.items;
let mut primary = some_or_bail!(
sidebar_items.try_swap_remove(0),
Err(ExtractionError::InvalidData("no primary sidebar".into()))
);
let mut primary =
sidebar_items
.try_swap_remove(0)
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"no primary sidebar",
)))?;
(
primary
@ -113,10 +111,12 @@ impl MapResponse<Playlist> for response::Playlist {
)
}
None => {
let header_banner = some_or_bail!(
header.playlist_header_renderer.playlist_header_banner,
Err(ExtractionError::InvalidData("no thumbnail found".into()))
);
let header_banner = header
.playlist_header_renderer
.playlist_header_banner
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"no thumbnail found",
)))?;
let mut byline = header.playlist_header_renderer.byline;
let last_update_txt = byline
@ -131,12 +131,8 @@ impl MapResponse<Playlist> for response::Playlist {
};
let n_videos = match ctoken {
Some(_) => {
ok_or_bail!(
util::parse_numeric(&header.playlist_header_renderer.num_videos_text),
Err(ExtractionError::InvalidData("no video count".into()))
)
}
Some(_) => util::parse_numeric(&header.playlist_header_renderer.num_videos_text)
.map_err(|_| ExtractionError::InvalidData(Cow::Borrowed("no video count")))?,
None => videos.len() as u64,
};
@ -187,7 +183,9 @@ impl MapResponse<Paginator<PlaylistVideo>> for response::PlaylistCont {
let mut actions = self.on_response_received_actions;
let action = actions
.try_swap_remove(0)
.ok_or_else(|| ExtractionError::InvalidData("no onResponseReceivedAction".into()))?;
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"no onResponseReceivedAction",
)))?;
let (items, ctoken) =
map_playlist_items(action.append_continuation_items_action.continuation_items.c);

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use serde::{de::IgnoredAny, Serialize};
use crate::{
@ -12,7 +14,7 @@ use super::{response, ClientType, MapResponse, MapResult, RustyPipeQuery, YTCont
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct QSearch<'a> {
context: YTContext,
context: YTContext<'a>,
query: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
params: Option<String>,
@ -69,11 +71,11 @@ impl RustyPipeQuery {
.http_request_txt(self.client.inner.http.get(url).build()?)
.await?;
let trimmed = response.get(5..).ok_or_else(|| {
Error::Extraction(ExtractionError::InvalidData(
"could not get string slice".into(),
))
})?;
let trimmed = response
.get(5..)
.ok_or(Error::Extraction(ExtractionError::InvalidData(
Cow::Borrowed("could not get string slice"),
)))?;
let parsed = serde_json::from_str::<(
IgnoredAny,

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use crate::{
error::{Error, ExtractionError},
model::{Paginator, VideoItem},
@ -54,7 +56,7 @@ impl MapResponse<Paginator<VideoItem>> for response::Startpage {
let mut contents = self.contents.two_column_browse_results_renderer.tabs;
let grid = contents
.try_swap_remove(0)
.ok_or_else(|| ExtractionError::InvalidData("no contents".into()))?
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no contents")))?
.tab_renderer
.content
.section_list_renderer
@ -78,7 +80,7 @@ impl MapResponse<Vec<VideoItem>> for response::Trending {
let mut contents = self.contents.two_column_browse_results_renderer.tabs;
let items = contents
.try_swap_remove(0)
.ok_or_else(|| ExtractionError::InvalidData("no contents".into()))?
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no contents")))?
.tab_renderer
.content
.section_list_renderer

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use serde::Serialize;
use crate::{
@ -12,8 +14,8 @@ use super::{response, ClientType, MapResponse, RustyPipeQuery, YTContext};
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct QResolveUrl {
context: YTContext,
struct QResolveUrl<'a> {
context: YTContext<'a>,
url: String,
}
@ -28,7 +30,7 @@ impl RustyPipeQuery {
});
let mut path_split = url
.path_segments()
.ok_or_else(|| Error::Other("invalid url: empty path".into()))?;
.ok_or(Error::Other(Cow::Borrowed("invalid url: empty path")))?;
let get_start_time = || {
params
@ -41,7 +43,7 @@ impl RustyPipeQuery {
Some("watch") => {
let id = params
.get("v")
.ok_or_else(|| Error::Other("invalid url: no video id".into()))?
.ok_or(Error::Other(Cow::Borrowed("invalid url: no video id")))?
.to_string();
Ok(UrlTarget::Video {
@ -56,7 +58,7 @@ impl RustyPipeQuery {
Some("playlist") => {
let id = params
.get("list")
.ok_or_else(|| Error::Other("invalid url: no playlist id".into()))?
.ok_or(Error::Other(Cow::Borrowed("invalid url: no playlist id")))?
.to_string();
Ok(UrlTarget::Playlist { id })
@ -190,14 +192,16 @@ impl MapResponse<UrlTarget> for response::ResolvedUrl {
let page_type = self
.endpoint
.command_metadata
.ok_or_else(|| ExtractionError::InvalidData("No command metadata".into()))?
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"No command metadata",
)))?
.web_command_metadata
.web_page_type;
let id = self
.endpoint
.browse_endpoint
.ok_or_else(|| ExtractionError::InvalidData("No browse ID".into()))?
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("No browse ID")))?
.browse_id;
Ok(MapResult {

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use serde::Serialize;
use crate::{
@ -16,7 +18,7 @@ use super::{
#[derive(Debug, Serialize)]
struct QVideo<'a> {
context: YTContext,
context: YTContext<'a>,
/// YouTube video ID
video_id: &'a str,
/// Set to true to allow extraction of streams with sensitive content
@ -79,10 +81,14 @@ impl MapResponse<VideoDetails> for response::VideoDetails {
let contents = self
.contents
.ok_or_else(|| ExtractionError::ContentUnavailable("Video not found".into()))?;
let current_video_endpoint = self
.current_video_endpoint
.ok_or_else(|| ExtractionError::ContentUnavailable("Video not found".into()))?;
.ok_or(ExtractionError::ContentUnavailable(Cow::Borrowed(
"Video not found",
)))?;
let current_video_endpoint =
self.current_video_endpoint
.ok_or(ExtractionError::ContentUnavailable(Cow::Borrowed(
"Video not found",
)))?;
let video_id = current_video_endpoint.watch_endpoint.video_id;
if id != video_id {