# tw_today.rb $Revision: 1.1 $ # (C) 2010 pyon # You can redistribute it and/or modify it under GPL2. require 'fileutils' require 'open-uri' require 'pstore' require 'rexml/document' require 'time' require 'timeout' require 'uri' def tw_today_cache_dir(date) File::join(@cache_path, 'tw_today', date.strftime('%Y')) end def tw_today_cache_file(date) File::join(tw_today_cache_dir(date), date.strftime('%Y%m%d.db')) end def tw_today_read_cache(date) return if !FileTest.exist?(tw_today_cache_file(date)) begin tweets = {} db = PStore.new(tw_today_cache_file(date)) db.transaction(:read_only) do tweets = db.fetch(date.strftime('%Y%m%d'), {}) db.abort end return tweets rescue PStore::Error end end def tw_today_write_cache(date, tweet) FileUtils.mkdir_p(tw_today_cache_dir(date)) datestr = date.strftime('%Y%m%d') begin db = PStore.new(tw_today_cache_file(date)) db.transaction do db[datestr] = {} if !db.root?(datestr) db[datestr][tweet['id']] = tweet if !tweet.empty? db.commit end rescue PStore::Error end end def tw_today_get_xml(date) uri = URI::HTTP.build( :host => 'twitter.com', :path => "/statuses/user_timeline/#{@conf['twitter.user']}.xml", :query => 'count=200' ) begin timeout(15){uri.read} rescue Timeout::Error rescue OpenURI::HTTPError end end def tw_today_html(date, tweets) screen_name = @conf['twitter.user'] r = %Q[
#{h t[1]['tweet']} (#{h t[1]['time'].strftime('%m/%d %H:%M')})
] end r << %Q[