Guest User

twocaptcha go

a guest
Dec 3rd, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.59 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "github.com/levigross/grequests"
  6.     "strings"
  7.     "errors"
  8.     "time"
  9. )
  10.  
  11.  
  12. type TwoCaptchaAPI struct {
  13.     APIKey string
  14.     Location string
  15. }
  16.  
  17. func NewTwoCaptcha(apikey string) TwoCaptchaAPI {
  18.     return TwoCaptchaAPI{APIKey: apikey, Location:"http://2captcha.com/"}
  19. }
  20.  
  21. func (tc *TwoCaptchaAPI) SolveCaptcha(sitekey string, page string, proxy string, proxytype string) (string, error) {
  22.     resp, err := grequests.Post(fmt.Sprintf("%sin.php", tc.Location), &grequests.RequestOptions{
  23.         Data: map[string]string {
  24.             "key": tc.APIKey,
  25.             "method": "userrecaptcha",
  26.             "googlekey": sitekey,
  27.             "pageurl": page,
  28.             "invisible": "1",
  29.             "proxy": proxy,
  30.             "proxytype": proxytype,
  31.         },
  32.         Host: "2captcha.com",
  33.     })
  34.  
  35.     if err != nil {
  36.         return "", err
  37.     }
  38.  
  39.     content := resp.String()
  40.  
  41.     resp.Close()
  42.  
  43.     if strings.Count(content, "|") != 1 {
  44.         return "", errors.New(content)
  45.     }
  46.  
  47.     captchaID := strings.Split(content, "|")[1]
  48.  
  49.     tries := 0
  50.  
  51.     for ;; {
  52.         resp, err = grequests.Get(fmt.Sprintf("%sres.php", tc.Location), &grequests.RequestOptions{
  53.             Params: map[string]string {
  54.                 "key": tc.APIKey,
  55.                 "action": "get",
  56.                 "id": captchaID,
  57.             },
  58.             Host: "2captcha.com",
  59.         })
  60.  
  61.         if err != nil {
  62.             tries += 1
  63.             if tries > 5 {
  64.                 return "", errors.New("API is down")
  65.             }
  66.         }
  67.  
  68.         content = resp.String()
  69.         resp.Close()
  70.  
  71.         if !strings.Contains(content, "CAPCHA_NOT_READY") {
  72.             if !strings.Contains(content, "|") {
  73.                 return "", errors.New(content)
  74.             }
  75.             greq := strings.Split(content, "|")[1]
  76.             return greq, nil
  77.         }
  78.  
  79.         time.Sleep(time.Second * 1)
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment