2020-01-19 11:13:46 +00:00
#!/usr/bin/env python3
2020-01-19 11:53:10 +00:00
# Copyright (C) 2020 Lynne (@lynnesbian@fedi.lynnesbian.space)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2020-01-19 11:13:46 +00:00
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 ( )
2020-01-19 11:20:56 +00:00
if not args . simulate :
client = Mastodon (
2020-01-19 11:44:54 +00:00
client_id = " clientcred.secret " ,
access_token = " usercred.secret " ,
api_base_url = " https://botsin.space "
)
2020-01-19 11:13:46 +00:00
db = sqlite3 . connect ( " dict.db " )
db . text_factory = str
c = db . cursor ( )
2020-01-19 11:44:54 +00:00
data = c . execute ( r ' SELECT word, definition FROM entries WHERE word LIKE " %a r % " AND wordtype LIKE " n. " AND definition NOT LIKE " See % " AND definition NOT LIKE " Alt. % " ' ) . fetchall ( )
2020-01-19 11:13:46 +00:00
dset = random . choice ( data )
2020-01-19 11:44:54 +00:00
definition = dset [ 1 ] . replace ( " \n " , " " ) # remove newlines from definitions
definition = definition . replace ( " etc. " , " etc " ) # remove full stop from etc so as not to confuse the """algorithm"""
definition = re . search ( r " ^(?:An?|The)? ?([^.]*) " , definition ) . group ( 1 ) # remove leading "A", "An", and "The", and stop at the first full stop
definition = definition [ 0 ] . lower ( ) + definition [ 1 : ] # make first letter lowercase
if " " not in definition :
# "what's a pirate's favourite disembarkation? a debarrrkation!" doesn't make sense
# so replace single words with "alternate word for {word}"
# example: "what's a pirate's favourite alternate word for disembarkation? a debarrrkation!"
# not perfect but much better
definition = " alternative word for {} " . format ( definition )
cw = " What ' s a pirate ' s favourite {} ? " . format ( definition )
toot = " A {} ! " . format (
dset [ 0 ] . lower ( ) . replace ( " ar " , " a " + ( ' r ' * random . randint ( 3 , 10 ) ) )
2020-01-19 11:13:46 +00:00
)
if not args . simulate :
try :
2020-01-19 11:44:54 +00:00
client . status_post ( toot , visibility = ' unlisted ' , spoiler_text = cw )
2020-01-19 11:13:46 +00:00
except Exception as err :
2020-01-19 11:20:56 +00:00
toot = " Arr, @lynnesbian@fedi.lynnesbian.space, ye scallywag! " \
+ " I ' ve encountered an error most dire! " \
+ " Check error.log in ye bot directory!! "
with open ( " error.log " , " w " ) as f :
f . write ( " \n " . join ( traceback . format_tb ( sys . exc_info ( ) [ 2 ] ) ) )
2020-01-19 11:13:46 +00:00
client . status_post ( toot , visibility = ' unlisted ' , spoiler_text = " Error! " )
2020-01-19 11:44:54 +00:00
print ( cw )
2020-01-19 11:13:46 +00:00
print ( toot )