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"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"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 22:20:40 +02:00
|
|
|
// PerformGetRequest does all the grunt work for a YouTube HTTPS GET request.
|
2015-04-09 08:44:48 +02:00
|
|
|
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
|
|
|
}
|