diff options
author | cel 🌸 <cel@blos.sm> | 2023-02-15 18:08:23 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-02-15 18:08:23 +0000 |
commit | b8bbceb00be3ba01cd14a111f4dfc407880f493e (patch) | |
tree | a5a4897e10f992b70c05bb494c4eb3c1c09091e8 /src/scrobbles.rs | |
parent | fce07d2749f2e5fad3e4925919233d2f3796b684 (diff) | |
download | blossom-b8bbceb00be3ba01cd14a111f4dfc407880f493e.tar.gz blossom-b8bbceb00be3ba01cd14a111f4dfc407880f493e.tar.bz2 blossom-b8bbceb00be3ba01cd14a111f4dfc407880f493e.zip |
refactor rust into separate files and add proper error handling
Diffstat (limited to 'src/scrobbles.rs')
-rw-r--r-- | src/scrobbles.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/scrobbles.rs b/src/scrobbles.rs new file mode 100644 index 0000000..07b1ad1 --- /dev/null +++ b/src/scrobbles.rs @@ -0,0 +1,55 @@ +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, + } + } + } +} |