/
captcha.go
146 lines (125 loc) · 3.28 KB
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package anticaptcha
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"strconv"
"strings"
"time"
)
//CaptchaService handles communication with the captcha actions of the
//anti-captcha API
type CaptchaService service
//UploadCaptchaFromURL represents upload captcha from url
func (s *CaptchaService) UploadCaptchaFromURL(url string) (int, error) {
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
return 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
strBase64 := base64.StdEncoding.EncodeToString(body)
captchaID, err := s.uploadCaptcha(strBase64)
if err != nil {
return 0, err
}
return captchaID, err
}
//UploadCaptchaFromFile represents updaload image(jpg, gif, png) to http://anti-captcha.com API
//and get capthca ID
func (s *CaptchaService) UploadCaptchaFromFile(path string) (int, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return 0, errors.New("File does not exist")
}
strBase64 := base64.StdEncoding.EncodeToString(content)
captchaID, err := s.uploadCaptcha(strBase64)
if err != nil {
return 0, err
}
return captchaID, err
}
//UploadCaptchaFromBase64 represents updaload base64 string to http://anti-captcha.com API
//and get capthca ID
func (s *CaptchaService) UploadCaptchaFromBase64(base64 string) (int, error) {
captchaID, err := s.uploadCaptcha(base64)
if err != nil {
return 0, err
}
return captchaID, err
}
//GetText represents return captcha text by ID
func (s *CaptchaService) GetText(id int) (string, error) {
reqURL := fmt.Sprintf("res.php?key=%s&action=get&id=%d", s.client.APIKey, id)
req, err := s.client.NewRequest("GET", reqURL, nil)
if err != nil {
return "", err
}
res, err := s.client.Do(req)
if err != nil {
return "", err
}
resOk, err := checkResponse(res)
if err != nil {
if fmt.Sprintf("%s", err) == "CAPCHA_NOT_READY" {
time.Sleep(5 * time.Second)
return s.GetText(id)
}
return "", err
}
return resOk, err
}
//checkResponse represents check response for have OK message
func checkResponse(body []byte) (string, error) {
response := string(body)
if strings.Contains(response, "OK") {
list := strings.Split(response, "|")
for i := range list {
list[i] = strings.TrimSpace(list[i])
}
return list[1], nil
}
return "", errors.New(response)
}
//uploadCaptcha represents updaload base64 string to http://anti-captcha.com API
//and get capthca ID
func (s CaptchaService) uploadCaptcha(base64 string) (int, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
params := s.client.CaptchaAdditionalParams.LoadParams()
for key, value := range params {
_ = writer.WriteField(key, value)
}
_ = writer.WriteField("body", base64)
_ = writer.WriteField("key", s.client.APIKey)
_ = writer.WriteField("method", "base64")
err := writer.Close()
if err != nil {
return 0, err
}
req, err := s.client.NewRequest("POST", "in.php", body)
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
data, err := s.client.Do(req)
if err != nil {
return 0, err
}
res, err := checkResponse(data)
if err != nil {
return 0, err
}
captchaID, err := strconv.Atoi(res)
if err != nil {
return 0, err
}
return captchaID, nil
}