aboutsummaryrefslogblamecommitdiffstats
path: root/src/scrobbles.rs
blob: 07b1ad1ebca3ed735a703504b040fba20ddac85d (plain) (tree)






















































                                                            
use listenbrainz::raw::response::UserPlayingNowResponse;
use serde::{Deserialize, Serialize};

use crate::error::BlossomError;

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

#[derive(Serialize, Deserialize)]
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,
            }
        }
    }
}