#!/usr/bin/perl

name: RapidShark
short: RK
uri: qr{rapidshark\.pl/}
status: OK 2009-09-22

start:
	GET( $-{_uri} );

	ERROR( "file not found" )
		if m{<b>(Plik nie został odnaleziony|File Not Found|Datei nicht gefunden)</b>};
	ERROR( "file not found" )
		if m{<font class="err">No such file};

	m{<font style="font-size:12px;">Chcesz pobrac plik <font color="red">.*/(.*?)</font> \(($STDSIZE)\)</font>};
	INFO( name => $1, asize => $2 );

	! s/^.*?<Form method="POST" action=''>//s;
	! s/(.*?)<\/Form>.*$/$1/s;

	my %opts;
	$opts{$1} = $2 while s/<input\s+type="hidden"\s+name="(.*?)"\s+value="(.*?)">//;

	! m{<input type="submit" name="method_free" value="(.*?)">};
	$opts{method_free} = $1;

	GET( "", post => \%opts );
	$-{dl_page} = $-{_referer};

	if ( /(?:You have to wait|Proszę czekać) (.*) (?:till next download|aby ściągnąć kolejny plik)/ ) {
		$_ = $1;
		my $wait = 0;
		$wait += 60 * 60 * $1 if /(\d+) hour/;
		$wait += 60 * $1 if /(\d+) minute/;
		$wait += $1 if /(\d+) second/;
		RESTART( $wait, "free limit reached" );
	}

	! s/^.*?<form name="F1" method="post" action=""//si;
	! s/(.*?)<\/form>.*$/$1/si;

	my %opts;
	$opts{$1} = $2 while s/<input name="(.*?)" value="(.*?)" type="hidden">//s;
	$opts{$1} = $2 while s/<input type="hidden" name="(.*?)" value="(.*?)">//s;

	#! m{<input.*?id="btn_download" value="(.*?)">};
	#$opts{btn_download} = $1;

	$-{post} = \%opts;

	! m{<span id="countdown">(\d+)</span>};
	$-{wait} = $1;

	! m{<img src="(http://www\.rapidshark\.pl/captchas/.*?jpg)">};
	my $img = $1;

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

	CAPTCHA( "image/jpeg" );
	$-{post}->{code} = $_;

	my $wait = $-{wait} - (time - $-{cap_start});
	$wait = 1 if $wait < 1;

	WAIT( $wait, "starting download" );
	
	DOWNLOAD( "", post => $-{post} );

# vim:ts=4:sw=4
