main.rs
This commit is contained in:
parent
c54571a447
commit
8d8fe53574
2 changed files with 44 additions and 33 deletions
30
src/fetch_input.rs
Normal file
30
src/fetch_input.rs
Normal file
|
|
@ -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<dyn std::error::Error>> {
|
||||
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::<Url>().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(())
|
||||
}
|
||||
47
src/main.rs
47
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<dyn std::error::Error>> {
|
||||
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::<Url>().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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue