Skip to content

Instantly share code, notes, and snippets.

@eric-wood
Created March 26, 2012 15:03
Show Gist options
  • Save eric-wood/2205710 to your computer and use it in GitHub Desktop.
Save eric-wood/2205710 to your computer and use it in GitHub Desktop.
Load real-time tweets into ActiveRecord (strips out location data)
#!/usr/bin/env ruby
require 'tweetstream'
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require APP_PATH
Rails.application.require_environment!
TweetStream.configure do |config|
config.consumer_key = 'redacted'
config.consumer_secret = 'redacted'
config.oauth_token = 'redacted'
config.oauth_token_secret = 'redacted'
config.auth_method = :oauth
config.parser = :json_gem
end
#TweetStream::Client.new.track('foursquare',
# San Franciso, College Station, Houston, Seattle
TweetStream::Client.new.locations([-123,36,-121,38,-96.51,30.42,-96.21,30.83,-95.86,29.43,-95.11,30.07,-122.43,47.51,-122.14,47.73],
:delete => Proc.new { |status_id, user_id|
Rails.logger.info "[TweetStream] Requesting to delete: #{status_id}"
},
:limit => Proc.new { |skip_count|
Rails.logger.info "[TweetStream] Limiting: #{skip_count}"
},
:error => Proc.new { |message|
Rails.logger.info "[TweetStream][#{Time.now}] TweetStream error: #{message}"
},
:reconnect => Proc.new { |timeout, retries|
Rails.logger.info "[TweetStream][#{Time.now}] Reconnect: #{timeout} secs on #{retries} retry"
}
) do |status|
Rails.logger.info "[TweetStream] Status: #{status.id}"
# Parse out the (structured) tweet message.
# If the message is unstructured, discard it
# Grab only tweets from foursquare
if status[:source] && status[:source].include?('foursquare')
# Parse out the location name from the tweet
matching = /I'm at (.*?) (\(|w\/|http)/.match(status[:text])
matching ||= /\(@ (([^w]|w[^\/])*)(w\/.*)?\)/.match(status[:text])
# matching ||= /the mayor of (.*) on @foursquare/.match(status[:text])
if matching
status[:name] = matching[1]
end
status[:url] = /(http:\/\/[^ ]*)$/.match(status[:text])
if status[:url]
status[:url] = status[:url][1]
end
#ap status
if status[:name] && status[:place] && status[:user] \
&& status[:geo] && status[:geo][:coordinates]
Checkin.create(
:place_id => status[:place][:id],
:place_name => status[:name].strip,
:post_date => DateTime.parse(status[:created_at]),
:url => status[:url],
:user_id => status[:user][:id_str],
:latitude => status[:geo][:coordinates][0],
:longitude => status[:geo][:coordinates][1]
)
#Rails.logger.info "[TweetStream] Adding checkin: #{status[:name].strip}"
puts status[:name].strip
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment