use std::{ fs::File, ops::Sub, path::{Path, PathBuf}, sync::Mutex, }; use rustypipe::{ client::{ClientType, RustyPipe}, param::{ search_filter::{self, ItemType, SearchFilter}, Country, }, report::{Report, Reporter}, }; pub async fn download_testfiles(project_root: &Path) { let mut testfiles = project_root.to_path_buf(); testfiles.push("testfiles"); player(&testfiles).await; player_model(&testfiles).await; playlist(&testfiles).await; playlist_cont(&testfiles).await; video_details(&testfiles).await; comments_top(&testfiles).await; comments_latest(&testfiles).await; recommendations(&testfiles).await; channel_videos(&testfiles).await; channel_shorts(&testfiles).await; channel_livestreams(&testfiles).await; channel_playlists(&testfiles).await; channel_info(&testfiles).await; channel_videos_cont(&testfiles).await; channel_playlists_cont(&testfiles).await; search(&testfiles).await; search_cont(&testfiles).await; search_playlists(&testfiles).await; search_empty(&testfiles).await; startpage(&testfiles).await; startpage_cont(&testfiles).await; trending(&testfiles).await; music_playlist(&testfiles).await; music_playlist_cont(&testfiles).await; music_playlist_related(&testfiles).await; music_album(&testfiles).await; music_search(&testfiles).await; music_search_tracks(&testfiles).await; music_search_albums(&testfiles).await; music_search_artists(&testfiles).await; music_search_playlists(&testfiles).await; music_search_cont(&testfiles).await; music_search_suggestion(&testfiles).await; music_artist(&testfiles).await; music_details(&testfiles).await; music_lyrics(&testfiles).await; music_related(&testfiles).await; music_radio(&testfiles).await; music_radio_cont(&testfiles).await; music_new_albums(&testfiles).await; music_new_videos(&testfiles).await; music_charts(&testfiles).await; music_genres(&testfiles).await; music_genre(&testfiles).await; } const CLIENT_TYPES: [ClientType; 5] = [ ClientType::Desktop, ClientType::DesktopMusic, ClientType::TvHtml5Embed, ClientType::Android, ClientType::Ios, ]; /// Store pretty-printed response json pub struct TestFileReporter { path: PathBuf, count: Mutex, } impl TestFileReporter { pub fn new>(path: P) -> Self { Self { path: path.as_ref().to_path_buf(), count: Mutex::new(0), } } } impl Reporter for TestFileReporter { fn report(&self, report: &Report) { if report.level != rustypipe::report::Level::DBG { println!("Error: {}", report.error.as_deref().unwrap_or_default()); return; } let mut root = self.path.clone(); root.set_file_name(""); std::fs::create_dir_all(root).unwrap(); let count = { let mut cl = self.count.lock().unwrap(); *cl += 1; cl.sub(1) }; let path = if count == 0 { self.path.clone() } else { let mut p = self.path.clone(); p.set_file_name(format!( "{}_{}.{}", p.file_stem().unwrap_or_default().to_string_lossy(), count, p.extension().unwrap_or_default().to_string_lossy() )); p }; let data = serde_json::from_str::(&report.http_request.resp_body).unwrap(); let file = File::create(&path).unwrap(); serde_json::to_writer_pretty(file, &data).unwrap(); println!("Downloaded {}", path.display()); } } fn rp_testfile(json_path: &Path) -> RustyPipe { let reporter = TestFileReporter::new(json_path); RustyPipe::builder() .reporter(Box::new(reporter)) .report() .strict() .build() } async fn player(testfiles: &Path) { let video_id = "pPvd8UxmSbQ"; for client_type in CLIENT_TYPES { let mut json_path = testfiles.to_path_buf(); json_path.push("player"); json_path.push(format!("{client_type:?}_video.json").to_lowercase()); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query() .player_from_client(video_id, client_type) .await .unwrap(); } } async fn player_model(testfiles: &Path) { let rp = RustyPipe::builder().strict().build(); for (name, id) in [("multilanguage", "tVWWp1PqDus"), ("hdr", "LXb3EKWsInQ")] { let mut json_path = testfiles.to_path_buf(); json_path.push("player_model"); json_path.push(format!("{name}.json").to_lowercase()); if json_path.exists() { continue; } let player_data = rp .query() .player_from_client(id, ClientType::Desktop) .await .unwrap(); let file = File::create(&json_path).unwrap(); serde_json::to_writer_pretty(file, &player_data).unwrap(); println!("Downloaded {}", json_path.display()); } } async fn playlist(testfiles: &Path) { for (name, id) in [ ("short", "RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk"), ("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ"), ("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("playlist"); json_path.push(format!("playlist_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().playlist(id).await.unwrap(); } } async fn playlist_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("playlist"); json_path.push("playlist_cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let playlist = rp .query() .playlist("PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ") .await .unwrap(); let rp = rp_testfile(&json_path); playlist.videos.next(rp.query()).await.unwrap().unwrap(); } async fn video_details(testfiles: &Path) { for (name, id) in [ ("music", "XuM2onMGvTI"), ("mv", "ZeerrnuLi5E"), ("ccommons", "0rb9CfOvojk"), ("chapters", "nFDBxBUfE74"), ("live", "86YLFOog4GM"), ("agegate", "HRKu0cvrr_o"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("video_details"); json_path.push(format!("video_details_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().video_details(id).await.unwrap(); } } async fn comments_top(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("video_details"); json_path.push("comments_top.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let details = rp.query().video_details("ZeerrnuLi5E").await.unwrap(); let rp = rp_testfile(&json_path); details .top_comments .next(rp.query()) .await .unwrap() .unwrap(); } async fn comments_latest(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("video_details"); json_path.push("comments_latest.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let details = rp.query().video_details("ZeerrnuLi5E").await.unwrap(); let rp = rp_testfile(&json_path); details .latest_comments .next(rp.query()) .await .unwrap() .unwrap(); } async fn recommendations(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("video_details"); json_path.push("recommendations.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let details = rp.query().video_details("ZeerrnuLi5E").await.unwrap(); let rp = rp_testfile(&json_path); details.recommended.next(rp.query()).await.unwrap(); } async fn channel_videos(testfiles: &Path) { for (name, id) in [ ("base", "UC2DjFE7Xf11URZqWBigcVOQ"), ("music", "UC_vmjW5e1xEHhYjY2a0kK1A"), // YouTube Music channels have no videos ("shorts", "UCh8gHdtzO2tXd593_bjErWg"), // shorts and livestreams are rendered differently ("live", "UChs0pSaEoNLV4mevBFGaoKA"), ("empty", "UCxBa895m48H5idw5li7h-0g"), ("upcoming", "UCcvfHa-GHSOHFAjU0-Ie57A"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("channel"); json_path.push(format!("channel_videos_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().channel_videos(id).await.unwrap(); } } async fn channel_shorts(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("channel"); json_path.push("channel_shorts.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query() .channel_shorts("UCh8gHdtzO2tXd593_bjErWg") .await .unwrap(); } async fn channel_livestreams(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("channel"); json_path.push("channel_livestreams.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query() .channel_livestreams("UC2DjFE7Xf11URZqWBigcVOQ") .await .unwrap(); } async fn channel_playlists(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("channel"); json_path.push("channel_playlists.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query() .channel_playlists("UC2DjFE7Xf11URZqWBigcVOQ") .await .unwrap(); } async fn channel_info(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("channel"); json_path.push("channel_info.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query() .channel_info("UC2DjFE7Xf11URZqWBigcVOQ") .await .unwrap(); } async fn channel_videos_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("channel"); json_path.push("channel_videos_cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let videos = rp .query() .channel_videos("UC2DjFE7Xf11URZqWBigcVOQ") .await .unwrap(); let rp = rp_testfile(&json_path); videos.content.next(rp.query()).await.unwrap().unwrap(); } async fn channel_playlists_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("channel"); json_path.push("channel_playlists_cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let playlists = rp .query() .channel_playlists("UC2DjFE7Xf11URZqWBigcVOQ") .await .unwrap(); let rp = rp_testfile(&json_path); playlists.content.next(rp.query()).await.unwrap().unwrap(); } async fn search(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("search"); json_path.push("default.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query().search("doobydoobap").await.unwrap(); } async fn search_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("search"); json_path.push("cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let search = rp.query().search("doobydoobap").await.unwrap(); let rp = rp_testfile(&json_path); search.items.next(rp.query()).await.unwrap().unwrap(); } async fn search_playlists(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("search"); json_path.push("playlists.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query() .search_filter("pop", &SearchFilter::new().item_type(ItemType::Playlist)) .await .unwrap(); } async fn search_empty(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("search"); json_path.push("empty.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query() .search_filter( "test", &SearchFilter::new() .feature(search_filter::Feature::IsLive) .feature(search_filter::Feature::Is3d), ) .await .unwrap(); } async fn startpage(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("trends"); json_path.push("startpage.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query().startpage().await.unwrap(); } async fn startpage_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("trends"); json_path.push("startpage_cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let startpage = rp.query().startpage().await.unwrap(); let rp = rp_testfile(&json_path); startpage.next(rp.query()).await.unwrap(); } async fn trending(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("trends"); json_path.push("trending.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query().trending().await.unwrap(); } async fn music_playlist(testfiles: &Path) { for (name, id) in [ ("short", "RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk"), ("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ"), ("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_playlist"); json_path.push(format!("playlist_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_playlist(id).await.unwrap(); } } async fn music_playlist_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_playlist"); json_path.push("playlist_cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let playlist = rp .query() .music_playlist("PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ") .await .unwrap(); let rp = rp_testfile(&json_path); playlist.tracks.next(rp.query()).await.unwrap().unwrap(); } async fn music_playlist_related(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_playlist"); json_path.push("playlist_related.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let playlist = rp .query() .music_playlist("PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ") .await .unwrap(); let rp = rp_testfile(&json_path); playlist .related_playlists .next(rp.query()) .await .unwrap() .unwrap(); } async fn music_album(testfiles: &Path) { for (name, id) in [ ("one_artist", "MPREb_nlBWQROfvjo"), ("various_artists", "MPREb_8QkDeEIawvX"), ("single", "MPREb_bHfHGoy7vuv"), ("description", "MPREb_PiyfuVl6aYd"), ("unavailable", "MPREb_AzuWg8qAVVl"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_playlist"); json_path.push(format!("album_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_album(id).await.unwrap(); } } async fn music_search(testfiles: &Path) { for (name, query) in [ ("default", "black mamba"), ("typo", "liblingsmensch"), ("radio", "pop radio"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_search"); json_path.push(format!("main_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_search(query).await.unwrap(); } } async fn music_search_tracks(testfiles: &Path) { for (name, query, videos) in [ ("default", "black mamba", false), ("videos", "black mamba", true), ("typo", "liblingsmensch", false), ( "no_artist_link", "Am sichersten seid ihr im Auto #HURRICANESWIMTEAM", false, ), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_search"); json_path.push(format!("tracks_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); if videos { rp.query().music_search_videos(query).await.unwrap(); } else { rp.query().music_search_tracks(query).await.unwrap(); } } } async fn music_search_albums(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_search"); json_path.push("albums.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query().music_search_albums("black mamba").await.unwrap(); } async fn music_search_artists(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_search"); json_path.push("artists.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query() .music_search_artists("black mamba") .await .unwrap(); } async fn music_search_playlists(testfiles: &Path) { for (name, community) in [("ytm", false), ("community", true)] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_search"); json_path.push(format!("playlists_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query() .music_search_playlists_filter("pop", community) .await .unwrap(); } } async fn music_search_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_search"); json_path.push("tracks_cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let res = rp.query().music_search_tracks("black mamba").await.unwrap(); let rp = rp_testfile(&json_path); res.items.next(rp.query()).await.unwrap().unwrap(); } async fn music_search_suggestion(testfiles: &Path) { for (name, query) in [("default", "t"), ("empty", "reujbhevmfndxnjrze")] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_search"); json_path.push(format!("suggestion_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_search_suggestion(query).await.unwrap(); } } async fn music_artist(testfiles: &Path) { for (name, id, all_albums) in [ ("default", "UClmXPfaYhXOYsNn_QUyheWQ", true), ("no_more_albums", "UC_vmjW5e1xEHhYjY2a0kK1A", true), ("only_singles", "UCfwCE5VhPMGxNPFxtVv7lRw", true), ("no_artist", "UCh8gHdtzO2tXd593_bjErWg", true), ("only_more_singles", "UC0aXrjVxG5pZr99v77wZdPQ", true), ("secondary_channel", "UCC9192yGQD25eBZgFZ84MPw", false), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_artist"); json_path.push(format!("artist_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_artist(id, all_albums).await.unwrap(); } } async fn music_details(testfiles: &Path) { for (name, id) in [("mv", "ZeerrnuLi5E"), ("track", "7nigXQS1Xb0")] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_details"); json_path.push(format!("details_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_details(id).await.unwrap(); } } async fn music_lyrics(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_details"); json_path.push("lyrics.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let res = rp.query().music_details("n4tK7LYFxI0").await.unwrap(); let rp = rp_testfile(&json_path); rp.query() .music_lyrics(&res.lyrics_id.unwrap()) .await .unwrap(); } async fn music_related(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_details"); json_path.push("related.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let res = rp.query().music_details("ZeerrnuLi5E").await.unwrap(); let rp = rp_testfile(&json_path); rp.query() .music_related(&res.related_id.unwrap()) .await .unwrap(); } async fn music_radio(testfiles: &Path) { for (name, id) in [("mv", "RDAMVMZeerrnuLi5E"), ("track", "RDAMVM7nigXQS1Xb0")] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_details"); json_path.push(format!("radio_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_radio(id).await.unwrap(); } } async fn music_radio_cont(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_details"); json_path.push("radio_cont.json"); if json_path.exists() { return; } let rp = RustyPipe::new(); let res = rp.query().music_radio("RDAMVM7nigXQS1Xb0").await.unwrap(); let rp = rp_testfile(&json_path); res.next(rp.query()).await.unwrap().unwrap(); } async fn music_new_albums(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_new"); json_path.push("albums_default.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query().music_new_albums().await.unwrap(); } async fn music_new_videos(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_new"); json_path.push("videos_default.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query().music_new_videos().await.unwrap(); } async fn music_charts(testfiles: &Path) { for (name, country) in [("global", Some(Country::Zz)), ("US", Some(Country::Us))] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_charts"); json_path.push(&format!("charts_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_charts(country).await.unwrap(); } } async fn music_genres(testfiles: &Path) { let mut json_path = testfiles.to_path_buf(); json_path.push("music_genres"); json_path.push("genres.json"); if json_path.exists() { return; } let rp = rp_testfile(&json_path); rp.query().music_genres().await.unwrap(); } async fn music_genre(testfiles: &Path) { for (name, id) in [ ("default", "ggMPOg1uX1lMbVZmbzl6NlJ3"), ("mood", "ggMPOg1uX1JOQWZFeDByc2Jm"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("music_genres"); json_path.push(&format!("genre_{name}.json")); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().music_genre(id).await.unwrap(); } }