From 859f9fe9f8225e7bc3dcf769d3704ce3614db6bf Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Mon, 16 Jan 2023 05:18:41 +0000 Subject: make listenbrainz widget --- Cargo.lock | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++-- templates/home.html.tera | 4 ++-- 4 files changed, 126 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74e154b..c2e24d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aead" version = "0.5.1" @@ -96,6 +102,21 @@ dependencies = [ "autocfg", ] +[[package]] +name = "attohttpc" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b85f766c20e6ae766956f7a2fcc4e0931e79a7e1f48b29132b5d647021114914" +dependencies = [ + "flate2", + "http", + "log", + "native-tls", + "serde", + "serde_json", + "url", +] + [[package]] name = "atty" version = "0.2.14" @@ -283,6 +304,15 @@ dependencies = [ "libc", ] +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -447,6 +477,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "flate2" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "fnv" version = "1.0.7" @@ -962,6 +1002,17 @@ dependencies = [ "cc", ] +[[package]] +name = "listenbrainz" +version = "0.5.0" +source = "git+https://sectorinf.com/cel/listenbrainz-rs.git#b0cd2d6b8aa3d56fa3308a741cadc365d3683b6c" +dependencies = [ + "attohttpc", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "lock_api" version = "0.4.9" @@ -1017,6 +1068,15 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "miniz_oxide" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.6.23" @@ -1840,9 +1900,11 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" name = "site" version = "0.1.0" dependencies = [ + "listenbrainz", "reqwest", "rocket", "rocket_dyn_templates", + "serde", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index c584041..c05bd31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,6 @@ edition = "2021" rocket = { version = "0.5.0-rc.2", features = ["json"] } reqwest = { version = "0.11", features = ["json"] } tokio = { version = "1", features = ["full"] } +serde = { version = "1.0", features = ["derive"] } rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["tera"] } +listenbrainz = { git = "https://sectorinf.com/cel/listenbrainz-rs.git" } 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")] diff --git a/templates/home.html.tera b/templates/home.html.tera index 7ffd4aa..3396894 100644 --- a/templates/home.html.tera +++ b/templates/home.html.tera @@ -20,8 +20,8 @@ {% endif %} -{% if is_scrobbling %} -now playing: Parfum d'étoiles - Ichiko Aoba +{% if listenbrainz.is_scrobbling %} +now playing: {{ listenbrainz.song }} - {{ listenbrainz.artist }} {% endif %} {% endblock header %} -- cgit