Properly parse the data

This commit is contained in:
Savanni D'Gerinel 2022-11-26 23:01:00 -05:00
parent 576df264f9
commit e37add37df
3 changed files with 79 additions and 18 deletions

22
challenge/Cargo.lock generated
View File

@ -49,6 +49,9 @@ name = "challenge"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"reqwest", "reqwest",
"serde",
"serde_json",
"serde_yaml",
"tokio", "tokio",
] ]
@ -721,6 +724,19 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_yaml"
version = "0.9.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d232d893b10de3eb7258ff01974d6ee20663d8e833263c99409d4b13a0209da"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.0" version = "1.4.0"
@ -930,6 +946,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unsafe-libyaml"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"

View File

@ -6,5 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
reqwest = { version = "0.11", features = [ "cookies" ] } reqwest = { version = "0.11", features = [ "cookies", "json" ] }
tokio = { version = "1.22", features = [ "full" ] } tokio = { version = "1.22", features = [ "full" ] }
serde = { version = "1.0" }
serde_json = { version = "1.0" }
serde_yaml = { version = "0.9" }

View File

@ -1,20 +1,32 @@
use reqwest; use reqwest;
use serde::Deserialize;
use std::collections::HashMap;
#[tokio::main] const INPUT_DATA: &str = include_str!("../680754.json");
async fn main() { const TEAMS: &str = include_str!("../teams.yaml");
let session_cookie = std::env::vars()
.find_map(|(key, value)| { #[derive(Clone, Debug, Deserialize)]
if key == "SESSION_COOKIE" { struct Board {
Some(value) members: HashMap<String, Member>,
} else {
None
} }
})
.unwrap();
#[derive(Clone, Debug, Deserialize)]
struct Member {
local_score: u32,
name: String,
stars: u32,
}
#[derive(Clone, Debug, Deserialize)]
struct Team {
name: String,
members: Vec<String>,
}
async fn request_board(session_token: &str, year: &str, board_id: &str) -> Board {
let jar = reqwest::cookie::Jar::default(); let jar = reqwest::cookie::Jar::default();
let url = "https://adventofcode.com/".parse::<reqwest::Url>().unwrap(); let url = "https://adventofcode.com/".parse::<reqwest::Url>().unwrap();
jar.add_cookie_str(&format!("session={}", session_cookie), &url); jar.add_cookie_str(&format!("session={}", session_token), &url);
let client = reqwest::ClientBuilder::new() let client = reqwest::ClientBuilder::new()
.cookie_store(true) .cookie_store(true)
@ -22,13 +34,37 @@ async fn main() {
.build() .build()
.unwrap(); .unwrap();
let board = client client
.get("https://adventofcode.com/2021/leaderboard/private/view/680754.json") .get(format!(
"https://adventofcode.com/{}/leaderboard/private/view/{}.json",
year, board_id
))
.send() .send()
.await .await
.unwrap() .unwrap()
.text() .json()
.await; .await
.unwrap()
}
#[tokio::main]
async fn main() {
/*
let session_token = std::env::vars()
.find_map(|(key, value)| {
if key == "AOC_SESSION_TOKEN" {
Some(value)
} else {
None
}
})
.unwrap();
let board = request_board(&session_token, "2022", "680754");
*/
let teams: Vec<Team> = serde_yaml::from_str(TEAMS).unwrap();
let board: Board = serde_json::from_str(INPUT_DATA).unwrap();
println!("resulting text: {:?}", teams);
println!("resulting text: {:?}", board); println!("resulting text: {:?}", board);
} }