Skip to content

Instantly share code, notes, and snippets.

@mayth
Created December 29, 2011 18:54
Show Gist options
  • Save mayth/1535592 to your computer and use it in GitHub Desktop.
Save mayth/1535592 to your computer and use it in GitHub Desktop.
ID to screen name and followers count without twitter gem
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