1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-05 23:22:51 +00:00

Implements several services and improves API

This commit is contained in:
2012-01-15 18:31:50 -02:00
parent f210afd16a
commit 23f80b50e6
117 changed files with 3741 additions and 1335 deletions

View File

@@ -0,0 +1,20 @@
<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-depositfiles</artifactId>
<groupId>com.rogiel.httpchannel.services</groupId>
<name>HttpChannel/Service/DepositFiles</name>
<description>Provides upload access to depositfiles.com</description>
<dependencies>
<dependency>
<groupId>com.rogiel.httpchannel</groupId>
<artifactId>httpchannel-captcha-recaptcha</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,212 @@
package com.rogiel.httpchannel.service.impl;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.captcha.impl.ReCaptcha;
import com.rogiel.httpchannel.captcha.impl.ReCaptchaService;
import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AbstractUploader;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability;
import com.rogiel.httpchannel.service.CapabilityMatrix;
import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.ServiceID;
import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.Uploader;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration;
import com.rogiel.httpchannel.service.config.NullUploaderConfiguration;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.exception.UnresolvedCaptchaException;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* This service handles uploads to UploadKing.com.
*
* @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*/
public class DepositFilesService extends AbstractHttpService implements
Service, UploadService<NullUploaderConfiguration>,
AuthenticationService<NullAuthenticatorConfiguration> {
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("depositfiles");
private static final Pattern UPLOAD_URL_PATTERN = Pattern
.compile("http://fileshare([0-9]*)\\.depositfiles\\.com/(.*)/\\?X-Progress-ID=(.*)");
private static final Pattern DOWNLOAD_URL_PATTERN = Pattern
.compile("http://(www\\.)?depositfiles\\.com/files/([0-9A-z]*)");
private static final Pattern VALID_LOGIN_REDIRECT = Pattern
.compile("window.location.href");
private final ReCaptchaService captchaService = new ReCaptchaService();
@Override
public ServiceID getID() {
return SERVICE_ID;
}
@Override
public int getMajorVersion() {
return 1;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize, NullUploaderConfiguration configuration) {
return new UploadKingUploader(filename, filesize, configuration);
}
@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize) {
return getUploader(filename, filesize, newUploaderConfiguration());
}
@Override
public NullUploaderConfiguration newUploaderConfiguration() {
return NullUploaderConfiguration.SHARED_INSTANCE;
}
@Override
public long getMaximumFilesize() {
return 2 * 1024 * 1024 * 1024;
}
@Override
public String[] getSupportedExtensions() {
return null;
}
@Override
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
return new CapabilityMatrix<UploaderCapability>(
UploaderCapability.UNAUTHENTICATED_UPLOAD,
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD,
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential, NullAuthenticatorConfiguration configuration) {
return new UploadKingAuthenticator(credential, configuration);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential) {
return getAuthenticator(credential, newAuthenticatorConfiguration());
}
@Override
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() {
return NullAuthenticatorConfiguration.SHARED_INSTANCE;
}
@Override
public CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability() {
return new CapabilityMatrix<AuthenticatorCapability>();
}
protected class UploadKingUploader extends
AbstractUploader<NullUploaderConfiguration> implements
Uploader<NullUploaderConfiguration>,
LinkedUploadChannelCloseCallback {
private Future<HTMLPage> uploadFuture;
public UploadKingUploader(String filename, long filesize,
NullUploaderConfiguration configuration) {
super(filename, filesize, configuration);
}
@Override
public UploadChannel openChannel() throws IOException {
final HTMLPage page = get("http://www.depositfiles.com/").asPage();
final String url = page.findFormAction(UPLOAD_URL_PATTERN);
final String uploadID = page.getInputValue("UPLOAD_IDENTIFIER");
final String maxFileSize = page.getInputValue("MAX_FILE_SIZE");
final LinkedUploadChannel channel = createLinkedChannel(this);
uploadFuture = multipartPost(url).parameter("files", channel)
.parameter("go", true)
.parameter("UPLOAD_IDENTIFIER", uploadID)
.parameter("agree", true)
.parameter("MAX_FILE_SIZE", maxFileSize).asPageAsync();
return waitChannelLink(channel, uploadFuture);
}
@Override
public String finish() throws IOException {
try {
final String link = uploadFuture.get().findScript(
DOWNLOAD_URL_PATTERN, 0);
if (link == null)
return null;
return link;
} catch (InterruptedException e) {
return null;
} catch (ExecutionException e) {
throw (IOException) e.getCause();
}
}
}
protected class UploadKingAuthenticator extends
AbstractAuthenticator<NullAuthenticatorConfiguration> implements
Authenticator<NullAuthenticatorConfiguration> {
public UploadKingAuthenticator(Credential credential,
NullAuthenticatorConfiguration configuration) {
super(credential, configuration);
}
@Override
public void login() throws IOException {
HTMLPage page = post("http://depositfiles.com/login.php?return=%2F")
.parameter("go", true)
.parameter("login", credential.getUsername())
.parameter("password", credential.getPassword()).asPage();
final ReCaptcha captcha = captchaService.create(page);
if (captcha != null) {
if (!resolveCaptcha(captcha))
throw new UnresolvedCaptchaException();
page = post("http://depositfiles.com/login.php?return=%2F")
.parameter("go", true)
.parameter("login", credential.getUsername())
.parameter("password", credential.getPassword())
.parameter("recaptcha_challenge_field", captcha.getID())
.parameter("recaptcha_response_field",
captcha.getAnswer()).asPage();
}
if (!page.contains(VALID_LOGIN_REDIRECT))
throw new AuthenticationInvalidCredentialException();
return;
}
@Override
public void logout() throws IOException {
post("http://www.uploadking.com/login").parameter("do", "logout")
.request();
// TODO check logout status
}
}
}

View File

@@ -0,0 +1 @@
com.rogiel.httpchannel.service.impl.MultiUploadService

View File

@@ -0,0 +1,39 @@
package com.rogiel.httpchannel.service.impl;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.util.ChannelUtils;
public class DepositFilesServiceTest {
private DepositFilesService service;
@Before
public void setUp() throws Exception {
service = new DepositFilesService();
}
@Test
public void testNonLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.UNAUTHENTICATED_UPLOAD",
service.getUploadCapabilities().has(
UploaderCapability.UNAUTHENTICATED_UPLOAD));
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final URL url = ChannelUtils.upload(service, path);
Assert.assertNotNull(url);
System.out.println(url);
}
}

View File

@@ -0,0 +1,3 @@
This is a simple upload test file.
This is for testing purposes only.

View File

@@ -7,4 +7,7 @@
<relativePath>..</relativePath>
</parent>
<artifactId>httpchannel-service-hotfile</artifactId>
<groupId>com.rogiel.httpchannel.services</groupId>
<name>HttpChannel/Service/HotFile</name>
<description>Provides download and upload access to hotfile.com</description>
</project>

View File

