Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter の特定ユーザの 3200 発言を取得して YAML で保存する (適当)
$KCODE = 'UTF-8'
require 'rubygems'
require 'json'
require 'ya2yaml'
require 'oauth/cli/twitter' # gem install oauth-cli-twitter
# usage ruby twitter_get_all_statuses.rb username path/to/file
# TODO: 今何発言とったか表示できるようにする
# TODO: 最古だけじゃなく最新も取得できるようにする
# BUG : 最後の発言を延々ととり続けてしまう
class AllStatuses
include OAuth::CLI::Twitter
API_URL = 'http://twitter.com/statuses/user_timeline.json'
PIT_KEY = 'get-all-statuses'
def access_token
@access_token ||= get_access_token(:pit => PIT_KEY, :browser => true)
end
def url(max_id)
url = API_URL + "?screen_name=#{@user}"
url += "&max_id=#{max_id}" if max_id
puts url
url
end
def statuses(max_id)
statuses = JSON.parse(access_token.get(url(max_id)).body)
statuses.map {|s| s.delete('user'); s }
end
def last_id(path)
return nil if !File.exist?(path)
statuses = YAML.load(File.read(path))
statuses.last['id']
end
def start(user, path)
@user, @path = user, path
max_id = last_id(path)
file = File.open(path, 'a')
loop do
statuses = statuses(max_id)
statuses.each {|s| puts s['text'] }
file.write(statuses.to_yaml.sub(/---\s\n/, ''))
max_id = (statuses.last['id'] + 1).to_s
sleep 3
end
end
end
AllStatuses.new.start(ARGV[0], ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.