#!/usr/bin/perl -w
use strict;
use HTML::SimpleLinkExtor;
use LWP::Simple;
use Image::Grab;
#Set $save_dir to the directory path in which you want the files to be saved.
#Note: the directory must exist prior to program execution
print "\n\nEnter the full path to the directory where you want your images saved\n";
my $save_dir = <>;
chomp($save_dir);
my $exclude_pattern = "%";
print "Is the URL a directory path or a path to a webpage.
1) Directory path = \thttp://www.website.com/directory/
2) Webpage path = \thttp://www.website.com/directory/webpage.htm
Select 1 or 2: ";
my $urlType = <>;
print "\nEnter the full URL of the web page or directory that links to the wanted images\n";
my $page_dir = <>;
$page_dir = "http://" . $page_dir if $page_dir !~ /^http:\/\//;
my $link;
my $image;
my $counter = 0;
my $pic = Image::Grab->new();
my $html = get($page_dir);
my $extor = HTML::SimpleLinkExtor->new(search_url=>'$page_dir');
$extor->parse($html);
my @page_links = $extor->links;
$page_dir = parse_url($page_dir) if ($urlType == 2);
chomp($page_dir);
foreach $link (@page_links) {
if ($link =~ /jpg$/) {
next if $link =~ /$exclude_pattern/;
$counter++;
my @temp = split(/\//, $link); #Ensure that only filename and not directory path
my $filename = pop(@temp); #is placed in $filename
@temp = "";
if ($link =~ /^http/) { #If returned link is hard-link use returned link
$pic->url($link);
$pic->grab;
} else { #If returned link is a relative link, construct full path.
print $link . "\n";
$link =~ s/\///;
print $link . "\n";
$pic->url("${page_dir}${link}");
$pic->grab;
}
@temp = check_existance($filename, $save_dir);
$filename = shift @temp;
my $save_dir = shift @temp;
print "$page_dir$link\n";
open(IMAGE, ">${save_dir}${filename}") || die "$!";
print IMAGE $pic->image;
close IMAGE;
print "${counter}: Saved ${save_dir}${filename}\n\n";
}
}
print "Finished!\n";
sub parse_url {
my $page = shift;
my @temp = split(/\//, $page);
pop(@temp);
my $page_dir = join('/', @temp) . "/";
@temp = "";
print "$page_dir\n";
return $page_dir;
}
sub check_existance {
my $filename = shift;
my $save_dir = shift;
if (-e "${save_dir}${filename}") {
print "${save_dir}${filename} exists. Renaming file!\n";
$filename = rand(999) % 999 . $filename;
}
return ($filename, $save_dir)
}