Created
December 27, 2015 08:55
-
-
Save alea12/c1d4975c8b7196be866f to your computer and use it in GitHub Desktop.
deleted_tweets.rb
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 'twitter' | |
require 'big_query' | |
# Twitter Gem のバグを回避 | |
class HTTP::URI | |
def port | |
443 if self.https? | |
end | |
end | |
# Twitter API 認証情報 | |
CONSUMER_KEY = '' | |
CONSUMER_SECRET = '' | |
ACCESS_TOKEN = '' | |
ACCESS_TOKEN_SECRET = '' | |
# Twitter Client の新規作成 | |
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 | |
# GCP API 認証情報 | |
opts = {} | |
opts['client_id'] = '' | |
opts['service_email'] = '' | |
opts['key'] = '' | |
opts['project_id'] = '' | |
opts['dataset'] = '' | |
bq = BigQuery::Client.new(opts) | |
if bq.tables.empty? | |
table_names = ['tweets', 'deleted_tweets'] | |
table_names.each do |table_name| | |
table_name = table_name | |
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 | |
end | |
streaming_client.user do |object| | |
if object.kind_of? Twitter::Streaming::DeletedTweet | |
status_id = object.id | |
begin | |
res = bq.query("SELECT * FROM [dataset.tweets] where status_id=#{status_id} LIMIT 1") | |
res = res['rows'][0]['f'] | |
data = { | |
'status_id' => res[0]['v'], | |
'screen_name' => res[1]['v'], | |
'text' => res[2]['v'], | |
'json' => res[4]['v'] | |
} | |
puts data['screen_name'].to_s + ": " + data['text'] | |
bq.insert('deleted_tweets', data) | |
puts "---削除されたツイートのデータを保存した---" | |
rescue | |
puts "---削除されたツイートのデータがなかった---" | |
end | |
end | |
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 | |
} | |
bq.insert('tweets', data) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment