Created
September 3, 2012 20:51
-
-
Save obrun/3613378 to your computer and use it in GitHub Desktop.
Polycom MiniBrowser xhtml Weather Page/Application Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w -I/home/YOURACCOUNT/cpan/lib | |
# | |
# WeatherPage | |
# | |
# Reads wunderground.com's Weather Service and creats a page in $outputfile | |
# which is a XHTML page that may be read by PolyCom phones via a web server. | |
# | |
# Master to be found at: https://gist.github.com/3613378 | |
# Please leave comments and defects as comments on https://gist.github.com/3613378 | |
# | |
# Reads any weather alerts. | |
# If there are no alerts it reads the current and then forecast conditions | |
# and uses that to create a meaningful digest of the weather. | |
# | |
# Requires the CPAN XML::TreePP which is found via the -I above if local | |
# | |
# Usage: WeatherPage.cgi $location $outputfile | |
# OR from cron | |
# 12,27,42,57 * * * * /home/YOURACCOUNT/cgi-bin/WeatherPage.cgi | |
# | |
# NOTICE: | |
# Copyright 2012 ISDL division of SwarmPoint LLC. | |
# Provided by Inter Site Data Links (ISDL.com) as is and without warranty. | |
# Licensed for commercial or private use, modification, adaption or translation | |
# with the inclusion of this notice. | |
# | |
use strict; | |
use XML::TreePP; | |
# Obtain a free api key from wunderground.com | |
my $apikey = "YOURFREEAPIKEY"; | |
# Also update YOURACCOUNT below for the location of your web pages | |
my $location = $ARGV[0]; | |
if (!defined($location)) { | |
# A default location | |
# $location = "Australia/Sydney"; | |
# $location = "NY/New_York"; | |
$location = "10128"; | |
} | |
my $outputfile = $ARGV[1]; | |
if (!defined($outputfile)) { | |
# A default outputfile | |
$outputfile = '>/home/YOURACCOUNT/public_html/weather.xhtml'; | |
} | |
if ($location !~ /^(\d{5})$/ && $location !~ /\//) { | |
print qq(ERROR: invalid location format\n); | |
open (MYFILE, $outputfile); | |
print MYFILE qq(ERROR: invalid location format\n); | |
close MYFILE; | |
exit(0); | |
} | |
# Get the XML tree of a given URL | |
sub getTree # URL | |
{ | |
my $tpp = XML::TreePP->new(); | |
my $tree = $tpp->parsehttp( GET => $_[0] ); | |
unless ( defined($tree) && length $tree > 0 ) { | |
sleep(2); | |
$tree = $tpp->parsehttp( GET => $_[0] ); | |
unless ( defined($tree) && length $tree > 0 ) { | |
sleep(2); | |
$tree = $tpp->parsehttp( GET => $_[0] ); | |
} | |
} | |
return $tree; | |
} | |
sub notify # Title, Message | |
{ | |
open (MYFILE, $outputfile); | |
print MYFILE qq(<?xml version="1.0" encoding="utf-8"?>\n); | |
print MYFILE qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">\n); | |
print MYFILE qq(<html xmlns="http://www.w3.org/1999/xhtml">\n); | |
print MYFILE qq(<head><title>$_[0]</title></head>\n); # Title | |
print MYFILE qq(<body>\n); | |
print MYFILE qq(<p>\n); | |
print MYFILE qq($_[1]\n); # Message | |
print MYFILE qq(</p>\n); | |
print MYFILE qq(</body>\n); | |
print MYFILE qq(</html>\n); | |
close MYFILE; | |
} | |
# Alert | |
# If there is a weather alert (Hurricanes, Storms etc.) then display that | |
# and forget about the weather forecast. | |
# To test code changes find a set of zip codes with current severe weather via | |
# http://www.wunderground.com/severe.asp as alert structures can vary. | |
my $tree = getTree( "http://api.wunderground.com/api/$apikey/alerts/lang:EN/q/$location.xml" ); | |
unless ( defined($tree) && length $tree > 0 ) { | |
print "The weather alert service is currently unavailable. \n"; | |
notify( 'Weather is Unavailable!', 'The weather alert service is currently unavailable from the data vendor.'); | |
exit(0); | |
} | |
my $alerts = $tree->{response}->{alerts}; | |
if ( length $alerts > 0 ) { | |
my $result = ''; | |
my @total_alerts_array = $tree->{response}->{alerts}->{alert}; | |
my $total_alerts = 1; | |
if ( ref($total_alerts_array[0]) eq 'ARRAY' ) { | |
my $total_alerts = $#{$total_alerts_array[0]}+1; | |
} | |
for (my $alert_index = 0; $alert_index < $total_alerts; $alert_index++) { | |
if ( $total_alerts == 1 ) { | |
if ( ref($total_alerts_array[0]) eq 'ARRAY' ) { | |
# I am not sure if this can ever happen but to be safe. | |
$result .= $tree->{response}->{alerts}->{alert}->[$alert_index]->{message}; | |
} else { | |
$result .= $tree->{response}->{alerts}->{alert}->{message}; | |
} | |
} else { | |
# First 8 charactures of dates give time. 8th may be a space. | |
my $alert_begin = $tree->{response}->{alerts}->{alert}->[$alert_index]->{date}; | |
my $alert_end = $tree->{response}->{alerts}->{alert}->[$alert_index]->{expires}; | |
# Type = TOW, SPE, WAT, WRN | |
my $alert_type = $tree->{response}->{alerts}->{alert}->[$alert_index]->{type}; | |
my $alert_message = $tree->{response}->{alerts}->{alert}->[$alert_index]->{message}; | |
my $alert_description = $tree->{response}->{alerts}->{alert}->[$alert_index]->{description}; | |
if ( $alert_type eq 'SPE' ) { | |
$result .= '<p>' . $alert_message . '</p>'; | |
} else { | |
$result .= '<p>' . $alert_description . ' (' . (substr lc($alert_begin), 0, 8) . '-' . (substr lc($alert_end), 0, 8) . ')</p>'; | |
} | |
} | |
} | |
notify( 'ALERT!', $result ); | |
exit(0); | |
} | |
# Current conditions | |
$tree = getTree( "http://api.wunderground.com/api/$apikey/conditions/lang:EN/q/$location.xml"); | |
unless ( defined($tree) && length $tree > 0 ) { | |
print "The weather service is currently unavailable. \n"; | |
notify( 'Weather is Unavailable!', 'The weather service is currently unavailable from the data vendor'); | |
exit(0); | |
} | |
my $city = $tree->{response}->{current_observation}->{display_location}->{city}; | |
my $currtempf = $tree->{response}->{current_observation}->{temp_f}; | |
my $currtempc = $tree->{response}->{current_observation}->{temp_c}; | |
my $currhumidity = $tree->{response}->{current_observation}->{relative_humidity}; | |
my $currcondition = $tree->{response}->{current_observation}->{icon}; | |
$currcondition = ucfirst($currcondition); | |
my $currimage = $tree->{response}->{current_observation}->{icon_url}; | |
my $currwind = $tree->{response}->{current_observation}->{wind_string}; | |
# Forecast | |
$tree = getTree( "http://api.wunderground.com/api/$apikey/forecast/lang:EN/q/$location.xml" ); | |
unless ( defined($tree) && length $tree > 0 ) { | |
print "The weather forecast is currently unavailable. \n"; | |
notify( 'Forecast is Unavailable!', 'The weather forecast is currently unavailable from the data vendor.'); | |
exit(0); | |
} | |
my $forecast = $tree->{response}->{forecast}->{txt_forecast}->{forecastdays}->{forecastday}->[0]->{fcttext}; | |
my $todaylow = $tree->{response}->{forecast}->{simpleforecast}->{forecastdays}->{forecastday}->[0]->{low}->{fahrenheit}; | |
my $todayhigh = $tree->{response}->{forecast}->{simpleforecast}->{forecastdays}->{forecastday}->[0]->{high}->{fahrenheit}; | |
my $todaycond = $tree->{response}->{forecast}->{simpleforecast}->{forecastdays}->{forecastday}->[0]->{conditions}; | |
my $tomorrowcond = $tree->{response}->{forecast}->{simpleforecast}->{forecastdays}->{forecastday}->[1]->{conditions}; | |
#my $title = "$forecast., High: $todayhigh"; | |
my $title = "$currcondition., High: $todayhigh"; | |
#"<img src=\"$currimage\"></img>"; | |
my $message = "$city \-\- It is $currtempf F ($currtempc C), $currcondition. High $todayhigh, Low $todaylow, $todaycond. Humidity: $currhumidity. Wind $currwind. Tomorrow: $tomorrowcond."; | |
notify ( $title, $message ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment