-
Notifications
You must be signed in to change notification settings - Fork 2
/
capMonster_tool.go
212 lines (197 loc) · 6.32 KB
/
capMonster_tool.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package capMonsterTool
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
//CapMonster API URL
const baseURL = "https://api.capmonster.cloud"
type GetBalanceResponse struct {
//Error identificator.
//0 - no error, errorCode property missing
//1 - error, information about it is in the errorCode property
ErrorId int
//The number of money available
Balance float64
//Error code.
ErrorCode string
//Error description.
ErrorDescription string
}
type CreateTaskResponse struct {
//Error identificator.
//0 - no error, errorCode property missing
//1 - error, information about it is in the errorCode property
ErrorId int
//Task ID for future use in getTask method.
TaskId int
//Error code.
ErrorCode string
//Error description.
ErrorDescription string
}
type Solution struct {
//gRecaptcha response
GRecaptchaResponse string
//processing - task is not ready yet
//ready - task complete, solution object can be found in solution property
Status string
}
type GetTaskResultResponse struct {
//Error identificator.
//0 - no error, errorCode property missing
//1 - error, information about it is in the errorCode property
ErrorId int
//processing - task is not ready yet
//ready - task complete, solution object can be found in solution property
Status string
//Captcha solution
Solution Solution
//Error code.
ErrorCode string
//Error description.
ErrorDescription string
}
type GetBalancePayload struct {
//Unique key of your account
ClientKey string `json:"clientKey"`
}
type GetTaskResultPayload struct {
//Unique key of your account
ClientKey string `json:"clientKey"`
//ID which was obtained in createTask method.
TaskId int `json:"taskId"`
}
type CreateTaskPayload struct {
//Unique key of your account
ClientKey string `json:"clientKey"`
//Task data
Task interface{} `json:"task"`
//not required
//Optional web address where we will send result of captcha task processing. Contents are sent by POST request and are same to the contents of getTaskResult method. The content of the response is not checked and you must accept the request in 2 seconds then the connection will be closed.
CallbackUrl string `json:"callbackUrl"`
}
// close properly the body
func bodyCloser(c io.Closer) {
if err := c.Close(); err != nil {
fmt.Errorf("error: %s", err)
}
}
//do a request with the specified method, url and payload
func doRequest(method string, url string, payload []byte) ([]byte, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer bodyCloser(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
//CreateTask creates a task for solving selected captcha type.
//In the parameters you need to pass the client authorization data and typed task data
//Method address: https://api.capmonster.cloud/createTask
//Request format: JSON POST
func CreateTask(clientKey string, task interface{}) (int, error) {
url := baseURL + "/createTask"
payload, err := json.Marshal(CreateTaskPayload{ClientKey: clientKey, Task: task})
if err != nil {
return 0, err
}
body, err := doRequest("POST", url, payload)
if err != nil {
return 0, err
}
var createTaskResponse CreateTaskResponse
if err = json.Unmarshal(body, &createTaskResponse); err != nil {
return 0, err
}
if createTaskResponse.ErrorId != 0 {
return 0, CapMonsterError{Err: createTaskResponse.ErrorCode, Desc: createTaskResponse.ErrorDescription}
}
return createTaskResponse.TaskId, nil
}
//CreateTaskWithCallbackUrl creates a task for solving selected captcha type.
//In the parameters you need to pass the client authorization data, typed task data and callback url
//Method address: https://api.capmonster.cloud/createTask
//Request format: JSON POST
func CreateTaskWithCallbackUrl(clientKey string, task interface{}, callbackUrl string) (int, error) {
url := baseURL + "/createTask"
payload, err := json.Marshal(CreateTaskPayload{ClientKey: clientKey, Task: task, CallbackUrl: callbackUrl})
if err != nil {
return 0, err
}
body, err := doRequest("POST", url, payload)
if err != nil {
return 0, err
}
var createTaskResponse CreateTaskResponse
if err = json.Unmarshal(body, &createTaskResponse); err != nil {
return 0, err
}
if createTaskResponse.ErrorId != 0 {
return 0, CapMonsterError{Err: createTaskResponse.ErrorCode, Desc: createTaskResponse.ErrorDescription}
}
return createTaskResponse.TaskId, nil
}
//GetTaskResult get the result of the specified task
//In the parameters you need to pass your client key and the ID of your task.
//Method address: https://api.capmonster.cloud/getTaskResult
//Request format: JSON POST
//Request limit: 120 requests per task.
func GetTaskResult(clientKey string, taskId int) (string, error) {
url := baseURL + "/getTaskResult"
payload, err := json.Marshal(GetTaskResultPayload{ClientKey: clientKey, TaskId: taskId})
if err != nil {
return "", err
}
body, err := doRequest("POST", url, payload)
if err != nil {
return "", err
}
var getTaskResultResponse GetTaskResultResponse
if err = json.Unmarshal(body, &getTaskResultResponse); err != nil {
return "", err
}
if getTaskResultResponse.ErrorId != 0 {
return "", CapMonsterError{Err: getTaskResultResponse.ErrorCode, Desc: getTaskResultResponse.ErrorDescription}
}
if getTaskResultResponse.Status == "processing" {
return "processing", CapMonsterProcessing{}
}
return getTaskResultResponse.Solution.GRecaptchaResponse, nil
}
//GetBalance retrieve your actual account balance
//In the parameters you need to pass your client key.
//Method address: https://api.capmonster.cloud/getBalance
//Request format: JSON POST
func GetBalance(clientKey string) (float64, error) {
url := baseURL + "/getBalance"
payload, err := json.Marshal(GetBalancePayload{ClientKey: clientKey})
if err != nil {
return 0., err
}
body, err := doRequest("POST", url, payload)
if err != nil {
return 0., err
}
var getBalanceResponse GetBalanceResponse
if err = json.Unmarshal(body, &getBalanceResponse); err != nil {
return 0., err
}
if getBalanceResponse.ErrorId != 0 {
return 0., CapMonsterError{Err: getBalanceResponse.ErrorCode, Desc: getBalanceResponse.ErrorDescription}
}
return getBalanceResponse.Balance, nil
}