modmapper/src/nexus_api/mod.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

2021-07-09 04:37:08 +00:00
use anyhow::Result;
use chrono::DateTime;
use chrono::Duration;
use chrono::Utc;
use reqwest::Response;
2021-07-11 23:45:26 +00:00
use tracing::info;
2021-07-09 04:37:08 +00:00
pub mod download_link;
pub mod files;
pub static GAME_NAME: &str = "skyrimspecialedition";
pub const GAME_ID: u32 = 1704;
pub static USER_AGENT: &str = "mod-mapper/0.1";
pub fn rate_limit_wait_duration(res: &Response) -> Result<Option<std::time::Duration>> {
let daily_remaining = res
.headers()
.get("x-rl-daily-remaining")
.expect("No daily remaining in response headers");
let hourly_remaining = res
.headers()
.get("x-rl-hourly-remaining")
.expect("No hourly limit in response headers");
let hourly_reset = res
.headers()
.get("x-rl-hourly-reset")
.expect("No hourly reset in response headers");
2021-07-11 23:45:26 +00:00
info!(daily_remaining = ?daily_remaining, hourly_remaining = ?hourly_remaining, "rate limit check");
2021-07-09 04:37:08 +00:00
if hourly_remaining == "0" {
let hourly_reset = hourly_reset.to_str()?.trim();
let hourly_reset: DateTime<Utc> =
(DateTime::parse_from_str(hourly_reset, "%Y-%m-%d %H:%M:%S %z")?
+ Duration::seconds(5))
.into();
let duration = (hourly_reset - Utc::now()).to_std()?;
2021-07-11 23:45:26 +00:00
info!(
hourly_reset = ?hourly_reset,
duration = ?duration, "need to wait until rate-limit hourly reset"
);
2021-07-09 04:37:08 +00:00
return Ok(Some(duration));
}
Ok(None)
}