Download the list of followers from your Mastodon account
It recently came to my mind that it might be handy to have a list of all the accounts that I follow on Mastodon. I can surely see them on the web or in any decent client, but as a backup it would be great to have a list to store somewhere.
As I already played a little with the Mastodon API I decided to write a short script that downloads a list of user@instance values. And as I am using Python only occasionally, I decided to do this script in Python.
Of course, we need a Mastodon account. And in this account we need to set up an application to access our account data with an access token. To do this, we need to go to the settings page of our Mastodon account and select the Development entry.
We create a new application, the only access rights we need to provide is read access; I used the global read access here, because I might try to get some other data later as well.
After that, select the new created application and copy the value of the access token. Keep it secret, don’t give it to anybody.
You need python3 installed on your machine and the Mastodon.py library. You can install it with pip:
pip install Mastodon.py
The Python Script
The following python script reads the accounts you are following, the first parameter to be passed is the URL of the instance (for example https://infosec.exchange), the second parameter is the access token.
#!/usr/bin/env python3 import argparse from mastodon import Mastodon parser = argparse.ArgumentParser(description='Download the list of accounts you are following') parser.add_argument('api_base_url', help='The api url of your mastodon server') parser.add_argument('access_token', help='The access token of your mastodon account') args = parser.parse_args() # Set up the mastodon client by passing in the access token and the api url of the server mastodon = Mastodon(access_token=args.access_token, api_base_url=args.api_base_url) # Get the id of the account userId = mastodon.account_verify_credentials().id # now get the accounts we are following and keep only the account names accounts =  following = mastodon.account_following(userId) while following != None : accounts += list(map(lambda x: x.acct, following)) following = mastodon.fetch_next(previous_page = following) print(*accounts, sep='\n')
The script can be downloaded here. An example call from a test account where I just follow my main Mastodon account:
./mastodon-list-followers.py https://infosec.exchange <access_token> firstname.lastname@example.org
Perhaps this is useful to someone 😎