# # recemt_tweet.rb: Twitter status plugin for tDiary # # Copyright (C) 2007 by Nishimoto Masaki # Distributed under GPL. # require 'open-uri' require 'timeout' require 'rexml/document' def recent_tweet( id, count ) begin cache = "#{@cache_path}/recent_tweet.xml" xml = open( cache ) {|f| f.read } if Time::now > File::mtime( cache ) + 10*60 then File::delete( cache ) # clear cache 10 minutes later end rescue Errno::ENOENT begin xml = recent_tweet_call_api( id ) open( cache, 'wb' ) {|f| f.write( xml ) } rescue Timeout::Error, StandardError return %Q|
recent_tweet: #{$!}
| end end begin doc = REXML::Document::new( xml ) if doc then html = '
' html << '

' html << 'What am I doing...' html << '

' html << '
' @conf.to_native( html ) else return '
recent_tweet: Failed to open file
' end rescue REXML::ParseException return '
recent_tweet: Failed to parse XML
' end end def recent_tweet_call_api( id ) request = "http://twitter.com/statuses/user_timeline/#{id}.xml" proxy = @conf['proxy'] proxy = 'http://' + proxy if proxy timeout( 10 ) do open( request, :proxy => proxy ) {|f| f.read } end end