# $Id: GigaSize 11716 2010-07-27 20:18:28Z sparky $
# Get::GigaSize - 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: GigaSize
short: GS
web: "http://gigasize.com/"
tos: "http://www.gigasize.com/page.php?p=terms"
uri: qr{gigasize\.com/get\.php\?d=}
cookie: gs
status: OK 2010-07-14

start:
	GET( $-{_uri} );

	ERROR( "file not found" ) if /The file\s+has been deleted/;

	! m{<strong>Name</strong>:\s*<b>(.*?)</b></p>\s*<p>Size:\s*<span>($STDSIZE)</span>}o;
	my ( $name, $size ) = ( $1, $2 );
	$name =~ s/\.\.\.$/\0/;
	INFO( iname => $name, asize => $size );

stage_captcha:
	! $-{form} = $self->form( match => { action => qr{^/formdownload\.php$} } );

stage_getimage:
	GET( "/randomImage.php", keep_referer => 1 );

	CAPTCHA(
		qr/[A-Z]{3}/,
		process => \&gs_decaptcha
	);
	GOTO stage_getimage unless $_;

	$-{form}->set( txtNumber => $_ );
	GET( $-{form}->post() );

	if ( $-{_referer} =~ /err=invalidchecksum$/ ) {
		CAPTCHA_RESULT( "FAIL" );
		GOTO stage_captcha;
	}
	RESTART( - irand( 60, 300 ), "free limit reached, waiting time unknown" )
		if /YOU HAVE REACHED YOUR FREE DOWNLOAD LIMIT/;

	! $-{form} = $self->form( id => "formDownload" );

	CAPTCHA_RESULT( "OK" );

	! m{var seconds=(\d+)};
	WAIT( $1, "starting download" );
	
	CLICK_DOWNLOAD( $-{form}->post() );

perl:

sub gs_color_select
{
	my @s = sort { $a <=> $b } @_;
	return $s[2];
}

sub gs_decaptcha
{
	my $img = shift;
	$img->color_filter( \&gs_color_select );
	$img = $img->crop( x1 => 15, y1 => 3, x2 => 42, y2 => 16 );
	$img->luma_emphasize( 10, 50 );
	return $img->doublesize->ocr();
}


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