add continue from cursor option to DownloadCreationsJson

This commit is contained in:
2025-08-25 15:42:36 -07:00
parent ddf294c6f9
commit afd8a74e34

View File

@@ -1,4 +1,5 @@
use std::{io::Read,path::PathBuf};
use std::io::Read;
use std::path::{Path,PathBuf};
use clap::{Args,Parser,Subcommand};
use anyhow::{anyhow,Result as AResult};
use futures::StreamExt;
@@ -148,6 +149,8 @@ struct DownloadCreationsJsonSubcommand{
group_id:Option<u64>,
#[arg(long,group="owner",required=true)]
user_id:Option<u64>,
#[arg(long)]
continue_from_cursor:Option<bool>,
}
/// Download the list of asset ids (not the assets themselves) in a user's inventory. The output is written to `output_folder/versions.json`
#[derive(Args)]
@@ -600,6 +603,7 @@ async fn main()->AResult<()>{
subcommand.group_id,
)?,
subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()),
subcommand.continue_from_cursor.unwrap_or(false),
).await,
Commands::DownloadUserInventoryJson(subcommand)=>download_user_inventory_json(
cookie_from_args(
@@ -1170,19 +1174,60 @@ async fn get_creations_pages(
Ok(())
}
async fn download_creations_json(cookie:Cookie,owner:rbx_asset::cookie::Owner,output_folder:PathBuf)->AResult<()>{
async fn download_creations_pages_from_checkpoint(context:&CookieContext,owner:rbx_asset::cookie::Owner,output_folder:&Path,continue_from_cursor:bool)->AResult<Vec<CreationsItem>>{
let mut versions_path=output_folder.to_owned();
versions_path.set_file_name("versions.json");
let mut cursor_path=output_folder.to_owned();
cursor_path.set_file_name("cursor");
let (mut asset_list,mut config)=if continue_from_cursor{
// load state from files
let (versions,cursor)=tokio::try_join!(
tokio::fs::read(versions_path.as_path()),
tokio::fs::read_to_string(cursor_path.as_path()),
)?;
(
serde_json::from_slice(&versions)?,
rbx_asset::cookie::CreationsPageRequest{
owner,
cursor:Some(cursor),
}
)
}else{
// create new state
(
Vec::new(),
rbx_asset::cookie::CreationsPageRequest{
owner,
cursor:None,
}
)
};
match get_creations_pages(&context,&mut asset_list,&mut config).await{
Ok(())=>println!("Pages polling complete"),
Err(e)=>println!("Error: {e}"),
}
let cursor_fut=async{
if let Some(cursor)=config.cursor{
println!("writing cursor state...");
// there was a problem, write out cursor
tokio::fs::write(cursor_path,cursor).await?;
}
Ok(())
};
let versions_fut=tokio::fs::write(versions_path,serde_json::to_string(&asset_list)?);
tokio::try_join!(versions_fut,cursor_fut)?;
Ok(asset_list)
}
async fn download_creations_json(cookie:Cookie,owner:rbx_asset::cookie::Owner,output_folder:PathBuf,continue_from_cursor:bool)->AResult<()>{
let context=CookieContext::new(cookie);
let mut config=rbx_asset::cookie::CreationsPageRequest{
owner,
cursor:None,
};
let mut asset_list=Vec::new();
let item_list=get_creations_pages(&context,&mut asset_list,&mut config).await?;
let mut path=output_folder.clone();
path.set_file_name("versions.json");
tokio::fs::write(path,serde_json::to_string(&item_list)?).await?;
download_creations_pages_from_checkpoint(&context,owner,output_folder.as_path(),continue_from_cursor).await?;
Ok(())
}