From 411cbadb59f25036fae4b821f4731c623b6b2204 Mon Sep 17 00:00:00 2001 From: Gabriel Plassard Date: Tue, 13 Oct 2015 00:08:36 +0200 Subject: [PATCH 1/4] Supports adding youtube playlist with more than 50 items --- service_youtube.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/service_youtube.go b/service_youtube.go index 7ecd02a..56deef0 100644 --- a/service_youtube.go +++ b/service_youtube.go @@ -154,22 +154,27 @@ 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 - } + morePages := true + pageToken := "" + for morePages{ //Iterate over the pages - 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) + // 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); 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 From 6ef13568fae13fabef8c02688f6963a81a60fe68 Mon Sep 17 00:00:00 2001 From: Gabriel Plassard Date: Wed, 14 Oct 2015 20:49:18 +0200 Subject: [PATCH 2/4] support maximum songs per playlist config --- config.gcfg | 4 ++++ parseconfig.go | 1 + service_youtube.go | 10 +++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) 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_youtube.go b/service_youtube.go index 56deef0..99dbced 100644 --- a/service_youtube.go +++ b/service_youtube.go @@ -173,9 +173,17 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) { songArray = append(songArray, song) } } - if pageToken, err = apiResponse.String("nextPageToken"); err != nil{ + if pageToken, err = apiResponse.String("nextPageToken"); err != nil || playlistSizeExceeded(songArray) { morePages = false } } + if (dj.conf.General.MaxSongPerPlaylist > 0 && len(songArray) > dj.conf.General.MaxSongPerPlaylist){ + songArray = songArray[:dj.conf.General.MaxSongPerPlaylist] + } return songArray, nil } + +// checks if the number of songs of the playlist exceeds the configured playlist maximum size +func playlistSizeExceeded(songs []Song) bool{ + return dj.conf.General.MaxSongPerPlaylist > 0 && len(songs) > dj.conf.General.MaxSongPerPlaylist +} From 6776d6869fcb75cdf411c9ed24019486978f136e Mon Sep 17 00:00:00 2001 From: Gabriel Plassard Date: Thu, 15 Oct 2015 23:07:35 +0200 Subject: [PATCH 3/4] Refactor max playlist size --- service_youtube.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/service_youtube.go b/service_youtube.go index 99dbced..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,9 +155,13 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) { title: title, } - morePages := true + + maxSongs := math.MaxInt32 + if (dj.conf.General.MaxSongPerPlaylist > 0){ + maxSongs = dj.conf.General.MaxSongPerPlaylist + } pageToken := "" - for morePages{ //Iterate over the pages + 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", @@ -166,24 +171,16 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) { } songs, _ := apiResponse.Array("items") - for j := 0; j < len(songs); j++ { + 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 || playlistSizeExceeded(songArray) { - morePages = false + if pageToken, err = apiResponse.String("nextPageToken"); err != nil { + break } } - if (dj.conf.General.MaxSongPerPlaylist > 0 && len(songArray) > dj.conf.General.MaxSongPerPlaylist){ - songArray = songArray[:dj.conf.General.MaxSongPerPlaylist] - } return songArray, nil } - -// checks if the number of songs of the playlist exceeds the configured playlist maximum size -func playlistSizeExceeded(songs []Song) bool{ - return dj.conf.General.MaxSongPerPlaylist > 0 && len(songs) > dj.conf.General.MaxSongPerPlaylist -} From c1f89fbaaf3f074a1b4ffe9a121398a3ef44e2f1 Mon Sep 17 00:00:00 2001 From: Gabriel Plassard Date: Fri, 16 Oct 2015 23:15:33 +0200 Subject: [PATCH 4/4] Support MaxSongPerPlaylist configuration for soundcloud playlists --- service_soundcloud.go | 3 +++ 1 file changed, 3 insertions(+) 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 {