Advertisement
Guest User

Ozy

a guest
Apr 30th, 2010
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.54 KB | None | 0 0
  1. #TwitterAdiumSL
  2. #Ozy
  3.  
  4. #Credit for original development
  5. #Jesse Newland
  6. #jnewland@gmail.com
  7.  
  8. #load required standard libraries
  9. require 'yaml'
  10. require 'rexml/document'
  11. require 'fileutils'
  12. require 'net/http'
  13. require 'timeout'
  14. begin
  15.   require 'Time'
  16. rescue
  17. end
  18.  
  19. #Read the preferences file for username/password
  20. #On the first run, create preferences file
  21. begin
  22.   configfile = open(ENV["HOME"]+"/.twitter")
  23.   config = YAML::load configfile
  24. rescue
  25.   begin
  26.     Fileutils.mv(ENV["HOME"]+"/.twitterc",ENV["HOME"]+"/.twitter")
  27.   rescue
  28.     template = <<EOF
  29. #~/.twitter
  30. #
  31. #Please fill in fields like this:
  32. #
  33. #username: twitteradium
  34. #email: twitteradium@jnewland.com
  35. #password: secret
  36. #
  37. username:
  38. email:
  39. password:
  40. EOF
  41.     configfile = open(ENV["HOME"]+"/.twitter","w")
  42.     configfile.write(template)
  43.     configfile.close
  44.   end
  45.   configfile = open(ENV["HOME"]+"/.twitter")
  46.   config = YAML::load open(ENV["HOME"]+"/.twitter")
  47. end
  48.  
  49. #If username/preferences is not set, request preference file update
  50. if config["username"] == nil || config["password"] == nil
  51.   system("open #{ENV["HOME"]}/.twitter")
  52.   puts "Welcome to Twitter Adium - please edit your ~/.twitter file to contain your username, email, and password - http://twitter.com/twitteradium"
  53.   exit(0)
  54. end
  55.  
  56. #Store last twitter and timestamp
  57. timefile = "/tmp/.#{ENV["LOGNAME"]}_TwitterAdium.time"
  58. tweetfile = "/tmp/.#{ENV["LOGNAME"]}_TwitterAdium.twitter"
  59.  
  60. #Read last tweet's time and string
  61. begin
  62.     time_updated = open(timefile).read.to_i
  63.     last_tweet = open(tweetfile).read.to_s
  64. #If either time or string fails to return, set them
  65. #This is the initial run case
  66. rescue
  67.     time_updated = Time.now.to_i
  68.     open(timefile,"w").write(Time.now.to_i)
  69.     last_tweet = ''
  70. end
  71.  
  72. #If 5 minutes has passed or this is the first run, update
  73. #5 minutes is chosen to work simultaneously with a twitter client
  74. if (Time.now.to_i - time_updated.to_i) > 300  || last_tweet == ''
  75.   open(timefile,"w").write(Time.now.to_i)
  76. #Otherwise, use last tweet and exit
  77. #This reduces the number of twitter requests
  78. else
  79.   puts last_tweet
  80.   exit(0)
  81. end
  82.  
  83. #Location of local Twitter XML file
  84. user_timeline_uri = "/users/show/#{config["username"]}.xml"
  85.  
  86. #Update the status
  87. begin
  88.   Timeout::timeout(15) do
  89. #Open a connection to Twitter
  90.     Net::HTTP.start('twitter.com') do |http|
  91. #Setup the grab for the XML file
  92.       req = Net::HTTP::Get.new(user_timeline_uri)
  93. #Login
  94.       req.basic_auth config["username"], config["password"]
  95. #Request the XML file
  96.       response = http.request(req)
  97. #Handle the response
  98.       case response
  99. #If the response is successful, update with the current status
  100.       when Net::HTTPSuccess
  101. #Recognize the XML from the response
  102.         xmldoc = REXML::Document.new(response.body)
  103. #Grab the status from the XML as a string
  104.         tweet = xmldoc.root.elements["status[1]/text"].text
  105. #If the status starts with an '@' for a reply, ignore it
  106.         if tweet.index('@') == 0
  107. #If the status is new, update it
  108.         elsif !(last_tweet == tweet)
  109.           open(tweetfile,"w").write(tweet)
  110.           last_tweet = open(tweetfile).read.to_s
  111.         end
  112.         puts last_tweet
  113.       end
  114.     end
  115.   end
  116. #If the request times out, handle
  117. rescue Timeout::Error
  118. #Update the last request time to minimize requests
  119.   open(timefile,"w").write(Time.now.to_i)
  120. #If this is the first run, update a default status
  121.   if last_tweet == ''
  122.     puts 'Twitter is slow. http://twitter.com/twitteradium'
  123. #Otherwise, use the last status and exit
  124.   else
  125.     puts last_tweet
  126.   end
  127.   exit(O)
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement