parser.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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").Each(func(i int, s *goquery.Selection) {
  95. if src, ok := s.Attr("data-thumb"); ok {
  96. newImgUrl := re.ReplaceAllString(src, ".")
  97. ret = append(ret, newImgUrl)
  98. }
  99. })
  100. return
  101. }
  102. func getPrice(body string) (ret string) {
  103. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  104. util.CheckError(err)
  105. s := doc.Find("span.woocommerce-Price-amount.amount")
  106. s.Each(func(i int, selection *goquery.Selection) {
  107. if i == 0 {
  108. ret = selection.Text()
  109. }
  110. })
  111. //log.Println(s.Nodes[0].FirstChild.FirstChild.Data)
  112. //log.Println(s.Nodes[0].FirstChild.NextSibling.Data)
  113. return
  114. }
  115. func getCategories(body string) (ret string) {
  116. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  117. util.CheckError(err)
  118. s := doc.Find("span.posted_in")
  119. ret = s.Text()
  120. idx := strings.Index(ret, ":")
  121. if idx > 0 {
  122. ret = strings.TrimSpace(ret[idx+1:])
  123. }
  124. //log.Println(s.Nodes[0].FirstChild.FirstChild.Data)
  125. //log.Println(s.Nodes[0].FirstChild.NextSibling.Data)
  126. return
  127. }
  128. func getOptions(body string) (ret model.Option) {
  129. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  130. util.CheckError(err)
  131. s := doc.Find("body select")
  132. s.Each(func(i int, selection *goquery.Selection) {
  133. skippedFistOption := false
  134. v, _ := selection.Attr("name")
  135. if v != "rating" {
  136. selection.Find("option").Each(func(i int, selection *goquery.Selection) {
  137. if skippedFistOption {
  138. ret.Choices = append(ret.Choices, model.Choice{
  139. Name: selection.Text(),
  140. })
  141. } else {
  142. skippedFistOption = true
  143. }
  144. })
  145. }
  146. })
  147. return
  148. }