tct-main.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, err := parse(p.Uri, body)
  70. if err != nil {
  71. prodInfo.ItemNick = "Parsing Failed"
  72. }
  73. // 개별 prodInfo가 계속 추가될 수 있도록 코드를 변경요.
  74. vRet.ProductPage = append(vRet.ProductPage, toProductPage(prodInfo))
  75. }
  76. // ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  77. ret, _ := json.Marshal(vRet)
  78. return c.JSONBlob(http.StatusOK, ret)
  79. }
  80. func toProductPage(info model.ItemInfo) locals.Product {
  81. ret := locals.Product{
  82. SolutionName: locals.SolutionType(info.SolutionName),
  83. Version: info.Version,
  84. Emails: info.Emails,
  85. DomainName: info.DomainName,
  86. DomainURI: info.DomainURI,
  87. ItemName: info.ItemName,
  88. ItemNick: info.ItemNick,
  89. ModelName: info.ModelName,
  90. ModelNo: info.ModelNo,
  91. BrandName: info.BrandName,
  92. Sku: info.Sku,
  93. ItemCategory: info.ItemCategory,
  94. Manufacturer: info.Manufacturer,
  95. Origin: info.Origin,
  96. Language: info.Language,
  97. Currency: info.Currency,
  98. SalesPrice: info.SalesPrice,
  99. DeliveryPrice: info.DeliveryPrice,
  100. MinimumQty: info.MinimumQty,
  101. UserCredit: info.UserCredit,
  102. Options: nil,
  103. Images: info.Images,
  104. ShortDesc: info.ShortDesc,
  105. OriginDesc: info.OriginDesc,
  106. TextDesc: info.TextDesc,
  107. }
  108. for i, v := range info.Options {
  109. ret.Options = append(ret.Options, locals.Option{
  110. Name: v.Name,
  111. })
  112. ret.Options[i].Choices = make([]locals.Choice, 0)
  113. for _, choice := range v.Choices {
  114. ret.Options[i].Choices = append(ret.Options[i].Choices, locals.Choice{
  115. Name: choice.Name,
  116. Price: choice.Price,
  117. })
  118. }
  119. }
  120. return ret
  121. }
  122. type ItemUrlScrapReq struct {
  123. ItemUrl string
  124. }
  125. func ItemUrlScrap(c echo.Context) error {
  126. v := c.Get("receiver").(ItemUrlScrapReq)
  127. body, err := util.Get(v.ItemUrl)
  128. if err != nil {
  129. return err
  130. }
  131. itemInfo, err := parse(v.ItemUrl, body)
  132. if err != nil {
  133. return c.String(604, "ertvwerawqfd-ItemUrl Parse failed: "+err.Error())
  134. }
  135. ret, _ := json.MarshalIndent(itemInfo, "", "\t")
  136. // fmt.Println(string(data))
  137. // ret, _ := json.Marshal(itemInfo)
  138. return c.JSONBlob(http.StatusOK, ret)
  139. }
  140. var regexpTitle *regexp.Regexp
  141. func init() {
  142. regexpTitle, _ = regexp.Compile("<title>(.*)</title>")
  143. }
  144. func getTitle(body string) string {
  145. ss := regexpTitle.FindAllStringSubmatch(body, 1)
  146. if len(ss) == 1 {
  147. return ss[0][1]
  148. }
  149. return ""
  150. }
  151. func parse(uri, body string) (ret model.ItemInfo, err error) {
  152. t, theme, err := getSolutionType(body)
  153. if err != nil {
  154. return
  155. }
  156. ret = parseSolution(t, theme, uri, body)
  157. return
  158. }
  159. func parseSolution(t model.SolutionType, theme, uri, body string) (ret model.ItemInfo) {
  160. ret.SolutionName = t
  161. u, err := url.Parse(uri)
  162. if err != nil {
  163. return
  164. }
  165. ret.DomainName = u.Host
  166. ret.DomainURI = uri
  167. if t == model.SolutionTypeWooCommerce {
  168. wordpress.Parse(body, &ret)
  169. } else if t == model.SolutionTypeShopify {
  170. shopify.Parse(body, &ret)
  171. } else if t == model.SolutionTypeMagento {
  172. magento.Parse(body, &ret)
  173. } else if t == model.SolutionTypeCafe24 {
  174. cafe24.Parse(body, &ret)
  175. } else if t == model.SolutionTypeGodo {
  176. godo.Parse(body, &ret)
  177. } else if t == model.SolutionTypeYoung {
  178. young.Parse(body, &ret)
  179. } else if t == model.SolutionTypeOthers {
  180. magento.Parse(body, &ret)
  181. }
  182. return
  183. }
  184. func getSolutionType(body string) (t model.SolutionType, theme string, reterr error) {
  185. if strings.Contains(body, "window.CAFE24") {
  186. t = model.SolutionTypeCafe24
  187. } else if strings.Contains(body, "woocommerce-page") {
  188. t = model.SolutionTypeWooCommerce
  189. } else if strings.Contains(body, "고도몰5") {
  190. t = model.SolutionTypeGodo
  191. } else if strings.Contains(body, "cdn.shopify.com") {
  192. t = model.SolutionTypeShopify
  193. } else if strings.Contains(body, "magento") {
  194. t = model.SolutionTypeMagento
  195. } else if strings.Contains(body, "it_id=") {
  196. t = model.SolutionTypeYoung
  197. } else {
  198. t = model.SolutionTypeOthers
  199. //reterr = errors.New("no found solution type")
  200. }
  201. theme = ""
  202. re := regexp.MustCompile(`wp-content\/themes\/(.+?)\/`)
  203. result := re.FindStringSubmatch(body)
  204. if len(result) > 1 {
  205. theme = result[1]
  206. } else {
  207. theme = "generic"
  208. }
  209. //ioutil.WriteFile(string(t) + ".html", []byte(body), 644)
  210. return
  211. }