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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.