SVN: toys/fun/rsget.pl

sparky sparky at pld-linux.org
Sun Dec 28 23:45:46 CET 2008


Author: sparky
Date: Sun Dec 28 23:45:46 2008
New Revision: 10043

Modified:
   toys/fun/rsget.pl
Log:
- make sure file is closed after finishing download


Modified: toys/fun/rsget.pl
==============================================================================
--- toys/fun/rsget.pl	(original)
+++ toys/fun/rsget.pl	Sun Dec 28 23:45:46 2008
@@ -20,9 +20,7 @@
 	my ($chunk, $self) = @_;
 
 	unless ( exists $self->{total} ) {
-		my $info = $self->{curl}->getinfo(
-			CURLINFO_CONTENT_LENGTH_DOWNLOAD
-		);
+		my $info = $self->{curl}->getinfo( CURLINFO_CONTENT_LENGTH_DOWNLOAD );
 		$self->{total} = $info || '?';
 		$self->{start} = time;
 	}
@@ -65,30 +63,35 @@
 
 	my $curl = new WWW::Curl::Easy;
 
-	$curl->setopt(CURLOPT_WRITEHEADER, \*NULL);
-	$curl->setopt(CURLOPT_MAXREDIRS, 10);
-	$curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
-	$curl->setopt(CURLOPT_HTTPHEADER, $curl_headers);
-	$curl->setopt(CURLOPT_URL, $url);
-	$curl->setopt(CURLOPT_REFERER, $opts{referer})
+	$curl->setopt( CURLOPT_WRITEHEADER, \*NULL);
+	$curl->setopt( CURLOPT_MAXREDIRS, 10);
+	$curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
+	$curl->setopt( CURLOPT_HTTPHEADER, $curl_headers);
+	$curl->setopt( CURLOPT_URL, $url);
+	$curl->setopt( CURLOPT_REFERER, $opts{referer})
 		if $opts{referer};
-	$curl->setopt(CURLOPT_ENCODING, 'gzip,deflate');
+	$curl->setopt( CURLOPT_ENCODING, 'gzip,deflate');
 
 	if ( $opts{post} ) {
 		my $post = $opts{post};
-		$curl->setopt(CURLOPT_POST, 1 );
-		$curl->setopt(CURLOPT_POSTFIELDS, $post );
+		$curl->setopt( CURLOPT_POST, 1 );
+		$curl->setopt( CURLOPT_POSTFIELDS, $post );
 	}
 
 	my $body;
 	if ( $opts{file} ) {
-		$curl->setopt(CURLOPT_WRITEFUNCTION, \&body_file);
+		$curl->setopt( CURLOPT_WRITEFUNCTION, \&body_file);
+		$body = {
+			curl => $curl,
+			got => 0,
+			fn => ($opts{net} || "").$opts{file}
+		};
 		open my $f_out, '>', $opts{file};
-		$curl->setopt(CURLOPT_FILE, { curl => $curl, got => 0, file => $f_out,
-				fn => ($opts{net} || "").$opts{file} });
+		$body->{file} = $f_out;
+		$curl->setopt( CURLOPT_FILE, $body );
 	} else {
-		$curl->setopt(CURLOPT_WRITEFUNCTION, \&body_scalar);
-		$curl->setopt(CURLOPT_FILE, \$body);
+		$curl->setopt( CURLOPT_WRITEFUNCTION, \&body_scalar);
+		$curl->setopt( CURLOPT_FILE, \$body);
 	}
 
 	if ( $curl->perform != 0 ) {
@@ -98,9 +101,9 @@
 	}
 
 	if ( $opts{file} ) {
-		return $curl->getinfo(
-			CURLINFO_CONTENT_LENGTH_DOWNLOAD
-		);
+		close $body->{file};
+		delete $body->{curl}; # remove circular dep
+		return $curl->getinfo( CURLINFO_CONTENT_LENGTH_DOWNLOAD );
 	}
 	return $body;
 }
@@ -119,9 +122,7 @@
 
 	print "\r[RS] $fn: (re)starting...         ";
 	my $body = curl( $file );
-	unless ( $body ) {
-		goto rsget_restart;
-	}
+	goto rsget_restart unless $body;
 	if ( $body =~ /The file could not be found\.  Please check the download link\./m ) {
 		print "\r[RS] $fn: file not found    \n";
 		return "file not found";
@@ -132,9 +133,7 @@
 	sleep 1 + rand 5;
 
 	$body = curl( $link, post => 'dl.start=Free' );
-	unless ( $body ) {
-		goto rsget_restart;
-	}
+	goto rsget_restart unless $body;
 	if ( $body =~ /Please wait until the download is completed/m ) {
 		die "Multi-download not allowed\n";
 	}
@@ -145,7 +144,7 @@
 			print "\r[RS] $fn: waiting $i minutes ";
 			sleep 60;
 		}
-		sleep 20;
+		sleep 2;
 		goto rsget_restart;
 	}
 	$body =~ /var c=([0-9]+);/m;
@@ -156,10 +155,14 @@
 	$body =~ /form name="dlf" action="(.*?)"/m;
 	$link = $1;
 
-	curl( $link, post => 'mirror=on', file => $fn, net => '[RS] ' );
-	print "DONE!\n";
-
-	return "DONE";
+	my $size = curl( $link, post => 'mirror=on', file => $fn, net => '[RS] ' );
+	if ( $size ) {
+		print "DONE!\n";
+		return "DONE($size)";
+	} else {
+		print "There've been some problem\n";
+		return "problems";
+	}
 }
 
 my $get_list = shift @ARGV || 'get.list';
@@ -174,18 +177,26 @@
 	my $get_func = undef;
 	open my $list, '<', $get_list;
 	while ( my $line = <$list> ) {
-		if ( $get_func or $line =~ /^\s*(#.*)?$/ ) {
+		if ( $line =~ /^\s*(#.*)?$/ ) {
 			push @newlist, $line;
 			next;
 		}
+		my $url = undef;
+		my $func = undef;
 		if ( $line =~ m/^\s*(http:\/\/rapidshare\.com\/.*?)\s*$/ ) {
-			$get_url = $1;
-			if ( exists $gotlist{$get_url} ) {
-				chomp $line;
-				push @newlist, "# " . $line . " " . $gotlist{$get_url} . "\n";
+			$url = $1;
+			$func = \&rsget;
+		}
+
+		if ( $url ) {
+			if ( exists $gotlist{$url} ) {
+				push @newlist, "# " . $gotlist{$url} . ": " . $line;
 			} else {
-				$get_func = \&rsget;
-				push @newlist, $get_url . "\n";
+				push @newlist, $url . "\n";
+				if ( not $get_func ) {
+					$get_url = $url;
+					$get_func = $func;
+				}
 			}
 			next;
 		}


More information about the pld-cvs-commit mailing list