This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
anticaptcha.go
174 lines (153 loc) · 4.54 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
package anticaptcha
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"time"
)
var (
baseURL = &url.URL{Host: "api.anti-captcha.com", Scheme: "https", Path: "/"}
checkInterval = 2 * time.Second
)
type Client struct {
APIKey string
}
// 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)
// TODO treat api errors and handle them properly
if _, ok := responseBody["taskId"]; ok {
if taskId, ok := responseBody["taskId"].(float64); ok {
return taskId, nil
}
return 0, errors.New("task number of irregular format")
}
return 0, errors.New("task number not found in server response")
}
// 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, timeoutInterval time.Duration) (string, error) {
taskID, err := c.createTaskRecaptcha(websiteURL, recaptchaKey)
if err != nil {
return "", err
}
check := time.NewTicker(10 * time.Second)
timeout := time.NewTimer(timeoutInterval)
for {
select {
case <-check.C:
response, err := c.getTaskResult(taskID)
if err != nil {
return "", err
}
if response["status"] == "ready" {
return response["solution"].(map[string]interface{})["gRecaptchaResponse"].(string), nil
}
check = time.NewTicker(checkInterval)
case <-timeout.C:
return "", errors.New("antiCaptcha check result timeout")
}
}
}
// 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)
// TODO treat api errors and handle them properly
return responseBody["taskId"].(float64), nil
}
// 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" {
time.Sleep(checkInterval)
response, err = c.getTaskResult(taskID)
if err != nil {
return "", err
}
} else {
break
}
}
return response["solution"].(map[string]interface{})["text"].(string), nil
}