brendano (owner)

Revisions

gist: 84189 Download_button fork
public
Public Clone URL: git://gist.github.com/84189.git
Embed All Files: show embed
hacked up twitter.rb for TwitterAdium #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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 jnewland@gmail.com
# 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: 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 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