Gossamer Forum
Home : General : Perl Programming :

LWP Help

Quote Reply
LWP Help
Hi, I am having a little problem with LWP. This script checks links in a mysql database. Everything works ok, but as I was testing I noticed an issue. When it checks a url that no longer exists, but the server it formerly resided on has custom 404 error pages, the link checks as being valid. I guess what I want to do is only consider the url valid if it returns a 200. How can I modify the script to work like this?

#!/usr/bin/perl

use LWP::UserAgent;
use DBI;

$db_database = "db";
$db_uid = "user";
$db_pwd = "pass";
($ua = LWP::UserAgent->new)->timeout(20); #actually set timeout


$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to db\n");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;

$i = 0;
$works = 0;
$notworks = 0;
print "\n\n";
#while (my $url = $sth->fetchrow_array) {
while (defined(my $url = $sth->fetchrow_array)) {
if(($ua->request(HTTP::Request->new('HEAD', $url)))->is_success()) {
$validity = "link works";
$valid_update = $dbh->do("UPDATE files SET valid = 1 WHERE url = '$url'");
++$works
}
else {
$validity = "link sucks";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
++$notworks;
}
++$i;
print "$i of $numrows\n$validity\n$url\n\n";
}
$sth->finish;

Quote Reply
Re: LWP Help In reply to
I found a solution. I just needed to change the conditions of my if/else test to:

if (($ua->request(HTTP::Request->new('HEAD', $url)))->code() == 200) {