Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

URLs' Checking (Search Engines)

by nikos (Scribe)
on Jan 31, 2002 at 19:22 UTC ( [id://142489]=CUFP: print w/replies, xml ) Need Help??

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);

Replies are listed 'Best First'.
Re: URLs' Checking (Search Engines)
by CharlesClarkson (Curate) on Feb 01, 2002 at 13:42 UTC

    It might be faster and more accurate if $count was derived from keys %sites instead of incrementing it. This is especially true if some $siteid is not unique.

    $count=0; $sth->bind_columns(\($siteid, $imageurl)); while($sth->fetch()) { $sites{$siteid}=$imageurl; $count++; }
    Would become:
    $sth->bind_columns(\($siteid, $imageurl)); while($sth->fetch()) { $sites{$siteid} = $imageurl; } $count = keys %sites;

    $flag, $type, and $size don't have to be file scoped. If you reverse the logic of $flag and change its name to $error, the foreach block becomes easier to read.

    my $error = 1; if ( my($type, $size) = ( head($imageurl) )[0, 1] ) { if ($size < 25600) { if($type =~ /image\/(gif|jpeg)/) { print "OK ($size bytes, $type)\n"; $error = 0; } else { print "Wrong file type ($type, must be image/gif or image/ +jpeg)\n"; } } else { print "Image size exceeded ($size bytes, should be < 25600)\n" +; } } else { print "Error\n"; } if ( $error ) { print FILE "update big_ass_table set imagedown=imagedown+1 where s +iteid=$siteid and imageurl='$imageurl';\n"; } else { print FILE "update big_ass_table set imagedown=0 where siteid=$sit +eid and imageurl='$imageurl';\n"; }



    HTH,
    Charles K. Clarkson
    Clarkson Energy Homes, Inc.
    254 968-8328
      That's true. Thanks for your suggestions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://142489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-28 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found