groovyでoutputzの文字数を取得する
Outputzは自分の入力文字数をロギングしてくれるサービス。
ライフログの一部として使うにはもってこいのサービスだと思う。
APIが公開されてるので、各種エディタ、統合開発環境などのソフトからインプットをすることができる。
Firefoxのアドオン、TwitterではTweenが対応してる。
でも、データを取得するAPIがない。
2010年末に1年のまとめを行ってるときに、outputzのデータを取得しようとしたら
以下のエラーになって、「今年」のデータが参照できなかった。
「今月」を使用しても、4ヶ月ぐらいまでしか過去にさかのぼれなかった。
(今は、登録した時の2009/7まで、さかのぼれるようだ。)
昨日のデータを取得するGroovyスクリプトを書いた。
スケジューラーで日付が変わったときに取得しておけばいいだろう。
ファイルにロギングしておいて、最終的には、Timeplotでグラフ化したい。
ソース
@Grab('net.sourceforge.htmlunit:htmlunit:2.8') import com.gargoylesoftware.htmlunit.WebClient MAGIC_WORD="?????" //outputz の復活の呪文 webClient = new WebClient() configPage = webClient.getPage('http://outputz.com/config/') form =configPage.getForms() field = form[0].getInputByName('magic_word') field.setValueAttribute(MAGIC_WORD) button = form[0].getInputByValue('register') result = button.click() yesterday = (new Date() -1).format("yyyyMMdd") println yesterday dailyPage = webClient.getPage("http://outputz.com/daily/${yesterday}") /* ex <span>1,987</span>bytes</p> */ span = dailyPage.getByXPath("id('contentBox')/div[1]/p")[0] /* ex 1,987 bytes */ text = span.asText() /* ex 1,987→1987 */ dailyByte =( text.split()[0] =~ /,/).replaceAll("") record = (new Date() -1).format("yyyy-MM-dd") + ",${dailyByte}" println record file = new File("C:\\usr\\var\\outputz\\outputz.txt") file.append(record)
実行結果
[D:\workspace\groovy_SandBox]groovy outputz.groovy 2>nul 2011-01-07,2276
大量に警告とruntimeErrorがでるので、表示用に標準エラーは捨てた。