1
0
Fork 0
mirror of https://github.com/Lynnesbian/FediBooks/ synced 2024-11-25 08:38:59 +00:00

seperated text generation and posting into their own functions

This commit is contained in:
Lynne Megido 2020-01-20 12:53:11 +10:00
parent 841098cc18
commit 898f2f7aae
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

View file

@ -34,13 +34,7 @@ def extract_post(post):
text = text.rstrip("\n") # remove trailing newline(s) text = text.rstrip("\n") # remove trailing newline(s)
return text return text
def make_post(args): def generate_output(handle):
id = None
acct = None
if len(args) > 1:
id = args[1]
acct = args[3]
handle = args[0]
db = MySQLdb.connect( db = MySQLdb.connect(
host = cfg['db_host'], host = cfg['db_host'],
user=cfg['db_user'], user=cfg['db_user'],
@ -128,25 +122,40 @@ def make_post(args):
# also format handles without instances, e.g. @user instead of @user@instan.ce # also format handles without instances, e.g. @user instead of @user@instan.ce
post = re.sub(r"(?<!\S)@(\w+)", r"@{}\1".format(zws), post) post = re.sub(r"(?<!\S)@(\w+)", r"@{}\1".format(zws), post)
# print(post) return post
visibility = bot['post_privacy'] if len(args) == 1 else args[2]
visibilities = ['public', 'unlisted', 'private']
if visibilities.index(visibility) < visibilities.index(bot['post_privacy']):
# if post_privacy is set to a more restricted level than the visibility of the post we're replying to, use the user's setting
visibility = bot['post_privacy']
if acct is not None:
post = "{} {}".format(acct, post)
# ensure post isn't longer than bot['length']
# TODO: ehhhhhhhhh def make_post(args):
post = post[:bot['length']] id = None
# send toot!! acct = None
try: if len(args) > 1:
client.status_post(post, id, visibility = visibility, spoiler_text = bot['content_warning']) id = args[1]
except MastodonUnauthorizedError: acct = args[3]
# user has revoked the token given to the bot handle = args[0]
# this needs to be dealt with properly later on, but for now, we'll just disable the bot
c.execute("UPDATE bots SET enabled = FALSE WHERE handle = %s", (handle,)) post = generate_output(handle)
# print(post)
visibility = bot['post_privacy'] if len(args) == 1 else args[2]
visibilities = ['public', 'unlisted', 'private']
if visibilities.index(visibility) < visibilities.index(bot['post_privacy']):
# if post_privacy is set to a more restricted level than the visibility of the post we're replying to, use the user's setting
visibility = bot['post_privacy']
if acct is not None:
post = "{} {}".format(acct, post)
# ensure post isn't longer than bot['length']
# TODO: ehhhhhhhhh
post = post[:bot['length']]
# send toot!!
print(post)
return
try:
client.status_post(post, id, visibility = visibility, spoiler_text = bot['content_warning'])
except MastodonUnauthorizedError:
# user has revoked the token given to the bot
# this needs to be dealt with properly later on, but for now, we'll just disable the bot
c.execute("UPDATE bots SET enabled = FALSE WHERE handle = %s", (handle,))
if id == None: if id == None:
# this wasn't a reply, it was a regular post, so update the last post date # this wasn't a reply, it was a regular post, so update the last post date