Create a gist now

Instantly share code, notes, and snippets.

Capture tweets for a given lat/lon bounding box
#!/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