Last active
January 2, 2016 10:29
-
-
Save danpalmer/8290617 to your computer and use it in GitHub Desktop.
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 requests | |
import math | |
import unicodecsv as csv | |
import time | |
from requests_oauthlib import OAuth1 | |
APP_TOKEN = '' | |
APP_SECRET = '' | |
USER_TOKEN = '' | |
USER_SECRET = '' | |
USERNAME = 'psythor' | |
USER_ID = '629133' | |
RATE_LIMIT_SLEEP = 20 | |
ids_url = 'https://api.twitter.com/1.1/followers/ids.json?screen_name=' + \ | |
USERNAME + '&user_id=' + USER_ID | |
info_url = 'https://api.twitter.com/1.1/users/lookup.json' | |
auth = OAuth1(APP_TOKEN, APP_SECRET, USER_TOKEN, USER_SECRET) | |
ids_request = requests.get(ids_url, auth=auth) | |
user_ids = [] | |
for user_id in ids_request.json()['ids']: | |
user_ids.append(str(user_id)) | |
print("Got Follower IDs") | |
runtime = int((RATE_LIMIT_SLEEP + 2) * (len(user_ids) / 100.0)) | |
print("This script will take approximately %i seconds" % runtime) | |
print("because Twitter rate limit requests.") | |
with open('followers.csv', 'wb') as f: | |
w = None | |
batches = int(math.ceil(len(user_ids) / 100.0)) | |
for i in range(0, batches): | |
print("Getting users %i to %i" % (100*i, 100*(i+1))) | |
ids = ','.join(user_ids[100*i:100*(i+1)]) | |
user_request = requests.post(info_url, data={'user_id':ids}, auth=auth) | |
if user_request.status_code != 200: | |
print(user_request.text) | |
print("Failed to get URL: " + info_url) | |
print(" for user_ids: " + ids) | |
for user in user_request.json(): | |
user = dict((key,value) for key, value in user.iteritems() if not isinstance(value, dict)) | |
if not w: | |
w = csv.DictWriter(f, user.keys(), extrasaction='ignore') | |
w.writeheader() | |
w.writerow(user) | |
time.sleep(RATE_LIMIT_SLEEP) | |
print("Successful") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment