Skip to content

Instantly share code, notes, and snippets.

@tomohiro
Created October 26, 2009 13:11
Show Gist options
  • Select an option

  • Save tomohiro/218629 to your computer and use it in GitHub Desktop.

Select an option

Save tomohiro/218629 to your computer and use it in GitHub Desktop.

Revisions

  1. Tomohiro, TAIRA revised this gist Jan 6, 2010. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions mixi_voice_bot.rb
    Original file line number Diff line number Diff line change
    @@ -107,7 +107,7 @@ def setup_options
    end

    def start
    mixi_login
    login
    @identity = get_identity

    @socket = TCPSocket.open(@host, @port)
    @@ -128,7 +128,7 @@ def start

    get_thread = Thread.new do
    loop do
    get_voice
    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_voice($1, $2)
    reply($1, $2)
    when /^rm ([0-9]+)/
    remove_voice($1)
    delete($1)
    else
    post_voice(message[1])
    add(message[1])
    end
    get_voice
    get
    end
    end

    def mixi_login
    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 post_voice(voice)
    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_voice(key, voice)
    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 remove_voice(post_time)
    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_voice
    def get
    voices = crawl_recent_voice
    voices.sort.each do |key, voice|
    if @caches.empty? or !@caches.has_key? key
  2. Tomohiro, TAIRA revised this gist Nov 4, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mixi_voice_bot.rb
    Original 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 'uri'
    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 2
    sleep 5
    end
    end
    @caches = voices
  3. Tomohiro, TAIRA revised this gist Oct 30, 2009. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions mixi_voice_bot.rb
    Original 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'
  4. Tomohiro, TAIRA revised this gist Oct 30, 2009. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions mixi_voice_bot.rb
    Original 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
    Daemons.daemonize

    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
  5. Tomohiro, TAIRA revised this gist Oct 30, 2009. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions mixi_voice_bot.rb
    Original 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
    setup_options
    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'
    @irc_port = 6667
    @bot_name = 'voice'
    @crawl_time = 60

    ARGV.options do |o|
  6. Tomohiro, TAIRA revised this gist Oct 29, 2009. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions mixi_voice_bot.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    require 'mechanize'
    require 'nokogiri'

    Version="0.1.0"
    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
    end

    @caches = []
    @identity = nil
    end
    @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 ポート番号 (規定は 6667)') { |v| @irc_port = v }
    o.on('-b', '--bot-name BOT', 'mixi ボイスを発言する bot の名前 (規定は voice)') { |v| @bot_name = 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', 'mixi ボイスをクロールする間隔 (規定は 60秒)') { |v| @crawl_time = 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
    end

    get_thread = Thread.new do
  7. Tomohiro, TAIRA revised this gist Oct 29, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mixi_voice_bot.rb
    Original 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-name CHANNEL', '接続先のチャンネル名') { |v| @channel = 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 }
  8. Tomohiro, TAIRA revised this gist Oct 29, 2009. 1 changed file with 0 additions and 0 deletions.
    Empty file modified mixi_voice_bot.rb
    100644 → 100755
    Empty file.
  9. Tomohiro, TAIRA revised this gist Oct 29, 2009. 1 changed file with 54 additions and 35 deletions.
    89 changes: 54 additions & 35 deletions mixi_voice_bot.rb
    Original 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
    @irc_host = 'your_irc_host'
    @irc_port = '6667'
    @channel = '#your_channel'
    @bot_name = 'voice'
    @nickname = 'your_nickname'

    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

    @email = 'your_mixi_mail@example.com'
    @password = 'your_mixi_password'

    end

    @caches = []
    @identity = nil
    @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
    end

    get_thread = Thread.new do
    loop do
    get_voice
    sleep 300
    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 'http://mixi.jp' do |login_page|
    @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 'http://mixi.jp/recent_echo.pl' do |post_page|
    @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 'http://mixi.jp/recent_echo.pl' do |post_page|
    @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 'http://mixi.jp/recent_echo.pl'
    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 'http://mixi.jp/recent_echo.pl'
    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)
  10. Tomohiro revised this gist Oct 28, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mixi_voice_bot.rb
    Original 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 = '#mixi'
    @channel = '#your_channel'
    @bot_name = 'voice'
    @nickname = 'your_nickname'

  11. Tomohiro revised this gist Oct 28, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mixi_voice_bot.rb
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ def initialize
    @irc_port = '6667'
    @channel = '#mixi'
    @bot_name = 'voice'
    @nickname = 'Tomohiro'
    @nickname = 'your_nickname'

    super(@irc_host, @irc_port, {
    :nick => @bot_name,
  12. Tomohiro, TAIRA revised this gist Oct 28, 2009. 1 changed file with 53 additions and 31 deletions.
    84 changes: 53 additions & 31 deletions mixi_voice_bot.rb
    Original file line number Diff line number Diff line change
    @@ -15,12 +15,12 @@ def initialize
    @irc_port = '6667'
    @channel = '#mixi'
    @bot_name = 'voice'
    @nickname = 'your_irc_nickname'
    @nickname = 'Tomohiro'

    super(@irc_host, @irc_port, {
    :nick => @bot_name,
    :user => @bot_name,
    :real => @bot_name,
    :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'
    @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
    while true
    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/
    reply_voice(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

    # 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
    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|
    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
    key = timestamp(comment)
    voices[key] = build_voice(comment)
    end

    @caches = voices
    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
  13. Tomohiro, TAIRA revised this gist Oct 28, 2009. 1 changed file with 56 additions and 38 deletions.
    94 changes: 56 additions & 38 deletions mixi_voice_bot.rb
    Original 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 = 'you_irc_host'
    @irc_host = 'your_irc_host'
    @irc_port = '6667'
    @channel = '#mixi'
    @nickname = 'voice'

    @bot_name = 'voice'
    @nickname = 'your_irc_nickname'

    super(@irc_host, @irc_port, {
    :nick => @nickname,
    :user => @nickname,
    :real => @nickname,
    :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_email'
    @email = 'your_mixi_mail'
    @password = 'your_mixi_password'

    @caches = []
    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
    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
    @logger.error 'IOError' + e.to_s
    @log.error 'IOError' + e.to_s
    ensure
    finish
    end

    def on_privmsg(message)
    post_voice message[1]
    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_with(:name => 'login_form') do |form|
    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

    get_voice
    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(NOTICE, @opts.channel, "[#{voice[:nickname]}]#{voice[:reply]} #{voice[:comment]}")
    if @caches.empty? or !@caches.has_key?(id)
    post(PRIVMSG, @opts.channel, "[#{voice[:nickname]}]#{voice[:reply]} #{voice[:comment]} (#{id})")
    end
    sleep 0.5
    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
  14. Tomohiro, TAIRA revised this gist Oct 27, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mixi_voice_bot.rb
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@

    class MixiVoiceBot < Net::IRC::Client
    def initialize
    @irc_host = 'you_ic_host'
    @irc_host = 'you_irc_host'
    @irc_port = '6667'
    @channel = '#mixi'
    @nickname = 'voice'
  15. Tomohiro, TAIRA revised this gist Oct 27, 2009. 1 changed file with 35 additions and 15 deletions.
    50 changes: 35 additions & 15 deletions mixi_voice_bot.rb
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,10 @@

    class MixiVoiceBot < Net::IRC::Client
    def initialize
    @irc_host = 'your_irc_host'
    @irc_host = 'you_ic_host'
    @irc_port = '6667'
    @channel = '#MixiVoice'
    @nickname = 'MixiVoice'
    @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_account@example.com'
    @email = 'your_mixi_email'
    @password = 'your_mixi_password'

    @caches = []
    end

    def start
    @@ -53,14 +55,9 @@ def start
    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 60
    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|
    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
    id = timestamp(comment)
    voices[id] = build_voice(comment)
    end

    return "#{nickname}: #{reply} #{comment}"
    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

  16. Tomohiro, TAIRA revised this gist Oct 27, 2009. 1 changed file with 41 additions and 41 deletions.
    82 changes: 41 additions & 41 deletions mixi_voice_bot.rb
    Original file line number Diff line number Diff line change
    @@ -10,27 +10,43 @@
    require 'nokogiri'

    class MixiVoiceBot < Net::IRC::Client
    EMAIL = 'your_mixi_account@example.com'
    PASSWORD = 'your_mixi_password'

    def initialize(*args)
    super
    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

    @agent = login
    @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(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)
    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
    post(NOTICE, @opts.channel, voice)
    end
    sleep 30
    sleep 60
    end
    end

    post_thread.join
    get_thread.join
    rescue IOError => e
    puts e.to_s
    @logger.error 'IOError' + e.to_s
    ensure
    finish
    end
    @@ -60,35 +76,25 @@ 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)
    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

    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
    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'
    voices = []

    (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(
    'IRC_SERVER_HOST', 'IRC_SERVER_PORT', {
    :nick => 'MixiVoice',
    :user => 'MixiVoice',
    :real => 'MixiVoice',
    :channel => '#MixiVoice',
    }).start
    MixiVoiceBot.new.start
  17. Tomohiro created this gist Oct 26, 2009.
    109 changes: 109 additions & 0 deletions mixi_voice_bot.rb
    Original 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