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.

parse.go 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package browseajax
  2. import (
  3. "github.com/valyala/fastjson"
  4. "errors"
  5. )
  6. var missingData = errors.New("missing data")
  7. func ParsePage(rootObj *fastjson.Value) error {
  8. // Root as array
  9. root, err := rootObj.Array()
  10. if err != nil { return err }
  11. // Find response container
  12. var container *fastjson.Value
  13. for _, item := range root {
  14. if item.Exists("response") {
  15. container = item
  16. break
  17. }
  18. }
  19. if container == nil { return missingData }
  20. // Get error obj
  21. // Get items from grid
  22. itemsObj := container.Get(
  23. "response",
  24. "continuationContents",
  25. "gridContinuation",
  26. "items",
  27. )
  28. if itemsObj == nil { return missingData }
  29. // Items as array
  30. items, err := itemsObj.Array()
  31. if err != nil { return err }
  32. // Enumerate
  33. for _, item := range items {
  34. // Find URL
  35. urlObj := item.Get(
  36. "gridVideoRenderer",
  37. "navigationEndpoint",
  38. "commandMetadata",
  39. "webCommandMetadata",
  40. "url",
  41. )
  42. if urlObj == nil { return missingData }
  43. // URL as string
  44. urlBytes, err := urlObj.StringBytes()
  45. if err != nil { return err }
  46. url := string(urlBytes)
  47. println(url)
  48. }
  49. return nil
  50. }