parser.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package wordpress
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/PuerkitoBio/goquery"
  6. "kkscrap-go/controllers/scraper/common"
  7. "kkscrap-go/controllers/scraper/util"
  8. "kkscrap-go/model"
  9. "regexp"
  10. "strings"
  11. )
  12. func Parse(body string, item *model.ItemInfo) {
  13. p := getProduct(body)
  14. if p.Image != "" {
  15. item.Images = append(item.Images, p.Image)
  16. }
  17. item.Images = append(item.Images, getImages(body)...)
  18. if p.Offers != nil && len(p.Offers) > 0 {
  19. item.SalesPrice = util.GetFloat32(p.Offers[0].Price)
  20. item.Currency = p.Offers[0].Pricecurrency
  21. }
  22. item.Sku = fmt.Sprintf("%v", p.Sku)
  23. item.ShortDesc = p.Description
  24. item.OriginDesc = getProductDescription(body)
  25. item.TextDesc = getTextDesc(body)
  26. item.ItemName = p.Name
  27. item.Language = common.GetLanguage(body)
  28. item.Emails = common.GetEmails(body)
  29. item.Options = append(item.Options, getOptions(body))
  30. return
  31. }
  32. func getProductDescription(body string) string {
  33. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  34. util.CheckError(err)
  35. s := doc.Find("div.woocommerce-Tabs-panel--description")
  36. //s.Each(func(i int, selection *goquery.Selection) {
  37. // log.Println(selection.Html())
  38. //})
  39. html, err := s.Html()
  40. if err != nil {
  41. return ""
  42. }
  43. return html
  44. }
  45. func getTextDesc(body string) string {
  46. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  47. util.CheckError(err)
  48. text := doc.Find("div.woocommerce-tabs").Text()
  49. return text
  50. }
  51. func getProduct(body string) (ret WordPressProduct) {
  52. jsonStr := getJson(body)
  53. if idx := strings.Index(jsonStr, "@graph"); idx >= 0 {
  54. p := WordPressItem{}
  55. json.Unmarshal([]byte(jsonStr), &p)
  56. ret = p.Graph[1]
  57. } else {
  58. json.Unmarshal([]byte(jsonStr), &ret)
  59. }
  60. return
  61. }
  62. func getJson(body string) string {
  63. idx := strings.Index(body, "<script type=\"application/ld+json\">")
  64. if idx < 0 {
  65. return ""
  66. }
  67. body = body[idx+len("<script type=\"application/ld+json\">"):]
  68. idx = strings.Index(body, "</script>")
  69. body = body[:idx]
  70. return body
  71. }
  72. func getImages(body string) (ret []string) {
  73. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  74. util.CheckError(err)
  75. //doc.Find("img.wp-post-image").Each(func(i int, s *goquery.Selection) {
  76. // if src, ok := s.Attr("src"); ok {
  77. // width, _ := s.Attr("width")
  78. // height, _ := s.Attr("height")
  79. // w, _ := strconv.ParseInt(width, 10, 64)
  80. // h, _ := strconv.ParseInt(height, 10, 64)
  81. // //log.Println(src, width, height)
  82. // ret = append(ret, model.Image{
  83. // Path: src,
  84. // Width: int(w),
  85. // Height: int(h),
  86. // })
  87. // }
  88. //})
  89. //sort.Slice(ret, func(i, j int) bool {
  90. // return ret[i].Width*ret[i].Height > ret[j].Width*ret[j].Height
  91. //})
  92. //ret = ret[:1]
  93. re := regexp.MustCompile(`-\d+x\d+\.`)
  94. doc.Find("figure.woocommerce-product-gallery__wrapper div a").Each(func(i int, s *goquery.Selection) {
  95. if src, ok := s.Attr("href"); ok {
  96. ret = append(ret, src)
  97. }
  98. })
  99. return
  100. }
  101. func getPrice(body string) (ret string) {
  102. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  103. util.CheckError(err)
  104. s := doc.Find("span.woocommerce-Price-amount.amount")
  105. s.Each(func(i int, selection *goquery.Selection) {
  106. if i == 0 {
  107. ret = selection.Text()
  108. }
  109. })
  110. //log.Println(s.Nodes[0].FirstChild.FirstChild.Data)
  111. //log.Println(s.Nodes[0].FirstChild.NextSibling.Data)
  112. return
  113. }
  114. func getCategories(body string) (ret string) {
  115. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  116. util.CheckError(err)
  117. s := doc.Find("span.posted_in")
  118. ret = s.Text()
  119. idx := strings.Index(ret, ":")
  120. if idx > 0 {
  121. ret = strings.TrimSpace(ret[idx+1:])
  122. }
  123. //log.Println(s.Nodes[0].FirstChild.FirstChild.Data)
  124. //log.Println(s.Nodes[0].FirstChild.NextSibling.Data)
  125. return
  126. }
  127. func getOptions(body string) (ret model.Option) {
  128. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  129. util.CheckError(err)
  130. s := doc.Find("body select")
  131. s.Each(func(i int, selection *goquery.Selection) {
  132. skippedFistOption := false
  133. v, _ := selection.Attr("name")
  134. if v != "rating" {
  135. selection.Find("option").Each(func(i int, selection *goquery.Selection) {
  136. if skippedFistOption {
  137. ret.Choices = append(ret.Choices, model.Choice{
  138. Name: selection.Text(),
  139. })
  140. } else {
  141. skippedFistOption = true
  142. }
  143. })
  144. }
  145. })
  146. return
  147. }