summaryrefslogtreecommitdiff
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
parent3a7c55a6e5e02edfe48d38c861a760fdb5aabb4a (diff)
Add features
- Bot now logs its output to avoid repeat posts - Added description of features to README
-rw-r--r--.gitignore1
-rw-r--r--config__.default.py10
-rw-r--r--generate.py37
-rw-r--r--readme.md7
4 files changed, 47 insertions, 8 deletions
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: