-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.go
74 lines (60 loc) · 1.61 KB
/
youtube.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package dyatl
import (
"encoding/json"
"io/ioutil"
"net/url"
"strings"
"github.com/pkg/errors"
)
func YoutubeIdFromUrl(u *url.URL) string {
switch u.Host {
case "www.youtube.com", "m.youtube.com", "youtube.com":
return u.Query().Get("v")
case "youtu.be":
return strings.Trim(u.Path, "/")
}
return ""
}
type YoutubeData struct {
Type string `json:"type"`
Version string `json:"version"`
Title string `json:"title"`
AuthorName string `json:"author_name"`
AuthorUrl string `json:"author_url"`
Html string `json:"html"`
Width int `json:"width"`
Height int `json:"height"`
ProviderName string `json:"provider_name"`
ProviderUrl string `json:"provider_url"`
ThumbnailUrl string `json:"thumbnail_url"`
ThumbnailHeight int `json:"thumbnail_height"`
ThumbnailWidth int `json:"thumbnail_width"`
}
var (
Unauthorized = errors.New("Unauthorized")
BadRequest = errors.New("Bad Request")
)
func (c Client) GetYoutubeData(id string) (YoutubeData, error) {
var data YoutubeData
dataUrl := "https://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3D" + id
resp, err := c.Get(dataUrl)
if err != nil {
return data, err
}
dataBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return data, errors.Wrapf(err, "read body fail on `%s`", dataUrl)
}
defer resp.Body.Close()
switch string(dataBytes) {
case "Bad Request":
return data, BadRequest
case "Unauthorized":
return data, Unauthorized
default:
if err := json.Unmarshal(dataBytes, &data); err != nil {
return data, errors.Wrapf(err, "unmarshal fail on `%s`", string(dataBytes))
}
}
return data, err
}