Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #TwitterAdiumSL
- #Ozy
- #Credit for original development
- #Jesse Newland
- #jnewland@gmail.com
- #load required standard libraries
- require 'yaml'
- require 'rexml/document'
- require 'fileutils'
- require 'net/http'
- require 'timeout'
- begin
- require 'Time'
- rescue
- end
- #Read the preferences file for username/password
- #On the first run, create preferences file
- begin
- configfile = open(ENV["HOME"]+"/.twitter")
- config = YAML::load configfile
- rescue
- begin
- Fileutils.mv(ENV["HOME"]+"/.twitterc",ENV["HOME"]+"/.twitter")
- rescue
- template = <<EOF
- #~/.twitter
- #
- #Please fill in fields like this:
- #
- #username: twitteradium
- #email: twitteradium@jnewland.com
- #password: secret
- #
- username:
- email:
- password:
- EOF
- configfile = open(ENV["HOME"]+"/.twitter","w")
- configfile.write(template)
- configfile.close
- end
- configfile = open(ENV["HOME"]+"/.twitter")
- config = YAML::load open(ENV["HOME"]+"/.twitter")
- end
- #If username/preferences is not set, request preference file update
- if config["username"] == nil || config["password"] == nil
- system("open #{ENV["HOME"]}/.twitter")
- puts "Welcome to Twitter Adium - please edit your ~/.twitter file to contain your username, email, and password - http://twitter.com/twitteradium"
- exit(0)
- end
- #Store last twitter and timestamp
- timefile = "/tmp/.#{ENV["LOGNAME"]}_TwitterAdium.time"
- tweetfile = "/tmp/.#{ENV["LOGNAME"]}_TwitterAdium.twitter"
- #Read last tweet's time and string
- begin
- time_updated = open(timefile).read.to_i
- last_tweet = open(tweetfile).read.to_s
- #If either time or string fails to return, set them
- #This is the initial run case
- rescue
- time_updated = Time.now.to_i
- open(timefile,"w").write(Time.now.to_i)
- last_tweet = ''
- end
- #If 5 minutes has passed or this is the first run, update
- #5 minutes is chosen to work simultaneously with a twitter client
- if (Time.now.to_i - time_updated.to_i) > 300 || last_tweet == ''
- open(timefile,"w").write(Time.now.to_i)
- #Otherwise, use last tweet and exit
- #This reduces the number of twitter requests
- else
- puts last_tweet
- exit(0)
- end
- #Location of local Twitter XML file
- user_timeline_uri = "/users/show/#{config["username"]}.xml"
- #Update the status
- begin
- Timeout::timeout(15) do
- #Open a connection to Twitter
- Net::HTTP.start('twitter.com') do |http|
- #Setup the grab for the XML file
- req = Net::HTTP::Get.new(user_timeline_uri)
- #Login
- req.basic_auth config["username"], config["password"]
- #Request the XML file
- response = http.request(req)
- #Handle the response
- case response
- #If the response is successful, update with the current status
- when Net::HTTPSuccess
- #Recognize the XML from the response
- xmldoc = REXML::Document.new(response.body)
- #Grab the status from the XML as a string
- tweet = xmldoc.root.elements["status[1]/text"].text
- #If the status starts with an '@' for a reply, ignore it
- if tweet.index('@') == 0
- #If the status is new, update it
- elsif !(last_tweet == tweet)
- open(tweetfile,"w").write(tweet)
- last_tweet = open(tweetfile).read.to_s
- end
- puts last_tweet
- end
- end
- end
- #If the request times out, handle
- rescue Timeout::Error
- #Update the last request time to minimize requests
- open(timefile,"w").write(Time.now.to_i)
- #If this is the first run, update a default status
- if last_tweet == ''
- puts 'Twitter is slow. http://twitter.com/twitteradium'
- #Otherwise, use the last status and exit
- else
- puts last_tweet
- end
- exit(O)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement