Supports adding youtube playlist with more than 50 items
This commit is contained in:
parent
5d56a368f8
commit
411cbadb59
|
@ -154,23 +154,28 @@ 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
|
||||||
|
|
||||||
|
// Retrieve items in this page of the playlist
|
||||||
|
url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s&pageToken=%s",
|
||||||
|
id, os.Getenv("YOUTUBE_API_KEY"), pageToken)
|
||||||
if apiResponse, err = PerformGetRequest(url); err != nil {
|
if apiResponse, err = PerformGetRequest(url); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
numVideos, _ := apiResponse.Int("pageInfo", "totalResults")
|
|
||||||
if numVideos > 50 {
|
|
||||||
numVideos = 50
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < numVideos; i++ {
|
songs, _ := apiResponse.Array("items")
|
||||||
index := strconv.Itoa(i)
|
for j := 0; j < len(songs); j++ {
|
||||||
|
index := strconv.Itoa(j)
|
||||||
videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId")
|
videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId")
|
||||||
if song, err := yt.NewSong(user, videoID, "", playlist); err == nil {
|
if song, err := yt.NewSong(user, videoID, "", playlist); err == nil {
|
||||||
songArray = append(songArray, song)
|
songArray = append(songArray, song)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if pageToken, err = apiResponse.String("nextPageToken"); err != nil{
|
||||||
|
morePages = false
|
||||||
|
}
|
||||||
|
}
|
||||||
return songArray, nil
|
return songArray, nil
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue