feat(scraper): add output file

This commit is contained in:
Matthew Esposito 2024-10-30 15:15:38 -04:00
parent f3d2f0cc59
commit bc9530821d

View File

@ -18,6 +18,8 @@ struct Cli {
#[arg(short = 'f', long = "format", value_enum)] #[arg(short = 'f', long = "format", value_enum)]
format: Format, format: Format,
#[arg(short = 'o', long = "output")]
output: Option<String>,
} }
#[derive(Debug, Clone, ValueEnum)] #[derive(Debug, Clone, ValueEnum)]
@ -49,7 +51,7 @@ enum Format {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let (sub, final_count, sort, format) = (cli.sub, cli.count, cli.sort, cli.format); let (sub, final_count, sort, format, output) = (cli.sub, cli.count, cli.sort, cli.format, cli.output);
let initial = format!("/r/{sub}/{sort}.json?&raw_json=1"); let initial = format!("/r/{sub}/{sort}.json?&raw_json=1");
let (mut posts, mut after) = Post::fetch(&initial, false).await.unwrap(); let (mut posts, mut after) = Post::fetch(&initial, false).await.unwrap();
while posts.len() < final_count { while posts.len() < final_count {
@ -65,7 +67,7 @@ async fn main() {
match format { match format {
Format::Json => { Format::Json => {
let filename: String = format!("{sub}.json"); let filename: String = output.unwrap_or_else(|| format!("{sub}.json"));
let json = serde_json::to_string(&posts).unwrap(); let json = serde_json::to_string(&posts).unwrap();
std::fs::write(filename, json).unwrap(); std::fs::write(filename, json).unwrap();
} }