Pragger のリリースも近い?

Praggerがどうやら最初のリリースに向けて具体的に動き出したっぽい。

ちょうどいいので今まで作ったプラグインについてまとめておく。どれもPraggerと同じライセンスでOKです。

load_lirs.rb

LIRS を取得するプラグイン。これでいいのか実は不安。

publish:hatena_graph.rb

はてなグラフにデータをポストするプラグイン。今日のデータしかポストできない。

get_hatena_graph.rb

はてなグラフからデータをダウンロードするプラグイン。目的のグラフのデータだけを抜き出す。これは公開してなかった。

 ## Get Hatena Graph data
 ## 
 ## - module: get_hatena_graph
 ##   config:
 ##     user_id: your-hatena-user-id
 ##     password: your-password
 ##     graph_name: the-name-of-graph
 
 begin
   require 'rubygems'
 rescue LoadError
 end
 require 'mechanize'
 require 'uri'
 require 'kconv'
 require 'csv'
 
 class HatenaGraph
   def initialize(id,password)
     @id = id
     @password = password
     @agent = WWW::Mechanize.new
     if proxy = ENV['http_proxy']
       proxy = URI.parse(proxy)
       @agent.set_proxy(proxy.host, proxy.port)
     end
     @graph = @agent.get("http://graph.hatena.ne.jp/#{id}/")
   end
 
   def login
     login_link = @graph.links.text("ログイン".toutf8)
     login_page = @agent.get(login_link.href)
     login_form = login_page.forms.first
     login_form['key'] = @id
     login_form['password'] = @password
     redirect_page = @agent.submit(login_form)
     @graph_link = redirect_page.links.text("こちら".toutf8)
     @graph_page = @agent.get(@graph_link.href)
   end
 
   def fetch_csv
     @data_link = @graph_page.links.text("データ".toutf8)
     @data_page = @agent.get(@data_link.href)
     csv_link = @data_page.links.text("ダウンロード".toutf8)
     csv_file = @agent.get(csv_link.href)                    # => WWW::Mechanize::File
     tmpfile = Tempfile.open("hatenagraph")
     path = tmpfile.path
     csv_file.save_as(path)
     tmpfile.close
     path
   end
 end
 
 def get_hatena_graph(config, data)
   graph = HatenaGraph.new(config['user_id'], config['password']) 
   graph.login
   csvpath = graph.fetch_csv
   csv = CSV.open(csvpath, "r").map{|r| r }
   graph_names = csv.shift
   i = graph_names.index(config['graph_name'].tosjis)
   csv.map{|r| r[i].to_f }
 end
 

設定ファイルはこんな感じ。

 - module: get_hatena_graph
   config:
     user_id: takatoh
     password: xxxxxxxx
     graph_name: "読んだページ数"

argv.rb

小物その1,コマンドラインからデータを取得。

 def argv(config,data)
   return ARGV.clone
 end

average.rb

小物その2,平均。

 def average(config, data)
   sum = data.inject(0.0){|a,b| a + b.to_f }
   return [ sum / data.size ]
 end