Automatically refreshing election results in San Francisco

I’m at a post-campaign party right now, and we’re all huddled around the computer watching the San Francisco election results. At first, the common injunction to whoever was sitting at the computer was to “hit refresh” to see if new results were posted. I quickly tired of hitting refresh, so I cooked up a small CGI script to fetch the results, and embedded them in a page to automatically update.

The CGI is pretty simple. The nice thing about the San Francisco results is that they’re plain text embedded in a pair of <pre> tags, so all the CGI has to do is grab the text between the tags:
#!/usr/bin/perl -w

use CGI;
use LWP::Simple;
use Time::Piece;

# Get results from between

 and 

my $sf_results_html = get ‘http://www.sfgov.org/site/election_index.asp?id=47578’;

my @htmlbeforepre = split ( ‘

', $sf_results_html );
my @htmlafterpre = split ( '

‘, $htmlbeforepre[1] );
my $sf_results_txt = $htmlafterpre[0];

# write up page, with date
my $page = new CGI;
print $page->header;
print $page->start_html(‘SF Election results’);

my $t = localtime;
print “Date retrieved by CGI = $t\n”;

print “\n

\n";
print $sf_results_txt;
print "\n

\n”;

print $page->end_html;

Then, to automatically refresh it, I embedded in an server-side include that refreshed every three seconds. (Though, on further reflection, perhaps I should have set the timeout to be longer.)




Auto Refreshing election results




Page generated on



Leave a Reply