From c85fddcb4f38676e3b46cfdd1900f1dfcb2e43c8 Mon Sep 17 00:00:00 2001 From: Matt Kemp Date: Sun, 26 Jun 2016 23:57:15 -0500 Subject: [PATCH] Allow playlists larger than 50 items (#159) This commit ensures that playlists longer than 50 items return successfully when `max_tracks_per_playlist` is set higher than 50. Previously if this value was raised higher than 50 the addition of a playlist with more than 50 items would hang indefinitely. --- services/youtube.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/services/youtube.go b/services/youtube.go index da68465..cde6b7e 100644 --- a/services/youtube.go +++ b/services/youtube.go @@ -134,9 +134,15 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T maxItems = viper.GetInt("queue.max_tracks_per_playlist") } + // YouTube playlist searches return a max of 50 results per page + maxResults := 50 + if maxResults > maxItems { + maxResults = maxItems + } + pageToken := "" for len(tracks) < maxItems { - curResp, curErr := http.Get(fmt.Sprintf(playlistItemsURL, id, maxItems, viper.GetString("api_keys.youtube"), pageToken)) + curResp, curErr := http.Get(fmt.Sprintf(playlistItemsURL, id, maxResults, viper.GetString("api_keys.youtube"), pageToken)) defer curResp.Body.Close() if curErr != nil { // An error occurred, simply skip this track. @@ -163,6 +169,11 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T break } } + + pageToken, _ = v.GetString("nextPageToken") + if pageToken == "" { + break + } } if len(tracks) == 0 {