fix: aoc request cookies
This commit is contained in:
@@ -5,6 +5,9 @@ year = YEAR_HERE
|
|||||||
# leaderboard's ID
|
# leaderboard's ID
|
||||||
leaderboard = ID_HERE
|
leaderboard = ID_HERE
|
||||||
|
|
||||||
|
# session from your browser's cookies
|
||||||
|
session = "SESSION HERE"
|
||||||
|
|
||||||
# how long to retain successful leaderboard responses
|
# how long to retain successful leaderboard responses
|
||||||
# validity = "15 minutes"
|
# validity = "15 minutes"
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
[advent]
|
[advent]
|
||||||
year = 0
|
year = 0
|
||||||
leaderboard = 0
|
leaderboard = 0
|
||||||
|
session = ""
|
||||||
validity = "5s"
|
validity = "5s"
|
||||||
fail_validity = "2s"
|
fail_validity = "2s"
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ use std::{
|
|||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(serde::Deserialize, Debug, Clone, Copy)]
|
#[derive(serde::Deserialize, Debug, Clone)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct AdventConfig {
|
pub struct AdventConfig {
|
||||||
pub year: u16,
|
pub year: u16,
|
||||||
pub leaderboard: u32,
|
pub leaderboard: u32,
|
||||||
|
pub session: String,
|
||||||
|
|
||||||
#[serde(with = "humantime_serde")]
|
#[serde(with = "humantime_serde")]
|
||||||
#[serde(default = "AdventConfig::default_validity")]
|
#[serde(default = "AdventConfig::default_validity")]
|
||||||
@@ -33,12 +34,16 @@ impl AdventConfig {
|
|||||||
if let Some(data) = option_env!("AOC_TEST_DATA") {
|
if let Some(data) = option_env!("AOC_TEST_DATA") {
|
||||||
Ok(data.to_string())
|
Ok(data.to_string())
|
||||||
} else {
|
} else {
|
||||||
// TODO: we are missing session cookies for aoc to reply
|
use reqwest::header;
|
||||||
let res = reqwest::get(format!(
|
|
||||||
|
let res = reqwest::Client::new()
|
||||||
|
.get(format!(
|
||||||
"https://adventofcode.com/{YEAR}/leaderboard/private/view/{ID}.json",
|
"https://adventofcode.com/{YEAR}/leaderboard/private/view/{ID}.json",
|
||||||
YEAR = self.year,
|
YEAR = self.year,
|
||||||
ID = self.leaderboard
|
ID = self.leaderboard
|
||||||
))
|
))
|
||||||
|
.header(header::COOKIE, format!("session={}", self.session))
|
||||||
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let res = res.error_for_status()?;
|
let res = res.error_for_status()?;
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ impl LeaderboardState {
|
|||||||
/// Fetches the leaderboard and returns the instant when the cached response expires. Failures
|
/// Fetches the leaderboard and returns the instant when the cached response expires. Failures
|
||||||
/// are also cached for their own amount of time.
|
/// are also cached for their own amount of time.
|
||||||
async fn fetch_params(
|
async fn fetch_params(
|
||||||
params: crate::config::AdventConfig,
|
params: &crate::config::AdventConfig,
|
||||||
) -> (Instant, LeaderboardWatchedInfo) {
|
) -> (Instant, LeaderboardWatchedInfo) {
|
||||||
match params.fetch().await {
|
match params.fetch().await {
|
||||||
Ok(data) => (
|
Ok(data) => (
|
||||||
@@ -78,7 +78,7 @@ impl LeaderboardState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn new(params: crate::config::AdventConfig) -> Self {
|
pub async fn new(params: crate::config::AdventConfig) -> Self {
|
||||||
let (init_expiry, info) = Self::fetch_params(params).await;
|
let (init_expiry, info) = Self::fetch_params(¶ms).await;
|
||||||
let info = watch::Sender::new(info);
|
let info = watch::Sender::new(info);
|
||||||
|
|
||||||
let listener_attach = Arc::new(Notify::new());
|
let listener_attach = Arc::new(Notify::new());
|
||||||
@@ -96,7 +96,7 @@ impl LeaderboardState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("trace: updating cached object");
|
eprintln!("trace: updating cached object");
|
||||||
let (new_expiry, new_params) = Self::fetch_params(params).await;
|
let (new_expiry, new_params) = Self::fetch_params(¶ms).await;
|
||||||
next_expiry = new_expiry;
|
next_expiry = new_expiry;
|
||||||
_ = sender.send_replace(new_params); // returns previous
|
_ = sender.send_replace(new_params); // returns previous
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user