Compare commits

..

No commits in common. "c22af9bedfbce4129c680015c7a1b012d3a70c47" and "cabc194158f958f5c869e93c6a15a3be9ae47018" have entirely different histories.

2 changed files with 22 additions and 17 deletions

View File

@ -1,6 +1,6 @@
# Copy this to `~/.config/mwbot.toml`.
api_url = "{}"
rest_url = "{}"
api_url = "https://en.wiktionary.org/w/api.php"
rest_url = "https://en.wiktionary.org/api/rest_v1"
[auth]
username = "{}"

View File

@ -15,7 +15,9 @@ struct Cli {
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
enum Action {
/// Move config to ~/.config
Setup {
Setup,
/// Run the bot
Run {
/// Specify the username of the bot
#[arg(long, env = "MW_USERNAME")]
username: String,
@ -31,20 +33,20 @@ enum Action {
/// Specify the REST URL of the bot
#[arg(long, env = "MW_REST_URL")]
rest_url: String,
},
/// Run the bot
Run,
}
fn setup(action: Action) -> Result<(), std::io::Error> {
fn setup() -> Result<(), std::io::Error> {
if let Ok(config_template) = std::fs::read_to_string("mwbot.toml") {
match action {
Action::Run => {panic!();}
Action::Setup { username, botpassword, oauth2_token, api_url, rest_url } => {
let filled_in_config = formatx!(config_template, api_url, rest_url, username, botpassword, oauth2_token).unwrap();
std::fs::write(shellexpand::tilde("~/.config/mwbot.toml").into_owned(), filled_in_config).unwrap();
}
}
let username = env::var("MW_USERNAME").expect("The .env file must have a \"MW_USERNAME\" attribute.");
let oauth2_token = env::var("MW_OAUTH2").expect("The .env file must have a \"MW_OAUTH2\" attribute.");
let botpassword: String = env::var("MW_BOTPASSWORD").expect("The .env file must have a \"MW_BOTPASSWORD\" attribute.");
let filled_in_config = formatx!(config_template, username, botpassword, oauth2_token).unwrap();
std::fs::write(shellexpand::tilde("~/.config/mwbot.toml").into_owned(), filled_in_config).unwrap();
Ok(())
} else {
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Unable to find mwbot.toml; did you execute the script from the same directory?"))
@ -58,12 +60,15 @@ async fn main() -> Result<(), std::io::Error> {
let cli = Cli::parse();
match cli.action {
action @ Action::Setup {..} => {
setup(action)?;
Action::Setup => {
setup()?;
eprintln!("Successfully set up ~/.config/mwbot.toml.")
},
Action::Run => {
let bot = mwbot::Bot::from_default_config().await.unwrap();
Action::Run {username, botpassword, oauth2_token, api_url, rest_url} => {
let bot = mwbot::Bot::builder(api_url, rest_url)
.set_botpassword(username, botpassword)
.build()
.await.unwrap();
let page = bot.page("Bulgaria").unwrap();
let html = page.html().await.unwrap().into_mutable();
println!("{:?}", html);