diff --git a/cleanup.py b/cleanup.py index 86cd62d..bd94b69 100644 --- a/cleanup.py +++ b/cleanup.py @@ -6,7 +6,14 @@ from dotenv import load_dotenv from mastodon import Mastodon, MastodonRatelimitError from os import getenv -def cleanup(limit: int = 40, id: int | None = None): +def cleanup(offset: int = 200, limit: int = 40, id: int | None = None): + """Delete old posts without any interactions. + + Args: + offset (int, optional): After how many posts should the function start. Defaults to 200. + limit (int, optional): Size of posts batches. Defaults to 40. + id (int | None, optional): Post ID from where to start cleanup. If this value is set, the offset will be ignored. Defaults to None. + """ # ----- initialization ----- # # load .env @@ -19,32 +26,47 @@ def cleanup(limit: int = 40, id: int | None = None): ratelimit_method="throw" ) - # ----- go 200 posts into the past, if manual id is not set ----- # + # ----- go *limit* posts into the past, if manual id is not set ----- # + + def get_account_statuses(max_id: int, limit: int = 40) -> list: + if id_oldest_of_last_posts == -1: + return mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, limit=limit) + else: + return mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, max_id=max_id, limit=limit) if id is None: - # get latest posts - last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, limit=limit) + last_posts = [] id_oldest_of_last_posts = -1 - for _ in range(4): - # return if there are no posts to check + iterations = offset // 40 + remainder = offset % 40 + # get latest posts until offset is reached + for _ in range(iterations): + last_posts = get_account_statuses(max_id=id_oldest_of_last_posts, limit=40) if len(last_posts) == 0: + print("Offset is higher than the number of posts in the past.") + return + id_oldest_of_last_posts = last_posts[-1]["id"] + print("Last post:", last_posts[-1]["url"]) + if remainder != 0: + last_posts = get_account_statuses(max_id=id_oldest_of_last_posts, limit=remainder) + if len(last_posts) == 0: + print("Offset is higher than the number of posts in the past.") return id_oldest_of_last_posts = last_posts[-1]["id"] - last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, max_id=id_oldest_of_last_posts, limit=limit) else: id_oldest_of_last_posts = id last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, max_id=id, limit=limit) - - # return if there are no posts to check - if len(last_posts) == 0: - return + # return if there are no posts to check + if len(last_posts) == 0: + print("There are no older posts than the given id.") + return # ----- delete old inactive posts ----- # - # default: check 10 batches of 40 posts + # default: check 100 batches of 40 posts # this usually tries until the rate limit is triggered or there are no more posts # if hitting the rate limit is undesired, reduce the number of iterations - for i in range(10): + for i in range(100): # skip getting new batch in the first iteration if the id was set manually if i != 0 or id is None: # find id of the currently oldest post @@ -72,4 +94,4 @@ def cleanup(limit: int = 40, id: int | None = None): print("Ignoring post:", submission["url"]) if __name__ == "__main__": - cleanup(limit=40, id=None) + cleanup(offset=200, limit=40, id=None)