Skip to content

Instantly share code, notes, and snippets.

@nkmrgk
Created April 27, 2011 07:33
Show Gist options
  • Save nkmrgk/943855 to your computer and use it in GitHub Desktop.
Save nkmrgk/943855 to your computer and use it in GitHub Desktop.
tepco-epf.pl for developer.yahoo.co.jp
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use File::HomeDir;
use File::Find::Rule;
use WebService::Simple;
my $appid = 'your-appid-of-developer.yahoo.co.jp';
my $yahooapi = 'http://setsuden.yahooapis.jp/v1/Setsuden/electricPowerForecast';
sub api {
my $uri = shift;
my $service = WebService::Simple->new(
base_url => $uri,
response_parser => 'JSON',
);
my $res = $service->get({
appid => $appid,
output => 'json'
});
my $ref = $res->parse_response;
die $ref->{Error}->{Message} if ($ref->{Error});
return $ref;
}
sub mkdir_on_desktop {
my ($dirname, $app) = @_;
my $desktop = File::HomeDir->my_desktop;
my @dirs = File::Find::Rule->name('*'.$app)->maxdepth(1)->in($desktop);
my $oldpath = ($dirs[0] and -d $dirs[0]) ? $dirs[0] :
File::Spec->catfile($desktop, $dirname);
my $newpath = File::Spec->catfile($desktop, $dirname);
mkdir $newpath if (!-d $oldpath);
rename $oldpath, $newpath if (-d $oldpath);
return $newpath;
}
sub main {
my $app = basename($0, '.pl');
my $epf = api($yahooapi);
my @forecasts;
my $current_hour = substr($epf->{ElectricPowerForecasts}->{UpdateTime}, 11, 2);
my $current_epu;
my $current_capa;
foreach my $fc (@{$epf->{ElectricPowerForecasts}->{Forecast}}) {
my $epu = int(($fc->{Usage}->{'$'} / $fc->{Capacity}->{'$'}) * 100);
if ($fc->{Hour} == $current_hour) {
$current_epu = $epu;
$current_capa = sprintf '[%d of %dkW]',
$fc->{Usage}->{'$'} / 10000,
$fc->{Capacity}->{'$'} / 10000;
push @forecasts, $epu . '%';
next;
}
next if (!$current_epu);
my $diff = $epu - $current_epu;
$diff = '+' . $diff if ($diff > 0);
push @forecasts, $diff . '%';
#push @forecasts, '['.$fc->{Hour}.':00', "$epu%", ($fc->{Usage}->{'$'}/10000).'kW]';
}
my $dirname = "@forecasts $current_capa $app";
warn "$dirname\n";
my $path = mkdir_on_desktop($dirname, $app);
# logging
if (0) {
use Data::Dumper;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Sortkeys = 1;
local $Data::Dumper::Terse = 1;
open my $fh, '>>', File::Spec->catfile($path, "$app.txt") or die;
print $fh join "\t", (scalar(localtime(time)), $dirname . "\n");
print $fh Dumper($epf);
close $fh;
}
}
&main;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment