SVN: toys/fun/rsget.pl

sparky sparky at pld-linux.org
Tue Jan 6 23:47:20 CET 2009


Author: sparky
Date: Tue Jan  6 23:47:20 2009
New Revision: 10053

Modified:
   toys/fun/rsget.pl
Log:
- added megaupload, requires perl-GD and gocr for captchas


Modified: toys/fun/rsget.pl
==============================================================================
--- toys/fun/rsget.pl	(original)
+++ toys/fun/rsget.pl	Tue Jan  6 23:47:20 2009
@@ -97,6 +97,16 @@
 
 	{
 		my $fn = $self->{file_name};
+		if ( -r $fn ) {
+			my $ext = "";
+			$ext = $1 if $fn =~ s/(\..{3,5})$//;
+			my $i = 1;
+			while ( -r "$fn-$i$ext" ) {
+				$i++;
+			}
+			$fn .= "-$i$ext";
+			$self->{file_name} = $fn;
+		}
 		my $net = $self->{obj}->{net};
 		$self->{obj}->{netname} = "[$net] $fn: ";
 	}
@@ -217,7 +227,8 @@
 	if ( $obj->{curl_next} ) {
 		my $func = $obj->{curl_next};
 		my $body = $ecurl->{file}
-			? "DONE $ecurl->{file_name} $ecurl->{size_got} / $ecurl->{size_total}"
+			? "DONE $ecurl->{file_name} " . main::bignum( $ecurl->{size_got} )
+				. " / " . main::bignum( $ecurl->{size_total} )
 			: $ecurl->{body};
 		my $eurl = $curl->getinfo( CURLINFO_EFFECTIVE_URL );
 		
@@ -248,7 +259,7 @@
 		my $size_got = $ecurl->{size_got};
 		my $size_total = $ecurl->{size_total};
 
-		my $size = "$size_got / $size_total";
+		my $size = main::bignum( $size_got ) . " / " . main::bignum( $size_total );
 		my $eta = "";
 		my $time_diff = $time - $ecurl->{time_start};
 		if ( $size_total > 0 ) {
@@ -270,7 +281,7 @@
 {
 	my $ecurl = shift;
 
-	my $size_total = $ecurl->{size_total};
+	my $size_total = $ecurl->{size_got};
 
 	my $time_diff = time() - $ecurl->{time_start};
 	$time_diff = 1 unless $time_diff;
@@ -279,7 +290,7 @@
 
 	my @l = localtime;
 	my $date = sprintf "%d-%.2d-%.2d %2d:%.2d:%.2d", $l[5] + 1900, $l[4] + 1, @l[(3,2,1,0)];
-	return "DONE $size_total; ${speed}KB/s $eta @ $date";
+	return "DONE " . main::bignum( $size_total ) . "; ${speed}KB/s $eta @ $date";
 }
 
 
@@ -609,12 +620,12 @@
 sub stage4
 {
 	my ($self, $body, $url) = @_;
-	$self->print("starting............");
+	$self->print("reading captcha");
 
 	my $captcha = Get::NetLoad::Captcha::resolve( $body );
 
 	unless ( defined $captcha ) {
-		return $self->stage1();
+		return $self->curl( $self->{s3dir} . $self->{captcha_img}, \&stage4, cookies => $self->{cookie}, referer => $url );
 	}
 	$self->{captcha} = $captcha;
 
@@ -624,7 +635,7 @@
 sub stage5
 {
 	my $self = shift;
-	$self->print("starting...............");
+	$self->print("starting............");
 
 	my $post = "file_id=$self->{file_id}&captcha_check=$self->{captcha}&start=";
 	$self->curl(
@@ -636,18 +647,28 @@
 sub stage6
 {
 	my ($self, $body, $url) = @_;
-	$self->print("starting..................");
+	$self->print("starting...............");
 
 	if ( $body =~ /You may forgot the security code or it might be wrong/ ) {
 		return $self->stage1();
 	}
-
+	if ( $body =~ /This file is currently unavailable/ ) {
+		return $self->error( "file currently unavailable" );
+	}
 	if ( $body =~ /You could download your next file in.*countdown\(([0-9]+)/ ) {
 		my $wait = $1 / 100;
 		$wait = 1200 if $wait > 1200;
 		return $self->wait( $wait, \&stage1, "free limit reached, waiting" );
 	}
-	$body =~ /please wait .*countdown\(([0-9]+),/;
+	unless ( $body =~ /please wait .*countdown\(([0-9]+),/ ) {
+		if ( $self->{s6problems} ) {
+			return $self->error( "unknown problem" );
+		} else {
+			$self->{s6problems} = 1;
+			return $self->wait( 5 * 60, \&stage1, "unknown problems, retrying in" );
+		}
+	}
+	$self->{s6problems} = 0;
 	my $wait = $1 / 100 + 1;
 
 	my ($full_link) = ($body =~ m#<a class="Orange_Link" href="(.*?)"#);
@@ -741,8 +762,8 @@
 	$nimg->copy( $trimg, 13 + 2*$w, 1, $xmin, $ymin, $w, $h );
 	$nimg->copy( $trimg, 15 + 3*$w, 1, $xmin, $ymin, $w, $h );
 
-	use IPC::Open2;
-	open2( *READ, *WRITE, "pngtopnm | gocr -f ASCII -a 5 -m 56 -C 0123456789 - 2>/dev/null" );
+	require IPC::Open2;
+	IPC::Open2::open2( *READ, *WRITE, "pngtopnm | gocr -f ASCII -a 5 -m 56 -C 0123456789 - 2>/dev/null" );
 	print WRITE $nimg->png;
 	close WRITE;
 	my $num = <READ> || "";
@@ -750,8 +771,7 @@
 
 	my ($gocr) = ($num =~ /^([0-9])/);
 
-	use IPC::Open2;
-	open2( *READ, *WRITE, "pngtopnm | ocrad --filter=numbers_only - 2>/dev/null" );
+	IPC::Open2::open2( *READ, *WRITE, "pngtopnm | ocrad --filter=numbers_only - 2>/dev/null" );
 	print WRITE $nimg->png;
 	close WRITE;
 	$num = <READ> || "";
@@ -889,6 +909,189 @@
 }
 
 # }}}
+package Get::MegaUpload; # {{{
+
+BEGIN {
+	our @ISA;
+	@ISA = qw(Get);
+}
+
+my $mucookie = 0;
+sub new
+{
+	my $proto = shift;
+	my $class = ref $proto || $proto;
+	my $url = shift;
+
+	++$mucookie;
+	my $cookie = ".mu.$nlcookie.txt";
+	unlink $cookie if -e $cookie;
+
+	Get::makenew( "MU", $class, $url, cookie => $cookie );
+}
+
+sub stage1
+{
+	my $self = shift;
+
+	$self->print("starting...");
+	$self->curl( $self->{url}, \&stage2, cookies => $self->{cookie} );
+}
+
+sub stage2
+{
+	my ($self, $body, $url) = @_;
+	$self->print("starting......");
+
+	my %search = (
+		captcha_img => qr#<img src="(/capgen\.php\?[0-9a-f]+)"#,
+		action => qr#<form method="POST" action="(.*?)"#,
+		s2id => qr#<input type="hidden" name="d" value="(.*?)"#,
+		s2icode => qr#<input type="hidden" name="imagecode" value="(.*?)"#,
+		s2mevagar => qr#<input type="hidden" name="megavar" value="(.*?)"#,
+	);
+
+	foreach my $name ( keys %search ) {
+		my $search = $search{$name};
+		if ( $body =~ m/$search/ ) {
+			$self->{$name} = $1;
+		} else {
+			return $self->stage1()
+		}
+	}
+
+	(my $domain = $url) =~ s#^(.*?://.*?)/.*$#$1#; ## vim
+	$self->{s2url} = $url;
+
+	$self->curl( $domain . $self->{captcha_img}, \&stage3, cookies => $self->{cookie}, referer => $url );
+}
+
+sub stage3
+{
+	my ($self, $body, $url) = @_;
+
+	$self->print("reading captcha");
+
+	my $captcha = Get::MegaUpload::Captcha::resolve( $body );
+
+	unless ( defined $captcha ) {
+		return $self->stage1();
+	}
+
+	my $post = "d=$self->{s2id}&imagecode=$self->{s2icode}&megavar=$self->{s2mevagar}&imagestring=$captcha";
+
+	$self->curl( $self->{action}, \&stage4, post => $post, cookies => $self->{cookie}, referer => $url );
+}
+
+sub stage4
+{
+	my ($self, $body, $url) = @_;
+
+	my %search = (
+		s4wait => qr#x[0-9]+=([0-9]+);#,
+		s4g => qr#var . = String\.fromCharCode\(Math.abs\(-?([0-9]+)\)\);#,
+		s4j1 => qr#var . = '(.)' \+ String\.fromCharCode\(Math\.sqrt\([0-9]+\)\);#,
+		s4j2 => qr#var . = '.' \+ String\.fromCharCode\(Math\.sqrt\(([0-9]+)\)\);#,
+		s4href => qr#document\.getElementById\("dlbutton"\)\.innerHTML = '<a href="(.*?)"#,
+	);
+
+	foreach my $name ( keys %search ) {
+		my $search = $search{$name};
+		if ( $body =~ m/$search/ ) {
+			$self->{$name} = $1;
+		} else {
+			return $self->stage1()
+		}
+	}
+
+	my $furl = $self->{s4href};
+
+	my $g = chr $self->{s4g};
+	my $j2 = chr sqrt $self->{s4j2};
+	my $jg = $self->{s4j1} . $j2 . $g;
+
+	$furl =~ s/' \+ . \+ . \+ '/$jg/;
+
+	$self->{file_url} = $furl;
+	$self->{file_referer} = $url;
+
+	$self->wait( $self->{s4wait}, \&stage5, "starting in" );
+}
+
+sub stage5
+{
+	my $self = shift;
+	$self->print("downloading");
+
+	$self->download( referer => $self->{file_referer}, cookies => $self->{cookie} );
+}
+
+sub DESTROY
+{
+	my $self = shift;
+	unlink $self->{cookie};
+}
+
+# }}}
+package Get::MegaUpload::Captcha; # {{{
+
+sub resolve
+{
+	my $capdata = shift;
+	require GD;
+
+	my $img = GD::Image->new( $capdata );
+
+	my @img;
+	foreach ( 0..2 ) {
+		my $img = GD::Image->newPalette( 70, 32 );
+		my $w = $img->colorAllocate( 255, 255, 255 );
+		my $b = $img->colorAllocate( 0, 0, 0 );
+		push @img, { img => $img, w => $w, b => $b };
+	}
+
+	my $bg = $img->getPixel( 0, 0 );
+
+	foreach my $y ( 1..30 ) {
+		foreach my $x ( 1..68 ) {
+			my $ci = $img->getPixel( $x, $y );
+			next if $ci == $bg;
+			my ($r, $g, $b ) = $img->rgb( $ci );
+
+			next if $g < 0x60 or $g > 0x80 or $b < 0x60 or $g > 0x80;
+			if ( $r > 110 and $r < 130 ) {
+				$img[ 0 ]->{img}->setPixel( $x, $y, $img[ 0 ]->{b} );
+			}
+			if ( $r > 160 and $r < 180 ) {
+				$img[ 1 ]->{img}->setPixel( $x, $y, $img[ 1 ]->{b} );
+			}
+			if ( $r > 210 and $r < 230 ) {
+				$img[ 2 ]->{img}->setPixel( $x, $y, $img[ 2 ]->{b} );
+			}
+		}
+	}
+
+	my @l;
+	require IPC::Open2;
+	foreach ( 0..2 ) {
+		my $img = $img[$_]->{img};
+		IPC::Open2::open2( *READ, *WRITE, "pngtopnm | gocr -f ASCII -m 56 -C A-Z - 2>/dev/null" );
+		print WRITE $img->png;
+		close WRITE;
+		my $out = <READ> || "";
+		close READ;
+
+		if ( $out =~ /^([A-Z])/ ) {
+			push @l, $1;
+		} else {
+			return undef;
+		}
+	}
+
+	return join "", @l;
+}
+
+# }}}
 package main;
 
 my $get_list = shift @ARGV || 'get.list';
@@ -916,9 +1119,12 @@
 		} elsif ( $line =~ m#^\s*(http://netload\.in/datei.*?)\s*$# ) { #){# vimbug
 			$url = $1;
 			new Get::NetLoad( $1 );
-		} elsif ( $line =~ m#^\s*(http://odsiebie.com/(pokaz|pobierz)/.*?)\s*$# ) { #){# vimbug
+		} elsif ( $line =~ m#^\s*(http://odsiebie\.com/(pokaz|pobierz)/.*?)\s*$# ) { #){# vimbug
 			$url = $1;
 			new Get::OdSiebie( $1 );
+		} elsif ( $line =~ m#^\s*(http://(www\.)?megaupload\.com/\?d=.*?)\s*$# ) { #){# vimbug
+			$url = $1;
+			new Get::MegaUpload( $1 );
 		}
 
 		if ( $url ) {
@@ -974,4 +1180,11 @@
 	return sprintf '%d:%.2d', $minutes, $seconds;
 }
 
+sub bignum($)
+{
+	local $_ = shift;
+	s/(...?)(?=(...)+$)/$1_/g;
+	return $_;
+}
+
 # vim:ts=4:sw=4:fdm=marker


More information about the pld-cvs-commit mailing list