2023-01-16 05:18:41 +00:00
|
|
|
use listenbrainz::raw::response::UserPlayingNowResponse;
|
2023-01-15 21:43:21 +00:00
|
|
|
use rocket::fs::{relative, FileServer};
|
2023-01-16 05:18:41 +00:00
|
|
|
use rocket::response::Responder;
|
2023-01-15 21:43:21 +00:00
|
|
|
use rocket_dyn_templates::{context, Template};
|
2023-01-16 05:18:41 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-01-15 21:43:21 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
|
2023-01-16 05:18:41 +00:00
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:43:21 +00:00
|
|
|
#[get("/")]
|
|
|
|
async fn home() -> Template {
|
2023-01-16 05:18:41 +00:00
|
|
|
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 },
|
|
|
|
)
|
2023-01-15 21:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/contact")]
|
|
|
|
async fn contact() -> Template {
|
|
|
|
Template::render("contact", context! {})
|
|
|
|
}
|
|
|
|
|
|
|
|
// #[get("/test")]
|
|
|
|
// async fn test() -> Result<Template, BlossomError> {
|
|
|
|
// let posts = reqwest::get("https://skinnyver.se/api/v1/accounts/cel/statuses").await;
|
|
|
|
// posts
|
|
|
|
// }
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), rocket::Error> {
|
|
|
|
let _rocket = rocket::build()
|
|
|
|
.attach(Template::fairing())
|
|
|
|
.mount("/", routes![home, contact])
|
|
|
|
.mount("/", FileServer::from(relative!("static")))
|
|
|
|
.launch()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
enum BlossomError {
|
|
|
|
#[response(status = 500)]
|
|
|
|
Reqwest(&'static str, #[response(ignore)] reqwest::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for BlossomError {
|
|
|
|
fn from(e: reqwest::Error) -> Self {
|
|
|
|
BlossomError::Reqwest("reqwest error", e)
|
|
|
|
}
|
|
|
|
}
|