Exemple #1
0
def store_specified_isbn(target: Path, isbn: str, honto: HontoSearchCliant,
                         ehon: EhonSearchCliant, config: dict,
                         logger: MyLogger, db: DatabaseCliant) -> None:
    """isbnを元に元情報を上書きし、移動する

    Args:
        target (Path): 対象書籍のpath
        isbn (str): isbn
        honto (HontoSearchCliant): Hontoの検索クライアント
        ehon (EhonSearchCliant): E-honの検索クライアント
        config (dict): configのdict
        logger (MyLogger): logger
        db (DatabaseCliant): データベースのクライアント

    Raises:
        HontoDoesNotHaveDataError: Hontoがその書籍のページを持っていないときのエラー
    """
    try:
        info = fetch_book_info_from_isbn(isbn, honto, ehon)
    except HontoDoesNotHaveDataError as e:
        send_err_dir(target, Path(config["output_dir"]))
        logger.write("ERROR", str(e))
        raise HontoDoesNotHaveDataError(e)
    else:
        dst = construct_dst(config["output_dir"], info)
        dst.parent.mkdir(parents=True, exist_ok=True)
        shutil.move(str(target), dst)
        logger.write("SUCCESS", str(dst))

        # データベースに情報を追加
        fetch_result = {
            "title": info["title"],
            "isbn": info["isbn"],
            "authors": "\t".join(info["authors"] or [""]),
            "publishers": info["publisher"],
            "categories": info["category"],
            "destination": str(dst)
        }

        db.store(fetch_result)
Exemple #2
0
def completion_no_isbn(source_csv_path: str) -> None:
    """手作業でisbnや移動先を書いたcsvを元に補完作業をする

    Args:
        source_csv_path (str): ソースとなるcsv
    """
    logger = MyLogger()
    honto = HontoSearchCliant()
    ehon = EhonSearchCliant()

    with open(Path(__file__).resolve().parents[1] / "config.yml") as f:
        config = yaml.safe_load(f)
        Path(config["output_dir"]).resolve().mkdir(exist_ok=True, parents=True)

    db_cliant = DatabaseCliant(Path(config["database_path"]))

    csv_path = Path(source_csv_path).resolve()
    with open(csv_path) as f:
        reader = csv.reader(f)
        for row in reader:
            book_path = Path(row[0]).resolve()
            neemock = str(row[1])
            if re.fullmatch(r"[0-9-]+", neemock):
                isbn = neemock
                try:
                    store_specified_isbn(book_path, isbn, honto, ehon, config,
                                         logger, db_cliant)
                except HontoDoesNotHaveDataError as e:
                    send_err_dir(book_path, Path(config["output_dir"]))
                    logger.write("ERROR", str(e))
            else:
                dst = Path(neemock)
                db_cliant.update_dst(book_path, dst)
                shutil.move(str(book_path), str(dst / book_path.name))

    db_cliant.close()
Exemple #3
0
def main():
    logger = MyLogger()
    profect_dir = Path(__file__).resolve().parents[1]
    config_path = profect_dir / "config.yml"

    if not config_path.exists():
        generate_config_file(config_path)

    with open(config_path, "r") as f:
        config = yaml.safe_load(f)

    Path(config["output_dir"]).mkdir(exist_ok=True, parents=True)
    db_cliant = DatabaseCliant(Path(config["database_path"]))
    honto_cliant = HontoSearchCliant()
    ehon_cliant = EhonSearchCliant()

    # input_dir内のPDFに対して処理をする
    for pdf_file in Path(config["input_dir"]).glob("**/*.pdf"):
        try:
            book_info = fetch_book_info_from_pdf(pdf_file, honto_cliant, ehon_cliant)
        except NotFoundIsbnError as e:
            send_err_dir(pdf_file, Path(config["output_dir"]))
            logger.write("ERROR", str(e))
            continue
        except HontoDoesNotHaveDataError as e:
            send_err_dir(pdf_file, Path(config["output_dir"]))
            logger.write("ERROR", str(e))
            continue

        dst = construct_dst(config["output_dir"], book_info)
        dst.parent.mkdir(parents=True, exist_ok=True)
        shutil.move(str(pdf_file), dst)
        logger.write("SUCCESS", dst)

        # データベースに情報を追加
        fetch_result = {
            "title": book_info["title"],
            "isbn": book_info["isbn"],
            "authors": "\t".join(book_info["authors"] or [""]),
            "publishers": book_info["publisher"],
            "categories": book_info["category"],
            "destination": str(dst)
        }

        db_cliant.store(fetch_result)
    db_cliant.close()