#!/usr/bin/perl -w use strict; use warnings; use LWP::UserAgent; use LWP::Simple; use WWW::Mechanize; use Digest::MD5 qw( md5_hex ); # Coded by Brendan Galvin from June 3rd 2013, to June 5th 2013. # This product is open-source freeware, and credit to the original source must be given to Brendan Galvin upon re-distribution of the original source or any program, script or application made using the original source. # http://www.linkedin.com/pub/brendan-galvin/26/267/94b my $flag=0; print"\n\nURL for mass-downloading (only download links using the tag, not images or other embedded elements): "; chomp(my $url = ); print"\nExtensions to download (seperated by comma's): "; chomp(my $extensions = ); $extensions =~ s/[.]//g; print"\nLocation to store downloaded files: "; chomp(my $location = ); print"\nHow many downloads would you like to skip starting from the first (in case you started this download earlier and have already downloaded some of the files)? "; chomp(my $skips = ); print"\nAre you going to want to skip any files while the program is running (y/n)?"; chomp(my $skiporno = ); my $error = ""; my @extension = split(',', $extensions); my %extens = map{$_ => 1} @extension; sub GetFileSize{ my $url=shift; my $ua = new LWP::UserAgent; $ua->agent("Mozilla/5.0"); my $req = new HTTP::Request 'HEAD' => $url; $req->header('Accept' => 'text/html'); my $res = $ua->request($req); if ($res->is_success) { my $headers = $res->headers; return $headers; }else{ $flag = 1; $error .= "Error retrieving file information at $url "; } return 0; } my $mech = WWW::Mechanize->new(); $mech->get($url); my $base = $mech->base; my @links = $mech->links(); for my $link ( @links ) { my $skip = 'n'; if($link->url() =~ m/([^.]+)$/){ my $ext = ($link->url() =~ m/([^.]+)$/)[0]; if(exists($extens{$ext})){ my $newurl = $link->url(); if($newurl !~ /http::\/\/$/ig){ my $baseurl = URI->new_abs($newurl, $base); $newurl = $baseurl; } my $filename = $newurl; $filename =~ m/.*\/(.*)$/; $filename = $1; if($skips > 0){ $skips -= 1; print "\n\nSkipped $filename at " . $link->url(); next; }else{ my $header = GetFileSize($newurl); my $urlmech = WWW::Mechanize->new(); $urlmech->show_progress("true"); print"\n\n\n$filename at $newurl\n"; print "File size: ".$header->content_length." bytes\n" unless $flag==1; print "Last modified: ".localtime($header->last_modified)."\n" unless $flag==1; if($skiporno eq 'y'){ print"Skip file (y/n)?"; chomp($skip = ); } if($skip ne 'y'){ print " downloading...\n"; my $response = $urlmech->get($newurl, ':content_file' => "$filename", )->decoded_content; }else{ print"\nSkipping...\n\n"; next or print"Error skipping.\n"; } } } } } print"\n\n\nTasks completed.\n"; if($error ne ""){ print"\nErrors: $error"; }else{ print"No errors.\n"; }