Skip to content

Instantly share code, notes, and snippets.

@hgmnz
Created February 4, 2012 13:02
Show Gist options
  • Save hgmnz/1737721 to your computer and use it in GitHub Desktop.
Save hgmnz/1737721 to your computer and use it in GitHub Desktop.
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
@andrasio
Copy link

andrasio commented Feb 4, 2012

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment