Add target option to run-bot

This commit is contained in:
Sam W 2022-09-13 18:48:16 +01:00
parent b1753d8775
commit 25d3846e1c
1 changed files with 9 additions and 4 deletions

View File

@ -98,6 +98,8 @@ enum Commands {
RunBot {
#[clap(short, long, action)]
dry_run: bool,
#[clap(short, long, value_parser)]
target: Option<String>,
},
/// Print details about the currently authed user
Whoami,
@ -112,7 +114,7 @@ fn main() -> StdError<()> {
Commands::ScrapeWeb => do_scrape_web(),
Commands::ListTweets => do_list_tweets(),
Commands::Whoami => do_whoami(),
Commands::RunBot { dry_run } => run_bot(*dry_run),
Commands::RunBot { dry_run, target } => run_bot(*dry_run, target.to_owned()),
}
}
@ -194,11 +196,14 @@ fn get_client(headers: Option<reqwest::header::HeaderMap>) -> StdError<reqwest::
Ok(c.build()?)
}
fn run_bot(dry_run: bool) -> StdError<()> {
fn run_bot(dry_run: bool, target: Option<String>) -> StdError<()> {
let all = scrape_web()?;
let (title, filename) = all
let (title, filename) = if let Some(target) = target {
all.iter().find(|(title, _)| title.to_lowercase().contains(&target.to_lowercase()))
} else {
all
.choose(&mut rand::thread_rng())
.ok_or("got no images m8")?;
}.ok_or("got no images m8")?;
event!(Level::INFO, title, filename, "Picked random thing");
let client = get_client(None)?;
event!(Level::INFO, "Fetching metadata...");