Add features

- Bot now logs its output to avoid repeat posts
- Added description of features to README
This commit is contained in:
will 2024-05-20 18:47:04 -06:00
parent 2c6858e306
commit 5605a39104
4 changed files with 47 additions and 8 deletions

1
.gitignore vendored
View file

@ -1,5 +1,6 @@
*.json
*.py[oc]
/test.py
/config__.py
/secrets__.py
/venv

View file

@ -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
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

View file

@ -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={

View file

@ -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: