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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package apiclassic
  2. import (
  3. "net/http"
  4. "errors"
  5. "encoding/xml"
  6. "github.com/terorie/yt-mango/net"
  7. "fmt"
  8. )
  9. const videoURL = "https://www.youtube.com/watch?has_verified=1&bpctr=6969696969&v="
  10. const subtitleURL = "https://video.google.com/timedtext?type=list&v="
  11. const channelURL = "https://www.youtube.com/channel/%s/about"
  12. func GrabVideo(videoID string) *http.Request {
  13. req, err := http.NewRequest("GET", videoURL + videoID, nil)
  14. if err != nil { panic(err) }
  15. setHeaders(&req.Header)
  16. return req
  17. }
  18. func GrabSubtitleList(videoID string) (tracks *XMLSubTrackList, err error) {
  19. req, err := http.NewRequest("GET", subtitleURL + videoID, nil)
  20. if err != nil { return }
  21. setHeaders(&req.Header)
  22. res, err := net.Client.Do(req)
  23. if err != nil { return }
  24. if res.StatusCode != 200 { return nil, errors.New("HTTP failure") }
  25. defer res.Body.Close()
  26. decoder := xml.NewDecoder(res.Body)
  27. tracks = new(XMLSubTrackList)
  28. err = decoder.Decode(tracks)
  29. return
  30. }
  31. func GrabChannel(channelID string) *http.Request {
  32. req, err := http.NewRequest("GET", fmt.Sprintf(channelURL, channelID), nil)
  33. if err != nil { panic(err) }
  34. setHeaders(&req.Header)
  35. return req
  36. }
  37. func setHeaders(h *http.Header) {
  38. h.Add("Host", "www.youtube.com")
  39. h.Add("User-Agent", "yt-mango/0.1")
  40. }