Move work command
[yt-mango.git] / apiclassic / grab.go
blob24fe469a03e86c63570a6ecea66a5bad4dd763e0
1 package apiclassic
3 import (
4 "net/http"
5 "errors"
6 "encoding/xml"
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 }
28 return
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)
46 return
49 func setHeaders(h *http.Header) {
50 h.Add("Host", "www.youtube.com")
51 h.Add("User-Agent", "yt-mango/0.1")