From 15e8c8208fe3da559e3f96f4dd4d4910da95ffd6 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sat, 13 Jan 2024 21:22:25 -0800 Subject: [PATCH] add --continue feature for download history --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 77a436b..b989bf7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,8 @@ struct Cli{ start_version:Option, #[arg(long)] end_version:Option, + #[arg(long)] + r#continue:bool, //decompile options #[arg(long)] @@ -139,6 +141,7 @@ async fn main()->AResult<()>{ match cli.command{ Commands::DownloadHistory=>download_history(DownloadHistoryConfig{ + continue_from_versions:cli.r#continue, end_version:cli.end_version, start_version:cli.start_version.unwrap_or(0), output_folder:cli.output.unwrap(), @@ -355,6 +358,7 @@ async fn download_asset_version(client:&reqwest::Client,cookie:&str,asset_id_str } struct DownloadHistoryConfig{ + continue_from_versions:bool, end_version:Option, start_version:u64, output_folder:std::path::PathBuf, @@ -362,7 +366,46 @@ struct DownloadHistoryConfig{ asset_id:AssetID, } -async fn download_history(config:DownloadHistoryConfig)->AResult<()>{ +async fn download_history(mut config:DownloadHistoryConfig)->AResult<()>{ + let mut asset_list:Vec=Vec::new(); + if config.end_version.is_none()&&config.continue_from_versions{ + //load prexisting versions list + let mut versions_path=config.output_folder.clone(); + versions_path.push("versions.json"); + match std::fs::File::open(versions_path){ + Ok(versions_file)=>asset_list.append(&mut serde_json::from_reader(versions_file)?), + Err(e)=>match e.kind(){ + std::io::ErrorKind::NotFound=>return Err(anyhow::Error::msg("Cannot continue from versions.json - file does not exist")), + _=>Err(e)?, + } + } + //write down which versions are contained + let mut reference_hashmap=std::collections::HashSet::new(); + for asset_version in &asset_list{ + reference_hashmap.insert(asset_version.assetVersionNumber); + } + //find the highest number + match asset_list.iter().map(|asset_version|asset_version.assetVersionNumber).max(){ + Some(max)=>{ + //count down contiguously until a number is missing + for i in (1..=max).rev(){ + if !reference_hashmap.contains(&i){ + //that is end_version + config.end_version=Some(i); + break; + } + } + //if all versions are contained, set start_version to the max + 1 + if config.end_version.is_none(){ + config.start_version=max+1; + } + }, + None=>Err(anyhow::Error::msg("Cannot continue from versions.json - there are no previous versions"))?, + } + //clear the asset list, I can't tell if roblox provides a way to start on a specific page + //so it just starts from the beginning every time + asset_list.clear(); + } let client=reqwest::Client::new(); let asset_id_string=config.asset_id.to_string(); @@ -372,7 +415,6 @@ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{ //poll paged list of all asset versions let mut cursor:Option=None; - 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; @@ -933,7 +975,7 @@ struct DecompileHistoryConfig{ } async fn decompile_history_into_git(config:DecompileHistoryConfig)->AResult<()>{ - //poll paged list of all asset versions + //use prexisting versions list let mut versions_path=config.input_folder.clone(); versions_path.push("versions.json"); let asset_list:Vec=serde_json::from_reader(std::fs::File::open(versions_path)?)?;