#!/usr/bin/perl -W # Created by Patrick Webster 20051006 patrick@aushack.com # # Download data from the Weather Co. (Australia) and process it. # Then display the realtime weather in a Cisco 7940 VoIP friendly format. # # Version 0.1a - Initial release # # To do: Read the cities from the XML response, not hardcoded array. # Make sure you set the url, username and password, which follows below. $uname = 'username'; $password = 'password'; $url = 'http://clients.theweather.com.au/' . $uname . '/data/wx_capcity_4day.xml'; # You shouldn't need to modify anything below. # Set the required modules use HTTP::Request; use HTTP::Headers; use LWP::UserAgent; use XML::Simple; use Data::Dumper; use Cisco::IPPhone; # Create HTTP header object with BASIC authentication, from the username # and password set earlier, and GET the content. $h = HTTP::Headers->new; $h->authorization_basic($uname, $password); $request = HTTP::Request->new(GET => $url, $h); $ua = LWP::UserAgent->new; $response = $ua->request($request); if ($response->is_success) { # Uncomment below if you want to see what is returned from the server for debugging. # print $response->content; } else { print STDERR $response->status_line, "\n"; exit 1; } # Create the xml object using XML::Simple, fill with the HTTP response into memory. $xml = new XML::Simple; $xmlData = $xml->XMLin($response->content); # Uncomment the line below if you have trouble finding your way around $xmlData tree. # Do not forget to recomment before the Cisco phones can access it again. # print Dumper($xmlData); # The cities are not in an XML array format, so I have manually created the array here. # Use the Dumper to check what cities are supported. @cities = ("Melbourne","Sydney","Hobart","Canberra","Perth","Darwin","Brisbane","Adelaide"); for ($cC=0;$cC<@cities;$cC++) { $ciscoText .= "City: $cities[$cC]:\n"; for ($day=0;$day<4;$day++) { $ciscoText .= "Day: $xmlData->{weather}->{countries}->{country}->{location}->{$cities[$cC]}->{forecasts}->{forecast}->[$day]->{day_name} $xmlData->{weather}->{countries}->{country}->{location}->{$cities[$cC]}->{forecasts}->{forecast}->[$day]->{date}\n"; $ciscoText .= "Condition: $xmlData->{weather}->{countries}->{country}->{location}->{$cities[$cC]}->{forecasts}->{forecast}->[$day]->{icon}\n"; $ciscoText .= "Min Temp: $xmlData->{weather}->{countries}->{country}->{location}->{$cities[$cC]}->{forecasts}->{forecast}->[$day]->{temp_min_c}->{content}\n"; $ciscoText .= "Max Temp: $xmlData->{weather}->{countries}->{country}->{location}->{$cities[$cC]}->{forecasts}->{forecast}->[$day]->{temp_max_c}->{content}\n"; $ciscoText .= "\n"; } } # Create the Cisco Phone object. Note that this automagically creates the content-type # when returning the CGI (that is, text/xml), so don't do this yourself. $ciscoData = new Cisco::IPPhone; $ciscoData->Text( { Title => "The Weather @ $xmlData->{metadata}->{create_time}->{content}", Prompt => "Provided by The Weather Co.", Text => "$ciscoText" }); # Send it! ;-) Enjoy print $ciscoData->Content; # EOF