From 3347b83334beb80c8cfb768438b7a356c2ac4723 Mon Sep 17 00:00:00 2001 From: lluni Date: Tue, 1 Aug 2023 23:57:15 +0200 Subject: [PATCH] Stop cleanup when hitting rate limit --- cleanup.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cleanup.py b/cleanup.py index c4a5d6f..8e5177d 100644 --- a/cleanup.py +++ b/cleanup.py @@ -3,7 +3,7 @@ # ----- imports ----- # from dotenv import load_dotenv -from mastodon import Mastodon +from mastodon import Mastodon, MastodonRatelimitError from os import getenv def cleanup(limit: int = 40, id: int | None = None): @@ -15,7 +15,8 @@ def cleanup(limit: int = 40, id: int | None = None): # initialize mastodon mastodon = Mastodon( access_token = getenv("MASTODON_USER_SECRET"), - api_base_url = getenv("MASTODON_URL") + api_base_url = getenv("MASTODON_URL"), + ratelimit_method="throw" ) # ----- go 200 posts into the past, if manual id is not set ----- # @@ -58,7 +59,13 @@ def cleanup(limit: int = 40, id: int | None = None): # if post has no favorites, boosts or replies, delete it if submission["favourites_count"] == 0 and submission["reblogs_count"] == 0 and submission["replies_count"] == 0: print("Trying to delete post:", submission["url"]) - mastodon.status_delete(submission["id"]) + try: + mastodon.status_delete(submission["id"]) + except MastodonRatelimitError: + print("Hit rate limit while trying to delete post:", submission["url"]) + return + else: + print("Ignoring post:", submission["url"]) if __name__ == "__main__": cleanup(limit=40, id=None)