Skip to content

Commit f8f6ec5

Browse files
committed
pagination support
1 parent fc26a64 commit f8f6ec5

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/discovery/listenbrainz.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type Metadata struct {
5757
type Recordings map[string]Metadata
5858

5959
type CreatedFor struct {
60+
Count int `json:"count"`
61+
Offset int `json:"offset"`
62+
PlaylistCount int `json:"playlist_count"`
6063
Playlists []struct {
6164
Playlist struct {
6265
Creator string `json:"creator"`
@@ -127,7 +130,7 @@ func (c *ListenBrainz) QueryTracks() ([]*models.Track, error) {
127130

128131
switch c.cfg.Discovery {
129132
case "playlist":
130-
id, err := c.getImportPlaylist(c.cfg.User)
133+
id, err := c.getImportPlaylist(c.cfg.User, "", 0)
131134
if err != nil {
132135
return nil, err
133136
}
@@ -227,8 +230,8 @@ func (c *ListenBrainz) getTracks(mbids []string, singleArtist bool) ([]*models.T
227230

228231
}
229232

230-
func (c *ListenBrainz) getImportPlaylist(user string) (string, error) { // Get user LB playlists and find wanted playlists ID
231-
body, err := c.lbRequest(fmt.Sprintf("user/%s/playlists/createdfor", user))
233+
func (c *ListenBrainz) getImportPlaylist(user, id string, offset int) (string, error) { // Get user LB playlists and find wanted playlists ID
234+
body, err := c.lbRequest(fmt.Sprintf("user/%s/playlists/createdfor?offset=%d", user, offset))
232235
if err != nil {
233236
return "", fmt.Errorf("getImportPlaylist(): %s", err.Error())
234237
}
@@ -238,7 +241,18 @@ func (c *ListenBrainz) getImportPlaylist(user string) (string, error) { // Get u
238241
if err != nil {
239242
return "", fmt.Errorf("getImportPlaylist(): %s", err.Error())
240243
}
244+
if id != "" {
245+
return id, nil
246+
}
247+
if playlists.Offset + playlists.Count <= playlists.PlaylistCount {
248+
id, _ = c.parseCreatedFor(playlists)
249+
return c.getImportPlaylist(user, id, playlists.Count)
250+
} else {
251+
return c.parseCreatedFor(playlists)
252+
}
253+
}
241254

255+
func (c ListenBrainz) parseCreatedFor(playlists CreatedFor) (string, error) {
242256
var currentWeek, currentDay int
243257
now := time.Now().Local()
244258
if c.cfg.ImportPlaylist != "daily-jams" {

0 commit comments

Comments
 (0)