Weather::Com::Base
Perl extension for getting weather information from weather.com
- Provided by: libweather-com-perl (Version: 0.5.3-2)
- Report a bug
Perl extension for getting weather information from weather.com
use Data::Dumper;
use Weather::Com::Base;
# define parameters for weather search
my %params = (
'current' => 1,
'forecast' => 3,
'links' => 1,
'units' => 's',
'proxy' => 'http://proxy.sonstwo.de',
'timeout' => 250,
'debug' => 1,
'partner_id' => 'somepartnerid',
'license' => '12345678',
);
# instantiate a new weather.com object
my $weather_com = Weather::Com::Base->new(%params);
# search for locations called 'Heidelberg'
my $locations = $weather_com->search('Heidelberg')
or die "No location found!\n";
# and then get the weather for each location found
foreach (keys %{$locations}) {
my $weather = $weather_com->get_weather($_);
print Dumper($weather);
}
Weather::Com::Base is a Perl module that provides low level OO interface to gather all weather information that is provided by weather.com.
This module should not be used directly because of the business rules applying if one want's to use the weather.com's xoap interface. These business rules enforce you to implement several caching rules.
Therefore, if you want to use a low level interface, please use
Weather::Com::Cached instead. It has the same interface as this
module but it implements all caching rules of weather.com.
EXPORT
None by default. But there are a few static methods for conversion purposes you may wanna use:
Takes the temperature in celsius and returns the temperature in fahrenheit (as an integer value).
Takes the temperature in fahrenheit and returns the temperature in celius (as an integer value).
Takes a wind mnemonic ("N", "WNW", etc.) or a long name of a wind direction ("North Northeast") and returns the other format.
The long names are only understood if used as follows:
"North"
"North Northwest"
"Northwest"
"West Northwest"
"West"
"West Southwest"
"Southwest"
"South Southwest"
"South"
"South Southeast"
"Southeast"
"East Southeast"
"East"
"East Northeast"
"Northeast"
"North Northeast"
"Variable"
new(hash or hashref)
The constructor takes a hash or a hashref containing a bunch of parameters used to configure your weather search. None of these paramters is mandatory. As there are reasonable defaults for any of them, you need only to provide those parameters where you whish to go with non-default values.
The parameters in detail:
Defaults to 0 (false).
If set to 0 (false) no forecast is read, if set to a value between 1 and 10 a forecast for the number of days is requested. If set to any other value, this is interpreted as 0!
Defaults to 0 (false).
Defaults to 'm'.
Defaults to undef.
Defaults to undef.
Defaults to 180 seconds.
Defaults to 0 (off).
search(place to search)
Searches for all known locations matching the provided search string.
At weather.com you have to request weather data for a specific location. Therefor you first have to find the location id for the location you are looking for. weather.com provides two possibilities to search:
If the search causes an error, this methods dies with a (hopefully) helpful error message.
If the search returns no matches, 0 is returned.
Else, the method returns a hashref containing all locations found. The hashref looks as follows if you search for Heidelberg:
$HASHREF = {
'GMXX0053' => 'Heidelberg, Germany',
'USKY0990' => 'Heidelberg, KY',
'USMS0154' => 'Heidelberg, MS'
};
The keys of the hash are the location ids as used within
weather.com. This keys have to be used for fetching the weather
information of one location.
get_weather(location id)
This method fetches all weather information as defined by the object attributes you may have modified while instantiating the weather object.
If an error occurs while fetching weather information, this method dies setting $@ to a meaningfull error message.
The following hashref shows the maximum of data that can be fetched from weather.com. The parts of the hashref are explained afterwards.
$HASHREF = {
'head' => {
'ur' => 'mm',
'ud' => 'km',
'us' => 'km/h',
'form' => 'MEDIUM',
'up' => 'mb',
'locale' => 'en_US',
'ut' => 'C'
},
'loc' => {
'suns' => '8:40 PM',
'zone' => '2',
'lat' => '49.41',
'tm' => '3:48 PM',
'sunr' => '6:18 AM',
'dnam' => 'Heidelberg, Germany',
'id' => 'GMXX0053',
'lon' => '8.68'
},
'cc' => {
'icon' => '28',
'flik' => '21',
'obst' => 'Mannhein, Germany',
'lsup' => '8/16/04 3:20 PM Local Time',
'tmp' => '21',
'hmid' => '78',
'wind' => {
'gust' => 'N/A',
'd' => '170',
's' => '11',
't' => 'S'
},
'bar' => {
'r' => '1,010.8',
'd' => 'steady'
},
'dewp' => '17',
'uv' => {
't' => 'Moderate',
'i' => '3'
},
'vis' => '10.0',
't' => 'Mostly Cloudy'
},
'dayf' => {
'lsup' => '8/16/04 12:17 PM Local Time',
'day' => [
{
'hi' => '27',
'suns' => '8:40 PM',
'dt' => 'Aug 16',
'part' => [
{
'hmid' => '57',
'wind' => {
'gust' => 'N/A',
'd' => '204',
's' => '16',
't' => 'SSW'
},
'icon' => '28',
'p' => 'd',
'ppcp' => '20',
't' => 'Mostly Cloudy'
},
{
'hmid' => '87',
'wind' => {
'gust' => 'N/A',
'd' => '215',
's' => '13',
't' => 'SW'
},
'icon' => '11',
'p' => 'n',
'ppcp' => '60',
't' => 'Showers'
}
],
'd' => '0',
'sunr' => '6:18 AM',
'low' => '16',
't' => 'Monday'
},
... next day ...,
... and so on ...,
]
},
'lnks' => {
'link' => [
{
'l' => 'http://www.weather.com/outlook/health/allergies/USMS0154?par=xoap',
'pos' => '1',
't' => 'Pollen Reports'
},
{
'l' => 'http://www.weather.com/outlook/travel/flights/citywx/USMS0154?par=xoap',
'pos' => '2',
't' => 'Airport Delays'
},
{
'l' => 'http://www.weather.com/outlook/events/special/result/USMS0154?when=thisweek&par=xoap',
'pos' => '3',
't' => 'Special Events'
},
{
'l' => 'http://www.weather.com/services/desktop.html?par=xoap',
'pos' => '4',
't' => 'Download Desktop Weather'
}
],
'type' => 'prmo'
},
'ver' => '2.0'
};
Header Information Block
The hashref head contains unit of measure definitions for the whole dataset. Usually they are either all metric or all us.
Location Information Block
The hashref loc contains some information about the location that does not change very much each hour or day.
Time is always presented like 8:45 AM or 11:33 PM.
Current Conditions Block
The hashref cc contains information about the current conditions.
Forecasts
Up to 10 days of forecasts can be found in the hashref dayf.
Links
The hashref lnks contains some links to other weather information that may be interesting for the chosen location. This will not be explained in further detail here. Just play around with the sample...
See also Weather::Com::Cached for the cached version of the low level API.
Thomas Schnuecker, <thomas@schnuecker.de>
Copyright (C) 2004-2007 by Thomas Schnuecker
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The data provided by weather.com and made accessible by this OO interface can be used for free under special terms. Please have a look at the application programming guide of weather.com (http://www.weather.com/services/xmloap.html)!