make download_history faster

This commit is contained in:
Quaternions 2024-01-12 16:19:01 -08:00
parent b3f5dd6c4d
commit eb20f8056d

View File

@ -288,10 +288,7 @@ async fn download_list(cookie:String,asset_id_file_map:AssetIDFileMap)->AResult<
Ok(()) Ok(())
} }
async fn get_version_history(client:&reqwest::Client,cookie:&str,asset_id:AssetID)->AResult<Vec<AssetVersion>>{ async fn download_page(client:&reqwest::Client,cookie:&str,asset_id:AssetID,cursor:Option<String>)->AResult<VersionPage>{
let mut cursor:Option<String>=None;
let mut asset_list=Vec::new();
loop{
let mut url=reqwest::Url::parse(format!("https://develop.roblox.com/v1/assets/{}/saved-versions",asset_id).as_str())?; let mut url=reqwest::Url::parse(format!("https://develop.roblox.com/v1/assets/{}/saved-versions",asset_id).as_str())?;
//url borrow scope //url borrow scope
{ {
@ -308,16 +305,19 @@ async fn get_version_history(client:&reqwest::Client,cookie:&str,asset_id:AssetI
let resp=client.get(url) let resp=client.get(url)
.header("Cookie",cookie) .header("Cookie",cookie)
.send().await?; .send().await?;
match resp.json::<VersionPage>().await{ Ok(resp.json::<VersionPage>().await?)
Ok(mut page)=>{ }
async fn get_version_history(client:&reqwest::Client,cookie:&str,asset_id:AssetID)->AResult<Vec<AssetVersion>>{
let mut cursor:Option<String>=None;
let mut asset_list=Vec::new();
loop{
let mut page=download_page(client,cookie,asset_id,cursor).await?;
asset_list.append(&mut page.data); asset_list.append(&mut page.data);
if page.nextPageCursor.is_none(){ if page.nextPageCursor.is_none(){
break; break;
} }
cursor=page.nextPageCursor; cursor=page.nextPageCursor;
},
Err(e)=>panic!("error: {}",e),
}
} }
asset_list.sort_by(|a,b|a.assetVersionNumber.cmp(&b.assetVersionNumber)); asset_list.sort_by(|a,b|a.assetVersionNumber.cmp(&b.assetVersionNumber));
Ok(asset_list) Ok(asset_list)
@ -356,45 +356,52 @@ struct DownloadHistoryConfig{
async fn download_history(config:DownloadHistoryConfig)->AResult<()>{ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{
let client=reqwest::Client::new(); let client=reqwest::Client::new();
//poll paged list of all asset versions
let asset_list=get_version_history(&client,&config.cookie.as_str(),config.asset_id).await?;
let mut path=config.output_folder.clone();
path.set_file_name("versions.json");
tokio::fs::write(path,serde_json::to_string(&asset_list)?).await?;
//download all versions
let asset_id_string=config.asset_id.to_string(); let asset_id_string=config.asset_id.to_string();
futures::stream::iter(asset_list)
.map(|asset_version|{ //poll paged list of all asset versions
let client=&client; let mut cursor:Option<String>=None;
let cookie=config.cookie.as_str(); let mut asset_list=Vec::new();
let asset_id_str=asset_id_string.as_str(); let mut join_handles=Vec::new();
loop{
let mut page=download_page(&client,config.cookie.as_str(),config.asset_id,cursor).await?;
for asset_version in &page.data{
let version_number=asset_version.assetVersionNumber;
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 output_folder=config.output_folder.clone();
async move{ join_handles.push(tokio::spawn(async move{
let resp=download_asset_version(client,cookie,asset_id_str,asset_version.assetVersionNumber.to_string().as_str()).await?; 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?))?{ let contents=match maybe_gzip_decode(std::io::Cursor::new(resp.bytes().await?))?{
ReaderType::GZip(readable)=>read_readable(readable)?, ReaderType::GZip(readable)=>read_readable(readable)?,
ReaderType::Raw(readable)=>read_readable(readable)?, ReaderType::Raw(readable)=>read_readable(readable)?,
}; };
let mut path=output_folder; let mut path=output_folder;
path.set_file_name(format!("{}_v{}.rbxl",config.asset_id,asset_version.assetVersionNumber)); path.set_file_name(format!("{}_v{}.rbxl",config.asset_id,version_number));
Ok((path,contents)) tokio::fs::write(path,contents).await?;
Ok::<_,anyhow::Error>(())
}));
} }
}) asset_list.append(&mut page.data);
.buffer_unordered(CONCURRENT_REQUESTS) if page.nextPageCursor.is_none(){
.for_each(|b:AResult<_>|async{ break;
match b{
Ok((dest,data))=>{
match tokio::fs::write(dest,data).await{
Err(e)=>eprintln!("fs error: {}",e),
_=>(),
} }
}, cursor=page.nextPageCursor;
Err(e)=>eprintln!("dl error: {}",e),
} }
}).await;
asset_list.sort_by(|a,b|a.assetVersionNumber.cmp(&b.assetVersionNumber));
let mut path=config.output_folder.clone();
path.set_file_name("versions.json");
tokio::fs::write(path,serde_json::to_string(&asset_list)?).await?;
for join_handle in join_handles{
join_handle.await??;
}
Ok(()) Ok(())
} }