-
-
Save gregorycoleman/f049dc7530fa7349c8c1f9151b0bae25 to your computer and use it in GitHub Desktop.
Loads a list of followers from the Twitter REST API and steps through them, saving basic information about their follower and friend counts.
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
| import simplejson as json | |
| import urllib | |
| followers = json.load(open("followers.json"))["ids"] | |
| offset = 100 | |
| alldata = [] | |
| csv = "name\tfollowers\tfriends\tstatuses\tfollowing\n" | |
| for i in range(1+len(followers) / offset): | |
| idx = i * offset | |
| users = followers[idx:idx+offset] | |
| user_id = ','.join(str(x) for x in users) | |
| print 'loading '+str(len(users))+' users from '+str(idx) | |
| url = 'https://api.twitter.com/1/users/lookup.json' | |
| params = urllib.urlencode({'user_id': user_id}) | |
| data = urllib.urlopen(url, params).read() | |
| alldata += json.loads(data) | |
| for user in alldata: | |
| csv += '\t'.join(str(x) for x in [ | |
| user['screen_name'], | |
| user['followers_count'], | |
| user['friends_count'], | |
| user['statuses_count'], | |
| user['following']]) + '\n' | |
| open('cache/'+str(i)+'.json','w').write(data) | |
| open('all-followers.csv','w').write(csv) | |
| open('all-followers.json','w').write(json.dumps(alldata)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment