line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Weather::Yahoo; |
2
|
1
|
|
|
1
|
|
24785
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
46
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5969
|
use WWW::Mechanize; |
|
1
|
|
|
|
|
582427
|
|
|
1
|
|
|
|
|
175
|
|
5
|
1
|
|
|
1
|
|
1389
|
use XML::XPath; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use utf8; |
7
|
|
|
|
|
|
|
#use XML::XPath::XMLParser; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
BEGIN { |
10
|
|
|
|
|
|
|
use Exporter (); |
11
|
|
|
|
|
|
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
12
|
|
|
|
|
|
|
$VERSION = '0.07'; |
13
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#Give a hoot don't pollute, do not export more than needed by default |
16
|
|
|
|
|
|
|
@EXPORT = qw(); |
17
|
|
|
|
|
|
|
@EXPORT_OK = qw(); |
18
|
|
|
|
|
|
|
%EXPORT_TAGS = (); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
|
|
|
|
|
|
my $proto = shift; |
23
|
|
|
|
|
|
|
my $class = ref $proto || $proto; |
24
|
|
|
|
|
|
|
my $self = {}; |
25
|
|
|
|
|
|
|
bless $self, $class; |
26
|
|
|
|
|
|
|
my $city = shift; |
27
|
|
|
|
|
|
|
my $unit = shift || 'c'; #c or f, (celcius vs farenheit) |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
if ( ! $city ) { |
30
|
|
|
|
|
|
|
warn 'No city given. Usage Example: WWW::Weather::Yahoo->new( "São Paulo, SP" ) '; |
31
|
|
|
|
|
|
|
return undef; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $mech = WWW::Mechanize->new( ); |
35
|
|
|
|
|
|
|
$mech->agent_alias( 'Windows IE 6' ); |
36
|
|
|
|
|
|
|
my $woeid; |
37
|
|
|
|
|
|
|
if ( $city =~ m/^(\d+)$/g ) { |
38
|
|
|
|
|
|
|
$woeid = $city; #user passed an woeid |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else { |
41
|
|
|
|
|
|
|
$woeid = woeid_by_location( $city ); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
if ( !$woeid ) { |
45
|
|
|
|
|
|
|
warn "Nothing found. Check the city input."; |
46
|
|
|
|
|
|
|
return undef; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
$self->{_woeid} = $woeid; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $weather_url = |
51
|
|
|
|
|
|
|
"http://weather.yahooapis.com/forecastrss?w=$woeid&u=$unit"; |
52
|
|
|
|
|
|
|
my $url = $weather_url; |
53
|
|
|
|
|
|
|
$mech->get( $url ); |
54
|
|
|
|
|
|
|
my $content = $mech->content; |
55
|
|
|
|
|
|
|
if ( $content =~ m/City not found/ ) { |
56
|
|
|
|
|
|
|
return undef; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
$self->{_weather} = parse_content( $content, $city ); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
return $self; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub woeid_by_location { |
64
|
|
|
|
|
|
|
my $loc = shift; |
65
|
|
|
|
|
|
|
my $finder = |
66
|
|
|
|
|
|
|
"http://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text =\' $loc \' "; |
67
|
|
|
|
|
|
|
my $mech = WWW::Mechanize->new(); |
68
|
|
|
|
|
|
|
$mech->agent_alias('Windows IE 6'); |
69
|
|
|
|
|
|
|
$mech->get($finder); |
70
|
|
|
|
|
|
|
my $xml = $mech->content; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
if ( !$xml ) { |
73
|
|
|
|
|
|
|
warn 'INVALID LOCAL'; |
74
|
|
|
|
|
|
|
return undef; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $xml_placefinder = XML::XPath->new( xml => $xml ); |
78
|
|
|
|
|
|
|
return $xml_placefinder->findvalue('//woeid'); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub parse_content { |
82
|
|
|
|
|
|
|
my $content = shift; |
83
|
|
|
|
|
|
|
my $city = shift; |
84
|
|
|
|
|
|
|
my $weather = {}; |
85
|
|
|
|
|
|
|
$weather->{city_name} = $city; |
86
|
|
|
|
|
|
|
if ( $content =~ |
87
|
|
|
|
|
|
|
m/yweather:location city="(.+)"( +)region="(.+)"( +)country="(.+)"/ ) |
88
|
|
|
|
|
|
|
{ |
89
|
|
|
|
|
|
|
$weather->{location_city} = $1; |
90
|
|
|
|
|
|
|
$weather->{location_region} = $3; |
91
|
|
|
|
|
|
|
$weather->{location_country} = $5; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
if ( $content =~ |
94
|
|
|
|
|
|
|
m/yweather:units temperature="(.+)"( +)distance="(.+)"( +)pressure="(.+)"( +)speed="(.+)"/ |
95
|
|
|
|
|
|
|
) |
96
|
|
|
|
|
|
|
{ |
97
|
|
|
|
|
|
|
$weather->{unit_temperature} = $1; |
98
|
|
|
|
|
|
|
$weather->{unit_distance} = $3; |
99
|
|
|
|
|
|
|
$weather->{unit_pressure} = $5; |
100
|
|
|
|
|
|
|
$weather->{unit_speed} = $7; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
if ( $content =~ |
104
|
|
|
|
|
|
|
m/yweather:wind chill="(.+)"( +)direction="(.+)"( +)speed="(.+)"/ ) |
105
|
|
|
|
|
|
|
{ |
106
|
|
|
|
|
|
|
$weather->{wind_chill} = $1; |
107
|
|
|
|
|
|
|
$weather->{wind_direction} = $3; |
108
|
|
|
|
|
|
|
$weather->{wind_speed} = $5; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
if ( $content =~ |
111
|
|
|
|
|
|
|
m/yweather:atmosphere humidity="(.+)"( +)visibility="(.+)"( +)pressure="(.+)"( +)rising="(.+)"/ |
112
|
|
|
|
|
|
|
) |
113
|
|
|
|
|
|
|
{ |
114
|
|
|
|
|
|
|
$weather->{atmosphere_humidity} = $1; |
115
|
|
|
|
|
|
|
$weather->{atmosphere_visibility} = $3; |
116
|
|
|
|
|
|
|
$weather->{atmosphere_pressure} = $5; |
117
|
|
|
|
|
|
|
$weather->{atmosphere_rising} = $7; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
if ( $content =~ m/yweather:astronomy sunrise="(.+)"( +)sunset="(.+)"/ ) { |
121
|
|
|
|
|
|
|
$weather->{astronomy_sunrise} = $1; |
122
|
|
|
|
|
|
|
$weather->{astronomy_sunset} = $3; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
if ( $content =~ m/geo:lat>(.+) ) { |
125
|
|
|
|
|
|
|
$weather->{location_lat} = $1; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
if ( $content =~ m/geo:long>(.+)<\// ) { |
128
|
|
|
|
|
|
|
$weather->{location_lng} = $1; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
if ( $content =~ |
132
|
|
|
|
|
|
|
m/yweather:condition( +)text="(.+)"( +)code="(.+)"( +)temp="(.+)"( +)date="(.+)"/ |
133
|
|
|
|
|
|
|
) |
134
|
|
|
|
|
|
|
{ |
135
|
|
|
|
|
|
|
$weather->{condition_text} = $2; |
136
|
|
|
|
|
|
|
$weather->{condition_code} = $4; |
137
|
|
|
|
|
|
|
$weather->{condition_temp} = $6; |
138
|
|
|
|
|
|
|
$weather->{condition_date} = $8; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
if ( $content =~ m/img src="(.+)"/ ) { |
142
|
|
|
|
|
|
|
$weather->{condition_img_src} = $1; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $count = 1; |
146
|
|
|
|
|
|
|
while ( $content =~ |
147
|
|
|
|
|
|
|
m/yweather:forecast day="(.+)"( +)date="(.+)"( +)low="(.+)"( +)high="(.+)"( +)text="(.+)"( +)code="(.+)"/g |
148
|
|
|
|
|
|
|
) |
149
|
|
|
|
|
|
|
{ |
150
|
|
|
|
|
|
|
if ( $count == 2 ) { |
151
|
|
|
|
|
|
|
$weather->{forecast_tomorrow_day} = $1; |
152
|
|
|
|
|
|
|
$weather->{forecast_tomorrow_date} = $3; |
153
|
|
|
|
|
|
|
$weather->{forecast_tomorrow_low} = $5; |
154
|
|
|
|
|
|
|
$weather->{forecast_tomorrow_high} = $7; |
155
|
|
|
|
|
|
|
$weather->{forecast_tomorrow_text} = $9; |
156
|
|
|
|
|
|
|
$weather->{forecast_tomorrow_code} = $11; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
$count++; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
return $weather; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 NAME |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
WWW::Weather::Yahoo - Gets information from yahoo weather. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 SYNOPSIS |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
use WWW::Weather::Yahoo; |
170
|
|
|
|
|
|
|
my $yw = WWW::Weather::Yahoo->new( 'São Paulo, SP', 'c' );#by city name |
171
|
|
|
|
|
|
|
my $yw = WWW::Weather::Yahoo->new( 'São Paulo, SP', 'f' );#by city name |
172
|
|
|
|
|
|
|
my $yw = WWW::Weather::Yahoo->new( 455827 , 'c' ); #by woeid |
173
|
|
|
|
|
|
|
print $yw->{ _weather }{location_city}; |
174
|
|
|
|
|
|
|
print $yw->{ _weather }{location_region}; |
175
|
|
|
|
|
|
|
print $yw->{ _weather }{location_country}; |
176
|
|
|
|
|
|
|
print $yw->{ _weather }{unit_temperature}; |
177
|
|
|
|
|
|
|
print $yw->{ _weather }{unit_distance}; |
178
|
|
|
|
|
|
|
print $yw->{ _weather }{unit_pressure}; |
179
|
|
|
|
|
|
|
print $yw->{ _weather }{unit_speed}; |
180
|
|
|
|
|
|
|
print $yw->{ _weather }{wind_chill}; |
181
|
|
|
|
|
|
|
print $yw->{ _weather }{wind_direction}; |
182
|
|
|
|
|
|
|
print $yw->{ _weather }{wind_speed}; |
183
|
|
|
|
|
|
|
print $yw->{ _weather }{atmosphere_humidity}; |
184
|
|
|
|
|
|
|
print $yw->{ _weather }{atmosphere_visibility}; |
185
|
|
|
|
|
|
|
print $yw->{ _weather }{atmosphere_pressure}; |
186
|
|
|
|
|
|
|
print $yw->{ _weather }{atmosphere_rising}; |
187
|
|
|
|
|
|
|
print $yw->{ _weather }{astronomy_sunrise}; |
188
|
|
|
|
|
|
|
print $yw->{ _weather }{astronomy_sunset}; |
189
|
|
|
|
|
|
|
print $yw->{ _weather }{location_lat}; |
190
|
|
|
|
|
|
|
print $yw->{ _weather }{location_lng}; |
191
|
|
|
|
|
|
|
print $yw->{ _weather }{condition_text}; |
192
|
|
|
|
|
|
|
print $yw->{ _weather }{condition_code}; |
193
|
|
|
|
|
|
|
print $yw->{ _weather }{condition_temp}; |
194
|
|
|
|
|
|
|
print $yw->{ _weather }{condition_date}; |
195
|
|
|
|
|
|
|
print $yw->{ _weather }{condition_img_src}; |
196
|
|
|
|
|
|
|
print $yw->{ _weather }{forecast_tomorrow_day}; |
197
|
|
|
|
|
|
|
print $yw->{ _weather }{forecast_tomorrow_date}; |
198
|
|
|
|
|
|
|
print $yw->{ _weather }{forecast_tomorrow_low}; |
199
|
|
|
|
|
|
|
print $yw->{ _weather }{forecast_tomorrow_high}; |
200
|
|
|
|
|
|
|
print $yw->{ _weather }{forecast_tomorrow_text}; |
201
|
|
|
|
|
|
|
print $yw->{ _weather }{forecast_tomorrow_code}; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 DESCRIPTION |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
WWW::Weather::Yahoo retrieves weather data from http://weather.yahoo.com. |
207
|
|
|
|
|
|
|
Enter the city name and get weather data. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 AUTHOR |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Hernan Lopes |
213
|
|
|
|
|
|
|
CPAN ID: HERNAN |
214
|
|
|
|
|
|
|
HERNAN |
215
|
|
|
|
|
|
|
hernanlopes@gmail.com |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 COPYRIGHT |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
This program is free software licensed under the... |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
The BSD License |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
The full text of the license can be found in the |
224
|
|
|
|
|
|
|
LICENSE file included with this module. |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 SEE ALSO |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
perl(1). |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
1; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# The preceding line will help the module return a true value |
236
|
|
|
|
|
|
|
|