ruby mixi 日記投稿 日記更新

以前に遊びで書いたmixiへ投稿するプログラムを少し書き直したので残して置く
相変わらずmechanizeは便利だな!
mixiへの自動投稿はもっと簡単な方法があるとおもうけど、とりあえず自分はこんな感じに書いた。
mixiへの投稿はidとpost keyが有れば投稿出来るのでまずはそれを取得するプログラム

require 'rubygems'
require 'mechanize'
mail = 'e-mail'
pass = 'password'
agent = Mechanize.new
agent.post('http://mixi.jp/login.pl', :next_url => 'home.pl', :email => mail, :password => pass)
id = agent.cookie_jar.jar['mixi.jp']['/']['BF_SESSION'].value.split(/_/).first
agent.get('http://mixi.jp/add_diary.pl', :id => id, :submit => 'main', :diary_body => :test, :diary_title => :test)
post_key = agent.page.form(:action => 'add_diary.pl').hiddens.select{|item| item.name == 'post_key'}.first.value
puts "id => #{id}, post_key => #{post_key}"

mixiのメールとパスワードを設定して実行するとidとpost keyが出力されるはず
それをメモして置く

require 'rubygems'
require 'mechanize'
require 'kconv'
mail = 'e-mail'
pass = 'password'
post_key = 'post_key'
id = 'id'
title = 'test'
body = 'test'
agent = Mechanize.new
agent.post('http://mixi.jp/login.pl', :next_url => 'home.pl', :email => mail, :password => pass)
agent.post('http://mixi.jp/add_diary.pl', :diary_title => title.toeuc, :diary_body => body.toeuc, :id => id, :submit => 'confirm', :post_key => post_key)

注意するのはmixiでの投稿は文字コードeucで投稿すること、じゃないと文字化けします
これだけだと、タイトルと本文しか投稿できないな。。。