Skip to content

Instantly share code, notes, and snippets.

@miketheman
Last active December 22, 2022 19:20
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 miketheman/c6d60e6efb277f73abf57387c8e76085 to your computer and use it in GitHub Desktop.
Save miketheman/c6d60e6efb277f73abf57387c8e76085 to your computer and use it in GitHub Desktop.
Add a bunch of AWS Heroes (and some other folks, too) to a Mastodon list.
"""
Add users to a Mastodon instance list.
Uses https://mastodonpy.readthedocs.io/
"""
from os import environ
from pathlib import Path
from mastodon import Mastodon
from mastodon.errors import MastodonNotFoundError, MastodonServiceUnavailableError
HERO_MASTODON_ACCOUNTS = [
"@abjoerne@awscommunity.social",
"@andreas@social.cloudonaut.io",
"@ArcticF0xx@hachyderm.io",
"@ben11kehoe@mastodon.cloud",
"@CalvinHP@fosstodon.org",
"@donkersgoed@hachyderm.io",
"@esh@awscommunity.social",
"@fz@awscommunity.social",
"@gaurav@awscommunity.social",
"@hoegertn@mastodon.social",
"@iamstan@hachyderm.io",
"@iamstan@infosec.exchange",
"@iamvlaaaaaaad@hachyderm.io",
"@ian@ian.mn",
"@lizrice@hachyderm.io",
"@margaretvaltie@awscommunity.social",
"@marknca@infosec.exchange",
"@metaskills@hachyderm.io",
"@miketheman@hachyderm.io",
"@mistwire@fosstodon.org",
"@nideveloper@hachyderm.io",
"@ogrodnek@hachyderm.io",
"@osterjour@awscommunity.social",
"@pgarbe@awscommunity.social",
"@prashanth@awscommunity.social",
"@robcube@hachyderm.io",
"@Sathyabhat@mastodon.social",
"@theburningmonk@hachyderm.io",
"@tmclaugh@mstdn.social",
"@walid@hachyderm.io",
# requires accepting follow request first
"@danilop@awscommunity.social",
"@hiroko@mastodon.social",
"@ianuragkale@awscommunity.social",
"@mnapoli@phpc.social",
"@renato@awscommunity.social",
]
NON_HERO_MASTODON_ACCOUNTS = [
# Not exactly Heroes, but good folks nonetheless
"@ajaynairthinks@hachyderm.io",
"@emilyfreeman@hachyderm.io",
"@farrah@awscommunity.social",
"@gunnargrosch@hachyderm.io",
"@jeffbarr@awscommunity.social",
"@mreferre@awscommunity.social",
"@rossbarich@awscommunity.social",
]
def authenticate() -> None:
"""Authenticate to Mastodon.
This works when you have the environment variables set.
You must set the following environment variables:
MASTODON_URL: The URL of the Mastodon instance to authenticate to
MASTODON_USER: The username of the user to authenticate as
MASTODON_PASS: The password of the user to authenticate as
in your environment via:
export MASTODON_URL="https://hachyderm.io"
export MASTODON_USER="yourusername"
export MASTODON_PASS="yourpassword"
"""
# Only authenticate if we don't have the credentials already
if (
Path("mastolist_clientcred.secret").exists()
and Path("mastolist_usercred.secret").exists()
):
return
print("Authenticating to Mastodon")
Mastodon.create_app(
"mastolist",
api_base_url=environ["MASTODON_URL"],
to_file="mastolist_clientcred.secret",
)
mastodon = Mastodon(
client_id="mastolist_clientcred.secret",
)
mastodon.log_in(
username=environ["MASTODON_USER"],
password=environ["MASTODON_PASS"],
to_file="mastolist_usercred.secret",
)
def get_or_create_list(mastodon: Mastodon, list_name: str) -> int:
my_lists = mastodon.lists()
for l in my_lists:
if l["title"] == list_name:
return l["id"]
return mastodon.list_create(list_name)["id"]
def main():
"""Do the thing."""
# Once really.
authenticate()
mastodon = Mastodon(access_token="mastolist_usercred.secret")
aws_heroes_list = get_or_create_list(mastodon, "AWS Heroes")
aws_heroes_accounts = mastodon.list_accounts(aws_heroes_list)
for account in HERO_MASTODON_ACCOUNTS + NON_HERO_MASTODON_ACCOUNTS:
print(account)
try:
found = mastodon.search(account)["accounts"]
except MastodonServiceUnavailableError:
print(" Mastodon Service Unavailable. Skipping.")
continue
if not found:
print(f"Couldn't find {account}")
continue
# if not already on the list, add it
if found[0]["id"] not in [a["id"] for a in aws_heroes_accounts]:
print(f" Adding {found[0]['acct']}, id: {found[0]['id']}")
try:
mastodon.list_accounts_add(aws_heroes_list, found[0]["id"])
except MastodonNotFoundError:
print(
f" Couldn't add {found[0]['acct']}\n"
" Usually means account requires accepting a Follow Request first."
)
print("Done")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment