Re: (like) wget in gtk2::progressbar



On Mon, 8 Sep 2008 18:16:25 +0200
anguila <anguila gmail com> wrote:

I want to download a file (via http) and see this progress in a a
gtk2::progressbar. Until now, i used wget, because it have progress bar,
download ratio, filesize, etc...
How can I download a file and get data to put in progressbar widget? Can I
use wget? I should us another module? Which?

I search into the net, and I see something like this, but the command was
cp, and it use strace, i think there must be a better way to do that...

Thanks,
David

You could continue to use wget, open a piped-open command, and regex
it's output for the percentage done, but LWP is probably easier. LWP has
a callback that allows you to intercept the chunks downloaded.

Here is a super simple example. A problem with this script, is it will
tend to concentrate on the download, so if you want this part of a bigger
script that you want to keep running, while the download occurs, you need to
put the LWP code into a separate thread or fork it off.

But here are the basics:

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect( delete_event => sub { Gtk2->main_quit; } );
$window->set_border_width(10);

my $vbox = Gtk2::VBox->new( 0, 5 );

my $button = Gtk2::Button->new("Get File");

$button->signal_connect( clicked => \&getfile );

$vbox->pack_start( $button, TRUE, TRUE, 0 );

my $progress = new Gtk2::ProgressBar;
my $fraction = 0.0;
$progress->set_fraction($fraction);
$vbox->pack_start( $progress, TRUE, TRUE, 0 );

$window->add($vbox);
$window->show_all;
Gtk2->main;

#####################################################
sub getfile {
 #from lwpcook
 use LWP::UserAgent;
 my $ua = LWP::UserAgent->new;
 my $URL = 'http://zentara.net/zentara1.avi';
 my $filename = substr( $URL, rindex( $URL, "/" ) + 1 );
 #print "$filename\n";
 open( IN, ">$filename" ) or die $!;

 my $expected_length;
 my $bytes_received = 0;
 my $fraction = 0;
 my $res = $ua->request(HTTP::Request->new(GET => $URL),
    sub {
        my ( $chunk, $res ) = @_;
        $bytes_received += length($chunk);
        unless ( defined $expected_length ) {
            $expected_length = $res->content_length || 0;
        }
        
        if ($expected_length) {
            $fraction = $bytes_received / $expected_length;
            printf STDERR "%d%% - ", 100 * $fraction;
        
        }
            print STDERR "$bytes_received bytes received\n";
               
           #print "$fraction\n";    
           $progress->set_fraction($fraction);
           my $text = sprintf('%.2d', $fraction * 100) . '%';
           $progress->set_text($text);
           #update progressbar    
           Gtk2->main_iteration while Gtk2->events_pending;

        # XXX Should really do something with the chunk itself
        print IN $chunk;
    }
);

print $res->status_line, "\n";
close IN;
}
__END__


zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]