From 5574c34045903f6b8e9d5bfcf6d7bb5fa189457d Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sat, 13 Jan 2024 20:49:06 -0800 Subject: [PATCH] end_version for download_history + catch download error and write versions.json --- src/main.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 090fb50..77a436b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,8 @@ struct Cli{ //TODO: read the versions.json file instead of doing this #[arg(long)] start_version:Option, + #[arg(long)] + end_version:Option, //decompile options #[arg(long)] @@ -137,6 +139,7 @@ async fn main()->AResult<()>{ match cli.command{ Commands::DownloadHistory=>download_history(DownloadHistoryConfig{ + end_version:cli.end_version, start_version:cli.start_version.unwrap_or(0), output_folder:cli.output.unwrap(), cookie:cookie.unwrap(), @@ -352,6 +355,7 @@ async fn download_asset_version(client:&reqwest::Client,cookie:&str,asset_id_str } struct DownloadHistoryConfig{ + end_version:Option, start_version:u64, output_folder:std::path::PathBuf, cookie:String, @@ -371,9 +375,19 @@ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{ let mut asset_list=Vec::new(); loop{ let mut page=download_page(&client,config.cookie.as_str(),config.asset_id,cursor).await?; + let client=&client; + let cookie=config.cookie.clone(); + let asset_id_str=asset_id_string.clone(); + let output_folder=config.output_folder.clone(); + let data=&page.data; + let join_set=&mut join_set; + let error_catcher=||async move{ let mut cancel_paging=false; - for asset_version in &page.data{ + for asset_version in data{ let version_number=asset_version.assetVersionNumber; + if config.end_version.is_some_and(|v|vAResult<()>{ join_set.join_next().await.unwrap()??; } let client=client.clone(); - let cookie=config.cookie.clone(); - let asset_id_str=asset_id_string.clone(); - let output_folder=config.output_folder.clone(); + let cookie=cookie.clone(); + let asset_id_str=asset_id_str.clone(); + let output_folder=output_folder.clone(); join_set.spawn(async move{ let resp=download_asset_version(&client,cookie.as_str(),asset_id_str.as_str(),version_number.to_string().as_str()).await?; let contents=match maybe_gzip_decode(std::io::Cursor::new(resp.bytes().await?))?{ @@ -392,7 +406,7 @@ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{ ReaderType::Raw(readable)=>read_readable(readable)?, }; - let mut path=output_folder; + let mut path=output_folder.clone(); path.set_file_name(format!("{}_v{}.rbxl",config.asset_id,version_number)); tokio::fs::write(path,contents).await?; @@ -400,6 +414,16 @@ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{ Ok::<_,anyhow::Error>(()) }); } + Ok::<_,anyhow::Error>(cancel_paging) + }; + let cancel_paging=match error_catcher().await{ + Ok(cancel)=>cancel, + Err(e)=>{ + println!("download error: {}",e); + //cancel download and write versions + true + }, + }; if page.nextPageCursor.is_none()||cancel_paging{ for asset_version in page.data.into_iter(){ if config.start_version<=asset_version.assetVersionNumber{