Created
February 4, 2012 13:02
-
-
Save hgmnz/1737721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'tweetstream' | |
require 'sequel' | |
DB = Sequel.connect('postgres:///bostonio') | |
TweetStream.configure do |config| | |
config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] | |
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] | |
config.oauth_token = ENV['TWITTER_OAUTH_TOKEN'] | |
config.oauth_token_secret = ENV['TWITTER_OAUTH_TOKEN_SECRET'] | |
config.auth_method = :oauth | |
end | |
def to_pg_array(array) | |
cleaned_up = array.reject(&:nil?).reject(&:empty?) | |
"{ #{cleaned_up.join(',')} }" unless cleaned_up.empty? | |
end | |
keywords = %w(boston.io bostonio boston conference conf) | |
TweetStream::Client.new.track(keywords) do |status| | |
begin | |
putc "." | |
hashtags = to_pg_array(status[:entities][:hashtags].map { |hashtag| hashtag["text"] }) | |
urls = to_pg_array(status[:entities][:urls].map { |url| url["expanded_url"] }) | |
DB[:tweets].insert(screen_name: status[:user][:screen_name], | |
text: status[:text], | |
hashtags: hashtags, | |
urls: urls, | |
created_at: status[:created_at]) | |
rescue Exception => e | |
puts | |
puts "Error saving tweet, oh well. #{e.message}" | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:)