import argparse import os import requests from dotenv import load_dotenv def main(): ap = argparse.ArgumentParser( description='Export the list of followed users from GNU social', ) ap.add_argument( '-e', '--envfile', nargs='?', default='.env', ) ap.add_argument( '-o', '--outfile', nargs='?', default='following_accounts.csv', ) args = ap.parse_args() load_dotenv(args.envfile) hostname = os.environ['GNU_SOCIAL_HOST'] username = os.environ['GNU_SOCIAL_USER'] page = 1 accounts = [] while True: print('Fetching page %d' % page) r = requests.get( '{host}/api/statuses/friends.json?screen_name={username}&count=50&page={page}'.format(host=hostname, username=username, page=page), auth=('x', 'x'), ) data = r.json() if not data: break accounts += [ '{}@{}\n'.format( account['statusnet_profile_url'].split('/')[-1].lstrip('@'), account['statusnet_profile_url'].split('/')[2], ) for account in data ] page += 1 print('Writing %d accounts to %s' % (len(accounts), args.outfile)) with open(args.outfile, 'w') as f: f.writelines(accounts) if __name__ == '__main__': main()