From 62e5217b9e2beb3d6da58507a31d0be0a2139c4a Mon Sep 17 00:00:00 2001 From: Akash Mahanty Date: Sun, 3 Jan 2021 19:38:25 +0530 Subject: [PATCH] reduce code complexity: refactoring, less flow breaking structures --- waybackpy/cli.py | 224 ++++++++++++++++++++++++++--------------------- 1 file changed, 124 insertions(+), 100 deletions(-) diff --git a/waybackpy/cli.py b/waybackpy/cli.py index b21b57a..577d158 100644 --- a/waybackpy/cli.py +++ b/waybackpy/cli.py @@ -73,16 +73,12 @@ def _total_archives(obj): def _near(obj, args): _near_args = {} - if args.year: - _near_args["year"] = args.year - if args.month: - _near_args["month"] = args.month - if args.day: - _near_args["day"] = args.day - if args.hour: - _near_args["hour"] = args.hour - if args.minute: - _near_args["minute"] = args.minute + args_arr = [args.year, args.month, args.day, args.hour, args.minute] + keys = ["year", "month", "day", "hour", "minute"] + + for key, arg in zip(keys, args_arr): + if arg: + _near_args[key] = arg try: return obj.near(**_near_args) @@ -133,29 +129,27 @@ def _known_urls(obj, args): def _get(obj, args): if args.get.lower() == "url": - output = obj.get() + return obj.get() - elif args.get.lower() == "archive_url": - output = obj.get(obj.archive_url) + if args.get.lower() == "archive_url": + return obj.get(obj.archive_url) - elif args.get.lower() == "oldest": - output = obj.get(obj.oldest()) + if args.get.lower() == "oldest": + return obj.get(obj.oldest()) - elif args.get.lower() == "latest" or args.get.lower() == "newest": - output = obj.get(obj.newest()) + if args.get.lower() == "latest" or args.get.lower() == "newest": + return obj.get(obj.newest()) - elif args.get.lower() == "save": - output = obj.get(obj.save()) + if args.get.lower() == "save": + return obj.get(obj.save()) - else: - output = "Use get as \"--get 'source'\", 'source' can be one of the followings: \ - \n1) url - get the source code of the url specified using --url/-u.\ - \n2) archive_url - get the source code of the newest archive for the supplied url, alias of newest.\ - \n3) oldest - get the source code of the oldest archive for the supplied url.\ - \n4) newest - get the source code of the newest archive for the supplied url.\ - \n5) save - Create a new archive and get the source code of this new archive for the supplied url." - return output + return "Use get as \"--get 'source'\", 'source' can be one of the followings: \ + \n1) url - get the source code of the url specified using --url/-u.\ + \n2) archive_url - get the source code of the newest archive for the supplied url, alias of newest.\ + \n3) oldest - get the source code of the oldest archive for the supplied url.\ + \n4) newest - get the source code of the newest archive for the supplied url.\ + \n5) save - Create a new archive and get the source code of this new archive for the supplied url." def args_handler(args): @@ -173,31 +167,103 @@ def args_handler(args): obj = Url(args.url, args.user_agent) if args.save: - output = _save(obj) - elif args.archive_url: - output = _archive_url(obj) - elif args.json: - output = _json(obj) - elif args.oldest: - output = _oldest(obj) - elif args.newest: - output = _newest(obj) - elif args.known_urls: - output = _known_urls(obj, args) - elif args.total: - output = _total_archives(obj) - elif args.near: - output = _near(obj, args) - elif args.get: - output = _get(obj, args) - else: - output = ( + return _save(obj) + if args.archive_url: + return _archive_url(obj) + if args.json: + return _json(obj) + if args.oldest: + return _oldest(obj) + if args.newest: + return _newest(obj) + if args.known_urls: + return _known_urls(obj, args) + if args.total: + return _total_archives(obj) + if args.near: + return _near(obj, args) + if args.get: + return _get(obj, args) + + return ( "You only specified the URL. But you also need to specify the operation." "\nSee 'waybackpy --help' for help using this tool." ) - return output +def add_userAgentArg(userAgentArg): + help_text = 'User agent, default user_agent is "waybackpy python package - https://github.com/akamhy/waybackpy"' + userAgentArg.add_argument("--user_agent", "-ua", help=help_text) + +def add_saveArg(saveArg): + saveArg.add_argument( + "--save", "-s", action="store_true", help="Save the URL on the Wayback machine" + ) + +def add_auArg(auArg): + auArg.add_argument( + "--archive_url", + "-au", + action="store_true", + help="Get the latest archive URL, alias for --newest", + ) + +def add_jsonArg(jsonArg): + jsonArg.add_argument( + "--json", + "-j", + action="store_true", + help="JSON data of the availability API request", + ) + +def add_oldestArg(oldestArg): + oldestArg.add_argument( + "--oldest", + "-o", + action="store_true", + help="Oldest archive for the specified URL", + ) + +def add_newestArg(newestArg): + newestArg.add_argument( + "--newest", + "-n", + action="store_true", + help="Newest archive for the specified URL", + ) + +def add_totalArg(totalArg): + totalArg.add_argument( + "--total", + "-t", + action="store_true", + help="Total number of archives for the specified URL", + ) + +def add_getArg(getArg): + getArg.add_argument( + "--get", + "-g", + help="Prints the source code of the supplied url. Use '--get help' for extended usage", + ) + +def add_knownUrlArg(knownUrlArg): + knownUrlArg.add_argument( + "--known_urls", "-ku", action="store_true", help="URLs known for the domain." + ) + help_text = "Use with '--known_urls' to include known URLs for subdomains." + knownUrlArg.add_argument("--subdomain", "-sub", action="store_true", help=help_text) + help_text = "Only include live URLs. Will not inlclude dead links." + knownUrlArg.add_argument("--alive", "-a", action="store_true", help=help_text) + + +def add_nearArgs(nearArgs): + nearArgs.add_argument("--year", "-Y", type=int, help="Year in integer") + nearArgs.add_argument("--month", "-M", type=int, help="Month in integer") + nearArgs.add_argument("--day", "-D", type=int, help="Day in integer.") + nearArgs.add_argument("--hour", "-H", type=int, help="Hour in intege") + nearArgs.add_argument("--minute", "-MIN", type=int, help="Minute in integer") + def parse_args(argv): parser = argparse.ArgumentParser() @@ -207,83 +273,41 @@ def parse_args(argv): ) userAgentArg = parser.add_argument_group("User Agent") - help_text = 'User agent, default user_agent is "waybackpy python package - https://github.com/akamhy/waybackpy"' - userAgentArg.add_argument("--user_agent", "-ua", help=help_text) + add_userAgentArg(userAgentArg) saveArg = parser.add_argument_group("Create new archive/save URL") - saveArg.add_argument( - "--save", "-s", action="store_true", help="Save the URL on the Wayback machine" - ) + add_saveArg(saveArg) auArg = parser.add_argument_group("Get the latest Archive") - auArg.add_argument( - "--archive_url", - "-au", - action="store_true", - help="Get the latest archive URL, alias for --newest", - ) + add_auArg(auArg) jsonArg = parser.add_argument_group("Get the JSON data") - jsonArg.add_argument( - "--json", - "-j", - action="store_true", - help="JSON data of the availability API request", - ) + add_jsonArg(jsonArg) oldestArg = parser.add_argument_group("Oldest archive") - oldestArg.add_argument( - "--oldest", - "-o", - action="store_true", - help="Oldest archive for the specified URL", - ) + add_oldestArg(oldestArg) newestArg = parser.add_argument_group("Newest archive") - newestArg.add_argument( - "--newest", - "-n", - action="store_true", - help="Newest archive for the specified URL", - ) + add_newestArg(newestArg) totalArg = parser.add_argument_group("Total number of archives") - totalArg.add_argument( - "--total", - "-t", - action="store_true", - help="Total number of archives for the specified URL", - ) + add_totalArg(totalArg) getArg = parser.add_argument_group("Get source code") - getArg.add_argument( - "--get", - "-g", - help="Prints the source code of the supplied url. Use '--get help' for extended usage", - ) + add_getArg(getArg) knownUrlArg = parser.add_argument_group( "URLs known and archived to Waybcak Machine for the site." ) - knownUrlArg.add_argument( - "--known_urls", "-ku", action="store_true", help="URLs known for the domain." - ) - help_text = "Use with '--known_urls' to include known URLs for subdomains." - knownUrlArg.add_argument("--subdomain", "-sub", action="store_true", help=help_text) - help_text = "Only include live URLs. Will not inlclude dead links." - knownUrlArg.add_argument("--alive", "-a", action="store_true", help=help_text) + add_knownUrlArg(knownUrlArg) nearArg = parser.add_argument_group("Archive close to time specified") nearArg.add_argument( "--near", "-N", action="store_true", help="Archive near specified time" ) - + #The following is adding supplementary args used with near. nearArgs = parser.add_argument_group("Arguments that are used only with --near") - nearArgs.add_argument("--year", "-Y", type=int, help="Year in integer") - nearArgs.add_argument("--month", "-M", type=int, help="Month in integer") - nearArgs.add_argument("--day", "-D", type=int, help="Day in integer.") - nearArgs.add_argument("--hour", "-H", type=int, help="Hour in intege") - nearArgs.add_argument("--minute", "-MIN", type=int, help="Minute in integer") + add_nearArgs(nearArgs) parser.add_argument( "--version", "-v", action="store_true", help="Waybackpy version"