From f5016fe684037f28d84f36232cc07f0820c146b9 Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Sat, 18 Jul 2020 17:38:16 +1000 Subject: [PATCH] add --archive-deleted flag This adds a flag to restrict the archive to only toots about to be deleted, rather than archiving everything. --- bin/ephemetoot | 8 +++++--- lib/ephemetoot.py | 25 +++++++++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/bin/ephemetoot b/bin/ephemetoot index 24ffdea..aa9d04e 100644 --- a/bin/ephemetoot +++ b/bin/ephemetoot @@ -39,6 +39,9 @@ from lib import ephemetoot vnum = pkg_resources.require("ephemetoot")[0].version parser = ArgumentParser() +parser.add_argument( + "--archive-deleted", action="store_true", help="Only archive toots that are being deleted" +) parser.add_argument( "--config", action="store", metavar="filepath", default="config.yaml", help="Filepath of your config file, absolute or relative to the current directory. If no --config path is provided, ephemetoot will use 'config.yaml'." ) @@ -46,7 +49,7 @@ parser.add_argument( "--datestamp", action="store_true", help="Include a datetime stamp for every action (e.g. deleting a toot)" ) parser.add_argument( - "--hide_skipped", action="store_true", help="Do not write to log when skipping saved toots" + "--hide-skipped", "--hide_skipped", action="store_true", help="Do not write to log when skipping saved toots" ) parser.add_argument( "--pace", action="store_true", help="Slow deletion actions to match API rate limit to avoid pausing" @@ -84,8 +87,7 @@ if __name__ == "__main__": print('================================================') print('') if options.test: - print("This is a test run...") - print('') + print("This is a test run...\n") with open(config_file) as config: for accounts in yaml.safe_load_all(config): for user in accounts: diff --git a/lib/ephemetoot.py b/lib/ephemetoot.py index f79327f..230fccb 100644 --- a/lib/ephemetoot.py +++ b/lib/ephemetoot.py @@ -116,13 +116,14 @@ def checkToots(config, options, retry_count=0): archive_path = os.path.join( os.getcwd(), config["archive"] ) if archive_path[-1] != '/': archive_path += '/' - print(archive_path) + filename = os.path.join(archive_path, str(toot["id"]) + ".json") - # write toot to archive - with open(filename, "w") as f: - f.write(json.dumps(toot, indent=4, default=jsondefault)) - f.close() + if not options.archive_deleted: + # write toot to archive + with open(filename, "w") as f: + f.write(json.dumps(toot, indent=4, default=jsondefault)) + f.close() toot_tags = set() for tag in toot.tags: @@ -210,6 +211,12 @@ def checkToots(config, options, retry_count=0): print( "Rate limit reached. Waiting for a rate limit reset" ) + # check for --archive-deleted + if options.archive_deleted and "id" in toot and "archive" in config: + # write toot to archive + with open(filename, "w") as f: + f.write(json.dumps(toot, indent=4, default=jsondefault)) + f.close() mastodon.status_unreblog(toot.reblog) else: if options.datestamp: @@ -249,7 +256,13 @@ def checkToots(config, options, retry_count=0): + str(format(diff / 60, ".0f")) + " minutes.\n" ) - + # check for --archive-deleted + if options.archive_deleted and "id" in toot and "archive" in config: + # write toot to archive + with open(filename, "w") as f: + f.write(json.dumps(toot, indent=4, default=jsondefault)) + f.close() + mastodon.status_delete(toot) except MastodonRatelimitError: