# $Id: FileFactory 12180 2011-03-03 01:04:58Z sparky $
# Get::FileFactory - 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: FileFactory
short: FF
web: "http://filefactory.com/"
tos: "http://filefactory.com/legal/terms.php"
uri: qr{filefactory\.com/file/[0-9a-z]{6}}
status: OK 2010-07-12

start:
	GET( $-{_uri} );

	RESTART( -2 * 60, "no slots for free users" )
		if /there are currently no free download slots available/;
	DELAY( 2 * 60, "server overloaded" )
		if /All of the available .*download slots on this server are in use/;
	ERROR( "file not found" ) if /File Not Found|This file has been deleted/;
	! m{<title>(.*?) - download now for free};
	my $name = $1;
	! m{<div id="info" class="metadata">\s*<span>($STDSIZE) file uploaded}so;
	my $size = $1;
	INFO( name => $name, asize => $size );
	
	MULTI() if /You are currently downloading/;
	if ( /starthtimer[\s\S]*timerend=d\.getTime\(\)\+(\d+);/m ) {
		RESTART( $1 / 1000, "free limit reached" ) if $1 > 0;
	}

	$-{post} = {
		recaptcha_shortencode_field => "undefined",
	};
	! m#(check):'([0-9a-f]{32})'#;
	$-{post}->{$1} = $2;

	! m#Recaptcha\.create\("([a-zA-Z0-9]{40})"#;
	CLICK( "http://www.google.com/recaptcha/api/challenge?k=$1&ajax=1&cachestop=" . rand, keep_referer => 1 );

	! m{challenge : '(\S+?)',};
	$-{post}->{recaptcha_challenge_field} = $1;
	GET( "http://www.google.com/recaptcha/api/image?c=$1", keep_referer => 1 );

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

	RESTART( 0, "Captcha not solved" ) unless $_;

	$-{post}->{recaptcha_response_field} = $_;
	GET( "http://www.filefactory.com/file/checkCaptcha.php", post => $-{post}, keep_referer => 1 );

	if ( /{status:"fail",message:"Entered code was incorrect\."}/ ) {
		CAPTCHA_RESULT( "FAIL" );
		RESTART( 0, "Captcha failed" );
	} else {
		CAPTCHA_RESULT( "OK" );
	}

	! m#{status:"ok",path:"(.*?)"}#;
	GET( $1 );

	! m#<a href="(\S+)" id="downloadLinkTarget">#;
	$-{file_uri} = $1;

	! m#<input type="hidden" id="startWait" value="(\d+)"#;
	WAIT( -$1, "starting download" );

	CLICK_DOWNLOAD( $-{file_uri} );

	# file turned out to be html, means we have to wait
	MULTI() if /You are currently downloading too many files at once/;
	RESTART( $1 * 60 - 30, "free limit reached" )
		if /Please wait (\d+) minutes to download more files/;
	RESTART( $1, "free limit reached" )
		if /Please wait (\d+) seconds to download more files/;

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