/get-followers.py Secret
Last active
August 13, 2023 20:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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