aboutsummaryrefslogtreecommitdiffstats
path: root/src/scrobbles.rs
blob: d4f6d31c4f60be009c0264dd36c5fc166f0bf2bd (plain) (blame)
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
use listenbrainz::raw::response::UserPlayingNowResponse;
use serde::{Deserialize, Serialize};

use crate::Result;

pub async fn get_now_playing(client: &listenbrainz::raw::Client) -> Result<NowPlayingData> {
    let playingnow = client.user_playing_now("celblossom")?;
    Ok(NowPlayingData::new(playingnow))
}

#[derive(Serialize, Deserialize, Default)]
pub struct NowPlayingData {
    pub is_scrobbling: bool,
    pub song: Option<String>,
    pub artist: Option<String>,
}

impl NowPlayingData {
    fn new(playingnow: UserPlayingNowResponse) -> Self {
        let is_scrobbling = playingnow.payload.count > 0;
        if is_scrobbling {
            Self {
                is_scrobbling,
                song: Some(
                    playingnow
                        .payload
                        .listens
                        .first()
                        .unwrap()
                        .track_metadata
                        .track_name
                        .clone(),
                ),
                artist: Some(
                    playingnow
                        .payload
                        .listens
                        .first()
                        .unwrap()
                        .track_metadata
                        .artist_name
                        .clone(),
                ),
            }
        } else {
            Self {
                is_scrobbling,
                song: None,
                artist: None,
            }
        }
    }
}