forked from fbnz156/anticaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanticaptcha.go
192 lines (171 loc) · 4.94 KB
/
anticaptcha.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package anticaptcha
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"time"
)
var (
baseURL = &url.URL{Host: "api.anti-captcha.com", Scheme: "https", Path: "/"}
)
type LogFunction func(params ...interface{})
type Client struct {
APIKey string
SendInterval time.Duration
LogFunction LogFunction
}
// Method to create the task to process the recaptcha, returns the task_id
func (c *Client) createTaskRecaptcha(websiteURL string, recaptchaKey string) (float64, error) {
// Mount the data to be sent
body := map[string]interface{}{
"clientKey": c.APIKey,
"task": map[string]interface{}{
"type": "NoCaptchaTaskProxyless",
"websiteURL": websiteURL,
"websiteKey": recaptchaKey,
},
}
b, err := json.Marshal(body)
if err != nil {
return 0, err
}
// Make the request
u := baseURL.ResolveReference(&url.URL{Path: "/createTask"})
resp, err := http.Post(u.String(), "application/json", bytes.NewBuffer(b))
if err != nil {
return 0, err
}
defer resp.Body.Close()
// Decode response
responseBody := make(map[string]interface{})
json.NewDecoder(resp.Body).Decode(&responseBody)
if errorCode, ok := responseBody["errorCode"].(string); ok {
return 0, errors.New(errorCode)
}
if taskId, ok := responseBody["taskId"].(float64); ok {
return taskId, nil
} else {
return 0, errors.New("task id is empty")
}
}
// Method to check the result of a given task, returns the json returned from the api
func (c *Client) getTaskResult(taskID float64) (map[string]interface{}, error) {
// Mount the data to be sent
body := map[string]interface{}{
"clientKey": c.APIKey,
"taskId": taskID,
}
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
// Make the request
u := baseURL.ResolveReference(&url.URL{Path: "/getTaskResult"})
resp, err := http.Post(u.String(), "application/json", bytes.NewBuffer(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode response
responseBody := make(map[string]interface{})
json.NewDecoder(resp.Body).Decode(&responseBody)
return responseBody, nil
}
// SendRecaptcha Method to encapsulate the processing of the recaptcha
// Given a url and a key, it sends to the api and waits until
// the processing is complete to return the evaluated key
func (c *Client) SendRecaptcha(websiteURL string, recaptchaKey string) (string, error) {
// Create the task on anti-captcha api and get the task_id
taskID, err := c.createTaskRecaptcha(websiteURL, recaptchaKey)
if err != nil {
return "", err
}
// Check if the result is ready, if not loop until it is
response, err := c.getTaskResult(taskID)
if err != nil {
return "", err
}
for {
if response["status"] == "processing" {
c.log("Result is not ready, waiting a few seconds to check again...")
time.Sleep(c.SendInterval)
response, err = c.getTaskResult(taskID)
if err != nil {
return "", err
}
} else {
c.log("Result is ready.")
break
}
}
return response["solution"].(map[string]interface{})["gRecaptchaResponse"].(string), nil
}
func (c Client) log(params ...interface{}) {
if c.LogFunction != nil {
c.LogFunction(params...)
}
}
// Method to create the task to process the image captcha, returns the task_id
func (c *Client) createTaskImage(imgString string) (float64, error) {
// Mount the data to be sent
body := map[string]interface{}{
"clientKey": c.APIKey,
"task": map[string]interface{}{
"type": "ImageToTextTask",
"body": imgString,
},
}
b, err := json.Marshal(body)
if err != nil {
return 0, err
}
// Make the request
u := baseURL.ResolveReference(&url.URL{Path: "/createTask"})
resp, err := http.Post(u.String(), "application/json", bytes.NewBuffer(b))
if err != nil {
return 0, err
}
defer resp.Body.Close()
// Decode response
responseBody := make(map[string]interface{})
json.NewDecoder(resp.Body).Decode(&responseBody)
if errorCode, ok := responseBody["errorCode"].(string); ok {
return 0, errors.New(errorCode)
}
if taskId, ok := responseBody["taskId"].(float64); ok {
return taskId, nil
} else {
return 0, errors.New("task id is empty")
}
}
// SendImage Method to encapsulate the processing of the image captcha
// Given a base64 string from the image, it sends to the api and waits until
// the processing is complete to return the evaluated key
func (c *Client) SendImage(imgString string) (string, error) {
// Create the task on anti-captcha api and get the task_id
taskID, err := c.createTaskImage(imgString)
if err != nil {
return "", err
}
// Check if the result is ready, if not loop until it is
response, err := c.getTaskResult(taskID)
if err != nil {
return "", err
}
for {
if response["status"] == "processing" {
c.log("Result is not ready, waiting a few seconds to check again...")
time.Sleep(c.SendInterval)
response, err = c.getTaskResult(taskID)
if err != nil {
return "", err
}
} else {
c.log("Result is ready.")
break
}
}
return response["solution"].(map[string]interface{})["text"].(string), nil
}