in reply to Capture wget progress bar

wget's progress bar looks like it prints return chars (\r)instead of newlines. Maybe try changing the default input record separator ($/) to something else.

Replies are listed 'Best First'.
Re: Re: Capture wget progress bar
by rendler (Pilgrim) on Mar 16, 2003 at 00:54 UTC
    Someone on Perl beginners suggested that it might be writing directly to the console and therefore not using an IO stream.
    Someone else also suggested using the logging facilty of wget, and I came up with this.
    sub wget { my $file = shift; $SIG{INT} = sub { unlink $log_f; exit }; if (my $pid = fork) { system "wget -o $log_f --progress=bar:force -c $file"; } else { die "cannot fork: $!" unless defined $pid; } sleep 1 until -f $log_f; open LOG, $log_f or die "Couldn't open '$log_f': $!\n"; my ($pos, $length, $status); while (1) { for ($pos = tell LOG; $_ = <LOG>; $pos = tell LOG) { s/^\s+//; if (/^Length: ([\d,]+)/) { print "Downloading: $file [$1] bytes.\n"; } elsif (/^\d+%/) { print "$_\r"; $status = 'downloading'; } elsif (defined $status eq 'downloading' and !/^\d+%/) { unlink $log_f; print "\n"; last; } } sleep 1; seek LOG, $pos, 0; } }
    Of course the error handling for it like that is not too great.