refactor code in cli module
This commit is contained in:
parent
cf18090f90
commit
ec341fa8b3
129
waybackpy/cli.py
129
waybackpy/cli.py
@ -7,7 +7,7 @@ import random
|
|||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
from json import dumps
|
from json import dumps
|
||||||
from typing import Generator, List, Optional
|
from typing import Any, Generator, List, Optional
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import requests
|
import requests
|
||||||
@ -43,6 +43,70 @@ def echo_availability_api(
|
|||||||
click.echo(dumps(availability_api_instance.json))
|
click.echo(dumps(availability_api_instance.json))
|
||||||
|
|
||||||
|
|
||||||
|
def handle_cdx(data: List[Any]) -> None:
|
||||||
|
"""
|
||||||
|
Handles the CDX CLI options and output.
|
||||||
|
"""
|
||||||
|
url = data[0]
|
||||||
|
user_agent = data[1]
|
||||||
|
start_timestamp = data[2]
|
||||||
|
end_timestamp = data[3]
|
||||||
|
cdx_filter = data[4]
|
||||||
|
collapse = data[5]
|
||||||
|
cdx_print = data[6]
|
||||||
|
limit = data[7]
|
||||||
|
gzip = data[8]
|
||||||
|
match_type = data[9]
|
||||||
|
|
||||||
|
filters = list(cdx_filter)
|
||||||
|
collapses = list(collapse)
|
||||||
|
cdx_print = list(cdx_print)
|
||||||
|
|
||||||
|
cdx_api = WaybackMachineCDXServerAPI(
|
||||||
|
url,
|
||||||
|
user_agent=user_agent,
|
||||||
|
start_timestamp=start_timestamp,
|
||||||
|
end_timestamp=end_timestamp,
|
||||||
|
filters=filters,
|
||||||
|
match_type=match_type,
|
||||||
|
gzip=gzip,
|
||||||
|
collapses=collapses,
|
||||||
|
limit=limit,
|
||||||
|
)
|
||||||
|
|
||||||
|
snapshots = cdx_api.snapshots()
|
||||||
|
|
||||||
|
for snapshot in snapshots:
|
||||||
|
if len(cdx_print) == 0:
|
||||||
|
click.echo(snapshot)
|
||||||
|
else:
|
||||||
|
output_string = []
|
||||||
|
if any(val in cdx_print for val in ["urlkey", "url-key", "url_key"]):
|
||||||
|
output_string.append(snapshot.urlkey)
|
||||||
|
if any(
|
||||||
|
val in cdx_print for val in ["timestamp", "time-stamp", "time_stamp"]
|
||||||
|
):
|
||||||
|
output_string.append(snapshot.timestamp)
|
||||||
|
if "original" in cdx_print:
|
||||||
|
output_string.append(snapshot.original)
|
||||||
|
if any(val in cdx_print for val in ["mimetype", "mime-type", "mime_type"]):
|
||||||
|
output_string.append(snapshot.mimetype)
|
||||||
|
if any(
|
||||||
|
val in cdx_print for val in ["statuscode", "status-code", "status_code"]
|
||||||
|
):
|
||||||
|
output_string.append(snapshot.statuscode)
|
||||||
|
if "digest" in cdx_print:
|
||||||
|
output_string.append(snapshot.digest)
|
||||||
|
if "length" in cdx_print:
|
||||||
|
output_string.append(snapshot.length)
|
||||||
|
if any(
|
||||||
|
val in cdx_print for val in ["archiveurl", "archive-url", "archive_url"]
|
||||||
|
):
|
||||||
|
output_string.append(snapshot.archive_url)
|
||||||
|
|
||||||
|
click.echo(" ".join(output_string))
|
||||||
|
|
||||||
|
|
||||||
def save_urls_on_file(url_gen: Generator[str, None, None]) -> None:
|
def save_urls_on_file(url_gen: Generator[str, None, None]) -> None:
|
||||||
"""
|
"""
|
||||||
Save output of CDX API on file.
|
Save output of CDX API on file.
|
||||||
@ -347,58 +411,19 @@ def main( # pylint: disable=no-value-for-parameter
|
|||||||
click.echo(url_)
|
click.echo(url_)
|
||||||
|
|
||||||
elif cdx:
|
elif cdx:
|
||||||
filters = list(cdx_filter)
|
data = [
|
||||||
collapses = list(collapse)
|
|
||||||
cdx_print = list(cdx_print)
|
|
||||||
|
|
||||||
cdx_api = WaybackMachineCDXServerAPI(
|
|
||||||
url,
|
url,
|
||||||
user_agent=user_agent,
|
user_agent,
|
||||||
start_timestamp=start_timestamp,
|
start_timestamp,
|
||||||
end_timestamp=end_timestamp,
|
end_timestamp,
|
||||||
filters=filters,
|
cdx_filter,
|
||||||
match_type=match_type,
|
collapse,
|
||||||
gzip=gzip,
|
cdx_print,
|
||||||
collapses=collapses,
|
limit,
|
||||||
limit=limit,
|
gzip,
|
||||||
)
|
match_type,
|
||||||
|
]
|
||||||
snapshots = cdx_api.snapshots()
|
handle_cdx(data)
|
||||||
|
|
||||||
for snapshot in snapshots:
|
|
||||||
if len(cdx_print) == 0:
|
|
||||||
click.echo(snapshot)
|
|
||||||
else:
|
|
||||||
output_string = []
|
|
||||||
if any(val in cdx_print for val in ["urlkey", "url-key", "url_key"]):
|
|
||||||
output_string.append(snapshot.urlkey)
|
|
||||||
if any(
|
|
||||||
val in cdx_print
|
|
||||||
for val in ["timestamp", "time-stamp", "time_stamp"]
|
|
||||||
):
|
|
||||||
output_string.append(snapshot.timestamp)
|
|
||||||
if "original" in cdx_print:
|
|
||||||
output_string.append(snapshot.original)
|
|
||||||
if any(
|
|
||||||
val in cdx_print for val in ["mimetype", "mime-type", "mime_type"]
|
|
||||||
):
|
|
||||||
output_string.append(snapshot.mimetype)
|
|
||||||
if any(
|
|
||||||
val in cdx_print
|
|
||||||
for val in ["statuscode", "status-code", "status_code"]
|
|
||||||
):
|
|
||||||
output_string.append(snapshot.statuscode)
|
|
||||||
if "digest" in cdx_print:
|
|
||||||
output_string.append(snapshot.digest)
|
|
||||||
if "length" in cdx_print:
|
|
||||||
output_string.append(snapshot.length)
|
|
||||||
if any(
|
|
||||||
val in cdx_print
|
|
||||||
for val in ["archiveurl", "archive-url", "archive_url"]
|
|
||||||
):
|
|
||||||
output_string.append(snapshot.archive_url)
|
|
||||||
|
|
||||||
click.echo(" ".join(output_string))
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
click.echo(
|
click.echo(
|
||||||
|
Loading…
Reference in New Issue
Block a user