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:
# Get results from between
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 );
my $sf_results_txt = $htmlafterpre;
# write up page, with date
my $page = new CGI;
print $page->start_html(‘SF Election results’);
my $t = localtime;
print “Date retrieved by CGI = $t\n”;
\n"; print $sf_results_txt; print "\n
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.)
Page generated on