From 859f9fe9f8225e7bc3dcf769d3704ce3614db6bf Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Mon, 16 Jan 2023 05:18:41 +0000 Subject: make listenbrainz widget --- src/main.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'src/main.rs') 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, + artist: Option, +} + +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")] -- cgit