Renew All

Created 2006-06-27 / Edited 2006-11-19

Tags: Code, Perl, Library, Scrape

I just got a late notice from the Phoenix Public Library. Being overdue on my library books, movies, and anything similar is a long family tradition handed down from generation to generation. One of the only things that could keep Mom from taking us to the library is not being able to find enough spare change to pay for the fines.

Let us then overcome our nature through the use of technology. The library has an online renewal tool. Therefore... cpan:WWW::Mechanize to the rescue!

#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;

my $card = 'XXX'; # Your real 14-digit card number goes here!
my $lastname = 'wilcox';

my $agent = WWW::Mechanize->new( autocheck => 1 );
$agent->get('http://www.phoenixpubliclibrary.org/login.jsp');
$agent->form_number(3);
$agent->current_form->value('libcard', $card);
$agent->current_form->value('lastname', $lastname);
$agent->submit();
$agent->submit();
$agent->follow(qr((?-xism:Renew\sAll)));
print $agent->content;
$agent->follow(qr((?-xism:Exit)));

Stick that in your pipe and smoke it. Or maybe stick it in your cron job.

I actually used cpan:WWW::Mechanize::Shell to generate this, which I highly recommend. This solution isn't robust at all though... I don't check to make sure that any of my web requests actually worked or that they are going to the correct place. That's left as an excercise for the reader :)