use strict; use warnings; use WWW::Mechanize; use LWP::Simple; use Data::Dumper; use Time::HiRes qw( usleep tv_interval gettimeofday ); $|++; my $url = shift || die "no url given!\n"; my $ext = shift || 'mp3'; #die "no file extention given!\n"; my $mech = WWW::Mechanize->new(agent=>"Mozilla/5.0"); my $ua = LWP::UserAgent->new; $mech->get($url); $ua->timeout(5); my $links = $mech->links; no warnings; for my $link ( @{$links} ) { my $url = $link->url_abs; my $res = $ua->head($url); my $http_res = HTTP::Response->new($res); my $abs_name = $http_res ->{'_rc'}->{'_previous'}->{'_header +s'}->{'location'}; my ($rel_name) = $abs_name =~ /.+\/(.+)$/; my $local_name = $rel_name; if( $rel_name =~ /\Q$ext\E$/i ){ $rel_name =~ s/\%\d+/ /g; $rel_name =~ s/(.{35}).+/$1.../; print(pack('A45', "[$rel_name]"), " is being download +ed ... "); my $s_time = q{}; my $e_time = q{}; my $flag = []; for( 1..5 ){ local $SIG{ALRM} = sub { die("timeout") }; eval { alarm(2); $flag = [head($abs_name)]; alarm(0); }; next if $@ =~ m|timeout|; last if $@ !~ m|timeout|; } if( $flag->[0] ){ $s_time = [gettimeofday]; getstore($abs_name, $local_name); $e_time = tv_interval ($s_time, [gettimeofday] +); } $e_time = $e_time ? $e_time . " seconds" : 'less than + 1/100 second'; print "timeout failure in $e_time seconds :(\n" and ne +xt if $@; print "done in $e_time\n"; } }

Update: for example - download.pl 'http://10.10.0.100/show_file.php?id=F-S26-10410041' mp3 # or jpeg, or whatever


Replies are listed 'Best First'.
Re: downloader (fighting with missing urls)
by Aristotle (Chancellor) on Jul 27, 2006 at 12:30 UTC

    Might be useful if you at least summarised the point of your code in a few words beyond your non-descript title. At the very, very least, a usage message and a CPAN-style one-liner about its purpose would be much better than figuratively saying “just read the sourcecode, won’t ya?”

    Does this do anything that wget doesn’t? (Specifically with the --tries and --timeout switches.)

    Makeshifts last the longest.

    A reply falls below the community's threshold of quality. You may see it by logging in.