From 1d6374010dd634e38ed89c86f34ef1962d61cefe Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Wed, 6 Mar 2024 22:34:20 +0100 Subject: [PATCH] fix book work importing --- scripts/add_item.py | 18 ++++++++++++------ scripts/process_logs.py | 2 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/add_item.py b/scripts/add_item.py index fa42ac9..49c3229 100644 --- a/scripts/add_item.py +++ b/scripts/add_item.py @@ -64,7 +64,7 @@ def return_if_exists(item_id, media_type, log) -> dict | None: existing_items = [ log_item for log_item in log_items - if "id" in log_item and log_item["id"] == int(item_id) + if "id" in log_item and log_item["id"] == item_id ] if len(existing_items) > 0: logger.info(f"Found item in '{log}'") @@ -83,7 +83,7 @@ def delete_existing(item_id, media_type, log) -> None: log_items = [ log_item for log_item in log_items - if "id" not in log_item or ("id" in log_item and log_item["id"] != int(item_id)) + if "id" not in log_item or ("id" in log_item and log_item["id"] != item_id) ] if len(log_items) < (old_len - 1): raise Exception("More than one deletion made, discarding…") @@ -355,7 +355,7 @@ def import_from_openlibrary_by_ol_key(key) -> dict: if mode in ["works", "authors"]: if "authors" == mode: matched_cached_authors = [ - aut for aut in cached_authors if aut["id"] == ol_id + aut for aut in cached_authors if aut["ol_id"] == ol_id ] if len(matched_cached_authors) == 1: logging.info( @@ -388,7 +388,7 @@ def import_from_openlibrary_by_ol_key(key) -> dict: if "authors" == mode: author = {"ol_id": ol_id, "name": item["name"]} - + print(author) if "personal_name" in item: if item["name"] != item["personal_name"]: author["personal_name"] = item["personal_name"] @@ -404,12 +404,18 @@ def import_from_openlibrary_by_ol_key(key) -> dict: return author if "works" == mode: - work = {"ol_id": ol_id, "title": item["title"]} + work = {"ol_id": ol_id, "title": item["title"], "authors": []} + + if "authors" in item: + for author in item["authors"]: + work["authors"].append(import_from_openlibrary_by_ol_key(author["author"]["key"])) for result_key in ["first_publish_date", "subjects"]: if result_key in item: work[result_key] = item[result_key] + work["date_added"] = datetime.today().strftime("%Y-%m-%d") + return work else: @@ -572,7 +578,7 @@ def main() -> None: while re.search("[0-9]+", item_id) is None: item_id = input("Enter TMDB ID: ") - add_item_to_log(re.search("(tt)?[0-9]+", item_id)[0], media_type, log) + add_item_to_log(re.search("(OL|tt)?[0-9]+[WMA]?", item_id)[0], media_type, log) except Exception: logger.exception("Exception occurred") diff --git a/scripts/process_logs.py b/scripts/process_logs.py index 94f6438..69110ab 100644 --- a/scripts/process_logs.py +++ b/scripts/process_logs.py @@ -141,6 +141,8 @@ def process_log(media_type, log) -> None: try: new_log_item = import_by_id(ol_work_id[0], media_type, log) except: + new_log_item = item + item["skip"] = True logger.info("Skipping…") elif "ISBN13" in item and item["ISBN13"] != "" and item["ISBN13"] is not None: