Skip to content

Instantly share code, notes, and snippets.

@jvns
Last active August 13, 2023 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jvns/0fe51383cbbb63e94177c60f1e0371c6 to your computer and use it in GitHub Desktop.
Save jvns/0fe51383cbbb63e94177c60f1e0371c6 to your computer and use it in GitHub Desktop.
import requests
import sys
import time
def get_user_id(domain, username):
# https://social.jvns.ca/api/v1/accounts/lookup?acct=@b0rk
url = f"https://{domain}/api/v1/accounts/lookup?acct=@{username}"
response = requests.get(url)
assert response is not None and response.status_code == 200
return response.json()["id"]
def get_all_followers(domain, username):
user_id = get_user_id(domain, username)
url = f"https://{domain}/api/v1/accounts/{user_id}/followers"
while url:
response = requests.get(url)
while response is None or response.status_code != 200:
print("maybe rate limited, sleeping for 10 seconds", file=sys.stderr)
time.sleep(5)
response = requests.get(url)
url = response.links.get("next", {}).get("url")
for user in response.json():
acct = user["acct"]
if "@" not in acct:
acct += "@mastodon.social"
print(acct)
sys.stdout.flush()
# try to be nice and not hammer the API
time.sleep(0.5)
def main():
if len(sys.argv) != 2:
print("Usage: python3 get-all-followers.py some.mastodon.instance/@username")
return
profile_url = sys.argv[1]
domain, username = profile_url.split("@")
if domain.startswith("https://"):
domain = domain[8:]
get_all_followers(domain, username)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment