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:
commit
caac88d8b2
|
@ -26,6 +26,10 @@ DefaultComment = "Hello! I am a bot. Type !help for a list of commands."
|
||||||
# Default Value: 0
|
# Default Value: 0
|
||||||
MaxSongDuration = 0
|
MaxSongDuration = 0
|
||||||
|
|
||||||
|
# Maximum songs per playlist (0 = unrestricted)
|
||||||
|
# Default Value: 50
|
||||||
|
MaxSongPerPlaylist = 50
|
||||||
|
|
||||||
# Is playlist shuffling enabled when the bot starts?
|
# Is playlist shuffling enabled when the bot starts?
|
||||||
# Default Value: false
|
# Default Value: false
|
||||||
AutomaticShuffleOn = false
|
AutomaticShuffleOn = false
|
||||||
|
|
|
@ -22,6 +22,7 @@ type DjConfig struct {
|
||||||
PlaylistSkipRatio float32
|
PlaylistSkipRatio float32
|
||||||
DefaultComment string
|
DefaultComment string
|
||||||
MaxSongDuration int
|
MaxSongDuration int
|
||||||
|
MaxSongPerPlaylist int
|
||||||
AutomaticShuffleOn bool
|
AutomaticShuffleOn bool
|
||||||
}
|
}
|
||||||
Cache struct {
|
Cache struct {
|
||||||
|
|
|
@ -66,6 +66,9 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) ([]Song, error) {
|
||||||
title: title,
|
title: title,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dj.conf.General.MaxSongPerPlaylist > 0 && len(tracks) > dj.conf.General.MaxSongPerPlaylist){
|
||||||
|
tracks = tracks[:dj.conf.General.MaxSongPerPlaylist]
|
||||||
|
}
|
||||||
// Add all tracks
|
// Add all tracks
|
||||||
for _, t := range tracks {
|
for _, t := range tracks {
|
||||||
if song, err := sc.NewSong(user, jsonq.NewQuery(t), 0, playlist); err == nil {
|
if song, err := sc.NewSong(user, jsonq.NewQuery(t), 0, playlist); err == nil {
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/jmoiron/jsonq"
|
"github.com/jmoiron/jsonq"
|
||||||
"github.com/layeh/gumble/gumble"
|
"github.com/layeh/gumble/gumble"
|
||||||
|
@ -154,23 +155,32 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) {
|
||||||
title: title,
|
title: title,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve items in playlist
|
|
||||||
url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s",
|
maxSongs := math.MaxInt32
|
||||||
id, os.Getenv("YOUTUBE_API_KEY"))
|
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 {
|
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) && len(songArray) < maxSongs ; 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 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
return songArray, nil
|
return songArray, nil
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue