diff --git a/src/downloader.rs b/src/downloader.rs index b238bac..a1efa21 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -74,6 +74,12 @@ impl Downloader { let queue: Vec = tracks.into_iter().map(|t| t.into()).collect(); self.add_to_queue_multiple(queue).await; } + SpotifyItem::Artist(a) => { + let tracks = self.spotify.full_artist(&a.id).await?; + let queue: Vec = tracks.into_iter().map(|t| t.into()).collect(); + self.add_to_queue_multiple(queue).await; + } + // Unsupported SpotifyItem::Other(u) => { error!("Unsupported URI: {}", u); diff --git a/src/spotify.rs b/src/spotify.rs index ae13e0e..5376840 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -1,5 +1,5 @@ use aspotify::{ - Album, Client, ClientCredentials, Playlist, PlaylistItemType, Track, TrackSimplified, + Album, Client, ClientCredentials, Playlist, PlaylistItemType, Track, TrackSimplified, Artist }; use librespot::core::authentication::Credentials; use librespot::core::config::SessionConfig; @@ -79,6 +79,10 @@ impl Spotify { let album = self.spotify.albums().get_album(id, None).await?; Ok(SpotifyItem::Album(album.data)) } + "artist" => { + let artist = self.spotify.artists().get_artist(id).await?; + Ok(SpotifyItem::Artist(artist.data)) + } // Unsupported / Unimplemented _ => Ok(SpotifyItem::Other(uri.to_string())), } @@ -140,8 +144,34 @@ impl Spotify { } } } + + pub async fn full_artist(&self, id: &str) -> Result, SpotifyError> { + let mut items = vec![]; + let mut offset = 0; + loop { + let page = self + .spotify + .artists() + .get_artist_albums(id, None, 50, offset, None) + .await?; + + for album in &mut page + .data + .items + .iter() { + items.append(&mut self.full_album(&album.id).await?) + } + + // End + offset += page.data.items.len(); + if page.data.total == offset { + return Ok(items); + } + } + } } + impl Clone for Spotify { fn clone(&self) -> Self { Self { @@ -163,6 +193,7 @@ pub enum SpotifyItem { Track(Track), Album(Album), Playlist(Playlist), + Artist(Artist), /// Unimplemented Other(String), }