use scraper::{Html, Selector}; pub async fn fetch_input(auth: String, day: u8) -> String { let input = reqwest::Client::new() .get(format!("https://adventofcode.com/2023/day/{day}/input")) .header("Cookie", format!("session={auth}")) .send() .await .expect("Unable to make input request") .text() .await .expect("Unable to parse string from input response") .trim_end() .to_owned(); if input != "Puzzle inputs differ by user. Please log in to get your puzzle input." { input } else { panic!("Invalid auth") } } pub async fn fetch_large(day: u8, part: u8) -> (String, Option) { ( reqwest::Client::new() .get(format!("https://github.com/Vap0r1ze/aoc-large-inputs/raw/main/dist/{day:02}/input_large.txt")) .send() .await .expect("Unable to make input request") .error_for_status() .expect("Unable to fetch large response for day, does it exist?") .text() .await .expect("Unable to parse string from input response") .trim_end() .to_owned(), match reqwest::Client::new() .get(format!("https://github.com/Vap0r1ze/aoc-large-inputs/raw/main/dist/{day:02}/output_large.txt")) .send() .await .expect("Unable to make input request") .error_for_status() .ok() { Some(r) => Some( r.text() .await .expect("Unable to parse string from input response") .trim_end() .to_owned() .lines() .nth((part - 1) as usize) .expect("Can't get part-th line of large output") .to_string() ), _ => None } ) } pub async fn fetch_example(auth: String, day: u8, part: u8) -> (String, Option) { let html = reqwest::Client::new() .get(format!("https://adventofcode.com/2023/day/{day}")) .header("Cookie", format!("session={auth}")) .send() .await .expect("Unable to make input request") .text() .await .expect("Unable to parse string from input response"); let dom = Html::parse_document(&html); let input_selector = Selector::parse("pre > code").unwrap(); let solution_selector = Selector::parse(match part { 1 => "article:nth-child(1) code > em", 2 => "article:nth-child(3) code > em", _ => panic!(), }) .unwrap(); let input = dom .select(&input_selector) .next() .unwrap() .inner_html() .trim_end() .to_string(); let solution = dom .select(&solution_selector) .last() .map(|s| s.inner_html().trim_end().to_string()); (input, solution) } #[cfg(test)] mod tests { use crate::fetcher; #[tokio::test] async fn fetches_large_data() { fetcher::fetch_large(6, 1).await; } }