1
0
mirror of https://github.com/hughrun/ephemetoot synced 2025-01-10 13:02:36 +01:00
ephemetoot-eliminare-vecchi.../ephemetoot.py

148 lines
5.9 KiB
Python
Raw Normal View History

2018-12-24 05:30:56 +01:00
# #####################################################################
# Ephemetoot - A script to delete your old toots
2019-03-08 06:16:00 +01:00
# Copyright (C) 2018 Hugh Rundle, 2019 Hugh Rundle & Mark Eaton
2019-02-13 22:59:57 +01:00
# Based partially on tweet-deleting script by @flesueur
2018-12-24 05:30:56 +01:00
# (https://gist.github.com/flesueur/bcb2d9185b64c5191915d860ad19f23f)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# You can contact Hugh on Mastodon @hugh@ausglam.space
2018-12-24 05:30:56 +01:00
# or email hugh [at] hughrundle [dot] net
# #####################################################################
2019-02-14 05:22:35 +01:00
from argparse import ArgumentParser
2018-12-24 05:30:56 +01:00
import config
import json
from mastodon import Mastodon, MastodonError
2018-12-24 05:30:56 +01:00
from datetime import datetime, timedelta, timezone
2019-05-09 23:58:59 +02:00
import time
2018-12-24 05:30:56 +01:00
2019-02-14 05:22:35 +01:00
parser = ArgumentParser()
parser.add_argument(
"--test", action="store_true", help="do a test run without deleting any toots"
)
options = parser.parse_args()
if options.test:
print("This is a test run...")
2019-02-13 22:59:57 +01:00
print("Fetching account details...")
2018-12-24 05:30:56 +01:00
2019-05-12 07:40:15 +02:00
mastodon = Mastodon(
access_token=config.access_token,
api_base_url=config.base_url,
ratelimit_method="wait",
)
2018-12-24 05:30:56 +01:00
cutoff_date = datetime.now(timezone.utc) - timedelta(days=config.days_to_keep)
user_id = mastodon.account_verify_credentials().id
timeline = mastodon.account_statuses(user_id, limit=40)
2019-02-13 22:59:57 +01:00
2018-12-24 05:30:56 +01:00
def checkToots(timeline, deleted_count=0):
2019-02-13 22:59:57 +01:00
for toot in timeline:
toot_tags = set()
for tag in toot.tags:
toot_tags.add(tag.name)
2019-02-13 22:59:57 +01:00
try:
if config.keep_pinned and hasattr(toot, "pinned") and toot.pinned:
2019-02-13 22:59:57 +01:00
print("📌 skipping pinned toot - " + str(toot.id))
elif toot.id in config.toots_to_keep:
2019-02-13 22:59:57 +01:00
print("💾 skipping saved toot - " + str(toot.id))
elif toot.visibility in config.visibility_to_keep:
print("👀 skipping " + toot.visibility + " toot - " + str(toot.id))
elif len(config.hashtags_to_keep.intersection(toot_tags)) > 0:
print("#️⃣ skipping toot with hashtag - " + str(toot.id))
2019-02-13 22:59:57 +01:00
elif cutoff_date > toot.created_at:
if hasattr(toot, "reblog") and toot.reblog:
print(
"👎 unboosting toot "
+ str(toot.id)
+ " boosted "
+ toot.created_at.strftime("%d %b %Y")
)
deleted_count += 1
# unreblog the original toot (their toot), not the toot created by boosting (your toot)
2019-03-06 01:08:52 +01:00
if not options.test:
if mastodon.ratelimit_remaining == 0:
print(
"Rate limit reached. Waiting for a rate limit reset..."
)
2019-03-06 01:08:52 +01:00
mastodon.status_unreblog(toot.reblog)
2019-02-13 22:59:57 +01:00
else:
print(
"❌ deleting toot "
+ str(toot.id)
+ " tooted "
+ toot.created_at.strftime("%d %b %Y")
)
deleted_count += 1
2019-05-12 07:40:15 +02:00
time.sleep(
2
) # wait 2 secs between deletes to be a bit nicer to the server
2019-02-14 05:22:35 +01:00
if not options.test:
if mastodon.ratelimit_remaining == 0:
print(
"Rate limit reached. Waiting for a rate limit reset..."
)
mastodon.status_delete(toot)
except MastodonError as e:
print("🛑 ERROR deleting toot - " + str(toot.id) + " - " + e.args[3])
print("Waiting 1 minute before re-trying...")
2019-05-12 07:40:15 +02:00
time.sleep(60)
try:
print("Attempting delete again")
mastodon.status_delete(toot)
2019-05-12 07:40:15 +02:00
time.sleep(
2
) # wait 2 secs between deletes to be a bit nicer to the server
except Exception as e:
print("🛑 ERROR deleting toot - " + str(toot.id))
print(e)
print("Exiting due to error.")
break
except KeyboardInterrupt:
print("Operation aborted.")
break
except Exception as e:
2019-05-12 07:40:15 +02:00
print("🛑 Unknown ERROR deleting toot - " + str(toot.id))
print(e)
2019-02-13 22:59:57 +01:00
# the account_statuses call is paginated with a 40-toot limit
# get the id of the last toot to include as 'max_id' in the next API call.
# then keep triggering new rounds of checkToots() until there are no more toots to check
2018-12-24 05:30:56 +01:00
try:
2019-02-13 22:59:57 +01:00
max_id = timeline[-1:][0].id
next_batch = mastodon.account_statuses(user_id, limit=40, max_id=max_id)
if len(next_batch) > 0:
checkToots(next_batch, deleted_count)
else:
2019-02-14 05:22:35 +01:00
if options.test:
print(
"Test run completed. This would have removed "
2019-02-14 05:22:35 +01:00
+ str(deleted_count)
+ " toots."
)
else:
print("Removed " + str(deleted_count) + " toots.")
2019-02-13 22:59:57 +01:00
except IndexError:
print("No toots found!")
2018-12-24 05:30:56 +01:00
# trigger from here
2019-02-13 22:59:57 +01:00
if __name__ == "__main__":
account = mastodon.account(user_id)
print("Checking " + str(account.statuses_count) + " toots...")
2019-05-12 07:40:15 +02:00
checkToots(timeline)