Having started to follow a few blogs, I needed a reminder of when they change. RSS? I don't know, and I don't know how it works. And web services, like http://www.changedetection.com/, doing this don't check regularly enough.
So I wrote a script, obviously; Dylan - For the sites they are a-changin'
Note 1: It's Windows-biased in the way it starts the web browser when a page changes, but the action can easily be replaced with, say, sending a mail.
Note 2: Why doesn't HEAD work? Why doesn't the headers contain the document's change time? I was under the impression that it should.
#!/usr/local/bin/perl -w
use strict;
use LWP::Simple qw(head get);
use MD5;
print q{Dylan v0.0.1, 2003-03-24, johanl@bossmedia.se
For the sites they are a-changin'
};
main();
sub main {
my $ext = ".snapshot";
my $daysLifetime = 14;
my $secondsDelay = 60 * 10;
my $commandShow = q{START %s};
my @aUrl = (
"http://www.sidhe.org/~dan/blog/",
);
while(1) {
print "\nChecking urls:\n" . join("\n", map { " $_" } @aUrl)
+. "\n";
for my $url( aChangedUrls(\@aUrl, $ext, $daysLifetime) ) {
my $command = sprintf($commandShow, $url);
system($command) and warn("Could not show url ($url)\n");
}
print "\nZZzzzzz for ($secondsDelay) seconds\n";
sleep($secondsDelay);
}
}
sub aChangedUrls {
my ($raUrl, $ext, $daysLifetime) = @_;
my $isFirstTime = ! glob("*$ext"); #No files == first time
my @aChanged;
for my $url (@$raUrl) {
my $text = get($url) or warn("Could not fetch ($url)\n"), next
+;
my $fileMd5 = MD5->hexhash($text) . $ext;
if(! -r $fileMd5) {
open(my $fh, "> $fileMd5") or warn("Could not create ($fil
+eMd5)\n"), next;
close($fh);
push(@aChanged, $url);
}
}
@aChanged = () if($isFirstTime); #It didn't "change" if it
+was the first time
#Clean up old stuff
unlink($_) for ( grep { -M > $daysLifetime} glob("*$ext"));
return(@aChanged);
}
__END__
http://www.bahnhof.se/~johanl/perl/Dylan/dylan.pl.txt