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

Adds -SNAPSHOT to Maven version parameter and few modifications to API

This commit is contained in:
2012-01-17 17:07:48 -02:00
parent 08d22a12fe
commit 673bfc6639
56 changed files with 536 additions and 399 deletions

View File

@@ -4,7 +4,7 @@
<parent>
<artifactId>httpchannel-captcha</artifactId>
<groupId>com.rogiel.httpchannel</groupId>
<version>1.0.0</version>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<groupId>com.rogiel.httpchannel.captcha</groupId>

View File

@@ -5,9 +5,7 @@ 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;
@@ -50,7 +48,7 @@ public class CaptchaTrader {
private final HttpClient client = new DefaultHttpClient();
private final JSONParser json = new JSONParser();
private final URI apiURL;
private final URI apiURI;
private final String applicationKey;
private final String username;
private final String password;
@@ -61,9 +59,9 @@ public class CaptchaTrader {
* @param applicationKey
* the key
*/
public CaptchaTrader(URI apiURL, String applicationKey, String username,
public CaptchaTrader(URI apiURI, String applicationKey, String username,
String password) {
this.apiURL = apiURL;
this.apiURI = apiURI;
this.applicationKey = applicationKey;
this.username = username;
this.password = password;
@@ -74,7 +72,7 @@ public class CaptchaTrader {
*
* @param applicationKey
* the key
* @throws MalformedURLException
* @throws MalformedURIException
*/
public CaptchaTrader(String applicationKey, String username, String password) {
this(URI.create("http://api.captchatrader.com/"), applicationKey,
@@ -84,27 +82,27 @@ public class CaptchaTrader {
/**
* Submit a CAPTCHA already hosted on an existing website.
*
* @param url
* The URL of the CAPTCHA image.
* @param uri
* The URI of the CAPTCHA image.
* @return The decoded CAPTCHA.
* @throws Any
* exceptions sent by the server.
*/
public ResolvedCaptcha submit(URL url) throws CaptchaTraderException,
public ResolvedCaptcha submit(URI uri) throws CaptchaTraderException,
IOException {
final URI requestUri = apiURL.resolve("submit");
final URI requestUri = apiURI.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()));
entity.addPart("value", new StringBody(uri.toString()));
request.setEntity(entity);
final List<Object> response = validate(execute(request));
return new ResolvedCaptcha(this, ((Long) response.get(0)).intValue(),
return new ResolvedCaptcha(this, ((Long) response.get(0)).toString(),
(String) response.get(1));
}
@@ -118,17 +116,16 @@ public class CaptchaTrader {
* @throws CaptchaTraderException
* any of the possible errors
*/
public void response(ResolvedCaptcha captcha, boolean state)
public void respond(ResolvedCaptcha captcha, boolean state)
throws CaptchaTraderException, IOException {
final URI requestUri = apiURL.resolve("respond");
final URI requestUri = apiURI.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())));
entity.addPart("ticket", new StringBody(captcha.getID()));
entity.addPart("username", new StringBody(username));
request.setEntity(entity);
validate(execute(request));
@@ -143,7 +140,7 @@ public class CaptchaTrader {
*/
public int getCredits() throws CaptchaTraderException, IOException {
return ((Number) validate(
execute(new HttpGet(apiURL.resolve("get_credits/username:"
execute(new HttpGet(apiURI.resolve("get_credits/username:"
+ username + "/password:" + password + "/")))).get(1))
.intValue();
}
@@ -185,7 +182,7 @@ public class CaptchaTrader {
return new InvalidApplicationKeyException();
case "INVALID PARAMETERS":
return new InvalidParametersException();
case "INVALID URL":
case "INVALID URI":
return new InvalidURLException();
case "INVALID USER":
return new InvalidUserException();

View File

@@ -13,16 +13,16 @@ import com.captchatrader.exception.CaptchaTraderException;
*/
public class ResolvedCaptcha {
private final CaptchaTrader api;
private final int id;
private final String id;
private final String answer;
public ResolvedCaptcha(CaptchaTrader api, int id, String answer) {
public ResolvedCaptcha(CaptchaTrader api, String id, String answer) {
this.api = api;
this.id = id;
this.answer = answer;
}
public int getID() {
public String getID() {
return id;
}
@@ -31,10 +31,10 @@ public class ResolvedCaptcha {
}
public void valid() throws CaptchaTraderException, IOException {
api.response(this, true);
api.respond(this, true);
}
public void invalid() throws CaptchaTraderException, IOException {
api.response(this, true);
api.respond(this, true);
}
}

View File

@@ -32,7 +32,7 @@ public class CaptchaTraderService implements ImageCaptchaService {
/**
* The current application key to be used
*/
private String currentApplicationKey = APP_KEY;
private String currentApplicationKey = CaptchaTraderService.APP_KEY;
/**
* The CaptchaTrader.com API object
@@ -56,8 +56,8 @@ public class CaptchaTraderService implements ImageCaptchaService {
public void solve(ImageCaptcha captcha)
throws UnsolvableCaptchaServiceException {
try {
logger.debug("Resolving CAPTCHA {}", captcha.getImageURL());
final ResolvedCaptcha resolved = api.submit(captcha.getImageURL());
logger.debug("Resolving CAPTCHA {}", captcha.getImageURI());
final ResolvedCaptcha resolved = api.submit(captcha.getImageURI());
captcha.setAnswer(resolved.getAnswer());
captcha.setAttachment(resolved);
logger.debug("CAPTCHA solved, answer is \"{}\"",

View File

@@ -3,7 +3,7 @@
*/
package com.captchatrader;
import java.net.URL;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
@@ -33,7 +33,7 @@ public class CaptchaTraderTest {
"2acc44805ec208cc4d6b00c75a414996", p.getProperty("username"),
p.getProperty("password"));
final ResolvedCaptcha resolved = api
.submit(new URL(
.submit(new URI(
"http://www.google.com/recaptcha/api/image?c=03AHJ_VusNSxAzZgs9OEvH79rOWOFDYXE2ElE5qkCr9kFU-ZU7gqy72tqEL3j_qCLYwdXgh4jaxU1iECISuUwt0zHbelni-lq8c7RVGSjUtJiMyHwlTTsG5CxWKIEus--yy3GPvwaW9l4N7hFnT57lLq272EOxcFDGYA"));
System.out.println(resolved);
resolved.valid();