command_line support (#18)

* Update wrapper.py

* entry points cli

* Suppress the urllib2/3 Exception

* rm cli code, will create a new cli.py file

* Create cli.py

* update cli entry pts

* Update cli.py

* Update cli.py

* import print_function

* Update cli.py

* Update cli.py

* Delete pypi_uploader.sh

* resolve conflicts with the master

* update the test ; resolve the conflicts

* decrease code complexity

* cli method changed to main

* get is not for just local usage

* get method should be available from interface

* get is used in the interface

* Update cli.py
This commit is contained in:
Akash 2020-07-22 16:40:13 +05:30 committed by GitHub
parent 3bfc3b46d0
commit dee9105794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 14 deletions

View File

@ -1,12 +0,0 @@
#!/bin/bash
python3 --V
cd ..
python3 -m venv waybackvenv
. waybackvenv/bin/activate
cd -
pip3 install --upgrade pip3
pip3 install setuptools -U
pip3 install wheel --upgrade
pip3 install twine -U
python3 setup.py sdist bdist_wheel
twine upload dist/*

View File

@ -42,6 +42,11 @@ setup(
'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: CPython',
], ],
entry_points={
'console_scripts': [
'waybackpy = waybackpy.cli:main'
]
},
project_urls={ project_urls={
'Documentation': 'https://waybackpy.readthedocs.io', 'Documentation': 'https://waybackpy.readthedocs.io',
'Source': 'https://github.com/akamhy/waybackpy', 'Source': 'https://github.com/akamhy/waybackpy',

View File

@ -146,7 +146,8 @@ def test_newest():
def test_get(): def test_get():
target = waybackpy.Url("google.com", user_agent) target = waybackpy.Url("google.com", user_agent)
assert "Welcome to Google" in target._get(target.oldest()) assert "Welcome to Google" in target.get(target.oldest())
def test_wayback_timestamp(): def test_wayback_timestamp():

101
waybackpy/cli.py Normal file
View File

@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
from waybackpy.wrapper import Url
def _save(obj):
print(obj.save())
def _oldest(obj):
print(obj.oldest())
def _newest(obj):
print(obj.newest())
def _total_archives(obj):
print(obj.total_archives())
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
print(obj.near(**_near_args))
def _get(obj, args):
if args.get.lower() == "url":
print(obj.get())
elif args.get.lower() == "oldest":
print(obj.get(obj.oldest()))
elif args.get.lower() == "latest" or args.get.lower() == "newest":
print(obj.get(obj.newest()))
elif args.get.lower() == "save":
print(obj.get(obj.save()))
else:
print("Please 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) latest - get the source code of the latest 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():
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\".")
parser.add_argument("-s", "--save", action='store_true', help="Save the URL on the Wayback machine.")
parser.add_argument("-o", "--oldest", action='store_true', help="Oldest archive for the specified URL.")
parser.add_argument("-l", "--latest", action='store_true', help="Latest/Newest archive for the specified URL.")
parser.add_argument("-t", "--total", action='store_true', help="Total number of archives for the specified URL.")
parser.add_argument("-g", "--get", help="Get the source code of the supplied url. Use '--get help' for extended usage.")
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("-m", "--minute", type=int, help="Minute in integer. For use with --near.")
args = parser.parse_args()
if not args.url:
print("please specify an URL using \"--url https://mywebiste.com\". Use --help for 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)
print(repr(obj))
if args.save:
_save(obj)
elif args.oldest:
_oldest(obj)
elif args.latest:
_newest(obj)
elif args.total:
_total_archives(obj)
elif args.near:
_near(obj, args)
elif args.get:
_get(obj, args)
else:
print("Please specify any operation as an argument. Use 'waybackpy --help' for help using wayback.\
\nLatest docs and version available at https://github.com/akamhy/waybackpy ")
if __name__ == "__main__":
main()

View File

@ -89,7 +89,7 @@ class Url:
header = _get_response(req).headers header = _get_response(req).headers
return "https://" + _archive_url_parser(header) return "https://" + _archive_url_parser(header)
def _get(self, url="", user_agent="", encoding=""): def get(self, url="", user_agent="", encoding=""):
"""Return the source code of the supplied URL. """Return the source code of the supplied URL.
If encoding is not supplied, it is auto-detected from the response. If encoding is not supplied, it is auto-detected from the response.
""" """