diff options
author | cel 🌸 <cel@blos.sm> | 2023-01-16 05:18:41 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-01-16 05:18:41 +0000 |
commit | 859f9fe9f8225e7bc3dcf769d3704ce3614db6bf (patch) | |
tree | ac72f7a62862bae02669784a33c12980826b10d1 /src | |
parent | 3ac85babe9158c9557289c06cc78710384069177 (diff) | |
download | blossom-859f9fe9f8225e7bc3dcf769d3704ce3614db6bf.tar.gz blossom-859f9fe9f8225e7bc3dcf769d3704ce3614db6bf.tar.bz2 blossom-859f9fe9f8225e7bc3dcf769d3704ce3614db6bf.zip |
make listenbrainz widget
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 65a40c4..7e61f15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,71 @@ +use listenbrainz::raw::response::UserPlayingNowResponse; use rocket::fs::{relative, FileServer}; -use rocket::{response::Responder, serde::json::Json}; +use rocket::response::Responder; use rocket_dyn_templates::{context, Template}; +use serde::{Deserialize, Serialize}; #[macro_use] extern crate rocket; +#[derive(Serialize, Deserialize)] +struct ScrobbleData { + is_scrobbling: bool, + song: Option<String>, + artist: Option<String>, +} + +impl ScrobbleData { + 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, + } + } + } +} + #[get("/")] async fn home() -> Template { - Template::render("home", context! { is_live: true, is_scrobbling: true }) + let listenbrainz = listenbrainz::raw::Client::new(); + let playingnow = listenbrainz.user_playing_now("celblossom").unwrap(); + println!( + "{}", + playingnow.payload.listens[0].track_metadata.artist_name + ); + let listenbrainz_data = ScrobbleData::new(playingnow); + println!("{}", listenbrainz_data.is_scrobbling); + + Template::render( + "home", + context! { is_live: false, listenbrainz: listenbrainz_data }, + ) } #[get("/contact")] |