aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cache.rs15
-rw-r--r--src/config.rs50
2 files changed, 46 insertions, 19 deletions
diff --git a/src/cache.rs b/src/cache.rs
index 482a9b8..dfe23e7 100644
--- a/src/cache.rs
+++ b/src/cache.rs
@@ -1,4 +1,5 @@
use std::{future::Future, time::*, collections::HashMap, hash::Hash};
+use reqwest::StatusCode;
#[derive(Debug)]
pub struct AsyncCache<K, V, F> {
func: F,
@@ -8,9 +9,9 @@ pub struct AsyncCache<K, V, F> {
impl<K, V, F, Fut> AsyncCache<K, V, F>
where
- F: for<'a> FnMut(&'a K) -> Fut,
+ for<'a> F: FnMut(&'a K) -> Fut + 'a,
K: Hash + PartialEq + Eq + Clone,
- Fut: Future<Output = Result<V, &'static str>>
+ Fut: Future<Output = Result<V, (StatusCode, &'static str)>>
{
pub fn new(interval: Duration, func: F) -> Self {
Self{
@@ -19,7 +20,7 @@ where
}
}
- pub async fn get(&mut self, key: &K) -> Result<&V, &'static str> {
+ pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> {
if self.is_stale(&key) {
self.renew(&key).await
} else {
@@ -27,7 +28,7 @@ where
}
}
- pub async fn renew(&mut self, key: &K) -> Result<&V, &'static str> {
+ pub async fn renew(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> {
let val = (self.func)(&key).await?;
self.cache.insert(key.clone(), (Instant::now(), val));
Ok(&self.cache.get(key).unwrap().1)
@@ -51,12 +52,12 @@ where
impl<K, V, F, Fut> AsyncCache<K, V, F>
where
- F: for<'a> FnMut(&'a K) -> Fut,
+ for<'a> F: FnMut(&'a K) -> Fut + 'a,
K: Hash + PartialEq + Eq + Clone,
V: Clone,
- Fut: Future<Output = Result<V, &'static str>>
+ Fut: Future<Output = Result<V, (StatusCode, &'static str)>>
{
- pub async fn get_owned(&mut self, key: &K) -> Result<V, &'static str> {
+ pub async fn get_owned(&mut self, key: &K) -> Result<V, (StatusCode, &'static str)> {
self.get(key).await.cloned()
}
}
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<Arc<State>> = LazyLock::new(|| {
State::new()
});
-fn getter(username: &String) -> Pin<Box<dyn Future<Output = Result<(User, Track), &'static str>>>> {
- Box::pin(async{Err("nope")})
-}
+fn getter(username: &String) -> Pin<Box<dyn Future<Output = Result<(User, Track), (StatusCode, &'static str)>>>> {
+ 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::<GetUserInfo>().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<RwLock<AsyncCache<String, (User, Track), fn(&String) -> Pin<Box<dyn Future<Output = Result<(User, Track), &'static str>>>>>>>;
+ let tracksinfo = tracksreq.json::<GetRecentTracks>().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<Box<(dyn Future<Output = Result<(User, Track), (StatusCode, &'static str)>>)>>;
+type Cache = Arc<RwLock<AsyncCache<String, (User, Track), Getter>>>;
#[derive(Debug)]
enum Whitelist {
Exclusive{cache: Cache, whitelist: HashSet<String>},
@@ -34,8 +57,9 @@ pub struct State {
api_key: Arc<str>,
whitelist: Whitelist,
port: u16,
- custom_themes: HashMap<String, Arc<str>>,
- send_refresh_header: bool
+ themes: HashMap<String, Arc<str>>,
+ 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<String, (User, Track), Getter>))
};
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<str> { 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<Arc<str>> { self.themes.get(theme).cloned() }
}