-
-
Save ha1t/286500 to your computer and use it in GitHub Desktop.
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 ruby | |
$LOAD_PATH << 'lib' | |
$LOAD_PATH << '../lib' | |
require 'optparse' | |
require 'uri' | |
require 'rubygems' | |
require 'daemons' | |
require 'net/irc' | |
require 'mechanize' | |
require 'nokogiri' | |
Version = '0.1.0' | |
=begin | |
= mixi_voice_bot.rb | |
== IRC チャンネルに JOIN して mixi ボイスの閲覧・投稿・返信・削除を行う Bot | |
Authors:: Tomohiro, TAIRA <[email protected]> | |
Version:: 0.0.1 | |
Copyright:: Copyright (C) Tomohiro, TAIRA, 2009. All rights reserved. | |
URL:: http://github.com/Tomohiro | |
== 参考 | |
http://github.com/cho45/net-irc/blob/master/examples/echo_bot.rb | |
== 起動例 | |
(1) 通常の起動 | |
* $ ./mixi_voice_bot.rb -h irc.example.com -c "#mixi_example" -n MyNick -E [email protected] -P your_mixi_password | |
(2) デーモンとして起動 | |
* $ ./mixi_voice_bot.rb -h irc.example.com -c "#mixi_example" -n MyNick -E [email protected] -P your_mixi_password -D | |
(3) 起動オプション | |
* $ ./mixi_voice_bot.rb --help | |
Usage: mixi_voice_bot [options] | |
-h, --irc-host HOST 接続先の IRC サーバ名 | |
-p, --irc-port [PORT=6667] 接続先の IRC ポート番号 (規定は 6667) | |
-b, --bot-name [BOT=voice] mixi ボイスを発言する bot の名前 (規定は voice) | |
-c, --channel CHANNEL 接続先のチャンネル名 | |
-n, --nickname NICK ボイスへの投稿を許す IRC のニックネーム | |
-t, --crwal-time [SEC=60] mixi ボイスをクロールする間隔 (規定は 60秒) | |
-E, --email EMAIL mixi のログイン Email アドレス | |
-P, --password PASSWORD mixi のログインパスワード | |
-D, --daemonize プロセスをデーモン化する | |
== 使用方法 | |
(1) mixi ボイス Bot と同じチャンネルに JOIN する | |
(2) 閲覧: [ニックネーム] メッセージ (KEY) の形式で表示される | |
(3) 投稿: 発言する | |
(4) 返信: 以下のフォーマットで発言する | |
* re KEY メッセージ | |
(5) 削除: 以下のフォーマットで発言する | |
* rm KEY | |
=end | |
class MixiVoiceBot < Net::IRC::Client | |
MIXI_LOGIN_URI = 'http://mixi.jp' | |
RECENT_VOICE_URI = 'http://mixi.jp/recent_voice.pl' | |
def initialize | |
setup_options | |
super(@irc_host, @irc_port, { | |
:nick => @bot_name, | |
:user => @bot_name, | |
:real => @bot_name, | |
:channel => @channel, | |
}) | |
@agent = WWW::Mechanize.new | |
if ENV['http_proxy'] | |
proxy = URI.parse(ENV['http_proxy']) | |
@agent.set_proxy(proxy.host, proxy.port) | |
end | |
@caches = [] | |
@identity = nil | |
end | |
def setup_options | |
@irc_host = 'localhost' | |
@irc_port = 6969 | |
@bot_name = 'mxvoice' | |
@crawl_time = 30 | |
ARGV.options do |o| | |
o.on('-h', '--irc-host HOST', '接続先の IRC サーバ名') { |v| @irc_host = v } | |
o.on('-p', "--irc-port [PORT=#{@irc_port}]", '接続先の IRC ポート番号 (規定は 6667)') { |v| @irc_port = v } | |
o.on('-b', "--bot-name [BOT=#{@bot_name}]", 'mixi ボイスを発言する bot の名前 (規定は voice)') { |v| @bot_name = v } | |
o.on('-c', '--channel CHANNEL', '接続先のチャンネル名') { |v| @channel = v } | |
o.on('-n', '--nickname NICK', 'ボイスへの投稿を許す IRC のニックネーム') { |v| @nickname = v } | |
o.on('-t', "--crwal-time [SEC=#{@crawl_time}]", 'mixi ボイスをクロールする間隔 (規定は 60秒)') { |v| @crawl_time = v } | |
o.on('-E', '--email EMAIL', 'mixi のログイン Email アドレス') { |v| @email = v } | |
o.on('-P', '--password PASSWORD', 'mixi のログインパスワード') { |v| @password = v } | |
o.on('-D', '--daemonize', 'プロセスをデーモン化する') { |v| Daemons.daemonize } | |
o.parse! | |
end | |
end | |
def on_rpl_welcome(m) | |
post JOIN, @chennel | |
login_mixi | |
@identity = get_identity | |
get_thread = Thread.new do | |
loop do | |
get | |
sleep @crawl_time | |
end | |
end | |
get_thread.join | |
end | |
def on_privmsg(message) | |
if message.prefix =~ Regexp.new(@nickname) | |
case message[1] | |
when /^re ([0-9]+) (.+)/ | |
reply($1, $2) | |
when /^rm ([0-9]+)/ | |
delete($1) | |
else | |
add(message[1]) | |
end | |
get | |
end | |
end | |
def login_mixi | |
@agent.get MIXI_LOGIN_URI do |login_page| | |
login_page.form 'login_form' do |form| | |
form.email = @email | |
form.password = @password | |
end.submit | |
end | |
end | |
def add(voice) | |
@agent.get RECENT_VOICE_URI do |post_page| | |
post_page.form_with(:action => 'add_voice.pl') do |form| | |
form.body = voice | |
end.submit | |
end | |
end | |
def reply(key, voice) | |
if @caches.has_key? key | |
member_id = @caches[key][:member_id] | |
post_time = @caches[key][:post_time] | |
@agent.get RECENT_VOICE_URI do |post_page| | |
post_page.form_with(:action => '/add_voice.pl') do |form| | |
form.body = voice | |
form.parent_member_id = member_id | |
form.parent_post_time = post_time | |
end.submit | |
end | |
else | |
post(NOTICE, @opts.channel, '指定された返信先が見つかりません') | |
end | |
end | |
def delete(post_time) | |
@agent.post "http://mixi.jp/delete_voice.pl?post_time=#{post_time}&post_key=#{@identity}&redirect=recent_voice" | |
@caches = crawl_recent_voice | |
end | |
def get | |
p '-- START GET --' | |
voices = crawl_recent_voice | |
voices.sort.each do |key, voice| | |
if @caches.empty? or [email protected]_key? key | |
post(PRIVMSG, @opts.channel, "[#{voice[:nickname]}]#{voice[:reply]} #{voice[:comment]} (#{key})") | |
sleep 5 | |
end | |
end | |
@caches = voices | |
end | |
def get_identity | |
recent_page = @agent.get RECENT_VOICE_URI | |
identity = (Nokogiri::HTML(recent_page.body)/'input#post_key').first['value'] | |
end | |
def crawl_recent_voice | |
p '-- START crawl_recent_voice --' | |
recent_page = @agent.get RECENT_VOICE_URI | |
voices = {} | |
p '-- START crawl_recent_voice PARSE --' | |
html = Nokogiri(recent_page.body) | |
html.search("ul li.archive").each do |comment| | |
p '-- START COMMENT --' | |
key = comment.at('input.postTime') | |
voices[key] = { | |
:member_id => comment.at('input.memberId').text, | |
:post_time => comment.at('input.postTime').text, | |
:nickname => comment.at('input.nickname').text, | |
#:reply => ((' ' + comment.at('a').text) if comment.at('a').text =~ /^>/), | |
:reply => '', | |
:comment => comment.at('div.voiced').text | |
} | |
end | |
p '-- END crawl_recent_voice PARSE --' | |
voices | |
end | |
def timestamp(comment) | |
comment.at('div.echo_post_time').text | |
end | |
end | |
#MixiVoiceBot.new('localhost', '6663', { | |
# :nick => "voice", | |
# :user => "voice", | |
# :real => "voice", | |
#}).start | |
MixiVoiceBot.new.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
普通にServerとして実装しなおしたほうが良い気がしてきたので途中でやる気がなくなった…