diff --git a/.gitignore b/.gitignore index 0195375..a35f512 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.json *.py[oc] +/test.py /config__.py /secrets__.py /venv diff --git a/config__.default.py b/config__.default.py index 0ed45fa..a4a7a15 100644 --- a/config__.default.py +++ b/config__.default.py @@ -1,15 +1,15 @@ model_file = "model.json" -# Path to the model generated by import_misskey.py -# I had to use absolute paths to get the cronjob to work (ie. /path/to/file.json instead of just file.json) +# Filename of the model file generated by import_misskey.py. Path is relative. instance_url = "example.com" # Domain of the instance the bot is on, eg. botsin.space cw = "markov bot generated post" -# Content warning to put on the bot's posts +# Content warning to put on the bot's posts. Leave blank (cw = "") for no CW. character_limit = 80 # Maximum length of the posts -log_length = 96 -# Currently unused \ No newline at end of file +log_length = 168 +# How many of the most recently generated posts to log, so that the bot won't repeat them +# The default value of 168 logs a week of hourly posts \ No newline at end of file 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={ diff --git a/readme.md b/readme.md index 707a096..82fbc5a 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,12 @@ Basic Markov bot for Misskey. Fork of https://activitypub.software/kopper/markov +Generates a model from a note export and posts automatically. + +Changes from original: +- Relevant options are no longer hardcoded for easier configuration +- Stores a log of past posts to avoid repeating itself +- Option to post without CW + ## Setup - Install dependencies: