Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "github.com/levigross/grequests"
- "strings"
- "errors"
- "time"
- )
- type TwoCaptchaAPI struct {
- APIKey string
- Location string
- }
- func NewTwoCaptcha(apikey string) TwoCaptchaAPI {
- return TwoCaptchaAPI{APIKey: apikey, Location:"http://2captcha.com/"}
- }
- func (tc *TwoCaptchaAPI) SolveCaptcha(sitekey string, page string, proxy string, proxytype string) (string, error) {
- resp, err := grequests.Post(fmt.Sprintf("%sin.php", tc.Location), &grequests.RequestOptions{
- Data: map[string]string {
- "key": tc.APIKey,
- "method": "userrecaptcha",
- "googlekey": sitekey,
- "pageurl": page,
- "invisible": "1",
- "proxy": proxy,
- "proxytype": proxytype,
- },
- Host: "2captcha.com",
- })
- if err != nil {
- return "", err
- }
- content := resp.String()
- resp.Close()
- if strings.Count(content, "|") != 1 {
- return "", errors.New(content)
- }
- captchaID := strings.Split(content, "|")[1]
- tries := 0
- for ;; {
- resp, err = grequests.Get(fmt.Sprintf("%sres.php", tc.Location), &grequests.RequestOptions{
- Params: map[string]string {
- "key": tc.APIKey,
- "action": "get",
- "id": captchaID,
- },
- Host: "2captcha.com",
- })
- if err != nil {
- tries += 1
- if tries > 5 {
- return "", errors.New("API is down")
- }
- }
- content = resp.String()
- resp.Close()
- if !strings.Contains(content, "CAPCHA_NOT_READY") {
- if !strings.Contains(content, "|") {
- return "", errors.New(content)
- }
- greq := strings.Split(content, "|")[1]
- return greq, nil
- }
- time.Sleep(time.Second * 1)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment