Skip to content

Commit

Permalink
Add pagination to album tracks, fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Apr 12, 2020
1 parent 12d9ce2 commit 447e64b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/spotify/spotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ AudioFeatures Spotify::trackAudioFeatures(QString trackId)
return AudioFeatures(json.object());
}

QVector<Track> Spotify::albumTracks(const QString &albumId, const QString &albumName, int offset)
{
auto json = get(QString("albums/%1/tracks?limit=50&offset=%2").arg(albumId).arg(offset)).object();
auto trackItems = json["items"].toArray();
QVector<Track> tracks;
tracks.reserve(50);
// Add all in current page
for (auto item : trackItems)
{
auto t = Track(item.toObject());
// Album name is not included, so we have to set it manually
t.album = albumName;
tracks.append(t);
}
// Add all in next page
if (json.contains("next") && !json["next"].isNull())
tracks.append(albumTracks(albumId, albumName, json["offset"].toInt() + json["limit"].toInt()));
return tracks;
}

QVector<Track> *Spotify::albumTracks(const QString &albumID)
{
auto json = get(QString("albums/%1").arg(albumID));
Expand All @@ -287,6 +307,10 @@ QVector<Track> *Spotify::albumTracks(const QString &albumID)
t.album = albumName;
tracks->append(t);
}
// Check if we have any more to add
auto tracksLimit = json["tracks"].toObject()["limit"].toInt();
if (json["tracks"].toObject()["total"].toInt() > tracksLimit)
tracks->append(albumTracks(albumID, albumName, tracksLimit));
// Return final vector
return tracks;
}
Expand Down
2 changes: 2 additions & 0 deletions src/spotify/spotify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace spt
template <class T>
QVector<T> loadItems(const QString &url);

QVector<Track> albumTracks(const QString &albumId, const QString &albumName, int offset);

bool refresh();
};
}

0 comments on commit 447e64b

Please sign in to comment.