make listenbrainz widget

This commit is contained in:
cel 🌸 2023-01-16 05:18:41 +00:00
parent 3ac85babe9
commit 859f9fe9f8
Signed by: cel
GPG Key ID: 48E29AF13B5F1349
4 changed files with 126 additions and 4 deletions

62
Cargo.lock generated
View File

@ -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",
]

View File

@ -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" }

View File

@ -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")]

View File

@ -20,8 +20,8 @@
</iframe>
{% endif %}
{% if is_scrobbling %}
<a href="https://listenbrainz.org/user/celblossom" class="panel" id="scrobble">now playing: Parfum d'étoiles - Ichiko Aoba</a>
{% if listenbrainz.is_scrobbling %}
<a href="https://listenbrainz.org/user/celblossom" class="panel" id="scrobble">now playing: {{ listenbrainz.song }} - {{ listenbrainz.artist }}</a>
{% endif %}
{% endblock header %}