Term::ProgressBar problems with multiple calls

L

Leonard Tulipan

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi there!

I am a newbie when it comes to posting to newsgroups, so please be
gentle! I posted this too c.l.p.modules, but maybe I just cannot write
perl-code so it fits in here.
Attached is a small sample script. In line 27 I build an array with
two urls.
I want to download those in a loop. This is what I get

$ ./progress_broken_sample.pl
Getting Trailers from 2 Trailerpages
Getting: http://nds2.photos.nokia.com/press/photo/phones/zip/6260_06.zip
Downloading 6260_06.zip
Answer: ""
~ ( 0.85MB )...
~ 0%
[=




]ETA ------
~ 1%
[=




]0m00s Left
........................ (more of that same till:) ....................
100%
[==========================================================================]--
DONE --
(nothing to do)
Getting: http://nds2.photos.nokia.com/press/photo/phones/zip/6260_02.zip
Downloading 6260_02.zip
Answer: ""
~ ( 1.58MB )...
(nothing to
do)




]
100%
[===========================================================================]--
DONE --
(nothing to do)

Now, the download seems to work, but the progress-bar does not
initialize correctly.
What I normally do with this script is download movie trailers from
apple http://sourceforge.net/projects/downlets/
So for now I always need to call the script like this:

for i in
http://www.apple.com/trailers/newline/monster_in_law/mil_apple_large.html
http://www.apple.com/trailers/fineline/the_sea_inside/large.html
~ do
~ ./downlets/downlet_apple.pl -u $i
done

But that way the whole automatic I built into the script is useless.
Can anybody help me. I probably did something terribly wrong when
calling term::progress. I tried looking in the code of the module
itself, but I didn't find, where the progress is saved. Maybe it needs
to be reset somehow, before reusing it in the loop..

Cheers
Leonard

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBm2ZVUiMT8G8GLvwRAlNcAKDKnvXNwidrcauv4SoDM6pteij7vQCg532X
TQggb7JhnQa5AshtVcv1j1g=
=BTnL
-----END PGP SIGNATURE-----

#!/usr/bin/perl
use strict;

my $debug=1;
my $ask;

# For immediate output of progress bar
$|++;

# LWP UserAgent is used for downloading the HTML Files from the web
# make sure we have the modules we need, else die peacefully.
# perl -e "use CPAN; install Bundle::LWP;"
eval("use LWP 5.6.9;"); die "[err] LWP 5.6.9 or greater required.\n" if $@;

# Progress Bar
eval("use Term::progressBar;"); # prevent word-wrapping.
die "[err] Term::progressBar not installed. Use \"perl -e \"use CPAN; install Term::progressBar;\"\" to install \n" if $@;

use File::Basename;

# Start a new instance from UserAgent
my $ua = LWP::UserAgent->new;
# Set User Agent to iTunes
$ua->agent('User-Agent: iTunes/4.6 (Macintosh; U; PPC Mac OS X 10.3)');

# We save the trailerpages ../large.html or the like in this array
my @trailerpages = ( "http://nds2.photos.nokia.com/press/photo/phones/zip/6260_06.zip", "http://nds2.photos.nokia.com/press/photo/phones/zip/6260_02.zip");

print "Getting Trailers from ". ($#trailerpages + 1) ." Trailerpages\n" if $debug;

foreach (@trailerpages)
{
printf "Getting: %s\n", $_ if $debug;

my $url = $_;

my $response;
my $result;
my $remote_headers;
my $total_size;
my $progress;
my $final_data_length;
my $next_update = 0; # reduce ProgressBar use.

if( -e basename($url)) {
print "Trailer ". substr(basename($url), 0, 40) ." already exists\n" if $debug;
} else {
print "Downloading ", substr(basename($url), 0, 40);
my $answer = "";
if($ask) {
print " OK? (Y/n): ";
$answer = <STDIN>;
chomp($answer);
}
print "\nAnswer: \"$answer\"\n" if $debug;

open (MOVFILE, "> " . basename($url)) || die $!;
if(! ($answer =~ /^n/i) ) {
# create a new useragent and download the actual URL.
# all the data gets thrown into $final_data, which
# the callback subroutine appends to.
#my $ua = LWP::UserAgent->new( );
$result = $ua->head($url);
$remote_headers = $result->headers;
$total_size = $remote_headers->content_length;
printf( " ( %.2fMB )... \n", $total_size/1024/1024 );
# initialize our progress bar.
$final_data_length = undef;
$next_update = 0;
$progress = Term::progressBar->new({count => $total_size, ETA => 'linear', fh => \*STDOUT});
$progress->target($total_size);
$progress->minor(0); # turns off the floating asterisks.
$progress->max_update_rate(1); # only relevant when ETA is used.
$progress->update(0);
$response = $ua->get($url, ':content_cb' => \&callback, );
# top off the progress bar.
$progress->update($total_size);

# per chunk.
sub callback {
my ($data, $response, $protocol) = @_;
print MOVFILE $data;
$final_data_length += length($data);
#print "\n" . $response. " " . length($data) . "\n";
# reduce usage, as per example 3 in POD.
$next_update = $progress->update($final_data_length)
if $final_data_length >= $next_update;
}

$progress->target(0);
$progress->update(0);
#$progress = '';

} else {
print "Empty File created\n";
}
close (MOVFILE);

}# $mov != 0

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top