123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package controllers_scraper
- import (
- "encoding/json"
- "fmt"
- "kkscrap-go/controllers/scraper/cafe24"
- "kkscrap-go/controllers/scraper/godo"
- "kkscrap-go/controllers/scraper/magento"
- "kkscrap-go/controllers/scraper/shopify"
- "kkscrap-go/locals"
- "kkscrap-go/controllers/scraper/wordpress"
- "kkscrap-go/controllers/scraper/young"
- "kkscrap-go/model"
- "net/http"
- "net/url"
- "regexp"
- "strings"
- util "kkscrap-go/controllers/scraper/util"
- // "golang.org/x/crypto/bcrypt"
- "github.com/labstack/echo"
- )
- type SolutionTypeGetReq struct {
- Url string
- }
- func SolutionTypeGet(c echo.Context) error {
- v := c.Get("receiver").(SolutionTypeGetReq)
- retv := &struct {
- SolutionType string
- ThemeType string
- }{}
- // itemInfo, err := parse(v.ItemUrl)
- // if err != nil {
- // return c.String(604, "ertvwerawqfd-ItemUrl Parse failed: "+err.Error())
- // }
- fmt.Println(v.Url)
- retv.SolutionType = "Wordpress"
- retv.ThemeType = "Avada"
- // ret, _ := json.MarshalIndent(itemInfo, "", "\t")
- ret, _ := json.Marshal(retv)
- return c.JSONBlob(http.StatusOK, ret)
- }
- type ProductPageGetReq struct {
- SolutionType string
- ThemeType string
- Products []ProductUri
- }
- type ProductUri struct {
- Uri string
- }
- // 오리지널 웹사이트 전체를 업테이트 하는 경우 Uri를 하나씩 보내면 비효율적이므로 하나의 배치로
- // 묶어서 요청할 수 있도록 한다. 주로 product-page-get를 쓰고 item-url-scrap은 deprecate 예정임.
- func ProductPageGet(c echo.Context) error {
- v := c.Get("receiver").(ProductPageGetReq)
- var vRet locals.ProductPage // Row(개별레코드)->Page(Row의 집합)->Book(Page의 집합)의 개념
- for _, row := range v.Products {
- // (1)Url 의 HTML를 2번 가져오는데 아래와 같이 1번만 가져와서 처리할 수 있도록 수정요.
- // kkk := htmlGet(row.Uri)
- if v.SolutionType == "" {
- // v.SolutionType, v.ThemeType = solType(&kkk)
- }
- prodInfo, err := parse(row.Uri)
- if err != nil {
- prodInfo.ItemNick = "Parsing Failed"
- }
- // 개별 prodInfo가 계속 추가될 수 있도록 코드를 변경요.
- // vRet.ProductPage = append(vRet.ProductPage, *prodInfo)
- }
- // ret, _ := json.MarshalIndent(itemInfo, "", "\t")
- ret, _ := json.Marshal(vRet)
- return c.JSONBlob(http.StatusOK, ret)
- }
- type ItemUrlScrapReq struct {
- ItemUrl string
- }
- func ItemUrlScrap(c echo.Context) error {
- v := c.Get("receiver").(ItemUrlScrapReq)
- // retv := &struct {
- // model.ItemInfo
- // }{}
- itemInfo, err := parse(v.ItemUrl)
- if err != nil {
- return c.String(604, "ertvwerawqfd-ItemUrl Parse failed: "+err.Error())
- }
- ret, _ := json.MarshalIndent(itemInfo, "", "\t")
- // fmt.Println(string(data))
- // ret, _ := json.Marshal(itemInfo)
- return c.JSONBlob(http.StatusOK, ret)
- }
- var regexpTitle *regexp.Regexp
- func init() {
- regexpTitle, _ = regexp.Compile("<title>(.*)</title>")
- }
- func getTitle(body string) string {
- ss := regexpTitle.FindAllStringSubmatch(body, 1)
- if len(ss) == 1 {
- return ss[0][1]
- }
- return ""
- }
- func parse(uri string) (ret model.ItemInfo, err error) {
- t, err := getSolutionType(uri)
- if err != nil {
- return
- }
- ret = parseSolution(t, uri)
- return
- }
- func parseSolution(t model.SolutionType, uri string) (ret model.ItemInfo) {
- ret.SolutionName = t
- u, err := url.Parse(uri)
- if err != nil {
- return
- }
- ret.DomainName = u.Host
- ret.DomainURI = uri
- if t == model.SolutionTypeWooCommerce {
- wordpress.Parse(uri, &ret)
- } else if t == model.SolutionTypeShopify {
- shopify.Parse(uri, &ret)
- } else if t == model.SolutionTypeMagento {
- magento.Parse(uri, &ret)
- } else if t == model.SolutionTypeCafe24 {
- cafe24.Parse(uri, &ret)
- } else if t == model.SolutionTypeGodo {
- godo.Parse(uri, &ret)
- } else if t == model.SolutionTypeYoung {
- young.Parse(uri, &ret)
- } else if t == model.SolutionTypeOthers {
- magento.Parse(uri, &ret)
- }
- return
- }
- func getSolutionType(url string) (t model.SolutionType, reterr error) {
- body, err := util.Get(url)
- if err != nil {
- reterr = err
- return
- }
- if strings.Contains(body, "window.CAFE24") {
- t = model.SolutionTypeCafe24
- } else if strings.Contains(body, "woocommerce-page") {
- t = model.SolutionTypeWooCommerce
- } else if strings.Contains(body, "고도몰5") {
- t = model.SolutionTypeGodo
- } else if strings.Contains(body, "cdn.shopify.com") {
- t = model.SolutionTypeShopify
- } else if strings.Contains(body, "magento") {
- t = model.SolutionTypeMagento
- } else if strings.Contains(body, "it_id=") {
- t = model.SolutionTypeYoung
- } else {
- t = model.SolutionTypeOthers
- //reterr = errors.New("no found solution type")
- }
- //ioutil.WriteFile(string(t) + ".html", []byte(body), 644)
- return
- }
|