add desktop music client
This commit is contained in:
parent
15e854b341
commit
26da4fad2c
4 changed files with 263 additions and 76 deletions
85
src/cache.rs
85
src/cache.rs
|
|
@ -20,7 +20,8 @@ pub struct Cache {
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||||
struct CacheData {
|
struct CacheData {
|
||||||
desktop_client: Option<CacheEntry<DesktopClientData>>,
|
desktop_client: Option<CacheEntry<ClientData>>,
|
||||||
|
music_client: Option<CacheEntry<ClientData>>,
|
||||||
deobf: Option<CacheEntry<DeobfData>>,
|
deobf: Option<CacheEntry<DeobfData>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,8 +41,8 @@ impl<T> From<T> for CacheEntry<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
pub struct DesktopClientData {
|
pub struct ClientData {
|
||||||
pub client_version: String,
|
pub version: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
|
@ -53,9 +54,9 @@ pub struct DeobfData {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cache {
|
impl Cache {
|
||||||
pub async fn get_desktop_client_data<F>(&self, updater: F) -> Result<DesktopClientData>
|
pub async fn get_desktop_client_data<F>(&self, updater: F) -> Result<ClientData>
|
||||||
where
|
where
|
||||||
F: Future<Output = Result<DesktopClientData>> + Send + 'static,
|
F: Future<Output = Result<ClientData>> + Send + 'static,
|
||||||
{
|
{
|
||||||
let mut cache = self.data.lock().await;
|
let mut cache = self.data.lock().await;
|
||||||
|
|
||||||
|
|
@ -71,6 +72,24 @@ impl Cache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_music_client_data<F>(&self, updater: F) -> Result<ClientData>
|
||||||
|
where
|
||||||
|
F: Future<Output = Result<ClientData>> + Send + 'static,
|
||||||
|
{
|
||||||
|
let mut cache = self.data.lock().await;
|
||||||
|
|
||||||
|
if cache.music_client.is_none()
|
||||||
|
|| cache.music_client.as_ref().unwrap().last_update < Utc::now() - Duration::hours(24)
|
||||||
|
{
|
||||||
|
let cdata = updater.await?;
|
||||||
|
cache.music_client = Some(CacheEntry::from(cdata.clone()));
|
||||||
|
self.save(&cache);
|
||||||
|
Ok(cdata)
|
||||||
|
} else {
|
||||||
|
Ok(cache.music_client.as_ref().unwrap().data.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_deobf_data<F>(&self, updater: F) -> Result<DeobfData>
|
pub async fn get_deobf_data<F>(&self, updater: F) -> Result<DeobfData>
|
||||||
where
|
where
|
||||||
F: Future<Output = Result<DeobfData>> + Send + 'static,
|
F: Future<Output = Result<DeobfData>> + Send + 'static,
|
||||||
|
|
@ -179,19 +198,35 @@ mod tests {
|
||||||
async fn test() {
|
async fn test() {
|
||||||
let cache = Cache::default();
|
let cache = Cache::default();
|
||||||
|
|
||||||
let cdata = cache
|
let desktop_c = cache
|
||||||
.get_desktop_client_data(async {
|
.get_desktop_client_data(async {
|
||||||
Ok(DesktopClientData {
|
Ok(ClientData {
|
||||||
client_version: "1.2.3".to_owned(),
|
version: "1.2.3".to_owned(),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
cdata,
|
desktop_c,
|
||||||
DesktopClientData {
|
ClientData {
|
||||||
client_version: "1.2.3".to_owned()
|
version: "1.2.3".to_owned()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
let music_c = cache
|
||||||
|
.get_music_client_data(async {
|
||||||
|
Ok(ClientData {
|
||||||
|
version: "4.5.6".to_owned(),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
music_c,
|
||||||
|
ClientData {
|
||||||
|
version: "4.5.6".to_owned()
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -220,19 +255,33 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Create a new cache from the first one's json
|
||||||
|
// and check if it returns the same cached data
|
||||||
let json = cache.to_json().await.unwrap();
|
let json = cache.to_json().await.unwrap();
|
||||||
let new_cache = Cache::from_json(&json);
|
let new_cache = Cache::from_json(&json);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
new_cache
|
new_cache
|
||||||
.get_desktop_client_data(async {
|
.get_desktop_client_data(async {
|
||||||
Ok(DesktopClientData {
|
Ok(ClientData {
|
||||||
client_version: "".to_owned(),
|
version: "".to_owned(),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
cdata
|
desktop_c
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
new_cache
|
||||||
|
.get_music_client_data(async {
|
||||||
|
Ok(ClientData {
|
||||||
|
version: "".to_owned(),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
music_c
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
@ -261,8 +310,8 @@ mod tests {
|
||||||
|
|
||||||
let cdata = cache
|
let cdata = cache
|
||||||
.get_desktop_client_data(async {
|
.get_desktop_client_data(async {
|
||||||
Ok(DesktopClientData {
|
Ok(ClientData {
|
||||||
client_version: "1.2.3".to_owned(),
|
version: "1.2.3".to_owned(),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
|
|
@ -288,8 +337,8 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
new_cache
|
new_cache
|
||||||
.get_desktop_client_data(async {
|
.get_desktop_client_data(async {
|
||||||
Ok(DesktopClientData {
|
Ok(ClientData {
|
||||||
client_version: "".to_owned(),
|
version: "".to_owned(),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,10 @@ use log::warn;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use reqwest::{header, Client, ClientBuilder, Method, Request, RequestBuilder, Response};
|
use reqwest::{header, Client, ClientBuilder, Method, Request, RequestBuilder, Response};
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cache::{Cache, DesktopClientData},
|
cache::{Cache, ClientData},
|
||||||
model::Locale,
|
model::Locale,
|
||||||
util,
|
util,
|
||||||
};
|
};
|
||||||
|
|
@ -104,22 +104,34 @@ const CONSENT_COOKIE_NO: &str = "PENDING+";
|
||||||
|
|
||||||
const YOUTUBEI_V1_URL: &str = "https://www.youtube.com/youtubei/v1/";
|
const YOUTUBEI_V1_URL: &str = "https://www.youtube.com/youtubei/v1/";
|
||||||
const YOUTUBEI_V1_GAPIS_URL: &str = "https://youtubei.googleapis.com/youtubei/v1/";
|
const YOUTUBEI_V1_GAPIS_URL: &str = "https://youtubei.googleapis.com/youtubei/v1/";
|
||||||
|
const YOUTUBE_MUSIC_V1_URL: &str = "https://music.youtube.com/youtubei/v1/";
|
||||||
|
|
||||||
const DISABLE_PRETTY_PRINT_PARAMETER: &str = "&prettyPrint=false";
|
const DISABLE_PRETTY_PRINT_PARAMETER: &str = "&prettyPrint=false";
|
||||||
|
|
||||||
const DESKTOP_CLIENT_VERSION: &str = "2.20220721.05.00_1";
|
const DESKTOP_CLIENT_VERSION: &str = "2.20220801.00.00";
|
||||||
const DESKTOP_API_KEY: &str = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
|
const DESKTOP_API_KEY: &str = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
|
||||||
const TVHTML5_CLIENT_VERSION: &str = "2.0";
|
const TVHTML5_CLIENT_VERSION: &str = "2.0";
|
||||||
|
const DESKTOP_MUSIC_API_KEY: &str = "AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30";
|
||||||
|
const DESKTOP_MUSIC_CLIENT_VERSION: &str = "1.20220727.01.00";
|
||||||
|
|
||||||
const MOBILE_CLIENT_VERSION: &str = "17.10.35";
|
const MOBILE_CLIENT_VERSION: &str = "17.10.35";
|
||||||
const ANDROID_API_KEY: &str = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w";
|
const ANDROID_API_KEY: &str = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w";
|
||||||
const IOS_API_KEY: &str = "AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc";
|
const IOS_API_KEY: &str = "AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc";
|
||||||
const IOS_DEVICE_MODEL: &str = "iPhone14,5";
|
const IOS_DEVICE_MODEL: &str = "iPhone14,5";
|
||||||
|
|
||||||
|
const CLIENT_VERSION_REGEXES: Lazy<[Regex; 3]> = Lazy::new(|| {
|
||||||
|
[
|
||||||
|
Regex::new("INNERTUBE_CONTEXT_CLIENT_VERSION\":\"([0-9\\.]+?)\"").unwrap(),
|
||||||
|
Regex::new("innertube_context_client_version\":\"([0-9\\.]+?)\"").unwrap(),
|
||||||
|
Regex::new("client.version=([0-9\\.]+)").unwrap(),
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
pub struct RustyTube {
|
pub struct RustyTube {
|
||||||
pub locale: Arc<Locale>,
|
pub locale: Arc<Locale>,
|
||||||
cache: Cache,
|
cache: Cache,
|
||||||
desktop_client: Arc<DesktopClient>,
|
desktop_client: Arc<DesktopClient>,
|
||||||
|
desktop_music_client: Arc<DesktopMusicClient>,
|
||||||
android_client: Arc<AndroidClient>,
|
android_client: Arc<AndroidClient>,
|
||||||
ios_client: Arc<IosClient>,
|
ios_client: Arc<IosClient>,
|
||||||
tvhtml5embed_client: Arc<TvHtml5EmbedClient>,
|
tvhtml5embed_client: Arc<TvHtml5EmbedClient>,
|
||||||
|
|
@ -146,7 +158,8 @@ impl RustyTube {
|
||||||
Self {
|
Self {
|
||||||
locale: locale.clone(),
|
locale: locale.clone(),
|
||||||
cache: cache.clone(),
|
cache: cache.clone(),
|
||||||
desktop_client: Arc::new(DesktopClient::new(locale.clone(), cache)),
|
desktop_client: Arc::new(DesktopClient::new(locale.clone(), cache.clone())),
|
||||||
|
desktop_music_client: Arc::new(DesktopMusicClient::new(locale.clone(), cache)),
|
||||||
android_client: Arc::new(AndroidClient::new(locale.clone())),
|
android_client: Arc::new(AndroidClient::new(locale.clone())),
|
||||||
ios_client: Arc::new(IosClient::new(locale.clone())),
|
ios_client: Arc::new(IosClient::new(locale.clone())),
|
||||||
tvhtml5embed_client: Arc::new(TvHtml5EmbedClient::new(locale)),
|
tvhtml5embed_client: Arc::new(TvHtml5EmbedClient::new(locale)),
|
||||||
|
|
@ -156,7 +169,7 @@ impl RustyTube {
|
||||||
pub fn get_ytclient(&self, client_type: ClientType) -> Arc<dyn YTClient> {
|
pub fn get_ytclient(&self, client_type: ClientType) -> Arc<dyn YTClient> {
|
||||||
match client_type {
|
match client_type {
|
||||||
ClientType::Desktop => self.desktop_client.clone(),
|
ClientType::Desktop => self.desktop_client.clone(),
|
||||||
ClientType::DesktopMusic => todo!(),
|
ClientType::DesktopMusic => self.desktop_music_client.clone(),
|
||||||
ClientType::TvHtml5Embed => self.tvhtml5embed_client.clone(),
|
ClientType::TvHtml5Embed => self.tvhtml5embed_client.clone(),
|
||||||
ClientType::Android => self.android_client.clone(),
|
ClientType::Android => self.android_client.clone(),
|
||||||
ClientType::Ios => self.ios_client.clone(),
|
ClientType::Ios => self.ios_client.clone(),
|
||||||
|
|
@ -198,7 +211,7 @@ impl YTClient for DesktopClient {
|
||||||
client_screen: None,
|
client_screen: None,
|
||||||
device_model: None,
|
device_model: None,
|
||||||
platform: "DESKTOP".to_owned(),
|
platform: "DESKTOP".to_owned(),
|
||||||
original_url: Some("https://www.youtube.com".to_owned()),
|
original_url: Some("https://www.youtube.com/".to_owned()),
|
||||||
hl: match localized {
|
hl: match localized {
|
||||||
true => self.locale.lang.to_owned(),
|
true => self.locale.lang.to_owned(),
|
||||||
false => "en".to_owned(),
|
false => "en".to_owned(),
|
||||||
|
|
@ -234,7 +247,7 @@ impl YTClient for DesktopClient {
|
||||||
self.http.clone()
|
self.http.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_type(&self)-> ClientType {
|
fn get_type(&self) -> ClientType {
|
||||||
ClientType::Desktop
|
ClientType::Desktop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -285,15 +298,7 @@ impl DesktopClient {
|
||||||
.await
|
.await
|
||||||
.context("Failed to download sw.js")?;
|
.context("Failed to download sw.js")?;
|
||||||
|
|
||||||
static CLIENT_VERSION_PATTERNS: Lazy<[Regex; 3]> = Lazy::new(|| {
|
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &swjs, 1)
|
||||||
[
|
|
||||||
Regex::new("INNERTUBE_CONTEXT_CLIENT_VERSION\":\"([0-9\\.]+?)\"").unwrap(),
|
|
||||||
Regex::new("innertube_context_client_version\":\"([0-9\\.]+?)\"").unwrap(),
|
|
||||||
Regex::new("client.version=([0-9\\.]+)").unwrap(),
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
util::get_cg_from_regexes(CLIENT_VERSION_PATTERNS.iter(), &swjs, 1)
|
|
||||||
.ok_or(anyhow!("Could not find desktop client version in sw.js"))
|
.ok_or(anyhow!("Could not find desktop client version in sw.js"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -306,12 +311,14 @@ impl DesktopClient {
|
||||||
.get_desktop_client_data(async move {
|
.get_desktop_client_data(async move {
|
||||||
let client_version =
|
let client_version =
|
||||||
Self::extract_client_version_from_swjs(http, &consent_cookie).await?;
|
Self::extract_client_version_from_swjs(http, &consent_cookie).await?;
|
||||||
Ok(DesktopClientData { client_version })
|
Ok(ClientData {
|
||||||
|
version: client_version,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match client_data {
|
match client_data {
|
||||||
Ok(client_data) => client_data.client_version,
|
Ok(client_data) => client_data.version,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("{}", e);
|
warn!("{}", e);
|
||||||
DESKTOP_CLIENT_VERSION.to_owned()
|
DESKTOP_CLIENT_VERSION.to_owned()
|
||||||
|
|
@ -370,7 +377,7 @@ impl YTClient for AndroidClient {
|
||||||
self.http.clone()
|
self.http.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_type(&self)-> ClientType {
|
fn get_type(&self) -> ClientType {
|
||||||
ClientType::Android
|
ClientType::Android
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -428,10 +435,7 @@ impl YTClient for IosClient {
|
||||||
method,
|
method,
|
||||||
format!(
|
format!(
|
||||||
"{}{}?key={}{}",
|
"{}{}?key={}{}",
|
||||||
YOUTUBEI_V1_GAPIS_URL,
|
YOUTUBEI_V1_GAPIS_URL, endpoint, IOS_API_KEY, DISABLE_PRETTY_PRINT_PARAMETER
|
||||||
endpoint,
|
|
||||||
IOS_API_KEY,
|
|
||||||
DISABLE_PRETTY_PRINT_PARAMETER
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.header("X-Goog-Api-Format-Version", "2")
|
.header("X-Goog-Api-Format-Version", "2")
|
||||||
|
|
@ -441,7 +445,7 @@ impl YTClient for IosClient {
|
||||||
self.http.clone()
|
self.http.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_type(&self)-> ClientType {
|
fn get_type(&self) -> ClientType {
|
||||||
ClientType::Ios
|
ClientType::Ios
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -515,7 +519,7 @@ impl YTClient for TvHtml5EmbedClient {
|
||||||
self.http.clone()
|
self.http.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_type(&self)-> ClientType {
|
fn get_type(&self) -> ClientType {
|
||||||
ClientType::TvHtml5Embed
|
ClientType::TvHtml5Embed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -533,20 +537,163 @@ impl TvHtml5EmbedClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct DesktopMusicClient {
|
||||||
|
locale: Arc<Locale>,
|
||||||
|
http: Client,
|
||||||
|
cache: Cache,
|
||||||
|
consent_cookie_yes: String,
|
||||||
|
consent_cookie_no: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl YTClient for DesktopMusicClient {
|
||||||
|
async fn get_context(&self, localized: bool) -> ContextYT {
|
||||||
|
ContextYT {
|
||||||
|
client: ClientInfo {
|
||||||
|
client_name: "WEB_REMIX".to_owned(),
|
||||||
|
client_version: self.get_client_version().await,
|
||||||
|
client_screen: None,
|
||||||
|
device_model: None,
|
||||||
|
platform: "DESKTOP".to_owned(),
|
||||||
|
original_url: Some("https://music.youtube.com/".to_owned()),
|
||||||
|
hl: match localized {
|
||||||
|
true => self.locale.lang.to_owned(),
|
||||||
|
false => "en".to_owned(),
|
||||||
|
},
|
||||||
|
gl: match localized {
|
||||||
|
true => self.locale.country.to_owned(),
|
||||||
|
false => "US".to_owned(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
request: Some(RequestYT::default()),
|
||||||
|
user: User::default(),
|
||||||
|
third_party: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn request_builder(&self, method: Method, endpoint: &str) -> RequestBuilder {
|
||||||
|
self.http
|
||||||
|
.request(
|
||||||
|
method,
|
||||||
|
format!(
|
||||||
|
"{}{}?key={}{}",
|
||||||
|
YOUTUBE_MUSIC_V1_URL, endpoint, DESKTOP_MUSIC_API_KEY, DISABLE_PRETTY_PRINT_PARAMETER
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.header(header::ORIGIN, "https://music.youtube.com")
|
||||||
|
.header(header::REFERER, "https://music.youtube.com")
|
||||||
|
.header(header::COOKIE, self.consent_cookie_no.to_owned())
|
||||||
|
.header("X-YouTube-Client-Name", "67")
|
||||||
|
.header("X-YouTube-Client-Version", self.get_client_version().await)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn http_client(&self) -> Client {
|
||||||
|
self.http.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_type(&self) -> ClientType {
|
||||||
|
ClientType::DesktopMusic
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DesktopMusicClient {
|
||||||
|
fn new(locale: Arc<Locale>, cache: Cache) -> Self {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let http = ClientBuilder::new()
|
||||||
|
.user_agent(DEFAULT_UA)
|
||||||
|
.gzip(true)
|
||||||
|
.brotli(true)
|
||||||
|
.build()
|
||||||
|
.expect("unable to build the HTTP client");
|
||||||
|
|
||||||
|
Self {
|
||||||
|
locale,
|
||||||
|
http,
|
||||||
|
cache,
|
||||||
|
consent_cookie_yes: format!(
|
||||||
|
"{}={}{}",
|
||||||
|
CONSENT_COOKIE,
|
||||||
|
CONSENT_COOKIE_YES,
|
||||||
|
rng.gen_range(100..1000)
|
||||||
|
),
|
||||||
|
consent_cookie_no: format!(
|
||||||
|
"{}={}{}",
|
||||||
|
CONSENT_COOKIE,
|
||||||
|
CONSENT_COOKIE_NO,
|
||||||
|
rng.gen_range(100..1000)
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn extract_client_version_from_swjs(
|
||||||
|
http: Client,
|
||||||
|
consent_cookie: &str,
|
||||||
|
) -> Result<String> {
|
||||||
|
let swjs = exec_request_text(
|
||||||
|
http.clone(),
|
||||||
|
http.get("https://music.youtube.com/sw.js")
|
||||||
|
.header(header::ORIGIN, "https://www.youtube.com")
|
||||||
|
.header(header::REFERER, "https://www.youtube.com")
|
||||||
|
.header(header::COOKIE, consent_cookie)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.context("Failed to download sw.js")?;
|
||||||
|
|
||||||
|
util::get_cg_from_regexes(CLIENT_VERSION_REGEXES.iter(), &swjs, 1)
|
||||||
|
.ok_or(anyhow!("Could not find music client version in sw.js"))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_client_version(&self) -> String {
|
||||||
|
let http = self.http.clone();
|
||||||
|
let consent_cookie = self.consent_cookie_yes.clone();
|
||||||
|
|
||||||
|
let client_data = self
|
||||||
|
.cache
|
||||||
|
.get_music_client_data(async move {
|
||||||
|
let client_version =
|
||||||
|
Self::extract_client_version_from_swjs(http, &consent_cookie).await?;
|
||||||
|
Ok(ClientData {
|
||||||
|
version: client_version,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match client_data {
|
||||||
|
Ok(client_data) => client_data.version,
|
||||||
|
Err(e) => {
|
||||||
|
warn!("{}", e);
|
||||||
|
DESKTOP_MUSIC_CLIENT_VERSION.to_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use test_log::test;
|
use test_log::test;
|
||||||
|
|
||||||
/*
|
static CLIENT_VERSION_REGEX: Lazy<Regex> =
|
||||||
|
Lazy::new(|| Regex::new(r#"^\d+\.\d{8}\.\d{2}\.\d{2}"#).unwrap());
|
||||||
|
|
||||||
#[test(tokio::test)]
|
#[test(tokio::test)]
|
||||||
async fn t_extract_client_version_from_swjs() {
|
async fn t_extract_desktop_client_version() {
|
||||||
let rt = RustyTube::new();
|
let rt = RustyTube::new();
|
||||||
let version = rt.extract_client_version_from_swjs().await.unwrap();
|
let client = rt.desktop_client;
|
||||||
|
let version = DesktopClient::extract_client_version_from_swjs(
|
||||||
|
client.http.clone(),
|
||||||
|
&client.consent_cookie_yes,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let version = version.unwrap();
|
assert!(CLIENT_VERSION_REGEX.is_match(&version).unwrap());
|
||||||
|
|
||||||
// Client version changes often, notify during test so the hardcoded version can be updated
|
// Client version changes often,
|
||||||
|
// notify during test so the hardcoded version can be updated
|
||||||
if version != DESKTOP_CLIENT_VERSION {
|
if version != DESKTOP_CLIENT_VERSION {
|
||||||
println!(
|
println!(
|
||||||
"INFO: YT Desktop Client was updated, new version: {}",
|
"INFO: YT Desktop Client was updated, new version: {}",
|
||||||
|
|
@ -556,34 +703,25 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test(tokio::test)]
|
#[test(tokio::test)]
|
||||||
async fn t_get_client_version() {
|
async fn t_extract_desktop_music_client_version() {
|
||||||
error!("Checking whether it still works...");
|
|
||||||
let rt = RustyTube::new();
|
let rt = RustyTube::new();
|
||||||
let client_version = rt.get_client_version().await;
|
let client = rt.desktop_music_client;
|
||||||
assert!(client_version.len() > 10);
|
let version = DesktopMusicClient::extract_client_version_from_swjs(
|
||||||
}
|
client.http.clone(),
|
||||||
|
&client.consent_cookie_yes,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
#[test]
|
assert!(CLIENT_VERSION_REGEX.is_match(&version).unwrap());
|
||||||
fn json_test() {
|
|
||||||
let request = BaseRequest {
|
|
||||||
context: ContextYT {
|
|
||||||
client: ClientInfo {
|
|
||||||
client_name: "WEB".to_owned(),
|
|
||||||
client_version: "x".to_owned(),
|
|
||||||
client_screen: None,
|
|
||||||
platform: "DESKTOP".to_owned(),
|
|
||||||
original_url: Some("https://www.youtube.com".to_owned()),
|
|
||||||
hl: "de".to_owned(),
|
|
||||||
gl: "DE".to_owned(),
|
|
||||||
},
|
|
||||||
request: Some(RequestYT::default()),
|
|
||||||
user: User::default(),
|
|
||||||
third_party: None,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
let request_str = serde_json::to_string_pretty(&request).unwrap();
|
// Client version changes often,
|
||||||
println!("{}", request_str);
|
// notify during test so the hardcoded version can be updated
|
||||||
|
if version != DESKTOP_MUSIC_CLIENT_VERSION {
|
||||||
|
println!(
|
||||||
|
"INFO: YT Desktop Music Client was updated, new version: {}",
|
||||||
|
version
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -393,7 +393,7 @@ fn map_player_data(response: Player, deobf: &Deobfuscator) -> Result<PlayerData>
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{any::Any, fs, io::Cursor, path::Path};
|
use std::{fs, io::Cursor, path::Path};
|
||||||
|
|
||||||
use crate::cache::DeobfData;
|
use crate::cache::DeobfData;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#[allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
|
|
|
||||||
Reference in a new issue