From d11b6326ccad7a988470c32ead0e5c8024b71444 Mon Sep 17 00:00:00 2001 From: Jake Date: Mon, 16 Feb 2015 16:34:38 +0000 Subject: [PATCH 1/7] Added a restriction on maximum video length --- commands.go | 2 ++ mumbledj.gcfg | 3 +++ parseconfig.go | 1 + song.go | 4 ++++ strings.go | 3 +++ 5 files changed, 13 insertions(+) diff --git a/commands.go b/commands.go index a8e0c43..7560cdb 100644 --- a/commands.go +++ b/commands.go @@ -195,6 +195,8 @@ func add(user *gumble.User, username, url string) { } } } + } else if fmt.Sprint(err) == "video exceeds the maximum allowed duration." { + dj.SendPrivateMessage(user, VIDEO_TOO_LONG_MSG) } else { dj.SendPrivateMessage(user, INVALID_YOUTUBE_ID_MSG) } diff --git a/mumbledj.gcfg b/mumbledj.gcfg index ab6e099..54ba67a 100644 --- a/mumbledj.gcfg +++ b/mumbledj.gcfg @@ -22,6 +22,9 @@ PlaylistSkipRatio = 0.5 # NOTE: If you do not want a comment by default, set the variable equal to an empty string (""). DefaultComment = "Hello! I am a bot. Type !help for a list of commands." +# Maximum song duration in seconds +# Default Value 600 +MaxSongDuration = 600 [Cache] diff --git a/parseconfig.go b/parseconfig.go index 27d8ed1..18de314 100644 --- a/parseconfig.go +++ b/parseconfig.go @@ -20,6 +20,7 @@ type DjConfig struct { SkipRatio float32 PlaylistSkipRatio float32 DefaultComment string + MaxSongDuration int } Cache struct { Enabled bool diff --git a/song.go b/song.go index c07315f..dde652a 100644 --- a/song.go +++ b/song.go @@ -60,6 +60,10 @@ func NewSong(user, id string, playlist *Playlist) (*Song, error) { duration, _ := jq.Int("data", "duration") videoDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60) + if duration > dj.conf.General.MaxSongDuration { + return nil, errors.New("video exceeds the maximum allowed duration.") + } + song := &Song{ submitter: user, title: videoTitle, diff --git a/strings.go b/strings.go index cd151c6..3a43444 100644 --- a/strings.go +++ b/strings.go @@ -22,6 +22,9 @@ const CHANNEL_DOES_NOT_EXIST_MSG = "The channel you specified does not exist." // Message shown to users when they attempt to add an invalid URL to the queue. const INVALID_URL_MSG = "The URL you submitted does not match the required format." +// Message shown to users when they attempt to add a video that's too long +const VIDEO_TOO_LONG_MSG = "The video you submitted exceeds the duration allowed by the server." + // Message shown to users when they attempt to perform an action on a song when // no song is playing. const NO_MUSIC_PLAYING_MSG = "There is no music playing at the moment." From d37995b13650368e336b4a9a33e4fca80369a5e3 Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 17 Feb 2015 18:47:06 +0000 Subject: [PATCH 2/7] Made the maximum song duration optional --- mumbledj.gcfg | 6 +++--- song.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mumbledj.gcfg b/mumbledj.gcfg index 54ba67a..e674c12 100644 --- a/mumbledj.gcfg +++ b/mumbledj.gcfg @@ -22,9 +22,9 @@ PlaylistSkipRatio = 0.5 # NOTE: If you do not want a comment by default, set the variable equal to an empty string (""). DefaultComment = "Hello! I am a bot. Type !help for a list of commands." -# Maximum song duration in seconds -# Default Value 600 -MaxSongDuration = 600 +# Maximum song duration in seconds (0 = unristrcted) +# Default Value: 0 +MaxSongDuration = 0 [Cache] diff --git a/song.go b/song.go index dde652a..f632334 100644 --- a/song.go +++ b/song.go @@ -60,7 +60,7 @@ func NewSong(user, id string, playlist *Playlist) (*Song, error) { duration, _ := jq.Int("data", "duration") videoDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60) - if duration > dj.conf.General.MaxSongDuration { + if duration > dj.conf.General.MaxSongDuration && dj.conf.General.MaxSongDuration > 0 { return nil, errors.New("video exceeds the maximum allowed duration.") } From eec8d9c3d2eb052a6f0a86450c2e0ccb73c30d20 Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 17 Feb 2015 18:51:55 +0000 Subject: [PATCH 3/7] Fixed playlists ignoring the max song length check --- playlist.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/playlist.go b/playlist.go index 9fed006..33ea4e8 100644 --- a/playlist.go +++ b/playlist.go @@ -75,7 +75,12 @@ func NewPlaylist(user, id string) (*Playlist, error) { playlist: playlist, dontSkip: false, } - dj.queue.AddSong(newSong) + // Don't spam the chat if a playlist contains songs that are too long + if dj.conf.General.MaxSongDuration == 0 { + dj.queue.AddSong(newSong) + } else if duration <= dj.conf.General.MaxSongDuration { + dj.queue.AddSong(newSong) + } } return playlist, nil From 6a954e04b13fa13469f5b4fbf120aa8315fabd2c Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 17 Feb 2015 19:14:25 +0000 Subject: [PATCH 4/7] Check whether song duration is restricted first --- song.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/song.go b/song.go index f632334..05f4beb 100644 --- a/song.go +++ b/song.go @@ -60,7 +60,7 @@ func NewSong(user, id string, playlist *Playlist) (*Song, error) { duration, _ := jq.Int("data", "duration") videoDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60) - if duration > dj.conf.General.MaxSongDuration && dj.conf.General.MaxSongDuration > 0 { + if dj.conf.General.MaxSongDuration > 0 && duration > dj.conf.General.MaxSongDuration { return nil, errors.New("video exceeds the maximum allowed duration.") } From 541f371239ff696a0160aa74d5730bcae12e301c Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 17 Feb 2015 19:18:09 +0000 Subject: [PATCH 5/7] Tidied playlist song duration check --- playlist.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/playlist.go b/playlist.go index 33ea4e8..aec48eb 100644 --- a/playlist.go +++ b/playlist.go @@ -76,9 +76,7 @@ func NewPlaylist(user, id string) (*Playlist, error) { dontSkip: false, } // Don't spam the chat if a playlist contains songs that are too long - if dj.conf.General.MaxSongDuration == 0 { - dj.queue.AddSong(newSong) - } else if duration <= dj.conf.General.MaxSongDuration { + if dj.conf.General.MaxSongDuration == 0 || duration <= dj.conf.General.MaxSongDuration { dj.queue.AddSong(newSong) } } From 38be6919135fdd73ce28d319f7a2e5cf3eb807fa Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 17 Feb 2015 19:47:57 +0000 Subject: [PATCH 6/7] Fixed panic when a song exceeded allowed duration --- playlist.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/playlist.go b/playlist.go index aec48eb..06cbfff 100644 --- a/playlist.go +++ b/playlist.go @@ -58,9 +58,9 @@ func NewPlaylist(user, id string) (*Playlist, error) { id: id, title: playlistTitle, } - + j := 0 for i := 0; i < playlistItems; i++ { - index := strconv.Itoa(i) + index := strconv.Itoa(j) songTitle, _ := jq.String("data", "items", index, "video", "title") songId, _ := jq.String("data", "items", index, "video", "id") songThumbnail, _ := jq.String("data", "items", index, "video", "thumbnail", "hqDefault") @@ -78,7 +78,8 @@ func NewPlaylist(user, id string) (*Playlist, error) { // Don't spam the chat if a playlist contains songs that are too long if dj.conf.General.MaxSongDuration == 0 || duration <= dj.conf.General.MaxSongDuration { dj.queue.AddSong(newSong) - } + j += 1 + } } return playlist, nil From 2b9394a37147aa460dd97cd2cc39b28233730f6c Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 17 Feb 2015 23:39:37 +0000 Subject: [PATCH 7/7] Fixed panic when attempting to play empty queue --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 7560cdb..2c4c556 100644 --- a/commands.go +++ b/commands.go @@ -210,7 +210,7 @@ func add(user *gumble.User, username, url string) { oldLength := dj.queue.Len() if newPlaylist, err := NewPlaylist(username, shortUrl); err == nil { dj.client.Self.Channel.Send(fmt.Sprintf(PLAYLIST_ADDED_HTML, username, newPlaylist.title), false) - if oldLength == 0 && !dj.audioStream.IsPlaying() { + if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() { if err := dj.queue.CurrentSong().Download(); err == nil { dj.queue.CurrentSong().Play() } else {