From 90e3cfc64c4d52a9f693be8d2fc4f555d793ac47 Mon Sep 17 00:00:00 2001 From: Lynne Lawson Date: Sat, 4 May 2019 16:44:57 +1000 Subject: [PATCH] don't create infinite loop threads, finally fixes #17 --- .editorconfig | 4 ++-- README.md | 1 + config.json | 5 +++-- main.py | 1 + reply.py | 18 ++++++++++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.editorconfig b/.editorconfig index 49df4e5..24c93ee 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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] diff --git a/README.md b/README.md index 94565d6..5da3e81 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config.json b/config.json index 82bd508..c065575 100644 --- a/config.json +++ b/config.json @@ -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 } diff --git a/main.py b/main.py index 70192d3..877d482 100755 --- a/main.py +++ b/main.py @@ -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'))) diff --git a/reply.py b/reply.py index 2e60de1..3298c84 100755 --- a/reply.py +++ b/reply.py @@ -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 @