@@ -130,7 +130,7 @@ func (c *ListenBrainz) QueryTracks() ([]*models.Track, error) {
130130
131131 switch c .cfg .Discovery {
132132 case "playlist" :
133- id , err := c .getImportPlaylist (c .cfg .User , "" , 0 )
133+ id , err := c .getImportPlaylist (c .cfg .User )
134134 if err != nil {
135135 return nil , err
136136 }
@@ -230,26 +230,32 @@ func (c *ListenBrainz) getTracks(mbids []string, singleArtist bool) ([]*models.T
230230
231231}
232232
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 ))
235- if err != nil {
236- return "" , fmt .Errorf ("getImportPlaylist(): %s" , err .Error ())
237- }
233+ func (c * ListenBrainz ) getImportPlaylist (user string ) (string , error ) { // Get user LB playlists and find wanted playlists ID
234+ var offset int
238235
239- var playlists CreatedFor
240- err = util .ParseResp (body , & playlists )
241- if err != nil {
242- return "" , fmt .Errorf ("getImportPlaylist(): %s" , err .Error ())
243- }
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 )
236+ for {
237+ body , err := c .lbRequest (fmt .Sprintf ("user/%s/playlists/createdfor?offset=%d" , user , offset ))
238+ if err != nil {
239+ return "" , fmt .Errorf ("getImportPlaylist(): %s" , err .Error ())
240+ }
241+
242+ var playlists CreatedFor
243+ err = util .ParseResp (body , & playlists )
244+ if err != nil {
245+ return "" , fmt .Errorf ("getImportPlaylist(): %s" , err .Error ())
246+ }
247+
248+ if id , err := c .parseCreatedFor (playlists ); err != nil {
249+ return id , nil
250+ }
251+
252+ if playlists .Count + playlists .Offset >= playlists .PlaylistCount {
253+ break
254+ }
255+
256+ offset += playlists .Count
252257 }
258+ return "" , fmt .Errorf ("failed to get %s playlist, check if ListenBrainz has generated one" , c .cfg .ImportPlaylist ,)
253259}
254260
255261func (c ListenBrainz ) parseCreatedFor (playlists CreatedFor ) (string , error ) {
0 commit comments