# $Id: MegaShares 11716 2010-07-27 20:18:28Z sparky $
# Get::MegaShares - 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: MegaShares
short: MS
web: "http://megashares.com/"
tos: "http://megashares.com/tos.php"
uri: qr{d01\.megashares\.com/((index\.php)?\?d01=|dl/)}
cookie: !ms
status: OK 2010-07-13

unify:
	return "http://d01.megashares.com/?d01=" . lc $1 if m#(?:\?d01=|/dl/)([0-9A-Za-z]{7})#;

start:
	# create fake cookie to skip first page
	my $stime = time() - 10000;
	COOKIE( [ (qw(.megashares.com TRUE / FALSE), $stime + 43200, 'freest', $stime . "%3A" ) ] );

	GET( $-{_uri} );

	ERROR( "temporarily unavailable" )
		if m{<dd class="red">([^<]|<br>)*(link is currently offline)([^<]|<br>)*</dd>};
	my $fnf = join "|",
		"Link was removed", "Invalid link", "Could not download file";
	ERROR( "file not found" )
		if m{<dd class="red">([^<]|<br>)*($fnf)([^<]|<br>)*</dd>}o;
	ERROR( "file not found" )
		if m{<dd class="red">Link has been deleted};
	MULTI()
		if m{You already have the maximum of 1 download running};

	RESTART( 600, "servers overloaded" )
		if m{All download slots for this link are currently filled};

	! m{<!-- download page link title //-->\s*<h1.*>(.+?)</h1>}s;
	my $name = $1;
	if ( $name =~ s/\.\.\.$// ) {
		if ( m{/(\Q$name\E.*?)["']} ) {
			$name = $1;
		} else {
			$name .= "\0";
		}
	}
	! m{<strong><span.*>Filesize:</span></strong> ($STDSIZE)<br />}so;
	my $size = $1;
	INFO( iname => $name, asize => $size );

	RESTART( $1 * 60 + $2, "free limit reached" )
		if m{Your download passport will renew\s*in 00:<strong>(\d+)</strong>:<strong>(\d+)</strong>};

	unless( m{<dt>Your Passport needs to be reactivated.</dt>} ) {
		! m{.*<a href="(.+?)">\s*<img.*src="images/download_file\.png" alt="download file"}s;
		$-{file_uri} = $1;

		m{You have\s+<.*?>\s+([\d\.]+ \S+?)\s+<.*?>\s+left that you can download with this passport.}s;
		my $left = $1;
		m{Your download passport will renew in\s*<strong>0</strong>:<strong>(\d+)</strong>:<strong>(\d+)</strong>}s;
		my $time = s2string( $1 * 60 + $2 );
		LOG( "Passport: $left, $time" );

		GOTO stage_download;
	}

	! m{var request_uri = "(.*?)";};
	$-{ajax} = $1 . "&rs=check_passport_renewal";

	! m{<input type="hidden" name="random_num" id="random_num" value="(\d+)" />};
	$-{random_num} = $1;

	! m{<input type="hidden" name="passport_num" id="passport_num" value="([0-9a-f]+)" />};
	$-{passport_num} = $1;

	! m{<img src="(index.*?)" alt="Security Code"};
	GET( $1, keep_referer => 1 );

	CAPTCHA(
		qr/[0-9]{4}/,
		process => \&ms_decaptcha
	);

	RESTART( 1, "Can't read captcha" ) unless defined $_;

	my $captcha = $_;
	my $rand = jstime();
	my $ajax = $-{ajax}
		. (join "", map { '&rsargs[]=' . $_ }
			( $captcha, $-{random_num}, $-{passport_num}, "replace_sec_pprenewal" ) )
		. "&rsrnd=$rand";

	GET( $ajax, keep_referer => 1 );

	if ( /Thank you for reactivating your passport/ ) {
		CAPTCHA_RESULT( "OK" );
		RESTART( 1, "Passport reactivated" );
	} else {
		CAPTCHA_RESULT( "FAIL" );
		RESTART( 1, "Wrong captcha ?" );
	}

stage_download:

	CLICK_DOWNLOAD( $-{file_uri} );

perl:

sub ms_decaptcha
{
	my $img = shift;

	$img->color_filter( sub { shift } );
	$img->luma_emphasize( 50, 51 );
	$img = $img->crop( x1 => 9, x2 => 45, y1 => 4, y2 => 15 );
	return $img->doublesize->ocr();
}

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