Skip to content

Instantly share code, notes, and snippets.

@brendano
Created March 24, 2009 16:18
Show Gist options
  • Save brendano/84189 to your computer and use it in GitHub Desktop.
Save brendano/84189 to your computer and use it in GitHub Desktop.
# hacked up twitter.rb for TwitterAdium
# to display first msg that's *not* and @-reply
# TwitterAdium is at http://www.adiumxtras.com/index.php?a=xtras&xtra_id=3484
# this file is for ~/Library/Application Support/Adium 2.0/Scripts/Twitter.AdiumScripts/Contents/Resources/twitter.rb
# brendan o'connor - anyall.org
# TwitterAdium by Jesse Newland [email protected]
# this file hacked up by brendan o'connor - anyall.org
require 'yaml'
require 'rexml/document'
require 'fileutils'
require 'net/http'
require 'timeout'
begin
require 'Time'
rescue
end
require 'rubygems'
require 'json'
#handle first run config file creation
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: [email protected]
#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 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"
twitterfile = "/tmp/.#{ENV["LOGNAME"]}_TwitterAdium.twitter"
begin
last = Time.parse(open(timefile).read).to_i
last_twitter = open(twitterfile).read.to_s
rescue
last = Time.now.to_i
open(timefile,"w").write(Time.now.strftime("%a %b %d %H:%M:%S %Y"))
last_twitter = ''
end
if (Time.now.to_i - last.to_i) > 120 || last_twitter == ''
open(timefile,"w").write(Time.now.to_s)
else
puts last_twitter
exit(0)
end
# user_timeline_uri = "/users/show/#{config["username"]}.xml"
uri = "/statuses/user_timeline.json"
begin
Timeout::timeout(15) do
Net::HTTP.start('twitter.com') do |http|
req = Net::HTTP::Get.new(uri)
req.basic_auth config["username"], config["password"]
response = http.request(req)
case response
when Net::HTTPSuccess
jsondoc = JSON.parse response.body
texts = jsondoc.map{|d| d['text']}
twitter = texts.find{|t| t !~ /^ *@/} || texts[0] || ""
# xmldoc = REXML::Document.new(response.body)
# require 'pp'
# pp xmldoc.root.elements['status']
# puts twitterfile
# twitter = xmldoc.root.elements["status[1]/text"].text
open(twitterfile,"w").write(twitter)
puts twitter
when Net::HTTPUnauthorized
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"
end
end
end
rescue Timeout::Error
open(timefile,"w").write(Time.now.to_s)
if last_twitter == ''
puts 'Twitter is slow. http://twitter.com/twitteradium'
else
puts last_twitter
end
exit(O)
rescue Exception
puts "<!-- #{$!.class} -->"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment