Stop cleanup when hitting rate limit

This commit is contained in:
lluni 2023-08-01 23:57:15 +02:00
parent a35a6303b6
commit 3347b83334
Signed by: lluni
GPG key ID: ACEEB468BC325D35

View file

@ -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)