機能: ircで誰かがURLを発言すると、それのhtmlを解析してtitleタグ部分をnoticeします。
botの定番機能ですよね!nadokaさんに同様のbotが添付されてたけどうまくうごかなかったので。勉強がてら書いた。
あ、mechanizeを使ってるので、無い人は gem install mechanize してください。
ruby-1.9.3p194
nadoka-0.8.2
mechanize-2.5.1
urltitlebot.nb
require 'uri'
require 'mechanize'
require 'kconv'
class UrlTitleBot < Nadoka::NDK_Bot
def on_privmsg prefix, ch, msg
if msg.include?("http:")
t = Thread.new do
urilist = URI.extract(msg, %w[http])
uri = URI.parse(urilist[0])
case uri.host
when /localhost/, /\A127\./, /\A192\.168\./, /\A10\./
@logger.slog "urltitlebot: local address"
else
agent = Mechanize.new
agent.user_agent_alias = 'Windows IE 7'
agent.read_timeout = 5 # sec
begin
page = agent.get(uri)
rescue TimeoutError,
Errno::ETIMEDOUT,
Errno::EINVAL,
Errno::ECONNRESET,
Errno::EHOSTUNREACH,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
Mechanize::SocketError,
EOFError
@logger.slog "urltitlebot: error"
rescue Mechanize::ResponseCodeError => ec
@logger.slog "urltitlebot: #{ec.response_code} error"
else
begin
noticemsg = page.title.gsub(/\n/,"").strip.gsub(/ +/,' ').tojis
send_notice(ch, noticemsg)
rescue NoMethodError
end
end
end #case
end #thread
end #if
end #def
end #class
これも習作なので、ツッコミ希望いたすよ。rescueしきれてないエラーがあったりして。スマートじゃない動かし方をしている気もするので。htmlかどうかはmime-typeで判断したほうが賢いよなぁ、とか。
bot作ってばっかりじゃRubyどころかプログラミングの基本が習得できないぜ。
Currently it sounds like Movable Type is the preferred blogging platform out there right now.
返信削除(from what I've read) Is that what you are using on your blog?
Also visit my homepage - Louis Vuitton Outlet