~aleteoryx/lfm_embed

ref: ab822e81f8e06fbdc274090fc0a3fb31ef7f7ed1 lfm_embed/src/config.rs -rw-r--r-- 6.2 KiB
ab822e81alyx pull down user info 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::sync::LazyLock;
use std::sync::Arc;
use std::future::Future;
use std::pin::Pin;
use std::fs;
use std::time::*;
use duration_str as ds;

use super::cache::AsyncCache;
use super::deserialize::{GetRecentTracks, GetUserInfo, Track, User};

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), (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!")); }

        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>},
    Open{default_cache: Cache, whitelist_cache: Cache, whitelist: HashSet<String>}
}
#[derive(Debug)]
pub struct State {
    api_key: Arc<str>,
    whitelist: Whitelist,
    port: u16,
    themes: HashMap<String, Arc<str>>,
    send_refresh_header: bool,
    http: Client
}

impl State {
    fn new() -> Arc<Self> {
        Arc::new(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(),
            
            themes: {
                if let Ok(themes_dir) = var("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();
                            if fs::metadata(&path).map(|m| m.is_file()).unwrap_or(false) &&
                                path.extension() == Some("css".as_ref()) {
                                    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() }
            },
            
            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 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);
                
                let load_whitelist = || -> Option<HashSet<String>> {
                    var("LFME_WHITELIST").ok().map(
                        |w| w.split(",").map(|s| s.trim().to_string()).collect()                           
                    )
                };
                
                match var("LFME_WHITELIST_MODE").map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()).as_str() {
                    "open" => {
                        Whitelist::Open{default_cache: default_cache(), whitelist_cache: whitelist_cache(), whitelist: load_whitelist().unwrap_or_default()}
                    },
                    "exclusive" => {
                        Whitelist::Exclusive{cache: whitelist_cache(), whitelist: load_whitelist().expect("LFME_WHITELIST not set, unable to serve anyone")}
                    },
                    m => {
                        panic!("Bad whitelist mode: `{m}`");
                    }
                }
            }
        })
    }
    
    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() }
}