Skip to content

Instantly share code, notes, and snippets.

@madduck
Created May 29, 2017 18:07
Show Gist options
  • Save madduck/982e797f45376f1490790c8073e7810d to your computer and use it in GitHub Desktop.
Save madduck/982e797f45376f1490790c8073e7810d to your computer and use it in GitHub Desktop.
Dump the list of Twitter followers in batches
#!/usr/bin/python3
from authdata import *
import twython
import itertools
import time
import sys
twitter = twython.Twython(app_key=consumer_key,
app_secret=consumer_secret,
oauth_token=access_token,
oauth_token_secret=access_secret)
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
args = [iter(iterable)] * n
return itertools.zip_longest(fillvalue=fillvalue, *args)
i=0
followers = twitter.cursor(twitter.get_followers_ids,
count=5000, stringify_ids=True)
print('Obtained followers…', file=sys.stderr)
for chunk in grouper(followers, 100):
chunk = [c for c in chunk if c]
print(' fetching user data for chunk of {0:d} users…'.format(len(chunk)),
file=sys.stderr)
n = 0
for follower in twitter.lookup_user(user_id=','.join(chunk)):
print(' [{0:02d}] @{1:s}'.format(n, follower['screen_name']),
file=sys.stderr)
n += 1
print('\t'.join([follower[i] for i in ('screen_name','name','id_str')]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment