Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # In this house, we don't import all of Pandas just write a bloody .csv
- from csv import DictWriter
- from datetime import datetime
- import sys
- import time
- # In this house, we import the standard library *first*:
- # https://peps.python.org/pep-0008/#imports
- from atproto import Client
- # In this house, functions are first-class objects.
- def how_long(f, *args, **kwargs):
- then = datetime.now()
- res = f(*args, **kwargs)
- now = datetime.now()
- return res, (now - then).total_seconds()
- # In this house, we avoid extraneous whitespace:
- # https://peps.python.org/pep-0008/#pet-peeves
- def all_followers(client, handle, batch=100):
- api_args = {'actor': handle, 'limit': batch}
- chunk, latency = how_long(client.bsky.graph.get_followers, api_args)
- while chunk.cursor:
- yield from chunk.followers
- # Very slighty throttle the API calls...
- time.sleep(latency)
- chunk, latency = how_long(client.bsky.graph.get_followers,
- {**api_args, **{'cursor': chunk.cursor}})
- def prof2dict(prof):
- return {k: prof.__dict__.get(k) for k in __PROFILE_KEYS__}
- __PROFILE_KEYS__ = ('handle', 'avatar', 'description', 'displayName')
- def dump_followers(client, handle, fname=None):
- if fname is None:
- fname = '_'.join(handle.split('.')) + '.csv'
- with open(fname, 'w') as csvfile:
- writer = DictWriter(csvfile, __PROFILE_KEYS__)
- writer.writeheader()
- writer.writerows(map(prof2dict, all_followers(client, handle)))
- # In this house, typing 'bsky.social' gets tedious.
- def bskyify(name):
- if len(name.split('.')) < 2:
- return '.'.join((name, 'bsky', 'social'))
- else:
- return name
- if __name__ == '__main__':
- if len(sys.argv) != 4:
- print('USAGE: username, password, target username')
- else:
- _, uname, pw, target = sys.argv
- uname = bskyify(uname)
- target = bskyify(target)
- at_client = Client()
- try:
- at_client.login(uname, pw)
- except:
- at_client = None
- print("Couldn't log-in!")
- if at_client:
- dump_followers(at_client, target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement