From e0b3d8d182be7e7fb11e02619028ecbdacd7cbf4 Mon Sep 17 00:00:00 2001 From: Mithronn Date: Sat, 10 Aug 2024 21:56:05 +0300 Subject: [PATCH] 10.08.2024 * Custom video response error handler on request --- src/info.rs | 8 ++++++-- src/structs.rs | 4 ++++ src/utils.rs | 22 ++++++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/info.rs b/src/info.rs index 76ad41e..b00710a 100644 --- a/src/info.rs +++ b/src/info.rs @@ -24,8 +24,8 @@ use crate::{ utils::{ between, check_experiments, choose_format, clean_video_details, get_functions, get_html, get_html5player, get_random_v6_ip, get_video_id, get_ytconfig, is_age_restricted_from_html, - is_not_yet_broadcasted, is_play_error, is_private_video, is_rental, - parse_live_video_formats, parse_video_formats, sort_formats, + is_not_yet_broadcasted, is_play_error, is_player_response_error, is_private_video, + is_rental, parse_live_video_formats, parse_video_formats, sort_formats, }, }; @@ -171,6 +171,10 @@ impl Video { return Err(VideoError::VideoNotFound); } + if let Some(reason) = is_player_response_error(&player_response, &["not a bot"]) { + return Err(VideoError::VideoPlayerResponseError(reason)); + } + let is_age_restricted = is_age_restricted_from_html(&player_response, &response); if is_private_video(&player_response) && !is_age_restricted { diff --git a/src/structs.rs b/src/structs.rs index 5c33087..a50ac2f 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -248,6 +248,9 @@ pub enum VideoError { /// Video is private #[error("Video is private")] VideoIsPrivate, + /// Video player response errors + #[error("Player Response Error: {0}")] + VideoPlayerResponseError(String), /// Reqwest error #[error(transparent)] Reqwest(#[from] reqwest::Error), @@ -995,6 +998,7 @@ pub struct StreamingDataFormatColorInfo { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct PlayabilityStatus { pub status: Option, + pub reason: Option, #[serde(rename = "errorScreen")] pub error_screen: Option, } diff --git a/src/utils.rs b/src/utils.rs index 3db6bb4..b4dff04 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -938,18 +938,36 @@ pub fn is_not_yet_broadcasted(player_response: &PlayerResponse) -> bool { #[cfg_attr(feature = "performance_analysis", flamer::flame)] pub fn is_play_error(player_response: &PlayerResponse, statuses: Vec<&str>) -> bool { - let playability = player_response + let playability_status = player_response .playability_status .as_ref() .and_then(|x| x.status.clone()); - if let Some(playability_some) = playability { + if let Some(playability_some) = playability_status { return statuses.contains(&playability_some.as_str()); } false } +#[cfg_attr(feature = "performance_analysis", flamer::flame)] +pub fn is_player_response_error( + player_response: &PlayerResponse, + reasons: &[&str], +) -> Option { + if let Some(reason) = player_response + .playability_status + .as_ref() + .and_then(|status| status.reason.as_deref()) + { + if reasons.contains(&reason) { + return Some(reason.to_string()); + } + } + + None +} + #[cfg_attr(feature = "performance_analysis", flamer::flame)] pub fn is_private_video(player_response: &PlayerResponse) -> bool { player_response