|
Posted by Leonard Tulipan on November 16, 2004, 4:46 pm
Please log in for more thread options
This is a multi-part message in MIME format.
--------------010809050908090205000906
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi there!
I am a newbie when it comes to posting to newsgroups, so please be gentle!
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
iD8DBQFBmhK2UiMT8G8GLvwRAkDfAKCZl/vtkw842N0Lt8sEoeTZUBGdoACfcRHM
Ko/hOHs/kfSvMo7hlXo9g1U=
=OxHI
-----END PGP SIGNATURE-----
--------------010809050908090205000906
Content-Type: text/plain;
name="progress_broken_sample.pl"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="progress_broken_sample.pl"
#!/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) ." Trailerpagesn" if
$debug;
foreach (@trailerpages)
{
printf "Getting: %sn", $_ 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 existsn" 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 createdn";
}
close (MOVFILE);
}# $mov != 0
}
--------------010809050908090205000906--
|
| Similar Threads | Posted | | Term::Clui in modlist/User_Interfaces/Term ? | October 5, 2007, 1:52 am |
| Question about Term::ANSIMenu | November 2, 2004, 2:55 am |
| Can't install Term::ReadKey | December 16, 2004, 3:47 pm |
| Term::ReadLine and ActivePerl | June 9, 2006, 5:01 pm |
| Term::ReadKey for VMS platform | July 25, 2008, 11:45 am |
| ANNOUNCE: Zoidberg 0.90 && Term::ReadLine::Zoid 0.05 | September 6, 2004, 7:03 pm |
| Term-Size make fails on debian | September 6, 2005, 4:28 pm |
| Term::ReadLine - Terminal does not support AddHistory | June 14, 2005, 12:54 pm |
| Term::Cap -- Setting terminal type under Windows XP | May 24, 2006, 1:46 pm |
| Term::Cap -- Setting terminal type under Windows XP | May 29, 2006, 9:22 pm |
|