line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hubot::Scripts::weather; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Hubot::Scripts::weather::VERSION = '0.1.0'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Weather Script for Hubot. |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
21878
|
use utf8; |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
31
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
26
|
|
10
|
1
|
|
|
1
|
|
954
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
55931
|
|
|
1
|
|
|
|
|
30
|
|
11
|
1
|
|
|
1
|
|
1103
|
use Data::Printer; |
|
1
|
|
|
|
|
35625
|
|
|
1
|
|
|
|
|
7
|
|
12
|
1
|
|
|
1
|
|
1021
|
use Encode; |
|
1
|
|
|
|
|
11163
|
|
|
1
|
|
|
|
|
1406
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub load { |
15
|
0
|
|
|
0
|
0
|
|
my ( $class, $robot ) = @_; |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
$robot->hear( |
18
|
|
|
|
|
|
|
qr/^forecast (.+)/i, |
19
|
|
|
|
|
|
|
\&forecast_process, |
20
|
|
|
|
|
|
|
); |
21
|
0
|
|
|
|
|
|
$robot->hear( |
22
|
|
|
|
|
|
|
qr/^weather (.+)/i, |
23
|
|
|
|
|
|
|
\¤t_process, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub forecast_process { |
28
|
0
|
|
|
0
|
0
|
|
my $msg = shift; |
29
|
0
|
|
|
|
|
|
my $user_country = $msg->match->[0]; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
my $woeid = woeid_process($msg, $user_country); |
32
|
|
|
|
|
|
|
|
33
|
0
|
0
|
|
|
|
|
if ( $woeid =~ /^\d+/) { |
34
|
0
|
|
|
|
|
|
my @weekly = condition_process($woeid, 'weekly'); |
35
|
0
|
|
|
|
|
|
$msg->send("[$weekly[0]"." $weekly[1]]"." Low/High[$weekly[2]℃ /$weekly[3]℃ ]"." Condition[$weekly[4]]"); |
36
|
0
|
|
|
|
|
|
$msg->send("[$weekly[5]"." $weekly[6]]"." Low/High[$weekly[7]℃ /$weekly[8]℃ ]"." Condition[$weekly[9]]"); |
37
|
0
|
|
|
|
|
|
$msg->send("[$weekly[10]"." $weekly[11]]"." Low/High[$weekly[12]℃ /$weekly[13]℃ ]"." Condition[$weekly[14]]"); |
38
|
0
|
|
|
|
|
|
$msg->send("[$weekly[15]"." $weekly[16]]"." Low/High[$weekly[17]℃ /$weekly[18]℃ ]"." Condition[$weekly[19]]"); |
39
|
0
|
|
|
|
|
|
$msg->send("[$weekly[20]"." $weekly[21]]"." Low/High[$weekly[22]℃ /$weekly[23]℃ ]"." Condition[$weekly[24]]"); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else { |
42
|
0
|
|
|
|
|
|
$msg->send($woeid); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub current_process { |
47
|
0
|
|
|
0
|
0
|
|
my $msg = shift; |
48
|
0
|
|
|
|
|
|
my $user_country = $msg->match->[0]; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $woeid = woeid_process($msg, $user_country); |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
if ( $woeid =~ /^\d+/) { |
53
|
0
|
|
|
|
|
|
my %current = condition_process($woeid, 'current'); |
54
|
0
|
|
|
|
|
|
$msg->send("$current{location}"."[ LastTime:$current{date} ]"); |
55
|
0
|
|
|
|
|
|
$msg->send("The status of current weather-[$current{condition}]"." temp-[$current{temp}℃ ]"." humidity-[$current{humidty}%]" . |
56
|
|
|
|
|
|
|
" direction- [$current{direction}km]"." speed-[$current{speed}km/h]"." sunrise/sunset-[$current{sunrise}/$current{sunset}]"); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
0
|
|
|
|
|
|
$msg->send($woeid); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub condition_process { |
64
|
0
|
|
|
0
|
0
|
|
my ($woeid_param, $user_state) = @_; |
65
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
66
|
0
|
|
|
|
|
|
my %current; |
67
|
|
|
|
|
|
|
my @weekly; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $y_rep = $ua->get("http://weather.yahooapis.com/forecastrss?w=$woeid_param&u=c"); |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
if ($y_rep->is_success) { |
72
|
0
|
|
|
|
|
|
my $html = $y_rep->decoded_content; |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
if ( $user_state eq 'current' ) { |
|
|
0
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my ($condition, $temp, $date) = ($html =~ m{}gsm); |
76
|
0
|
|
|
|
|
|
$current{condition} = $condition; |
77
|
0
|
|
|
|
|
|
$current{temp} = $temp; |
78
|
0
|
|
|
|
|
|
$current{date} = $date; |
79
|
0
|
|
|
|
|
|
my ($city, $country) = ($html =~ m{}gsm); |
80
|
0
|
|
|
|
|
|
$current{location} = "$country - $city"; |
81
|
0
|
|
|
|
|
|
my ($chill, $direction, $speed) = ($html =~ m{}gsm); |
82
|
0
|
|
|
|
|
|
$current{chill} = $chill; |
83
|
0
|
|
|
|
|
|
$current{direction} = $direction; |
84
|
0
|
|
|
|
|
|
$current{speed} = $speed; |
85
|
0
|
|
|
|
|
|
my ($humidty, $visibility, $pressure, $rising) = ($html =~ m{}gsm); |
86
|
0
|
|
|
|
|
|
$current{humidty} = $humidty; |
87
|
0
|
|
|
|
|
|
$current{visibility} = $visibility; |
88
|
0
|
|
|
|
|
|
$current{pressure} = $pressure; |
89
|
0
|
|
|
|
|
|
$current{rising} = $rising; |
90
|
0
|
|
|
|
|
|
my ($sunrise, $sunset) = ($html =~ m{}gsm); |
91
|
0
|
|
|
|
|
|
$current{sunrise} = $sunrise; |
92
|
0
|
|
|
|
|
|
$current{sunset} = $sunset; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return %current; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
elsif ( $user_state eq 'weekly' ) { |
97
|
0
|
|
|
|
|
|
my @weekly = $html =~ m{}gsm; |
98
|
0
|
|
|
|
|
|
return @weekly; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
else { |
103
|
0
|
|
|
|
|
|
die $y_rep->status_line; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub woeid_process { |
108
|
0
|
|
|
0
|
0
|
|
my ($msg, $country) = @_; |
109
|
0
|
|
|
|
|
|
my $param = "$country"; |
110
|
0
|
|
|
|
|
|
my $error_msg = 'The name of the country or the city name wrong.'; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my $rep = $ua->get("http://woeid.rosselliot.co.nz/lookup/$param"); |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
if ($rep->is_success) { |
117
|
0
|
|
|
|
|
|
my @woeid = $rep->decoded_content =~ m{data-woeid="(\d+)"}gsm; |
118
|
0
|
|
|
|
|
|
my @countrys = $rep->decoded_content =~ m{data-woeid="\d+"> | .*? | .*? | (.*?) | }gsm;
119
|
|
|
|
|
|
|
|
120
|
0
|
0
|
0
|
|
|
|
if ( $countrys[0] || $countrys[1]) { |
|
|
0
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return "$woeid[0]"; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
elsif (!@woeid ) { |
124
|
0
|
|
|
|
|
|
return "$error_msg"; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
else { |
127
|
0
|
|
|
|
|
|
return $woeid[0]; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
else { |
131
|
0
|
|
|
|
|
|
die $rep->status_line; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |