Public feed #723642

bg.pl

public text v1 · immutable
Raw Edit
·published 2007-10-02 22:38 UTC
rendered paste body
#!/usr/bin/perl
use Term::ProgressBar;
use URI;
use LWP::UserAgent;
use LWP::Simple;
use Cwd;
use strict;
use warnings;
use File::stat;
bgnets();

#download bg zone
open(NE, "<process") or die("Cannot open process for reading: $!");
open(E, ">bg_nets.tmp") or die("Cannot open bg_nets.tmp for writing: $!");
while (<NE>)
{
if (m/(^[\d].*)\s/)
{
print E "$1\n";
}
}
close(E) or die("Cannot close file : $!");
close(NE) or die("Cannot close file : $!");
unlink ("process");
my $tmp="bg_nets.txt";
if (!(-e $tmp)){
	open(E, ">bg_nets.txt") or die("Cannot create bg_nets.txt: $!");
	close(E) or die("Cannot close file : $!");
}
undef $tmp;
#check for updates
my $filenew = stat("bg_nets.tmp")->size;
my $fileold = stat("bg_nets.txt")->size;
if ($filenew>$fileold)
{
	unlink ("bg_nets.txt");
	rename ("bg_nets.tmp","bg_nets.txt");
}else{
unlink ("bg_nets.tmp");
}





sub bgnets{
	print `clear`;
	print  "Downloading bg_nets.txt, please wait ......\n";
	my $ua = LWP::UserAgent->new;

	my $url = 'http://ip.ludost.net/cgi/process?country=1&country_list=bg&format_template=prefix&format_name=&format_target=&format_default=';

	my $uri = URI->new($url);
	my $path = $uri->path;
	$path =~ s{.*/}{};
	$path = "download" unless length $path;
	$path = "X$path" while -e $path;
	
	open my $outhandle, ">", $path or die "Cannot create $path: $!";

	my $bar = Term::ProgressBar->new({ name => 'Download',
			count => 1024,
			ETA => 'linear'});

	my $output = 0;
	my $target_is_set = 0;
	my $next_so_far = 0;
	$ua->get
	($url,
		":content_cb" => sub {
			my ($chunk, $response, $protocol) = @_;

			unless ($target_is_set) {
				if (my $cl = $response->content_length) {
					$bar->target($cl);
					$target_is_set = 1;
				} else {
					$bar->target($output + 2 * length $chunk);
				}
			}

			$output += length $chunk;
			print {$outhandle} $chunk;

			if ($output >= $next_so_far) {
				$next_so_far = $bar->update($output);
			}
		});

	$bar->target($output);
	$bar->update($output);
}