tct-main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 ProductPageGetReq struct {
  42. SolutionType string
  43. ThemeType string
  44. Products []ProductUri
  45. }
  46. type ProductUri struct {
  47. Uri string
  48. }
  49. // 오리지널 웹사이트 전체를 업테이트 하는 경우 Uri를 하나씩 보내면 비효율적이므로 하나의 배치로
  50. // 묶어서 요청할 수 있도록 한다. 주로 product-page-get를 쓰고 item-url-scrap은 deprecate 예정임.
  51. func ProductPageGet(c echo.Context) error {
  52. v := c.Get("receiver").(ProductPageGetReq)
  53. var vRet locals.ProductPage // Row(개별레코드)->Page(Row의 집합)->Book(Page의 집합)의 개념
  54. for _, row := range v.Products {
  55. // (1)Url 의 HTML를 2번 가져오는데 아래와 같이 1번만 가져와서 처리할 수 있도록 수정요.
  56. // kkk := htmlGet(row.Uri)
  57. if v.SolutionType == "" {
  58. // v.SolutionType, v.ThemeType = solType(&kkk)
  59. }
  60. prodInfo, err := parse(row.Uri)
  61. if err != nil {
  62. prodInfo.ItemNick = "Parsing Failed"
  63. }
  64. // 개별 prodInfo가 계속 추가될 수 있도록 코드를 변경요.
  65. // vRet.ProductPage = append(vRet.ProductPage, *prodInfo)
  66. }
  67. // ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  68. ret, _ := json.Marshal(vRet)
  69. return c.JSONBlob(http.StatusOK, ret)
  70. }
  71. type ItemUrlScrapReq struct {
  72. ItemUrl string
  73. }
  74. func ItemUrlScrap(c echo.Context) error {
  75. v := c.Get("receiver").(ItemUrlScrapReq)
  76. // retv := &struct {
  77. // model.ItemInfo
  78. // }{}
  79. itemInfo, err := parse(v.ItemUrl)
  80. if err != nil {
  81. return c.String(604, "ertvwerawqfd-ItemUrl Parse failed: "+err.Error())
  82. }
  83. ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  84. // fmt.Println(string(data))
  85. // ret, _ := json.Marshal(itemInfo)
  86. return c.JSONBlob(http.StatusOK, ret)
  87. }
  88. var regexpTitle *regexp.Regexp
  89. func init() {
  90. regexpTitle, _ = regexp.Compile("<title>(.*)</title>")
  91. }
  92. func getTitle(body string) string {
  93. ss := regexpTitle.FindAllStringSubmatch(body, 1)
  94. if len(ss) == 1 {
  95. return ss[0][1]
  96. }
  97. return ""
  98. }
  99. func parse(uri string) (ret model.ItemInfo, err error) {
  100. t, err := getSolutionType(uri)
  101. if err != nil {
  102. return
  103. }
  104. ret = parseSolution(t, uri)
  105. return
  106. }
  107. func parseSolution(t model.SolutionType, uri string) (ret model.ItemInfo) {
  108. ret.SolutionName = t
  109. u, err := url.Parse(uri)
  110. if err != nil {
  111. return
  112. }
  113. ret.DomainName = u.Host
  114. ret.DomainURI = uri
  115. if t == model.SolutionTypeWooCommerce {
  116. wordpress.Parse(uri, &ret)
  117. } else if t == model.SolutionTypeShopify {
  118. shopify.Parse(uri, &ret)
  119. } else if t == model.SolutionTypeMagento {
  120. magento.Parse(uri, &ret)
  121. } else if t == model.SolutionTypeCafe24 {
  122. cafe24.Parse(uri, &ret)
  123. } else if t == model.SolutionTypeGodo {
  124. godo.Parse(uri, &ret)
  125. } else if t == model.SolutionTypeYoung {
  126. young.Parse(uri, &ret)
  127. } else if t == model.SolutionTypeOthers {
  128. magento.Parse(uri, &ret)
  129. }
  130. return
  131. }
  132. func getSolutionType(url string) (t model.SolutionType, reterr error) {
  133. body, err := util.Get(url)
  134. if err != nil {
  135. reterr = err
  136. return
  137. }
  138. if strings.Contains(body, "window.CAFE24") {
  139. t = model.SolutionTypeCafe24
  140. } else if strings.Contains(body, "woocommerce-page") {
  141. t = model.SolutionTypeWooCommerce
  142. } else if strings.Contains(body, "고도몰5") {
  143. t = model.SolutionTypeGodo
  144. } else if strings.Contains(body, "cdn.shopify.com") {
  145. t = model.SolutionTypeShopify
  146. } else if strings.Contains(body, "magento") {
  147. t = model.SolutionTypeMagento
  148. } else if strings.Contains(body, "it_id=") {
  149. t = model.SolutionTypeYoung
  150. } else {
  151. t = model.SolutionTypeOthers
  152. //reterr = errors.New("no found solution type")
  153. }
  154. //ioutil.WriteFile(string(t) + ".html", []byte(body), 644)
  155. return
  156. }