use std::{ fs::File, path::{Path, PathBuf}, }; use rustypipe::{ cache::FileStorage, client::{ClientType, RustyPipe}, report::{Report, Reporter}, }; 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, } impl TestFileReporter { pub fn new>(path: P) -> Self { Self { path: path.as_ref().to_path_buf(), } } } impl Reporter for TestFileReporter { fn report(&self, report: &Report) { let data = serde_json::from_str::(&report.http_request.resp_body).unwrap(); let file = File::create(&self.path).unwrap(); serde_json::to_writer_pretty(file, &data).unwrap(); println!("Downloaded {}", self.path.display()); } } fn rp_testfile(json_path: &Path) -> RustyPipe { let reporter = TestFileReporter::new(json_path); RustyPipe::new( Some(Box::new(FileStorage::default())), Some(Box::new(reporter)), None, ) } pub async fn download_testfiles(project_root: &Path) { let mut testfiles = project_root.to_path_buf(); testfiles.push("testfiles"); tokio::join!( player(&testfiles), player_model(&testfiles), playlist(&testfiles) ); } 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!("{:?}_video.json", client_type).to_lowercase()); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query() .report(true) .strict(true) .get_player(video_id, client_type) .await .unwrap(); } } async fn player_model(testfiles: &Path) { let rp = RustyPipe::default(); 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!("{}.json", name).to_lowercase()); if json_path.exists() { continue; } let player_data = rp .query() .strict(true) .get_player(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_{}.json", name)); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query() .report(true) .strict(true) .get_playlist(id) .await .unwrap(); } }