reduce code complexity: refactoring, less flow breaking structures
This commit is contained in:
parent
9823c809e9
commit
62e5217b9e
224
waybackpy/cli.py
224
waybackpy/cli.py
@ -73,16 +73,12 @@ def _total_archives(obj):
|
|||||||
|
|
||||||
def _near(obj, args):
|
def _near(obj, args):
|
||||||
_near_args = {}
|
_near_args = {}
|
||||||
if args.year:
|
args_arr = [args.year, args.month, args.day, args.hour, args.minute]
|
||||||
_near_args["year"] = args.year
|
keys = ["year", "month", "day", "hour", "minute"]
|
||||||
if args.month:
|
|
||||||
_near_args["month"] = args.month
|
for key, arg in zip(keys, args_arr):
|
||||||
if args.day:
|
if arg:
|
||||||
_near_args["day"] = args.day
|
_near_args[key] = arg
|
||||||
if args.hour:
|
|
||||||
_near_args["hour"] = args.hour
|
|
||||||
if args.minute:
|
|
||||||
_near_args["minute"] = args.minute
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return obj.near(**_near_args)
|
return obj.near(**_near_args)
|
||||||
@ -133,29 +129,27 @@ def _known_urls(obj, args):
|
|||||||
|
|
||||||
def _get(obj, args):
|
def _get(obj, args):
|
||||||
if args.get.lower() == "url":
|
if args.get.lower() == "url":
|
||||||
output = obj.get()
|
return obj.get()
|
||||||
|
|
||||||
elif args.get.lower() == "archive_url":
|
if args.get.lower() == "archive_url":
|
||||||
output = obj.get(obj.archive_url)
|
return obj.get(obj.archive_url)
|
||||||
|
|
||||||
elif args.get.lower() == "oldest":
|
if args.get.lower() == "oldest":
|
||||||
output = obj.get(obj.oldest())
|
return obj.get(obj.oldest())
|
||||||
|
|
||||||
elif args.get.lower() == "latest" or args.get.lower() == "newest":
|
if args.get.lower() == "latest" or args.get.lower() == "newest":
|
||||||
output = obj.get(obj.newest())
|
return obj.get(obj.newest())
|
||||||
|
|
||||||
elif args.get.lower() == "save":
|
if args.get.lower() == "save":
|
||||||
output = obj.get(obj.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):
|
def args_handler(args):
|
||||||
@ -173,31 +167,103 @@ def args_handler(args):
|
|||||||
obj = Url(args.url, args.user_agent)
|
obj = Url(args.url, args.user_agent)
|
||||||
|
|
||||||
if args.save:
|
if args.save:
|
||||||
output = _save(obj)
|
return _save(obj)
|
||||||
elif args.archive_url:
|
if args.archive_url:
|
||||||
output = _archive_url(obj)
|
return _archive_url(obj)
|
||||||
elif args.json:
|
if args.json:
|
||||||
output = _json(obj)
|
return _json(obj)
|
||||||
elif args.oldest:
|
if args.oldest:
|
||||||
output = _oldest(obj)
|
return _oldest(obj)
|
||||||
elif args.newest:
|
if args.newest:
|
||||||
output = _newest(obj)
|
return _newest(obj)
|
||||||
elif args.known_urls:
|
if args.known_urls:
|
||||||
output = _known_urls(obj, args)
|
return _known_urls(obj, args)
|
||||||
elif args.total:
|
if args.total:
|
||||||
output = _total_archives(obj)
|
return _total_archives(obj)
|
||||||
elif args.near:
|
if args.near:
|
||||||
output = _near(obj, args)
|
return _near(obj, args)
|
||||||
elif args.get:
|
if args.get:
|
||||||
output = _get(obj, args)
|
return _get(obj, args)
|
||||||
else:
|
|
||||||
output = (
|
return (
|
||||||
"You only specified the URL. But you also need to specify the operation."
|
"You only specified the URL. But you also need to specify the operation."
|
||||||
"\nSee 'waybackpy --help' for help using this tool."
|
"\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):
|
def parse_args(argv):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
@ -207,83 +273,41 @@ def parse_args(argv):
|
|||||||
)
|
)
|
||||||
|
|
||||||
userAgentArg = parser.add_argument_group("User Agent")
|
userAgentArg = parser.add_argument_group("User Agent")
|
||||||
help_text = 'User agent, default user_agent is "waybackpy python package - https://github.com/akamhy/waybackpy"'
|
add_userAgentArg(userAgentArg)
|
||||||
userAgentArg.add_argument("--user_agent", "-ua", help=help_text)
|
|
||||||
|
|
||||||
saveArg = parser.add_argument_group("Create new archive/save URL")
|
saveArg = parser.add_argument_group("Create new archive/save URL")
|
||||||
saveArg.add_argument(
|
add_saveArg(saveArg)
|
||||||
"--save", "-s", action="store_true", help="Save the URL on the Wayback machine"
|
|
||||||
)
|
|
||||||
|
|
||||||
auArg = parser.add_argument_group("Get the latest Archive")
|
auArg = parser.add_argument_group("Get the latest Archive")
|
||||||
auArg.add_argument(
|
add_auArg(auArg)
|
||||||
"--archive_url",
|
|
||||||
"-au",
|
|
||||||
action="store_true",
|
|
||||||
help="Get the latest archive URL, alias for --newest",
|
|
||||||
)
|
|
||||||
|
|
||||||
jsonArg = parser.add_argument_group("Get the JSON data")
|
jsonArg = parser.add_argument_group("Get the JSON data")
|
||||||
jsonArg.add_argument(
|
add_jsonArg(jsonArg)
|
||||||
"--json",
|
|
||||||
"-j",
|
|
||||||
action="store_true",
|
|
||||||
help="JSON data of the availability API request",
|
|
||||||
)
|
|
||||||
|
|
||||||
oldestArg = parser.add_argument_group("Oldest archive")
|
oldestArg = parser.add_argument_group("Oldest archive")
|
||||||
oldestArg.add_argument(
|
add_oldestArg(oldestArg)
|
||||||
"--oldest",
|
|
||||||
"-o",
|
|
||||||
action="store_true",
|
|
||||||
help="Oldest archive for the specified URL",
|
|
||||||
)
|
|
||||||
|
|
||||||
newestArg = parser.add_argument_group("Newest archive")
|
newestArg = parser.add_argument_group("Newest archive")
|
||||||
newestArg.add_argument(
|
add_newestArg(newestArg)
|
||||||
"--newest",
|
|
||||||
"-n",
|
|
||||||
action="store_true",
|
|
||||||
help="Newest archive for the specified URL",
|
|
||||||
)
|
|
||||||
|
|
||||||
totalArg = parser.add_argument_group("Total number of archives")
|
totalArg = parser.add_argument_group("Total number of archives")
|
||||||
totalArg.add_argument(
|
add_totalArg(totalArg)
|
||||||
"--total",
|
|
||||||
"-t",
|
|
||||||
action="store_true",
|
|
||||||
help="Total number of archives for the specified URL",
|
|
||||||
)
|
|
||||||
|
|
||||||
getArg = parser.add_argument_group("Get source code")
|
getArg = parser.add_argument_group("Get source code")
|
||||||
getArg.add_argument(
|
add_getArg(getArg)
|
||||||
"--get",
|
|
||||||
"-g",
|
|
||||||
help="Prints the source code of the supplied url. Use '--get help' for extended usage",
|
|
||||||
)
|
|
||||||
|
|
||||||
knownUrlArg = parser.add_argument_group(
|
knownUrlArg = parser.add_argument_group(
|
||||||
"URLs known and archived to Waybcak Machine for the site."
|
"URLs known and archived to Waybcak Machine for the site."
|
||||||
)
|
)
|
||||||
knownUrlArg.add_argument(
|
add_knownUrlArg(knownUrlArg)
|
||||||
"--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)
|
|
||||||
|
|
||||||
nearArg = parser.add_argument_group("Archive close to time specified")
|
nearArg = parser.add_argument_group("Archive close to time specified")
|
||||||
nearArg.add_argument(
|
nearArg.add_argument(
|
||||||
"--near", "-N", action="store_true", help="Archive near specified time"
|
"--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 = parser.add_argument_group("Arguments that are used only with --near")
|
||||||
nearArgs.add_argument("--year", "-Y", type=int, help="Year in integer")
|
add_nearArgs(nearArgs)
|
||||||
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")
|
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--version", "-v", action="store_true", help="Waybackpy version"
|
"--version", "-v", action="store_true", help="Waybackpy version"
|
||||||
|
Loading…
Reference in New Issue
Block a user