From ab822e81f8e06fbdc274090fc0a3fb31ef7f7ed1 Mon Sep 17 00:00:00 2001 From: alyx Date: Wed, 9 Aug 2023 16:59:03 -0400 Subject: pull down user info --- src/config.rs | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index ef9c709..bf02592 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,19 +11,42 @@ use duration_str as ds; use super::cache::AsyncCache; use super::deserialize::{GetRecentTracks, GetUserInfo, Track, User}; -use reqwest::Client; +use reqwest::{Client, StatusCode}; use dotenv::var; use tokio::sync::RwLock; +static INTERNAL_THEMES: &[(&'static str, &'static str)] = &[("plain", "")]; + pub static STATE: LazyLock> = LazyLock::new(|| { State::new() }); -fn getter(username: &String) -> Pin>>> { - Box::pin(async{Err("nope")}) -} +fn getter(username: &String) -> Pin>>> { + let username = username.clone(); + Box::pin(async move { + let userreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getInfo&format=json&user={username}&api_key={}", STATE.api_key)) + .send().await + .map_err(|e| {log::error!("Failed to get info for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?; + if userreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); } + + let userinfo = userreq.json::().await + .map_err(|e| {log::error!("Couldn't parse user.getInfo for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getInfo!")})?.user; + + let tracksreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&format=json&extended=1&limit=1&user={username}&api_key={}", STATE.api_key)) + .send().await + .map_err(|e| {log::error!("Failed to get tracks for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?; + if tracksreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); } + if tracksreq.status() == StatusCode::FORBIDDEN { return Err((StatusCode::FORBIDDEN, "You need to unprivate your song history!")); } -type Cache = Arc Pin>>>>>>; + let tracksinfo = tracksreq.json::().await + .map_err(|e| {log::error!("Couldn't parse user.getRecentTracks for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getRecentTracks!")})? + .recenttracks.track.into_iter().nth(0).ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?; + + Ok((userinfo, tracksinfo)) + }) +} +type Getter = fn(&String) -> Pin>)>>; +type Cache = Arc>>; #[derive(Debug)] enum Whitelist { Exclusive{cache: Cache, whitelist: HashSet}, @@ -34,8 +57,9 @@ pub struct State { api_key: Arc, whitelist: Whitelist, port: u16, - custom_themes: HashMap>, - send_refresh_header: bool + themes: HashMap>, + send_refresh_header: bool, + http: Client } impl State { @@ -44,10 +68,11 @@ impl State { api_key: var("LFME_API_KEY").expect("API key must be set").into(), port: var("LFME_PORT").map(|p| p.parse().expect("cannot parse as a port number")).unwrap_or(9999), send_refresh_header: var("LFME_SET_HEADER").map(|h| &h == "1").unwrap_or(false), + http: Client::builder().https_only(true).user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))).build().unwrap(), - custom_themes: { + themes: { if let Ok(themes_dir) = var("LFME_THEMES_DIR") { - fs::read_dir(themes_dir).expect("error reading LFME_THEMES_DIR") + INTERNAL_THEMES.iter().map(|(k, v)| (k.to_string(), (*v).into())).chain(fs::read_dir(themes_dir).expect("error reading LFME_THEMES_DIR") .map(|a| a.expect("error reading LFME_THEMES_DIR")) .filter_map(|a| { let path = a.path(); @@ -56,7 +81,7 @@ impl State { Some((path.file_stem().unwrap().to_str().expect("bad filename").to_string(), fs::read_to_string(&path).expect("couldn't read theme CSS").into())) } else { None } - }) + })) .collect() } else { HashMap::new() } @@ -65,7 +90,7 @@ impl State { whitelist: { let cache_from_var = |v: &str, d: u64| -> Cache { let refresh = var(v).map(|r| ds::parse(&r).expect("bad duration string")).unwrap_or_else(|_| Duration::from_secs(d)); - Arc::new(RwLock::new(AsyncCache::new(refresh, getter as _))) + Arc::new(RwLock::new(AsyncCache::new(refresh, getter as Getter) as AsyncCache)) }; let default_cache = || cache_from_var("LFME_DEFAULT_REFRESH", 300); let whitelist_cache = || cache_from_var("LFME_WHITELIST_REFRESH", 60); @@ -91,6 +116,7 @@ impl State { }) } - pub fn api_key(&self) -> Arc { self.api_key.clone() } pub fn port(&self) -> u16 { self.port } + pub fn send_refresh_header(&self) -> bool { self.send_refresh_header } + pub fn get_theme(&self, theme: &str) -> Option> { self.themes.get(theme).cloned() } } -- cgit v1.2.3-54-g00ecf