2023-08-01 18:55:54 +02:00
|
|
|
"""Cleanup old posts that did not have any interactions"""
|
|
|
|
|
|
|
|
# ----- imports ----- #
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
2023-08-01 23:57:15 +02:00
|
|
|
from mastodon import Mastodon, MastodonRatelimitError
|
2023-08-01 18:55:54 +02:00
|
|
|
from os import getenv
|
|
|
|
|
2023-08-01 22:49:29 +02:00
|
|
|
def cleanup(limit: int = 40, id: int | None = None):
|
|
|
|
# ----- initialization ----- #
|
2023-08-01 18:55:54 +02:00
|
|
|
|
2023-08-01 22:49:29 +02:00
|
|
|
# load .env
|
|
|
|
load_dotenv()
|
2023-08-01 18:55:54 +02:00
|
|
|
|
2023-08-01 22:49:29 +02:00
|
|
|
# initialize mastodon
|
|
|
|
mastodon = Mastodon(
|
|
|
|
access_token = getenv("MASTODON_USER_SECRET"),
|
2023-08-01 23:57:15 +02:00
|
|
|
api_base_url = getenv("MASTODON_URL"),
|
|
|
|
ratelimit_method="throw"
|
2023-08-01 22:49:29 +02:00
|
|
|
)
|
2023-08-01 18:55:54 +02:00
|
|
|
|
2023-08-01 22:49:29 +02:00
|
|
|
# ----- go 200 posts into the past, if manual id is not set ----- #
|
2023-08-01 18:55:54 +02:00
|
|
|
|
2023-08-01 22:49:29 +02:00
|
|
|
if id is None:
|
|
|
|
# get latest posts
|
|
|
|
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, limit=limit)
|
|
|
|
id_oldest_of_last_posts = -1
|
|
|
|
for _ in range(4):
|
2023-08-02 19:29:36 +02:00
|
|
|
# return if there are no posts to check
|
|
|
|
if len(last_posts) == 0:
|
|
|
|
return
|
2023-08-01 22:49:29 +02:00
|
|
|
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)
|
2023-08-01 18:55:54 +02:00
|
|
|
|
2023-08-01 22:49:29 +02:00
|
|
|
# return if there are no posts to check
|
|
|
|
if len(last_posts) == 0:
|
|
|
|
return
|
2023-08-01 18:55:54 +02:00
|
|
|
|
2023-08-02 19:33:39 +02:00
|
|
|
# ----- delete old inactive posts ----- #
|
2023-08-01 18:55:54 +02:00
|
|
|
|
2023-08-02 19:33:39 +02:00
|
|
|
# default: check 10 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):
|
2023-08-01 22:49:29 +02:00
|
|
|
# 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:
|
2023-08-02 00:32:48 +02:00
|
|
|
print("Deleting post:", submission["url"])
|
2023-08-01 23:57:15 +02:00
|
|
|
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"])
|
2023-08-01 22:49:29 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cleanup(limit=40, id=None)
|