Created
October 26, 2009 13:11
-
-
Save tomohiro/218629 to your computer and use it in GitHub Desktop.
Revisions
-
Tomohiro, TAIRA revised this gist
Jan 6, 2010 . 1 changed file with 11 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -107,7 +107,7 @@ def setup_options end def start login @identity = get_identity @socket = TCPSocket.open(@host, @port) @@ -128,7 +128,7 @@ def start get_thread = Thread.new do loop do get sleep @crawl_time end end @@ -145,17 +145,17 @@ 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 @agent.get MIXI_LOGIN_URI do |login_page| login_page.form 'login_form' do |form| form.email = @email @@ -164,15 +164,15 @@ def mixi_login end end def add(voice) @agent.get RECENT_VOICE_URI do |post_page| post_page.form_with(:action => 'add_echo.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] @@ -189,12 +189,12 @@ def reply_voice(key, voice) end end def delete(post_time) @agent.post "http://mixi.jp/delete_echo.pl?post_time=#{post_time}&post_key=#{@identity}&redirect=recent_echo" @caches = crawl_recent_voice end def get voices = crawl_recent_voice voices.sort.each do |key, voice| if @caches.empty? or !@caches.has_key? key -
Tomohiro, TAIRA revised this gist
Nov 4, 2009 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,11 +4,11 @@ $LOAD_PATH << '../lib' require 'optparse' require 'uri' require 'rubygems' require 'daemons' require 'net/irc' require 'mechanize' require 'nokogiri' @@ -199,7 +199,7 @@ def get_voice voices.sort.each do |key, voice| if @caches.empty? or !@caches.has_key? key post(PRIVMSG, @opts.channel, "[#{voice[:nickname]}]#{voice[:reply]} #{voice[:comment]} (#{key})") sleep 5 end end @caches = voices -
Tomohiro, TAIRA revised this gist
Oct 30, 2009 . 1 changed file with 49 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,6 +14,55 @@ Version = '0.0.1' =begin = mixi_voice_bot.rb == IRC チャンネルに JOIN して mixi ボイスの閲覧・投稿・返信・削除を行う Bot Authors:: Tomohiro, TAIRA <tomohiro.t@gmail.com> Version:: 0.0.1 Copyright:: Copyright (C) Tomohiro, TAIRA, 2009. All rights reserved. URL:: https://github.com/Tomohiro == 参考 https://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 your_account@example.com -P your_mixi_password (2) デーモンとして起動 * $ ./mixi_voice_bot.rb -h irc.example.com -c "#mixi_example" -n MyNick -E your_account@example.com -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' -
Tomohiro, TAIRA revised this gist
Oct 30, 2009 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -20,8 +20,6 @@ class MixiVoiceBot < Net::IRC::Client RECENT_VOICE_URI = 'http://mixi.jp/recent_echo.pl' def initialize setup_options super(@irc_host, @irc_port, { :nick => @bot_name, @@ -54,6 +52,7 @@ def setup_options 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 -
Tomohiro, TAIRA revised this gist
Oct 30, 2009 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,9 @@ $LOAD_PATH << '../lib' require 'optparse' require 'rubygems' require 'daemons' require 'net/irc' require 'uri' require 'mechanize' @@ -18,8 +20,9 @@ class MixiVoiceBot < Net::IRC::Client RECENT_VOICE_URI = 'http://mixi.jp/recent_echo.pl' def initialize Daemons.daemonize setup_options super(@irc_host, @irc_port, { :nick => @bot_name, :user => @bot_name, @@ -38,8 +41,8 @@ def initialize end def setup_options @irc_port = 6667 @bot_name = 'voice' @crawl_time = 60 ARGV.options do |o| -
Tomohiro, TAIRA revised this gist
Oct 29, 2009 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ require 'mechanize' require 'nokogiri' Version = '0.0.1' class MixiVoiceBot < Net::IRC::Client @@ -31,11 +31,11 @@ def initialize 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_port = 6667 @@ -44,11 +44,11 @@ def setup_options 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.parse! @@ -72,7 +72,7 @@ def start puts message name = "on_#{(COMMANDS[message.command.upcase] || message.command).downcase}" send(name, message) if respond_to? name end end get_thread = Thread.new do -
Tomohiro, TAIRA revised this gist
Oct 29, 2009 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -46,7 +46,7 @@ def setup_options o.on('-h', '--irc-host HOST', '接続先の IRC サーバ名') { |v| @irc_host = v } o.on('-p', '--irc-port PORT', '接続先の IRC ポート番号 (規定は 6667)') { |v| @irc_port = v } o.on('-b', '--bot-name BOT', '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', 'mixi ボイスをクロールする間隔 (規定は 60秒)') { |v| @crawl_time = v } o.on('-E', '--email EMAIL', 'mixi のログイン Email アドレス') { |v| @email = v } -
Tomohiro, TAIRA revised this gist
Oct 29, 2009 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
Empty file. -
Tomohiro, TAIRA revised this gist
Oct 29, 2009 . 1 changed file with 54 additions and 35 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,76 +1,95 @@ #!/usr/bin/env ruby $LOAD_PATH << 'lib' $LOAD_PATH << '../lib' require 'optparse' require 'rubygems' require 'net/irc' require 'uri' require 'mechanize' require 'nokogiri' Version="0.1.0" class MixiVoiceBot < Net::IRC::Client MIXI_LOGIN_URI = 'http://mixi.jp' RECENT_VOICE_URI = 'http://mixi.jp/recent_echo.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_port = 6667 @bot_name = 'voice' @crawl_time = 60 ARGV.options do |o| o.on('-h', '--irc-host HOST', '接続先の IRC サーバ名') { |v| @irc_host = v } o.on('-p', '--irc-port PORT', '接続先の IRC ポート番号 (規定は 6667)') { |v| @irc_port = v } o.on('-b', '--bot-name BOT', 'mixi ボイスを発言する bot の名前 (規定は voice)') { |v| @bot_name = v } o.on('-c', '--channel-name CHANNEL', '接続先のチャンネル名') { |v| @channel = v } o.on('-n', '--nickname NICK', 'ボイスへの投稿を許す IRC のニックネーム') { |v| @nickname = v } o.on('-t', '--crwal-time SEC', '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.parse! end end def start mixi_login @identity = get_identity @socket = TCPSocket.open(@host, @port) @socket.gets post(NICK, @opts.nick) post(USER, @opts.user, '0', '*', @opts.real) post(JOIN, @opts.channel) post_thread = Thread.new do while line = @socket.gets message = Message.parse(line) puts message name = "on_#{(COMMANDS[message.command.upcase] || message.command).downcase}" send(name, message) if respond_to? name end end get_thread = Thread.new do loop do get_voice sleep @crawl_time end end post_thread.join get_thread.join rescue IOError => e @log.error 'IOError' + e.to_s ensure finish end def on_privmsg(message) if message.prefix =~ Regexp.new(@nickname) case message[1] @@ -84,18 +103,18 @@ def on_privmsg(message) get_voice end end def mixi_login @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 post_voice(voice) @agent.get RECENT_VOICE_URI do |post_page| post_page.form_with(:action => 'add_echo.pl') do |form| form.body = voice end.submit @@ -107,7 +126,7 @@ def reply_voice(key, voice) 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_echo.pl') do |form| form.body = voice form.parent_member_id = member_id @@ -118,7 +137,7 @@ def reply_voice(key, voice) post(NOTICE, @opts.channel, '指定された返信先が見つかりません') end end def remove_voice(post_time) @agent.post "http://mixi.jp/delete_echo.pl?post_time=#{post_time}&post_key=#{@identity}&redirect=recent_echo" @caches = crawl_recent_voice @@ -134,16 +153,16 @@ def get_voice 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 recent_page = @agent.get RECENT_VOICE_URI voices = {} (Nokogiri::HTML(recent_page.body)/'td.comment').each do |comment| key = timestamp(comment) voices[key] = build_voice(comment) -
Tomohiro revised this gist
Oct 28, 2009 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ class MixiVoiceBot < Net::IRC::Client def initialize @irc_host = 'your_irc_host' @irc_port = '6667' @channel = '#your_channel' @bot_name = 'voice' @nickname = 'your_nickname' -
Tomohiro revised this gist
Oct 28, 2009 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ def initialize @irc_port = '6667' @channel = '#mixi' @bot_name = 'voice' @nickname = 'your_nickname' super(@irc_host, @irc_port, { :nick => @bot_name, -
Tomohiro, TAIRA revised this gist
Oct 28, 2009 . 1 changed file with 53 additions and 31 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,12 +15,12 @@ def initialize @irc_port = '6667' @channel = '#mixi' @bot_name = 'voice' @nickname = 'Tomohiro' super(@irc_host, @irc_port, { :nick => @bot_name, :user => @bot_name, :real => @bot_name, :channel => @channel, }) @@ -30,14 +30,16 @@ def initialize @agent.set_proxy(proxy.host, proxy.port) end @email = 'your_mixi_mail@example.com' @password = 'your_mixi_password' @caches = [] @identity = nil end def start mixi_login @identity = get_identity @socket = TCPSocket.open(@host, @port) @socket.gets @@ -55,7 +57,7 @@ def start end get_thread = Thread.new do loop do get_voice sleep 300 end @@ -72,8 +74,10 @@ def start def on_privmsg(message) if message.prefix =~ Regexp.new(@nickname) case message[1] when /^re ([0-9]+) (.+)/ reply_voice($1, $2) when /^rm ([0-9]+)/ remove_voice($1) else post_voice(message[1]) end @@ -98,41 +102,59 @@ def post_voice(voice) end end def reply_voice(key, voice) if @caches.has_key? key member_id = @caches[key][:member_id] post_time = @caches[key][:post_time] @agent.get 'http://mixi.jp/recent_echo.pl' do |post_page| post_page.form_with(:action => '/add_echo.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 remove_voice(post_time) @agent.post "http://mixi.jp/delete_echo.pl?post_time=#{post_time}&post_key=#{@identity}&redirect=recent_echo" @caches = crawl_recent_voice end def get_voice voices = crawl_recent_voice voices.sort.each do |key, voice| if @caches.empty? or !@caches.has_key? key post(PRIVMSG, @opts.channel, "[#{voice[:nickname]}]#{voice[:reply]} #{voice[:comment]} (#{key})") sleep 2 end end @caches = voices end def get_identity recent_page = @agent.get 'http://mixi.jp/recent_echo.pl' identity = (Nokogiri::HTML(recent_page.body)/'input#post_key').first['value'] end def crawl_recent_voice recent_page = @agent.get 'http://mixi.jp/recent_echo.pl' voices = {} (Nokogiri::HTML(recent_page.body)/'td.comment').each do |comment| key = timestamp(comment) voices[key] = build_voice(comment) end voices end def timestamp(comment) comment.at('div.echo_post_time').text end def build_voice(comment) { :member_id => comment.at('div.echo_member_id').text, @@ -143,5 +165,5 @@ def build_voice(comment) } end end MixiVoiceBot.new.start -
Tomohiro, TAIRA revised this gist
Oct 28, 2009 . 1 changed file with 56 additions and 38 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,120 +1,138 @@ #!/usr/bin/env ruby $LOAD_PATH << 'lib' $LOAD_PATH << '../lib' require 'rubygems' require 'net/irc' require 'uri' require 'mechanize' require 'nokogiri' class MixiVoiceBot < Net::IRC::Client def initialize @irc_host = 'your_irc_host' @irc_port = '6667' @channel = '#mixi' @bot_name = 'voice' @nickname = 'your_irc_nickname' 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 @email = 'your_mixi_mail' @password = 'your_mixi_password' @caches = [] end def start mixi_login @socket = TCPSocket.open(@host, @port) @socket.gets post(NICK, @opts.nick) post(USER, @opts.user, '0', '*', @opts.real) post(JOIN, @opts.channel) post_thread = Thread.new do while line = @socket.gets message = Message.parse(line) name = "on_#{(COMMANDS[message.command.upcase] || message.command).downcase}" send(name, message) if respond_to? name end end get_thread = Thread.new do while true get_voice sleep 300 end end post_thread.join get_thread.join rescue IOError => e @log.error 'IOError' + e.to_s ensure finish end def on_privmsg(message) if message.prefix =~ Regexp.new(@nickname) case message[1] when /^re/ reply_voice(message[1]) else post_voice(message[1]) end get_voice end end def mixi_login @agent.get 'http://mixi.jp' do |login_page| login_page.form 'login_form' do |form| form.email = @email form.password = @password end.submit end end def post_voice(voice) @agent.get 'http://mixi.jp/recent_echo.pl' do |post_page| post_page.form_with(:action => 'add_echo.pl') do |form| form.body = voice end.submit end end # TODO def reply_voice(voice) return @agent.get 'http://mixi.jp/recent_echo.pl' do |post_page| post_page.form_with(:action => '/add_echo.pl') do |form| form.body = voice.gsub(/^re/, '') form.parent_member_id = '' form.parent_post_time = '' end.submit end end def get_voice recent_page = @agent.get 'http://mixi.jp/recent_echo.pl' voices = {} (Nokogiri::HTML(recent_page.body)/'td.comment').each do |comment| id = timestamp(comment) voices[id] = build_voice(comment) end voices.sort.each do |id, voice| if @caches.empty? or !@caches.has_key?(id) post(PRIVMSG, @opts.channel, "[#{voice[:nickname]}]#{voice[:reply]} #{voice[:comment]} (#{id})") end sleep 2 end @caches = voices end def timestamp(comment) comment.at('div.echo_post_time').text end def build_voice(comment) { :member_id => comment.at('div.echo_member_id').text, @@ -125,5 +143,5 @@ def build_voice(comment) } end end MixiVoiceBot.new.start -
Tomohiro, TAIRA revised this gist
Oct 27, 2009 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ class MixiVoiceBot < Net::IRC::Client def initialize @irc_host = 'you_irc_host' @irc_port = '6667' @channel = '#mixi' @nickname = 'voice' -
Tomohiro, TAIRA revised this gist
Oct 27, 2009 . 1 changed file with 35 additions and 15 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -11,10 +11,10 @@ class MixiVoiceBot < Net::IRC::Client def initialize @irc_host = 'you_ic_host' @irc_port = '6667' @channel = '#mixi' @nickname = 'voice' super(@irc_host, @irc_port, { :nick => @nickname, @@ -29,8 +29,10 @@ def initialize @agent.set_proxy(proxy.host, proxy.port) end @email = 'your_mixi_email' @password = 'your_mixi_password' @caches = [] end def start @@ -53,14 +55,9 @@ def start end get_thread = Thread.new do while true get_voice sleep 300 end end @@ -91,18 +88,41 @@ def post_voice(voice) form.body = voice end.submit end get_voice end def get_voice recent_page = @agent.get 'http://mixi.jp/recent_echo.pl' voices = {} (Nokogiri::HTML(recent_page.body)/'td.comment').each do |comment| id = timestamp(comment) voices[id] = build_voice(comment) end voices.sort.each do |id, voice| if @caches.empty? or !@caches.has_key? id post(NOTICE, @opts.channel, "[#{voice[:nickname]}]#{voice[:reply]} #{voice[:comment]}") end sleep 0.5 end @caches = voices end def timestamp(comment) comment.at('div.echo_post_time').text end def build_voice(comment) { :member_id => comment.at('div.echo_member_id').text, :post_time => comment.at('div.echo_post_time').text, :nickname => comment.at('div.echo_nickname').text, :reply => ((' ' + comment.at('a').text) if comment.at('a').text =~ /^>/), :comment => comment.at('div.echo_body').text } end end -
Tomohiro, TAIRA revised this gist
Oct 27, 2009 . 1 changed file with 41 additions and 41 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,27 +10,43 @@ require 'nokogiri' class MixiVoiceBot < Net::IRC::Client def initialize @irc_host = 'your_irc_host' @irc_port = '6667' @channel = '#MixiVoice' @nickname = 'MixiVoice' super(@irc_host, @irc_port, { :nick => @nickname, :user => @nickname, :real => @nickname, :channel => @channel, }) @agent = WWW::Mechanize.new if ENV['http_proxy'] proxy = URI.parse(ENV['http_proxy']) @agent.set_proxy(proxy.host, proxy.port) end @email = 'your_mixi_account@example.com' @password = 'your_mixi_password' end def start mixi_login @socket = TCPSocket.open(@host, @port) @socket.gets post(PASS, (@opts.pass if @opts.pass)) post(NICK, @opts.nick) post(USER, @opts.user, '0', '*', @opts.real) post(JOIN, @opts.channel) post_thread = Thread.new do while line = @socket.gets message = Message.parse(line) name = "on_#{(COMMANDS[message.command.upcase] || message.command).downcase}" send(name, message) if respond_to? name end @@ -42,16 +58,16 @@ def start voice = get_voice unless recent_voice == voice recent_voice = voice post(NOTICE, @opts.channel, voice) end sleep 60 end end post_thread.join get_thread.join rescue IOError => e @logger.error 'IOError' + e.to_s ensure finish end @@ -60,35 +76,25 @@ def on_privmsg(message) post_voice message[1] end def mixi_login @agent.get 'http://mixi.jp' do |login_page| login_page.form_with(:name => 'login_form') do |form| form.email = @email form.password = @password end.submit end end def post_voice(voice) @agent.get 'http://mixi.jp/recent_echo.pl' do |post_page| post_page.form_with(:action => 'add_echo.pl') do |form| form.body = voice end.submit end end def get_voice recent_page = @agent.get 'http://mixi.jp/recent_echo.pl' (Nokogiri::HTML(recent_page.body)/'td.comment').each do |comment| nickname = comment.at('div.echo_nickname').text @@ -100,10 +106,4 @@ def get_voice end end MixiVoiceBot.new.start -
Tomohiro created this gist
Oct 26, 2009 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,109 @@ #!/usr/bin/env ruby $LOAD_PATH << 'lib' $LOAD_PATH << '../lib' require 'rubygems' require 'net/irc' require 'uri' require 'mechanize' require 'nokogiri' class MixiVoiceBot < Net::IRC::Client EMAIL = 'your_mixi_account@example.com' PASSWORD = 'your_mixi_password' def initialize(*args) super @agent = login end def start @socket = TCPSocket.open(@host, @port) @socket.gets post PASS, @opts.pass if @opts.pass post NICK, @opts.nick post USER, @opts.user, '0', '*', @opts.real post JOIN, @opts.channel post_thread = Thread.new do while l = @socket.gets message = Message.parse(l) name = "on_#{(COMMANDS[message.command.upcase] || message.command).downcase}" send(name, message) if respond_to? name end end get_thread = Thread.new do recent_voice = nil while true voice = get_voice unless recent_voice == voice recent_voice = voice post NOTICE, @opts.channel, voice end sleep 30 end end post_thread.join get_thread.join rescue IOError => e puts e.to_s ensure finish end def on_privmsg(message) post_voice message[1] end def login agent = WWW::Mechanize.new if ENV['http_proxy'] proxy = URI.parse(ENV['http_proxy']) agent.set_proxy(proxy.host, proxy.port) end login = agent.get 'http://mixi.jp' form = login.forms.first form.email = EMAIL form.password = PASSWORD form.submit agent end def post_voice(voice) @agent.get 'http://mixi.jp/recent_echo.pl' do |post_page| post_page.form_with(:action => 'add_echo.pl') do |f| f.body = voice end.submit end end def get_voice recent_page = @agent.get 'http://mixi.jp/recent_echo.pl' voices = [] (Nokogiri::HTML(recent_page.body)/'td.comment').each do |comment| nickname = comment.at('div.echo_nickname').text reply = (comment.at('a').text if comment.at('a').text =~ />/) || nil comment = comment.at('div.echo_body').text return "#{nickname}: #{reply} #{comment}" end end end MixiVoiceBot.new( 'IRC_SERVER_HOST', 'IRC_SERVER_PORT', { :nick => 'MixiVoice', :user => 'MixiVoice', :real => 'MixiVoice', :channel => '#MixiVoice', }).start