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);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.