advent-of-code-2023/src/fetcher.rs

59 lines
1.7 KiB
Rust
Raw Normal View History

use scraper::{Html, Selector};
pub async fn fetch_input(auth: String, day: u8) -> String {
2023-12-05 00:03:50 -07:00
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()
2023-12-05 00:03:50 -07:00
.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_example(auth: String, day: u8, part: u8) -> (String, String) {
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()
2023-12-05 00:03:50 -07:00
.expect("Unable to find example solution for part, is auth correct?")
.inner_html()
.trim_end()
.to_string();
(input, solution)
}