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

Commit 138c100

Browse files
Fixed YouTube playback offsets
1 parent 2f6bda5 commit 138c100

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
MumbleDJ Changelog
22
==================
33

4+
### November 5, 2016 -- `v3.2.1`
5+
* Fixed YouTube video offsets. Now YouTube URLs with `?t=<timestamp>` at the end will start the audio playback at the appropriate position.
6+
47
### November 5, 2016 -- `v3.2.0`
58
* Fixed a Go panic that would occur when a YouTube playlist contained a private video.
69
* 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!).

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func init() {
3232
services.DJ = DJ
3333
bot.DJ = DJ
3434

35-
DJ.Version = "v3.2.0"
35+
DJ.Version = "v3.2.1"
3636

3737
logrus.SetLevel(logrus.WarnLevel)
3838
}

services/youtube.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"math"
1414
"net/http"
1515
"regexp"
16+
"strings"
17+
"time"
1618

1719
"github.com/ChannelMeter/iso8601duration"
1820
"github.com/antonholmquist/jason"
@@ -98,9 +100,12 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
98100
tracks []interfaces.Track
99101
)
100102

103+
dummyOffset, _ := time.ParseDuration("0s")
104+
urlSplit := strings.Split(url, "?t=")
105+
101106
playlistURL = "https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s"
102107
playlistItemsURL = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&playlistId=%s&maxResults=%d&key=%s&pageToken=%s"
103-
id, err = yt.getID(url)
108+
id, err = yt.getID(urlSplit[0])
104109
if err != nil {
105110
return nil, err
106111
}
@@ -161,7 +166,7 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
161166

162167
// Unfortunately we have to execute another API call for each video as the YouTube API does not
163168
// return video durations from the playlistItems endpoint...
164-
newTrack, _ := yt.getTrack(videoID, submitter)
169+
newTrack, _ := yt.getTrack(videoID, submitter, dummyOffset)
165170
newTrack.Playlist = playlist
166171
tracks = append(tracks, newTrack)
167172

@@ -182,15 +187,21 @@ func (yt *YouTube) GetTracks(url string, submitter *gumble.User) ([]interfaces.T
182187
return tracks, nil
183188
}
184189

185-
track, err = yt.getTrack(id, submitter)
190+
// Submitter added a track!
191+
offset := dummyOffset
192+
if len(urlSplit) == 2 {
193+
offset, _ = time.ParseDuration(urlSplit[1])
194+
}
195+
196+
track, err = yt.getTrack(id, submitter, offset)
186197
if err != nil {
187198
return nil, err
188199
}
189200
tracks = append(tracks, track)
190201
return tracks, nil
191202
}
192203

193-
func (yt *YouTube) getTrack(id string, submitter *gumble.User) (bot.Track, error) {
204+
func (yt *YouTube) getTrack(id string, submitter *gumble.User, offset time.Duration) (bot.Track, error) {
194205
var (
195206
resp *http.Response
196207
err error
@@ -221,15 +232,16 @@ func (yt *YouTube) getTrack(id string, submitter *gumble.User) (bot.Track, error
221232
duration := durationConverted.ToDuration()
222233

223234
return bot.Track{
224-
ID: id,
225-
URL: "https://youtube.com/watch?v=" + id,
226-
Title: title,
227-
Author: author,
228-
Submitter: submitter.Name,
229-
Service: yt.ReadableName,
230-
Filename: id + ".track",
231-
ThumbnailURL: thumbnail,
232-
Duration: duration,
233-
Playlist: nil,
235+
ID: id,
236+
URL: "https://youtube.com/watch?v=" + id,
237+
Title: title,
238+
Author: author,
239+
Submitter: submitter.Name,
240+
Service: yt.ReadableName,
241+
Filename: id + ".track",
242+
ThumbnailURL: thumbnail,
243+
Duration: duration,
244+
PlaybackOffset: offset,
245+
Playlist: nil,
234246
}, nil
235247
}

0 commit comments

Comments
 (0)