Skip to content

Instantly share code, notes, and snippets.

@zgohr
Created December 17, 2011 17:29
Show Gist options
  • Save zgohr/1490812 to your computer and use it in GitHub Desktop.
Save zgohr/1490812 to your computer and use it in GitHub Desktop.
Get screen names from Twitter REST API given file of user ids.
import sys
import urllib2
import simplejson
def get_names(user_ids):
output_file = open('output.txt', 'a')
get_string = 'https://api.twitter.com/1/users/lookup.json?user_id={0}'.format(user_ids)
req = urllib2.Request(get_string)
try:
f = urllib2.urlopen(req)
response = f.read()
response = simplejson.loads(response)
for user in response:
output_file.write(user['screen_name'] + '\n')
except urllib2.HTTPError, e:
print e
output_file.close()
if (len(sys.argv) > 1):
file_name = sys.argv[1]
input_file = open(file_name, 'r+')
lines = input_file.readlines()
user_ids = ''
count = 0
for line in lines:
if line:
line = line.strip()
user_ids += line + ','
count += 1
if count == 99:
get_names(user_ids)
user_ids = ''
count = 0
if user_ids:
get_names(user_ids)
input_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment