chore(spotify): refactor spotify wrapper & function names
This commit is contained in:
		
							parent
							
								
									1cda3984d8
								
							
						
					
					
						commit
						c2307b53a7
					
				
							
								
								
									
										11
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								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!( | ||||
|  | ||||
| @ -9,6 +9,25 @@ pub enum SpotifyKind { | ||||
|     Playlist(String), | ||||
| } | ||||
| 
 | ||||
| pub struct TrackInfo { | ||||
|     pub(crate) name: String, | ||||
|     pub(crate) artists: Vec<String>, | ||||
|     pub(crate) duration: Duration, | ||||
| } | ||||
| 
 | ||||
| pub struct AlbumInfo { | ||||
|     pub(crate) name: String, | ||||
|     pub(crate) artists: Vec<String>, | ||||
|     pub(crate) genres: Vec<String>, | ||||
|     pub(crate) tracks: Vec<TrackInfo>, | ||||
| } | ||||
| 
 | ||||
| pub struct PlaylistInfo { | ||||
|     pub(crate) name: String, | ||||
|     pub(crate) artists: Vec<String>, | ||||
|     pub(crate) tracks: Vec<TrackInfo>, | ||||
| } | ||||
| 
 | ||||
| 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<SpotifyKind> { | ||||
| fn extract_artists_from_tracks(tracks: Vec<TrackSimplified>) -> Vec<String> { | ||||
|     tracks | ||||
|         .iter() | ||||
|         .flat_map(|t| t.artists.iter().map(|a| a.name.clone())) | ||||
|         .collect() | ||||
| } | ||||
| 
 | ||||
| pub fn get_entry_kind(url: &str) -> Option<SpotifyKind> { | ||||
|     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<SpotifyKind> { | ||||
|     return None; | ||||
| } | ||||
| 
 | ||||
| pub fn get_spotify_client() -> Box<Client> { | ||||
| pub fn get_client() -> Box<Client> { | ||||
|     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<String>, | ||||
|     pub(crate) duration: Duration, | ||||
| } | ||||
| 
 | ||||
| pub struct AlbumInfo { | ||||
|     pub(crate) name: String, | ||||
|     pub(crate) artists: Vec<String>, | ||||
|     pub(crate) genres: Vec<String>, | ||||
|     pub(crate) tracks: Vec<TrackInfo>, | ||||
| } | ||||
| 
 | ||||
| pub struct PlaylistInfo { | ||||
|     pub(crate) name: String, | ||||
|     pub(crate) artists: Vec<String>, | ||||
|     pub(crate) tracks: Vec<TrackInfo>, | ||||
| } | ||||
| 
 | ||||
| pub async fn get_spotify_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> { | ||||
| pub async fn get_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> { | ||||
|     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<Client>, id: &String) -> Option<Trac | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub async fn get_spotify_album(spotify: Box<Client>, id: &String) -> Option<AlbumInfo> { | ||||
| pub async fn get_album(spotify: Box<Client>, id: &String) -> Option<AlbumInfo> { | ||||
|     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<Client>, id: &String) -> Option<Albu | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| fn extract_artists_from_tracks(tracks: Vec<TrackSimplified>) -> Vec<String> { | ||||
|     tracks | ||||
|         .iter() | ||||
|         .flat_map(|t| t.artists.iter().map(|a| a.name.clone())) | ||||
|         .collect() | ||||
| } | ||||
| 
 | ||||
| pub async fn get_spotify_playlist(spotify: Box<Client>, id: &String) -> Option<PlaylistInfo> { | ||||
| pub async fn get_playlist(spotify: Box<Client>, id: &String) -> Option<PlaylistInfo> { | ||||
|     match spotify.playlists().get_playlist(id.as_str(), None).await { | ||||
|         Ok(playlist) => Some(PlaylistInfo { | ||||
|             name: playlist.data.name, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user