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)