2015-04-09 03:44:22 +02:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
|
|
|
* services/youtube/api.go
|
|
|
|
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
|
|
|
|
*/
|
|
|
|
|
2015-04-09 03:47:39 +02:00
|
|
|
package youtube
|
2015-04-09 03:44:22 +02:00
|
|
|
|
|
|
|
import (
|
2015-04-09 03:47:39 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2015-04-09 03:44:22 +02:00
|
|
|
|
2015-04-09 03:47:39 +02:00
|
|
|
"github.com/jmoiron/jsonq"
|
2015-04-09 03:44:22 +02:00
|
|
|
)
|
|
|
|
|
2015-04-09 08:44:48 +02:00
|
|
|
// Video holds the metadata for a YouTube video.
|
|
|
|
type Video struct {
|
2015-04-09 03:47:39 +02:00
|
|
|
id string
|
|
|
|
title string
|
|
|
|
duration string
|
|
|
|
secondsDuration string
|
|
|
|
thumbnail string
|
2015-04-09 03:44:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 08:44:48 +02:00
|
|
|
// Playlist holds the metadata for a YouTube playlist.
|
|
|
|
type Playlist struct {
|
|
|
|
id string
|
|
|
|
title string
|
|
|
|
videoIds []string
|
2015-04-09 03:44:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 08:44:48 +02:00
|
|
|
// GetYouTubeVideo retrieves the metadata for a new YouTube video, and creates and returns a
|
|
|
|
// Video type.
|
2015-04-09 03:44:22 +02:00
|
|
|
func GetYouTubeVideo(id string) (*YouTubeVideo, error) {
|
2015-04-09 08:44:48 +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 response, err := PerformGetRequest(url); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
title, _ := response.String("items", "0", "snippet", "title")
|
|
|
|
thumbnail, _ := response.String("items", "0", "snippet", "thumbnails", "high", "url")
|
|
|
|
duration, _ := response.String("items", "0", "contentDetails", "duration")
|
|
|
|
|
|
|
|
minutes := int(duration[2:strings.Index(duration, "M")])
|
|
|
|
seconds := int(duration[strings.Index(duration, "M")+1 : len(duration)-1])
|
|
|
|
totalSeconds := (minutes * 60) + seconds
|
|
|
|
durationString := fmt.Sprintf("%d:%d", minutes, seconds)
|
|
|
|
|
|
|
|
video := &YoutubeVideo{
|
|
|
|
id: id,
|
|
|
|
title: title,
|
|
|
|
duration: durationString,
|
|
|
|
secondsDuration: totalSeconds,
|
|
|
|
thumbnail: thumbnail,
|
|
|
|
}
|
|
|
|
return video, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYouTubePlaylist retrieves the metadata for a new YouTube playlist, and creates and returns
|
|
|
|
// a Playlist type.
|
|
|
|
func GetYouTubePlaylist(id string) (*YouTubePlaylist, error) {
|
|
|
|
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=%s&key=%s",
|
2015-04-09 03:47:39 +02:00
|
|
|
id, os.Getenv("YOUTUBE_API_KEY"))
|
2015-04-09 08:44:48 +02:00
|
|
|
if response, err := PerformGetRequest(url); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
title, _ := response.String("items", "0")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// PerformGetRequest does all the grunt work for a Youtube HTTPS GET request.
|
|
|
|
func PerformGetRequest(url string) (*jsonq.JsonQuery, error) {
|
2015-04-09 03:47:39 +02:00
|
|
|
jsonString := ""
|
2015-04-09 03:44:22 +02:00
|
|
|
|
2015-04-09 03:47:39 +02:00
|
|
|
if response, err := http.Get(url); err == nil {
|
|
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode == 200 {
|
|
|
|
if body, err := ioutil.ReadAll(response.Body); err == nil {
|
|
|
|
jsonString = string(body)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if response.StatusCode == 403 {
|
|
|
|
return nil, errors.New("Invalid API key supplied.")
|
|
|
|
}
|
2015-04-09 08:44:48 +02:00
|
|
|
return nil, errors.New("Invalid YouTube ID supplied.")
|
2015-04-09 03:47:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("An error occurred while receiving HTTP GET response.")
|
|
|
|
}
|
2015-04-09 03:44:22 +02:00
|
|
|
|
2015-04-09 03:47:39 +02:00
|
|
|
jsonData := map[string]interface{}{}
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(jsonString))
|
|
|
|
decoder.Decode(&jsonData)
|
|
|
|
jq := jsonq.NewQuery(jsonData)
|
2015-04-09 03:44:22 +02:00
|
|
|
|
2015-04-09 08:44:48 +02:00
|
|
|
return jq, nil
|
2015-04-09 03:44:22 +02:00
|
|
|
}
|