-
-
Save hitode909/711208 to your computer and use it in GitHub Desktop.
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
# -*- 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]) |
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
# -*- 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