diff --git a/ephemetoot/console.py b/ephemetoot/console.py index 3697451..a174842 100644 --- a/ephemetoot/console.py +++ b/ephemetoot/console.py @@ -30,7 +30,7 @@ import yaml from argparse import ArgumentParser from datetime import datetime, timezone import os -import pkg_resources +import pkg_resources # import funtions from ephemetoot import ephemetoot as func @@ -40,49 +40,81 @@ 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" + "--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'in the current directory" + "--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'in the current directory", ) parser.add_argument( - "--datestamp", action="store_true", help="Include a datetime stamp for every action (e.g. deleting a toot)" + "--datestamp", + action="store_true", + help="Include a datetime stamp for every action (e.g. deleting a toot)", ) parser.add_argument( - "--hide-skipped", "--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( - "--init", action="store_true", help="Create a config file that is saved in the current directory" + "--init", + action="store_true", + help="Create a config file that is saved in the current directory", ) parser.add_argument( - "--pace", action="store_true", help="Slow deletion actions to match API rate limit to avoid pausing" + "--pace", + action="store_true", + help="Slow deletion actions to match API rate limit to avoid pausing", +) +parser.add_argument("--quiet", action="store_true", help="Suppress most logging") +parser.add_argument( + "--retry-mins", + action="store", + metavar="minutes", + nargs="?", + const="1", + default="1", + type=int, + help="Number of minutes to wait between retries, when an error is thrown", ) parser.add_argument( - "--quiet", action="store_true", help="Suppress most logging" -) -parser.add_argument( - "--retry-mins", action="store", metavar="minutes", nargs="?", const="1", default="1", type=int, help="Number of minutes to wait between retries, when an error is thrown" -) -parser.add_argument( - "--schedule", action="store", metavar="filepath", nargs="?", const=".", help="Save and load plist file on MacOS" + "--schedule", + action="store", + metavar="filepath", + nargs="?", + const=".", + help="Save and load plist file on MacOS", ) parser.add_argument( "--test", action="store_true", help="Do a test run without deleting any toots" ) parser.add_argument( - "--time", action="store", metavar=('hour', 'minute'), nargs="*", help="Hour and minute to schedule: e.g. 9 30 for 9.30am" + "--time", + action="store", + metavar=("hour", "minute"), + nargs="*", + help="Hour and minute to schedule: e.g. 9 30 for 9.30am", ) parser.add_argument( - "--version", action="store_true", help="Display the version numbers of the installed and latest versions" + "--version", + action="store_true", + help="Display the version numbers of the installed and latest versions", ) options = parser.parse_args() -if options.config[0] == '~': +if options.config[0] == "~": config_file = os.path.expanduser(options.config) -elif options.config[0] == '/': +elif options.config[0] == "/": config_file = options.config -else: - config_file = os.path.join( os.getcwd(), options.config ) +else: + config_file = os.path.join(os.getcwd(), options.config) + def main(): try: @@ -95,11 +127,16 @@ def main(): func.schedule(options) else: if not options.quiet: - print('') - print('============= EPHEMETOOT v' + vnum + ' ================') - print('Running at ' + str( datetime.now(timezone.utc).strftime('%a %d %b %Y %H:%M:%S %z') )) - print('================================================') - print('') + print("") + print("============= EPHEMETOOT v" + vnum + " ================") + print( + "Running at " + + str( + datetime.now(timezone.utc).strftime("%a %d %b %Y %H:%M:%S %z") + ) + ) + print("================================================") + print("") if options.test: print("This is a test run...\n") with open(config_file) as config: @@ -111,5 +148,6 @@ def main(): print("šŸ•µļø Missing config file") print("Run \033[92mephemetoot --init\033[0m to create a new one\n") -if __name__ == '__main__': - main() \ No newline at end of file + +if __name__ == "__main__": + main() diff --git a/ephemetoot/ephemetoot.py b/ephemetoot/ephemetoot.py index afb4310..fbf6d4b 100644 --- a/ephemetoot/ephemetoot.py +++ b/ephemetoot/ephemetoot.py @@ -19,6 +19,7 @@ import requests # local from ephemetoot import plist + def init(): init_start = "\033[96m" @@ -32,76 +33,64 @@ def init(): conf_user = "" while len(conf_user) < 1: conf_user = input( - init_start - + "Username" - + init_eg - + "(without the '@' - e.g. alice):" - + init_end + init_start + + "Username" + + init_eg + + "(without the '@' - e.g. alice):" + + init_end ) conf_url = "" while len(conf_url) < 1: conf_url = input( - init_start - + "Base URL" - + init_eg - + "(e.g. example.social):" - + init_end + init_start + "Base URL" + init_eg + "(e.g. example.social):" + init_end ) conf_days = "" while conf_days.isdigit() == False: conf_days = input( - init_start - + "Days to keep" - + init_eg - + "(default 365):" - + init_end + init_start + "Days to keep" + init_eg + "(default 365):" + init_end ) conf_keep_pinned = "" while conf_keep_pinned not in ["y", "n"]: conf_keep_pinned = input( - init_start - + "Keep pinned toots?" - + init_eg - + "(y or n):" - + init_end + init_start + "Keep pinned toots?" + init_eg + "(y or n):" + init_end ) conf_pinned = "true" if conf_keep_pinned == "y" else "false" conf_keep_toots = input( - init_start + init_start + "Toots to keep" - + init_eg + + init_eg + " (optional list of IDs separated by commas):" + init_end - ) + ) conf_keep_hashtags = input( - init_start + init_start + "Hashtags to keep" - + init_eg + + init_eg + " (optional list separated by commas):" + init_end - ) + ) conf_keep_visibility = input( - init_start + init_start + "Visibility to keep" - + init_eg - + " (optional list separated by commas):" + + init_eg + + " (optional list separated by commas):" + init_end - ) + ) conf_archive = input( - init_start + init_start + "Archive path" - + init_eg + + init_eg + " (optional filepath for archive):" + init_end - ) + ) # write out the config file with open("config.yaml", "w") as configfile: @@ -114,28 +103,29 @@ def init(): configfile.write("\n keep_pinned: " + conf_pinned) if len(conf_keep_toots) > 0: - keep_list = conf_keep_toots.split(',') + keep_list = conf_keep_toots.split(",") configfile.write("\n toots_to_keep:") for toot in keep_list: - configfile.write("\n - " + toot.strip()) + configfile.write("\n - " + toot.strip()) if len(conf_keep_hashtags) > 0: - tag_list = conf_keep_hashtags.split(',') - configfile.write("\n hashtags_to_keep:") + tag_list = conf_keep_hashtags.split(",") + configfile.write("\n hashtags_to_keep:") for tag in tag_list: - configfile.write("\n - " + tag.strip()) + configfile.write("\n - " + tag.strip()) if len(conf_keep_visibility) > 0: - viz_list = conf_keep_visibility.split(',') - configfile.write("\n visibility_to_keep:") + viz_list = conf_keep_visibility.split(",") + configfile.write("\n visibility_to_keep:") for mode in viz_list: - configfile.write("\n - " + mode.strip()) + configfile.write("\n - " + mode.strip()) if len(conf_archive) > 0: configfile.write("\n archive: " + conf_archive) configfile.close() + def version(vnum): try: latest = requests.get( @@ -147,11 +137,14 @@ def version(vnum): print("-------------------------------") print("Using: \033[92mVersion " + vnum + "\033[0m") print("Latest: \033[92m" + latest_version + "\033[0m") - print("To upgrade to the most recent version run \033[92mpip3 install --update ephemetoot\033[0m") + print( + "To upgrade to the most recent version run \033[92mpip3 install --update ephemetoot\033[0m" + ) except Exception as e: print("Something went wrong:") + def schedule(options): try: @@ -172,7 +165,11 @@ def schedule(options): lines[23] = " " + options.time[1] + "" # write out file directly to ~/Library/LaunchAgents - f = open(os.path.expanduser("~/Library/LaunchAgents/") + "ephemetoot.scheduler.plist", mode="w") + f = open( + os.path.expanduser("~/Library/LaunchAgents/") + + "ephemetoot.scheduler.plist", + mode="w", + ) for line in lines: if line == lines[-1]: f.write(line) @@ -185,12 +182,12 @@ def schedule(options): ["launchctl unload ~/Library/LaunchAgents/ephemetoot.scheduler.plist"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, - shell=True + shell=True, ) # load the new file subprocess.run( ["launchctl load ~/Library/LaunchAgents/ephemetoot.scheduler.plist"], - shell=True + shell=True, ) print("ā° Scheduled!") except Exception as e: @@ -569,7 +566,9 @@ def checkToots(config, options, retry_count=0): except MastodonAPIError as e: if e.args[1] == 401: - print("\nšŸ™… User and/or access token does not exist or has been deleted (401)") + print( + "\nšŸ™… User and/or access token does not exist or has been deleted (401)" + ) elif e.args[1] == 404: print("\nšŸ”­ Can't find that server (404)") else: @@ -579,11 +578,7 @@ def checkToots(config, options, retry_count=0): if retry_count == 0: print("\nšŸ“” ephemetoot cannot connect to the server - are you online?") if retry_count < 4: - print( - "Waiting " - + str(options.retry_mins) - + " minutes before trying again" - ) + print("Waiting " + str(options.retry_mins) + " minutes before trying again") time.sleep(60 * options.retry_mins) retry_count += 1 print("Attempt " + str(retry_count + 1)) diff --git a/ephemetoot/plist.py b/ephemetoot/plist.py index 23d9a83..9be6343 100644 --- a/ephemetoot/plist.py +++ b/ephemetoot/plist.py @@ -1,4 +1,4 @@ -default_file = ''' +default_file = """ @@ -24,4 +24,4 @@ default_file = ''' 00 -''' \ No newline at end of file +"""