-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
288 lines (255 loc) · 7.38 KB
/
client.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package anticaptcha
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type Client struct {
ClientKey string
URL string
Debug bool
}
type Reqest struct {
ClientKey string `json:"clientKey"`
// tasks
Task interface{} `json:"task,omitempty"`
SoftId int `json:"softId,omitempty"`
LanguagePool string `json:"languagePool,omitempty"`
CallbackUrl string `json:"callbackUrl,omitempty"`
TaskID int `json:"taskId,omitempty"`
IsExtended bool `json:"isExtended,omitempty"`
// queue stats
QueueId int `json:"queueId,omitempty"`
// spending stats
Queue string `json:"queue,omitempty"`
Date int `json:"date,omitempty"`
IP string `json:"ip,omitempty"`
// app stat
Mode string `json:"mode,omitempty"`
// reseller
Count int `json:"count,omitempty"`
Amount float32 `json:"amount,omitempty"`
PurchaseLink string `json:"purchaseLink,omitempty"`
MinCreateDate int `json:"minCreateDate,omitempty"`
}
type Response_CreateTask struct {
ErrorID int `json:"errorId"`
TaskID int `json:"taskId,omitempty"`
}
type Response_GetTaskResult struct {
ErrorID int `json:"errorId"`
Status string `json:"status,omitempty"`
Solution struct {
// ImageToText
Text string `json:"text,omitempty"`
URL string `json:"url,omitempty"`
// NoCaptcha
GRecaptchaResponse string `json:"gRecaptchaResponse,omitempty"`
GRecaptchaResponseMD5 string `json:"gRecaptchaResponseMD5,omitempty"`
// FunCaptcha
Token string `json:"token,omitempty"`
// SquareNetText
CellNumbers []int `json:"cellNumbers"`
// CustomCaptcha
TaskID int `json:"taskId,omitempty"`
Status string `json:"status,omitempty"`
Answers map[string]string `json:"answers,omitempty"`
} `json:"solution,omitempty"`
Cost string `json:"cost,omitempty"`
IP string `json:"ip,omitempty"`
CreateTime int `json:"createTime,omitempty"`
EndTime int `json:"endTime,omitempty"`
SolveCount int `json:"solveCount,omitempty"`
}
func (r Response_GetTaskResult) ResultImage() (text, url string) {
return r.Solution.Text, r.Solution.URL
}
func (r Response_GetTaskResult) ResultRecaptcha() string {
return r.Solution.GRecaptchaResponse
}
func (r Response_GetTaskResult) ResultFunCaptcha() string {
return r.Solution.Token
}
func (r Response_GetTaskResult) ResultSquareNetText() []int {
return r.Solution.CellNumbers
}
type ResponseReportIncorrectImageCaptcha struct {
ErrorID int `json:"errorId"`
Status string `json:"status,omitempty"`
}
type Response_GetBalance struct {
ErrorID int `json:"errorId"`
Balance float32 `json:"balance,omitempty"`
}
type Response_GetQueueStats struct {
ErrorID int `json:"errorId"`
Waiting int `json:"waiting,omitempty"`
Load float64 `json:"load,omitempty"`
Bid float64 `json:"bid,omitempty"`
Speed float64 `json:"speed,omitempty"`
Total int `json:"total,omitempty"`
}
type Response_GetSpendingStats struct {
ErrorID int `json:"errorId"`
Data []struct {
DateFrom int `json:"dateFrom"`
DateTill int `json:"dateTill"`
Volume int `json:"volume"`
Money float64 `json:"money"`
} `json:"data"`
}
type Response_GetAppStats struct {
ErrorID int `json:"errorId"`
ChartData []struct {
Name string `json:"name"`
Data []struct {
Date string `json:"date,omitempty"`
Shortdate string `json:"shortdate,omitempty"`
Y int `json:"y,omitempty"`
Beginstamp int `json:"beginstamp,omitempty"`
Endstamp int `json:"endstamp,omitempty"`
Stamp int `json:"stamp,omitempty"`
} `json:"data"`
Itemname string `json:"itemname"`
ErrorID int `json:"errorId"`
Code string `json:"code"`
Description string `json:"description"`
Count int `json:"count,omitempty"`
} `json:"chartData"`
FromDate string `json:"fromDate"`
ToDate string `json:"toDate"`
}
type Response_GenerateCoupons struct {
ErrorID int `json:"errorId"`
Coupons []string `json:"coupons"`
}
type Response_GetResellerData struct {
ErrorID int `json:"errorId"`
EligibleBalance float64 `json:"eligibleBalance"`
Coupons []struct {
ID int `json:"id"`
Amount int `json:"amount"`
Code string `json:"code"`
Link string `json:"link"`
Status string `json:"status"`
} `json:"coupons"`
}
type Response struct {
ErrorID int `json:"errorId"`
}
// NewClient
// Create new anti-captcha client
func NewClient(ClientKey string) *Client {
return &Client{ClientKey: ClientKey, URL: URL}
}
func (c *Client) ChengeURL(url string) {
s := []rune(url)
if string(s[len(s)-1]) == "/" {
url = string(s[0 : len(s)-1])
}
c.URL = url
}
func (c *Client) SetDebug(v bool) {
c.Debug = v
}
func (c *Client) request(url string, req_data *Reqest, resp_data interface{}) (error *Error) {
error = &Error{}
req_data.ClientKey = c.ClientKey
payloadBytes, err := json.Marshal(req_data)
if err != nil {
error.set(err)
return error
}
if c.Debug {
fmt.Println("Request URL:", url)
fmt.Println("Request JSON:", string(payloadBytes))
}
body := bytes.NewReader(payloadBytes)
req, err := http.NewRequest("POST", url, body)
if err != nil {
error.set(err)
return error
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
//resp, err := http.DefaultClient.Do(req)
if err != nil {
error.set(err)
return error
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
error.set(err)
return error
}
if c.Debug {
fmt.Println("Response JSON:", string(b))
}
err = json.Unmarshal(b, resp_data)
if err != nil {
error.set(err)
return error
}
switch resp_data.(type) {
case *Response_CreateTask:
error.ErrorID = resp_data.(*Response_CreateTask).ErrorID
case *Response_GetTaskResult:
error.ErrorID = resp_data.(*Response_GetTaskResult).ErrorID
case *ResponseReportIncorrectImageCaptcha:
error.ErrorID = resp_data.(*ResponseReportIncorrectImageCaptcha).ErrorID
case *Response_GetBalance:
error.ErrorID = resp_data.(*Response_GetBalance).ErrorID
case *Response_GetQueueStats:
error.ErrorID = resp_data.(*Response_GetQueueStats).ErrorID
case *Response_GetSpendingStats:
error.ErrorID = resp_data.(*Response_GetSpendingStats).ErrorID
case *Response_GetAppStats:
error.ErrorID = resp_data.(*Response_GetAppStats).ErrorID
case *Response_GenerateCoupons:
error.ErrorID = resp_data.(*Response_GenerateCoupons).ErrorID
case *Response_GetResellerData:
error.ErrorID = resp_data.(*Response_GetResellerData).ErrorID
default:
error.setString("Response structure not found")
}
if error.ErrorID != 0 {
error.Error = error.ErrMsg()
}
return error
}
// WaitResult
func (c *Client) WaitResult(taskID int, timeout int64) (result *Response_GetTaskResult, error *Error) {
error = &Error{}
if timeout == 0 {
timeout = timeout + 120
}
endTime := time.Now().Unix() + timeout
for {
if time.Now().Unix() >= endTime {
error.setString("Timeout")
return result, error
}
time.Sleep(time.Second * 10)
result, error = c.GetTaskResult(taskID)
if error.Error != nil {
return result, error
}
if result.Status == TaskStatus_Ready {
return result, error
} else {
if c.Debug {
fmt.Println("Not ready. Wait 10s")
}
}
}
}