Example #1
0
def package(
    id_or_url: str,
    targets: Iterable[str],
    target_all: bool,
    packager_provider: BasePackagerProvider = Provide[
        Application.packagers.packager_provider],
    path_service: BasePathService = Provide[Application.services.path_service],
):
    """Package the selected novel into the formats of choosing"""
    if target_all:
        packagers = packager_provider.packagers()
    else:
        try:
            packagers = packager_provider.filter_packagers(targets)
        except ValueError as e:
            logger.error(e)
            sys.exit(1)

    try:
        novel = get_novel(id_or_url)
    except ValueError:
        sys.exit(1)

    for packager in packagers:
        try:
            path = packager.package(novel)
        except (ToolException, RequirementException, FileNotFoundError) as e:
            logger.error(
                f"Packaging to '{packager.keywords()[0]}' failed: {e}.")
            logger.exception(e)
        else:
            logger.info(
                f"Packaging to '{packager.keywords()[0]}' succeeded and saved to '{{novel.dir}}/{path_service.relative_to_novel_dir(path)}'."
            )
Example #2
0
def delete_associations(
    id_or_url: str,
    novel_service: BaseNovelService = Provide[
        Application.services.novel_service],
    path_service: BasePathService = Provide[Application.services.path_service],
    asset_service: BaseAssetService = Provide[
        Application.services.asset_service],
):
    """Removes all except vital information related to novel, this includes chapters, metadata, and assets."""
    try:
        novel = cli_helpers.get_novel(id_or_url)
    except ValueError:
        sys.exit(1)

    logger.info(f"Removing associated data from '{novel.title}' ({novel.id})…")

    novel_service.delete_volumes(novel)
    logger.info("Deleted volumes and chapters of novel.")

    novel_service.delete_metadata(novel)
    logger.info("Deleted metadata of novel.")

    asset_service.delete_assets_of_novel(novel)
    logger.info("Deleted asset entries of novel.")

    novel_dir = path_service.novel_data_path(novel)
    if novel_dir.exists():
        shutil.rmtree(novel_dir)
    logger.info(
        f"Deleted saved file data of novel: {{data.dir}}/{path_service.relative_to_data_dir(novel_dir)}."
    )
Example #3
0
def delete_downloaded_content(
    id_or_url: str,
    novel_service: BaseNovelService = Provide[
        Application.services.novel_service],
):
    """deletes all the downloaded content from chapters of novel"""
    try:
        novel = cli_helpers.get_novel(id_or_url)
    except ValueError:
        sys.exit(1)

    novel_service.delete_content(novel)
    logger.info(f"Deleted chapter content from '{novel.title}' ({novel.id}).")
Example #4
0
def import_metadata(
    id_or_url: str,
    metadata_url: str,
    novel_service: BaseNovelService = Provide[
        Application.services.novel_service],
):
    """import metadata from a metadata supplied into an existing novel"""
    try:
        novel = cli_helpers.get_novel(id_or_url)
    except ValueError:
        sys.exit(1)

    meta_source_gateway = cli_helpers.get_meta_source_gateway(metadata_url)
    metadata_dtos = meta_source_gateway.metadata_by_url(metadata_url)

    novel_service.update_metadata(novel, metadata_dtos)
Example #5
0
def remove_url(
    url: str,
    novel_service: BaseNovelService = Provide[
        Application.services.novel_service],
):
    """Removes the selected url from the database"""
    try:
        novel = get_novel(url)
    except ValueError:
        sys.exit(1)

    try:
        novel_service.remove_url(novel, url)
    except ValueError as e:
        logger.error(e)
        sys.exit(1)
    else:
        logger.info(f"Removed '{url}' from '{novel.title}' ({novel.id}).")
Example #6
0
def add_url(
    id_or_url: str,
    new_url: str,
    novel_service: BaseNovelService = Provide[
        Application.services.novel_service],
):
    """Deletes the novel and all its data"""
    try:
        novel = get_novel(id_or_url)
    except ValueError:
        sys.exit(1)

    try:
        novel_service.add_url(novel, new_url)
    except ValueError as e:
        logger.error(e)
        sys.exit(1)
    else:
        logger.info(f"Added '{new_url}' to '{novel.title}' ({novel.id}).")
Example #7
0
def delete_novel(
    id_or_url: str,
    novel_service: BaseNovelService = Provide[
        Application.services.novel_service],
    path_service: BasePathService = Provide[Application.services.path_service],
):
    """delete all records of novel. this includes chapters, and assets"""
    try:
        novel = cli_helpers.get_novel(id_or_url)
    except ValueError:
        sys.exit(1)

    logger.info(f"Deleting '{novel.title}' ({novel.id})…")

    novel_dir = path_service.novel_data_path(novel)
    if novel_dir.exists():
        shutil.rmtree(novel_dir)
    logger.info(
        f"Deleted data of novel: {{data.dir}}/{path_service.relative_to_data_dir(novel_dir)}."
    )

    novel_service.delete_novel(novel)
    logger.info("Deleted novel entry.")
Example #8
0
def show_info(
    id_or_url: str,
    fmt: Literal["default", "json"] = "default",
    novel_service: BaseNovelService = Provide[
        Application.services.novel_service],
):
    """print current information of novel"""
    try:
        novel = cli_helpers.get_novel(id_or_url, silent=True)
    except ValueError:
        sys.exit(1)

    chapters = novel_service.get_chapters(novel)

    data = {
        "novel": {
            "id": novel.id,
            "title": novel.title,
            "author": novel.author,
            "lang": novel.lang,
            "thumbnail": novel.thumbnail_url,
            "synopsis": novel.synopsis.splitlines(),
            "urls": [o.url for o in novel_service.get_urls(novel)],
        },
        "chapters": {
            "total": len(chapters),
            "downloaded": len([c for c in chapters if c.content]),
        },
    }

    if fmt is None or fmt == "default":
        endl = "\n"
        text = "[novel]" + endl

        def format_keyvalue(key, value):
            text = f"{key} = "

            if type(value) == str:
                text += f"'{value}'"
            else:
                text += str(value)

            text += endl
            return text

        for key, value in data["novel"].items():
            text += format_keyvalue(key, value)

        text += endl
        text += "[chapters]" + endl

        for key, value in data["chapters"].items():
            text += format_keyvalue(key, value)

    elif fmt == "json":
        text = json.dumps(data, indent=4)
    else:
        raise NSError(
            f"Provided novel information formatter is not supported: {fmt}.")

    for line in text.splitlines():
        logger.info(line)