fix catastrophic bug from abc7e07677

This commit is contained in:
Hugh Rundle 2020-04-27 15:28:07 +10:00
parent b251577ffe
commit e5488ced4d
2 changed files with 102 additions and 98 deletions

View File

@ -65,6 +65,9 @@ if __name__ == "__main__":
print('Running at ' + str(datetime.now() ))
print('=======================================')
print('')
if options.test:
print("This is a test run...")
print('')
with open(config_file) as config:
for accounts in yaml.safe_load_all(config):
for user in accounts:

View File

@ -46,11 +46,105 @@ def schedule(options):
except Exception:
print('🙁 Scheduling failed.')
def checkToots(config, options, deleted_count=0, retry_count=0):
if options.test:
print("This is a test run...")
def checkToots(config, options, retry_count=0):
print("Fetching account details for @" + config['username'] + "@" + config['base_url'] + "...")
try:
def checkBatch(timeline, deleted_count=0):
for toot in timeline:
toot_tags = set()
for tag in toot.tags:
toot_tags.add(tag.name)
try:
if config['keep_pinned'] and hasattr(toot, "pinned") and toot.pinned:
print("📌 skipping pinned toot - " + str(toot.id))
elif toot.id in config['toots_to_keep']:
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))
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)
if not options.test:
if mastodon.ratelimit_remaining == 0:
print(
"Rate limit reached. Waiting for a rate limit reset..."
)
mastodon.status_unreblog(toot.reblog)
else:
print(
"❌ deleting toot "
+ str(toot.id)
+ " tooted "
+ toot.created_at.strftime("%d %b %Y")
)
deleted_count += 1
time.sleep(
2
) # wait 2 secs between deletes to be a bit nicer to the server
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...")
time.sleep(60)
try:
print("Attempting delete again")
mastodon.status_delete(toot)
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:
print("🛑 Unknown ERROR deleting toot - " + str(toot.id))
print(e)
# 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
try:
max_id = timeline[-1:][0].id
next_batch = mastodon.account_statuses(user_id, limit=40, max_id=max_id)
if len(next_batch) > 0:
checkBatch(next_batch, deleted_count)
else:
if options.test:
print(
"Test run completed. This would have removed "
+ str(deleted_count)
+ " toots."
)
else:
print("Removed " + str(deleted_count) + " toots.")
print('')
print('---------------------------------------')
print('🥳 ==> 🧼 ==> 😇 User cleanup complete!')
print('---------------------------------------')
except IndexError:
print("No toots found!")
mastodon = Mastodon(
access_token=config['access_token'],
api_base_url="https://" + config['base_url'],
@ -64,101 +158,8 @@ def checkToots(config, options, deleted_count=0, retry_count=0):
print("Checking " + str(account.statuses_count) + " toots...")
for toot in timeline:
checkBatch(timeline)
toot_tags = set()
for tag in toot.tags:
toot_tags.add(tag.name)
try:
if config['keep_pinned'] and hasattr(toot, "pinned") and toot.pinned:
print("📌 skipping pinned toot - " + str(toot.id))
elif toot.id in config['toots_to_keep']:
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))
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)
if not options.test:
if mastodon.ratelimit_remaining == 0:
print(
"Rate limit reached. Waiting for a rate limit reset..."
)
mastodon.status_unreblog(toot.reblog)
else:
print(
"❌ deleting toot "
+ str(toot.id)
+ " tooted "
+ toot.created_at.strftime("%d %b %Y")
)
deleted_count += 1
time.sleep(
2
) # wait 2 secs between deletes to be a bit nicer to the server
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...")
time.sleep(60)
try:
print("Attempting delete again")
mastodon.status_delete(toot)
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:
print("🛑 Unknown ERROR deleting toot - " + str(toot.id))
print(e)
# 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
try:
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:
if options.test:
print(
"Test run completed. This would have removed "
+ str(deleted_count)
+ " toots."
)
else:
print("Removed " + str(deleted_count) + " toots.")
print('')
print('---------------------------------------')
print('🥳 ==> 🧼 ==> 😇 User cleanup complete!')
print('---------------------------------------')
except IndexError:
print("No toots found!")
except MastodonAPIError:
print('User and/or access token does not exist or has been deleted')
except MastodonNetworkError:
@ -168,6 +169,6 @@ def checkToots(config, options, deleted_count=0, retry_count=0):
time.sleep(60)
retry_count += 1
print( 'Attempt ' + str(retry_count + 1) )
checkToots(config, options, 0, retry_count)
checkToots(config, options, retry_count)
else:
print('Gave up waiting for network')