From 11a75aac1a08d9ef57586ea75aa4c71abed06399 Mon Sep 17 00:00:00 2001 From: Ada Isopods Date: Mon, 20 May 2024 18:47:04 -0600 Subject: Add features - Bot now logs its output to avoid repeat posts - Added description of features to README --- generate.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'generate.py') 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={ -- cgit v1.2.3