@@ -25,12 +25,12 @@ import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.htmlparser.Tag;
import com.rogiel.httpchannel.service.AbstractDownloader;
import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpDownloader;
import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AbstractUploader;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability;
@@ -50,28 +50,27 @@ import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.channel.InputStreamDownloadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannelContentBody;
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper;
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration;
import com.rogiel.httpchannel.service.config.NullDownloaderConfiguration;
import com.rogiel.httpchannel.service.config.NullUploaderConfiguration;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.impl.HotFileService.HotFileServiceConfiguration;
import com.rogiel.httpchannel.util.ThreadUtils;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* This service handles login, upload and download to HotFile.com.
*
* @author Rogiel
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @since 1.0
*/
public class HotFileService extends
AbstractHttpService<HotFileServiceConfiguration> implements Service,
UploadService, DownloadService, AuthenticationService {
public class HotFileService extends AbstractHttpService implements Service,
UploadService<NullUploaderConfiguration>,
DownloadService<NullDownloaderConfiguration>,
AuthenticationService<NullAuthenticatorConfiguration> {
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("hotfile");
private static final Pattern UPLOAD_URL_PATTERN = Pattern
.compile("http://u[0-9]*\\.hotfile\\.com/upload\\.cgi\\?[0-9]*");
@@ -85,11 +84,6 @@ public class HotFileService extends
private static final Pattern DOWNLOAD_URL_PATTERN = Pattern
.compile("http://hotfile\\.com/dl/([0-9]*)/([A-Za-z0-9]*)/(.*)");
public HotFileService() {
super(ServiceConfigurationHelper
.defaultConfiguration(HotFileServiceConfiguration.class));
}
@Override
public ServiceID getID() {
return SERVICE_ID;
@@ -106,9 +100,20 @@ public class HotFileService extends
}
@Override
public Uploader getUploader(String filename, long filesize,
String description) {
return new HotFileUploader(filename, filesize);
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize, NullUploaderConfiguration configuration) {
return new HotFileUploader(filename, filesize, configuration);
}
@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize) {
return getUploader(filename, filesize, newUploaderConfiguration());
}
@Override
public NullUploaderConfiguration newUploaderConfiguration() {
return NullUploaderConfiguration.SHARED_INSTANCE;
}
@Override
@@ -130,8 +135,19 @@ public class HotFileService extends
}
@Override
public Downloader getDownloader(URL url) {
return new HotFileDownloader(url);
public Downloader<NullDownloaderConfiguration> getDownloader(URL url,
NullDownloaderConfiguration configuration) {
return new HotFileDownloader(url, configuration);
}
@Override
public Downloader<NullDownloaderConfiguration> getDownloader(URL url) {
return getDownloader(url, newDownloaderConfiguration());
}
@Override
public NullDownloaderConfiguration newDownloaderConfiguration() {
return NullDownloaderConfiguration.SHARED_INSTANCE;
}
@Override
@@ -146,8 +162,20 @@ public class HotFileService extends
}
@Override
public Authenticator getAuthenticator(Credential credential) {
return new HotFileAuthenticator(credential);
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential, NullAuthenticatorConfiguration configuration) {
return new HotFileAuthenticator(credential, configuration);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential) {
return getAuthenticator(credential, newAuthenticatorConfiguration());
}
@Override
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() {
return NullAuthenticatorConfiguration.SHARED_INSTANCE;
}
@Override
@@ -155,36 +183,27 @@ public class HotFileService extends
return new CapabilityMatrix<AuthenticatorCapability>();
}
protected class HotFileUploader implements Uploader,
protected class HotFileUploader extends
AbstractUploader<NullUploaderConfiguration> implements
Uploader<NullUploaderConfiguration>,
LinkedUploadChannelCloseCallback {
private final String filename;
private final long filesize;
private Future<HTMLPage> uploadFuture;
public HotFileUploader(String filename, long filesize) {
super();
this.filename = filename;
this.filesize = filesize;
public HotFileUploader(String filename, long filesize,
NullUploaderConfiguration configuration) {
super(filename, filesize, configuration);
}
@Override
public UploadChannel upload() throws IOException {
final HTMLPage page = getAsPage("http://www.hotfile.com/");
final String action = page.getFormAction(UPLOAD_URL_PATTERN);
public UploadChannel openChannel() throws IOException {
final HTMLPage page = get("http://www.hotfile.com/").asPage();
final String action = page.findFormAction(UPLOAD_URL_PATTERN);
final LinkedUploadChannel channel = new LinkedUploadChannel(this,
filesize, filename);
final MultipartEntity entity = new MultipartEntity();
final LinkedUploadChannel channel = createLinkedChannel(this);
entity.addPart("uploads[]", new LinkedUploadChannelContentBody(
channel));
uploadFuture = postAsPageAsync(action, entity);
while (!channel.isLinked() && !uploadFuture.isDone()) {
ThreadUtils.sleep(100);
}
return channel;
uploadFuture = multipartPost(action)
.parameter("uploads[]", channel).asPageAsync();
return waitChannelLink(channel, uploadFuture);
}
@Override
@@ -199,17 +218,17 @@ public class HotFileService extends
}
}
protected class HotFileDownloader extends AbstractDownloader {
private final URL url;
public HotFileDownloader(URL url) {
this.url = url;
protected class HotFileDownloader extends
AbstractHttpDownloader<NullDownloaderConfiguration> {
public HotFileDownloader(URL url,
NullDownloaderConfiguration configuration) {
super(url, configuration);
}
@Override
public DownloadChannel download(DownloadListener listener, long position)
throws IOException {
final HTMLPage page = getAsPage(url.toString());
public DownloadChannel openChannel(DownloadListener listener,
long position) throws IOException {
final HTMLPage page = get(url).asPage();
// // try to find timer
// final String stringTimer = PatternUtils.find(DOWNLOAD_TIMER,
@@ -224,11 +243,12 @@ public class HotFileService extends
// }
final String downloadUrl = page
.getLink(DOWNLOAD_DIRECT_LINK_PATTERN);
.findLink(DOWNLOAD_DIRECT_LINK_PATTERN);
// final String tmHash = PatternUtils.find(DOWNLOAD_TMHASH_PATTERN,
// content);F
if (downloadUrl != null && downloadUrl.length() > 0) {
final HttpResponse downloadResponse = get(downloadUrl);
final HttpResponse downloadResponse = get(downloadUrl)
.request();
final String filename = FilenameUtils.getName(downloadUrl);
long contentLength = getContentLength(downloadResponse);
@@ -284,23 +304,21 @@ public class HotFileService extends
}
}
protected class HotFileAuthenticator implements Authenticator {
private final Credential credential;
public HotFileAuthenticator(Credential credential) {
this.credential = credential;
protected class HotFileAuthenticator extends
AbstractAuthenticator<NullAuthenticatorConfiguration> implements
Authenticator<NullAuthenticatorConfiguration> {
public HotFileAuthenticator(Credential credential,
NullAuthenticatorConfiguration configuration) {
super(credential, configuration);
}
@Override
public void login() throws ClientProtocolException, IOException {
final MultipartEntity entity = new MultipartEntity();
HTMLPage page = post("http://www.hotfile.com/login.php")
.parameter("returnto", "/index.php")
.parameter("user", credential.getUsername())
.parameter("pass", credential.getPassword()).asPage();
entity.addPart("returnto", new StringBody("/index.php"));
entity.addPart("user", new StringBody(credential.getUsername()));
entity.addPart("pass", new StringBody(credential.getPassword()));
HTMLPage page = postAsPage("http://www.hotfile.com/login.php",
entity);
final Tag accountTag = page.getTagByID("account");
if (accountTag == null)
throw new AuthenticationInvalidCredentialException();
@@ -308,18 +326,12 @@ public class HotFileService extends
@Override
public void logout() throws IOException {
final MultipartEntity entity = new MultipartEntity();
entity.addPart("logout", new StringBody("1"));
postAsString("http://www.megaupload.com/?c=account", entity);
post("http://www.megaupload.com/?c=account").parameter("logout",
true).request();
// TODO check logout status
}
}
public static interface HotFileServiceConfiguration extends
ServiceConfiguration {
}
@Override
public String toString() {
return this.getClass().getSimpleName() + " " + getMajorVersion() + "."

View File

@@ -20,7 +20,7 @@ import junit.framework.Assert;
import org.junit.Test;
import com.rogiel.httpchannel.service.Services;
import com.rogiel.httpchannel.service.helper.Services;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>

View File

@@ -22,8 +22,6 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.SeekableByteChannel;
@@ -38,23 +36,19 @@ import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.DownloadChannel;
import com.rogiel.httpchannel.service.DownloadService;
import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.ServiceHelper;
import com.rogiel.httpchannel.service.ServiceID;
import com.rogiel.httpchannel.service.Services;
import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.helper.Services;
import com.rogiel.httpchannel.service.helper.UploadServices;
import com.rogiel.httpchannel.util.ChannelUtils;
public class HotFileServiceTest {
private Service service;
private ServiceHelper helper;
private HotFileService service;
/**
* See <b>src/test/resources/config/hotfile.properties</b>
@@ -76,7 +70,6 @@ public class HotFileServiceTest {
public void setUp() throws Exception {
// MegaUploadServiceConfiguration.class;
service = new HotFileService();
helper = new ServiceHelper(service);
final Properties properties = new Properties();
properties.load(new FileInputStream(
@@ -87,33 +80,31 @@ public class HotFileServiceTest {
@Test
public void testServiceId() {
System.out.println("Service: " + service.toString());
assertEquals(ServiceID.create("hotfile"), service.getID());
}
@Test
public void testValidAuthenticator() throws IOException {
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
}
@Test(expected = AuthenticationInvalidCredentialException.class)
public void testInvalidAuthenticator() throws IOException {
((AuthenticationService) service).getAuthenticator(
service.getAuthenticator(
new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login();
}
@Test
public void testNonLoguedInUploader() throws IOException,
URISyntaxException {
public void testNonLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.FREE_UPLOAD",
((UploadService) service).getUploadCapabilities().has(
service.getUploadCapabilities().has(
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final UploadChannel channel = helper.upload(path,
"httpchannel test upload");
final UploadChannel channel = UploadServices.upload(service, path)
.openChannel();
final SeekableByteChannel inChannel = Files.newByteChannel(path);
try {
@@ -131,15 +122,15 @@ public class HotFileServiceTest {
public void testLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.PREMIUM_UPLOAD",
((UploadService) service).getUploadCapabilities().has(
service.getUploadCapabilities().has(
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final UploadChannel channel = helper.upload(path,
"httpchannel test upload");
final UploadChannel channel = UploadServices.upload(service, path)
.openChannel();
final SeekableByteChannel inChannel = Files.newByteChannel(path);
try {
@@ -154,30 +145,29 @@ public class HotFileServiceTest {
}
@Test
public void testDownloader() throws IOException, MalformedURLException {
public void testDownloader() throws IOException {
final URL downloadUrl = new URL(
"http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.htm");
final DownloadService service = Services.matchURL(downloadUrl);
final DownloadService<?> service = Services.matchURL(downloadUrl);
final DownloadChannel channel = service.getDownloader(downloadUrl)
.download(null, 0);
.openChannel(null, 0);
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(Channels.newInputStream(channel), bout);
System.out.println(bout.size());
}
@Test
public void testLoggedInDownloader() throws IOException,
MalformedURLException {
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
public void testLoggedInDownloader() throws IOException {
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
final DownloadChannel channel = ((DownloadService) service)
final DownloadChannel channel = service
.getDownloader(
new URL(
"http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html"))
.download(null, 0);
.openChannel(null, 0);
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(Channels.newInputStream(channel), bout);

View File

@@ -1,41 +0,0 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service.impl;
import org.junit.Assert;
import org.junit.Test;
import com.rogiel.httpchannel.service.Service;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ServiceCloningTest {
@Test
public void testDiscovery() {
Service original = HotFileService.SERVICE_ID.getService();
Service service = HotFileService.SERVICE_ID.getService().clone();
// set configuration to anything else
service.setServiceConfiguration(null);
Assert.assertNotSame(service, original);
Assert.assertNotSame(service.getServiceConfiguration(),
original.getServiceConfiguration());
}
}

View File

@@ -7,4 +7,7 @@
<relativePath>..</relativePath>
</parent>
<artifactId>httpchannel-service-megaupload</artifactId>
<groupId>com.rogiel.httpchannel.services</groupId>
<name>HttpChannel/Service/MegaUpload</name>
<description>Provides upload and download access to megaupload.com</description>
</project>

View File

@@ -0,0 +1,16 @@
package com.rogiel.httpchannel.service.impl;
import com.rogiel.httpchannel.service.Downloader.DownloaderConfiguration;
public class MegaUploadDownloaderConfiguration implements
DownloaderConfiguration {
private boolean respectWaitTime = true;
public boolean getRespectWaitTime() {
return respectWaitTime;
}
public void setRespectWaitTime(boolean respectWaitTime) {
this.respectWaitTime = respectWaitTime;
}
}

View File

@@ -18,8 +18,6 @@ package com.rogiel.httpchannel.service.impl;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
@@ -27,14 +25,11 @@ import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import com.rogiel.httpchannel.service.AbstractDownloader;
import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpDownloader;
import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AbstractUploader;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability;
@@ -51,37 +46,31 @@ import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.Uploader;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.channel.InputStreamDownloadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannelContentBody;
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper;
import com.rogiel.httpchannel.service.config.ServiceConfigurationProperty;
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.exception.DownloadLimitExceededException;
import com.rogiel.httpchannel.service.exception.DownloadLinkNotFoundException;
import com.rogiel.httpchannel.service.exception.UploadLinkNotFoundException;
import com.rogiel.httpchannel.service.impl.MegaUploadService.MegaUploadServiceConfiguration;
import com.rogiel.httpchannel.util.HttpClientUtils;
import com.rogiel.httpchannel.util.PatternUtils;
import com.rogiel.httpchannel.util.ThreadUtils;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* This service handles login, upload and download to MegaUpload.com.
*
* @author Rogiel
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @since 1.0
*/
public class MegaUploadService extends
AbstractHttpService<MegaUploadServiceConfiguration> implements Service,
UploadService, DownloadService, AuthenticationService {
public class MegaUploadService extends AbstractHttpService implements Service,
UploadService<MegaUploadUploaderConfiguration>,
DownloadService<MegaUploadDownloaderConfiguration>,
AuthenticationService<NullAuthenticatorConfiguration> {
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("megaupload");
private static final Pattern UPLOAD_URL_PATTERN = Pattern
.compile("http://www([0-9]*)\\.megaupload\\.com/upload_done\\.php\\?UPLOAD_IDENTIFIER=[0-9]*");
@@ -98,11 +87,6 @@ public class MegaUploadService extends
private static final Pattern LOGIN_USERNAME_PATTERN = Pattern
.compile("flashvars\\.username = \"(.*)\";");
public MegaUploadService() {
super(ServiceConfigurationHelper
.defaultConfiguration(MegaUploadServiceConfiguration.class));
}
@Override
public ServiceID getID() {
return SERVICE_ID;
@@ -119,9 +103,21 @@ public class MegaUploadService extends
}
@Override
public Uploader getUploader(String filename, long filesize,
String description) {
return new MegaUploadUploader(filename, filesize, description);
public Uploader<MegaUploadUploaderConfiguration> getUploader(
String filename, long filesize,
MegaUploadUploaderConfiguration configuration) {
return new MegaUploadUploader(filename, filesize, configuration);
}
@Override
public Uploader<MegaUploadUploaderConfiguration> getUploader(
String filename, long filesize) {
return getUploader(filename, filesize, newUploaderConfiguration());
}
@Override
public MegaUploadUploaderConfiguration newUploaderConfiguration() {
return new MegaUploadUploaderConfiguration();
}
@Override
@@ -143,8 +139,19 @@ public class MegaUploadService extends
}
@Override
public Downloader getDownloader(URL url) {
return new MegaUploadDownloader(url);
public Downloader<MegaUploadDownloaderConfiguration> getDownloader(URL url,
MegaUploadDownloaderConfiguration configuration) {
return new MegaUploadDownloader(url, configuration);
}
@Override
public Downloader<MegaUploadDownloaderConfiguration> getDownloader(URL url) {
return getDownloader(url, newDownloaderConfiguration());
}
@Override
public MegaUploadDownloaderConfiguration newDownloaderConfiguration() {
return new MegaUploadDownloaderConfiguration();
}
@Override
@@ -156,13 +163,28 @@ public class MegaUploadService extends
public CapabilityMatrix<DownloaderCapability> getDownloadCapabilities() {
return new CapabilityMatrix<DownloaderCapability>(
DownloaderCapability.UNAUTHENTICATED_DOWNLOAD,
DownloaderCapability.UNAUTHENTICATED_RESUME,
DownloaderCapability.NON_PREMIUM_ACCOUNT_DOWNLOAD,
DownloaderCapability.PREMIUM_ACCOUNT_DOWNLOAD);
DownloaderCapability.NON_PREMIUM_ACCOUNT_RESUME,
DownloaderCapability.PREMIUM_ACCOUNT_DOWNLOAD,
DownloaderCapability.PREMIUM_ACCOUNT_RESUME);
}
@Override
public Authenticator getAuthenticator(Credential credential) {
return new MegaUploadAuthenticator(credential);
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential, NullAuthenticatorConfiguration configuration) {
return new MegaUploadAuthenticator(credential, configuration);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential) {
return getAuthenticator(credential, newAuthenticatorConfiguration());
}
@Override
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() {
return NullAuthenticatorConfiguration.SHARED_INSTANCE;
}
@Override
@@ -170,50 +192,35 @@ public class MegaUploadService extends
return new CapabilityMatrix<AuthenticatorCapability>();
}
protected class MegaUploadUploader implements Uploader,
protected class MegaUploadUploader extends
AbstractUploader<MegaUploadUploaderConfiguration> implements
Uploader<MegaUploadUploaderConfiguration>,
LinkedUploadChannelCloseCallback {
private final String filename;
private final long filesize;
private final String description;
private Future<String> uploadFuture;
public MegaUploadUploader(String filename, long filesize,
String description) {
this.filename = filename;
this.filesize = filesize;
this.description = (description != null ? description
: configuration.getDefaultUploadDescription());
MegaUploadUploaderConfiguration configuration) {
super(filename, filesize, configuration);
}
@Override
public UploadChannel upload() throws IOException {
final HTMLPage page = getAsPage("http://www.megaupload.com/multiupload/");
final String url = page.getFormAction(UPLOAD_URL_PATTERN);
public UploadChannel openChannel() throws IOException {
final HTMLPage page = get("http://www.megaupload.com/multiupload/")
.asPage();
final String url = page.findFormAction(UPLOAD_URL_PATTERN);
final LinkedUploadChannel channel = new LinkedUploadChannel(this,
filesize, filename);
final MultipartEntity entity = new MultipartEntity();
entity.addPart("multifile_0", new LinkedUploadChannelContentBody(
channel));
entity.addPart("multimessage_0", new StringBody(description));
uploadFuture = postAsStringAsync(url, entity);
while (!channel.isLinked() && !uploadFuture.isDone()) {
ThreadUtils.sleep(100);
}
return channel;
final LinkedUploadChannel channel = createLinkedChannel(this);
uploadFuture = multipartPost(url)
.parameter("multimessage_0", configuration.description())
.parameter("multifile_0", channel).asStringAsync();
return waitChannelLink(channel, uploadFuture);
}
@Override
public String finish() throws IOException {
try {
String link = PatternUtils.find(DOWNLOAD_URL_PATTERN,
return PatternUtils.find(DOWNLOAD_URL_PATTERN,
uploadFuture.get());
if (link == null)
throw new UploadLinkNotFoundException();
return link;
} catch (InterruptedException e) {
return null;
} catch (ExecutionException e) {
@@ -222,17 +229,18 @@ public class MegaUploadService extends
}
}
protected class MegaUploadDownloader extends AbstractDownloader {
private final URL url;
public MegaUploadDownloader(URL url) {
this.url = url;
protected class MegaUploadDownloader extends
AbstractHttpDownloader<MegaUploadDownloaderConfiguration> implements
Downloader<MegaUploadDownloaderConfiguration> {
public MegaUploadDownloader(URL url,
MegaUploadDownloaderConfiguration configuration) {
super(url, configuration);
}
@Override
public DownloadChannel download(DownloadListener listener, long position)
throws IOException {
HttpResponse response = get(url.toString());
public DownloadChannel openChannel(DownloadListener listener,
long position) throws IOException {
HttpResponse response = get(url).request();
// disable direct downloads, we don't support them!
if (response.getEntity().getContentType().getValue()
@@ -240,30 +248,28 @@ public class MegaUploadService extends
// close connection
response.getEntity().getContent().close();
final List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("do", "directdownloads"));
pairs.add(new BasicNameValuePair("accountupdate", "1"));
pairs.add(new BasicNameValuePair("set_ddl", "0"));
// execute update
postAsString("http://www.megaupload.com/?c=account",
new UrlEncodedFormEntity(pairs));
post("http://www.megaupload.com/?c=account")
.parameter("do", "directdownloads")
.parameter("accountupdate", "1")
.parameter("set_ddl", "0").request();
// execute and re-request download
response = get(url.toString());
response = get(url).request();
}
final HTMLPage page = HttpClientUtils.toPage(response);
// try to find timer
int timer = page.findIntegerInScript(DOWNLOAD_TIMER, 1);
if (timer > 0 && configuration.respectWaitTime()) {
int timer = page.findScriptAsInt(DOWNLOAD_TIMER, 1);
if (timer > 0 && configuration.getRespectWaitTime()) {
timer(listener, timer * 1000);
}
final String downloadUrl = page
.getLink(DOWNLOAD_DIRECT_LINK_PATTERN);
.findLink(DOWNLOAD_DIRECT_LINK_PATTERN);
if (downloadUrl != null && downloadUrl.length() > 0) {
final HttpResponse downloadResponse = get(downloadUrl, position);
final HttpResponse downloadResponse = get(downloadUrl)
.position(position).request();
if (downloadResponse.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN
|| downloadResponse.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
downloadResponse.getEntity().getContent().close();
@@ -274,7 +280,7 @@ public class MegaUploadService extends
final String filename = FilenameUtils.getName(downloadUrl);
final long contentLength = getContentLength(downloadResponse);
return new InputStreamDownloadChannel(downloadResponse
return createInputStreamChannel(downloadResponse
.getEntity().getContent(), contentLength, filename);
}
} else {
@@ -283,51 +289,34 @@ public class MegaUploadService extends
}
}
protected class MegaUploadAuthenticator implements Authenticator {
private final Credential credential;
public MegaUploadAuthenticator(Credential credential) {
this.credential = credential;
protected class MegaUploadAuthenticator extends
AbstractAuthenticator<NullAuthenticatorConfiguration> implements
Authenticator<NullAuthenticatorConfiguration> {
public MegaUploadAuthenticator(Credential credential,
NullAuthenticatorConfiguration configuration) {
super(credential, configuration);
}
@Override
public void login() throws IOException {
final MultipartEntity entity = new MultipartEntity();
entity.addPart("login", new StringBody("1"));
entity.addPart("username", new StringBody(credential.getUsername()));
entity.addPart("password", new StringBody(credential.getPassword()));
final HTMLPage page = postAsPage(
"http://www.megaupload.com/?c=login", entity);
String username = page.findInScript(LOGIN_USERNAME_PATTERN, 1);
final HTMLPage page = post("http://www.megaupload.com/?c=login")
.parameter("login", true)
.parameter("username", credential.getUsername())
.parameter("", credential.getPassword()).asPage();
String username = page.findScript(LOGIN_USERNAME_PATTERN, 1);
if (username == null)
throw new AuthenticationInvalidCredentialException();
}
@Override
public void logout() throws IOException {
final MultipartEntity entity = new MultipartEntity();
entity.addPart("logout", new StringBody("1"));
postAsString("http://www.megaupload.com/?c=account", entity);
post("http://www.megaupload.com/?c=account").parameter("logout",
true).request();
// TODO check logout status
}
}
public static interface MegaUploadServiceConfiguration extends
ServiceConfiguration {
@ServiceConfigurationProperty(key = "megaupload.wait", defaultValue = "true")
boolean respectWaitTime();
@ServiceConfigurationProperty(key = "megaupload.port", defaultValue = "80")
int getPreferedDownloadPort();
@ServiceConfigurationProperty(key = "megaupload.description", defaultValue = "Uploaded by seedbox-httpchannel")
String getDefaultUploadDescription();
}
@Override
public String toString() {
return this.getClass().getSimpleName() + " " + getMajorVersion() + "."

View File

@@ -0,0 +1,29 @@
package com.rogiel.httpchannel.service.impl;
import com.rogiel.httpchannel.service.Uploader.DescriptionableUploaderConfiguration;
import com.rogiel.httpchannel.service.Uploader.UploaderConfiguration;
import com.rogiel.httpchannel.service.impl.MegaUploadService.MegaUploadUploader;
/**
* Describes an configuration for an {@link MegaUploadUploader}
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class MegaUploadUploaderConfiguration implements UploaderConfiguration,
DescriptionableUploaderConfiguration {
/**
* The upload description
*/
private String description = DescriptionableUploaderConfiguration.DEFAULT_DESCRIPTION;
@Override
public String description() {
return description;
}
@Override
public MegaUploadUploaderConfiguration description(String description) {
this.description = description;
return this;
}
}

View File

@@ -20,7 +20,7 @@ import junit.framework.Assert;
import org.junit.Test;
import com.rogiel.httpchannel.service.Services;
import com.rogiel.httpchannel.service.helper.Services;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>

View File

@@ -20,10 +20,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.SeekableByteChannel;
@@ -38,25 +36,18 @@ import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.DownloadChannel;
import com.rogiel.httpchannel.service.DownloadListener;
import com.rogiel.httpchannel.service.DownloadService;
import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.ServiceHelper;
import com.rogiel.httpchannel.service.ServiceID;
import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.impl.MegaUploadService.MegaUploadServiceConfiguration;
import com.rogiel.httpchannel.service.helper.UploadServices;
import com.rogiel.httpchannel.util.ChannelUtils;
public class MegaUploadServiceTest {
private Service service;
private ServiceHelper helper;
private MegaUploadService service;
/**
* See <b>src/test/resources/config/megaupload.properties</b>
@@ -78,7 +69,6 @@ public class MegaUploadServiceTest {
public void setUp() throws Exception {
// MegaUploadServiceConfiguration.class;
service = new MegaUploadService();
helper = new ServiceHelper(service);
final Properties properties = new Properties();
properties.load(new FileInputStream(
@@ -89,19 +79,18 @@ public class MegaUploadServiceTest {
@Test
public void testServiceId() {
System.out.println("Service: " + service.toString());
assertEquals(ServiceID.create("megaupload"), service.getID());
}
@Test
public void testValidAuthenticator() throws IOException {
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
}
@Test(expected = AuthenticationInvalidCredentialException.class)
public void testInvalidAuthenticator() throws IOException {
((AuthenticationService) service).getAuthenticator(
service.getAuthenticator(
new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login();
}
@@ -109,11 +98,11 @@ public class MegaUploadServiceTest {
public void testNonLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.FREE_UPLOAD",
((UploadService) service).getUploadCapabilities().has(
service.getUploadCapabilities().has(
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final UploadChannel channel = helper.upload(path,
"httpchannel test upload");
final UploadChannel channel = UploadServices.upload(service, path)
.openChannel();
final SeekableByteChannel inChannel = Files.newByteChannel(path);
try {
@@ -131,15 +120,15 @@ public class MegaUploadServiceTest {
public void testLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.PREMIUM_UPLOAD",
((UploadService) service).getUploadCapabilities().has(
service.getUploadCapabilities().has(
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final UploadChannel channel = helper.upload(path,
"httpchannel test upload");
final UploadChannel channel = UploadServices.upload(service, path)
.openChannel();
final SeekableByteChannel inChannel = Files.newByteChannel(path);
try {
@@ -154,10 +143,10 @@ public class MegaUploadServiceTest {
}
@Test
public void testFreeDownloader() throws IOException, MalformedURLException {
final DownloadChannel channel = ((DownloadService) service)
.getDownloader(new URL("http://www.megaupload.com/?d=CVQKJ1KM"))
.download(new DownloadListener() {
public void testFreeDownloader() throws IOException {
final DownloadChannel channel = service.getDownloader(
new URL("http://www.megaupload.com/?d=CVQKJ1KM")).openChannel(
new DownloadListener() {
@Override
public boolean timer(long time) {
System.out.println("Waiting " + time);
@@ -172,14 +161,13 @@ public class MegaUploadServiceTest {
}
@Test
public void testPremiumDownloader() throws IOException,
MalformedURLException {
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
public void testPremiumDownloader() throws IOException {
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
final DownloadChannel channel = ((DownloadService) service)
.getDownloader(new URL("http://www.megaupload.com/?d=CVQKJ1KM"))
.download(new DownloadListener() {
final DownloadChannel channel = service.getDownloader(
new URL("http://www.megaupload.com/?d=CVQKJ1KM")).openChannel(
new DownloadListener() {
@Override
public boolean timer(long time) {
System.out.println("Waiting " + time);
@@ -192,17 +180,18 @@ public class MegaUploadServiceTest {
}
@Test
public void testNoWaitDownloader() throws IOException,
MalformedURLException {
public void testNoWaitDownloader() throws IOException {
service = new MegaUploadService();
service.setServiceConfiguration(ServiceConfigurationHelper.file(
MegaUploadServiceConfiguration.class, new File(
"src/test/resources/megaupload-nowait.properties")));
// service.setServiceConfiguration(ServiceConfigurationHelper.file(
// MegaUploadServiceConfiguration.class, new File(
// "src/test/resources/megaupload-nowait.properties")));
final MegaUploadDownloaderConfiguration config = new MegaUploadDownloaderConfiguration();
config.setRespectWaitTime(false);
@SuppressWarnings("unused")
final DownloadChannel channel = ((DownloadService) service)
.getDownloader(new URL("http://www.megaupload.com/?d=CVQKJ1KM"))
.download(new DownloadListener() {
@SuppressWarnings({ "unused" })
final DownloadChannel channel = service.getDownloader(
new URL("http://www.megaupload.com/?d=CVQKJ1KM"), config)
.openChannel(new DownloadListener() {
@Override
public boolean timer(long time) {
System.out.println("Waiting " + time);

View File

@@ -0,0 +1,21 @@
<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-multiupload</artifactId>
<groupId>com.rogiel.httpchannel.services</groupId>
<name>HttpChannel/Service/MultiUpload</name>
<description>Provides upload access to multiupload.com</description>
<dependencies>
<dependency>
<groupId>com.rogiel.httpchannel</groupId>
<artifactId>httpchannel-tests</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,265 @@
package com.rogiel.httpchannel.service.impl;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.http.PostMultipartRequest;
import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpDownloader;
import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AbstractUploader;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability;
import com.rogiel.httpchannel.service.CapabilityMatrix;
import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.DownloadChannel;
import com.rogiel.httpchannel.service.DownloadListener;
import com.rogiel.httpchannel.service.DownloadService;
import com.rogiel.httpchannel.service.Downloader;
import com.rogiel.httpchannel.service.DownloaderCapability;
import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.ServiceID;
import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.Uploader;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration;
import com.rogiel.httpchannel.service.config.NullDownloaderConfiguration;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.exception.DownloadLimitExceededException;
import com.rogiel.httpchannel.service.exception.DownloadLinkNotFoundException;
import com.rogiel.httpchannel.service.exception.DownloadNotAuthorizedException;
import com.rogiel.httpchannel.service.exception.DownloadNotResumableException;
import com.rogiel.httpchannel.service.impl.MultiUploadUploaderConfiguration.MultiUploadMirrorService;
import com.rogiel.httpchannel.util.PatternUtils;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* This service handles uploads to MultiUpload.com.
*
* @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*/
public class MultiUploadService extends AbstractHttpService implements Service,
UploadService<MultiUploadUploaderConfiguration>,
DownloadService<NullDownloaderConfiguration>,
AuthenticationService<NullAuthenticatorConfiguration> {
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("multiupload");
// http://www52.multiupload.com/upload/?UPLOAD_IDENTIFIER=73132658610746
private static final Pattern UPLOAD_URL_PATTERN = Pattern
.compile("http://www([0-9]*)\\.multiupload\\.com/upload/\\?UPLOAD_IDENTIFIER=[0-9]*");
private static final Pattern DOWNLOAD_ID_PATTERN = Pattern
.compile("\"downloadid\":\"([0-9a-zA-Z]*)\"");
private static final Pattern DOWNLOAD_LINK_PATTERN = Pattern
.compile("http://(www\\.)?multiupload\\.com/([0-9a-zA-Z]*)");
private static final Pattern DIRECT_DOWNLOAD_LINK_PATTERN = Pattern
.compile("http://www[0-9]*\\.multiupload\\.com(:[0-9]*)?/files/([0-9a-zA-Z]*)/(.*)");
@Override
public ServiceID getID() {
return SERVICE_ID;
}
@Override
public int getMajorVersion() {
return 1;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public Uploader<MultiUploadUploaderConfiguration> getUploader(
String filename, long filesize,
MultiUploadUploaderConfiguration configuration) {
if (configuration == null)
configuration = new MultiUploadUploaderConfiguration();
return new MultiUploadUploader(filename, filesize, configuration);
}
@Override
public Uploader<MultiUploadUploaderConfiguration> getUploader(
String filename, long filesize) {
return getUploader(filename, filesize, newUploaderConfiguration());
}
@Override
public MultiUploadUploaderConfiguration newUploaderConfiguration() {
return new MultiUploadUploaderConfiguration();
}
@Override
public long getMaximumFilesize() {
return 1 * 1024 * 1024 * 1024;
}
@Override
public String[] getSupportedExtensions() {
return null;
}
@Override
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
return new CapabilityMatrix<UploaderCapability>(
UploaderCapability.UNAUTHENTICATED_UPLOAD,
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD,
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD);
}
@Override
public Downloader<NullDownloaderConfiguration> getDownloader(URL url,
NullDownloaderConfiguration configuration) {
return new MultiUploaderDownloader(url, configuration);
}
@Override
public Downloader<NullDownloaderConfiguration> getDownloader(URL url) {
return getDownloader(url, newDownloaderConfiguration());
}
@Override
public NullDownloaderConfiguration newDownloaderConfiguration() {
return NullDownloaderConfiguration.SHARED_INSTANCE;
}
@Override
public boolean matchURL(URL url) {
return DOWNLOAD_LINK_PATTERN.matcher(url.toString()).matches();
}
@Override
public CapabilityMatrix<DownloaderCapability> getDownloadCapabilities() {
return new CapabilityMatrix<>(
DownloaderCapability.UNAUTHENTICATED_DOWNLOAD,
DownloaderCapability.UNAUTHENTICATED_RESUME,
DownloaderCapability.NON_PREMIUM_ACCOUNT_DOWNLOAD,
DownloaderCapability.NON_PREMIUM_ACCOUNT_RESUME);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential, NullAuthenticatorConfiguration configuration) {
return new MultiUploadAuthenticator(credential, configuration);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential) {
return getAuthenticator(credential, newAuthenticatorConfiguration());
}
@Override
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() {
return NullAuthenticatorConfiguration.SHARED_INSTANCE;
}
@Override
public CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability() {
return new CapabilityMatrix<AuthenticatorCapability>();
}
protected class MultiUploadUploader extends
AbstractUploader<MultiUploadUploaderConfiguration> implements
Uploader<MultiUploadUploaderConfiguration>,
LinkedUploadChannelCloseCallback {
private Future<String> uploadFuture;
public MultiUploadUploader(String filename, long filesize,
MultiUploadUploaderConfiguration configuration) {
super(filename, filesize, configuration);
}
@Override
public UploadChannel openChannel() throws IOException {
final String url = get("http://www.multiupload.com/").asPage()
.findFormAction(UPLOAD_URL_PATTERN);
final LinkedUploadChannel channel = createLinkedChannel(this);
PostMultipartRequest request = multipartPost(url).parameter(
"description_0", configuration.description()).parameter(
"file_0", channel);
for (final MultiUploadMirrorService mirror : configuration
.uploadServices()) {
request.parameter("service_" + mirror.id, 1);
}
uploadFuture = request.asStringAsync();
return waitChannelLink(channel, uploadFuture);
}
@Override
public String finish() throws IOException {
try {
final String linkId = PatternUtils.find(DOWNLOAD_ID_PATTERN,
uploadFuture.get(), 1);
if (linkId == null)
return null;
return new StringBuilder("http://www.multiupload.com/").append(
linkId).toString();
} catch (InterruptedException e) {
return null;
} catch (ExecutionException e) {
throw (IOException) e.getCause();
}
}
}
protected class MultiUploaderDownloader extends
AbstractHttpDownloader<NullDownloaderConfiguration> implements
Downloader<NullDownloaderConfiguration> {
protected MultiUploaderDownloader(URL url,
NullDownloaderConfiguration configuration) {
super(url, configuration);
}
@Override
public DownloadChannel openChannel(DownloadListener listener,
long position) throws IOException,
DownloadLinkNotFoundException, DownloadLimitExceededException,
DownloadNotAuthorizedException, DownloadNotResumableException {
final HTMLPage page = get(url).asPage();
final String link = page.findLink(DIRECT_DOWNLOAD_LINK_PATTERN);
if (link == null)
throw new DownloadLinkNotFoundException();
return download(get(link).position(position));
}
}
protected class MultiUploadAuthenticator extends
AbstractAuthenticator<NullAuthenticatorConfiguration> implements
Authenticator<NullAuthenticatorConfiguration> {
public MultiUploadAuthenticator(Credential credential,
NullAuthenticatorConfiguration configuration) {
super(credential, configuration);
}
@Override
public void login() throws IOException {
final HTMLPage page = post("http://www.multiupload.com/login")
.parameter("username", credential.getUsername())
.parameter("password", credential.getPassword()).asPage();
if (!page.containsIgnoreCase(credential.getUsername()))
throw new AuthenticationInvalidCredentialException();
}
@Override
public void logout() throws IOException {
post("http://www.multiupload.com/login").parameter("do", "logout")
.request();
// TODO check logout status
}
}
}

View File

@@ -0,0 +1,110 @@
package com.rogiel.httpchannel.service.impl;
import java.util.EnumSet;
import com.rogiel.httpchannel.service.Uploader.DescriptionableUploaderConfiguration;
import com.rogiel.httpchannel.service.Uploader.UploaderConfiguration;
import com.rogiel.httpchannel.service.impl.MultiUploadService.MultiUploadUploader;
/**
* Describes an configuration for an {@link MultiUploadUploader}
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class MultiUploadUploaderConfiguration implements UploaderConfiguration,
DescriptionableUploaderConfiguration {
/**
* The upload description
*/
private String description = DescriptionableUploaderConfiguration.DEFAULT_DESCRIPTION;
/**
* The services in which Multiupload should mirror the uploaded file
*/
private EnumSet<MultiUploadMirrorService> uploadServices = EnumSet
.allOf(MultiUploadMirrorService.class);
/**
* An enumeration containing all supported services for Multiupload
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public enum MultiUploadMirrorService {
MEGAUPLOAD(1), UPLOADKING(16), DEPOSIT_FILES(7), HOTFILE(9), UPLOAD_HERE(
17), ZSHARE(6), FILE_SONIC(15), FILE_SERVE(18), WUPLOAD(19);
/**
* The internal multiupload id
*/
public final int id;
private MultiUploadMirrorService(int id) {
this.id = id;
}
}
@Override
public String description() {
return description;
}
@Override
public MultiUploadUploaderConfiguration description(String description) {
this.description = description;
return this;
}
/**
* Adds this service as an desired mirror
*
* @param service
* the service
*/
public MultiUploadUploaderConfiguration uploadService(
MultiUploadMirrorService... services) {
for (final MultiUploadMirrorService service : services) {
uploadServices.add(service);
}
return this;
}
/**
* Checks if the service is on the desired mirror list
*
* @param service
* the service
* @return <code>true</code> if the service is on the list
*/
public boolean containsUploadService(MultiUploadMirrorService service) {
return uploadServices.contains(service);
}
/**
* Removes this service from the mirror list
*
* @param service
* the service
*/
public MultiUploadUploaderConfiguration removeUploadService(
MultiUploadMirrorService service) {
uploadServices.remove(service);
return this;
}
/**
* Removes all services from the mirror list
*
* @return
*/
public MultiUploadUploaderConfiguration clearUploadServices() {
uploadServices.clear();
return this;
}
/**
* @return the list of services of which MultiUpload should try to make
* mirrors
*/
public EnumSet<MultiUploadMirrorService> uploadServices() {
return uploadServices;
}
}

View File

@@ -0,0 +1 @@
com.rogiel.httpchannel.service.impl.MultiUploadService

View File

@@ -0,0 +1,32 @@
package com.rogiel.httpchannel.service.impl;
import java.net.MalformedURLException;
import java.net.URL;
import com.rogiel.httpchannel.service.AbstractServiceTest;
import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.Service;
public class MultiUploadServiceTest extends AbstractServiceTest {
@Override
protected Service createService() {
return new MultiUploadService();
}
@Override
protected URL createDownloadURL() throws MalformedURLException {
return new URL("http://www.multiupload.com/QPDUXJDZZY");
}
@Override
protected Credential createValidCredential() {
return null;
}
@Override
protected Credential createInvalidCredential() {
return new Credential("invalid-"
+ Double.toString(Math.random() * 1000), Double.toString(Math
.random() * Integer.MAX_VALUE));
}
}

View File

@@ -0,0 +1,3 @@
This is a simple upload test file.
This is for testing purposes only.

View File

@@ -0,0 +1,13 @@
<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>
</project>

View File

@@ -0,0 +1,191 @@
package com.rogiel.httpchannel.service.impl;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AbstractUploader;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability;
import com.rogiel.httpchannel.service.CapabilityMatrix;
import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.ServiceID;
import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.Uploader;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration;
import com.rogiel.httpchannel.service.config.NullUploaderConfiguration;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.util.PatternUtils;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* This service handles uploads to UploadKing.com.
*
* @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*/
public class UploadKingService extends AbstractHttpService implements Service,
UploadService<NullUploaderConfiguration>,
AuthenticationService<NullAuthenticatorConfiguration> {
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("uploadking");
private static final Pattern UPLOAD_URL_PATTERN = Pattern
.compile("http://www([0-9]*)\\.uploadking\\.com/upload/\\?UPLOAD_IDENTIFIER=[0-9]*");
private static final Pattern DOWNLOAD_ID_PATTERN = Pattern
.compile("\"downloadid\":\"([0-9a-zA-Z]*)\"");
private static final String INVALID_LOGIN_STRING = "Incorrect username and/or password. Please try again!";
@Override
public ServiceID getID() {
return SERVICE_ID;
}
@Override
public int getMajorVersion() {
return 1;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize, NullUploaderConfiguration configuration) {
return new UploadKingUploader(filename, filesize, configuration);
}
@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize) {
return getUploader(filename, filesize, newUploaderConfiguration());
}
@Override
public NullUploaderConfiguration newUploaderConfiguration() {
return NullUploaderConfiguration.SHARED_INSTANCE;
}
@Override
public long getMaximumFilesize() {
return 1 * 1024 * 1024 * 1024;
}
@Override
public String[] getSupportedExtensions() {
return null;
}
@Override
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
return new CapabilityMatrix<UploaderCapability>(
UploaderCapability.UNAUTHENTICATED_UPLOAD,
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD,
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential, NullAuthenticatorConfiguration configuration) {
return new UploadKingAuthenticator(credential, configuration);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential) {
return getAuthenticator(credential, newAuthenticatorConfiguration());
}
@Override
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() {
return NullAuthenticatorConfiguration.SHARED_INSTANCE;
}
@Override
public CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability() {
return new CapabilityMatrix<AuthenticatorCapability>();
}
protected class UploadKingUploader extends
AbstractUploader<NullUploaderConfiguration> implements
Uploader<NullUploaderConfiguration>,
LinkedUploadChannelCloseCallback {
private Future<String> uploadFuture;
public UploadKingUploader(String filename, long filesize,
NullUploaderConfiguration configuration) {
super(filename, filesize, configuration);
}
@Override
public UploadChannel openChannel() throws IOException {
final HTMLPage page = get("http://www.uploadking.com/").asPage();
final String userCookie = page.getInputValueById("usercookie");
final String url = page.findFormAction(UPLOAD_URL_PATTERN);
final String uploadID = page.getInputValue("UPLOAD_IDENTIFIER");
final LinkedUploadChannel channel = createLinkedChannel(this);
uploadFuture = multipartPost(url).parameter("file_0", channel)
.parameter("u", userCookie)
.parameter("UPLOAD_IDENTIFIER", uploadID).asStringAsync();
return waitChannelLink(channel, uploadFuture);
}
@Override
public String finish() throws IOException {
try {
final String linkId = PatternUtils.find(DOWNLOAD_ID_PATTERN,
uploadFuture.get(), 1);
if (linkId == null)
return null;
return new StringBuilder("http://www.uploadking.com/").append(
linkId).toString();
} catch (InterruptedException e) {
return null;
} catch (ExecutionException e) {
throw (IOException) e.getCause();
}
}
}
protected class UploadKingAuthenticator extends
AbstractAuthenticator<NullAuthenticatorConfiguration> implements
Authenticator<NullAuthenticatorConfiguration> {
public UploadKingAuthenticator(Credential credential,
NullAuthenticatorConfiguration configuration) {
super(credential, configuration);
}
@Override
public void login() throws IOException {
final HTMLPage page = post("http://www.uploadking.com/login")
.parameter("do", "login")
.parameter("username", credential.getUsername())
.parameter("password", credential.getPassword()).asPage();
if (page.contains(INVALID_LOGIN_STRING))
throw new AuthenticationInvalidCredentialException();
}
@Override
public void logout() throws IOException {
post("http://www.uploadking.com/login").parameter("do", "logout")
.request();
// TODO check logout status
}
}
}

View File

@@ -0,0 +1 @@
com.rogiel.httpchannel.service.impl.MultiUploadService

View File

@@ -0,0 +1,39 @@
package com.rogiel.httpchannel.service.impl;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.util.ChannelUtils;
public class UploadKingServiceTest {
private UploadKingService service;
@Before
public void setUp() throws Exception {
service = new UploadKingService();
}
@Test
public void testNonLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.UNAUTHENTICATED_UPLOAD",
service.getUploadCapabilities().has(
UploaderCapability.UNAUTHENTICATED_UPLOAD));
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final URL url = ChannelUtils.upload(service, path);
Assert.assertNotNull(url);
System.out.println(url);
}
}

View File

@@ -0,0 +1,3 @@
This is a simple upload test file.
This is for testing purposes only.

View File

@@ -13,6 +13,7 @@
<modules>
<module>httpchannel-service-megaupload</module>
<module>httpchannel-service-hotfile</module>
<module>httpchannel-service-multiupload</module>
</modules>
<dependencies>
@@ -27,4 +28,6 @@
<version>1.0.0</version>
</dependency>
</dependencies>
<name>HttpChannel/Service</name>
<description>Parent module that all service implementations should inherit</description>
</project>