From c2307b53a7576f1103acc61b90c3d8b5399d1268 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Wed, 10 Nov 2021 14:35:02 +0100 Subject: [PATCH] chore(spotify): refactor spotify wrapper & function names --- src/main.rs | 11 ++++---- src/spotify/mod.rs | 62 +++++++++++++++++++++++----------------------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index c1ff728..d1b3186 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,14 +14,14 @@ async fn main() { let bot = Bot::from_env().auto_send(); teloxide::repl(bot, |message| async move { - let text = message.update.text().and_then(spotify::get_spotify_entry); + let text = message.update.text().and_then(spotify::get_entry_kind); match text { Some(spotify) => { - let spotify_client = spotify::get_spotify_client(); + let spotify_client = spotify::get_client(); match spotify { Track(id) => { log::debug!("Parsing spotify song: {}", id); - let track_info = spotify::get_spotify_track(spotify_client, &id).await; + let track_info = spotify::get_track(spotify_client, &id).await; match track_info { Some(info) => { let reply = format!( @@ -40,7 +40,7 @@ async fn main() { } Album(id) => { log::debug!("Parsing spotify album: {}", id); - let album_info = spotify::get_spotify_album(spotify_client, &id).await; + let album_info = spotify::get_album(spotify_client, &id).await; match album_info { Some(info) => { let mut reply = format!( @@ -68,8 +68,7 @@ async fn main() { } Playlist(id) => { log::debug!("Parsing spotify playlist: {}", id); - let playlist_info = - spotify::get_spotify_playlist(spotify_client, &id).await; + let playlist_info = spotify::get_playlist(spotify_client, &id).await; match playlist_info { Some(info) => { let reply = format!( diff --git a/src/spotify/mod.rs b/src/spotify/mod.rs index d77ef4c..0606aa9 100644 --- a/src/spotify/mod.rs +++ b/src/spotify/mod.rs @@ -9,6 +9,25 @@ pub enum SpotifyKind { Playlist(String), } +pub struct TrackInfo { + pub(crate) name: String, + pub(crate) artists: Vec, + pub(crate) duration: Duration, +} + +pub struct AlbumInfo { + pub(crate) name: String, + pub(crate) artists: Vec, + pub(crate) genres: Vec, + pub(crate) tracks: Vec, +} + +pub struct PlaylistInfo { + pub(crate) name: String, + pub(crate) artists: Vec, + pub(crate) tracks: Vec, +} + fn get_id_in_url(url: &str) -> Option<&str> { url.rsplit('/') .next() @@ -16,7 +35,14 @@ fn get_id_in_url(url: &str) -> Option<&str> { .and_then(|x| x.split('?').next()) } -pub fn get_spotify_entry(url: &str) -> Option { +fn extract_artists_from_tracks(tracks: Vec) -> Vec { + tracks + .iter() + .flat_map(|t| t.artists.iter().map(|a| a.name.clone())) + .collect() +} + +pub fn get_entry_kind(url: &str) -> Option { if url.contains("https://open.spotify.com/track/") { let track_id = get_id_in_url(url); return match track_id { @@ -41,33 +67,14 @@ pub fn get_spotify_entry(url: &str) -> Option { return None; } -pub fn get_spotify_client() -> Box { +pub fn get_client() -> Box { let spotify_creds = ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found."); let spotify_client = Box::new(Client::new(spotify_creds)); spotify_client } -pub struct TrackInfo { - pub(crate) name: String, - pub(crate) artists: Vec, - pub(crate) duration: Duration, -} - -pub struct AlbumInfo { - pub(crate) name: String, - pub(crate) artists: Vec, - pub(crate) genres: Vec, - pub(crate) tracks: Vec, -} - -pub struct PlaylistInfo { - pub(crate) name: String, - pub(crate) artists: Vec, - pub(crate) tracks: Vec, -} - -pub async fn get_spotify_track(spotify: Box, id: &String) -> Option { +pub async fn get_track(spotify: Box, id: &String) -> Option { match spotify.tracks().get_track(id.as_str(), None).await { Ok(track) => Some(TrackInfo { name: track.data.name, @@ -78,7 +85,7 @@ pub async fn get_spotify_track(spotify: Box, id: &String) -> Option, id: &String) -> Option { +pub async fn get_album(spotify: Box, id: &String) -> Option { match spotify.albums().get_album(id.as_str(), None).await { Ok(album) => Some(AlbumInfo { name: album.data.name, @@ -100,14 +107,7 @@ pub async fn get_spotify_album(spotify: Box, id: &String) -> Option) -> Vec { - tracks - .iter() - .flat_map(|t| t.artists.iter().map(|a| a.name.clone())) - .collect() -} - -pub async fn get_spotify_playlist(spotify: Box, id: &String) -> Option { +pub async fn get_playlist(spotify: Box, id: &String) -> Option { match spotify.playlists().get_playlist(id.as_str(), None).await { Ok(playlist) => Some(PlaylistInfo { name: playlist.data.name,