diff --git a/.gitignore b/.gitignore
index d23632f..505249e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,6 @@ toots.db
toots.db-journal
toots.db-wal
__pycache__/*
+.vscode/
+.editorconfig
+.*.swp
diff --git a/config.json b/config.json
index 7b1521d..cc02a33 100644
--- a/config.json
+++ b/config.json
@@ -1 +1,4 @@
-{"site":"https://botsin.space","cw":null}
\ No newline at end of file
+{
+ "site": "https://botsin.space",
+ "cw": null
+}
\ No newline at end of file
diff --git a/create.py b/create.py
deleted file mode 100755
index caa62a3..0000000
--- a/create.py
+++ /dev/null
@@ -1,62 +0,0 @@
-#!/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/.
-
-import markovify
-import json
-import re, random, multiprocessing, time, sqlite3, shutil, os
-
-def make_sentence(output):
- class nlt_fixed(markovify.NewlineText):
- def test_sentence_input(self, sentence):
- return True #all sentences are valid <3
-
- # with open("corpus.txt", encoding="utf-8") as fp:
- # model = nlt_fixed(fp.read())
-
- shutil.copyfile("toots.db", "toots-copy.db")
- db = sqlite3.connect("toots-copy.db")
- db.text_factory=str
- c = db.cursor()
- toots = c.execute("SELECT content FROM `toots`").fetchall()
- toots_str = ""
- for toot in toots:
- toots_str += "\n{}".format(toot[0])
- model = nlt_fixed(toots_str)
- toots_str = None
- db.close()
- os.remove("toots-copy.db")
-
- sentence = None
- tries = 0
- while sentence is None and tries < 10:
- sentence = model.make_short_sentence(500, tries=10000)
- tries = tries + 1
- sentence = re.sub("^@\u202B[^ ]* ", "", sentence)
- output.send(sentence)
-
-def make_toot(force_markov = False, args = None):
- return make_toot_markov()
-
-def make_toot_markov(query = None):
- tries = 0
- toot = None
- while toot == None and tries < 25:
- pin, pout = multiprocessing.Pipe(False)
- p = multiprocessing.Process(target = make_sentence, args = [pout])
- p.start()
- p.join(10)
- if p.is_alive():
- p.terminate()
- p.join()
- toot = None
- tries = tries + 1
- else:
- toot = pin.recv()
- if toot == None:
- toot = "Toot generation failed! Contact Lynne for assistance."
- return {
- "toot":toot,
- "media":None
- }
diff --git a/functions.py b/functions.py
new file mode 100755
index 0000000..c21271a
--- /dev/null
+++ b/functions.py
@@ -0,0 +1,87 @@
+#!/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/.
+
+import markovify
+from bs4 import BeautifulSoup
+import re, multiprocessing, sqlite3, shutil, os, json
+
+def make_sentence(output):
+ 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") #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()
+ toots = c.execute("SELECT content FROM `toots` ORDER BY RANDOM() LIMIT 10000").fetchall()
+ toots_str = ""
+ for toot in toots:
+ toots_str += "\n{}".format(toot[0])
+ model = nlt_fixed(toots_str)
+ toots_str = None
+ db.close()
+ os.remove("toots-copy.db")
+
+ sentence = None
+ tries = 0
+ while sentence is None and tries < 10:
+ sentence = model.make_short_sentence(500, tries=10000)
+ tries = tries + 1
+
+ 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):
+ return make_toot_markov()
+
+def make_toot_markov(query = None):
+ tries = 0
+ toot = None
+ 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) #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 #give up, and increment tries by one
+ else:
+ toot = pin.recv()
+ 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,
+ "media": None
+ }
+
+def extract_toot(toot):
+ toot = toot.replace("'", "'") #convert HTML stuff to normal stuff
+ toot = toot.replace(""", '"') #ditto
+ soup = BeautifulSoup(toot, "html.parser")
+ for lb in soup.select("br"): #replace
with linebreak
+ lb.insert_after("\n")
+ lb.decompose()
+
+ for p in soup.select("p"): #ditto for