This commit is contained in:
Spectre 2024-12-03 00:56:35 +01:00
parent b2fc33499f
commit 6b868037d1
3 changed files with 91 additions and 12 deletions

View file

@ -4,16 +4,21 @@ use solution::solution;
mod input_data;
use input_data::load_actual;
use reqwest::blocking::Client;
use reqwest::header::COOKIE;
use reqwest::{blocking::Client, Url};
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::sync::Arc;
use std::{time::Instant, env};
fn main() {
let year = 2024;
let day = 2;
let day = 1;
fetch_data(year, day).unwrap();
if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists() {
fetch_data(year, day).unwrap();
}
let Ok(data) = load_actual(year, day)
else { panic!("No Input Data"); };
@ -39,14 +44,21 @@ 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 client = Client::new();
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)
.header(COOKIE, session_cookie)
.send()?;
let body = response.text();
println!("Response: {:?}", body);
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(())
}