-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
39 lines (32 loc) · 984 Bytes
/
Rakefile
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
require './tweetclock'
task 'jobs:work' do
# Thanks Adam Wiggins
# http://adam.heroku.com/past/2010/3/19/consuming_the_twitter_streaming_api/
url = 'http://stream.twitter.com/1/statuses/sample.json'
def set_expiration(time)
if time.sec == 0
redis.expire time.to_i, MINUTES_TTL
else
redis.expire time.to_i, SECONDS_TTL
end
end
def handle_tweet(tweet)
return unless tweet['created_at']
time = Time.parse(tweet['created_at'])
redis.setnx time.to_i, tweet['id']
set_expiration(time)
end
EventMachine.run do
http = EventMachine::HttpRequest.new(url).get :head => {
'Authorization' => [ ENV['TWITTER_USERNAME'],
ENV['TWITTER_PASSWORD'] ] }
buffer = ""
http.stream do |chunk|
buffer += chunk
while line = buffer.slice!(/.+\r?\n/)
handle_tweet JSON.parse(line)
end
end
http.disconnect { raise "Connection to Twitter Streaming API lost." }
end
end