Supports adding youtube playlist with more than 50 items

This commit is contained in:
Gabriel Plassard 2015-10-13 00:08:36 +02:00
parent 5d56a368f8
commit 411cbadb59

View file

@ -154,22 +154,27 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) {
title: title, title: title,
} }
// Retrieve items in playlist morePages := true
url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s", pageToken := ""
id, os.Getenv("YOUTUBE_API_KEY")) for morePages{ //Iterate over the pages
if apiResponse, err = PerformGetRequest(url); err != nil {
return nil, err
}
numVideos, _ := apiResponse.Int("pageInfo", "totalResults")
if numVideos > 50 {
numVideos = 50
}
for i := 0; i < numVideos; i++ { // Retrieve items in this page of the playlist
index := strconv.Itoa(i) url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s&pageToken=%s",
videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId") id, os.Getenv("YOUTUBE_API_KEY"), pageToken)
if song, err := yt.NewSong(user, videoID, "", playlist); err == nil { if apiResponse, err = PerformGetRequest(url); err != nil {
songArray = append(songArray, song) return nil, err
}
songs, _ := apiResponse.Array("items")
for j := 0; j < len(songs); j++ {
index := strconv.Itoa(j)
videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId")
if song, err := yt.NewSong(user, videoID, "", playlist); err == nil {
songArray = append(songArray, song)
}
}
if pageToken, err = apiResponse.String("nextPageToken"); err != nil{
morePages = false
} }
} }
return songArray, nil return songArray, nil