tct-main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package controllers_scraper
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "kkscrap-go/controllers/scraper/cafe24"
  6. "kkscrap-go/controllers/scraper/godo"
  7. "kkscrap-go/controllers/scraper/magento"
  8. "kkscrap-go/controllers/scraper/shopify"
  9. "kkscrap-go/locals"
  10. "kkscrap-go/controllers/scraper/wordpress"
  11. "kkscrap-go/controllers/scraper/young"
  12. "kkscrap-go/model"
  13. "net/http"
  14. "net/url"
  15. "regexp"
  16. "strings"
  17. util "kkscrap-go/controllers/scraper/util"
  18. // "golang.org/x/crypto/bcrypt"
  19. "github.com/labstack/echo"
  20. )
  21. type SolutionTypeGetReq struct {
  22. Url string
  23. }
  24. func SolutionTypeGet(c echo.Context) error {
  25. v := c.Get("receiver").(SolutionTypeGetReq)
  26. retv := &struct {
  27. SolutionType string
  28. ThemeType string
  29. }{}
  30. // itemInfo, err := parse(v.ItemUrl)
  31. // if err != nil {
  32. // return c.String(604, "ertvwerawqfd-ItemUrl Parse failed: "+err.Error())
  33. // }
  34. fmt.Println(v.Url)
  35. retv.SolutionType = "Wordpress"
  36. retv.ThemeType = "Avada"
  37. // ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  38. ret, _ := json.Marshal(retv)
  39. return c.JSONBlob(http.StatusOK, ret)
  40. }
  41. type ProductPageParseReq struct {
  42. SolutionType string
  43. ThemeType string
  44. Products []ProductUri
  45. }
  46. type ProductUri struct {
  47. Uri string
  48. }
  49. // 오리지널 웹사이트 전체를 업테이트 하는 경우 Uri를 하나씩 보내면 비효율적이므로 하나의 배치로
  50. // 묶어서 요청할 수 있도록 한다. 주로 product-page-parse를 쓰고 item-url-scrap은 deprecate 예정임.
  51. func ProductPageParse(c echo.Context) error {
  52. v := c.Get("receiver").(ProductPageParseReq)
  53. var vRet locals.ProductPage // Row(개별레코드)->Page(Row의 집합)->Book(Page의 집합)의 개념
  54. for _, row := range v.Products {
  55. prodInfo, err := parse(row.Uri)
  56. if err != nil {
  57. prodInfo.ItemNick = "Parsing Failed"
  58. }
  59. // 개별 prodInfo가 계속 추가될 수 있도록 코드를 변경해주세요.
  60. // vRet.ProductPage = append(vRet.ProductPage, *prodInfo)
  61. }
  62. // ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  63. ret, _ := json.Marshal(vRet)
  64. return c.JSONBlob(http.StatusOK, ret)
  65. }
  66. type ItemUrlScrapReq struct {
  67. ItemUrl string
  68. }
  69. func ItemUrlScrap(c echo.Context) error {
  70. v := c.Get("receiver").(ItemUrlScrapReq)
  71. // retv := &struct {
  72. // model.ItemInfo
  73. // }{}
  74. itemInfo, err := parse(v.ItemUrl)
  75. if err != nil {
  76. return c.String(604, "ertvwerawqfd-ItemUrl Parse failed: "+err.Error())
  77. }
  78. ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  79. // fmt.Println(string(data))
  80. // ret, _ := json.Marshal(itemInfo)
  81. return c.JSONBlob(http.StatusOK, ret)
  82. }
  83. var regexpTitle *regexp.Regexp
  84. func init() {
  85. regexpTitle, _ = regexp.Compile("<title>(.*)</title>")
  86. }
  87. func getTitle(body string) string {
  88. ss := regexpTitle.FindAllStringSubmatch(body, 1)
  89. if len(ss) == 1 {
  90. return ss[0][1]
  91. }
  92. return ""
  93. }
  94. func parse(uri string) (ret model.ItemInfo, err error) {
  95. t, err := getSolutionType(uri)
  96. if err != nil {
  97. return
  98. }
  99. ret = parseSolution(t, uri)
  100. return
  101. }
  102. func parseSolution(t model.SolutionType, uri string) (ret model.ItemInfo) {
  103. ret.SolutionName = t
  104. u, err := url.Parse(uri)
  105. if err != nil {
  106. return
  107. }
  108. ret.DomainName = u.Host
  109. ret.DomainURI = uri
  110. if t == model.SolutionTypeWooCommerce {
  111. wordpress.Parse(uri, &ret)
  112. } else if t == model.SolutionTypeShopify {
  113. shopify.Parse(uri, &ret)
  114. } else if t == model.SolutionTypeMagento {
  115. magento.Parse(uri, &ret)
  116. } else if t == model.SolutionTypeCafe24 {
  117. cafe24.Parse(uri, &ret)
  118. } else if t == model.SolutionTypeGodo {
  119. godo.Parse(uri, &ret)
  120. } else if t == model.SolutionTypeYoung {
  121. young.Parse(uri, &ret)
  122. } else if t == model.SolutionTypeOthers {
  123. magento.Parse(uri, &ret)
  124. }
  125. return
  126. }
  127. func getSolutionType(url string) (t model.SolutionType, reterr error) {
  128. body, err := util.Get(url)
  129. if err != nil {
  130. reterr = err
  131. return
  132. }
  133. if strings.Contains(body, "window.CAFE24") {
  134. t = model.SolutionTypeCafe24
  135. } else if strings.Contains(body, "woocommerce-page") {
  136. t = model.SolutionTypeWooCommerce
  137. } else if strings.Contains(body, "고도몰5") {
  138. t = model.SolutionTypeGodo
  139. } else if strings.Contains(body, "cdn.shopify.com") {
  140. t = model.SolutionTypeShopify
  141. } else if strings.Contains(body, "magento") {
  142. t = model.SolutionTypeMagento
  143. } else if strings.Contains(body, "it_id=") {
  144. t = model.SolutionTypeYoung
  145. } else {
  146. t = model.SolutionTypeOthers
  147. //reterr = errors.New("no found solution type")
  148. }
  149. //ioutil.WriteFile(string(t) + ".html", []byte(body), 644)
  150. return
  151. }