Created
November 16, 2010 12:49
-
-
Save tily/701789 to your computer and use it in GitHub Desktop.
Twitter の特定ユーザの 3200 発言を取得して YAML で保存する (適当)
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
$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