1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-06 07:32:50 +00:00

Implements CaptchaTrader service

This commit is contained in:
2012-01-17 12:28:22 -02:00
parent e943b08ede
commit 08d22a12fe
81 changed files with 2750 additions and 543 deletions

View File

@@ -0,0 +1,207 @@
/**
*
*/
package com.captchatrader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.omg.CORBA.Any;
import com.captchatrader.exception.ApplicationKeyDisabledException;
import com.captchatrader.exception.CaptchaTraderException;
import com.captchatrader.exception.ConnectionLimitException;
import com.captchatrader.exception.DailyLimitException;
import com.captchatrader.exception.ImageTooLargeException;
import com.captchatrader.exception.IncorrectRespondsException;
import com.captchatrader.exception.InsuficientCreditsException;
import com.captchatrader.exception.InternalErrorException;
import com.captchatrader.exception.InvalidApplicationKeyException;
import com.captchatrader.exception.InvalidParametersException;
import com.captchatrader.exception.InvalidTicketException;
import com.captchatrader.exception.InvalidURLException;
import com.captchatrader.exception.InvalidUserException;
import com.captchatrader.exception.NotAnImageException;
import com.captchatrader.exception.SubmissionErrorException;
import com.captchatrader.exception.UserNotValidatedException;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class CaptchaTrader {
/**
* The {@link HttpClient} instance
*/
private final HttpClient client = new DefaultHttpClient();
private final JSONParser json = new JSONParser();
private final URI apiURL;
private final String applicationKey;
private final String username;
private final String password;
/**
* Creates a new API instance with the given application key
*
* @param applicationKey
* the key
*/
public CaptchaTrader(URI apiURL, String applicationKey, String username,
String password) {
this.apiURL = apiURL;
this.applicationKey = applicationKey;
this.username = username;
this.password = password;
}
/**
* Creates a new API instance with the given application key
*
* @param applicationKey
* the key
* @throws MalformedURLException
*/
public CaptchaTrader(String applicationKey, String username, String password) {
this(URI.create("http://api.captchatrader.com/"), applicationKey,
username, password);
}
/**
* Submit a CAPTCHA already hosted on an existing website.
*
* @param url
* The URL of the CAPTCHA image.
* @return The decoded CAPTCHA.
* @throws Any
* exceptions sent by the server.
*/
public ResolvedCaptcha submit(URL url) throws CaptchaTraderException,
IOException {
final URI requestUri = apiURL.resolve("submit");
final HttpPost request = new HttpPost(requestUri);
final MultipartEntity entity = new MultipartEntity();
entity.addPart("api_key", new StringBody(applicationKey));
entity.addPart("username", new StringBody(username));
entity.addPart("password", new StringBody(password));
entity.addPart("value", new StringBody(url.toString()));
request.setEntity(entity);
final List<Object> response = validate(execute(request));
return new ResolvedCaptcha(this, ((Long) response.get(0)).intValue(),
(String) response.get(1));
}
/**
* Responds if an CAPTCHA wes correctly answered or not
*
* @param captcha
* the CAPTCHA object
* @param state
* <code>true</code> if the CAPTCHA was correctly resolved
* @throws CaptchaTraderException
* any of the possible errors
*/
public void response(ResolvedCaptcha captcha, boolean state)
throws CaptchaTraderException, IOException {
final URI requestUri = apiURL.resolve("respond");
final HttpPost request = new HttpPost(requestUri);
final MultipartEntity entity = new MultipartEntity();
entity.addPart("is_correct", new StringBody(state ? "1" : "0"));
entity.addPart("username", new StringBody(username));
entity.addPart("password", new StringBody(password));
entity.addPart("ticket",
new StringBody(Integer.toString(captcha.getID())));
request.setEntity(entity);
validate(execute(request));
}
/**
* Checks the amount of resolutions available on a users account
*
* @return The decoded CAPTCHA.
* @throws Any
* exceptions sent by the server.
*/
public int getCredits() throws CaptchaTraderException, IOException {
return ((Number) validate(
execute(new HttpGet(apiURL.resolve("get_credits/username:"
+ username + "/password:" + password + "/")))).get(1))
.intValue();
}
private List<Object> validate(List<Object> response)
throws CaptchaTraderException {
if ((long) response.get(0) == -1) {
throw translateException((String) response.get(1));
}
return response;
}
@SuppressWarnings("unchecked")
private List<Object> execute(HttpUriRequest request) throws IOException {
final HttpResponse response = client.execute(request);
try {
return (List<Object>) json.parse(new InputStreamReader(response
.getEntity().getContent()));
} catch (IllegalStateException | ParseException e) {
throw new IOException(e);
}
}
private CaptchaTraderException translateException(String error) {
switch (error) {
case "API KEY DISABLED":
return new ApplicationKeyDisabledException();
case " CONNECTION LIMIT":
return new ConnectionLimitException();
case "DAILY LIMIT":
return new DailyLimitException();
case "IMAGE TOO LARGE":
return new ImageTooLargeException();
case "INSUFFICIENT CREDITS":
return new InsuficientCreditsException();
case "INTERNAL ERROR":
return new InternalErrorException();
case "INVALID API KEY":
return new InvalidApplicationKeyException();
case "INVALID PARAMETERS":
return new InvalidParametersException();
case "INVALID URL":
return new InvalidURLException();
case "INVALID USER":
return new InvalidUserException();
case "USER NOT VALIDATED":
return new UserNotValidatedException();
case "NOT AN IMAGE":
return new NotAnImageException();
case "SUBMISSION ERROR":
return new SubmissionErrorException();
case "INCORRECT REPORTS":
return new IncorrectRespondsException();
case "INVALID TICKET":
return new InvalidTicketException();
default:
return new CaptchaTraderException(error);
}
}
}

