forked from ryanking/earlybird
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathearlybird.rb
176 lines (154 loc) · 4.96 KB
/
earlybird.rb
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# TODO
# switch to xauth
# ask for u/p once, then save token (https://gist.github.com/304123/17685f51b5ecad341de9b58fb6113b4346a7e39f)
$KCODE = 'u'
%w[rubygems pp net/http json twitter-text term/ansicolor twitter highline/import].each{|l| require l}
include Term::ANSIColor
class EarlyBird
def initialize(user, pass, track)
httpauth = Twitter::HTTPAuth.new(user, pass)
@client = Twitter::Base.new(httpauth)
@friends = []
@track = track
end
def highlight(text)
text.gsub(Twitter::Regex::REGEXEN[:extract_mentions], ' ' + cyan('@\2')).
gsub(Twitter::Regex::REGEXEN[:auto_link_hashtags], ' ' + yellow('#\3'))
end
def search_highlight(text)
highlight(text)
@track.inject(text) do |newtext, term|
newtext.gsub /#{term}/i do |match|
green(match)
end
end
end
def print_tweet(sn, text)
print sn(sn) , ': ', highlight(text), "\n"
end
def print_search(sn, text)
print green(bold(sn)) , ': ', search_highlight(text), "\n"
end
def sn(sn)
red(bold(sn))
end
def user_and_status(user_id, status_id)
u = @client.user(user_id)
s = @client.status(status_id)
[u, s]
rescue Twitter::General => e
raise e unless e.message =~ /403/
end
def process(data)
if data['friends']
# initial dump of friends
@friends = data['friends']
elsif data['text'] #tweet
if @friends.include?(data['user']['id'])
if data['retweeted_status']
print sn(data['user']['screen_name']), " retweeted: " + "\n\t"
print_tweet(data['retweeted_status']['user']['screen_name'], data['retweeted_status']['text'])
else
print_tweet(data['user']['screen_name'], data['text'])
end
else
print 'search result: ' + sn(data['user']['screen_name']) + "\n"
print "\t"
print_search(data['user']['screen_name'], data['text'])
end
elsif data['event']
case data['event']
when 'favorite', 'unfavorite'
u, s = user_and_status(data['source']['id'], data['target_object']['id'])
print sn(u.screen_name), ' favorited: ' + "\n"
print "\t"
print_tweet(s.user.screen_name, s.text)
when 'retweet'
u, s = user_and_status(data['source']['id'], data['target_object']['id'])
print sn(u.screen_name), " #{data['event']}d: " + "\n"
print "\t"
print_tweet(s.user.screen_name, s.text)
when 'unfollow', 'follow', 'block'
s = @client.user(data['source']['id'])
t = @client.user(data['target']['id'])
print sn(s['screen_name']), ' ', data['event'], 'ed', ' ', sn(t['screen_name']), "\n"
else
puts "unknown event: #{data['event']}"
puts data
end
elsif data['delete']
# ignore deletes
else
puts 'unknown message'
puts data
puts '===='
end
rescue Twitter::RateLimitExceeded
puts "event dropped due to twitter rate limit (reset in #{@client.rate_limit_status['reset_time_in_seconds'] - Time.now} seconds)"
p @client.rate_limit_status
end
end
class Hose
KEEP_ALIVE = /\A3[\r][\n][\n][\r][\n]/
DECHUNKER = /\A[0-F]+[\r][\n]/
NEWLINE = /[\n]/
CRLF = /[\r][\n]/
EOF = /[\r][\n]\Z/
def unchunk(data)
data.gsub(/\A[0-F]+[\r][\n]/, '')
end
def keep_alive?(data)
data =~ KEEP_ALIVE
end
def extract_json(lines)
# lines.map {|line| Yajl::Stream.parse(StringIO.new(line)).to_mash rescue nil }.compact
lines.map {|line| JSON.parse(line).to_hash rescue nil }.compact
end
def run(user, pass, host, path, debug=false)
if debug
$stdin.each_line do |line|
process(line)
end
else
while true
begin
Net::HTTP.start(host) {|http|
req = Net::HTTP::Get.new(path)
req.basic_auth user, pass
http.request(req) do |response|
buffer = ''
response.read_body do |data|
unless keep_alive?(data)
buffer << unchunk(data)
if buffer =~ EOF
lines = buffer.split(CRLF)
buffer = ''
else
lines = buffer.split(CRLF)
buffer = lines.pop
end
extract_json(lines).each {|line| yield(line)}
end
end
end
}
rescue Errno::ECONNRESET
puts "disconnected from streaming api, reconnecting..."
sleep 5
end
end
end
end
end
print "username: "
# had to qualify by $stdin because it wanted to do gets from ARGV?
user = $stdin.gets.strip
pass = ask("Enter your password: ") { |q| q.echo = '*' }
track = ARGV.reject{|t| t == 'debug'}.join(' ').split(',')
url = '/2b/user.json'
if track.length > 0
url << "?track=" + CGI::escape(track.join(','))
end
puts "connecting to #{url}"
eb = EarlyBird.new(user, pass, track)
Hose.new.run(user, pass, 'betastream.twitter.com', url, ARGV.first == 'debug'){|line| eb.process(line)}