2023-03-26 04:57:18 +09:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
import click
|
|
|
|
from requests.exceptions import HTTPError
|
|
|
|
|
2023-04-08 03:32:34 +09:00
|
|
|
from . import __version__
|
2023-03-26 04:57:18 +09:00
|
|
|
from .client import ISDNClient
|
2023-04-08 03:32:34 +09:00
|
|
|
from .model import ISDN, ISDNRoot
|
2023-03-26 04:57:18 +09:00
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def version():
|
|
|
|
click.echo(f"isdn/{__version__}")
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("get", help="Get record from isdn.jp")
|
|
|
|
@click.argument("isdn")
|
|
|
|
@click.option("--format", "-f", type=click.Choice(["xml", "dict", "json"]), default="xml")
|
|
|
|
def get_isdn(isdn: str, format: str):
|
|
|
|
c = ISDNClient()
|
|
|
|
match format:
|
|
|
|
case "xml":
|
|
|
|
res = c.get_raw(isdn)
|
|
|
|
case "dict":
|
2023-04-08 03:32:34 +09:00
|
|
|
res = c.get(isdn).dict()
|
2023-03-26 04:57:18 +09:00
|
|
|
case "json":
|
2023-04-08 03:32:34 +09:00
|
|
|
res = c.get(isdn).json(ensure_ascii=False)
|
2023-03-26 04:57:18 +09:00
|
|
|
case _:
|
|
|
|
raise NotImplementedError
|
|
|
|
click.echo(res)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("list", help="List all ISDNs from isdn.jp")
|
|
|
|
def list_isdns():
|
|
|
|
c = ISDNClient()
|
|
|
|
for isdn in c.list():
|
|
|
|
click.echo(isdn)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command(help="Download all xml record files from isdn.jp")
|
|
|
|
@click.argument("directory", type=click.Path(exists=True, file_okay=False, writable=True))
|
|
|
|
@click.option("--force", "-f", is_flag=True, help="Overwrite existing files")
|
2023-04-03 04:16:28 +09:00
|
|
|
@click.option("--stop-on-error", is_flag=True, help="Stop when error occurs during download")
|
2023-04-03 01:32:09 +09:00
|
|
|
@click.option("--sleep-time", "-s", type=int, default=500, help="Sleep interval for download (millisecond)")
|
2023-04-03 04:16:28 +09:00
|
|
|
@click.option("--write-image", is_flag=True, help="Write cover image to file")
|
|
|
|
@click.option(
|
|
|
|
"--write-image-path",
|
|
|
|
type=click.Path(exists=True, file_okay=False, writable=True),
|
|
|
|
help="Directory path to write cover images",
|
|
|
|
)
|
|
|
|
def bulk_download(
|
|
|
|
directory: str, force: bool, stop_on_error: bool, sleep_time: int, write_image: bool, write_image_path: str
|
|
|
|
):
|
2023-03-26 04:57:18 +09:00
|
|
|
c = ISDNClient()
|
|
|
|
with click.progressbar(list(c.list()), show_pos=True) as bar:
|
|
|
|
for isdn in bar:
|
|
|
|
path = os.path.join(directory, f"{isdn}.xml")
|
2023-04-03 04:16:28 +09:00
|
|
|
image_path = os.path.join(write_image_path or directory, f"{isdn}.png")
|
|
|
|
if not force and os.path.exists(path) and (not write_image or write_image and os.path.exists(image_path)):
|
2023-03-26 04:57:18 +09:00
|
|
|
continue
|
2023-04-03 04:16:28 +09:00
|
|
|
|
2023-03-26 04:57:18 +09:00
|
|
|
try:
|
|
|
|
res = c.get_raw(isdn)
|
2023-04-03 04:16:28 +09:00
|
|
|
with open(path, "wb") as out:
|
|
|
|
out.write(res)
|
|
|
|
|
|
|
|
if write_image:
|
2023-04-08 03:32:34 +09:00
|
|
|
record = ISDNRoot.from_xml_first(res)
|
2023-04-03 04:16:28 +09:00
|
|
|
if record.sample_image_uri:
|
|
|
|
img = c.get_image(isdn)
|
|
|
|
with open(image_path, "wb") as out:
|
|
|
|
out.write(img)
|
2023-03-26 04:57:18 +09:00
|
|
|
except HTTPError as err:
|
|
|
|
if stop_on_error:
|
|
|
|
raise err
|
|
|
|
else:
|
|
|
|
continue
|
2023-04-03 04:16:28 +09:00
|
|
|
|
2023-03-26 04:57:18 +09:00
|
|
|
time.sleep(sleep_time / 1000)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command(help="Validate ISDN code")
|
|
|
|
@click.argument("isdn")
|
|
|
|
def validate(isdn: str):
|
|
|
|
try:
|
|
|
|
click.echo(ISDN(isdn).validate(raise_error=True))
|
|
|
|
except Exception as e:
|
|
|
|
click.echo(e, err=True)
|