Last active
April 27, 2023 21:31
-
-
Save bison--/657b7df736c62c7ac46fd3b24f34ce6f to your computer and use it in GitHub Desktop.
a tiny cli script that toots mastodon toots with some default settings from the account
This file contains 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
ACCESS_TOKEN = '<ACCESS_TOKEN>' # create a new app under: https://INSTANCE_DOMAIN/settings/applications | |
INSTANCE_DOMAIN = 'mastodon.social' | |
LANGUAGE = 'en' # fallback language |
This file contains 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 # pip install requests | |
import conf | |
# API endpoint for Mastodon | |
# eg <instance_name>.mastodon.social / mastodon.social | |
base_url = "https://{}/api/v1/".format(conf.INSTANCE_DOMAIN) | |
# Authentication details | |
headers = {"Authorization": "Bearer " + conf.ACCESS_TOKEN} | |
# Get current visibility setting | |
response = requests.get(base_url + "accounts/verify_credentials", headers=headers) | |
visibility = 'private' # fallback | |
language = '' | |
if response.status_code == 200: | |
visibility = response.json()["source"]["privacy"] | |
language = response.json()["source"]["language"] # iso2 | |
# when empty string, settings are set to use "interface" language by default | |
if language == '': | |
language = conf.LANGUAGE | |
else: | |
print("Error retrieving visibility settings.") | |
# show toot settings | |
print('visibility:', visibility) | |
print('language:', language) | |
# Publish Toot | |
toot_text = input('enter toot: ') | |
response = requests.post(base_url + "statuses", headers=headers, json={ | |
"status": toot_text, | |
"visibility": visibility, | |
'language': language | |
}) | |
# Check if the publication was successful | |
if response.status_code == 200: | |
print("Toot published!") | |
else: | |
print("Error publishing Toot.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment