Merge pull request #91 from GabrielPlassard/support_playlists_over_50_songs

Supports adding youtube playlist with more than 50 items (Closes https://github.com/matthieugrieger/mumbledj/issues/71)
This commit is contained in:
Matthieu Grieger 2015-10-16 14:52:45 -07:00
commit caac88d8b2
4 changed files with 33 additions and 15 deletions

View file

@ -26,6 +26,10 @@ DefaultComment = "Hello! I am a bot. Type !help for a list of commands."
# Default Value: 0
MaxSongDuration = 0
# Maximum songs per playlist (0 = unrestricted)
# Default Value: 50
MaxSongPerPlaylist = 50
# Is playlist shuffling enabled when the bot starts?
# Default Value: false
AutomaticShuffleOn = false

View file

@ -22,6 +22,7 @@ type DjConfig struct {
PlaylistSkipRatio float32
DefaultComment string
MaxSongDuration int
MaxSongPerPlaylist int
AutomaticShuffleOn bool
}
Cache struct {

View file

@ -66,6 +66,9 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) ([]Song, error) {
title: title,
}
if (dj.conf.General.MaxSongPerPlaylist > 0 && len(tracks) > dj.conf.General.MaxSongPerPlaylist){
tracks = tracks[:dj.conf.General.MaxSongPerPlaylist]
}
// Add all tracks
for _, t := range tracks {
if song, err := sc.NewSong(user, jsonq.NewQuery(t), 0, playlist); err == nil {

View file

@ -15,6 +15,7 @@ import (
"strconv"
"strings"
"time"
"math"
"github.com/jmoiron/jsonq"
"github.com/layeh/gumble/gumble"
@ -154,22 +155,31 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) {
title: title,
}
// Retrieve items in playlist
url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s",
id, os.Getenv("YOUTUBE_API_KEY"))
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++ {
index := strconv.Itoa(i)
videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId")
if song, err := yt.NewSong(user, videoID, "", playlist); err == nil {
songArray = append(songArray, song)
maxSongs := math.MaxInt32
if (dj.conf.General.MaxSongPerPlaylist > 0){
maxSongs = dj.conf.General.MaxSongPerPlaylist
}
pageToken := ""
for len(songArray) < maxSongs{ //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 {
return nil, err
}
songs, _ := apiResponse.Array("items")
for j := 0; j < len(songs) && len(songArray) < maxSongs ; 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 {
break
}
}
return songArray, nil