Skip to content

Instantly share code, notes, and snippets.

@stuartlangridge
Created February 15, 2023 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartlangridge/d4eda44155d2ceb191d13d52943b4ea9 to your computer and use it in GitHub Desktop.
Save stuartlangridge/d4eda44155d2ceb191d13d52943b4ea9 to your computer and use it in GitHub Desktop.
A little Python script to fetch all of a person's Mastodon followers and followings and see which servers they're on
#!/usr/bin/env python3
import requests
import requests_cache
import json
requests_cache.install_cache('masto_cache')
MASTODON_USERNAME = "sil@mastodon.social"
def fetch(endpoint):
url = f"https://{server}/api/v1/accounts/{account_id}/{endpoint}"
peeps = []
while True:
print("get url", url)
response = requests.get(url)
j = response.json()
if len(j) == 0: break
addresses = [x["acct"] for x in j]
naddresses = []
for address in addresses:
naddresses.append(f"{address}@{server}" if "@" not in address else address)
peeps += naddresses
link = response.headers["Link"].split(", ")
first_url, first_rel = link[0].split("; ")
if len(link) == 1:
break
else:
second_url, second_rel = link[1].split("; ")
if "next" in first_rel: url = first_url.strip()[1:-1]
elif "next" in second_rel: url = second_url.strip()[1:-1]
else:
raise Exception(f"Didn't understand Link header: {response.headers['Link']}")
return peeps
server = MASTODON_USERNAME.split("@")[1]
user_account_url = f"https://{server}/api/v1/accounts/lookup?acct={MASTODON_USERNAME}"
account_id = requests.get(user_account_url).json()["id"]
followers = fetch("followers")
following = fetch("following")
servers = {}
for f in followers + following:
name, s = f.split("@")
if s not in servers: servers[s] = 0
servers[s] += 1
print(sorted(servers.items(), key=lambda x:x[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment