Stop cleanup when hitting rate limit
This commit is contained in:
parent
a35a6303b6
commit
3347b83334
1 changed files with 10 additions and 3 deletions
13
cleanup.py
13
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)
|
||||
|
|
Loading…
Reference in a new issue