Python script to toot on your mastodon instance from the cli
#!/usr/bin/env python3 | |
# pip3 install Mastodon.py | |
# | |
# use in the python cli | |
# ./tooter.py "your message here" | |
# or just ./tooter.py to be prompted | |
# sable cantus | |
import sys | |
import os | |
import pkg_resources | |
from pkg_resources import DistributionNotFound | |
from mastodon import Mastodon | |
# change this to be your mastodon server url | |
server = 'https://noagendasocial.com' | |
# get your access_token from the Development section | |
token = '<your-token-here>' | |
# make sure Mastodon.py is installed | |
try: | |
pkg_resources.require('mastodon.py') | |
except: | |
sys.exit('dependency needed: $ pip3 install Mastodon.py') | |
# check for toots at the cli | |
if len(sys.argv) > 1: | |
m = [] | |
message = '' | |
for i in range(len(sys.argv)): | |
m.append(sys.argv[i]) | |
m.pop(0) # removes the script name from the list | |
for word in m: | |
message = message + word | |
message = message + ' ' | |
else: | |
# get the message from the user | |
message = input('your message: ') | |
# don't post if there is no text | |
if len(message) == 0: | |
sys.exit("no message data entered") | |
# create an api instance | |
mastodon = Mastodon( | |
access_token = token, | |
api_base_url = server | |
) | |
# call the instance and toot, returning the toot DICT | |
tooter = mastodon.toot(message) | |
# print out the uri of the toot for confirmation | |
print("tooted: ", tooter['uri']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment