don't create infinite loop threads, finally fixes #17

This commit is contained in:
Lynne Lawson 2019-05-04 16:44:57 +10:00
parent cb48a21da4
commit 90e3cfc64c
5 changed files with 25 additions and 4 deletions

View file

@ -6,8 +6,8 @@ root = true
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4
indent_style = tab
indent_size = 2
# Markdown
[*.md]

View file

@ -21,6 +21,7 @@ Configuring mstdn-ebooks is accomplished by editing `config.json`.
| instance_blacklist | ["bofa.lol", "witches.town"] | If your bot is following someone from a blacklisted instance, it will skip over them and not download their posts. This is useful for ensuring that mstdn-ebooks doesn't download posts from dead instances, without you having to unfollow the user(s) from them. |
| learn_from_cw | false | If true, mstdn-ebooks will learn from CW'd posts. |
| mention_handling | 1 | 0: Never use mentions. 1: Only generate fake mentions in the middle of posts, never at the start. 2: Use mentions as normal (old behaviour). |
| max_thread_length | 15 | The maximum number of bot posts in a row before it stops replying. A thread can be 10 or 10000 posts long, but the bot will stop after it has posted `max_thread_length` times. |
## Original README

View file

@ -1,7 +1,8 @@
{
"lang": "en",
"lang": "en",
"site": "https://botsin.space",
"cw": null,
"learn_from_cw": false,
"mention_handling": 1
"mention_handling": 1,
"max_thread_length": 15
}

View file

@ -19,6 +19,7 @@ cfg = {
"instance_blacklist": ["bofa.lol", "witches.town"],
"learn_from_cw": False,
"mention_handling": 1,
"max_thread_length": 15
}
try:
cfg.update(json.load(open('config.json', 'r')))

View file

@ -9,6 +9,7 @@ import functions
from bs4 import BeautifulSoup
cfg = json.load(open('config.json', 'r'))
threads = {}
client = mastodon.Mastodon(
client_id=cfg['client']['id'],
@ -27,6 +28,23 @@ class ReplyListener(mastodon.StreamListener):
if notification['type'] == 'mention': #if we're mentioned:
acct = "@" + notification['account']['acct'] #get the account's @
post_id = notification['status']['id']
# check if we've already been participating in this thread
try:
context = client.status_context(post_id)
except:
print("failed to fetch thread context")
return
me = client.account_verify_credentials()['id']
posts = 0
for post in context['ancestors']:
if post['account']['id'] == me:
posts += 1
if posts >= cfg['max_thread_length']:
# stop replying
print("didn't reply (max_thread_length exceeded)")
return
threads[post_id] = [time.time(), 0]
mention = extract_toot(notification['status']['content'])
toot = functions.make_toot(True)['toot'] #generate a toot
toot = acct + " " + toot #prepend the @