# $Id: HotFile 10867 2009-10-26 22:47:10Z sparky $
# Get::HotFile - File getter plugin for rsget.pl
#
# 2009 (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
uri: qr{hotfile\.com/}
status: OK 2009-10-22

start:
	GET( $-{_uri} );

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

	! m{<table class="downloading"><tr><td>.*?<b>(.*?)</b> <span class="size">\| ($STDSIZE)</span></td></tr></table>};
	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, "starting download" );

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

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

	! $-{capform} = $self->form( match => { body => qr/Enter word above/ } );

	! m#<img src="(/captcha\.php.*?)">#;
	GET( $1, keep_referer => 1 );

	CAPTCHA(
		qr/[a-zA-Z]{5,6}/,
		process => \&hf_decaptcha
	);

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

	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 );

perl:

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

sub hf_decaptcha
{
	my $img = shift;
	$img->color_filter( \&hf_color_select_lowest );
	$img->luma_emphasize( 90, 130 );
	return $img->ocr();
}

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