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.

parsechannel.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package apiclassic
  2. import (
  3. "github.com/terorie/yt-mango/data"
  4. "net/http"
  5. "errors"
  6. "github.com/PuerkitoBio/goquery"
  7. "strconv"
  8. )
  9. func ParseChannel(c *data.Channel, res *http.Response) (err error) {
  10. if res.StatusCode != 200 { return errors.New("HTTP failure") }
  11. defer res.Body.Close()
  12. doc, err := goquery.NewDocumentFromReader(res.Body)
  13. if err != nil { return }
  14. p := parseChannelInfo{c, doc}
  15. return p.parse()
  16. }
  17. type parseChannelInfo struct {
  18. c *data.Channel
  19. doc *goquery.Document
  20. }
  21. func (p *parseChannelInfo) parse() error {
  22. if err := p.parseMetas();
  23. err != nil { return err }
  24. return nil
  25. }
  26. func (p *parseChannelInfo) parseMetas() error {
  27. p.doc.Find("head").RemoveFiltered("#watch-container")
  28. enumMetas(p.doc.Find("head").Find("meta"), func(tag metaTag)bool {
  29. content := tag.content
  30. switch tag.typ {
  31. case metaProperty:
  32. switch tag.name {
  33. case "og:title":
  34. p.c.Name = content
  35. }
  36. case metaItemProp:
  37. switch tag.name {
  38. case "paid":
  39. if val, err := strconv.ParseBool(content);
  40. err == nil { p.c.Paid = val }
  41. }
  42. }
  43. return false
  44. })
  45. return nil
  46. }
  47. func (p *parseChannelInfo) parseAbout() error {
  48. p.doc.Find(".about-stats").Find(".about-stat").Each(func(_ int, s *goquery.Selection) {
  49. text := s.Text()
  50. println(text)
  51. })
  52. return nil
  53. }