Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created April 7, 2009 12:50
Show Gist options
  • Save edvakf/91219 to your computer and use it in GitHub Desktop.
Save edvakf/91219 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby -Ku
=begin
最初に Favotter のログを Google リーダーから取得して、同じフォルダに log.xml という名前で保存しておく必要がある。
Google リーダーのログは、ブラウザからこのようなURLで取得できる。
http://www.google.com/reader/atom/feed/http://d.hatena.ne.jp/edvakf/rss?n=5000
http://www.google.com/reader/atom/feed/http%3A%2F%2Ffavotter.matope.com%2Fuserrss.php%3Fuser%3DTERRAZI%26mode%3Dnew?n=5000
または、ツールを使うならこういう手順を踏む必要がある。
http://d.hatena.ne.jp/edvakf/20081212/1229110106
Ruby の Google Reader というライブラリを使ってもいいらしい。
http://googlereader.rubyforge.org/
下のスクリプトは本当に適当に作ったものなので、2回スキャンしてたりして効率も悪いので、ログファイルが大きくなってきたりするとマシンに負荷をかけたりする。それ以前に REXML で大きな XML ファイルをパースさせるのは負荷が高いので、nokogiri を使って書き直そうと思っているところ。
=end
require 'rexml/document'
require 'rubygems'
require 'twitter'
require 'time'
log = "#{File.dirname(__FILE__)}/log.xml"
entries = []
count = 0
doc = REXML::Document.new File.new(log)
doc.elements.each("feed/entry/summary"){|e|
d = e.get_text.to_s.gsub(/<.*?>/,"").gsub(/^TERRAZI /,"")
entries[count] = {"said"=>d.gsub(/\d fav by $/,""),"favs"=>d.gsub(/.*(\d) fav by $/){$1}}
count += 1
}
count = 0
REXML::XPath.each(doc,"//feed/entry/link/attribute::href"){|e|
entries[count]["link"] = e.to_s
count += 1
}
#puts entries.length
entry = entries[rand(entries.length)]
post = "#{entry["said"]} #{entry["link"]} #{entry["favs"]}favs"
#puts count
#puts post
client = Twitter::Client.new(:login => 'ユーザー名', :password => 'パスワード')
status = client.status(:post, post)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment