Skip to content

Instantly share code, notes, and snippets.

@akkijp
Last active October 20, 2015 07:50
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 akkijp/b28065b32f94c32d74e3 to your computer and use it in GitHub Desktop.
Save akkijp/b28065b32f94c32d74e3 to your computer and use it in GitHub Desktop.
twitter で指定ユーザーのツイートを過去200件まで自動取得する。取得したデータは、sqlight3にて保存される。
require 'rubygems'
require 'sqlite3'
require 'twitter'
require 'pp'
# ログイン
client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
SQLite3::Database.new("twitter.db") do |db|
# db.execute("create table tweet(post_id int, text text, source text);")
users =
[
"RakutenJP",
"Reuters_co_jp",
"WSJJapan",
"YOL_national",
"Yomiuri_Online",
"asahi",
"fsa_JAPAN",
"hatenablog",
"mainichi_kokura",
"mainichijpedit",
"mainichijpnews",
"med_premier",
"nhk_news",
"nikkei",
"suntory",
"takamatsu_mai",
"tenkijp",
"ymobileOfficial"
]
users.each do |user|
client.user_timeline(user, count: 200).map { |t|
post_id = t.attrs[:id]
text = t.attrs[:text]
source= t.attrs[:source]
tweet = db.execute("select * from tweet where post_id=?", post_id)
sql = "INSERT OR REPLACE INTO tweet VALUES (?, ?, ?)"
db.execute(sql, post_id, text, source) if tweet.size == 0
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment