Guest User

Ozy

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