# $Id: NetLoad 11802 2010-09-13 16:57:03Z sparky $
# Get::NetLoad - 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: NetLoad
short: NL
web: "http://netload.in/"
tos: "http://netload.in/index.php?id=13"
uri: qr{netload\.in/datei[a-zA-Z0-9]+}
cookie: nl
status: OK 2010-07-13

unify:
	return "http://netload.in/$1.htm"
		if m#/(datei[a-zA-Z0-9]+)[\./]#;

start:
	GET( $-{_uri} );
stage_first:

	ERROR( "file not found" )
		if /(Sorry, we don't host the requested file|unknown_file_data)/;
	ERROR( "file not found or something" )
		if />Code: ER_NFF_/;

	m#<div class="dl_first_filename">\s*(.+?)<span.*?>, ($STDSIZE)</span></div>#so;
	INFO( name => $1, asize => $2 );

	RESTART( 60, "Not prepared" )
		if /We will prepare your download/;

	! /href="(.*?captcha=1)"/;
	CLICK( de_ml( $1 ) );

	RESTART( 1, "Still on the same page ?" ) if /"(.*?captcha=1)"/;

	! m#please wait .*countdown\(([0-9]+),#;
	$-{s3wait} = $1;

	! $-{capform} = $self->form( match => { body => qr/Please enter the Securitycode/ } );

	! m#"(share/includes/captcha\.php\?t=[0-9]+)"#;
	$-{captcha_img} = $1;

stage_getcaptcha:
	GET( $-{captcha_img}, keep_referer => 1 );

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

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

	WAIT( $-{s3wait} / 100, "checking" );

	GET( $-{capform}->post() );

	if ( /You may forgot the security code or it might be wrong/ ) {
		PRINT( "Captcha failed" );
		CAPTCHA_RESULT( "FAIL" );
		GOTO stage_first;
	}
	ERROR( "file not found" )
		if /This file is currently unavailable/;
	DELAY( 600, "file temporarily unavailable" )
		if /Please retry again in a few minutes while the file is being uploaded/;
	RESTART( $1 / 100, "free limit reached" )
		if /You could download your next file in.*countdown\(([0-9]+)/;

	! /<a class="Orange_Link" href="(.*?)"/;
	$-{file_uri} = $1;

	CAPTCHA_RESULT( "OK" );

	! /please wait .*countdown\(([0-9]+),/;
	WAIT( $1 / 100, "starting download" );

	CLICK_DOWNLOAD( $-{file_uri} );

perl:

sub nl_color_select_grey
{
	my @s = sort { $a <=> $b } @_;
	return ( $s[2] - $s[0] > 50 ) ? 0xff : $s[0];
}

sub nl_filter_spots # remove dark pixels
{
	my $pix = shift;

	my $lim = 250;
	return if $pix->isAbove( $lim );

	my $whites = 0;
	my @sides = ( $pix->up, $pix->down, $pix->left, $pix->right );
	foreach my $spix ( @sides ) {
		return unless $spix; # borders are taken care of
		$whites++ if $spix->isAbove( $lim );
	}
	return if $whites <= 2;
	if ( $whites == 4 ) {
		$pix->set( 0xff );
		return;
	}

	# possible double spot
	my $bpix;
	foreach my $spix ( @sides ) {
		unless ( $spix->isAbove( $lim ) ) {
			$bpix = $spix;
			last;
		}
	}

	$whites = 0;
	my $sides = 0;
	@sides = ( $bpix->up, $bpix->down, $bpix->left, $bpix->right );
	foreach my $spix ( @sides ) {
		next unless $spix;
		$sides++;
		$whites++ if $spix->isAbove( $lim );
	}

	if ( $whites >= $sides - 1 ) {
		# it is a double spot
		$pix->set( 0xff );
		$bpix->set( 0xff );
	}
}

sub nl_filter_snow # remove light pixels
{
	my $pix = shift;

	my $lim = 10;
	return if $pix->isBelow( $lim );

	my $black = 0;
	my @sides = ( $pix->up, $pix->down, $pix->left, $pix->right );
	foreach my $i ( (0..3) ) {
		my $pix = $sides[ $i ];
		next unless $pix;
		$black |= 1 << $i if $pix->isBelow( $lim );
	}
	if ( ($black & 0x03) == 0x03
			or ($black & 0x0c) == 0x0c ) {
		$pix->set( 0 );
	}
}

sub nl_decaptcha
{
	my $img = shift;
	$img->color_filter( \&nl_color_select_grey );
	$img->set_border( 0xff );
	$img->luma_emphasize( 180, 256 );
	$img->pix_filter( \&nl_filter_spots );
	$img->pix_filter( \&nl_filter_snow );
	# TODO: chop to pieces and scan each digit separately
	local $_ = $img->doublesize->ocr();
	s/\s+//;
	return $_;
}

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