line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Livedoor::Weather; |
2
|
2
|
|
|
2
|
|
24792
|
use 5.008005; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
88
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
64
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
66
|
|
5
|
2
|
|
|
2
|
|
5302
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
208752
|
|
|
2
|
|
|
|
|
68
|
|
6
|
2
|
|
|
2
|
|
22
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
157
|
|
7
|
2
|
|
|
2
|
|
2563
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use JSON; |
9
|
|
|
|
|
|
|
use Encode; |
10
|
|
|
|
|
|
|
use Furl; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Class::Accessor::Lite::Lazy( |
15
|
|
|
|
|
|
|
new => 1, |
16
|
|
|
|
|
|
|
ro_lazy => [qw/furl/] |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _build_furl{ |
20
|
|
|
|
|
|
|
my $self = shift; |
21
|
|
|
|
|
|
|
return Furl->new(agent => 'WWW::Livedoor::Weather(Perl)',timeout => 10); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use constant BASE_URI => 'http://weather.livedoor.com/forecast'; |
25
|
|
|
|
|
|
|
use constant XML_URI => BASE_URI.'/rss/primary_area.xml'; |
26
|
|
|
|
|
|
|
use constant JSON_URI => BASE_URI.'/webservice/json/v1'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub get{ |
29
|
|
|
|
|
|
|
my($self,$city) = @_; |
30
|
|
|
|
|
|
|
my $cityid = do{ |
31
|
|
|
|
|
|
|
if($city =~ /\d+/){ |
32
|
|
|
|
|
|
|
$city; |
33
|
|
|
|
|
|
|
}else{ |
34
|
|
|
|
|
|
|
$self->_get_cityid($city); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
my $url = JSON_URI."?city=$cityid"; |
38
|
|
|
|
|
|
|
return $self->_parse_forecast($url); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _parse_forecast{ |
42
|
|
|
|
|
|
|
my ($self,$url) = @_; |
43
|
|
|
|
|
|
|
my $res = $self->furl->get($url); |
44
|
|
|
|
|
|
|
return $self->_forecastmap($res->content); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _forecastmap{ |
48
|
|
|
|
|
|
|
my($self,$json) = @_; |
49
|
|
|
|
|
|
|
my $ref; |
50
|
|
|
|
|
|
|
eval{$ref = JSON::decode_json($json)}; |
51
|
|
|
|
|
|
|
croak('failed reading weather information'.$@) if $@; |
52
|
|
|
|
|
|
|
return $ref; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _get_cityid{ |
56
|
|
|
|
|
|
|
my($self,$cityname) = @_; |
57
|
|
|
|
|
|
|
eval{$cityname = Encode::decode_utf8($cityname)}; |
58
|
|
|
|
|
|
|
if($@){ |
59
|
|
|
|
|
|
|
croak("cannot decode:$cityname".$@); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
my $response = $self->furl->get(XML_URI); |
62
|
|
|
|
|
|
|
my $content = $response->content; |
63
|
|
|
|
|
|
|
$self->{citymap} = $self->_parse_citymap($content); |
64
|
|
|
|
|
|
|
return $self->{citymap}->{$cityname}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _parse_citymap{ |
68
|
|
|
|
|
|
|
my($self,$content) = @_; |
69
|
|
|
|
|
|
|
my $ref = eval{ |
70
|
|
|
|
|
|
|
local $XML::Simple::PREFERED_PARSER = 'XML::Parser'; |
71
|
|
|
|
|
|
|
XMLin($content,ForceArray => [qw/area city/]); |
72
|
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
if($@){ |
74
|
|
|
|
|
|
|
croak('Oh! failed reading forecastmap:'.$@); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
my %city; |
77
|
|
|
|
|
|
|
foreach my $area(@{$ref->{channel}->{'ldWeather:source'}->{pref}}){ |
78
|
|
|
|
|
|
|
$city{$area->{city}->{$_}->{title}} = $_ for keys %{$area->{city}}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
return \%city; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |