fix: player_from_clients: fall back to TvHtml5Embed client

This commit is contained in:
ThetaDev 2024-08-17 00:21:47 +02:00
parent 8692ca81d9
commit d0ae7961ba
No known key found for this signature in database
GPG key ID: E319D3C5148D65B6
2 changed files with 14 additions and 12 deletions

View file

@ -220,12 +220,8 @@ static VISITOR_DATA_REGEX: Lazy<Regex> =
/// ///
/// The order may change in the future in case YouTube applies changes to their /// The order may change in the future in case YouTube applies changes to their
/// platform that disable a client or make it less reliable. /// platform that disable a client or make it less reliable.
pub const DEFAULT_PLAYER_CLIENT_ORDER: &[ClientType] = &[ pub const DEFAULT_PLAYER_CLIENT_ORDER: &[ClientType] =
ClientType::Tv, &[ClientType::Tv, ClientType::Android, ClientType::Ios];
ClientType::TvHtml5Embed,
ClientType::Android,
ClientType::Ios,
];
/// The RustyPipe client used to access YouTube's API /// The RustyPipe client used to access YouTube's API
/// ///

View file

@ -74,6 +74,9 @@ impl RustyPipeQuery {
/// ///
/// The clients are used in the given order. If a client cannot fetch the requested video, /// The clients are used in the given order. If a client cannot fetch the requested video,
/// an attempt is made with the next one. /// an attempt is made with the next one.
///
/// If an age-restricted video is detected, it will automatically use the [`ClientType::TvHtml5Embed`]
/// since it is the only one that can circumvent age restrictions.
pub async fn player_from_clients<S: AsRef<str> + Debug>( pub async fn player_from_clients<S: AsRef<str> + Debug>(
&self, &self,
video_id: S, video_id: S,
@ -81,9 +84,6 @@ impl RustyPipeQuery {
) -> Result<VideoPlayer, Error> { ) -> Result<VideoPlayer, Error> {
let video_id = video_id.as_ref(); let video_id = video_id.as_ref();
let mut last_e = Error::Other("no clients".into()); let mut last_e = Error::Other("no clients".into());
// Prefer to output age restriction error (e.g. if video cannot be played
// by Desktop because of age restriction and by TvHtml5Embed because it is non-embeddable)
let mut age_restricted_e = None;
for client in clients { for client in clients {
let res = self.player_from_client(video_id, *client).await; let res = self.player_from_client(video_id, *client).await;
@ -96,11 +96,17 @@ impl RustyPipeQuery {
msg, msg,
} = &e } = &e
{ {
age_restricted_e = if let Ok(res) = self
Some(Error::Extraction(ExtractionError::Unavailable { .player_from_client(video_id, ClientType::TvHtml5Embed)
.await
{
return Ok(res);
} else {
return Err(Error::Extraction(ExtractionError::Unavailable {
reason: UnavailabilityReason::AgeRestricted, reason: UnavailabilityReason::AgeRestricted,
msg: msg.to_owned(), msg: msg.to_owned(),
})); }));
}
} }
last_e = Error::Extraction(e); last_e = Error::Extraction(e);
} else { } else {
@ -110,7 +116,7 @@ impl RustyPipeQuery {
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }
Err(age_restricted_e.unwrap_or(last_e)) Err(last_e)
} }
/// Get YouTube player data (video/audio streams + basic metadata) using the specified client /// Get YouTube player data (video/audio streams + basic metadata) using the specified client