Significant portions of the code for this script are adapted from the iPython notebook files produced to support Mining the Social Web, 2nd Edition by Matthew A. Russell. The original files can be found here: Chapter 9: Twitter Cookbook.
!pip install twitter
import twitter
import time
import sys
#Elements of this code originally from: https://github.com/ptwobrussell/Mining-the-Social-Web-2nd-Edition
def oauth_login(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,CONSUMER_KEY, CONSUMER_SECRET):
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
return twitter_api
def make_twitter_request(twitter_api_func, max_errors=10, *args, **kw):
# A nested helper function that handles common HTTPErrors. Return an updated
# value for wait_period if the problem is a 500 level error. Block until the
# rate limit is reset if it's a rate limiting issue (429 error). Returns None
# for 401 and 404 errors, which requires special handling by the caller.
def handle_twitter_http_error(e, wait_period=2, sleep_when_rate_limited=True):
if wait_period > 3600: # Seconds
print('Too many retries. Quitting.')
raise e
# See https://dev.twitter.com/docs/error-codes-responses for common codes
if e.e.code == 401:
print('Encountered 401 Error (Not Authorized)')
return None
elif e.e.code == 404:
print('Encountered 404 Error (Not Found)')
return None
elif e.e.code == 429:
print('Encountered 429 Error (Rate Limit Exceeded): {}'.format(time.strftime("%c")))
if sleep_when_rate_limited:
print("Retrying in 15 minutes...ZzZ...")
sys.stderr.flush()
time.sleep(60*15 + 5)
print('...ZzZ...Awake now and trying again.')
return 2
else:
raise e # Caller must handle the rate limiting issue
elif e.e.code in (500, 502, 503, 504):
print('%s Encountered %i Error. Retrying in %i seconds' % \
(str(datetime.now()),e.e.code, wait_period))
time.sleep(wait_period)
wait_period *= 1.5
return wait_period
else:
raise e
wait_period = 2
error_count = 0
while True:
try:
return twitter_api_func(*args, **kw)
except twitter.api.TwitterHTTPError as e:
error_count = 0
wait_period = handle_twitter_http_error(e, wait_period)
if wait_period is None:
return
# Go to http://twitter.com/apps/new to create an app and get values
# for these credentials that you'll need to provide in place of these
# empty string values that are defined as placeholders.
# See https://dev.twitter.com/docs/auth/oauth for more information
# on Twitter's OAuth implementation.
#These are automatically generated when you create the Twitter app
#CONSUMER_KEY = 'YOUR_CONSUMER_KEY'
#CONSUMER_SECRET = 'YOUR_CONSUMER_SECRET'
#Generate this as the "Access Token"
#OAUTH_TOKEN = 'YOUR_OAUTH_ACCESS_TOKEN'
#OAUTH_TOKEN_SECRET = 'YOUR_OAUTH_ACCESS_TOKEN_SECRET'
#FILL IN YOUR DETAILS HERE
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
#ONCE YOU HAVE RUN THIS CELL, DELETE THE VALUES
#ONCE YOU HAVE FINISHED WITH THE APP, RESET OR DELETE THE KEYS ON THE TWITTER APP
#Generate a channel to the Twitter API
twitter_api = oauth_login(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,CONSUMER_KEY, CONSUMER_SECRET)
def get_user_lists(twitter_api, screen_name=None, page_sample=1):
assert (screen_name != None), \
"Must have screen_name"
tw_lists=[]
cursor=-1
while cursor != 0 and page_sample>0:
print("Getting lists for {}...".format(screen_name))
response = make_twitter_request(twitter_api.lists.memberships, screen_name=screen_name,cursor=cursor)
if response is None:
break
cursor = response['next_cursor']
tw_lists=tw_lists+response['lists']
page_sample=page_sample-1
return tw_lists
#Grab lists a user is a member of
tw_lists=get_user_lists(twitter_api,'cogdog',page_sample=100)
tw_lists[:5]
listnames = [ i['full_name'] for i in tw_lists ]
listnames
with open('myfile.txt','w') as outfile:
outfile.write("\n".join(listnames))
outfile.close()