Init
This commit is contained in:
commit
fc496204e7
24 changed files with 2758 additions and 0 deletions
1
aoc-helper/.gitignore
vendored
Normal file
1
aoc-helper/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target/
|
||||
1944
aoc-helper/Cargo.lock
generated
Normal file
1944
aoc-helper/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
13
aoc-helper/Cargo.toml
Normal file
13
aoc-helper/Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "aoc-helper"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
clap = { version = "4.5.41", features = ["derive"] }
|
||||
diesel = { version = "2.2.12", features = ["sqlite"] }
|
||||
glob = "0.3.2"
|
||||
log = "0.4.27"
|
||||
reqwest = { version = "0.12.22", features = ["blocking"] }
|
||||
simplelog = "0.12.2"
|
||||
90
aoc-helper/src/cli.rs
Normal file
90
aoc-helper/src/cli.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
use clap::{Args, Parser, Subcommand, ValueEnum};
|
||||
use std::fmt::Display;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
const YEAR_RANGE: RangeInclusive<usize> = 2015..=2025;
|
||||
const DAY_RANGE: RangeInclusive<usize> = 1..=25;
|
||||
|
||||
fn year_in_range(y: &str) -> Result<usize, String> {
|
||||
let year: usize = y.parse().map_err(|_| format!("`{y}` is not a number."))?;
|
||||
if YEAR_RANGE.contains(&year) {
|
||||
Ok(year)
|
||||
} else {
|
||||
Err(format!(
|
||||
"`{year}` not in range {}-{}.",
|
||||
YEAR_RANGE.start(),
|
||||
YEAR_RANGE.end()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn day_in_range(d: &str) -> Result<usize, String> {
|
||||
let day: usize = d.parse().map_err(|_| format!("`{d}` is not a number."))?;
|
||||
if DAY_RANGE.contains(&day) {
|
||||
Ok(day)
|
||||
} else {
|
||||
Err(format!(
|
||||
"`{day}` not in range {}-{}.",
|
||||
DAY_RANGE.start(),
|
||||
DAY_RANGE.end()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, ValueEnum)]
|
||||
pub enum Part {
|
||||
One,
|
||||
Two,
|
||||
}
|
||||
|
||||
impl Display for Part {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::One => write!(f, "1"),
|
||||
Self::Two => write!(f, "2"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "aoc-helper")]
|
||||
#[command(about = "CLI to help you interact with Advent Of Code.")]
|
||||
pub struct Cli {
|
||||
#[arg(short, long, global = true, help = "debug output")]
|
||||
pub verbose: bool,
|
||||
#[command(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Command {
|
||||
/// Get an input file
|
||||
Input(InputArgs),
|
||||
/// Submit an answer
|
||||
Answer(AnswerArgs),
|
||||
/// Retrieve cookie session from Firefox
|
||||
GetSession,
|
||||
/// Set given cookie session
|
||||
SetSession { session: String },
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct InputArgs {
|
||||
#[arg(short, long, help = "output filepath [default: <year>_<day>_day.txt]")]
|
||||
pub output: Option<String>,
|
||||
#[arg(short, long, help = "year to use", value_parser = year_in_range)]
|
||||
pub year: usize,
|
||||
#[arg(short, long, help = "day to use", value_parser = day_in_range)]
|
||||
pub day: usize,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct AnswerArgs {
|
||||
#[arg(short, long, help = "year to use for submission", value_parser = year_in_range)]
|
||||
pub year: usize,
|
||||
#[arg(short, long, help = "day to use for submission", value_parser = day_in_range)]
|
||||
pub day: usize,
|
||||
#[arg(short, long, help = "part to use for submission", value_enum)]
|
||||
pub part: Part,
|
||||
pub answer: String,
|
||||
}
|
||||
70
aoc-helper/src/client.rs
Normal file
70
aoc-helper/src/client.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use log::debug;
|
||||
use std::{env, fs};
|
||||
|
||||
use reqwest::{
|
||||
blocking::{self, Response},
|
||||
header,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
cli::Part,
|
||||
cookies::session::{CONFIG_DIR, SESSION_FILE},
|
||||
};
|
||||
|
||||
const BASE_URL: &str = "https://adventofcode.com";
|
||||
|
||||
pub struct AocClient {
|
||||
client: blocking::Client,
|
||||
base_url: String,
|
||||
}
|
||||
|
||||
impl AocClient {
|
||||
pub fn new() -> Result<AocClient, reqwest::Error> {
|
||||
let home_dir = env::home_dir().expect("cannot read HOME var env");
|
||||
let session = fs::read_to_string(home_dir.join(CONFIG_DIR).join(SESSION_FILE))
|
||||
.expect("missing session file. Did you correctly set up the session using get-session or set-session commands ?");
|
||||
|
||||
let mut headers = header::HeaderMap::new();
|
||||
|
||||
let mut session_value =
|
||||
header::HeaderValue::from_str(format!("session={session}").as_str()).unwrap();
|
||||
session_value.set_sensitive(true);
|
||||
|
||||
headers.insert(header::COOKIE, session_value);
|
||||
|
||||
Ok(AocClient {
|
||||
client: blocking::Client::builder()
|
||||
.default_headers(headers)
|
||||
.build()?,
|
||||
base_url: String::from(BASE_URL),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_input_file(&self, year: usize, day: usize) -> Result<Response, reqwest::Error> {
|
||||
let url = format!("{}/{year}/day/{day}/input", self.base_url);
|
||||
|
||||
debug!("preparing: GET {url}");
|
||||
self.client.get(url).send()
|
||||
}
|
||||
|
||||
pub fn post_answer(
|
||||
&self,
|
||||
year: usize,
|
||||
day: usize,
|
||||
part: &Part,
|
||||
answer: &str,
|
||||
) -> Result<Response, reqwest::Error> {
|
||||
let url = format!("{}/{year}/day/{day}/answer", self.base_url);
|
||||
|
||||
let part_str = match part {
|
||||
Part::One => "1",
|
||||
Part::Two => "2",
|
||||
};
|
||||
|
||||
debug!("preparing: POST {url} - level: {part_str}, answer: {answer}");
|
||||
self.client
|
||||
.post(url)
|
||||
.form(&[("level", part_str), ("answer", answer)])
|
||||
.send()
|
||||
}
|
||||
}
|
||||
102
aoc-helper/src/commands.rs
Normal file
102
aoc-helper/src/commands.rs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
use anyhow::{Context, anyhow};
|
||||
use log::{debug, info};
|
||||
use std::{
|
||||
env, fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::cookies::session;
|
||||
use crate::{cli::Part, client::AocClient};
|
||||
|
||||
pub fn cmd_set_session(session: &str) -> anyhow::Result<()> {
|
||||
session::write_session_to_file(session, session::SESSION_FILE)
|
||||
.with_context(|| format!("could not set up session: {session}"))?;
|
||||
|
||||
info!("session set up successfully to: {session}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_get_session() -> anyhow::Result<()> {
|
||||
let home_path = env::home_dir().unwrap();
|
||||
let search_path = home_path.join(".mozilla/firefox/*.default-release/cookies.sqlite");
|
||||
let search_path_str = search_path.to_str().unwrap();
|
||||
|
||||
let mut path = PathBuf::new();
|
||||
for entry in glob::glob(search_path_str)
|
||||
.with_context(|| format!("failed to read glob pattern: {search_path_str}"))?
|
||||
.flatten()
|
||||
{
|
||||
path = entry;
|
||||
}
|
||||
|
||||
debug!("found glob pattern for {search_path_str}");
|
||||
|
||||
let filename = path.file_name().ok_or(anyhow!(
|
||||
"could not find firefox database file: cookies.sqlite"
|
||||
))?;
|
||||
|
||||
// tmp database if firefox open
|
||||
let tmp_path = Path::new("/tmp").join(filename);
|
||||
debug!("tmp_path: {:?}", &tmp_path);
|
||||
fs::copy(path, &tmp_path)?;
|
||||
|
||||
let session = session::retrieve_session(&tmp_path)
|
||||
.map_err(|err| anyhow!(err))
|
||||
.context("could not retrieve session")?;
|
||||
|
||||
debug!("retrieve session ok");
|
||||
|
||||
session::write_session_to_file(&session, session::SESSION_FILE)
|
||||
.context("could not save session")?;
|
||||
fs::remove_file(tmp_path)?;
|
||||
|
||||
info!("session retrieved and saved successfully");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_get_input_file(year: usize, day: usize, output: &str) -> anyhow::Result<()> {
|
||||
let client = AocClient::new().context("could not build aoc client for future requests")?;
|
||||
|
||||
debug!("aoc client ok");
|
||||
|
||||
let response = client
|
||||
.get_input_file(year, day)
|
||||
.context(format!("could not get input file for year: {year} and day: {day:02}."))?
|
||||
.error_for_status()
|
||||
.context(format!("could not get input file for year: {year} and day: {day:02}. Are you sure the session cookie is correctly set up ?"))?;
|
||||
|
||||
debug!("request ok");
|
||||
|
||||
fs::write(output, response.text().unwrap()).context("could not write input data to file")?;
|
||||
|
||||
info!("input data successfully retrieved and saved to '{output}'");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_submit_answer(year: usize, day: usize, part: &Part, answer: &str) -> anyhow::Result<()> {
|
||||
let client = AocClient::new().context("could not build aoc client for future requests")?;
|
||||
|
||||
debug!("aoc client ok");
|
||||
|
||||
let response = client
|
||||
.post_answer(year, day, part, answer)
|
||||
.context(format!(
|
||||
"could not submit '{answer}' for year: {year} and day: {day:02} - part {part}"
|
||||
))?
|
||||
.error_for_status()
|
||||
.context(format!(
|
||||
"could not submit '{answer}' for year: {year} and day: {day:02} - part {part}. Are you sure the session cookie is correctly set up ?"
|
||||
))?;
|
||||
|
||||
debug!("request ok");
|
||||
|
||||
let response_text = response.text()?;
|
||||
|
||||
// Validate answer
|
||||
if response_text.contains("That's the right answer!") {
|
||||
info!("Correct ! That's the right answer.");
|
||||
} else {
|
||||
info!("That is not the right answer...");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
4
aoc-helper/src/cookies/mod.rs
Normal file
4
aoc-helper/src/cookies/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
mod model;
|
||||
mod schema;
|
||||
|
||||
pub mod session;
|
||||
21
aoc-helper/src/cookies/model.rs
Normal file
21
aoc-helper/src/cookies/model.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use diesel::prelude::Queryable;
|
||||
|
||||
#[allow(non_snake_case, unused)]
|
||||
#[derive(Debug, Queryable)]
|
||||
pub struct Cookies {
|
||||
pub id: Option<i32>,
|
||||
pub originAttributes: String,
|
||||
pub name: Option<String>,
|
||||
pub value: Option<String>,
|
||||
pub host: Option<String>,
|
||||
pub path: Option<String>,
|
||||
pub expiry: Option<i32>,
|
||||
pub lastAccessed: Option<i32>,
|
||||
pub creationTime: Option<i32>,
|
||||
pub isSecure: Option<i32>,
|
||||
pub isHttpOnly: Option<i32>,
|
||||
pub inBrowserElement: Option<i32>,
|
||||
pub sameSite: Option<i32>,
|
||||
pub schemeMap: Option<i32>,
|
||||
pub isPartitionedAttributeSet: Option<i32>,
|
||||
}
|
||||
19
aoc-helper/src/cookies/schema.rs
Normal file
19
aoc-helper/src/cookies/schema.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
diesel::table! {
|
||||
moz_cookies (id) {
|
||||
id -> Nullable<Integer>,
|
||||
originAttributes -> Text,
|
||||
name -> Nullable<Text>,
|
||||
value -> Nullable<Text>,
|
||||
host -> Nullable<Text>,
|
||||
path -> Nullable<Text>,
|
||||
expiry -> Nullable<Integer>,
|
||||
lastAccessed -> Nullable<Integer>,
|
||||
creationTime -> Nullable<Integer>,
|
||||
isSecure -> Nullable<Integer>,
|
||||
isHttpOnly -> Nullable<Integer>,
|
||||
inBrowserElement -> Nullable<Integer>,
|
||||
sameSite -> Nullable<Integer>,
|
||||
schemeMap -> Nullable<Integer>,
|
||||
isPartitionedAttributeSet -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
78
aoc-helper/src/cookies/session.rs
Normal file
78
aoc-helper/src/cookies/session.rs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
use diesel::prelude::*;
|
||||
use diesel::{ConnectionResult, SqliteConnection};
|
||||
use log::debug;
|
||||
use std::path::Path;
|
||||
use std::{env, fmt, fs, io};
|
||||
|
||||
use super::model::Cookies;
|
||||
|
||||
pub const CONFIG_DIR: &str = ".config/aoc-helper";
|
||||
pub const SESSION_FILE: &str = "session";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SessionError {
|
||||
Connection(diesel::result::ConnectionError),
|
||||
Query(diesel::result::Error),
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl From<diesel::result::ConnectionError> for SessionError {
|
||||
fn from(value: diesel::result::ConnectionError) -> Self {
|
||||
Self::Connection(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<diesel::result::Error> for SessionError {
|
||||
fn from(value: diesel::result::Error) -> Self {
|
||||
Self::Query(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SessionError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
match self {
|
||||
Self::Connection(e) => write!(f, "connection to database error: {e}"),
|
||||
Self::Query(e) => write!(f, "query database error: {e}"),
|
||||
Self::NotFound => write!(f, "'.adventofcode.com' session not found"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn connect_database<P: AsRef<Path>>(path: P) -> ConnectionResult<SqliteConnection> {
|
||||
let database_url = path.as_ref().to_str().unwrap();
|
||||
SqliteConnection::establish(database_url)
|
||||
}
|
||||
|
||||
pub fn retrieve_session<P: AsRef<Path>>(path: P) -> Result<String, SessionError> {
|
||||
use super::schema::moz_cookies::dsl::{host, moz_cookies, name};
|
||||
|
||||
let mut conn = connect_database(path)?;
|
||||
debug!("connected to database");
|
||||
|
||||
let records = moz_cookies
|
||||
.filter(host.eq(".adventofcode.com"))
|
||||
.filter(name.eq("session"))
|
||||
.limit(1)
|
||||
.load::<Cookies>(&mut conn)?;
|
||||
|
||||
debug!("retrieved: {} record", records.len());
|
||||
if !records.is_empty() {
|
||||
let session = &records[0];
|
||||
session
|
||||
.value
|
||||
.to_owned()
|
||||
.ok_or_else(|| SessionError::NotFound)
|
||||
} else {
|
||||
Err(SessionError::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_session_to_file(session: &str, filename: &str) -> io::Result<()> {
|
||||
let home_path = env::home_dir().unwrap();
|
||||
let config_dir_path = home_path.join(CONFIG_DIR);
|
||||
if !fs::exists(&config_dir_path)? {
|
||||
fs::create_dir(&config_dir_path)?;
|
||||
}
|
||||
fs::write(config_dir_path.join(filename), session)?;
|
||||
Ok(())
|
||||
}
|
||||
35
aoc-helper/src/logging.rs
Normal file
35
aoc-helper/src/logging.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use std::{env::home_dir, fs::File};
|
||||
|
||||
use log::SetLoggerError;
|
||||
use simplelog::{
|
||||
CombinedLogger, ConfigBuilder, SharedLogger, TermLogger, TerminalMode, WriteLogger,
|
||||
};
|
||||
|
||||
use crate::cookies::session::CONFIG_DIR;
|
||||
|
||||
pub fn init_logs(log_level: log::LevelFilter, filename: &str) -> Result<(), SetLoggerError> {
|
||||
let mut filepath = home_dir().unwrap();
|
||||
filepath.push(CONFIG_DIR);
|
||||
filepath.push(filename);
|
||||
|
||||
let logfile = File::create(filepath).unwrap();
|
||||
let config_writelogger = ConfigBuilder::new().set_time_format_rfc2822().build();
|
||||
let config_termlogger = ConfigBuilder::new()
|
||||
.set_time_level(log::LevelFilter::Off)
|
||||
.set_thread_level(log::LevelFilter::Off)
|
||||
.set_target_level(log::LevelFilter::Off)
|
||||
.set_max_level(log::LevelFilter::Debug)
|
||||
.build();
|
||||
|
||||
let loggers: Vec<Box<dyn SharedLogger>> = vec![
|
||||
WriteLogger::new(log::LevelFilter::Debug, config_writelogger, logfile),
|
||||
TermLogger::new(
|
||||
log_level,
|
||||
config_termlogger,
|
||||
TerminalMode::Mixed,
|
||||
simplelog::ColorChoice::Auto,
|
||||
),
|
||||
];
|
||||
|
||||
CombinedLogger::init(loggers)
|
||||
}
|
||||
57
aoc-helper/src/main.rs
Normal file
57
aoc-helper/src/main.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
mod cli;
|
||||
mod client;
|
||||
mod commands;
|
||||
mod cookies;
|
||||
mod logging;
|
||||
|
||||
use clap::Parser;
|
||||
use cli::Command;
|
||||
use commands::{cmd_get_input_file, cmd_get_session, cmd_set_session, cmd_submit_answer};
|
||||
use log::{LevelFilter, error};
|
||||
use std::process;
|
||||
|
||||
const LOG_FILE: &str = "aoc.log";
|
||||
|
||||
fn main() {
|
||||
let cli = cli::Cli::parse();
|
||||
|
||||
let log_level = if cli.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Info
|
||||
};
|
||||
|
||||
if let Err(e) = logging::init_logs(log_level, LOG_FILE) {
|
||||
error!("failed to init logs: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
run(&cli).unwrap_or_else(|err| {
|
||||
error!("{err:?}");
|
||||
process::exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
fn run(opts: &cli::Cli) -> anyhow::Result<()> {
|
||||
match &opts.command {
|
||||
Command::Input(input_args) => {
|
||||
let output = match input_args.output.clone() {
|
||||
Some(o) => o,
|
||||
None => format!("{}_{}_day.txt", input_args.year, input_args.day),
|
||||
};
|
||||
|
||||
cmd_get_input_file(input_args.year, input_args.day, &output)?;
|
||||
}
|
||||
Command::Answer(answer_args) => {
|
||||
cmd_submit_answer(
|
||||
answer_args.year,
|
||||
answer_args.day,
|
||||
&answer_args.part,
|
||||
&answer_args.answer,
|
||||
)?;
|
||||
}
|
||||
Command::GetSession => cmd_get_session()?,
|
||||
Command::SetSession { session } => cmd_set_session(session)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue