fix: load more tracks if album has more than 50

fixes #281
This commit is contained in:
Henrik Friedrichsen
2020-10-09 20:21:04 +02:00
parent 79093eca1e
commit 98914f27e1
2 changed files with 35 additions and 9 deletions

View File

@@ -33,15 +33,32 @@ impl Album {
}
if let Some(ref album_id) = self.id {
if let Some(fa) = spotify.full_album(&album_id) {
self.tracks = Some(
fa.tracks
.items
.iter()
.map(|st| Track::from_simplified_track(&st, &fa))
.collect(),
);
let mut collected_tracks = Vec::new();
if let Some(full_album) = spotify.full_album(album_id) {
let mut tracks_result = Some(full_album.tracks.clone());
while let Some(ref tracks) = tracks_result {
for t in &tracks.items {
collected_tracks.push(Track::from_simplified_track(t, &full_album));
}
debug!("got {} tracks", tracks.items.len());
// load next batch if necessary
tracks_result = match tracks.next {
Some(_) => {
debug!("requesting tracks again..");
spotify.album_tracks(
album_id,
50,
tracks.offset + tracks.items.len() as u32,
)
}
None => None,
}
}
}
self.tracks = Some(collected_tracks)
}
}
}

View File

@@ -19,7 +19,7 @@ use rspotify::model::artist::FullArtist;
use rspotify::model::page::{CursorBasedPage, Page};
use rspotify::model::playlist::{FullPlaylist, PlaylistTrack, SimplifiedPlaylist};
use rspotify::model::search::SearchResult;
use rspotify::model::track::{FullTrack, SavedTrack};
use rspotify::model::track::{FullTrack, SavedTrack, SimplifiedTrack};
use rspotify::model::user::PrivateUser;
use rspotify::senum::SearchType;
@@ -713,6 +713,15 @@ impl Spotify {
self.api_with_retry(|api| api.album(album_id))
}
pub fn album_tracks(
&self,
album_id: &str,
limit: u32,
offset: u32,
) -> Option<Page<SimplifiedTrack>> {
self.api_with_retry(|api| api.album_track(album_id, limit, offset))
}
pub fn artist_albums(
&self,
artist_id: &str,