You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

grab.go 1.3KB

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