use std::env; use std::fmt::format; use axum::routing::get; use axum::{Extension, Router}; use axum::extract::Query; use axum::response::IntoResponse; use lastfm_vore::{LastfmApiError, LastfmClient}; use serde::Deserialize; use lastfm_vore::auth::get_session::GetSessionResponse; #[derive(Clone)] struct Context { lastfm: LastfmClient, } #[tokio::main] async fn main() { let key = env::var("API_KEY").expect("API_KEY does not exist"); let secret_key = env::var("SECRET_KEY").expect("SECRET_KEY does not exist"); let lastfm_client = LastfmClient::new(&key, &secret_key); let app = Router::new() .route("/callback", get(callback)) .layer(Extension(Context { lastfm: lastfm_client })); let listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await.unwrap(); println!("wait a couple seconds and then head over to https://www.last.fm/api/auth/?api_key={}", key); axum::serve(listener, app).await.unwrap(); } #[derive(Deserialize, Debug)] struct CallbackQuery { token: String, } async fn callback( Extension(ctx): Extension, Query(query): Query, ) -> String { let res = lastfm_vore::auth::get_session::GetSession::send(&ctx.lastfm, query.token).await; match res { Ok(res) => { let res_two = lastfm_vore::user::get_recent_tracks::GetRecentTracks::send(&ctx.lastfm, res.session.name.clone(), None, None, None, None, true).await; match res_two { Ok(res_two) => { let res_two = res_two.recenttracks; let track = res_two.track.first().unwrap(); let artist = track.artist.clone().unwrap(); let mut format_string = format!("Hello {}! ", res.session.name); if track.is_playing() { format_string.push_str("You're currently playing: "); } else { format_string.push_str("You last listened to: "); } format_string.push_str(format!("{} by {} ", track.name, artist.get_artist_name()).as_str()); if let Some(album) = track.album.clone() { format_string.push_str(format!("on {}", album.text).as_str()); } format_string.push_str("!"); format_string } Err(e) => { e.to_string() } } } Err(e) => { e.to_string() } } }