From 8d8fe535740217ad700a7d1b895147b521e5b546 Mon Sep 17 00:00:00 2001 From: Spectre Date: Tue, 3 Dec 2024 01:28:15 +0100 Subject: [PATCH] main.rs --- src/fetch_input.rs | 30 +++++++++++++++++++++++++++++ src/main.rs | 47 ++++++++++++++-------------------------------- 2 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 src/fetch_input.rs diff --git a/src/fetch_input.rs b/src/fetch_input.rs new file mode 100644 index 0000000..cfa89fb --- /dev/null +++ b/src/fetch_input.rs @@ -0,0 +1,30 @@ +use reqwest::{blocking::Client, Url}; + +use std::fs::File; +use std::io::prelude::*; +use std::path::Path; +use std::sync::Arc; +use std::env; + +pub fn fetch_input(year: usize, day: usize) -> Result<(), Box> { + let url = format!("https://adventofcode.com/{year}/day/{day}/input"); + let session_cookie = env::var("ADVENT_TOKEN")?; + + let jar = reqwest::cookie::Jar::default(); + jar.add_cookie_str(&format!("session={session_cookie}; Domain=.adventofcode.com"), &"https://adventofcode.com".parse::().unwrap()); + + let client = Client::builder().cookie_provider(Arc::new(jar)).build().unwrap(); + let response = client + .get(url) + .send()?; + + let body = response.text()?; + + let path = format!("./data/{}/day{:02}", year, day); + + let mut output = File::create(path)?; + + output.write_all(body.as_bytes())?; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index e65dc88..0edfabb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,20 +4,24 @@ use solution::solution; mod input_data; use input_data::load_actual; -use reqwest::{blocking::Client, Url}; +mod fetch_input; +use fetch_input::fetch_input; -use std::fs::File; -use std::io::prelude::*; -use std::path::Path; -use std::sync::Arc; -use std::{time::Instant, env}; +use std::{path::Path, time::Instant, env}; fn main() { - let year = 2024; - let day = 1; + let year = env::var("ADVENT_YEAR") + .unwrap() + .parse() + .unwrap_or(2024); + let day = env::var("ADVENT_DAY") + .unwrap() + .parse() + .unwrap_or(1); - if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists() { - fetch_data(year, day).unwrap(); + if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists() + && env::var("ADVENT_TOKEN").is_ok() { + fetch_input(year, day).expect("Set ADVENT_TOKEN to the correct session cookie to fetch input automatically"); } let Ok(data) = load_actual(year, day) @@ -39,26 +43,3 @@ fn main() { println!("Part 2 result is {}, took {}ms", part2, elapsed2.as_millis()); } - -fn fetch_data(year: usize, day: usize) -> Result<(), Box> { - let url = format!("https://adventofcode.com/{year}/day/{day}/input"); - let session_cookie = env::var("ADVENT_TOKEN")?; - - let jar = reqwest::cookie::Jar::default(); - jar.add_cookie_str(&format!("session={session_cookie}; Domain=.adventofcode.com"), &"https://adventofcode.com".parse::().unwrap()); - - let client = Client::builder().cookie_provider(Arc::new(jar)).build().unwrap(); - let response = client - .get(url) - .send()?; - - let body = response.text()?; - - let path = format!("./data/{}/day{:02}", year, day); - - let mut output = File::create(path)?; - - output.write_all(body.as_bytes())?; - - Ok(()) -}