Created
October 26, 2009 13:11
-
-
Save tomohiro/218629 to your computer and use it in GitHub Desktop.
IRC チャンネルに JOIN して mixi ボイスの閲覧・投稿・返信・削除を行う Bot
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 characters
| #!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment