Skip to content

Instantly share code, notes, and snippets.

@toritori0318
Created April 2, 2011 15:12
Show Gist options
  • Save toritori0318/899557 to your computer and use it in GitHub Desktop.
Save toritori0318/899557 to your computer and use it in GitHub Desktop.
電力情報をIRCに流す
use strict;
use warnings;
use Furl;
use JSON;
use Encode qw /encode decode_utf8/;
use AnyEvent::Cron;
use AnyEvent::IRC::Client;
use Data::Dumper;
use DateTime::Format::Strptime;
use 5.10.0;
# cron
my $cron = AnyEvent::Cron->new(
# verbose => 1,
# debug => 1,
after => 1,
interval => 1,
ignore_floating => 1
);
# 00:00 (hour:minute)
$cron->add("*:*:10" => \&get_api)->run();
my $irc = 1; # 1:send irc / 0:send stdout
my $server = "192.168.0.xx";
my $port = 6667;
my $channel = "#tepco";
my $user = "tepco_bot";
# irc bot
my $pc = AnyEvent::IRC::Client->new;
$pc->reg_cb(
connect => sub {
my ( $pc, $err ) = @_;
if ( defined $err ) {
print "Couldn't connect to server: $err\n";
}
},
registered => sub {
my ($self) = @_;
print "registered!\n";
$pc->enable_ping(60);
},
disconnect => sub {
print "disconnected: $_[1]!\n";
}
);
$pc->send_srv( "JOIN", $channel );
$pc->send_chan( $channel, "NOTICE", $channel, "hello" );
$pc->connect( $server, $port,
{ nick => $user, user => $user, real => $user } );
my $save_capacity_updated = '';
my $save_usage_updated = '';
sub get_api {
my $url = 'http://tepco-usage-api.appspot.com/latest.json';
my $furl = Furl->new();
my $res = $furl->get($url);
unless ($res->is_success) {
warn "Can't download $url\n";
return;
}
my $result = JSON->new->utf8(0)->decode(decode_utf8($res->content));
if ($result->{Error}) {
warn encode('utf-8', $result->{Error}{Message}), "\n";
} else {
if($save_usage_updated ne $result->{usage_updated}
|| $save_capacity_updated ne $result->{capacity_updated} ) {
display($result);
}
$save_usage_updated = $result->{usage_updated};
$save_capacity_updated = $result->{capacity_updated};
}
}
sub display {
my $result = shift;
my ($year,$month,$day,$hour) = ($result->{year},$result->{month},$result->{day},$result->{hour});
my ($capacity,$usage) = ($result->{capacity},$result->{usage});
my ($capacity_updated,$usage_updated) = ($result->{capacity_updated},$result->{usage_updated});
my ($saving) = ($result->{saving});
sendd( '');
sendd( '時刻:' . $year . '/' . $month . '/' . $day . ' ' . $hour . '時台');
sendd( '消費電力データ更新日時:' . convert_timezone($usage_updated));
sendd( '供給可能最大電力決定日時:' . convert_timezone($capacity_updated));
sendd( '使用率 : ' . sprintf("%3.2f", ($usage/$capacity) * 100));
if($saving) {
sendd( '計画停電あり');
} else {
sendd( '計画停電なし');
}
}
sub sendd {
my $str = shift;
if($irc){
Encode::from_to($str,"utf8","iso-2022-jp");
$pc->send_chan( $channel, "NOTICE", $channel, $str);
}else{
say $str;
}
}
my $strp = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S'
);
sub convert_timezone {
my $date = shift;
return '' unless $date;
my $dt = $strp->parse_datetime($date);
$dt->add( hours => 9 );
return $dt->strftime('%Y-%m-%d %H:%M:%S');
}
my $cv = AnyEvent->condvar;
$cv->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment