# $Id: MegaShares 10867 2009-10-26 22:47:10Z sparky $
# Get::MegaShares - 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: MegaShares
short: MS
uri: qr{d01\.megashares\.com/(\?d01=|dl/)}
cookie: !ms
status: OK 2009-10-24

unify:
	return "http://d01.megashares.com/?d01=$1" if m#(?:\?d01=|/dl/)([0-9a-f]{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>};
	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{<dt>Filename:&nbsp;<strong>(.*?)</strong>&nbsp;&nbsp;&nbsp;size: ($STDSIZE)(?:ytes)?</dt>};
	my ( $name, $size ) = ( $1, $2 );
	$name =~ s/\.\.\.$/\0/;
	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="(.*?)">Click here to download</a>};
		$-{file_uri} = $1;

		m{You have ([\d\.]+ .*?) left that you can download with this passport.};
		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
