SHARE
TWEET

Ozy

a guest Dec 13th, 2009 593 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #TwitterAdium
  2. #Jesse Newland
  3. #jnewland@gmail.com
  4.  
  5. require 'yaml'
  6. require 'rexml/document'
  7. require 'fileutils'
  8. require 'net/http'
  9. require 'timeout'
  10. begin
  11.   require 'Time'
  12. rescue
  13. end
  14.  
  15. #handle first run config file creation
  16. begin
  17.   configfile = open(ENV["HOME"]+"/.twitter")
  18.   config = YAML::load configfile
  19. rescue
  20.   begin
  21.     Fileutils.mv(ENV["HOME"]+"/.twitterc",ENV["HOME"]+"/.twitter")
  22.   rescue
  23.     template = <<EOF
  24. # .twitter
  25. #
  26. # Please fill in fields like this:
  27. #
  28. #username: twitteradium
  29. #email: twitteradium@jnewland.com
  30. #password: secret
  31.  #
  32. username:
  33. email:
  34. password:
  35. EOF
  36.     configfile = open(ENV["HOME"]+"/.twitter","w")
  37.     configfile.write(template)
  38.     configfile.close
  39.   end
  40.   configfile = open(ENV["HOME"]+"/.twitter")
  41.   config = YAML::load open(ENV["HOME"]+"/.twitter")
  42. end
  43.  
  44. if config["username"] == nil || config["password"] == nil
  45.   system("open #{ENV["HOME"]}/.twitter")
  46.   puts "Welcome to Twitter Adium - please edit your ~/.twitter file to contain your username, email, and password - http://twitter.com/twitteradium"
  47.   exit(0)
  48. end
  49.  
  50.  
  51. #store last twitter and timestamp
  52. timefile = "/tmp/.#{ENV["LOGNAME"]}_TwitterAdium.time"
  53. twitterfile = "/tmp/.#{ENV["LOGNAME"]}_TwitterAdium.twitter"
  54.  
  55. begin
  56.     last = open(timefile).read.to_i
  57.     last_twitter = open(twitterfile).read.to_s
  58. rescue
  59.     last = Time.now.to_i
  60.     open(timefile,"w").write(Time.now.to_i)
  61.     last_twitter = ''
  62. end
  63.  
  64. if (Time.now.to_i - last.to_i) > 300  || last_twitter == ''
  65.   open(timefile,"w").write(Time.now.to_i)
  66. else
  67.   puts last_twitter
  68.   exit(0)
  69. end
  70.  
  71. user_timeline_uri = "/users/show/#{config["username"]}.xml"
  72.  
  73. begin
  74.   Timeout::timeout(15) do
  75.     Net::HTTP.start('twitter.com') do |http|
  76.       req = Net::HTTP::Get.new(user_timeline_uri)
  77.       req.basic_auth config["username"], config["password"]
  78.       response = http.request(req)
  79.       case response
  80.       when Net::HTTPSuccess
  81.         xmldoc = REXML::Document.new(response.body)
  82.         twitter = xmldoc.root.elements["status[1]/text"].text
  83.         if twitter.index('@') == 0
  84.         else
  85.           open(twitterfile,"w").write(twitter)
  86.           puts twitter
  87.         end
  88.       when Net::HTTPUnauthorized
  89.         system("open #{ENV["HOME"]}/.twitter")
  90.         puts "Welcome to Twitter Adium - please edit your ~/.twitter file to contain your username, email, and password - http://twitter.com/twitteradium"
  91.       end
  92.     end
  93.   end
  94. rescue Timeout::Error
  95.   open(timefile,"w").write(Time.now.to_i)
  96.   if last_twitter == ''
  97.     puts 'Twitter is slow. http://twitter.com/twitteradium'
  98.   else
  99.     puts last_twitter
  100.   end
  101.   exit(O)
  102. rescue Exception
  103.   puts "<!-- #{$!.class} -->"
  104. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top