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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package browseajax
  2. import (
  3. "net/http"
  4. "github.com/terorie/yt-mango/common"
  5. "errors"
  6. "io/ioutil"
  7. "github.com/valyala/fastjson"
  8. )
  9. const mainURL = "https://www.youtube.com/browse_ajax?ctoken="
  10. func GrabPage(channelID string, page uint) (*fastjson.Value, error) {
  11. // Generate page URL
  12. token := GenerateToken(channelID, uint64(page))
  13. url := mainURL + token
  14. // Prepare request
  15. req, err := http.NewRequest("GET", url, nil)
  16. if err != nil { return nil, err }
  17. req.Header.Add("X-YouTube-Client-Name", "1")
  18. req.Header.Add("X-YouTube-Client-Version", "2.20180726")
  19. // Send request
  20. res, err := common.Client.Do(req)
  21. if err != nil { return nil, err }
  22. if res.StatusCode == 500 {
  23. defer res.Body.Close()
  24. buf, _ := ioutil.ReadAll(res.Body)
  25. println(string(buf))
  26. }
  27. if res.StatusCode != 200 { return nil, errors.New("HTTP failure") }
  28. // Download response
  29. defer res.Body.Close()
  30. buf, err := ioutil.ReadAll(res.Body)
  31. if err != nil { return nil, err }
  32. // Parse JSON
  33. var p fastjson.Parser
  34. root, err := p.ParseBytes(buf)
  35. if err != nil { return nil, err }
  36. return root, nil
  37. }