parser.go 4.2 KB

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