diff --git a/src/artist.rs b/src/artist.rs index 99ca33f..65a11e0 100644 --- a/src/artist.rs +++ b/src/artist.rs @@ -43,23 +43,32 @@ impl Artist { } if let Some(ref artist_id) = self.id { - if let Some(sas) = spotify.artist_albums(artist_id, 50, 0) { - let mut albums: Vec = Vec::new(); + let mut collected_ids: Vec = Vec::new(); + let mut offset = 0; + while let Some(sas) = spotify.artist_albums(artist_id, 50, offset) { + let items_len = sas.items.len() as u32; + debug!("got {} albums", items_len); for sa in sas.items { - if Some("appears_on".into()) == sa.album_group { + if Some("appears_on") == sa.album_group.as_deref() { continue; } - if let Some(album_id) = sa.id { - if let Some(fa) = spotify.full_album(&album_id).as_ref() { - albums.push(fa.into()); - } + collected_ids.push(album_id); } } - self.albums = Some(albums); + match sas.next { + Some(_) => offset += items_len, + None => break, + } } + + let albums = match spotify.albums(&collected_ids) { + Some(fas) => fas.iter().map(Album::from).collect(), + None => Vec::new(), + }; + self.albums = Some(albums); } } diff --git a/src/spotify.rs b/src/spotify.rs index a530a00..7f0a836 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -653,6 +653,16 @@ impl Spotify { self.api_with_retry(|api| api.album(album_id)) } + pub fn albums(&self, album_ids: &[String]) -> Option> { + const MAX_SIZE: usize = 20; + let mut collected = Vec::new(); + for ids in album_ids.chunks(MAX_SIZE) { + let fas = self.api_with_retry(|api| api.albums(ids.to_vec()))?; + collected.extend_from_slice(&fas.albums); + } + Some(collected) + } + pub fn artist(&self, artist_id: &str) -> Option { self.api_with_retry(|api| api.artist(artist_id)) }