Fixed YouTube playback offsets

This commit is contained in:
Matthieu Grieger 2016-11-05 21:30:42 -07:00
parent 2f6bda5018
commit 138c1008eb
3 changed files with 30 additions and 15 deletions

View file

@ -1,6 +1,9 @@
MumbleDJ Changelog MumbleDJ Changelog
================== ==================
### November 5, 2016 -- `v3.2.1`
* Fixed YouTube video offsets. Now YouTube URLs with `?t=<timestamp>` at the end will start the audio playback at the appropriate position.
### November 5, 2016 -- `v3.2.0` ### November 5, 2016 -- `v3.2.0`
* Fixed a Go panic that would occur when a YouTube playlist contained a private video. * Fixed a Go panic that would occur when a YouTube playlist contained a private video.
* Added back immediate skipping for tracks/playlists that are skipped by the submitter. This was a feature that was present in the last major version of MumbleDJ but was forgotten when rewriting the bot (sorry!). * Added back immediate skipping for tracks/playlists that are skipped by the submitter. This was a feature that was present in the last major version of MumbleDJ but was forgotten when rewriting the bot (sorry!).

View file

@ -32,7 +32,7 @@ func init() {
services.DJ = DJ services.DJ = DJ
bot.DJ = DJ bot.DJ = DJ
DJ.Version = "v3.2.0" DJ.Version = "v3.2.1"
logrus.SetLevel(logrus.WarnLevel) logrus.SetLevel(logrus.WarnLevel)
} }

View file

@ -13,6 +13,8 @@ import (
"math" "math"
"net/http" "net/http"
"regexp" "regexp"
"strings"
"time"
"github.com/ChannelMeter/iso8601duration" "github.com/ChannelMeter/iso8601duration"
"github.com/antonholmquist/jason" "github.com/antonholmquist/jason"
@ -98,9 +100,12 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
tracks []interfaces.Track tracks []interfaces.Track
) )
dummyOffset, _ := time.ParseDuration("0s")
urlSplit := strings.Split(url, "?t=")
playlistURL = "https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s" playlistURL = "https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s"
playlistItemsURL = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&playlistId=%s&maxResults=%d&key=%s&pageToken=%s" playlistItemsURL = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&playlistId=%s&maxResults=%d&key=%s&pageToken=%s"
id, err = yt.getID(url) id, err = yt.getID(urlSplit[0])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -161,7 +166,7 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
// Unfortunately we have to execute another API call for each video as the YouTube API does not // Unfortunately we have to execute another API call for each video as the YouTube API does not
// return video durations from the playlistItems endpoint... // return video durations from the playlistItems endpoint...
newTrack, _ := yt.getTrack(videoID, submitter) newTrack, _ := yt.getTrack(videoID, submitter, dummyOffset)
newTrack.Playlist = playlist newTrack.Playlist = playlist
tracks = append(tracks, newTrack) tracks = append(tracks, newTrack)
@ -182,7 +187,13 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
return tracks, nil return tracks, nil
} }
track, err = yt.getTrack(id, submitter) // Submitter added a track!
offset := dummyOffset
if len(urlSplit) == 2 {
offset, _ = time.ParseDuration(urlSplit[1])
}
track, err = yt.getTrack(id, submitter, offset)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -190,7 +201,7 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
return tracks, nil return tracks, nil
} }
func (yt *YouTube) getTrack(id string, submitter *gumble.User) (bot.Track, error) { func (yt *YouTube) getTrack(id string, submitter *gumble.User, offset time.Duration) (bot.Track, error) {
var ( var (
resp *http.Response resp *http.Response
err error err error
@ -230,6 +241,7 @@ func (yt *YouTube) getTrack(id string, submitter *gumble.User) (bot.Track, error
Filename: id + ".track", Filename: id + ".track",
ThumbnailURL: thumbnail, ThumbnailURL: thumbnail,
Duration: duration, Duration: duration,
PlaybackOffset: offset,
Playlist: nil, Playlist: nil,
}, nil }, nil
} }