コード例 #1
0
ファイル: main.go プロジェクト: antonikonovalov/lite_cym
func processingOfCaptcha(url string, sess *webdriver.Session, cap *Capture) error {
	log.Print("START USING ANTIGATE FOR ", url)
	a := antigate.New(*antigateKey)
	captchaText, err := a.ProcessFromUrl(cap.Capture)
	if err != nil {
		return errors.New("can't process captcha from url by antigate url=" + cap.Capture + " err=" + err.Error())
	}
	inputEl, err := sess.FindElement(webdriver.CSS_Selector, `input.input__control.i-bem`)
	if err != nil {
		return errors.New("can't find input element for set capcha text: " + url + " error ::" + err.Error())
	}

	err = inputEl.SendKeys(captchaText)
	if err != nil {
		return errors.New("can't set captcha text by url " + url + " error ::" + err.Error())
	}
	err = inputEl.Submit()
	if err != nil {
		return errors.New("can't submit captcha answer:" + url + " error ::" + err.Error())
	}
	return nil
}
コード例 #2
0
ファイル: main.go プロジェクト: antonikonovalov/lite_cym
func SetVendorData(db *mgo.Session, sess *webdriver.Session, vendorID int) error {
	sess.Url(getVendorPage(vendorID))
	shopJson, err := sess.ExecuteScript(ShopScript, []interface{}{})
	if err != nil {
		return err
	}

	cap := &Capture{}
	err = json.Unmarshal(shopJson, cap)

	if err == nil && len(cap.Capture) > 0 {
		err = processingOfCaptcha(getVendorPage(vendorID), sess, cap)
		if err != nil {
			panic(err)
		}
		sess.Url(getVendorPage(vendorID))
		shopJson, err = sess.ExecuteScript(ShopScript, []interface{}{})
		if err != nil {
			return err
		}
	}

	shop := &Shop{}
	err = json.Unmarshal(shopJson, shop)
	if err != nil {
		return err
	}
	if !shop.IsExists {
		return errors.New("not found")
	}

	//при оставлении отзыва можно получить ссылку на магазин
	btnReviewEl, _ := sess.FindElement(webdriver.CSS_Selector, `.review-add-button`)
	btnReviewEl.Click()
	linkToShopEl, err := sess.FindElement(webdriver.CSS_Selector, ".headline__header > .title.title_size_32")
	if err != nil {
		log.Print("can't get link to shop for vendorID=", vendorID)
	} else {
		linkToShop, _ := linkToShopEl.Text()
		shop.Address = strings.TrimPrefix(linkToShop, `Мой отзыв о магазине `)
	}

	if shop.IsActive {
		newUrl := getCatalogPageByVandorID(vendorID)
		err = sess.Url(newUrl)
		if err != nil {
			return err
		}

		catalogsJson, err := sess.ExecuteScript(CatalogScript, []interface{}{})
		if err != nil {
			return err
		}
		cap2 := &Capture{}
		err = json.Unmarshal(catalogsJson, cap2)
		if err == nil && len(cap2.Capture) > 0 {
			err = processingOfCaptcha(getVendorPage(vendorID), sess, cap2)
			if err != nil {
				panic(err)
			}
			sess.Url(getCatalogPageByVandorID(vendorID))
			catalogsJson, err = sess.ExecuteScript(CatalogScript, []interface{}{})
			if err != nil {
				return err
			}
		}

		catalogs := []*Catalog{}
		err = json.Unmarshal(catalogsJson, &catalogs)
		if err != nil {
			return err
		}

		shop.Catalogs = catalogs
	}
	shop.ID = vendorID
	_, err = db.DB("").C("shops").UpsertId(shop.ID, shop)
	if err != nil {
		return err
	}
	return nil
}