Add option to download albums from artists

This commit is contained in:
oSumAtrIX 2021-10-11 12:45:14 +02:00
parent a5d94f2dee
commit acf5e213ba
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4
2 changed files with 38 additions and 1 deletions

View file

@ -74,6 +74,12 @@ impl Downloader {
let queue: Vec<Download> = 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<Download> = tracks.into_iter().map(|t| t.into()).collect();
self.add_to_queue_multiple(queue).await;
}
// Unsupported
SpotifyItem::Other(u) => {
error!("Unsupported URI: {}", u);

View file

@ -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<Vec<TrackSimplified>, 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),
}