piratebot/gen.py

38 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from mastodon import Mastodon
import argparse, sys, traceback, random, sqlite3, re
parser = argparse.ArgumentParser(description='Generate and post a toot.')
parser.add_argument('-s', '--simulate', dest='simulate', action='store_true',
help="Print the toot to stdout without posting it")
args = parser.parse_args()
client = Mastodon(
client_id="clientcred.secret",
access_token="usercred.secret",
api_base_url="https://botsin.space")
db = sqlite3.connect("dict.db")
db.text_factory=str
c = db.cursor()
data = c.execute(r'SELECT word, definition FROM entries WHERE word LIKE "%ar%" AND wordtype LIKE "n."').fetchall()
dset = random.choice(data)
toot = "What's a pirate's favourite {}?\nA {}!".format(
re.search(r"^([^.]+)", dset[1]).group(1).replace("\n ", ""), dset[0].lower().replace("ar", "a" + ('r' * random.randint(3,10)))
)
if not args.simulate:
try:
client.status_post(toot, visibility = 'unlisted')
except Exception as err:
toot = "Mistress @lynnesbian@fedi.lynnesbian.space, something has gone terribly" \
+ " wrong! While attempting to post a toot, I received the following" \
+ " error:\n" + "\n".join(traceback.format_tb(sys.exc_info()[2]))
client.status_post(toot, visibility = 'unlisted', spoiler_text = "Error!")
print(toot)