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


In reply to Dylan - For the sites they are a-changin' by jplindstrom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.