Created
December 29, 2011 18:54
-
-
Save mayth/1535592 to your computer and use it in GitHub Desktop.
ID to screen name and followers count without twitter gem
This file contains 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 'optparse' | |
require 'net/http' | |
require 'json' | |
out_file = '' | |
opt = OptionParser.new | |
opt.on('-o output') {|v| out_file = v} | |
opt.parse!(ARGV) | |
abort('no source file') if ARGV.size == 0 | |
src_file = ARGV[0] | |
abort('no source file') if src_file.empty? | |
if out_file.empty? | |
out_file = src_file + '.out.csv' | |
end | |
# show opts | |
puts '-- opts --' | |
puts 'source file = ' + src_file | |
puts 'output file = ' + out_file | |
puts '-- run --' | |
# get users | |
open(src_file, 'r') do |src| | |
open(out_file, 'w') do |dst| | |
src.each_slice(100) do |ids| | |
body = Net::HTTP.get(URI('http://api.twitter.com/1/users/lookup.json?' + URI.encode_www_form({'user_id' => ids.join(',')}))) | |
json = JSON.parse(body) | |
json.each do |user| | |
dst.puts("#{user['id']},#{user['screen_name']},#{user['followers_count']}") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment