Using bash to check a web server for PDF changes

Keeping up with elected official changes is a pain in the ass.

For a while I checked for changes when I thought of it, which caused the occasional histrionics. Then I switched to using the Google Civics API, which I figured was going to keep me from worrying about it. As it turns out, the Google Civics API is both (a) incomplete and often (b) inaccurate. For Mayor of Charlotte it lists the former mayor who was indicted and everybody in Charlotte wants desperately to forget about it. Hissy fits galore.

To top it off, our local Board of Elections stores that information in a PDF. Yay.

So I’m heading back off the Google Civics API again. This time I’m going to be a bit smarter about it however. Since I got all cloudy I have a real Linux server with real bash ready to do real stuff. This bash script uses curl to check the modified date on their PDF file from their web server against a date stored in a file. If the PDF was modified after that date, it downloads the file, emails it to me, and resets the check date in the file.

checkdate.txt
1
06-Jun-14
check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash

#get pdf if it was updated after the date in checkdate.txt
curl -z $(<checkdate.txt) http://charmeck.org/mecklenburg/county/BOE/elections/Docume
nts/POD%20Alpha.pdf -o officials.pdf

# if file exists send file as attachment to email, put new date in datestamp file
if [[ -f officials.pdf ]]; then
# overwrite date
date +"%d-%b-%y" > checkdate.txt

# send email
to='your@emailaddress.com'
(
# generate email headers and begin of the body asspecified by HERE document
cat - <<END
Subject: Elected officials updated!
From: no-reply@server.org
To: $to
Content-Type: text/plain

The elected officials have been updated.

If you don't change the elected officials in GeoPortal soon there could be bloodshed.

END
# generate/append uuencoded attachments
uuencode officials.pdf officials.pdf

) | /usr/sbin/sendmail -i -- $to

#remove pdf
rm officials.pdf
fi

cron it up for once a day, Bob’s your uncle. One hissy fit down, a few hundred to go.