Created
August 28, 2012 14:38
-
-
Save xtetsuji/3498576 to your computer and use it in GitHub Desktop.
Display Aterm WM3600R remaining battery charge.
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Encode; | |
use LWP::UserAgent; | |
use Web::Scraper; | |
binmode STDOUT, ':utf8'; | |
### <Config> | |
my $aterm_wm3600r_host = '192.168.0.1'; | |
my $basic_username = 'CUSTOM YOUR BASIC AUTH USERANME'; | |
my $basic_password = 'CUSTOM YOUR BASIC AUTH PASSWORD'; | |
### </Config> | |
$basic_username = $ENV{ATERM_USERNAME} if $ENV{ATERM_USERNAME}; | |
$basic_password = $ENV{ATERM_PASSWORD} if $ENV{ATERM_PASSWORD}; | |
if ( $basic_username =~ /\s/ || $basic_password =~ /\s/ ) { | |
die "change your information on source code.\nsee perldoc $0\n"; | |
} | |
my $uri = "http://${aterm_wm3600r_host}/index.cgi/eco_mode_main"; | |
my $realm = 'Aterm(admin)'; | |
my $ua = LWP::UserAgent->new; | |
$ua->credentials( $aterm_wm3600r_host . ":80", $realm, $basic_username, $basic_password ); | |
my $res = $ua->get($uri); | |
die $res->as_string if $res->is_error; | |
my $percent_scraper = scraper { | |
process "td.small_item_td2", content => 'TEXT'; | |
}; | |
my $td = $percent_scraper->scrape($res->content()); | |
my $td_content = decode('euc-jp', $td->{content}); | |
if ( $td_content =~ /((\d+)\%)/ ) { | |
print "$1\%\n"; | |
} | |
elsif ( $td_content =~ /\Q(充電中)/ ) { | |
my $per_count = $td_content =~ tr/■/■/; | |
print "充電中: 約" . ($per_count * 10) . "%\n" | |
} | |
else { | |
print "取得エラー\n"; | |
} | |
__END__ | |
=pod | |
=head1 NAME | |
aterm-wm3600r-battery - Display Aterm WM3600R remaining battery charge. | |
=head1 SYNOPSIS | |
#change in source code | |
### <Config> | |
my $aterm_wm3600r_host = 'CUSTOM YOUR ATERM IP ADDRESS (GATEWAY)'; | |
my $basic_username = 'CUSTOM YOUR BASIC AUTH USERANME'; | |
my $basic_password = 'CUSTOM YOUR BASIC AUTH PASSWORD'; | |
### </Config> | |
# execute (no command line option yet) | |
aterm-wm3600r-battery | |
=head1 REQUIREMENT | |
=over | |
=item * | |
Aterm WM3600 Firmware, version higher 1.2.0 | |
=item * | |
Web::Scraper | |
=item * | |
LWP::UserAgent (libwww-perl) | |
=back | |
=head1 AUTHOR | |
OGATA Tetsuji E<lt>tetsuji.ogata {at} gmail.comE<gt>. | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment