2015-04-14 08:40:39 +02:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
|
|
|
* service_youtube.go
|
|
|
|
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
|
|
|
|
*/
|
|
|
|
|
2015-07-30 18:28:01 +02:00
|
|
|
package main
|
2015-04-14 08:40:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-05-13 07:47:14 +02:00
|
|
|
"regexp"
|
2015-04-14 08:40:39 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-09-26 15:52:39 +02:00
|
|
|
"time"
|
2015-04-14 08:40:39 +02:00
|
|
|
|
|
|
|
"github.com/jmoiron/jsonq"
|
2015-07-27 23:29:50 +02:00
|
|
|
"github.com/layeh/gumble/gumble"
|
2015-04-14 08:40:39 +02:00
|
|
|
)
|
|
|
|
|
2015-07-28 13:35:52 +02:00
|
|
|
// Regular expressions for youtube urls
|
2015-07-28 01:11:35 +02:00
|
|
|
var youtubePlaylistPattern = `https?:\/\/www\.youtube\.com\/playlist\?list=([\w-]+)`
|
|
|
|
var youtubeVideoPatterns = []string{
|
2015-08-16 02:28:22 +02:00
|
|
|
`https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`,
|
|
|
|
`https?:\/\/youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`,
|
2015-07-28 01:11:35 +02:00
|
|
|
`https?:\/\/youtu.be\/([\w-]+)(\?t=\d*m?\d*s?)?`,
|
2015-08-16 02:28:22 +02:00
|
|
|
`https?:\/\/youtube.com\/v\/([\w-]+)(\?t=\d*m?\d*s?)?`,
|
|
|
|
`https?:\/\/www.youtube.com\/v\/([\w-]+)(\?t=\d*m?\d*s?)?`,
|
2015-07-28 01:11:35 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 14:48:53 +02:00
|
|
|
// YouTube implements the Service interface
|
|
|
|
type YouTube struct{}
|
|
|
|
|
2015-09-26 15:52:39 +02:00
|
|
|
// ServiceName is the human readable version of the service name
|
|
|
|
func (yt YouTube) ServiceName() string {
|
|
|
|
return "YouTube"
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrackName is the human readable version of the service name
|
|
|
|
func (yt YouTube) TrackName() string {
|
|
|
|
return "Video"
|
|
|
|
}
|
|
|
|
|
2015-08-15 23:22:59 +02:00
|
|
|
// URLRegex checks to see if service will accept URL
|
2015-07-30 14:48:53 +02:00
|
|
|
func (yt YouTube) URLRegex(url string) bool {
|
2015-07-28 01:11:35 +02:00
|
|
|
return RegexpFromURL(url, append(youtubeVideoPatterns, []string{youtubePlaylistPattern}...)) != nil
|
|
|
|
}
|
2015-07-27 23:13:40 +02:00
|
|
|
|
2015-08-15 23:22:59 +02:00
|
|
|
// NewRequest creates the requested song/playlist and adds to the queue
|
2015-09-26 15:52:39 +02:00
|
|
|
func (yt YouTube) NewRequest(user *gumble.User, url string) ([]Song, error) {
|
|
|
|
var songArray []Song
|
2015-07-28 01:13:13 +02:00
|
|
|
var shortURL, startOffset = "", ""
|
2015-07-28 00:30:59 +02:00
|
|
|
if re, err := regexp.Compile(youtubePlaylistPattern); err == nil {
|
|
|
|
if re.MatchString(url) {
|
2015-09-26 15:52:39 +02:00
|
|
|
shortURL = re.FindStringSubmatch(url)[1]
|
2015-09-26 17:04:39 +02:00
|
|
|
return yt.NewPlaylist(user, shortURL)
|
2015-07-28 00:30:59 +02:00
|
|
|
} else {
|
2015-07-28 01:11:35 +02:00
|
|
|
re = RegexpFromURL(url, youtubeVideoPatterns)
|
2015-07-28 00:36:50 +02:00
|
|
|
matches := re.FindAllStringSubmatch(url, -1)
|
2015-07-28 00:30:59 +02:00
|
|
|
shortURL = matches[0][1]
|
|
|
|
if len(matches[0]) == 3 {
|
|
|
|
startOffset = matches[0][2]
|
|
|
|
}
|
2015-08-13 14:44:38 +02:00
|
|
|
song, err := yt.NewSong(user, shortURL, startOffset, nil)
|
2015-09-26 17:56:50 +02:00
|
|
|
if !isNil(song) {
|
|
|
|
return append(songArray, song), nil
|
2015-08-07 14:54:44 +02:00
|
|
|
} else {
|
2015-09-26 15:52:39 +02:00
|
|
|
return nil, err
|
2015-08-07 14:54:44 +02:00
|
|
|
}
|
2015-07-28 00:30:59 +02:00
|
|
|
}
|
2015-07-28 00:37:52 +02:00
|
|
|
} else {
|
2015-09-26 16:09:07 +02:00
|
|
|
return nil, err
|
2015-07-28 00:30:59 +02:00
|
|
|
}
|
2015-07-27 23:13:40 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 23:22:59 +02:00
|
|
|
// NewSong gathers the metadata for a song extracted from a YouTube video, and returns the song.
|
2015-08-13 14:44:38 +02:00
|
|
|
func (yt YouTube) NewSong(user *gumble.User, id, offset string, playlist Playlist) (Song, error) {
|
2015-09-26 15:52:39 +02:00
|
|
|
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s", id, os.Getenv("YOUTUBE_API_KEY"))
|
|
|
|
if apiResponse, err := PerformGetRequest(url); err == nil {
|
|
|
|
title, _ := apiResponse.String("items", "0", "snippet", "title")
|
|
|
|
thumbnail, _ := apiResponse.String("items", "0", "snippet", "thumbnails", "high", "url")
|
|
|
|
duration, _ := apiResponse.String("items", "0", "contentDetails", "duration")
|
2015-05-10 06:45:00 +02:00
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
song := &YouTubeSong{
|
2015-04-16 08:32:32 +02:00
|
|
|
submitter: user,
|
|
|
|
title: title,
|
|
|
|
id: id,
|
2015-08-13 15:07:04 +02:00
|
|
|
url: "https://youtu.be/" + id,
|
2015-09-26 18:51:34 +02:00
|
|
|
offset: int(yt.parseTime(offset, `\?T\=(?P<days>\d+D)?(?P<hours>\d+H)?(?P<minutes>\d+M)?(?P<seconds>\d+S)?`).Seconds()),
|
2015-09-26 18:37:19 +02:00
|
|
|
duration: int(yt.parseTime(duration, `P(?P<days>\d+D)?T(?P<hours>\d+H)?(?P<minutes>\d+M)?(?P<seconds>\d+S)?`).Seconds()),
|
2015-04-16 08:32:32 +02:00
|
|
|
thumbnail: thumbnail,
|
2015-08-13 15:33:37 +02:00
|
|
|
format: "m4a",
|
2015-04-16 08:32:32 +02:00
|
|
|
skippers: make([]string, 0),
|
2015-07-28 14:29:14 +02:00
|
|
|
playlist: playlist,
|
2015-04-16 08:32:32 +02:00
|
|
|
dontSkip: false,
|
2015-09-26 15:52:39 +02:00
|
|
|
service: yt,
|
2015-04-14 08:40:39 +02:00
|
|
|
}
|
2015-07-27 21:44:09 +02:00
|
|
|
|
2015-04-14 08:40:39 +02:00
|
|
|
return song, nil
|
|
|
|
}
|
2015-09-26 15:52:39 +02:00
|
|
|
return nil, errors.New(fmt.Sprintf(INVALID_API_KEY, yt.ServiceName()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTime converts from the string youtube returns to a time.Duration
|
2015-09-26 18:37:19 +02:00
|
|
|
func (yt YouTube) parseTime(duration, regex string) time.Duration {
|
2015-09-26 18:51:34 +02:00
|
|
|
fmt.Printf("parseTime(%s)", strings.ToUpper(duration))
|
2015-09-26 15:52:39 +02:00
|
|
|
var days, hours, minutes, seconds, totalSeconds int64
|
|
|
|
if duration != "" {
|
2015-09-26 18:37:19 +02:00
|
|
|
timestampExp := regexp.MustCompile(regex)
|
2015-09-26 18:30:55 +02:00
|
|
|
timestampMatch := timestampExp.FindStringSubmatch(strings.ToUpper(duration))
|
2015-09-26 15:52:39 +02:00
|
|
|
timestampResult := make(map[string]string)
|
|
|
|
for i, name := range timestampExp.SubexpNames() {
|
|
|
|
if i < len(timestampMatch) {
|
|
|
|
timestampResult[name] = timestampMatch[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if timestampResult["days"] != "" {
|
|
|
|
days, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["days"], "D"), 10, 32)
|
|
|
|
}
|
|
|
|
if timestampResult["hours"] != "" {
|
|
|
|
hours, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["hours"], "H"), 10, 32)
|
|
|
|
}
|
|
|
|
if timestampResult["minutes"] != "" {
|
|
|
|
minutes, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["minutes"], "M"), 10, 32)
|
|
|
|
}
|
|
|
|
if timestampResult["seconds"] != "" {
|
|
|
|
seconds, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["seconds"], "S"), 10, 32)
|
|
|
|
}
|
|
|
|
|
2015-09-26 16:57:17 +02:00
|
|
|
totalSeconds = int64((days * 86400) + (hours * 3600) + (minutes * 60) + seconds)
|
2015-09-26 15:52:39 +02:00
|
|
|
} else {
|
|
|
|
totalSeconds = 0
|
|
|
|
}
|
2015-09-26 17:04:39 +02:00
|
|
|
output, _ := time.ParseDuration(strconv.Itoa(int(totalSeconds)) + "s")
|
|
|
|
return output
|
2015-04-14 08:40:39 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 14:48:53 +02:00
|
|
|
// NewPlaylist gathers the metadata for a YouTube playlist and returns it.
|
2015-09-26 16:09:07 +02:00
|
|
|
func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) {
|
2015-07-30 14:48:53 +02:00
|
|
|
var apiResponse *jsonq.JsonQuery
|
2015-09-26 16:09:07 +02:00
|
|
|
var songArray []Song
|
2015-07-30 14:48:53 +02:00
|
|
|
var err error
|
|
|
|
// Retrieve title of playlist
|
2015-08-13 14:44:38 +02:00
|
|
|
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s", id, os.Getenv("YOUTUBE_API_KEY"))
|
2015-08-02 19:55:51 +02:00
|
|
|
if apiResponse, err = PerformGetRequest(url); err != nil {
|
2015-07-30 14:48:53 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
title, _ := apiResponse.String("items", "0", "snippet", "title")
|
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
playlist := &YouTubePlaylist{
|
2015-07-30 14:48:53 +02:00
|
|
|
id: id,
|
|
|
|
title: title,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve items in playlist
|
|
|
|
url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s",
|
|
|
|
id, os.Getenv("YOUTUBE_API_KEY"))
|
2015-08-02 19:55:51 +02:00
|
|
|
if apiResponse, err = PerformGetRequest(url); err != nil {
|
2015-07-30 14:48:53 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
numVideos, _ := apiResponse.Int("pageInfo", "totalResults")
|
|
|
|
if numVideos > 50 {
|
|
|
|
numVideos = 50
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < numVideos; i++ {
|
|
|
|
index := strconv.Itoa(i)
|
|
|
|
videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId")
|
2015-09-26 16:09:07 +02:00
|
|
|
if song, err := yt.NewSong(user, videoID, "", playlist); err == nil {
|
|
|
|
songArray = append(songArray, song)
|
|
|
|
}
|
2015-07-30 14:48:53 +02:00
|
|
|
}
|
2015-09-26 16:09:07 +02:00
|
|
|
return songArray, nil
|
2015-07-30 14:48:53 +02:00
|
|
|
}
|