renamed create.py to functions.py

This commit is contained in:
Lynne 2019-01-11 22:47:42 +10:00
parent 0a66c1db51
commit 85fec32c83
No known key found for this signature in database
GPG key ID: FB7B970303ACE499
4 changed files with 22 additions and 23 deletions

1
.gitignore vendored
View file

@ -8,3 +8,4 @@ toots.db-journal
toots.db-wal
__pycache__/*
.vscode/
.editorconfig

View file

@ -4,15 +4,15 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import markovify
import json
import re, random, multiprocessing, time, sqlite3, shutil, os
from bs4 import BeautifulSoup
import re, random, multiprocessing, sqlite3, shutil, os, json
def make_sentence(output):
class nlt_fixed(markovify.NewlineText):
class nlt_fixed(markovify.NewlineText): #modified version of NewlineText that never rejects sentences
def test_sentence_input(self, sentence):
return True #all sentences are valid <3
shutil.copyfile("toots.db", "toots-copy.db")
shutil.copyfile("toots.db", "toots-copy.db") #create a copy of the database because reply.py will be using the main one
db = sqlite3.connect("toots-copy.db")
db.text_factory=str
c = db.cursor()
@ -30,7 +30,10 @@ def make_sentence(output):
while sentence is None and tries < 10:
sentence = model.make_short_sentence(500, tries=10000)
tries = tries + 1
sentence = re.sub("^@\u202B[^ ]* ", "", sentence)
sentence = re.sub("^(?:@\u202B[^ ]* )*", "", sentence) #remove leading pings (don't say "@bob blah blah" but still say "blah @bob blah")
sentence = re.sub("^(?:@\u200B[^ ]* )*", "", sentence)
output.send(sentence)
def make_toot(force_markov = False, args = None):
@ -39,19 +42,19 @@ def make_toot(force_markov = False, args = None):
def make_toot_markov(query = None):
tries = 0
toot = None
while toot == None and tries < 25:
while toot == None and tries < 10: #try to make a toot 10 times
pin, pout = multiprocessing.Pipe(False)
p = multiprocessing.Process(target = make_sentence, args = [pout])
p.start()
p.join(10)
if p.is_alive():
p.join(10) #wait 10 seconds to get something
if p.is_alive(): #if it's still trying to make a toot after 10 seconds
p.terminate()
p.join()
toot = None
tries = tries + 1
tries = tries + 1 #give up, and increment tries by one
else:
toot = pin.recv()
if toot == None:
if toot == None: #if we've tried and failed ten times, just give up
toot = "Toot generation failed! Contact Lynne (lynnesbian@fedi.lynnesbian.space) for assistance."
return {
"toot": toot,

13
gen.py
View file

@ -5,13 +5,11 @@
from mastodon import Mastodon
import argparse, sys, traceback, json
import create
import functions
parser = argparse.ArgumentParser(description='Generate and post a toot.')
parser.add_argument('reply', metavar='reply', type=str, nargs='?',
help='ID of the status to reply to')
parser.add_argument('-s', '--simulate', dest='simulate', action='store_true',
help="Print the toot to stdout without posting it")
help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
args = parser.parse_args()
@ -24,7 +22,7 @@ client = Mastodon(
api_base_url=cfg['site'])
if __name__ == '__main__':
toot = create.make_toot()
toot = functions.make_toot()
if not args.simulate:
try:
if toot['media'] != None:
@ -35,10 +33,7 @@ if __name__ == '__main__':
client.status_post(toot['toot'], visibility = 'unlisted', spoiler_text = cfg['cw'])
except Exception as err:
toot = {
"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]))
"toot": "An unknown error that should never happen occurred. Maybe it's because of the spoiler text, which is {}. If not, I have no idea what went wrong. This is an error message -- contact lynnesbian@fedi.lynnesbian.space for assistance.".format(cfg['cw'])
}
client.status_post(toot['toot'], visibility = 'unlisted', spoiler_text = "Error!")
print(toot['toot'])

View file

@ -5,7 +5,7 @@
import mastodon
import os, random, re, json
import create
import functions
from bs4 import BeautifulSoup
cfg = json.load(open('config.json', 'r'))
@ -43,7 +43,7 @@ class ReplyListener(mastodon.StreamListener):
acct = "@" + notification['account']['acct']
post_id = notification['status']['id']
mention = extract_toot(notification['status']['content'])
toot = create.make_toot(True)['toot']
toot = functions.make_toot(True)['toot']
toot = acct + " " + toot
print(acct + " says " + mention)
visibility = notification['status']['visibility']