ã¯ããã¦ã®ircbot
ã¾ã£ããã¨ãããã£ãã
ircdãåããã¦æ¥ã ã®ä½æ°ãªããã£ããããã£ã¦ããããæ¥ï¼
urléã£ãããã®ãã¼ã¸ã®ã¿ã¤ãã«ãè¿ãã¦ããããããã欲ãããªã
ã¨ãã話ãã§ã¦ãã¾ããï¼ããã¯POEã使ã£ã¦ã¿ããã£ã³ã¹ï¼ã£ã¦ãã¨ã§ircbotä½ãã«ææ¦ãããã¨ã«ãã¾ããï¼
ãµã³ãã«
POE::Compoennt::IRCã«ã¯ããã¤ãサンプルãã¤ãã¦ãããï¼podã«ããµã³ãã«ãã¤ãã¦ããã®ã§ããï¼ä¸çªç°¡åãªã®ãPOE Cookbookã ã£ãã®ã§ï¼ããããã¼ã¹ã«ãã¦ã¿ã¾ããï¼ãããã¨ã¯ç°¡åã§ï¼å ã®ã¹ã¯ãªããã®
if ( my ($rot13) = $msg =~ /^rot13 (.+)/ ) { $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
ã¨ãªã£ã¦ããé¨åãï¼urlãåãåã£ãããã®ã¿ã¤ãã«ãè¿ãããã«æ¸ãç´ãã°ããããã§ãï¼ãããå®ç¾ããããã«è¦ã¤ããã®ãHTML::TagParserã¢ã¸ã¥ã¼ã«ã§ããï¼ããã使ãã¨ï¼
my $html = HTML::TagParser->new( "index-j.html" ); my $elem = $html->getElementsByTagName( "title" ); print "<title>", $elem->innerText(), "</title>\n" if ref $elem;
ã¨ããã ãã§ï¼æå®ããhttp urlããã¿ã¤ãã«ãæãåºããã¨ãã§ãã¾ãï¼ããã«ãããããã¨ã«ã¯ï¼
This module natively understands the character encoding used in document by parsing its meta element.The parsed document's encoding is converted as this class's fixed internal encoding "UTF-8".
ã¨ãããã¨ãªã®ã§æåã³ã¼ãå¤æã¾ã§ãã¾ããã§ãã£ã¦ããã¦ãã¾ãï¼å¾ã¯ãµã¼ãã¸ã®æ¸ãè¾¼ã¿æç¹ã§ï¼ãµã¼ãæå®ã®iso-2022-jpã«å¤æãã¦ããã°OKï¼ãããªãããªã§ã¹ã¯ãªããã¯ãããªæãã«ãªãã¾ããï¼
title.pl
#!/usr/local/bin/perl use POE; use POE::Component::IRC; use HTML::TagParser; use Encode qw(from_to); use strict; my ( $url, $channel, $irc ); $url = q{s?https?://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+}; $channel = '#test'; $irc = POE::Component::IRC->spawn(); POE::Session->create( inline_states => { _start => \&bot_start, irc_001 => \&on_connect, irc_public => \&on_public, }, ); sub bot_start { my $kernel = $_[KERNEL]; my $heap = $_[HEAP]; my $session = $_[SESSION]; $irc->yield( register => 'all' ); my $nick = 'poe0'; $irc->yield( connect => { Nick => $nick, Username => 'cookbot', Ircname => 'POE::Component::IRC cookbook bot', Server => 'irc.hoge.co.jp', Port => '6667', } ); } sub on_connect { $irc->yield( join => $channel ); } sub on_public { my ($msg) = @_[ARG2]; if ( my ($http) = $msg =~ m/^title ($url)/ ) { my $title; eval { my $html = HTML::TagParser->new($http); my $elem = $html->getElementsByTagName('title'); $title = $elem->innerText(); from_to( $title, 'utf-8', 'iso-2022-jp' ); }; $title = 'Error to get Title.' if ($@); $irc->yield( privmsg => $channel, $title ); } } $poe_kernel->run(); exit 0;
ã¡ãã£ã¨ãã解説
http URLãæ£è¦è¡¨ç¾ã§æ¸ãã¦ã¿ãæ¹æ³ã¯Perlメモã詳ããã§ãï¼ããã§ã¯ï¼åç´ãªå ´åã使ã£ã¦ãã¾ãï¼
$url = q{s?https?://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+};
ã£ã¦ãããï¼è¤éãªã±ã¼ã¹ã使ãæ°åã¯ããã¾ããªãã§ããï¼ç¬
ããããï¼ã¿ã¤ãã«ãæãåºãé¨åã®ã¹ã¯ãªããã以ä¸ã®ããã«ãªã£ã¦ãã¾ãï¼æåã³ã¼ãã¯HTML::TagParserä¸ã§utf8ã«ãªã£ã¦ããã®ã§ï¼ãããfrom_toã使ã£ã¦jisã³ã¼ãã«å¤æãã¦ãã¾ãï¼
eval { my $html = HTML::TagParser->new($http); my $elem = $html->getElementsByTagName('title'); $title = $elem->innerText(); from_to( $title, 'utf-8', 'iso-2022-jp' ); }; $title = 'Error to get Title.' if ($@);