Created — forked from davetroy/stream.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Capture tweets for a given lat/lon bounding box

View stream.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
#!/usr/bin/env ruby
$: << File.dirname(__FILE__)
 
require 'rubygems'
require 'eventmachine'
require 'em-http' # gem name em-http-request
require 'yajl'
require 'yajl/json_gem'
 
class Streamer
def initialize
Signal.trap("INT") { EM.stop }
Signal.trap("TERM") { EM.stop }
EventMachine.run {
Hose.new
}
$stderr.puts "shutting down!"
end
end
 
class Hose
PARSER = Yajl::Parser.new
CREDS = ["TWITTER_USERNAME", "TWITTER_PASSWORD"]
BOX = [-76.75,39.18, -76.43,39.40]
def initialize
@http = EventMachine::HttpRequest.new('https://stream.twitter.com/1/statuses/filter.json',
:connection_timeout => 0, :inactivity_timeout => 0).post(
:head => {'authorization' => CREDS},
:body => {:locations => BOX.join(',')}
)
PARSER.on_parse_complete = proc { |h| process(h) }
@http.stream { |json| PARSER << json }
@http.errback { |e| restart }
@http.callback { |c| restart }
end
 
def process(s)
if s['geo']
lat = s['geo']['coordinates'][0]
lon = s['geo']['coordinates'][1]
elsif s['coordinates']
lat = s['coordinates']['coordinates'][1]
lon = s['coordinates']['coordinates'][0]
elsif s['place']
c = s['place']['bounding_box']['coordinates'][0]
lat = (c[0][1] + c[2][1])/2.0
lon = (c[0][0] + c[1][0])/2.0
end
s['lat'] = lat
s['lon'] = lon
return unless lat && lon && (lat>=BOX[1] && lon>=BOX[0]) && (lat<=BOX[3] && lon<=BOX[2])
ll = "#{lat},#{lon}"
http2 = EventMachine::HttpRequest.new("http://maps.googleapis.com/maps/api/geocode/json?latlng=#{ll}\&sensor=false").get
http2.callback { display(s, http2.response) }
end
def datestring
Time.now.strftime("%Y%m%d")
end
 
def display(s,c)
begin
addr = JSON.parse(c)
s['formatted_address'] = addr['results'].first['formatted_address']
rescue
s['formatted_address'] = "[#{s['lat']}, #{s['lon']}]"
end
ts = Time.now
s['timestamp'] = ts.to_i
puts s.to_json
end
def restart
$stderr.puts "restarting"
Hose.new
end
end
 
Streamer.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.