parser.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. cats := getCategories(body)
  30. catss := []string{}
  31. if strings.Contains(cats, ",") {
  32. catss = strings.Split(cats, ",")
  33. } else if strings.Contains(cats, "/") {
  34. catss = strings.Split(cats, "/")
  35. }
  36. for _, v := range catss {
  37. item.ItemCategory = append(item.ItemCategory, strings.TrimSpace(v))
  38. }
  39. return
  40. }
  41. func getProductDescription(body string) string {
  42. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  43. util.CheckError(err)
  44. s := doc.Find("div.woocommerce-Tabs-panel--description")
  45. //s.Each(func(i int, selection *goquery.Selection) {
  46. // log.Println(selection.Html())
  47. //})
  48. html, err := s.Html()
  49. if err != nil {
  50. return ""
  51. }
  52. return html
  53. }
  54. func getTextDesc(body string) string {
  55. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  56. util.CheckError(err)
  57. text := doc.Find("div.woocommerce-tabs").Text()
  58. return text
  59. }
  60. func getProduct(body string) (ret WordPressProduct) {
  61. jsonStr := getJson(body)
  62. if idx := strings.Index(jsonStr, "@graph"); idx >= 0 {
  63. p := WordPressItem{}
  64. json.Unmarshal([]byte(jsonStr), &p)
  65. ret = p.Graph[1]
  66. } else {
  67. json.Unmarshal([]byte(jsonStr), &ret)
  68. }
  69. return
  70. }
  71. func getJson(body string) string {
  72. idx := strings.Index(body, "<script type=\"application/ld+json\">")
  73. if idx < 0 {
  74. return ""
  75. }
  76. body = body[idx+len("<script type=\"application/ld+json\">"):]
  77. idx = strings.Index(body, "</script>")
  78. body = body[:idx]
  79. return body
  80. }
  81. func getImages(body string) (ret []string) {
  82. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  83. util.CheckError(err)
  84. //doc.Find("img.wp-post-image").Each(func(i int, s *goquery.Selection) {
  85. // if src, ok := s.Attr("src"); ok {
  86. // width, _ := s.Attr("width")
  87. // height, _ := s.Attr("height")
  88. // w, _ := strconv.ParseInt(width, 10, 64)
  89. // h, _ := strconv.ParseInt(height, 10, 64)
  90. // //log.Println(src, width, height)
  91. // ret = append(ret, model.Image{
  92. // Path: src,
  93. // Width: int(w),
  94. // Height: int(h),
  95. // })
  96. // }
  97. //})
  98. //sort.Slice(ret, func(i, j int) bool {
  99. // return ret[i].Width*ret[i].Height > ret[j].Width*ret[j].Height
  100. //})
  101. //ret = ret[:1]
  102. // re := regexp.MustCompile(`-\d+x\d+\.`)
  103. doc.Find("figure.woocommerce-product-gallery__wrapper div a").Each(func(i int, s *goquery.Selection) {
  104. if src, ok := s.Attr("href"); ok {
  105. ret = append(ret, src)
  106. }
  107. })
  108. return
  109. }
  110. func getPrice(body string) (ret string) {
  111. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  112. util.CheckError(err)
  113. s := doc.Find("span.woocommerce-Price-amount.amount")
  114. s.Each(func(i int, selection *goquery.Selection) {
  115. if i == 0 {
  116. ret = selection.Text()
  117. }
  118. })
  119. //log.Println(s.Nodes[0].FirstChild.FirstChild.Data)
  120. //log.Println(s.Nodes[0].FirstChild.NextSibling.Data)
  121. return
  122. }
  123. func getCategories(body string) (ret string) {
  124. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  125. util.CheckError(err)
  126. s := doc.Find("span.posted_in")
  127. ret = s.Text()
  128. idx := strings.Index(ret, ":")
  129. if idx > 0 {
  130. ret = strings.TrimSpace(ret[idx+1:])
  131. }
  132. //log.Println(s.Nodes[0].FirstChild.FirstChild.Data)
  133. //log.Println(s.Nodes[0].FirstChild.NextSibling.Data)
  134. return
  135. }
  136. func getOptions(body string) (ret model.Option) {
  137. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  138. util.CheckError(err)
  139. s := doc.Find("body select")
  140. s.Each(func(i int, selection *goquery.Selection) {
  141. skippedFistOption := false
  142. v, _ := selection.Attr("name")
  143. if v != "rating" {
  144. selection.Find("option").Each(func(i int, selection *goquery.Selection) {
  145. if skippedFistOption {
  146. ret.Choices = append(ret.Choices, model.Choice{
  147. Name: selection.Text(),
  148. })
  149. } else {
  150. skippedFistOption = true
  151. }
  152. })
  153. }
  154. })
  155. return
  156. }