Categories
Random

Jury Duty Status Page Monitor

I was on call for jury duty last week.

I wrote this little Perl script to check the jury status web page for my county and shoot me an email when it was updated. This script isn’t all that clean (it doesn’t f_lock the checksum file among other things), but it was a quick and dirty script that helped me get work done during the day where I was otherwise constantly checking the web site for updates (the county asks you to keep checking back).

This could be adapted to monitor any web page and look for changes. There are utilities that do this of course (Firefox plugins, for example) but it’s a good to be able to throw something like this together in a pinch.

Things in ALL CAPS have been removed for security, those would need to be replaced with The Real Things for this to work.

#! /usr/bin/perl

# Checks the jury page and sends an alert if the page has changed since the last run.
# This is pretty hacked, it doesn't check to see if the checksum file exists first,
# and it doesn't exit cleanly unless everything works perfectly, and it doesn't
# flock the file(s) anywhere so don't run multiple instances of it.
#
# Run it as a cron job.

use LWP::Simple;

my $url = 'FULL_URL_OF JURY_STATUS_PAGE';

$content = get $url;

my $checksumfile = "/tmp/jury.tmp";

# First, compare the checksum file to the content:
{
local $/ = undef;
open CHECKSUMFILE, "< $checksumfile";
$checksumfile_slurp = ;
close CHECKSUMFILE;
}

if ($content ne $checksumfile_slurp) {
        print "Page Changed. Sending alert...\n";
        &alert;
        print "Alert sent.\n";
        &save_new_checksumfile;
        print "New checksum saved.\n";
}

exit;

#####################################

sub save_new_checksumfile() {
 # Not bothering to flock here...
 open CHECKSUMFILE, "> $checksumfile";
 print CHECKSUMFILE $content;
 close CHECKSUMFILE;
}

sub alert {
        open(SENDMAIL, "|/usr/sbin/sendmail -oi -t -fEMAIL_FROM -odb");
        print SENDMAIL <<"EOF";
From: root\@MY_DOMAIN
To: EMAIL_TO
Subject: Jury Page Updated

The jury page has been changed.

$url

EOF
        close SENDMAIL;
}