diff --git a/src/main.rs b/src/main.rs index 94788e5..492b240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,18 @@ use crate::SpotifyURL::Track; use aspotify::{Client, ClientCredentials}; +use std::time::Duration; use teloxide::prelude::*; enum SpotifyURL { Track(String), } +struct TrackInfo { + name: String, + artist: Vec, + duration: Duration, +} + fn get_spotify_entry(url: &str) -> Option { if url.contains("https://open.spotify.com/track/") { let track_id = url.rsplit('/').next().and_then(|x| x.split('?').next()); @@ -17,16 +24,12 @@ fn get_spotify_entry(url: &str) -> Option { return None; } -struct TrackInfo { - name: String, - artist: Vec, -} - async fn get_spotify_track(spotify: Box, id: &String) -> Option { match spotify.tracks().get_track(id.as_str(), None).await { Ok(track) => Some(TrackInfo { name: track.data.name, artist: track.data.artists.iter().map(|x| x.name.clone()).collect(), + duration: track.data.duration, }), Err(_e) => None, } @@ -39,31 +42,34 @@ async fn main() { let bot = Bot::from_env().auto_send(); teloxide::repl(bot, |message| async move { - let spotify_creds = - ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found."); - let spotify_client = Box::new(Client::new(spotify_creds)); - - log::info!("Connected to Spotify"); let text = message.update.text().and_then(get_spotify_entry); match text { - Some(spotify) => match spotify { - Track(id) => { - let track_info = get_spotify_track(spotify_client, &id).await; - match track_info { - Some(info) => { - let reply = format!( - "Track information:\n\ - Track name: {}\n\ - Artists: {}", - info.name, - info.artist.join(", ") - ); - Some(message.reply_to(reply).await?) + Some(spotify) => { + let spotify_creds = + ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found."); + let spotify_client = Box::new(Client::new(spotify_creds)); + match spotify { + Track(id) => { + log::debug!("Parsing spotify song: {}", id); + let track_info = get_spotify_track(spotify_client, &id).await; + match track_info { + Some(info) => { + let reply = format!( + "Track information:\n\ + 🎵 Track name: {}\n\ + 🧑‍🎤 Artist(s): {}\n\ + ⏳ Duration: {} second(s)", + info.name, + info.artist.join(", "), + info.duration.as_secs() + ); + Some(message.reply_to(reply).await?) + } + None => None, } - None => None, } } - }, + } None => None, }; respond(())