I used that script to check images submitted to our site's search engine. The search engine looks like the following: a description of some site and a thumbnail of it. And this script checks for broken images in the lest and marks them as down. That way they won't be displayed on the page and our customers will see text-only descriptions. The result of the following script is a file with SQL commands to run in order to update the MySQL table. This script uses parallel fetching of images (HEAD requests from HTTP protocol actually). And it's based on the discussion that occured not a long time ago at perlmonks.com. Thanks anyone who helped me. I am glad to present you a very tiny script I made based on knowledge of people at perlmonks.com.
LWP::Parallel Doesn't Work For Me is a link to the thread I mentioned.
#!/usr/bin/perl
use strict;
use DBI;
use LWP::Simple;
use Parallel::ForkManager;
my $dbh=DBI->connect("dbi:mysql:shsearch","","", {RaiseError=>1});
my $sth=$dbh->prepare("select siteid, imageurl from big_ass_table grou
+p by siteid, imageurl");
$sth->execute();
my ($siteid, $imageurl, $type, $size, $flag, %sites, $count, $counter)
+;
$count=0;
$sth->bind_columns(\($siteid, $imageurl));
while($sth->fetch()) {
$sites{$siteid}=$imageurl;
$count++;
}
$dbh->disconnect();
my $pm=new Parallel::ForkManager(30);
print "Ready to fetch headers ($count to process)...\n";
open(FILE, ">sql.torun") || die "Cannot open file for writting: $!";
print FILE "update big_ass_table set imagedown=0;\n";
$counter=0;
foreach $siteid (sort{$a <=> $b} keys %sites) {
$imageurl=$sites{$siteid};
$counter++;
$pm->start and next;
print "($counter of $count) Checking $imageurl for site $siteid...";
if(($type, $size)=(head($imageurl))[0,1]) {
if($size < 25600) {
if($type =~ /image\/(gif|jpeg)/) {
print "OK ($size bytes, $type)\n";
$flag=1;
} else {
$flag=0;
print "Wrong file type ($type, must be image/gif or image/jpeg)\n"
+;
}
} else {
$flag=0;
print "Image size exceeded ($size bytes, should be < 25600)\n";
}
} else {
print "Error\n";
$flag=0;
}
if($flag==0) {
print FILE "update big_ass_table set imagedown=imagedown+1 where sit
+eid=$siteid and imageurl='$imageurl';\n";
} else {
print FILE "update big_ass_table set imagedown=0 where siteid=$sitei
+d and imageurl='$imageurl';\n";
}
$pm->finish();
}
$pm->wait_all_children();
close(FILE);