Perl Script to put Weather on your Polycom Microbrowser

 

Getting started

Here’s a quick little perl script to create a static html/xml file that can be displayed on your Polycom’s microbrowser. The script uses the Google weather API to check the weather based on a given zip code.

The install should be pretty quick. Ok, next for the perl script (weather.pl):

#!/usr/bin/perl -w
use strict;
use XML::TreePP;
use File::Basename;

#my $ZipCode = $ARGV[0];
my $ZipCode = 55438;
my $picPath = "/polyApps/pics/";


if ($ZipCode =~ /^(\d{5})$/) {
	$ZipCode = $1;
	}
else {
	print qq(ERROR: invalid zipcode format\n);
#	open (MYFILE, '>/var/www/html/polyweather.html');
#	print MYFILE qq(ERROR: invalid zipcode format\n);
#	close MYFILE;
	exit(0);
}   
      
my $url = "http://www.google.com/ig/api?weather=" . $ZipCode ;
my $tpp = XML::TreePP->new();
my $tree = $tpp->parsehttp( GET => $url );
my $city = $tree->{xml_api_reply}->{weather}->{forecast_information}->{city}->{"-data"};

if (length($city) < 1) {
		print "Unable to get info -- most likely a bad zip code. \n";
		open (MYFILE, '>/usr/share/www/doc/polyApps/polyweather550.html');
		print MYFILE qq(Unable to get info -- most likely a bad zip code.\n);
		close MYFILE;
		exit(0);
} 
 
