Added PerformGetRequest
This commit is contained in:
parent
6a836f13b7
commit
14bb9df146
|
@ -19,8 +19,8 @@ import (
|
|||
"github.com/jmoiron/jsonq"
|
||||
)
|
||||
|
||||
// Collection of metadata for a YouTube video.
|
||||
type YouTubeVideo struct {
|
||||
// Video holds the metadata for a YouTube video.
|
||||
type Video struct {
|
||||
id string
|
||||
title string
|
||||
duration string
|
||||
|
@ -28,46 +28,25 @@ type YouTubeVideo struct {
|
|||
thumbnail string
|
||||
}
|
||||
|
||||
// Collection of metadata for a YouTube playlist.
|
||||
type YouTubePlaylist struct {
|
||||
id string
|
||||
title string
|
||||
duration string
|
||||
secondsDuration string
|
||||
// Playlist holds the metadata for a YouTube playlist.
|
||||
type Playlist struct {
|
||||
id string
|
||||
title string
|
||||
videoIds []string
|
||||
}
|
||||
|
||||
// Retrieves the metadata for a new YouTube video, and creates and returns a
|
||||
// YouTubeVideo type.
|
||||
// GetYouTubeVideo retrieves the metadata for a new YouTube video, and creates and returns a
|
||||
// Video type.
|
||||
func GetYouTubeVideo(id string) (*YouTubeVideo, error) {
|
||||
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails&id=%s&key=%s",
|
||||
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s",
|
||||
id, os.Getenv("YOUTUBE_API_KEY"))
|
||||
jsonString := ""
|
||||
|
||||
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.")
|
||||
} else {
|
||||
return nil, errors.New("Invalid YouTube ID supplied.")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return nil, errors.New("An error occurred while receiving HTTP GET response.")
|
||||
if response, err := PerformGetRequest(url); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jsonData := map[string]interface{}{}
|
||||
decoder := json.NewDecoder(strings.NewReader(jsonString))
|
||||
decoder.Decode(&jsonData)
|
||||
jq := jsonq.NewQuery(jsonData)
|
||||
|
||||
title, _ := jq.String("items", "0", "snippet", "title")
|
||||
thumbnail, _ := jq.String("items", "0", "snippet", "thumbnails", "high", "url")
|
||||
duration, _ := jq.String("items", "0", "contentDetails", "duration")
|
||||
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])
|
||||
|
@ -83,3 +62,44 @@ func GetYouTubeVideo(id string) (*YouTubeVideo, error) {
|
|||
}
|
||||
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",
|
||||
id, os.Getenv("YOUTUBE_API_KEY"))
|
||||
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) {
|
||||
jsonString := ""
|
||||
|
||||
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.")
|
||||
}
|
||||
return nil, errors.New("Invalid YouTube ID supplied.")
|
||||
}
|
||||
} else {
|
||||
return nil, errors.New("An error occurred while receiving HTTP GET response.")
|
||||
}
|
||||
|
||||
jsonData := map[string]interface{}{}
|
||||
decoder := json.NewDecoder(strings.NewReader(jsonString))
|
||||
decoder.Decode(&jsonData)
|
||||
jq := jsonq.NewQuery(jsonData)
|
||||
|
||||
return jq, nil
|
||||
}
|
||||
|
|
Reference in a new issue