/
article_youtube_fetcher.go
77 lines (61 loc) · 1.55 KB
/
article_youtube_fetcher.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
75
76
77
package generators
import (
"github.com/pkg/errors"
"io/ioutil"
"net/http"
netUrl "net/url"
)
type articleYoutubeFetcher struct {
}
func (g *articleYoutubeFetcher) Fetch(url string) (string, string, error) {
title, err := g.getTitle(url)
if err != nil {
return "", "", errors.Wrap(err, "failed to get title")
}
videoID, err := g.getVideoID(url)
if err != nil {
return "", "", errors.Wrap(err, "failed to get video id")
}
return title, videoID, nil
}
func (g *articleYoutubeFetcher) IsFetchable(url string) bool {
u, err := netUrl.Parse(url)
if err != nil {
return false
}
cond := u.Scheme == "https"
cond = cond && u.Host == "www.youtube.com"
cond = cond && u.Path == "/watch"
queryParams, err := netUrl.ParseQuery(u.RawQuery)
if err != nil {
return false
}
_, ok := queryParams["v"]
cond = cond && ok
return cond
}
func (g *articleYoutubeFetcher) getTitle(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", errors.Wrap(err, "failed to request url")
}
defer resp.Body.Close()
htmlByte, err := ioutil.ReadAll(resp.Body)
html := string(htmlByte)
return getTitleFromHtml(html)
}
func (g *articleYoutubeFetcher) getVideoID(url string) (string, error) {
u, err := netUrl.Parse(url)
if err != nil {
return "", errors.Wrap(err, "failed to parse url")
}
queryParams, err := netUrl.ParseQuery(u.RawQuery)
if err != nil {
return "", errors.Wrap(err, "failed to parse raw query")
}
videoID, ok := queryParams["v"]
if !ok {
return "", errors.New("video id not exists")
}
return videoID[0], nil
}