use std::{ fs::File, path::{Path, PathBuf}, }; use rustypipe::{ client::{ClientType, RustyPipe}, report::{Report, Reporter}, }; 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), video_details(&testfiles), comments_top(&testfiles), ); } 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 mut root = self.path.clone(); root.set_file_name(""); std::fs::create_dir_all(root).unwrap(); 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::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!("{:?}_video.json", client_type).to_lowercase()); if json_path.exists() { continue; } let rp = rp_testfile(&json_path); rp.query().player(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!("{}.json", name).to_lowercase()); if json_path.exists() { continue; } let player_data = rp.query().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().playlist(id).await.unwrap(); } } async fn video_details(testfiles: &Path) { for (name, id) in [ ("music", "MZOgTu2dMTg"), ("mv", "ZeerrnuLi5E"), ("ccommons", "0rb9CfOvojk"), ("chapters", "nFDBxBUfE74"), ] { let mut json_path = testfiles.to_path_buf(); json_path.push("video_details"); json_path.push(format!("video_details_{}.json", name)); 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(format!("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); rp.query() .video_comments(&details.top_comments.ctoken.unwrap()) .await .unwrap(); // rp.query().video_comments("Eg0SC0lITnpPSGk4c0pzGAYyJSIRIgtJSE56T0hpOHNKczAAeAJCEGNvbW1lbnRzLXNlY3Rpb24%3D").await.unwrap(); // Desktop 1 // top: Eg0SC0lITnpPSGk4c0pzGAYyJSIRIgtJSE56T0hpOHNKczAAeAJCEGNvbW1lbnRzLXNlY3Rpb24%3D // latest: Eg0SC0lITnpPSGk4c0pzGAYyJSIRIgtJSE56T0hpOHNKczABeAJCEGNvbW1lbnRzLXNlY3Rpb24%3D // shows count // Desktop 2 // top: Eg0SC0lITnpPSGk4c0pzGAYyVSIuIgtJSE56T0hpOHNKczAAeAKqAhpVZ3lVZG5WQnBNR09tMnVMR3o1NEFhQUJBZzABQiFlbmdhZ2VtZW50LXBhbmVsLWNvbW1lbnRzLXNlY3Rpb24%3D // shows no count // latest: Eg0SC0lITnpPSGk4c0pzGAYyOCIRIgtJSE56T0hpOHNKczABeAIwAUIhZW5nYWdlbWVudC1wYW5lbC1jb21tZW50cy1zZWN0aW9u // shows no count // cont: Eg0SC0lITnpPSGk4c0pzGAYyJSIRIgtJSE56T0hpOHNKczAAeAJCEGNvbW1lbnRzLXNlY3Rpb24%3D // shows count } #[tokio::test] async fn test() { let id = "IHNzOHi8sJs"; let rp = RustyPipe::new(); let details = rp.query().video_details(id).await.unwrap(); let ctoken_top = details.top_comments.ctoken; let ctoken_latest = details.latest_comments.ctoken; dbg!(ctoken_top); dbg!(ctoken_latest); }