Last active
October 20, 2015 07:50
-
-
Save akkijp/b28065b32f94c32d74e3 to your computer and use it in GitHub Desktop.
twitter で指定ユーザーのツイートを過去200件まで自動取得する。取得したデータは、sqlight3にて保存される。
This file contains hidden or 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 '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