]> git.madduck.net Git - code/twitter-archiver.git/blob - followerdump.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

README updates
[code/twitter-archiver.git] / followerdump.py
1 #!/usr/bin/python3
2 #
3 # followerdump.py
4 #
5 # Dumps the list of your Twitter followers to stdout in the format
6 #
7 #   username <tab> displayname <tab> UID
8 #
9 # Copyright © 2017–2019 by martin f. krafft <madduck@madduck.net>
10 # Released under the Artistic Licence 2.0
11 #
12
13 from authdata import *
14
15 import twython
16 import itertools
17 import time
18 import sys
19
20 twitter = twython.Twython(app_key=consumer_key,
21         app_secret=consumer_secret,
22         oauth_token=access_token,
23         oauth_token_secret=access_secret)
24
25 def grouper(iterable, n, fillvalue=None):
26     "Collect data into fixed-length chunks or blocks"
27     args = [iter(iterable)] * n
28     return itertools.zip_longest(fillvalue=fillvalue, *args)
29
30 i=0
31 followers = twitter.cursor(twitter.get_followers_ids,
32         count=5000, stringify_ids=True)
33
34 #print('Obtained followers…', file=sys.stderr)
35
36 for chunk in grouper(followers, 100):
37     chunk = [c for c in chunk if c]
38     #print('  fetching user data for chunk of {0:d} users…'.format(len(chunk)),
39     #    file=sys.stderr)
40     n = 0
41     for follower in twitter.lookup_user(user_id=','.join(chunk)):
42         #print('    [{0:02d}] @{1:s}'.format(n, follower['screen_name']),
43         #        file=sys.stderr)
44         n += 1
45         print('\t'.join([follower[i] for i in ('screen_name','name','id_str')]))