This commit is contained in:
Julius 2019-12-03 23:22:34 +01:00
parent 72df65e295
commit 777e941469
4 changed files with 2009 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
**/*.rs.bk
.idea/

1908
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

22
Cargo.toml Normal file
View file

@ -0,0 +1,22 @@
[package]
name = "aoclog"
version = "0.1.0"
authors = ["Julius de Jeu <julius@voidcorp.nl>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = "0.9"
[dependencies.serenity]
default-features = false
features = [
"model",
"http",
"utils",
"rustls_backend"
]
version = "0.7"

76
src/main.rs Normal file
View file

@ -0,0 +1,76 @@
extern crate reqwest;
extern crate serde;
extern crate serde_json;
use std::collections::HashMap;
use std::str::FromStr;
use serde::{Deserialize, Deserializer};
use serde::de::{self};
use serde::export::fmt::Display;
use serde_json::Value;
#[derive(Deserialize, PartialEq, Debug)]
struct AOCRequest {
event: String,
owner_id: String,
members: HashMap<u32, User>,
}
#[derive(Deserialize, PartialEq, Debug)]
struct User {
global_score: u32,
name: Value,
stars: u8,
completion_day_level: HashMap<String, HashMap<String, Star>>,
last_star_ts: Value,
#[serde(deserialize_with = "from_str")]
id: u64,
local_score: u64,
}
#[derive(Deserialize, Eq, PartialEq, Debug)]
struct Star {
#[serde(deserialize_with = "from_str")]
get_star_ts: u64
}
fn from_str<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where T: FromStr,
T::Err: Display,
D: Deserializer<'de>
{
let s = String::deserialize(deserializer)?;
T::from_str(&s).map_err(de::Error::custom)
}
fn main() {
let client = reqwest::Client::new();
let json: AOCRequest = client.get("https://adventofcode.com/2019/leaderboard/private/view/356604.json")
.header("Cookie",
"session=lol")
.send().expect("Failed to send request").json().expect("Error parsing response");
//"https://discordapp.com/api/webhooks/651520885349548044/EY0yz2fvWiViMr_mr1AhgTQ2xikC32U_ncEnpAy4sy7zlsq8hDFufYuynUjw22vbLHlQ"
let id = 651_520_885_349_548_044;
let token = "EY0yz2fvWiViMr_mr1AhgTQ2xikC32U_ncEnpAy4sy7zlsq8hDFufYuynUjw22vbLHlQ";
let http = serenity::http::Http::default();
let mut wh = http.as_ref().get_webhook_with_token(id, token).expect("Cannot create webhook");
wh.edit(&http, Some("AOCHook"), Some(include_str!("./faviconlife"))).expect("oof?");
let mut res: Vec<&User> = json.members.iter().filter_map(|(_k, v)| {
if v.name.is_string() {
return Some(v);
}
None
}).collect();
res.sort_by(|x, y| x.local_score.cmp(&y.local_score).reverse());
let k = res.iter().filter_map(|v| v.name.as_str()).max_by_key(|s| s.len()).unwrap_or("").len() + 2;
let l: Vec<String> = res.iter().enumerate().map(|(i, u)| format!("{:0>2} - {:^len$} - {:0>4}", i + 1, u.name.as_str().unwrap(), u.local_score, len = k)).collect();
wh.execute(&http, false, |w| {
w.content(format!("```{}```", l.join("\n")));
w
}).expect("oof");
}