#!/usr/bin/perl

name: HotFile
short: HF
uri: qr{hotfile\.com/}
status: OK 2009-08-29

start:
	GET( $-{_uri} );

	ERROR( "file not found" ) unless length $_;
	ERROR( "file not found" ) if /This file is either 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;

	! s/^.*?<form style=".*?" action="(.*?)" method=post name=f>//s;
	$-{action} = $1;
	! s#^(.*?)</form>.*#$1#s;
	my %post;
	$post{$1} = $2 while s/<input type=hidden name=(.*?) value=(.*?)>//;
	$-{post} = \%post;

	WAIT( $wait, "starting download" );

	GET( $-{action}, post => $-{post} );
	$-{dl_page} = $-{_referer};

	if ( m#<a href="(.*?)">Click here to download</a># ) {
		return $self->stage_download();
	}

	! s#^.*<form action="(.*?)" method="post">##s;
	$-{form_uri} = $1;

	my %post = ( action => "checkcaptcha" );
	! m#<input type=hidden name=captchaid value=(\d+)>#;
	$post{captchaid} = $1;
	! m#<input type=hidden name=hash1 value=(\S+?)>#;
	$post{hash1} = $1;
	! m#<input type=hidden name=hash2 value=(\S+?)>#;
	$post{hash2} = $1;

	$-{post} = \%post;

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

	GET( $img );
	$-{_referer} = $-{dl_page};

	CAPTCHA( "image/jpeg" );

	$-{post}->{captcha} = $_;

	GET( $-{form_uri}, post => $-{post} );
stage_download:

	RESTART( 0, "invalid captcha ?" ) unless m#<a href="(.*?)">Click here to download</a>#;

	DOWNLOAD( $1 );

# vim:ts=4:sw=4
