【追記した】mixiボイスとtwitterに同時ポストするスクリプトを書いてみた.GUIにも対応してみた.

【追記】TkでGUIにも対応しておいた.



twitterでちょっと話題になったので,書いておく.


使い方.(Rubyがインストールされている状態で!)


(下記ソース | カキソース(ってオイスターソースだっけ?))を保存して,中の

mixi_email = "hoge@hoge.jp"
mixi_password = "fugafuga"

twitter_id = "hoge"
twitter_password = "fugafuga"

について書き換えて,
コマンドラインで,

gem install twitter
gem install mechanize

をかけて,

ruby mixitter.rb "うんこうんこー!"

と打ちこむとポストする.

ruby mixitter.rb

と打ちこむとGUIを起動する.


イカソース!
mixitter.rb

require "rubygems"
require "mechanize"
require "tk"
require "twitter"
require "kconv"

class Mixi
  MIXI_BASE = "http://mixi.jp/"
  MIXI_HOME = "#{MIXI_BASE}home.pl"

  attr_accessor :email, :password

  def initialize(email, password)
    @email = email
    @password = password
    @agent = WWW::Mechanize.new
  end

  def login
    top_page = @agent.get(MIXI_BASE)
    form_login = top_page.form_with(:name => 'login_form')
    form_login.email = email
    form_login.password = password
    @agent.submit(form_login)
  end

  def post(text)
    home_page = @agent.get(MIXI_HOME)
    form_echoPost = home_page.forms.find{|f| f.form_node['id'] == 'EchoPost'}
    form_echoPost.body = text
    @agent.submit(form_echoPost)
  end
end
  

mixi_email = "hogehoge@hoge.jp"
mixi_password = "fugafuga"

twitter_id = "hogehoge"
twitter_password = "fugafuga"

def mixitterpost(email, mpass, tid, tpass, comment)
  puts "mixiとtwitterに同時ポストするツール"
  puts ""

  comment = Kconv.toutf8(comment)

  puts "mixi================================="
  magent = Mixi.new(email, mpass)
  puts "認証中."
  magent.login
  puts "コメントをポスト中"
  magent.post(comment)

  puts ""

  puts "twitter=============================="
  puts "認証中."
  tauth = Twitter::HTTPAuth.new(tid, tpass)
  tagent = Twitter::Base.new(tauth)
  puts "コメントをポスト中"
  tagent.update(comment)
end

if(ARGV[0] != nil)
  mixitterpost(mixi_email, mixi_password, twitter_id, twitter_password, ARGV[0])
else
  @root = TkRoot.new(:title => "Mixitter")
  @text = TkEntry.new.pack
  @button = TkButton.new(nil, :text => "POST")
  @button.command = proc{
                           mixitterpost(mixi_email, mixi_password, twitter_id, twitter_password, @text.value)
                           @text.value = ""
                         }
  @button.pack
  Tk.mainloop
end


kconvを使っているのは,Windows環境を想定しているから.
Mixiクラスのコンストラクタでloginしていないのは,無駄に重くなることを恐れたため.


以上!