Skip to content

Instantly share code, notes, and snippets.

@hitode909
Forked from tily/twitter_get_all_statuses.rb
Created November 23, 2010 03:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/711208 to your computer and use it in GitHub Desktop.
Save hitode909/711208 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
$KCODE = 'UTF-8'
require 'rubygems'
require 'json'
require 'ya2yaml'
require 'oauth/cli/twitter' # gem install oauth-cli-twitter
BANNER = "usage ruby twitter_get_all_statuses.rb username path/to/file"
# TODO: 今何発言とったか表示できるようにする
# TODO: 最古だけじゃなく最新も取得できるようにする
class AllStatuses
include OAuth::CLI::Twitter
API_URL = 'http://api.twitter.com/1/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}&count=50&trim_user=true"
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 unless File.size?(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.shift if max_id
break if statuses.empty?
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
unless ARGV.length == 2
puts BANNER
exit 1
end
AllStatuses.new.start(ARGV[0], ARGV[1])
# -*- coding: utf-8 -*-
$KCODE = 'UTF-8'
require 'rubygems'
require 'json'
require 'yaml'
BANNER = "usage ruby yaml_to_text.rb path/to/file.yaml > output.txt"
module YamlToText
def self.process(path)
yaml = YAML.load_file path
yaml.reverse.each{ |status|
puts status["text"]
}
end
end
unless ARGV.length == 1
puts BANNER
exit 1
end
YamlToText.process(ARGV.first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment