Skip to content

Commit e0b3d8d

Browse files
committed
10.08.2024
* Custom video response error handler on request
1 parent d4798aa commit e0b3d8d

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/info.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::{
2424
utils::{
2525
between, check_experiments, choose_format, clean_video_details, get_functions, get_html,
2626
get_html5player, get_random_v6_ip, get_video_id, get_ytconfig, is_age_restricted_from_html,
27-
is_not_yet_broadcasted, is_play_error, is_private_video, is_rental,
28-
parse_live_video_formats, parse_video_formats, sort_formats,
27+
is_not_yet_broadcasted, is_play_error, is_player_response_error, is_private_video,
28+
is_rental, parse_live_video_formats, parse_video_formats, sort_formats,
2929
},
3030
};
3131

@@ -171,6 +171,10 @@ impl Video {
171171
return Err(VideoError::VideoNotFound);
172172
}
173173

174+
if let Some(reason) = is_player_response_error(&player_response, &["not a bot"]) {
175+
return Err(VideoError::VideoPlayerResponseError(reason));
176+
}
177+
174178
let is_age_restricted = is_age_restricted_from_html(&player_response, &response);
175179

176180
if is_private_video(&player_response) && !is_age_restricted {

src/structs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ pub enum VideoError {
248248
/// Video is private
249249
#[error("Video is private")]
250250
VideoIsPrivate,
251+
/// Video player response errors
252+
#[error("Player Response Error: {0}")]
253+
VideoPlayerResponseError(String),
251254
/// Reqwest error
252255
#[error(transparent)]
253256
Reqwest(#[from] reqwest::Error),
@@ -995,6 +998,7 @@ pub struct StreamingDataFormatColorInfo {
995998
#[derive(Clone, Debug, Serialize, Deserialize)]
996999
pub struct PlayabilityStatus {
9971000
pub status: Option<String>,
1001+
pub reason: Option<String>,
9981002
#[serde(rename = "errorScreen")]
9991003
pub error_screen: Option<ErrorScreen>,
10001004
}

src/utils.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,18 +938,36 @@ pub fn is_not_yet_broadcasted(player_response: &PlayerResponse) -> bool {
938938

939939
#[cfg_attr(feature = "performance_analysis", flamer::flame)]
940940
pub fn is_play_error(player_response: &PlayerResponse, statuses: Vec<&str>) -> bool {
941-
let playability = player_response
941+
let playability_status = player_response
942942
.playability_status
943943
.as_ref()
944944
.and_then(|x| x.status.clone());
945945

946-
if let Some(playability_some) = playability {
946+
if let Some(playability_some) = playability_status {
947947
return statuses.contains(&playability_some.as_str());
948948
}
949949

950950
false
951951
}
952952

953+
#[cfg_attr(feature = "performance_analysis", flamer::flame)]
954+
pub fn is_player_response_error(
955+
player_response: &PlayerResponse,
956+
reasons: &[&str],
957+
) -> Option<String> {
958+
if let Some(reason) = player_response
959+
.playability_status
960+
.as_ref()
961+
.and_then(|status| status.reason.as_deref())
962+
{
963+
if reasons.contains(&reason) {
964+
return Some(reason.to_string());
965+
}
966+
}
967+
968+
None
969+
}
970+
953971
#[cfg_attr(feature = "performance_analysis", flamer::flame)]
954972
pub fn is_private_video(player_response: &PlayerResponse) -> bool {
955973
player_response

0 commit comments

Comments
 (0)