aboutsummaryrefslogblamecommitdiffstats
path: root/src/main.rs
blob: 1a2aab7cda9f42a7910668d3337bb4e341ff50ce (plain) (tree)
1
2
3
4
5
6
7
8
9

                     
                                                        
                                     
                                       
                         
                                
                    
                                              
                                    



                    











































                                                         

                             

                                                                          
                                                          










                                                         


                     
                                                                                       
     






                                            




                                                     

                                                      



                                                                                    

    

                                                     









                                                                                               
                                       





                                                                      


                                              


                                               
                                           
                                        

















                                                              
use std::borrow::Cow;

use listenbrainz::raw::response::UserPlayingNowResponse;
use mastodon_async::{Data, Mastodon};
use rocket::fs::{relative, FileServer};
use rocket::http::Status;
use rocket::response::Responder;
use rocket::Request;
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();
    let listenbrainz_data = ScrobbleData::new(playingnow);

    let mut skinny_data = Data::default();
    skinny_data.base = Cow::from("https://skinnyver.se");
    let skinny_client = Mastodon::from(skinny_data);
    let mut skweets = skinny_client
        .statuses("cel", Default::default())
        .await
        .unwrap()
        .initial_items;

    skweets.truncate(6);

    Template::render(
        "home",
        context! { is_live: false, listenbrainz: listenbrainz_data, skweets: skweets },
    )
}

#[get("/contact")]
async fn contact() -> Template {
    Template::render("contact", context! {})
}

#[get("/plants")]
async fn plants() -> Result<Template, BlossomError> {
    todo!()
}

// #[get("/test")]
// async fn test() -> Result<Template, BlossomError> {
//     let posts = reqwest::get("https://skinnyver.se/api/v1/accounts/cel/statuses")
//         .await?
//         .json()
//         .await?;
// }

#[catch(default)]
fn error(status: Status, req: &Request) -> Template {
    let message;
    if status.code == 404 {
        message = "i either haven't built this page yet or it looks like you're a little lost";
    } else if status.code == 500 {
        message = "omg the server went kaputt!!";
    } else if status.code == 501 {
        message = "it looks like this is not yet here!!!";
    } else {
        message = "there was an error";
    }
    let status = format!("{}", status);
    Template::render(
        "error",
        context! { status: status, req: req.uri(), message: message },
    )
}

#[tokio::main]
async fn main() -> Result<(), rocket::Error> {
    let _rocket = rocket::build()
        .attach(Template::custom(|engines| {
            engines.tera.autoescape_on(vec![]);
        }))
        .mount("/", routes![home, contact])
        .register("/", catchers![error])
        .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)
    }
}