Skip to content

Commit

Permalink
10.08.2024
Browse files Browse the repository at this point in the history
* Custom video response error handler on request
  • Loading branch information
Mithronn committed Aug 10, 2024
1 parent d4798aa commit e0b3d8d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -995,6 +998,7 @@ pub struct StreamingDataFormatColorInfo {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PlayabilityStatus {
pub status: Option<String>,
pub reason: Option<String>,
#[serde(rename = "errorScreen")]
pub error_screen: Option<ErrorScreen>,
}
Expand Down
22 changes: 20 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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
Expand Down

0 comments on commit e0b3d8d

Please sign in to comment.