forked from nuveo/anticaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanticaptcha.go
More file actions
170 lines (152 loc) · 4.53 KB
/
anticaptcha.go
File metadata and controls
170 lines (152 loc) · 4.53 KB
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
package anticaptcha
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/url"
"time"
)
var (
baseURL = &url.URL{Host: "api.anti-captcha.com", Scheme: "https", Path: "/"}
sendInterval = 10 * 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
return responseBody["taskId"].(float64), nil
}
// 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" {
log.Println("Result is not ready, waiting a few seconds to check again...")
time.Sleep(sendInterval)
response, err = c.getTaskResult(taskID)
if err != nil {
return "", err
}
} else {
log.Println("Result is ready.")
break
}
}
return response["solution"].(map[string]interface{})["gRecaptchaResponse"].(string), nil
}
// 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" {
log.Println("Result is not ready, waiting a few seconds to check again...")
time.Sleep(sendInterval)
response, err = c.getTaskResult(taskID)
if err != nil {
return "", err
}
} else {
log.Println("Result is ready.")
break
}
}
return response["solution"].(map[string]interface{})["text"].(string), nil
}