xtunnelのためにスクレイピングの勉強をしていて、Mechanize+hpricotからMechanize+nokogiriの組み合わせに乗り換えようと色々と使ってみている中でできた物のひとつ。
昔しゃお先生がやっていたのを俺もやりたくて3ヶ月ぐらい前に作ったけど、mobilesuica.comのおサイフケータイ使用履歴は1日一度早朝に更新される仕様に変更されたらしくボツになった。


結局idやcssなどの手がかりが無くて手動で要素を取り出す事になり、nokogiriはHTMLタグを除去するのにしか使わなかった
MobileSuica.rb

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'nokogiri'
require 'mechanize'
require 'kconv'

module MobileSuica
  def MobileSuica.get(user,pass)
    agent = WWW::Mechanize.new
    agent.user_agent_alias = 'Windows IE 7'
    page = agent.get('http://www.mobilesuica.com/iq/ir/SuicaDisp.aspx?returnId=SFRCMMEPC03')
    login_form = page.forms_with(:name => 'form1').first
    login_form.fields_with(:name => 'MailAddress').first.value = user
    login_form.fields_with(:name => 'Password').first.value = pass
    page = login_form.click_button
    
    return page.body.toutf8.split(/<tr>/).delete_if{|tr|
      !(tr =~ /&yen;/m)
    }.map{|tr|
      tr.gsub(/\n/,"").split(/\r/)[0..5].map{|line| # 月日,種別,利用場所,種別,利用場所,残額
        Nokogiri(line).inner_text.chomp.strip.gsub(/[\t ]/,"")
      }
    }
  end
end


mobilesuicaのユーザ名、パスワードで履歴を2次元配列として取り出せる。
require 'MobileSuica'
MobileSuica.get("user", "pass")
月日、種別、利用場所、種別、利用場所、残額の順になる
01/30

川崎

横浜
5,110
01/30

相鉄横浜
窓出
川崎
5,320



履歴のうち最新の駅名をtwitterに投稿する。-dつけて起動するとdaemonになる
tweet-mobilesuica.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'webrick'
require 'twitter'
require 'MobileSuica'

def start(conf)
  loop do
    begin
      first = MobileSuica.get(conf["suica_user"], conf["suica_pass"]).first
    rescue
      first = nil
    end
    if first != nil
      last = first if !last
      if first != last # 1回前に取得した履歴と比較
        puts first
        puts '-'*10
        if first[1] == '入' && first[3] == '出' # 降車履歴の時
          message = first[4].chomp.strip+"なう (suica)"
          if !(message =~ /#{conf["ngwords"]}/)
            if conf["nopost"] != true
              httpAuth = Twitter::HTTPAuth.new(conf["twitter_user"], conf["twitter_pass"])
              tw = Twitter::Base.new(httpAuth)
              tw.update(message) # twitter post
            end
            puts message
          end
        end
        last = first
      end
    end
    sleep 60*60*1.5 # 1時間半待つ
  end
end

conf = YAML::load open File.dirname(__FILE__)+'/config.yaml'
if ARGV[0] == '-d'
  WEBrick::Daemon.start {
    start(conf)
  }
else
  start(conf)
end


設定ファイル。自宅の駅名などはngwordsに入れておく
config.yaml
# config.yaml
# mobilesuica.com user/pass
suica_user : 'username@docomo.ne.jp'
suica_pass : '12345678'

# twitter user/pass
twitter_user : 'shokai'
twitter_pass : 'password'

# postしない駅名を正規表現で
ngwords : "(東京|横浜)"

# for debug
#nopost : "true"