Last active
September 13, 2015 00:16
-
-
Save seanlinehan/d448a22ad2ce46029753 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
import oauth2 # <- pip install oauth2 | |
import time | |
# Oauth request helper. Mostly copy-pasta from Twitter's docs. | |
def oauth_req(url, http_method="GET"): | |
consumer = oauth2.Consumer(key='YOUR TWITTER API KEY', secret='YOUR TWITTER API SECRET') | |
client = oauth2.Client(consumer) | |
resp, content = client.request( url, method=http_method) | |
return content | |
twitter_user_ids = ['List', 'Of', 'Peoples', 'IDS'] | |
for i, twid in enumerate(twitter_user_ids): | |
# I like status updates, okay? | |
print "Working on %d (%d of %d)" % (twid, i+1, len(twitter_user_ids)) | |
# I've written enough scrapers in my life to know that things will break randomly and | |
# for a thousand reasons. Just catch them, tell me what happened, and move on. | |
try: | |
# Twitter limits the values returned to 5,000. I didn't write pagination code because I'm lazy. | |
response = oauth_req("https://api.twitter.com/1.1/friends/ids.json?user_id=%d" % twid) | |
obj = json.loads(response) | |
# File writing in fear. If I accidentally close the program, I don't want to be left high and dry. | |
with open('twitter_ids.txt', 'a') as fo: | |
# More status updates. | |
print "Found %d users" % len(obj['ids']) | |
print "" | |
for following_id in obj['ids']: | |
# Save both the user and who followed them | |
fo.write("%d,%d\n" % (following_id, twid)) | |
except Exception as e: | |
# ¯\_(ツ)_/¯ | |
print "Failure: " + e.message | |
# Twitter rates limits you to 15 requests in 15 minutes. Just sleep for a minute after each. | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment