# $Id: BadongoCom 11520 2010-06-04 16:01:05Z sparky $
# Get::BadongoCom - File, audio, video and picture 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: BadongoCom
short: BC
web: "http://www.badongo.com/"
uri: qr{badongo\.com/}
cookie: !badongo
status: BROKEN 2010-06-04

unify:
	s/#.*//; s{/$}{};
	my $host = "http://www.badongo.com";
	return "$host/file/$1" if m{/c?file/(\d+(/.*)?)};
	return "$host/vid/$1/1" if m{/c?vid/(\d+)};
	return "$host/pic/$1" if m{/pic/(\d+)};
	return "$host/file/$1" if m{/audio/(\d+)};

start:
	COOKIE( [ (qw(.badongo.com TRUE / FALSE), time + 86400, 'badongoL', 'en' ) ] );

	GET( $-{_uri} );

	GOTO stage_getlink if $-{_referer} =~ m{/c(?:file|vid)/};

	ERROR( "file not found" ) if /<div id="fileError">/;

	if ( not /This file has been split/ or $-{_referer} =~ m{/\d+/(.+)$} ) {
		my $part = $1;
		! m{<div class="finfo">(.*?)</div>};
		my $name = $1;
		! m{<div class="ffileinfo">Views : \d+ \| Filesize : ($STDSIZE)</div>};
		my $size = $1;
		if ( $part ) {
			INFO( name => "$name.$part", quality => "part of $1" );
		} else {
			INFO( name => $name, asize => $1 );
		}
	}

	GOTO stage_getpic if $-{_referer} =~ m{/pic/};

stage_getcaptcha:
	my $rsrnd = jstime();
	$-{_referer} =~ m{.*/(.*?)$};
	GET( "$1?rs=displayCaptcha&rst=&rsrnd=$rsrnd&rsargs[]=yellow",
		keep_referer => 1 );

	! m{<img src=\\"(/ccaptcha.*?)\\"};
	my $img = $1;

	! s/^.*?<form/<form/s;
	! s{</form>.*$}{</form>}s;
	s/\\n/ /g;
	s/\\"/"/g;

	! $-{form} = $self->form( source => $_ );

	GET( $img, keep_referer => 1 );

	CAPTCHA(
		qr/[A-Z]{4}/,
		#	process => \&bc_decaptcha
	);

	GOTO stage_getcaptcha unless defined $_;

	$-{form}->set( user_code => lc $_ );
	GET( $-{form}->post() );

	if ( $-{_referer} =~ m{/c(?:file|vid)/} ) {
		CAPTCHA_RESULT( "OK" );
	} else {
		CAPTCHA_RESULT( "FAIL" );
		GOTO stage_getcaptcha;
	}

	if ( my @links = m{<a href="(http://www\.badongo\.com/file/\d+/..)">}g ) {
		LINK( @links );
	}

stage_getlink:
	! /var check_n = (\d+);/;
	WAIT( $1, "getting download link" );

	my $linkType = "getFileLink";
	$linkType = "getVidLink" if $-{_referer} =~ m{/cvid/};
	my $rsrnd = jstime();
	$-{_referer} =~ m{.*/(.*?)$};
	GET( "$1?rs=$linkType&rst=&rsrnd=$rsrnd&rsargs[]=yellow" );

	! m{doDownload\(\\'(.*?)\\'\)};
	$-{dllink} = $1;

	CLICK( $-{dllink} . "/ifr?pr=1&zenc=" );

	$-{file_uri} = $-{dllink} . "/loc?pr=1";
	GOTO stage_download;

stage_getpic:
	! m{/pic/(\d+)};
	GET( "$1?size=original" );

	! m{<img src="(http://mediacache.*)" border="0">};
	$-{file_uri} = $1;
 
stage_download:
	DOWNLOAD( $-{file_uri} );

	MULTI() if m{<h3>FREE MEMBER WAITING PERIOD</h3>};
	RESTART( - irand( 60, 300 ), "free limit reached" )
		if /You have exceeded your Download Quota/;


perl:

sub bc_color_select
{
	my @s = sort { $a <=> $b } @_;
	my $diff = $s[2] - $s[0];
	return $diff >= 64 ? 0xff : $s[2] + $diff * 2;
}

sub bc_decaptcha
{
	my $img = shift;

	$img->color_filter( \&bc_color_select );
	$img = $img->crop( y1 => 10, y2 => 49 );
	my @text;
	foreach my $limg ( $img->chop( 50, 100, 150 ) ) {
		my %try;
		for ( my $min = 0; $min <= 200; $min += 100 ) {
			for ( my $max = $min + 200; $max <= 200; $max += 100 ) {
				my $copy = $limg->crop;
				$copy->luma_emphasize( $min, $max );

				my $txt = $copy->ocr();
				if ( $txt =~ /([A-Z])/ ) {
					$try{ $1 } ||= 0;
					$try{ $1 }++;
				}
			}
		}
		return "" unless keys %try;
		my @s = sort { $try{ $b } <=> $try{ $a } } keys %try;
		push @text, $s[ 0 ];
	}
	return join "", @text;
}


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