Coverge improvements (#22)
* Update cli.py * improved tests * chnages for proper testing * Type check using isinstance * Replace elifs with if when used after return * twitter.com --> www.ibm.com * fix typo * test archive urll parser and dunders * Update test_wrapper.py
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import argparse
|
||||
from waybackpy.wrapper import Url
|
||||
from waybackpy.__version__ import __version__
|
||||
|
||||
def _save(obj):
|
||||
print(obj.save())
|
||||
return (obj.save())
|
||||
|
||||
def _oldest(obj):
|
||||
print(obj.oldest())
|
||||
return (obj.oldest())
|
||||
|
||||
def _newest(obj):
|
||||
print(obj.newest())
|
||||
return (obj.newest())
|
||||
|
||||
def _total_archives(obj):
|
||||
print(obj.total_archives())
|
||||
return (obj.total_archives())
|
||||
|
||||
def _near(obj, args):
|
||||
_near_args = {}
|
||||
@@ -28,29 +29,54 @@ def _near(obj, args):
|
||||
_near_args["hour"] = args.hour
|
||||
if args.minute:
|
||||
_near_args["minute"] = args.minute
|
||||
print(obj.near(**_near_args))
|
||||
return (obj.near(**_near_args))
|
||||
|
||||
def _get(obj, args):
|
||||
if args.get.lower() == "url":
|
||||
print(obj.get())
|
||||
return (obj.get())
|
||||
|
||||
elif args.get.lower() == "oldest":
|
||||
print(obj.get(obj.oldest()))
|
||||
if args.get.lower() == "oldest":
|
||||
return (obj.get(obj.oldest()))
|
||||
|
||||
elif args.get.lower() == "latest" or args.get.lower() == "newest":
|
||||
print(obj.get(obj.newest()))
|
||||
if args.get.lower() == "latest" or args.get.lower() == "newest":
|
||||
return (obj.get(obj.newest()))
|
||||
|
||||
elif args.get.lower() == "save":
|
||||
print(obj.get(obj.save()))
|
||||
if args.get.lower() == "save":
|
||||
return (obj.get(obj.save()))
|
||||
|
||||
else:
|
||||
print("Use get as \"--get 'source'\", 'source' can be one of the followings: \
|
||||
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) oldest - get the source code of the oldest archive for the supplied url.\
|
||||
\n3) newest - get the source code of the newest archive for the supplied url.\
|
||||
\n4) save - Create a new archive and get the source code of this new archive for the supplied url.")
|
||||
|
||||
def main():
|
||||
def args_handler(args):
|
||||
if args.version:
|
||||
return (__version__)
|
||||
|
||||
if not args.url:
|
||||
return ("Specify an URL. See --help for help using waybackpy.")
|
||||
|
||||
if args.user_agent:
|
||||
obj = Url(args.url, args.user_agent)
|
||||
else:
|
||||
obj = Url(args.url)
|
||||
|
||||
if args.save:
|
||||
return _save(obj)
|
||||
if args.oldest:
|
||||
return _oldest(obj)
|
||||
if args.newest:
|
||||
return _newest(obj)
|
||||
if args.total:
|
||||
return _total_archives(obj)
|
||||
if args.near:
|
||||
return _near(obj, args)
|
||||
if args.get:
|
||||
return _get(obj, args)
|
||||
return ("Usage: waybackpy --url [URL] --user_agent [USER AGENT] [OPTIONS]. See --help for help using waybackpy.")
|
||||
|
||||
def parse_args(argv):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-u", "--url", help="URL on which Wayback machine operations would occur.")
|
||||
parser.add_argument("-ua", "--user_agent", help="User agent, default user_agent is \"waybackpy python package - https://github.com/akamhy/waybackpy\".")
|
||||
@@ -60,45 +86,18 @@ def main():
|
||||
parser.add_argument("-t", "--total", action='store_true', help="Total number of archives for the specified URL.")
|
||||
parser.add_argument("-g", "--get", help="Prints the source code of the supplied url. Use '--get help' for extended usage.")
|
||||
parser.add_argument("-v", "--version", action='store_true', help="Prints the waybackpy version.")
|
||||
|
||||
parser.add_argument("-N", "--near", action='store_true', help="Latest/Newest archive for the specified URL.")
|
||||
parser.add_argument("-Y", "--year", type=int, help="Year in integer. For use with --near.")
|
||||
parser.add_argument("-M", "--month", type=int, help="Month in integer. For use with --near.")
|
||||
parser.add_argument("-D", "--day", type=int, help="Day in integer. For use with --near.")
|
||||
parser.add_argument("-H", "--hour", type=int, help="Hour in integer. For use with --near.")
|
||||
parser.add_argument("-MIN", "--minute", type=int, help="Minute in integer. For use with --near.")
|
||||
return parser.parse_args(argv[1:])
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.version:
|
||||
print(__version__)
|
||||
return
|
||||
|
||||
if not args.url:
|
||||
print("Specify an URL. See --help")
|
||||
return
|
||||
|
||||
# create the object with or without the user_agent
|
||||
if args.user_agent:
|
||||
obj = Url(args.url, args.user_agent)
|
||||
else:
|
||||
obj = Url(args.url)
|
||||
|
||||
if args.save:
|
||||
_save(obj)
|
||||
elif args.oldest:
|
||||
_oldest(obj)
|
||||
elif args.newest:
|
||||
_newest(obj)
|
||||
elif args.total:
|
||||
_total_archives(obj)
|
||||
elif args.near:
|
||||
_near(obj, args)
|
||||
elif args.get:
|
||||
_get(obj, args)
|
||||
else:
|
||||
print("Usage: waybackpy --url [URL] --user_agent [USER AGENT] [OPTIONS]. See --help")
|
||||
|
||||
def main(argv):
|
||||
args = parse_args(argv)
|
||||
output = args_handler(args)
|
||||
print(output)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
sys.exit(main(sys.argv))
|
||||
|
Reference in New Issue
Block a user