Add starting cleanup from arbitrary post IDs to combat rate limits
This commit is contained in:
parent
f9b720e2e1
commit
a35a6303b6
1 changed files with 49 additions and 33 deletions
82
cleanup.py
82
cleanup.py
|
@ -6,43 +6,59 @@ from dotenv import load_dotenv
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
from os import getenv
|
from os import getenv
|
||||||
|
|
||||||
# ----- initialization ----- #
|
def cleanup(limit: int = 40, id: int | None = None):
|
||||||
|
# ----- initialization ----- #
|
||||||
|
|
||||||
# load .env
|
# load .env
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
# initialize mastodon
|
# initialize mastodon
|
||||||
mastodon = Mastodon(
|
mastodon = Mastodon(
|
||||||
access_token = getenv("MASTODON_USER_SECRET"),
|
access_token = getenv("MASTODON_USER_SECRET"),
|
||||||
api_base_url = getenv("MASTODON_URL")
|
api_base_url = getenv("MASTODON_URL")
|
||||||
)
|
)
|
||||||
|
|
||||||
# ----- go 200 posts into the past ----- #
|
# ----- go 200 posts into the past, if manual id is not set ----- #
|
||||||
|
|
||||||
# get latest posts
|
if id is None:
|
||||||
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, limit=40)
|
# get latest posts
|
||||||
id_oldest_of_last_posts = -1
|
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, limit=limit)
|
||||||
for i in range(4):
|
id_oldest_of_last_posts = -1
|
||||||
id_oldest_of_last_posts = last_posts[-1]["id"]
|
for _ in range(4):
|
||||||
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, max_id=id_oldest_of_last_posts, limit=40)
|
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)
|
||||||
|
|
||||||
# ----- delete old posts ----- #
|
# return if there are no posts to check
|
||||||
|
|
||||||
for i in range(5): # default: check 5 batches of 40 posts
|
|
||||||
# find id of the currently oldest post
|
|
||||||
last_post = last_posts[-1]
|
|
||||||
if last_post["favourites_count"] != 0 or last_post["reblogs_count"] != 0 or last_post["replies_count"] != 0:
|
|
||||||
id_oldest_of_last_posts = last_post["id"]
|
|
||||||
# get next batch of posts
|
|
||||||
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, max_id=id_oldest_of_last_posts, limit=40)
|
|
||||||
|
|
||||||
# break if there are no more posts
|
|
||||||
if len(last_posts) == 0:
|
if len(last_posts) == 0:
|
||||||
break
|
return
|
||||||
|
|
||||||
# delete all old posts of the current batch
|
# ----- delete old posts ----- #
|
||||||
for submission in last_posts:
|
|
||||||
# if post has no favorites, boosts or replies, delete it
|
# default: check 2 batches of 40 posts (and 6 batches if the id was set manually)
|
||||||
if submission["favourites_count"] == 0 and submission["reblogs_count"] == 0 and submission["replies_count"] == 0:
|
num_batches = 2 if id is None else 6
|
||||||
print("Trying to delete post:", submission["url"])
|
for i in range(num_batches):
|
||||||
mastodon.status_delete(submission["id"])
|
# 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
|
||||||
|
last_post = last_posts[-1]
|
||||||
|
if last_post["favourites_count"] != 0 or last_post["reblogs_count"] != 0 or last_post["replies_count"] != 0:
|
||||||
|
id_oldest_of_last_posts = last_post["id"]
|
||||||
|
# get next batch of posts
|
||||||
|
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, max_id=id_oldest_of_last_posts, limit=limit)
|
||||||
|
|
||||||
|
# return if there are no more posts to check
|
||||||
|
if len(last_posts) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# delete all old posts of the current batch
|
||||||
|
for submission in last_posts:
|
||||||
|
# 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"])
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
cleanup(limit=40, id=None)
|
||||||
|
|
Loading…
Reference in a new issue