my $currdatetime = $tree->{xml_api_reply}->{weather}->{forecast_information}->{current_date_time}->{"-data"};
$currdatetime = substr($currdatetime, 0, length($currdatetime) - 9);
my $currtempf = $tree->{xml_api_reply}->{weather}->{current_conditions}->{temp_f}->{"-data"};
my $currtempc = $tree->{xml_api_reply}->{weather}->{current_conditions}->{temp_c}->{"-data"};
my $currhumidity = $tree->{xml_api_reply}->{weather}->{current_conditions}->{humidity}->{"-data"};
my $currcondition = $tree->{xml_api_reply}->{weather}->{current_conditions}->{condition}->{"-data"};
my $currwind = $tree->{xml_api_reply}->{weather}->{current_conditions}->{wind_condition}->{"-data"};
my $currpic = basename $tree->{xml_api_reply}->{weather}->{current_conditions}->{icon}->{"-data"};
$currpic =~ s/\.gif/\.bmp/;
$currpic = $picPath . $currpic; my $todayhigh = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[0]->{high}->{"-data"};
my $todaylow = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[0]->{low}->{"-data"};
my $todaycond = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[0]->{condition}->{"-data"};
my $todaypic = basename $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[0]->{icon}->{"-data"};
$todaypic =~ s/\.gif/\.bmp/;
$todaypic = $picPath . $todaypic; my $tomorrowhigh = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[1]->{high}->{"-data"};
my $tomorrowlow = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[1]->{low}->{"-data"};
my $tomorrowcond = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[1]->{condition}->{"-data"};
my $tomorrowpic = basename $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[1]->{icon}->{"-data"};
$tomorrowpic =~ s/\.gif/\.bmp/;
$tomorrowpic = $picPath . $tomorrowpic;
my $tomorrowdayname = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[1]->{day_of_week}->{"-data"};
my $nextdayhigh = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[2]->{high}->{"-data"};
my $nextdaylow = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[2]->{low}->{"-data"};
my $nextdaycond = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[2]->{condition}->{"-data"};
my $nextdaypic = basename $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[2]->{icon}->{"-data"};
$nextdaypic =~ s/\.gif/\.bmp/;
$nextdaypic = $picPath . $nextdaypic;
my $nextdayname = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[2]->{day_of_week}->{"-data"};
my $thirddayhigh = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[3]->{high}->{"-data"};
my $thirddaylow = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[3]->{low}->{"-data"};
my $thirddaycond = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[3]->{condition}->{"-data"};
my $thirddaypic = basename $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[3]->{icon}->{"-data"};
$thirddaypic =~ s/\.gif/\.bmp/;
$thirddaypic = $picPath . $thirddaypic;
my $thirddayname = $tree->{xml_api_reply}->{weather}->{forecast_conditions}->[3]->{day_of_week}->{"-data"};
my $polyhtml = qq(<?xml version="1.0" encoding="utf-8"?>\n);
$polyhtml = $polyhtml . qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">\n);
$polyhtml = $polyhtml . qq(<html xmlns="http://www.w3.org/1999/xhtml">\n);
$polyhtml = $polyhtml . qq(<head><title>$city</title></head>\n);
$polyhtml = $polyhtml . qq(<body>\n);
$polyhtml = $polyhtml . qq(<table width="300" border="1" align="center">\n);
$polyhtml = $polyhtml . qq(<tr>\n);
$polyhtml = $polyhtml . qq(<td align="center">Current</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">Today</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">$tomorrowdayname</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">$nextdayname</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">$thirddayname</td>\n);
$polyhtml = $polyhtml . qq(</tr>\n);
$polyhtml = $polyhtml . qq(<tr>\n);
$polyhtml = $polyhtml . qq(<td align="center"><img src="$currpic" height="40" width="40"></img></td>\n);
$polyhtml = $polyhtml . qq(<td align="center"><img src="$todaypic" height="40" width="40"></img></td>\n);
$polyhtml = $polyhtml . qq(<td align="center"><img src="$tomorrowpic" height="40" width="40"></img></td>\n);
$polyhtml = $polyhtml . qq(<td align="center"><img src="$nextdaypic" height="40" width="40"></img></td>\n);
$polyhtml = $polyhtml . qq(<td align="center"><img src="$thirddaypic" height="40" width="40"></img></td>\n);
$polyhtml = $polyhtml . qq(</tr>\n);
$polyhtml = $polyhtml . qq(<tr>\n);
$polyhtml = $polyhtml . qq(<td align="center">$currtempf</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">$todayhigh°/$todaylow°</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">$tomorrowhigh°/$tomorrowlow°</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">$nextdayhigh°/$nextdaylow°</td>\n);
$polyhtml = $polyhtml . qq(<td align="center">$thirddayhigh°/$thirddaylow°</td>\n);
$polyhtml = $polyhtml . qq(</tr>\n);
$polyhtml = $polyhtml . qq(</table>\n);
$polyhtml = $polyhtml . qq-Current $currhumidity. $currwind.\n-;
$polyhtml = $polyhtml . qq(<br></br> Last Updated $currdatetime\n);
$polyhtml = $polyhtml . qq(</body>\n);
$polyhtml = $polyhtml . qq(</html>\n); #open (MYFILE, '>/var/www/html/polyweather.html');
#print MYFILE $polyhtml;
#close MYFILE; print $polyhtml;

How does it work?

Google has a nice little weather API which will provide a weather forecast for a given zip code via xml.
The perl script scrolls through the xml, parsing for the requested data and making a little webpage.
To call the script, you would run something such as:

perl weather.pl 90210

Now, for the perl script. First, if you don’t have the XML::TreePP module, you’ll need to install it. Which is as easy as:

# perl -MCPAN -e shell
cpan> install XML::TreePP

Create a folder to store your Apps. I used polyApps, so copy weather.pl and all the images to /usr/share/www/doc/polyApps/ It will help to have a SMB share setup so you can copy your files.

Schedule Crontab

Setup a Crontab schedule to run the weather.pl script; for example, twice a day every day.

00 06,12 * * * /usr/share/www/doc/polyApps/weather550.pl > /usr/share/www/doc/polyweather550.html

or to schedule it to run hourly:

01 * * * * /usr/share/www/doc/polyApps/weather550.pl > /usr/share/www/doc/polyweather550.html

To view current logged-in users crontab entries, type crontab -l

To edit a crontab entry, use crontab -e By default this will edit the current logged-in users crontab.
Save the file with :wq

By default crontab sends the job output to the user who scheduled the job. If you want to redirect the output to a specific user, add or update the MAIL variable in the crontab.
If you want the mail not to be sent to anywhere, add or update the MAIL variable in the crontab as: MAIL=""

Point the phones MicroBrowser - idleDisplay - home field to the url: http://sipx.domain.com/polyweather550.html and set the refresh period (in seconds).

If cron isn't running or you don't see any updates to polyweather550.html, check out the log file at
/var/log/cron