カテゴリー
- カメラ
- サクラエディタ
- ぷよぷよ
- プリンタ
- マルチモニタ
- 作った
- 買った
- 電子書籍
- .NET
- ActionScript
- AIR
- Amazon
- Android
- Apache
- Apple
- AppleScript
- ASP.NET
- ATOK
- C#
- css
- Cubase
- dev
- DNS
- DS
- DTM
- Emacs
- eneloop
- Feed
- FeliCa
- FFmpeg
- Firefox
- Flash
- FlashDevelop
- Flex
- Flickr
- Fusion
- GIMP
- git
- Gmail
- GoogleAppsScript
- GoogleCalendar
- GoogleChrome
- GoogleDocs
- GoogleDrive
- Greasemonkey
- hatena
- http
- iPad
- iPhone
- iPod
- Java
- JavaScript
- librahack
- Linux
- Lisp
- livedoorReader
- Mac
- MacPorts
- Markdown
- mixi
- mobile
- MySQL
- PC
- Perl
- PowerPoint
- Rails
- Ruby
- Seasar
- Sendai.js
- serial
- Skype
- SQLite
- SteveJobs
- SublimeText2
- Subversion
- Surface
- tmux
- Tool
- TV
- Ubuntu
- UserJS
- VB
- vim
- Vista
- VisualStudio
- VPN
- Wii
- Windows
- xyzzy
- Yahoo!Pipes
- YouTube
2010.02.03
Twitterにパケット代を投稿するpacketterを更新
以前作ったpacketterをいろいろ直した。
packetterとは?
自動でMy SoftBankのサイトからパケット通信料を取得して、Twitterに投稿するRubyスクリプト。
使い方など、詳しくは以前の記事を参照。
変更点
変更点は以下。
- 月をまたいだときに別な金額を取得してしまう不具合の修正。
- 投稿内容に日付を含めるようにした。
- 投稿内容に前回投稿時からの差額(増加分)を含めるようにした。
- My SoftBankやTwitterのログイン情報を別ファイルで持つようにした。
投稿例
新バージョンでは以下のように投稿される。
2月2日までのSoftBankパケット通信料 : 55,489円 (+31,540円)
Twitter / nacookan: 2月2日までのSoftBankパケット通信料 : 55,48 ...
packetter.rb
新しいコードは以下。
※ 2010-2-4 一部修正。詳細はコメント欄参照。
#!/usr/bin/ruby -Ku require 'rubygems' require 'mechanize' require 'nkf' require 'open-uri' require 'net/http' require 'erb' load File.join(File.dirname(__FILE__), 'config.rb') # logger #require 'logger' #WWW::Mechanize.log = Logger.new('packetter.txt') #WWW::Mechanize.log.level = Logger::DEBUG # setup puts 'setup...' agent = WWW::Mechanize.new agent.follow_meta_refresh = true agent.user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_0 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A345 Safari/525.20' # login puts 'login...' agent.get('https://mb.softbank.jp/scripts/japanese/sss/user/login.jsp') agent.page.form_with(:name => 'loginForm'){|form| form.action = '/scripts/japanese/sss/user/loginCheck.jsp' form.field_with(:name => 'userId').value = $my_softbank[:user_id] form.field_with(:name => 'password').value = $my_softbank[:password] form.click_button } # jumo to customerservice puts 'jump to customerservice...' agent.get('https://mb.softbank.jp/scripts/japanese/sss/ref/obicip.jsp') agent.page.form_with(:name => 'newForm'){|form| form.click_button } # verify pin puts 'verify pin...' agent.get( 'https://www.customerservice.mb.softbank.jp/obi/x/authhlr.do?' + [ 'wcohid=5', "authHLRRequireDto.HLR=#{$my_softbank[:pin]}", 'method=%8E%9F%82%D6', 'jskytid=1', 'sid=T9A1', 'pid=J01P', 'method=', 'idex=', 'dummy=', ].join('&') ) # jump to pricemonth puts 'jump to pricemonth...' agent.page.link_with(:href => /pricemonth/).click # get contents td = agent.page.search('td[@id="contents"]').inner_text.gsub(/[\r\n]/, '') # date td =~ /年([0-9]+月[0-9]+日)\)/ date = NKF::nkf('-Wwxm0Z0', $1) puts "date : #{date}" # packet fee td =~ /パケット通信料([0-9,]+円)/ fee = $1 puts "packet fee : #{fee}" # latest file latest_file = File.join(File.dirname(__FILE__), 'latest') # load latest and compare if File.exist?(latest_file) puts 'load latest...' f = open(latest_file) latest = f.read.chomp f.close # compare fee_new = fee.delete(',').to_i fee_old = latest.delete(',').to_i fee_diff = fee_new - fee_old if fee_diff < 0 diff = '' else diff = " (+#{fee_diff.to_s.gsub(/(.*\d)(\d\d\d)/, '\1,\2')}円)" end puts "diff : #{diff}" end # save latest f = File.open(latest_file, 'w') f.puts fee f.close # post puts 'post...' Net::HTTP.version_1_2 Net::HTTP.start('twitter.com', 80) do |http| req = Net::HTTP::Post.new('/statuses/update.xml') req.basic_auth $twitter[:user_id], $twitter[:password] req.body = 'status=' + ERB::Util.u("#{date}までのSoftBankパケット通信料 : #{fee}#{diff} http://tinyurl.com/packetter") res = http.request(req) end puts 'finished.'
config.rb
ログイン情報はこっちのファイルに記述。packetter.rbと同じディレクトリに置いてね。
$my_softbank = { :user_id => 'my_softbank_id', :password => 'p@ssw0rd', :pin => '9999', } $twitter = { :user_id => 'twitter_user_id', :password => 'p@ssw0rd', }
メモ
My SoftBankの料金のページは、「当月分の料金だけが出る場合」「前月分だけが出る場合」「両方出る場合」があることがわかった。html的にもビシっとしてなくて、Mechanizeでsearchとかを使ってもなかなか困難だった。なので正規表現で取ってきた。ついでなので日付も取った。
また、最後に処理をしたときの金額をファイル(latest)に記録しておいて、次回はそれとの差額を算出してそれも投稿内容に含めている。通常、このスクリプトは1日1回実行すると思うので、前日からの増分、つまり1日で使った量がわかるようになる。
トラックバック - http://d.hatena.ne.jp/nacookan/20100203/1265211945
リンク元
- 4 http://ezsch.ezweb.ne.jp/search/?sr=0101&query=ツイッターのパケット代は
- 2 http://bit.ly/bmNtg1
- 2 http://docomo.ne.jp/cp/as-rslt.cgi?pno=1&key=Twitterのパケ代&sid=000
- 2 http://ezsch.ezweb.ne.jp/search/?query=Twitterの通信料&start-index=7&adpage=3&ct=2097152&sr=0401&t=20100617131530
- 2 http://ezsch.ezweb.ne.jp/search/?query=Twitterの通信料&start-index=7&adpage=3&ct=2&sr=0000&t=20100514040105
- 2 http://ezsch.ezweb.ne.jp/search/?query=Twitter+パケット&start-index=16&adpage=4&ct=1301&sr=0101&t=20110607131732&filter=1
- 2 http://ezsch.ezweb.ne.jp/search/?query=http+パケット料金&start-index=27&adpage=5&ct=2&sr=0000&t=20100726220247
- 2 http://ezsch.ezweb.ne.jp/search/?query=iPhoneでmobileツイッター見たらパケット代&ct=0001&pd=1&sr=0000
- 2 http://longurl.org
- 2 http://search.yahoo.co.jp/search?p=ツイッター パケット代&ei=UTF-8&fr=top_ga1_sa&x=wrt

