Add cleanup script to delete old inactive posts

This commit is contained in:
lluni 2023-08-01 18:55:54 +02:00
parent 4c3f56bf2b
commit 091927de06
Signed by: lluni
GPG key ID: ACEEB468BC325D35

44
cleanup.py Normal file
View file

@ -0,0 +1,44 @@
"""Cleanup old posts that did not have any interactions"""
# ----- imports ----- #
from dotenv import load_dotenv
from mastodon import Mastodon
from os import getenv
# ----- initialization ----- #
# load .env
load_dotenv()
# initialize mastodon
mastodon = Mastodon(
access_token = getenv("MASTODON_USER_SECRET"),
api_base_url = getenv("MASTODON_URL")
)
# ----- go 200 posts into the past ----- #
# get latest posts
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), exclude_replies=True, limit=40)
id_oldest_of_last_posts = -1
for i in range(4):
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=40)
# ----- delete old posts ----- #
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)
# 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"])