tct-main.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/controllers/scraper/util"
  10. "kkscrap-go/controllers/scraper/wordpress"
  11. "kkscrap-go/controllers/scraper/young"
  12. "kkscrap-go/locals"
  13. "kkscrap-go/model"
  14. "net/http"
  15. "net/url"
  16. "regexp"
  17. "strings"
  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. body, err := util.Get(v.Url)
  31. if err != nil {
  32. return err
  33. }
  34. fmt.Println(v.Url)
  35. st, theme, err := getSolutionType(body)
  36. if err != nil {
  37. return c.JSONBlob(http.StatusOK, []byte(err.Error()))
  38. }
  39. retv.SolutionType = string(st)
  40. retv.ThemeType = theme
  41. return c.JSON(http.StatusOK, retv)
  42. }
  43. type ProductPageGetReq struct {
  44. SolutionType model.SolutionType
  45. ThemeType string
  46. Products []ProductUri
  47. }
  48. type ProductUri struct {
  49. Uri string
  50. }
  51. // 오리지널 웹사이트 전체를 업테이트 하는 경우 Uri를 하나씩 보내면 비효율적이므로 하나의 배치로
  52. // 묶어서 요청할 수 있도록 한다. 주로 product-page-get를 쓰고 item-url-scrap은 deprecate 예정임.
  53. func ProductPageGet(c echo.Context) error {
  54. v := c.Get("receiver").(ProductPageGetReq)
  55. var vRet locals.ProductPage // Row(개별레코드)->Page(Row의 집합)->Book(Page의 집합)의 개념
  56. for _, p := range v.Products {
  57. body, err := util.Get(p.Uri)
  58. if err != nil {
  59. return err
  60. }
  61. // 전체 웹사이트가 아니라 개별 상품페이지(1개페이지)의 경우 SolutionType 없이 request됨
  62. if v.SolutionType == "" {
  63. var err error
  64. v.SolutionType, v.ThemeType, err = getSolutionType(body)
  65. if err != nil {
  66. return c.String(http.StatusBadRequest, err.Error())
  67. }
  68. }
  69. prodInfo := parseSolution(v.SolutionType, v.ThemeType, p.Uri, body)
  70. vRet.ProductPage = append(vRet.ProductPage, toProductPage(prodInfo))
  71. }
  72. // ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  73. ret, _ := json.Marshal(vRet)
  74. return c.JSONBlob(http.StatusOK, ret)
  75. }
  76. func toProductPage(info model.ItemInfo) locals.Product {
  77. ret := locals.Product{
  78. SolutionName: locals.SolutionType(info.SolutionName),
  79. Version: info.Version,
  80. Emails: info.Emails,
  81. DomainName: info.DomainName,
  82. DomainURI: info.DomainURI,
  83. ItemName: info.ItemName,
  84. ItemNick: info.ItemNick,
  85. ModelName: info.ModelName,
  86. ModelNo: info.ModelNo,
  87. BrandName: info.BrandName,
  88. Sku: info.Sku,
  89. ItemCategory: info.ItemCategory,
  90. Manufacturer: info.Manufacturer,
  91. Origin: info.Origin,
  92. Language: info.Language,
  93. Currency: info.Currency,
  94. SalesPrice: info.SalesPrice,
  95. DeliveryPrice: info.DeliveryPrice,
  96. MinimumQty: info.MinimumQty,
  97. UserCredit: info.UserCredit,
  98. Options: nil,
  99. Images: info.Images,
  100. ShortDesc: info.ShortDesc,
  101. OriginDesc: info.OriginDesc,
  102. TextDesc: info.TextDesc,
  103. }
  104. for i, v := range info.Options {
  105. ret.Options = append(ret.Options, locals.Option{
  106. Name: v.Name,
  107. })
  108. ret.Options[i].Choices = make([]locals.Choice, 0)
  109. for _, choice := range v.Choices {
  110. ret.Options[i].Choices = append(ret.Options[i].Choices, locals.Choice{
  111. Name: choice.Name,
  112. Price: choice.Price,
  113. })
  114. }
  115. }
  116. return ret
  117. }
  118. type ItemUrlScrapReq struct {
  119. ItemUrl string
  120. }
  121. func ItemUrlScrap(c echo.Context) error {
  122. v := c.Get("receiver").(ItemUrlScrapReq)
  123. body, err := util.Get(v.ItemUrl)
  124. if err != nil {
  125. return err
  126. }
  127. itemInfo, err := parse(v.ItemUrl, body)
  128. if err != nil {
  129. return c.String(604, "ertvwerawqfd-ItemUrl Parse failed: "+err.Error())
  130. }
  131. ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  132. // fmt.Println(string(data))
  133. // ret, _ := json.Marshal(itemInfo)
  134. return c.JSONBlob(http.StatusOK, ret)
  135. }
  136. var regexpTitle *regexp.Regexp
  137. func init() {
  138. regexpTitle, _ = regexp.Compile("<title>(.*)</title>")
  139. }
  140. func getTitle(body string) string {
  141. ss := regexpTitle.FindAllStringSubmatch(body, 1)
  142. if len(ss) == 1 {
  143. return ss[0][1]
  144. }
  145. return ""
  146. }
  147. func parse(uri, body string) (ret model.ItemInfo, err error) {
  148. t, theme, err := getSolutionType(body)
  149. if err != nil {
  150. return
  151. }
  152. ret = parseSolution(t, theme, uri, body)
  153. return
  154. }
  155. func parseSolution(t model.SolutionType, theme, uri, body string) (ret model.ItemInfo) {
  156. ret.SolutionName = t
  157. u, err := url.Parse(uri)
  158. if err != nil {
  159. return
  160. }
  161. ret.DomainName = u.Host
  162. ret.DomainURI = uri
  163. if t == model.SolutionTypeWooCommerce {
  164. wordpress.Parse(body, &ret)
  165. } else if t == model.SolutionTypeShopify {
  166. shopify.Parse(body, &ret)
  167. } else if t == model.SolutionTypeMagento {
  168. magento.Parse(body, &ret)
  169. } else if t == model.SolutionTypeCafe24 {
  170. cafe24.Parse(body, &ret)
  171. } else if t == model.SolutionTypeGodo {
  172. godo.Parse(body, &ret)
  173. } else if t == model.SolutionTypeYoung {
  174. young.Parse(body, &ret)
  175. } else if t == model.SolutionTypeOthers {
  176. magento.Parse(body, &ret)
  177. }
  178. return
  179. }
  180. func getSolutionType(body string) (t model.SolutionType, theme string, reterr error) {
  181. if strings.Contains(body, "window.CAFE24") {
  182. t = model.SolutionTypeCafe24
  183. } else if strings.Contains(body, "woocommerce-page") {
  184. t = model.SolutionTypeWooCommerce
  185. } else if strings.Contains(body, "고도몰5") {
  186. t = model.SolutionTypeGodo
  187. } else if strings.Contains(body, "cdn.shopify.com") {
  188. t = model.SolutionTypeShopify
  189. } else if strings.Contains(body, "magento") {
  190. t = model.SolutionTypeMagento
  191. } else if strings.Contains(body, "it_id=") {
  192. t = model.SolutionTypeYoung
  193. } else {
  194. t = model.SolutionTypeOthers
  195. //reterr = errors.New("no found solution type")
  196. }
  197. theme = ""
  198. re := regexp.MustCompile(`wp-content\/themes\/(.+?)\/`)
  199. result := re.FindStringSubmatch(body)
  200. if len(result) > 1 {
  201. theme = result[1]
  202. } else {
  203. theme = "generic"
  204. }
  205. //ioutil.WriteFile(string(t) + ".html", []byte(body), 644)
  206. return
  207. }