Skip to content
This repository has been archived by the owner on Jan 20, 2021. It is now read-only.

Commit

Permalink
Fixed YouTube playback offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugrieger committed Nov 6, 2016
1 parent 2f6bda5 commit 138c100
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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`
* 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!).
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
services.DJ = DJ
bot.DJ = DJ

DJ.Version = "v3.2.0"
DJ.Version = "v3.2.1"

logrus.SetLevel(logrus.WarnLevel)
}
Expand Down
40 changes: 26 additions & 14 deletions services/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"math"
"net/http"
"regexp"
"strings"
"time"

"github.com/ChannelMeter/iso8601duration"
"github.com/antonholmquist/jason"
Expand Down Expand Up @@ -98,9 +100,12 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
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"
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 {
return nil, err
}
Expand Down Expand Up @@ -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
// return video durations from the playlistItems endpoint...
newTrack, _ := yt.getTrack(videoID, submitter)
newTrack, _ := yt.getTrack(videoID, submitter, dummyOffset)
newTrack.Playlist = playlist
tracks = append(tracks, newTrack)

Expand All @@ -182,15 +187,21 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
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 {
return nil, err
}
tracks = append(tracks, track)
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 (
resp *http.Response
err error
Expand Down Expand Up @@ -221,15 +232,16 @@ func (yt *YouTube) getTrack(id string, submitter *gumble.User) (bot.Track, error
duration := durationConverted.ToDuration()

return bot.Track{
ID: id,
URL: "https://youtube.com/watch?v=" + id,
Title: title,
Author: author,
Submitter: submitter.Name,
Service: yt.ReadableName,
Filename: id + ".track",
ThumbnailURL: thumbnail,
Duration: duration,
Playlist: nil,
ID: id,
URL: "https://youtube.com/watch?v=" + id,
Title: title,
Author: author,
Submitter: submitter.Name,
Service: yt.ReadableName,
Filename: id + ".track",
ThumbnailURL: thumbnail,
Duration: duration,
PlaybackOffset: offset,
Playlist: nil,
}, nil
}

0 comments on commit 138c100

Please sign in to comment.