parser.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(uri string, item *model.ItemInfo) {
  13. body, err := util.Get(uri)
  14. util.CheckError(err)
  15. p := getProduct(body)
  16. item.Images = append(item.Images, p.Image)
  17. item.Images = append(item.Images, getImages(body)...)
  18. item.SalesPrice = util.GetFloat32(p.Offers[0].Price)
  19. item.Sku = fmt.Sprintf("%v", p.Sku)
  20. item.ShortDesc = p.Description
  21. item.OriginDesc = getProductDescription(body)
  22. item.TextDesc = getTextDesc(body)
  23. item.ItemName = p.Name
  24. item.Currency = p.Offers[0].Pricecurrency
  25. item.Language = common.GetLanguage(body)
  26. item.Emails = common.GetEmails(body)
  27. item.Options = append(item.Options, getOptions(body))
  28. return
  29. }
  30. func getProductDescription(body string) string {
  31. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  32. util.CheckError(err)
  33. s := doc.Find("div.woocommerce-Tabs-panel--description")
  34. //s.Each(func(i int, selection *goquery.Selection) {
  35. // log.Println(selection.Html())
  36. //})
  37. html, err := s.Html()
  38. if err != nil {
  39. return ""
  40. }
  41. return html
  42. }
  43. func getTextDesc(body string) string {
  44. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  45. util.CheckError(err)
  46. text := doc.Find("div.woocommerce-tabs").Text()
  47. return text
  48. }
  49. func getProduct(body string) (ret WordPressProduct) {
  50. jsonStr := getJson(body)
  51. if idx := strings.Index(jsonStr, "@graph"); idx >= 0 {
  52. p := WordPressItem{}
  53. json.Unmarshal([]byte(jsonStr), &p)
  54. ret = p.Graph[1]
  55. } else {
  56. json.Unmarshal([]byte(jsonStr), &ret)
  57. }
  58. return
  59. }
  60. func getJson(body string) string {
  61. idx := strings.Index(body, "<script type=\"application/ld+json\">")
  62. if idx < 0 {
  63. return ""
  64. }
  65. body = body[idx+len("<script type=\"application/ld+json\">"):]
  66. idx = strings.Index(body, "</script>")
  67. body = body[:idx]
  68. return body
  69. }
  70. func getImages(body string) (ret []string) {
  71. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  72. util.CheckError(err)
  73. //doc.Find("img.wp-post-image").Each(func(i int, s *goquery.Selection) {
  74. // if src, ok := s.Attr("src"); ok {
  75. // width, _ := s.Attr("width")
  76. // height, _ := s.Attr("height")
  77. // w, _ := strconv.ParseInt(width, 10, 64)
  78. // h, _ := strconv.ParseInt(height, 10, 64)
  79. // //log.Println(src, width, height)
  80. // ret = append(ret, model.Image{
  81. // Path: src,
  82. // Width: int(w),
  83. // Height: int(h),
  84. // })
  85. // }
  86. //})
  87. //sort.Slice(ret, func(i, j int) bool {
  88. // return ret[i].Width*ret[i].Height > ret[j].Width*ret[j].Height
  89. //})
  90. //ret = ret[:1]
  91. re := regexp.MustCompile(`-\d+x\d+\.`)
  92. doc.Find("figure.woocommerce-product-gallery__wrapper div").Each(func(i int, s *goquery.Selection) {
  93. if src, ok := s.Attr("data-thumb"); ok {
  94. newImgUrl := re.ReplaceAllString(src, ".")
  95. ret = append(ret, newImgUrl)
  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. }