1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-05 23:22:51 +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

@@ -1,20 +1,22 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>httpchannel-service</artifactId>
<groupId>com.rogiel.httpchannel</groupId>
<version>1.0.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>httpchannel-service-uploadking</artifactId>
<groupId>com.rogiel.httpchannel.services</groupId>
<name>HttpChannel/Service/UploadKing</name>
<description>Provides upload access to uploadking.com</description>
<dependencies>
<dependency>
<groupId>com.rogiel.httpchannel</groupId>
<artifactId>httpchannel-captcha-recaptcha</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>httpchannel-service</artifactId>
<groupId>com.rogiel.httpchannel</groupId>
<version>1.0.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>httpchannel-service-uploadking</artifactId>
<groupId>com.rogiel.httpchannel.services</groupId>
<name>HttpChannel/Service/UploadKing</name>
<description>Provides upload access to uploadking.com</description>
<dependencies>
<dependency>
<groupId>com.rogiel.httpchannel.captcha</groupId>
<artifactId>httpchannel-captcha-captchatrader</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -6,8 +6,8 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.captcha.impl.AjaxReCaptchaService;
import com.rogiel.httpchannel.captcha.impl.ReCaptcha;
import com.rogiel.httpchannel.captcha.ImageCaptcha;
import com.rogiel.httpchannel.captcha.ReCaptchaExtractor;
import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpDownloader;
import com.rogiel.httpchannel.service.AbstractHttpService;
@@ -67,8 +67,6 @@ public class UploadKingService extends AbstractHttpService implements Service,
private static final String INVALID_LOGIN_STRING = "Incorrect username and/or password. Please try again!";
private final AjaxReCaptchaService captchaService = new AjaxReCaptchaService();
@Override
public ServiceID getID() {
return SERVICE_ID;
@@ -190,6 +188,9 @@ public class UploadKingService extends AbstractHttpService implements Service,
final String url = page.findFormAction(UPLOAD_URL_PATTERN);
final String uploadID = page.getInputValue("UPLOAD_IDENTIFIER");
logger.debug("Upload URL: {}, UserCookie: {}, UploadID: {}",
new Object[] { url, userCookie, uploadID });
final LinkedUploadChannel channel = createLinkedChannel(this);
uploadFuture = multipartPost(url).parameter("file", channel)
.parameter("u", userCookie)
@@ -202,6 +203,7 @@ public class UploadKingService extends AbstractHttpService implements Service,
try {
final String linkId = PatternUtils.find(DOWNLOAD_ID_PATTERN,
uploadFuture.get(), 1);
logger.debug("Upload to uploadking.com finished");
if (linkId == null)
return null;
return new StringBuilder("http://www.uploadking.com/").append(
@@ -224,19 +226,29 @@ public class UploadKingService extends AbstractHttpService implements Service,
@Override
public DownloadChannel openChannel(DownloadListener listener,
long position) throws IOException {
logger.debug("Downloading {} with uploadking.com");
HTMLPage page = get(url).asPage();
final int waitTime = page.findScriptAsInt(TIMER_PATTERN, 1) * 1000;
logger.debug("Wait time is {}", waitTime);
final ReCaptcha captcha = captchaService.create(page.toString());
final ImageCaptcha captcha = ReCaptchaExtractor.extractAjaxCaptcha(
page, http);
if (captcha != null) {
logger.debug("Service is requiring CAPTCHA {}", captcha);
final long start = System.currentTimeMillis();
resolveCaptcha(captcha);
final long delta = System.currentTimeMillis() - start;
if (delta < waitTime)
if (delta < waitTime) {
logger.debug(
"After captcha resolving, still {} ms remaining",
delta);
timer(listener, waitTime - delta);
} else {
logger.debug("CAPTCHA solving took longer than timer, skipping it");
}
String content = post(url)
.parameter("recaptcha_challenge_field", captcha.getID())
@@ -244,10 +256,15 @@ public class UploadKingService extends AbstractHttpService implements Service,
captcha.getAnswer()).asString();
String downloadLink = PatternUtils.find(
DIERCT_DOWNLOAD_URL_PATTERN, content, 1);
if (downloadLink == null)
if (downloadLink == null) {
captchaService.invalid(captcha);
throw new InvalidCaptchaException();
} else {
captchaService.valid(captcha);
}
downloadLink = downloadLink.replaceAll(Pattern.quote("\\/"),
"/");
logger.debug("Direct download URL is {}", downloadLink);
return download(get(downloadLink).position(position));
}
throw new DownloadLinkNotFoundException();

View File

@@ -4,14 +4,20 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Properties;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import com.rogiel.httpchannel.captcha.CaptchaService;
import com.rogiel.httpchannel.captcha.impl.CaptchaTraderService;
import com.rogiel.httpchannel.service.DownloadChannel;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.util.ChannelUtils;
@@ -37,26 +43,19 @@ public class UploadKingServiceTest {
System.out.println(url);
}
// @Test
// public void testDownloader() throws IOException {
// service.setCaptchaResolver(new CaptchaResolver() {
// @Override
// public boolean resolve(Captcha rawCaptcha) {
// final ImageCaptcha captcha = (ImageCaptcha) rawCaptcha;
// System.out.println(captcha.getImageURL());
// try {
// captcha.setAnswer(new BufferedReader(new InputStreamReader(
// System.in)).readLine());
// System.out.println("Answer is: " + captcha.getAnswer());
// return true;
// } catch (IOException e) {
// return false;
// }
// }
// });
//
// final DownloadChannel channel = service.getDownloader(
// new URL("http://www.uploadking.com/WM3PHD9JAY")).openChannel(512);
// System.out.println(new String(ChannelUtils.toByteArray(channel)));
// }
@Test
public void testDownloader() throws IOException {
final Properties p = new Properties();
p.load(Files.newInputStream(
Paths.get("../../httpchannel-captcha/src/test/resources/captchatrader.properties"),
StandardOpenOption.READ));
final CaptchaService<?> s = new CaptchaTraderService();
s.authenticate(p.getProperty("username"), p.getProperty("password"));
service.setCaptchaService(s);
final DownloadChannel channel = service.getDownloader(
new URL("http://www.uploadking.com/WM3PHD9JAY")).openChannel(0);
System.out.println(new String(ChannelUtils.toByteArray(channel)));
}
}