From 2b2b4af0b26cdd0d4bf2218d3f527abd88658abf Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Mon, 6 Jan 2025 02:09:15 +0100 Subject: [PATCH] fix: only use auth-enabled clients for fetching player with auth option enabled --- src/client/mod.rs | 25 ++++++++++++++++--------- src/client/player.rs | 10 ++++++++-- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 0f41bca..f439d48 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -1712,14 +1712,14 @@ impl RustyPipeQuery { } } - /// Return the first client type from the given list which has login credentials available. - /// - /// Returns [`None`] if authentication has been disabled or there are no available client types. - pub fn auth_enabled_client(&self, clients: &[ClientType]) -> Option { - if self.opts.auth == Some(false) { - return None; - } - let (has_cookie, has_token) = { + /// Filter the given list of client types and iterate over those which have login credentials available. + pub fn auth_enabled_clients<'a>( + &self, + clients: &'a [ClientType], + ) -> impl Iterator + 'a { + let (has_cookie, has_token) = if self.opts.auth == Some(false) { + (false, false) + } else { let auth_cookie = self.client.inner.cache.auth_cookie.read().unwrap(); let oauth_token = self.client.inner.cache.oauth_token.read().unwrap(); (auth_cookie.is_some(), oauth_token.is_some()) @@ -1727,7 +1727,7 @@ impl RustyPipeQuery { clients .iter() - .find(|c| { + .filter(move |c| { if c.is_web() { has_cookie } else if **c == ClientType::Tv { @@ -1739,6 +1739,13 @@ impl RustyPipeQuery { .copied() } + /// Return the first client type from the given list which has login credentials available. + /// + /// Returns [`None`] if authentication has been disabled or there are no available client types. + pub fn auth_enabled_client(&self, clients: &[ClientType]) -> Option { + self.auth_enabled_clients(clients).next() + } + /// Create a new context object, which is included in every request to /// the YouTube API and contains language, country and device parameters. /// diff --git a/src/client/player.rs b/src/client/player.rs index 0434a15..f74224f 100644 --- a/src/client/player.rs +++ b/src/client/player.rs @@ -79,8 +79,14 @@ impl RustyPipeQuery { let video_id = video_id.as_ref(); let mut last_e = Error::Other("no clients".into()); - for client in clients { - let res = self.player_from_client(video_id, *client).await; + let clients_iter: Box> = if self.opts.auth == Some(true) { + Box::new(self.auth_enabled_clients(clients)) + } else { + Box::new(clients.iter().cloned()) + }; + + for client in clients_iter { + let res = self.player_from_client(video_id, client).await; match res { Ok(res) => return Ok(res), Err(Error::Extraction(e)) => {