-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKionHotJava.pl
More file actions
executable file
·60 lines (55 loc) · 1.53 KB
/
KionHotJava.pl
File metadata and controls
executable file
·60 lines (55 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl -CS
use strict;
use warnings;
use utf8;
use HTTP::Tiny;
use JSON::PP;
use KHJConfig;
sub fetch_temp {
my $response=HTTP::Tiny->new->get($KHJConfig::livedoorWeatherURL.$KHJConfig::locationParam);
die "Accessing weather information failed.\n" unless $response->{success};
die "Response length is 0.\n" if !length $response->{content};
my $weather=decode_json($response->{content});
my $forecasts=${$weather}{forecasts};
my $maxC=undef;
foreach(@$forecasts) {
if(${$_}{dateLabel} eq $KHJConfig::targetDateLabel) {
my $temps=${$_}{temperature};
my $maxtemps=${$temps}{max};
if(defined($maxtemps)) {
$maxC=${$maxtemps}{celsius};
}
}
}
if(defined($maxC)) {
my %result;
$result{temperature}=$maxC;
$result{link}=${$weather}{link};
return \%result;
} else {
die "Max temperature not in the forecast -- maybe you need to run this program earlier in the morning.\n";
}
}
sub msgForTemp {
my $temp=shift;
foreach (@KHJConfig::hotjavaMessages) {
if($temp >= ${$_}{'thresh'}) {
return ${$_}{msg};
}
}
return undef;
}
sub run_tempHotJava {
my $temp=fetch_temp();
#print STDERR ${$temp}{temperature}." C\n";
my $msg=msgForTemp(int(${$temp}{temperature}));
if(defined($msg)) {
# Do the HotJava thing!
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$mon++;
my $message=$msg.${$temp}{link}." ".$mon."/".$mday."\n";
system($KHJConfig::tCommand,"update",$message);
#print $message;
}
}
run_tempHotJava();