Created
December 17, 2011 17:29
-
-
Save zgohr/1490812 to your computer and use it in GitHub Desktop.
Get screen names from Twitter REST API given file of user ids.
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
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