diff --git a/config.gcfg b/config.gcfg index 408f51b..4f75aa9 100644 --- a/config.gcfg +++ b/config.gcfg @@ -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 diff --git a/parseconfig.go b/parseconfig.go index c69e8b3..a4df46b 100644 --- a/parseconfig.go +++ b/parseconfig.go @@ -22,6 +22,7 @@ type DjConfig struct { PlaylistSkipRatio float32 DefaultComment string MaxSongDuration int + MaxSongPerPlaylist int AutomaticShuffleOn bool } Cache struct { diff --git a/service_soundcloud.go b/service_soundcloud.go index 4fd85bb..bb2adb7 100644 --- a/service_soundcloud.go +++ b/service_soundcloud.go @@ -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 { diff --git a/service_youtube.go b/service_youtube.go index 7ecd02a..848cf85 100644 --- a/service_youtube.go +++ b/service_youtube.go @@ -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