Merge pull request #3 from MarkEEaton/test-run-argparse

add test run functionality
This commit is contained in:
Hugh Rundle 2019-03-08 15:49:31 +11:00 committed by GitHub
commit eb0d055275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 3 deletions

View File

@ -22,11 +22,20 @@
# or email hugh [at] hughrundle [dot] net # or email hugh [at] hughrundle [dot] net
# ##################################################################### # #####################################################################
from argparse import ArgumentParser
import config import config
import json import json
from mastodon import Mastodon from mastodon import Mastodon
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
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...")
print("Fetching account details...") print("Fetching account details...")
mastodon = Mastodon(access_token=config.access_token, api_base_url=config.base_url) mastodon = Mastodon(access_token=config.access_token, api_base_url=config.base_url)
@ -53,6 +62,7 @@ def checkToots(timeline, deleted_count=0):
) )
deleted_count += 1 deleted_count += 1
# unreblog the original toot (their toot), not the toot created by boosting (your toot) # unreblog the original toot (their toot), not the toot created by boosting (your toot)
if not options.test:
mastodon.status_unreblog(toot.reblog) mastodon.status_unreblog(toot.reblog)
else: else:
print( print(
@ -62,6 +72,7 @@ def checkToots(timeline, deleted_count=0):
+ toot.created_at.strftime("%d %b %Y") + toot.created_at.strftime("%d %b %Y")
) )
deleted_count += 1 deleted_count += 1
if not options.test:
mastodon.status_delete(toot) mastodon.status_delete(toot)
except: except:
print("🛑 **error** with toot - " + str(toot.id)) print("🛑 **error** with toot - " + str(toot.id))
@ -74,6 +85,13 @@ def checkToots(timeline, deleted_count=0):
next_batch = mastodon.account_statuses(user_id, limit=40, max_id=max_id) next_batch = mastodon.account_statuses(user_id, limit=40, max_id=max_id)
if len(next_batch) > 0: if len(next_batch) > 0:
checkToots(next_batch, deleted_count) checkToots(next_batch, deleted_count)
else:
if options.test:
print(
"Test run. This would have removed "
+ str(deleted_count)
+ " toots."
)
else: else:
print("Removed " + str(deleted_count) + " toots.") print("Removed " + str(deleted_count) + " toots.")
except IndexError: except IndexError: