Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.vk.api.sdk.captcha;

public abstract class CaptchaHandlerAbstract {
private int maxCaptchaProcessRetries = 2;
private int maxTechnicalResolveRetries = 2;

public int getMaxCaptchaProcessRetries() {
return maxCaptchaProcessRetries;
}

public int getMaxTechnicalResolveRetries() {
return maxTechnicalResolveRetries;
}

public CaptchaHandlerAbstract(int maxCaptchaProcessRetries, int maxTechnicalResolveRetries) {
this.maxCaptchaProcessRetries = maxCaptchaProcessRetries;
this.maxTechnicalResolveRetries = maxTechnicalResolveRetries;
}

public CaptchaHandlerAbstract() {
}

abstract public String getCaptchaCode(String sid, String img) throws CaptchaResolveException;

public boolean checkCode(String key) {
if(key == null || !key.matches("^[0-9a-zA-Z]*?$")) return false;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.vk.api.sdk.captcha;


import com.vk.api.sdk.captcha.anticaptcha.GetCaptchaSimple;

public class CaptchaHandlerAnticaptcha extends CaptchaHandlerAbstract {
private final String apiKey;
private final Integer sleepSecondBetweenTaskCheck;
private final Integer maxRetries;

public CaptchaHandlerAnticaptcha(String apiKey, Integer sleepSecondBetweenTaskCheck, Integer maxRetries) {
this.apiKey = apiKey;
this.sleepSecondBetweenTaskCheck = sleepSecondBetweenTaskCheck;
this.maxRetries = maxRetries;
}

@Override
public String getCaptchaCode(String sid, String img) throws CaptchaResolveException {
System.out.println("Resolving captcha via anti-captcha.com...");
return GetCaptchaSimple.getCaptchaCode(img, apiKey, sleepSecondBetweenTaskCheck, maxRetries);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.vk.api.sdk.captcha;


import java.util.Objects;
import java.util.Scanner;

public class CaptchaHandlerConsole extends CaptchaHandlerAbstract {

@Override
public String getCaptchaCode(String sid, String img) throws CaptchaResolveException {
System.out.println("----------------------------------------------");
System.out.println("NEED TO ENTER CAPTCHA");
System.out.println("SID: "+sid);
System.out.println("Link: "+img);
System.out.println("Enter 'q' or 'quit' to decline! ");
System.out.println("---");
Scanner consoleInputScanner = new Scanner(System.in);
String userEnteredCaptcha;
while(true) {
System.out.print("PLEASE ENTER CAPTCHA: ");
userEnteredCaptcha = consoleInputScanner.next();
if (Objects.equals(userEnteredCaptcha, "q") || Objects.equals(userEnteredCaptcha, "quit"))
throw new CaptchaResolveException("User declined to enter the captcha");
if(checkCode(userEnteredCaptcha)) break;
else {
System.out.println("Wrong captcha format, please re-enter captcha.");
continue;
}
}
System.out.println("Captcha entered: "+userEnteredCaptcha);
System.out.println("----------------------------------------------");
return userEnteredCaptcha;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.vk.api.sdk.captcha;


import java.util.Objects;
import java.util.Scanner;

public class CaptchaHandlerFactory {
public static CaptchaHandlerConsole getConsoleHandler() {
return new CaptchaHandlerConsole();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vk.api.sdk.captcha;

import com.vk.api.sdk.exceptions.ApiException;

public class CaptchaResolveException extends ApiException {
public CaptchaResolveException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.vk.api.sdk.captcha.anticaptcha;


import com.vk.api.sdk.captcha.CaptchaResolveException;
import com.vk.api.sdk.captcha.anticaptcha.entities.ImageToTextTaskRequestObject;
import com.vk.api.sdk.captcha.anticaptcha.entities.requests.CreateTaskRequest;
import com.vk.api.sdk.captcha.anticaptcha.entities.requests.CreateTaskResponse;
import com.vk.api.sdk.captcha.anticaptcha.entities.requests.GetCaptchaResultRequest;
import com.vk.api.sdk.captcha.anticaptcha.entities.requests.GetCaptchaResultResponse;
import com.vk.api.sdk.captcha.anticaptcha.simpleRequest.simpleRequest;
import org.apache.commons.io.IOUtils;
import org.apache.http.message.BasicHeader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Objects;

public class GetCaptchaSimple {

public static byte[] readFromPathOrUrl(String urlOrPath) throws IOException {
if(urlOrPath.contains("http")) {
URL url = new URL (urlOrPath);
InputStream is = null;
try {
is = url.openStream();
return IOUtils.toByteArray(is);
}
catch (IOException e) {
System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
e.printStackTrace ();
throw e;
}
finally {
if (is != null) { is.close(); }
}
}
else return Files.readAllBytes(Path.of(urlOrPath));
}


public static String getCaptchaCode(String captchaUrlOrPath, String antiCaptchaApiKey, Integer sleepSecondBetweenTaskCheck, Integer maxRetries) throws CaptchaResolveException {
if(sleepSecondBetweenTaskCheck==null) sleepSecondBetweenTaskCheck=5;
if(maxRetries==null) maxRetries=2;

//Base64 of image
String encodedImage = null;
try {
encodedImage = Base64.getEncoder().encodeToString(readFromPathOrUrl(captchaUrlOrPath));
} catch (IOException e) {
e.printStackTrace();
}
if (encodedImage == null) throw new CaptchaResolveException("Cant resolve captcha, cant get base64 of img!");

//Set resolve task
Integer taskId = null;
try {
CreateTaskResponse createTaskResponse = new simpleRequest()
.setUrl("http://api.anti-captcha.com/createTask")
.addHeader(new BasicHeader("Accept", "application/json"))
.addHeader(new BasicHeader("Content-Type", "application/json"))
.doRequest(new CreateTaskRequest().setClientKey(antiCaptchaApiKey).setTask(new ImageToTextTaskRequestObject().setBody(encodedImage)), CreateTaskResponse.class);
taskId = createTaskResponse.getTaskId();
} catch (IOException e) {
e.printStackTrace();
throw new CaptchaResolveException("IO exception during create task!");
}
if (taskId == null) throw new CaptchaResolveException("Cant resolve captcha, anticaptcha /createTask not returned taskId!");

//Get captcha resolve
for (int i = 0; i < maxRetries; i++) {
try {
Thread.sleep(sleepSecondBetweenTaskCheck*1000);
} catch (InterruptedException e) {
e.printStackTrace();
throw new CaptchaResolveException("Cant resolve captcha, wait interrupted!");
}

//Get captcha code
String captchaText = null;
try {
GetCaptchaResultResponse response = new simpleRequest()
.setUrl("http://api.anti-captcha.com/getTaskResult")
.addHeader(new BasicHeader("Accept", "application/json"))
.addHeader(new BasicHeader("Content-Type", "application/json"))
.doRequest(new GetCaptchaResultRequest().setClientKey(antiCaptchaApiKey).setTaskId(taskId), GetCaptchaResultResponse.class);
if (Objects.equals(response.getStatus(), "processing")) continue;
if (!Objects.equals(response.getStatus(), "ready"))
throw new CaptchaResolveException("Cant resolve captcha, anticaptcha result is not in correct status, error: "
+ response.getErrorId()
+ " " + response.getErrorCode()
+ " " + response.getErrorDescription()
);
captchaText = response.getSolution().getText();
if (captchaText == null)
throw new CaptchaResolveException("Cant resolve captcha, anticaptcha result text is null!");
return captchaText;

} catch (IOException e) {
e.printStackTrace();
throw new CaptchaResolveException("IO exception during getTaskResult");
}
}
throw new CaptchaResolveException("Cant be here!");
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.vk.api.sdk.captcha.anticaptcha.entities;

import com.google.gson.annotations.SerializedName;

public class ImageToTextTaskRequestObject {
private String type = "ImageToTextTask";
private String body;
private Boolean phrase;
@SerializedName("case")
private Boolean tCase;
private Integer numeric;
private Boolean math;
private Integer minLength;
private Integer maxLength;
private String commend;
private String websiteURL;

public ImageToTextTaskRequestObject setType(String type) {
this.type = type;
return this;
}

public ImageToTextTaskRequestObject setBody(String body) {
this.body = body;
return this;
}

public ImageToTextTaskRequestObject setPhrase(Boolean phrase) {
this.phrase = phrase;
return this;
}

public ImageToTextTaskRequestObject setCase(Boolean tCase) {
this.tCase = tCase;
return this;
}

public ImageToTextTaskRequestObject setNumeric(Integer numeric) {
this.numeric = numeric;
return this;
}

public ImageToTextTaskRequestObject setMath(Boolean math) {
this.math = math;
return this;
}

public ImageToTextTaskRequestObject setMinLength(Integer minLength) {
this.minLength = minLength;
return this;
}

public ImageToTextTaskRequestObject setMaxLength(Integer maxLength) {
this.maxLength = maxLength;
return this;
}

public ImageToTextTaskRequestObject setCommend(String commend) {
this.commend = commend;
return this;
}

public ImageToTextTaskRequestObject setWebsiteURL(String websiteURL) {
this.websiteURL = websiteURL;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vk.api.sdk.captcha.anticaptcha.entities;

public class ImageToTextTaskResultObject {
private String text;
private String url;

public String getText() {
return text;
}

public String getUrl() {
return url;
}
}
Loading