# $Id: FreakShare 11781 2010-08-15 22:16:47Z pawelz $
# Get::FreakShare - 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: FreakShare
short: FS
web: "http://freakshare.net/"
tos: "http://freakshare.net/terms-of-service.html"
uri: qr{freakshare\.net/files/}
cookie: !fs
status: OK 2010-08-16

start:
	GET( $-{_uri} );

	GET( "/index.php?language=EN" );

	RESTART( - irand( 60, 300 ), "free limit reached" )
		if /Your Traffic is used up for today/;
	ERROR( "file not found: $1" )
		if m#<h1 .*?>Error</h1>\s+<div.*?>\s*(.*?)\s+<#s;

	! m#<h1 class="box_heading" style="text-align:center;">(.*?) - ($STDSIZE)yte</h1>#o;
	INFO( name => $1, asize => $2 );

	! $-{form} = $self->form( match => { body => qr/"Free Download"/ } );

	! m#var time = (\d+)(?:\.\d+);\s*var intervall;#;
	WAIT( -$1, "expecting recaptcha" );

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

stage_solvecaptcha:
	unless ( $-{capform} = $self->form( match => { body => qr/recaptcha\.net/ } ) ) {
		! $-{capform} = $self->form( match => { onsubmit => qr/^disableButton/ } );
		GOTO stage_download;
	}

	! 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 => $_ );

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

	if ( m/recaptcha/ or /^bad try$/ ) {
		CAPTCHA_RESULT( "FAIL" );
		GOTO stage_solvecaptcha;
	}

	MULTI() if /Sorry, you cant download more then 1 files at time/;

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