Created
October 19, 2012 02:02
-
-
Save cho45/3915857 to your computer and use it in GitHub Desktop.
au lte recent usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use utf8; | |
use strict; | |
use warnings; | |
use WWW::Mechanize; | |
use Encode; | |
use Config::Pit; | |
use HTML::TreeBuilder::XPath; | |
use DateTime; | |
use Email::MIME; | |
use Email::Send; | |
use Data::Section::Simple qw(get_data_section); | |
use Text::Xslate; | |
use constant TO => 'foobar@example.com'; | |
my $config = pit_get("cs.kddi.com", require => { | |
"username" => "your username on example", | |
"password" => "your password on example" | |
}); | |
my $usage = []; | |
do { | |
my $mech = WWW::Mechanize->new; | |
$mech->agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1"); | |
$mech->get('https://cs.kddi.com/support/login.html'); | |
$mech->submit_form( | |
form_name => 'auLogin', | |
fields => { | |
UserID => $config->{username}, | |
Password => $config->{password}, | |
} | |
); | |
$mech->submit_form( form_name => 'recentcharge'); | |
$mech->submit_form( form_name => 'formlteKiloBytesu'); | |
my $html = decode('Shift_JIS', $mech->res->content); | |
my $doc = HTML::TreeBuilder->new_from_content($html); | |
my $inner = $doc->findnodes('//div[@id="innerBox"]')->[0] or die "failed to find innerBox"; | |
my $rows = $inner->findnodes('h4[text() = "日毎の内訳"]/following-sibling::div[@class="row"]'); | |
for my $row (@$rows) { | |
my ($year, $month, $day, $kilobytes) = $row->findvalue('.') =~ m{\((\d\d\d\d)/(\d\d)/(\d\d)\)[\s\S]*?([\d,]+)\s*キロバイト}; | |
$kilobytes =~ s{,}{}g; | |
push @$usage, +{ | |
date => DateTime->new(year => $year, month => $month, day => $day, time_zone => 'Asia/Tokyo'), | |
kilobytes => $kilobytes + 0, | |
}; | |
} | |
$doc->delete; | |
}; | |
my $body = Text::Xslate->new( | |
syntax => 'TTerse', | |
function => { | |
format_bytes => sub { | |
my ($bytes) = shift; | |
if ($bytes > 1024 ** 3) { | |
sprintf('%.2fGB', $bytes / (1024 ** 3)); | |
} elsif ($bytes > 1024 ** 2) { | |
sprintf('%.2fMB', $bytes / (1024 ** 2)); | |
} elsif ($bytes > 1024 ** 1) { | |
sprintf('%.2fKB', $bytes / (1024 ** 1)); | |
} else { | |
sprintf('%dB', $bytes); | |
} | |
} | |
}, | |
)->render_string(get_data_section('body'), { usage => $usage }); | |
my $mail = Email::MIME->create( | |
header => [ | |
From => TO(), | |
To => TO(), | |
Subject => '[daily] au LTE usage', | |
], | |
attributes => { | |
content_type => 'text/plain; charset=utf-8', | |
}, | |
body => encode_utf8 $body, | |
); | |
if (shift eq 'send') { | |
my $sender = Email::Send->new({ mailer => 'SMTP' }); | |
$sender->send($mail); | |
} else { | |
print $mail->as_string; | |
} | |
__DATA__ | |
@@ body | |
[% FOR u IN usage %] | |
[% u.date.strftime('%Y/%m/%d') %]: [% u.kilobytes %]Kb ([% (u.kilobytes * 1024) | format_bytes %]) | |
[% END %] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment