chore(spotify): refactor spotify wrapper & function names

pull/1/head
Davide Polonio 2021-11-10 14:35:02 +01:00
parent 1cda3984d8
commit c2307b53a7
2 changed files with 36 additions and 37 deletions

View File

@ -14,14 +14,14 @@ async fn main() {
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();
teloxide::repl(bot, |message| async move { 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 { match text {
Some(spotify) => { Some(spotify) => {
let spotify_client = spotify::get_spotify_client(); let spotify_client = spotify::get_client();
match spotify { match spotify {
Track(id) => { Track(id) => {
log::debug!("Parsing spotify song: {}", 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 { match track_info {
Some(info) => { Some(info) => {
let reply = format!( let reply = format!(
@ -40,7 +40,7 @@ async fn main() {
} }
Album(id) => { Album(id) => {
log::debug!("Parsing spotify 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 { match album_info {
Some(info) => { Some(info) => {
let mut reply = format!( let mut reply = format!(
@ -68,8 +68,7 @@ async fn main() {
} }
Playlist(id) => { Playlist(id) => {
log::debug!("Parsing spotify playlist: {}", id); log::debug!("Parsing spotify playlist: {}", id);
let playlist_info = let playlist_info = spotify::get_playlist(spotify_client, &id).await;
spotify::get_spotify_playlist(spotify_client, &id).await;
match playlist_info { match playlist_info {
Some(info) => { Some(info) => {
let reply = format!( let reply = format!(

View File

@ -9,6 +9,25 @@ pub enum SpotifyKind {
Playlist(String), 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> { fn get_id_in_url(url: &str) -> Option<&str> {
url.rsplit('/') url.rsplit('/')
.next() .next()
@ -16,7 +35,14 @@ fn get_id_in_url(url: &str) -> Option<&str> {
.and_then(|x| x.split('?').next()) .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/") { if url.contains("https://open.spotify.com/track/") {
let track_id = get_id_in_url(url); let track_id = get_id_in_url(url);
return match track_id { return match track_id {
@ -41,33 +67,14 @@ pub fn get_spotify_entry(url: &str) -> Option<SpotifyKind> {
return None; return None;
} }
pub fn get_spotify_client() -> Box<Client> { pub fn get_client() -> Box<Client> {
let spotify_creds = let spotify_creds =
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found."); ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
let spotify_client = Box::new(Client::new(spotify_creds)); let spotify_client = Box::new(Client::new(spotify_creds));
spotify_client spotify_client
} }
pub struct TrackInfo { pub async fn get_track(spotify: Box<Client>, id: &String) -> Option<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> {
match spotify.tracks().get_track(id.as_str(), None).await { match spotify.tracks().get_track(id.as_str(), None).await {
Ok(track) => Some(TrackInfo { Ok(track) => Some(TrackInfo {
name: track.data.name, 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 { match spotify.albums().get_album(id.as_str(), None).await {
Ok(album) => Some(AlbumInfo { Ok(album) => Some(AlbumInfo {
name: album.data.name, 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> { pub async fn get_playlist(spotify: Box<Client>, id: &String) -> Option<PlaylistInfo> {
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> {
match spotify.playlists().get_playlist(id.as_str(), None).await { match spotify.playlists().get_playlist(id.as_str(), None).await {
Ok(playlist) => Some(PlaylistInfo { Ok(playlist) => Some(PlaylistInfo {
name: playlist.data.name, name: playlist.data.name,