summaryrefslogtreecommitdiff
path: root/generate.py
diff options
context:
space:
mode:
authorAda Isopods <me@bathynomus.xyz>2024-05-20 18:47:04 -0600
committerAda Isopods <me@bathynomus.xyz>2026-10-21 23:26:19 -0600
commit11a75aac1a08d9ef57586ea75aa4c71abed06399 (patch)
tree4acf5d5293ececc6f03918b5a3c8182a42ec01d8 /generate.py
parent3a7c55a6e5e02edfe48d38c861a760fdb5aabb4a (diff)
Add features
- Bot now logs its output to avoid repeat posts - Added description of features to README
Diffstat (limited to 'generate.py')
-rw-r--r--generate.py37
1 files changed, 34 insertions, 3 deletions
diff --git a/generate.py b/generate.py
index fd4803d..ee8cc3a 100644
--- a/generate.py
+++ b/generate.py
@@ -1,12 +1,32 @@
import chain
import sys
import requests
+import json
+import os
import secrets__
import config__
-model_f = open(config__.model_file)
-model = chain.Text.from_json(model_f.read())
+dir_path = os.path.dirname(os.path.realpath(__file__))
+
+# Import previous post log
+
+log_path = dir_path + "/posts_log.json"
+
+try:
+ with open(log_path, "r") as infile:
+ posts_log = json.loads(infile.read())
+except FileNotFoundError:
+ with open(log_path, "w") as outfile:
+ outfile.write('[""]')
+ posts_log = []
+
+# Import Markov model
+
+with open(dir_path + "/" + config__.model_file) as model_f:
+ model = chain.Text.from_json(model_f.read())
+
+# Generate sentence
generated = False
text = None
@@ -14,11 +34,22 @@ text = None
while not generated:
text = model.make_short_sentence(config__.character_limit, tries=900, min_words=3)
generated = text is not None
+ if generated:
+ if text in posts_log:
+ generated = False
+
+# Output
+
+posts_log.append(text)
+with open(log_path, "w") as outfile:
+ outfile.write(json.dumps(posts_log[-config__.log_length:]))
text = text.replace('@','@​').replace('#','#​')
print(text)
-apicreate = "https://" + config__.instance_url + "/api/notes/create"\
+# Post to instance
+
+apicreate = "https://" + config__.instance_url + "/api/notes/create"
if(config__.cw == ""):
requests.post(apicreate, json={