7 "github.com/PuerkitoBio/goquery"
8 "github.com/terorie/yt-mango/common"
11 const mainURL
= "https://www.youtube.com/watch?has_verified=1&bpctr=6969696969&v="
12 const subtitleURL
= "https://video.google.com/timedtext?type=list&v="
14 // Grabs a HTML video page and returns the document tree
15 func GrabVideo(videoID
string) (doc
*goquery
.Document
, err error
) {
16 req
, err
:= http
.NewRequest("GET", mainURL
+ videoID
, nil)
17 if err
!= nil { return }
18 setHeaders(&req
.Header
)
20 res
, err
:= common
.Client
.Do(req
)
21 if err
!= nil { return }
22 if res
.StatusCode
!= 200 { return nil, errors
.New("HTTP failure") }
24 defer res
.Body
.Close()
25 doc
, err
= goquery
.NewDocumentFromReader(res
.Body
)
26 if err
!= nil { return nil, err
}
31 // Grabs and parses a subtitle list
32 func GrabSubtitleList(videoID
string) (tracks
*XMLSubTrackList
, err error
) {
33 req
, err
:= http
.NewRequest("GET", subtitleURL
+ videoID
, nil)
34 if err
!= nil { return }
35 setHeaders(&req
.Header
)
37 res
, err
:= common
.Client
.Do(req
)
38 if err
!= nil { return }
39 if res
.StatusCode
!= 200 { return nil, errors
.New("HTTP failure") }
41 defer res
.Body
.Close()
42 decoder
:= xml
.NewDecoder(res
.Body
)
44 tracks
= new(XMLSubTrackList
)
45 err
= decoder
.Decode(tracks
)
49 func setHeaders(h
*http
.Header
) {
50 h
.Add("Host", "www.youtube.com")
51 h
.Add("User-Agent", "yt-mango/0.1")