Skip to content

Instantly share code, notes, and snippets.

@alea12
Created December 27, 2015 07:10
Show Gist options
  • Save alea12/21b8ebfff727f8b0798c to your computer and use it in GitHub Desktop.
Save alea12/21b8ebfff727f8b0798c to your computer and use it in GitHub Desktop.
test.rb
require 'big_query'
require 'twitter'
require 'pp'
class HTTP::URI
def port
443 if self.https?
end
end
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
streaming_client = Twitter::Streaming::Client.new do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = ACCESS_TOKEN
config.access_token_secret = ACCESS_TOKEN_SECRET
end
opts = {}
opts['client_id'] = ''
opts['service_email'] = ''
opts['key'] = ''
opts['project_id'] = ''
opts['dataset'] = ''
bq = BigQuery::Client.new(opts)
if bq.tables.empty?
table_name = 'tweets'
table_schema = { status_id: { type: 'INTEGER' },
screen_name: { type: 'STRING' },
text: { type: 'STRING' },
created_at: { type: 'TIMESTAMP' },
json: { type: 'STRING' },
deleted: { type: 'INTEGER' } }
bq.create_table(table_name, table_schema)
end
streaming_client.user do |object|
if object.kind_of? Twitter::Tweet
data = {
'status_id' => object.id,
'screen_name' => object.user.screen_name,
'text' => object.text,
'created_at' => object.created_at.strftime("%Y-%m-%d %H:%M:%S"),
'json' => object.to_h.to_json,
'deleted' => 0
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment