Skip to content

Instantly share code, notes, and snippets.

@zero-mstd
Last active November 2, 2024 17:58
Show Gist options
  • Save zero-mstd/97a55bf662fc1b426f0fd487a13d4b93 to your computer and use it in GitHub Desktop.
Save zero-mstd/97a55bf662fc1b426f0fd487a13d4b93 to your computer and use it in GitHub Desktop.
A tool for exporting Mastodon followers
###############################################################################
# A tool for exporting Mastodon followers
# Author: Zero
# Last update: 2022-02-09
###############################################################################
# Usage
#
# 1. Save this code as `export_mastodon_followers.py` in your computer.
#
# 2. Edit it, note that there are three places need to be changed:
#
# a. <your_access_token_here> (line 32)
# You can get your access token by: Settings -> Development ->
# NEW APPLICATION -> fill in an application name -> SUBMIT -> click your
# application name -> you will see your access token.
#
# b. <mastodon.example> (line 33)
# Replace it with the domain address of the instance you are using.
#
# c. <:id> (line 33)
# When you click one's avatar on Mastodon web UI, you can see the url
# is in this format: `https://<mastodon.example>/web/accounts/<:id>`, the
# last string of numbers is <:id>.
#
# 3. Execute this python file, and deal with the output as you like. For example,
# in Linux, you can `python export_mastodon_followers.py > followers_list`.
###############################################################################
import requests
import re
headers = {'Authorization': 'Bearer <your_access_token_here>'}
url = 'https://<mastodon.example>/api/v1/accounts/<:id>/followers'
i = 1
while 1:
fl = requests.get(url, headers = headers)
fj = fl.json()
for e in fj:
acct = e["acct"]
print(str(i) + ' ' + acct)
i = i + 1
if (not 'Link' in fl.headers) or (not 'rel="next"' in fl.headers['Link']):
break
else:
next_url = re.match(r"\<(.*?)\>", fl.headers['Link']).groups()[0]
url = next_url
@p37307
Copy link

p37307 commented Jun 18, 2023

Works good. URL for <:ID> is different on my instance than it says in the instructions. To get the needed ID, I had to click on my profile image, and it is all the numbers after avatars and before original, minus the slashes. instanceURL/accounts/avatars/100/100/100/100/100/100/original/7b0a791cfbb5b18e.gif
For example, the ID would be 100100100100100100
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment