# $Id: HotFile 11716 2010-07-27 20:18:28Z sparky $
# Get::HotFile - File getter plugin for rsget.pl
#
# 2009-2010 (c) Przemysław Iskra <sparky@pld-linux.org>
#		This program is free software,
# you may distribute it under GPL v2 or newer.

name: HotFile
short: HF
web: "http://hotfile.com/"
tos: "http://hotfile.com/terms-of-service.html"
uri: qr{hotfile\.com/dl/\d+/[0-9a-f]+/.+}
uri: qr{pl\.hotfile\.com/dl/\d+/[0-9a-f]+/.+}
uri: qr{new\.hotfile\.com/dl/\d+/[0-9a-f]+/.+}
status: OK 2010-07-23

unify:
	return "http://hotfile.com/dl/$1"
		if m{hotfile\.com/dl/(\d+/[0-9a-f]+/.+?)(?:\.html)?$};

start:
	GET( $-{_uri} . "?lang=en" );

	ERROR( "file not found" ) unless length $_;
	ERROR( "file not found" ) if /This file is either removed|File is removed/;

	! m{<strong>Downloading:</strong> (.*?) <span>\|</span> <strong>($STDSIZE)</strong>}o;
	INFO( name => $1, asize => $2 );

	MULTI() if /You are currently downloading/;

	! /starthtimer.*?timerend=d\.getTime\(\)\+(\d+);/s;
	RESTART( $1 / 1000, "free limit reached" ) if $1 > 0;

	! /starttimer.*?timerend=d\.getTime\(\)\+(\d+);/s;
	my $wait = $1 / 1000;

	! $-{form} = $self->form( name => "f" );

	WAIT( $wait + irand( 2, 10 ), "expecting captcha" );

stage_getcaptcha:
	CLICK( $-{form}->post() );

	GOTO stage_download if m#<a href="(.*?)">Click here to download</a>#;

	! $-{capform} = $self->form( match => { body => qr/recaptcha\.net/ } );

	! m{ src="(http://api\.recaptcha\.net/challenge.*)"};

	GET( $1, keep_referer => 1 );

	! m{server\s*:\s*'(\S+?)'};
	my $server = $1;
	
	! m{challenge\s*:\s*'(\S+?)',};
	$-{capform}->set( recaptcha_challenge_field => $1 );

	GET( $server . "image?c=" . $1, keep_referer => 1 );

	CAPTCHA(
		qr/\S+ \S+/
	);

	GOTO stage_getcaptcha unless defined $_;
	$-{capform}->set( recaptcha_response_field => $_ );

	GET( $-{capform}->post() );
stage_download:

	if ( m#<a href="(.*?)">Click here to download</a># ) {
		CAPTCHA_RESULT( "OK" );
	} else {
		CAPTCHA_RESULT( "FAIL" );
		GOTO stage_getcaptcha;
	}

	CLICK_DOWNLOAD( $1 );

# vim: filetype=perl:ts=4:sw=4