View File

@@ -0,0 +1,40 @@
/**
*
*/
package com.captchatrader;
import java.io.IOException;
import com.captchatrader.exception.CaptchaTraderException;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ResolvedCaptcha {
private final CaptchaTrader api;
private final int id;
private final String answer;
public ResolvedCaptcha(CaptchaTrader api, int id, String answer) {
this.api = api;
this.id = id;
this.answer = answer;
}
public int getID() {
return id;
}
public String getAnswer() {
return answer;
}
public void valid() throws CaptchaTraderException, IOException {
api.response(this, true);
}
public void invalid() throws CaptchaTraderException, IOException {
api.response(this, true);
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ApplicationKeyDisabledException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public ApplicationKeyDisabledException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public ApplicationKeyDisabledException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public ApplicationKeyDisabledException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public ApplicationKeyDisabledException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public ApplicationKeyDisabledException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class CaptchaTraderException extends Exception {
private static final long serialVersionUID = 1L;
/**
*
*/
public CaptchaTraderException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public CaptchaTraderException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public CaptchaTraderException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public CaptchaTraderException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public CaptchaTraderException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ConnectionLimitException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public ConnectionLimitException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public ConnectionLimitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public ConnectionLimitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public ConnectionLimitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public ConnectionLimitException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class DailyLimitException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public DailyLimitException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public DailyLimitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public DailyLimitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public DailyLimitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public DailyLimitException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ImageTooLargeException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public ImageTooLargeException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public ImageTooLargeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public ImageTooLargeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public ImageTooLargeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public ImageTooLargeException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class IncorrectRespondsException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public IncorrectRespondsException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public IncorrectRespondsException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public IncorrectRespondsException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public IncorrectRespondsException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public IncorrectRespondsException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InsuficientCreditsException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InsuficientCreditsException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InsuficientCreditsException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InsuficientCreditsException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InsuficientCreditsException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InsuficientCreditsException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InternalErrorException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InternalErrorException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InternalErrorException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InternalErrorException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InternalErrorException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InternalErrorException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidApplicationKeyException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidApplicationKeyException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidApplicationKeyException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidApplicationKeyException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidApplicationKeyException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidApplicationKeyException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidParametersException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidParametersException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidParametersException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidParametersException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidParametersException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidParametersException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidTicketException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidTicketException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidTicketException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidTicketException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidTicketException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidTicketException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidURLException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidURLException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidURLException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidURLException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidURLException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidURLException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidUserException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidUserException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidUserException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidUserException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidUserException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidUserException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class NotAnImageException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public NotAnImageException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public NotAnImageException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public NotAnImageException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public NotAnImageException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public NotAnImageException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class SubmissionErrorException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public SubmissionErrorException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public SubmissionErrorException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public SubmissionErrorException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public SubmissionErrorException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public SubmissionErrorException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class UserNotValidatedException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public UserNotValidatedException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public UserNotValidatedException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public UserNotValidatedException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public UserNotValidatedException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public UserNotValidatedException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,117 @@
/**
*
*/
package com.rogiel.httpchannel.captcha.impl;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.captchatrader.CaptchaTrader;
import com.captchatrader.ResolvedCaptcha;
import com.captchatrader.exception.CaptchaTraderException;
import com.rogiel.httpchannel.captcha.ImageCaptcha;
import com.rogiel.httpchannel.captcha.ImageCaptchaService;
import com.rogiel.httpchannel.captcha.exception.AuthenticationCaptchaServiceException;
import com.rogiel.httpchannel.captcha.exception.InvalidCaptchaServiceException;
import com.rogiel.httpchannel.captcha.exception.UnsolvableCaptchaServiceException;
/**
* Implements CAPTCHA solving using CaptchaTrader.com service. If
* username-password authentication is not desired, send the API key as username
* and <code>null</code> password.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class CaptchaTraderService implements ImageCaptchaService {
private static final Logger logger = LoggerFactory
.getLogger(CaptchaTraderService.class);
private static final String APP_KEY = "2acc44805ec208cc4d6b00c75a414996";
/**
* The current application key to be used
*/
private String currentApplicationKey = APP_KEY;
/**
* The CaptchaTrader.com API object
*/
private CaptchaTrader api;
@Override
public int authenticate(String username, String password)
throws AuthenticationCaptchaServiceException {
api = new CaptchaTrader(currentApplicationKey, username, password);
logger.debug("Authenticating into CaptchaTrader");
try {
// this will validate the account
return (int) Math.ceil(((double) api.getCredits()) / 10);
} catch (IOException | CaptchaTraderException e) {
throw new AuthenticationCaptchaServiceException(e);
}
}
@Override
public void solve(ImageCaptcha captcha)
throws UnsolvableCaptchaServiceException {
try {
logger.debug("Resolving CAPTCHA {}", captcha.getImageURL());
final ResolvedCaptcha resolved = api.submit(captcha.getImageURL());
captcha.setAnswer(resolved.getAnswer());
captcha.setAttachment(resolved);
logger.debug("CAPTCHA solved, answer is \"{}\"",
resolved.getAnswer());
} catch (IOException | CaptchaTraderException e) {
throw new UnsolvableCaptchaServiceException(e);
}
}
@Override
public void valid(ImageCaptcha captcha)
throws InvalidCaptchaServiceException {
final Object attachment = captcha.getAttachment();
if (attachment instanceof ResolvedCaptcha) {
try {
logger.debug("Notifying server that CAPTCHA {} is valid",
captcha);
((ResolvedCaptcha) attachment).valid();
} catch (CaptchaTraderException | IOException e) {
throw new InvalidCaptchaServiceException(e);
}
} else {
throw new InvalidCaptchaServiceException();
}
}
@Override
public void invalid(ImageCaptcha captcha)
throws InvalidCaptchaServiceException {
final Object attachment = captcha.getAttachment();
if (attachment instanceof ResolvedCaptcha) {
try {
logger.debug("Notifying server that CAPTCHA {} is invalid",
captcha);
((ResolvedCaptcha) attachment).invalid();
} catch (CaptchaTraderException | IOException e) {
throw new InvalidCaptchaServiceException(e);
}
} else {
throw new InvalidCaptchaServiceException();
}
}
/**
* Sets the CaptchaTrader API key. Each application should provide a key,
* otherwise the default "HttpChannel" key will be used.
*
* @param key
* the application key
*/
public void setApplicationKey(String key) {
if (key == null)
key = APP_KEY;
logger.debug("Setting new application key as {}", key);
currentApplicationKey = key;
}
}