Created
May 21, 2011 16:22
-
-
Save toritori0318/984651 to your computer and use it in GitHub Desktop.
東京の放射線量をIRCに流す
This file contains hidden or 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
use strict; | |
use warnings; | |
use Encode qw /encode_utf8 decode_utf8 from_to/; | |
use AnyEvent::Cron; | |
use AnyEvent::IRC::Client; | |
use Data::Dumper; | |
use Web::Scraper; | |
use URI; | |
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:second) | |
$cron->add("*:20:*" => \&main)->run(); | |
my $irc = 1; # 1:send irc / 0:send stdout | |
my $server = "192.168.0.xx"; | |
my $port = 6667; | |
my $channel = "#radiation"; | |
my $user = "radiation_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_date = ''; | |
sub main { | |
$DB::single=1; | |
my $result = &scrape(); | |
if($result && $result->{date}) { | |
if($save_date ne $result->{date}) { | |
&display($result); | |
} | |
$save_date = $result->{date}; | |
}else{ | |
warn "Can't scrape\n"; | |
} | |
} | |
sub scrape { | |
#my $file = \do { my $file = "hourly_data.html"; open my $fh, $file or die "$file: $!"; join '', <$fh> }; | |
my $uri = URI->new("http://ftp.jaist.ac.jp/pub/emergency/monitoring.tokyo-eiken.go.jp/monitoring/hourly_data.html"); | |
my $scraper = scraper { | |
process '/html/body/div/table[4]/tr[3]', 'row' => scraper { | |
process '//td[1]', 'date' => 'TEXT'; | |
process '//td[2]', 'max' => 'TEXT'; | |
process '//td[3]', 'min' => 'TEXT'; | |
process '//td[4]', 'avg' => 'TEXT'; | |
}; | |
}; | |
my $result = $scraper->scrape($uri); | |
return $result->{row} if $result; | |
} | |
sub display { | |
my $result = shift; | |
my $date = (utf8::is_utf8($result->{date})) ? encode_utf8($result->{date}) : $result->{date} ; | |
my $max = $result->{max}; | |
my $min = $result->{min}; | |
my $avg = $result->{avg}; | |
sendd( '[測定場所:東京都新宿区百人町] ' . $date); | |
sendd( "線量率 max : $max min : $min avg : $avg (平常時平均値:0.0350)"); | |
} | |
sub sendd { | |
my $str = shift; | |
if($irc){ | |
from_to($str,"utf8","iso-2022-jp"); | |
$pc->send_chan( $channel, "NOTICE", $channel, $str); | |
}else{ | |
say $str; | |
} | |
} | |
AE::cv->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment