Skip to content

Instantly share code, notes, and snippets.

@alea12
Created December 31, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alea12/6468fed6de0df1ea9e1a to your computer and use it in GitHub Desktop.
Save alea12/6468fed6de0df1ea9e1a to your computer and use it in GitHub Desktop.
require 'twitter'
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
host: 'localhost',
username: 'root',
database: 'twitter',
# For emoji
charset: 'utf8mb4',
encoding: 'utf8mb4',
collation: 'utf8mb4_general_ci'
)
class Tweet < ActiveRecord::Base
self.primary_key = :id
end
# Aboid Twitter Gem Bug: https://github.com/sferik/twitter/issues/709
class HTTP::URI
def port
443 if self.https?
end
end
streaming_client = Twitter::Streaming::Client.new do |config|
config.consumer_key = ''
config.consumer_secret = ''
config.access_token = ''
config.access_token_secret = ''
end
streaming_client.user do |object|
if object.kind_of? Twitter::Streaming::DeletedTweet
begin
t = Tweet.find(object.id)
t.deleted = 1
t.save
puts "つい消しを見た"
rescue ActiveRecord::RecordNotFound
puts "つい消しを見たけど知らないやつだった"
end
elsif object.kind_of? Twitter::Tweet
t = Tweet.new
t.id = object.id
t.screen_name = object.user.screen_name
t.text = object.text
t.created_at = object.created_at.strftime("%Y-%m-%d %H:%M:%S")
t.json = object.to_h.to_json
t.deleted = 0
t.save
end
end
create database twitter;
use twitter;
create table tweets (
`id` bigint not null,
`screen_name` varchar(128) not null,
`text` text character set utf8mb4 not null,
`created_at` datetime not null,
`json` longtext character set utf8mb4,
`deleted` tinyint default 0
) default charset=utf8mb4;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment