aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 7e61f15ea84ccb680d49f93a78c0a0496ab73358 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use listenbrainz::raw::response::UserPlayingNowResponse;
use rocket::fs::{relative, FileServer};
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 {
    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")]
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)
    }
}