1
- package com .kinancity .core .captcha .captchaai ;
1
+ package com .kinancity .core .captcha .capsolver ;
2
2
3
3
import com .fasterxml .jackson .core .JsonProcessingException ;
4
4
import com .fasterxml .jackson .databind .DeserializationFeature ;
7
7
import com .kinancity .core .captcha .CaptchaException ;
8
8
import com .kinancity .core .captcha .CaptchaProvider ;
9
9
import com .kinancity .core .captcha .CaptchaQueue ;
10
- import com .kinancity .core .captcha .captchaai .dto .request .BalanceRequestDto ;
11
- import com .kinancity .core .captcha .captchaai .dto .request .CreateTaskRequestDto ;
12
- import com .kinancity .core .captcha .captchaai .dto .request .TaskResultRequestDto ;
13
- import com .kinancity .core .captcha .captchaai .dto .response .BalanceResponseDto ;
14
- import com .kinancity .core .captcha .captchaai .dto .response .BaseResponseDto ;
15
- import com .kinancity .core .captcha .captchaai .dto .response .CreateTaskResponseDto ;
16
- import com .kinancity .core .captcha .captchaai .dto .response .GetTaskResulResponseDto ;
10
+ import com .kinancity .core .captcha .capsolver .dto .request .BalanceRequestDto ;
11
+ import com .kinancity .core .captcha .capsolver .dto .request .CreateTaskRequestDto ;
12
+ import com .kinancity .core .captcha .capsolver .dto .request .TaskResultRequestDto ;
13
+ import com .kinancity .core .captcha .capsolver .dto .response .BalanceResponseDto ;
14
+ import com .kinancity .core .captcha .capsolver .dto .response .BaseResponseDto ;
15
+ import com .kinancity .core .captcha .capsolver .dto .response .CreateTaskResponseDto ;
16
+ import com .kinancity .core .captcha .capsolver .dto .response .GetTaskResulResponseDto ;
17
17
import lombok .Setter ;
18
18
import okhttp3 .*;
19
19
import okhttp3 .logging .HttpLoggingInterceptor ;
28
28
import java .util .Set ;
29
29
import java .util .stream .Collectors ;
30
30
31
- public class CaptchaaiCaptchaProvider extends CaptchaProvider {
31
+ public class CapsolverCaptchaProvider extends CaptchaProvider {
32
32
33
- private static final String BASE_URL = "https://api.captchaai.io " ;
33
+ private static final String BASE_URL = "https://api.capsolver.com " ;
34
34
private static final String BALANCE_URL = BASE_URL + "/getBalance" ;
35
35
private static final String NEW_TASK_URL = BASE_URL + "/createTask" ;
36
36
private static final String GET_TASK_URL = BASE_URL + "/getTaskResult" ;
37
37
38
38
39
- private static final String HTTP_ERROR_MSG = "Could not reach Captchaai servers" ;
39
+ private static final String HTTP_ERROR_MSG = "Could not reach Capsolver servers" ;
40
40
public static final String ERROR_ZERO_BALANCE = "ERROR_ZERO_BALANCE" ;
41
41
42
42
private final ObjectMapper objectMapper ;
@@ -49,7 +49,7 @@ public class CaptchaaiCaptchaProvider extends CaptchaProvider {
49
49
50
50
private int maxRequestPerTask = 120 ;
51
51
52
- private List <CaptchaiiTask > tasks ;
52
+ private List <CapsolverTask > tasks ;
53
53
54
54
private boolean hasBalanceLeft = true ;
55
55
@@ -65,15 +65,15 @@ public class CaptchaaiCaptchaProvider extends CaptchaProvider {
65
65
private int minTimeBeforeFirstResolve = 5 ;
66
66
67
67
/**
68
- * How often should we call Captchaai (default 5000 ms)
68
+ * How often should we call Capsolver (default 5000 ms)
69
69
*/
70
70
@ Setter
71
71
private int waitBeforeRetry = 5000 ;
72
72
73
73
@ Setter
74
74
private int waitForBalance = 5 *60 *1000 ;
75
75
76
- public CaptchaaiCaptchaProvider (CaptchaQueue queue , String apiKey ) throws CaptchaException {
76
+ public CapsolverCaptchaProvider (CaptchaQueue queue , String apiKey ) throws CaptchaException {
77
77
this .queue = queue ;
78
78
this .apiKey = apiKey ;
79
79
@@ -92,12 +92,12 @@ public CaptchaaiCaptchaProvider(CaptchaQueue queue, String apiKey) throws Captch
92
92
this .objectMapper = new ObjectMapper ().configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
93
93
94
94
if (this .apiKey == null || this .apiKey .isEmpty ()) {
95
- throw new CaptchaException ("Missing Captchaai API key" );
95
+ throw new CaptchaException ("Missing Capsolver API key" );
96
96
}
97
97
}
98
98
99
99
public static CaptchaProvider getInstance (CaptchaQueue queue , String apiKey ) throws CaptchaException {
100
- return new CaptchaaiCaptchaProvider (queue , apiKey );
100
+ return new CapsolverCaptchaProvider (queue , apiKey );
101
101
}
102
102
103
103
@ Override
@@ -109,15 +109,15 @@ public void run() {
109
109
if (this .getBalance () > 0 ) {
110
110
hasBalanceLeft = true ;
111
111
}
112
- } catch (TechnicalException | CaptchaaiConfigurationException e ) {
112
+ } catch (TechnicalException | CapsolverConfigurationException e ) {
113
113
logger .error ("Error getting balance" , e );
114
114
}
115
115
}
116
116
117
117
if (hasBalanceLeft ) {
118
118
119
119
LocalDateTime minDate = LocalDateTime .now ().minusSeconds (minTimeBeforeFirstResolve );
120
- Set <CaptchaiiTask > captchaiiTasksToResolve = tasks .stream ().filter (c -> c .getSentTime ().isBefore (minDate )).collect (Collectors .toSet ());
120
+ Set <CapsolverTask > captchaiiTasksToResolve = tasks .stream ().filter (c -> c .getSentTime ().isBefore (minDate )).collect (Collectors .toSet ());
121
121
122
122
123
123
if (captchaiiTasksToResolve .isEmpty ()) {
@@ -127,7 +127,7 @@ public void run() {
127
127
// Waiting for captchas
128
128
logger .debug ("Check status of {} captchas" , captchaiiTasksToResolve .size ());
129
129
130
- for (CaptchaiiTask task : captchaiiTasksToResolve ) {
130
+ for (CapsolverTask task : captchaiiTasksToResolve ) {
131
131
try {
132
132
Request taskResultRequest = buildGetTaskResultRequest (task .getTaskId ());
133
133
Response solveResponse = captchaClient .newCall (taskResultRequest ).execute ();
@@ -137,7 +137,7 @@ public void run() {
137
137
138
138
this .manageTaskResulResponse (task , taskResponse );
139
139
140
- } catch (CaptchaaiConfigurationException e ) {
140
+ } catch (CapsolverConfigurationException e ) {
141
141
logger .error ("Error while creating GetTaskResult request : {}" , e .getMessage ());
142
142
} catch (IOException e ) {
143
143
logger .error ("Error while calling captcha provider : {}" , e .getMessage ());
@@ -176,7 +176,7 @@ public void run() {
176
176
}
177
177
178
178
logger .info ("Requested new Captcha, taskid : {}" , taskResponse .getTaskId ());
179
- tasks .add (new CaptchaiiTask (taskResponse .getTaskId ()));
179
+ tasks .add (new CapsolverTask (taskResponse .getTaskId ()));
180
180
181
181
} catch (TechnicalException | IOException e ) {
182
182
logger .error ("Error while calling captcha provider : {}" , e .getMessage ());
@@ -186,7 +186,7 @@ public void run() {
186
186
break ;
187
187
}
188
188
}
189
- } catch (CaptchaaiConfigurationException e ) {
189
+ } catch (CapsolverConfigurationException e ) {
190
190
logger .error ("Error while creating new task request : {}" , e .getMessage ());
191
191
}
192
192
}
@@ -211,7 +211,7 @@ public void run() {
211
211
}
212
212
}
213
213
214
- private void manageTaskResulResponse (CaptchaiiTask task , GetTaskResulResponseDto taskResponse ) {
214
+ private void manageTaskResulResponse (CapsolverTask task , GetTaskResulResponseDto taskResponse ) {
215
215
if ("Idle" .equals (taskResponse .getStatus ())) {
216
216
logger .debug ("Task {} Idle, will retry later" , task .getTaskId ());
217
217
task .setNbTries (task .getNbTries () + 1 );
@@ -247,9 +247,9 @@ private void manageTaskResulResponse(CaptchaiiTask task, GetTaskResulResponseDto
247
247
*
248
248
* @return current balance in USD
249
249
* @throws TechnicalException
250
- * @throws CaptchaaiConfigurationException
250
+ * @throws CapsolverConfigurationException
251
251
*/
252
- public double getBalance () throws TechnicalException , CaptchaaiConfigurationException {
252
+ public double getBalance () throws TechnicalException , CapsolverConfigurationException {
253
253
Request sendRequest = buildBalanceCheckequest ();
254
254
try (Response sendResponse = captchaClient .newCall (sendRequest ).execute ()) {
255
255
String body = sendResponse .body ().string ();
@@ -268,15 +268,15 @@ public double getBalance() throws TechnicalException, CaptchaaiConfigurationExce
268
268
private TechnicalException generateException (BaseResponseDto responseDto ) {
269
269
return new TechnicalException (
270
270
(new StringBuilder ())
271
- .append ("Captchaai Error, " )
271
+ .append ("Capsolver Error, " )
272
272
.append (responseDto .getErrorCode ())
273
273
.append (" : " )
274
274
.append (responseDto .getErrorDescription ())
275
275
.toString ());
276
276
}
277
277
278
278
279
- private Request buildGetTaskResultRequest (String taskId ) throws CaptchaaiConfigurationException {
279
+ private Request buildGetTaskResultRequest (String taskId ) throws CapsolverConfigurationException {
280
280
try {
281
281
HttpUrl url = HttpUrl .parse (GET_TASK_URL ).newBuilder ()
282
282
.build ();
@@ -292,12 +292,12 @@ private Request buildGetTaskResultRequest(String taskId) throws CaptchaaiConfigu
292
292
.build ();
293
293
return request ;
294
294
} catch (JsonProcessingException e ) {
295
- throw new CaptchaaiConfigurationException ("Failed creating new taskResult request" , e );
295
+ throw new CapsolverConfigurationException ("Failed creating new taskResult request" , e );
296
296
}
297
297
}
298
298
299
299
300
- private Request buildSendCaptchaRequest () throws CaptchaaiConfigurationException {
300
+ private Request buildSendCaptchaRequest () throws CapsolverConfigurationException {
301
301
try {
302
302
HttpUrl url = HttpUrl .parse (NEW_TASK_URL ).newBuilder ()
303
303
.build ();
@@ -313,7 +313,7 @@ private Request buildSendCaptchaRequest() throws CaptchaaiConfigurationException
313
313
.build ();
314
314
return request ;
315
315
} catch (JsonProcessingException e ) {
316
- throw new CaptchaaiConfigurationException ("Failed creating new task request" , e );
316
+ throw new CapsolverConfigurationException ("Failed creating new task request" , e );
317
317
}
318
318
}
319
319
@@ -323,7 +323,7 @@ private Request buildSendCaptchaRequest() throws CaptchaaiConfigurationException
323
323
*
324
324
* @return Request to sent for balance check
325
325
*/
326
- private Request buildBalanceCheckequest () throws CaptchaaiConfigurationException {
326
+ private Request buildBalanceCheckequest () throws CapsolverConfigurationException {
327
327
try {
328
328
HttpUrl url = HttpUrl .parse (BALANCE_URL ).newBuilder ()
329
329
.build ();
@@ -339,7 +339,7 @@ private Request buildBalanceCheckequest() throws CaptchaaiConfigurationException
339
339
.build ();
340
340
return request ;
341
341
} catch (JsonProcessingException e ) {
342
- throw new CaptchaaiConfigurationException ("Failed creating balance request" , e );
342
+ throw new CapsolverConfigurationException ("Failed creating balance request" , e );
343
343
}
344
344
}
345
345
0 commit comments