|
|
import logging
|
|
|
from typing import Dict, Optional, Tuple
|
|
|
|
|
|
import isbnlib
|
|
|
from rich.panel import Panel
|
|
|
from rich.table import Table
|
|
|
|
|
|
from . import utils
|
|
|
from .console import DEFAULT_PADDING, console, error_console, int_prompt, prompt
|
|
|
from .console import (DEFAULT_PADDING, console, error_console, int_prompt,
|
|
|
prompt)
|
|
|
from .style import GreenBoldText
|
|
|
|
|
|
|
|
|
def book_already_in_database(
|
|
|
isbn: str, userentity_id: int
|
|
|
) -> Tuple[bool, Optional[int]]:
|
|
|
all_books = utils.get_request(
|
|
|
f"objects/userobjects?query[]=userentity_id={userentity_id}"
|
|
|
)
|
|
|
|
|
|
for book in all_books:
|
|
|
book_metadata = utils.get_request(
|
|
|
f"userfields/userentity-books/{book['id']}", cached=True
|
|
|
)
|
|
|
|
|
|
if book_metadata["isbn"] is None:
|
|
|
continue
|
|
|
|
|
|
if isbnlib.canonical(book_metadata["isbn"]) == isbn:
|
|
|
return True, book["id"]
|
|
|
|
|
|
return False, None
|
|
|
|
|
|
|
|
|
def add_book_by_barcode(barcode: str) -> None:
|
|
|
|
|
|
canonical_isbn = isbnlib.canonical(barcode)
|
|
@@ -20,61 +43,67 @@ def add_book_by_barcode(barcode: str) ->
|
|
|
userentity = utils.get_request("objects/userentities?query[]=name=books")
|
|
|
userentity_id = userentity[0]["id"]
|
|
|
|
|
|
all_books = utils.get_request(
|
|
|
f"objects/userobjects?query[]=userentity_id={userentity_id}"
|
|
|
already_in_database, book_id = book_already_in_database(
|
|
|
canonical_isbn, userentity_id
|
|
|
)
|
|
|
|
|
|
for book in all_books:
|
|
|
if already_in_database:
|
|
|
book_metadata = utils.get_request(
|
|
|
f"userfields/userentity-books/{book['id']}", cached=True
|
|
|
f"userfields/userentity-books/{book_id}", cached=False
|
|
|
)
|
|
|
|
|
|
if book_metadata["isbn"] is None:
|
|
|
continue
|
|
|
grid = Table.grid(padding=DEFAULT_PADDING)
|
|
|
grid.add_column(justify="right", no_wrap=True)
|
|
|
grid.add_column(justify="left", style="cyan", no_wrap=True)
|
|
|
grid.add_row(GreenBoldText("Title:"), book_metadata["title"])
|
|
|
|
|
|
if isbnlib.canonical(book_metadata["isbn"]) == canonical_isbn:
|
|
|
if book_metadata["authors"] is not None:
|
|
|
grid.add_row(
|
|
|
GreenBoldText("Authors:"),
|
|
|
", ".join(book_metadata["authors"].split("\n")),
|
|
|
)
|
|
|
grid.add_row(GreenBoldText("Amount:"), book_metadata["amount"])
|
|
|
|
|
|
book_metadata = utils.get_request(
|
|
|
f"userfields/userentity-books/{book['id']}", cached=False
|
|
|
if book_metadata["categories"] is not None:
|
|
|
grid.add_row(
|
|
|
GreenBoldText("Categories:"),
|
|
|
book_metadata["categories"].replace(",", ", "),
|
|
|
)
|
|
|
|
|
|
grid = Table.grid(padding=DEFAULT_PADDING)
|
|
|
grid.add_column(justify="right", no_wrap=True)
|
|
|
grid.add_column(justify="left", style="cyan", no_wrap=True)
|
|
|
grid.add_row(GreenBoldText("Title:"), book_metadata["title"])
|
|
|
grid.add_row(GreenBoldText("Amount:"), book_metadata["amount"])
|
|
|
console.print(
|
|
|
Panel(grid, title="[green bold]Book already found[/green bold]")
|
|
|
)
|
|
|
console.print(Panel(grid, title="[green bold]Book already found[/green bold]"))
|
|
|
|
|
|
add_to_amount = bool(
|
|
|
int_prompt.ask("Add? (Enter 0 to abort)", choices=["0", "1"], default=0)
|
|
|
add_to_amount = bool(
|
|
|
int_prompt.ask("Add? (Enter 0 to abort)", choices=["0", "1"], default=0)
|
|
|
)
|
|
|
if not add_to_amount:
|
|
|
logging.debug("User aborted task!")
|
|
|
return
|
|
|
else:
|
|
|
book_metadata["amount"] = str(int(book_metadata["amount"]) + 1)
|
|
|
response = utils.put_request(
|
|
|
f"userfields/userentity-books/{book['id']}",
|
|
|
book_metadata,
|
|
|
cached=True,
|
|
|
)
|
|
|
if not add_to_amount:
|
|
|
logging.debug("User aborted task!")
|
|
|
return
|
|
|
else:
|
|
|
book_metadata["amount"] = str(int(book_metadata["amount"]) + 1)
|
|
|
response = utils.put_request(
|
|
|
f"userfields/userentity-books/{book['id']}",
|
|
|
book_metadata,
|
|
|
cached=True,
|
|
|
)
|
|
|
console.print("Successfully updated!")
|
|
|
return
|
|
|
console.print("Successfully updated!")
|
|
|
return
|
|
|
|
|
|
metadata = isbnlib.meta(canonical_isbn, "worldcat")
|
|
|
try:
|
|
|
metadata = isbnlib.meta(canonical_isbn, "worldcat")
|
|
|
if metadata["Title"] is None and metadata["Authors"] is None:
|
|
|
metadata = isbnlib.meta(canonical_isbn, "default")
|
|
|
except:
|
|
|
metadata = isbnlib.meta(canonical_isbn, "default")
|
|
|
|
|
|
grid = Table.grid(padding=DEFAULT_PADDING)
|
|
|
grid.add_column(justify="right", no_wrap=True)
|
|
|
grid.add_column(justify="left", style="cyan", no_wrap=True)
|
|
|
grid.add_column(justify="left", style="cyan", no_wrap=False)
|
|
|
grid.add_row(GreenBoldText("Title:"), metadata.get("Title", None))
|
|
|
grid.add_row(GreenBoldText("Author(s)"), ", ".join(metadata.get("Authors", None)))
|
|
|
console.print(Panel(grid, title="[green bold]Book Info[/green bold]"))
|
|
|
|
|
|
ok = bool(
|
|
|
int_prompt.ask(
|
|
|
"Is the metadata correct? (Enter 0 to abort)", choices=["0", "1"], default=0
|
|
|
"Is the metadata correct? (Enter 0 to abort)", choices=["0", "1"], default=1
|
|
|
)
|
|
|
)
|
|
|
if not ok:
|