From be535daa476a0e2403cffafc1fd2a8ce2be332dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Kaarevik?= Date: Sun, 2 Apr 2023 03:02:37 +0200 Subject: [PATCH] added usage of isbnlib to data fetchers --- data_fetcher/batch_csv_processor.py | 66 ++++++++++++------- data_fetcher/data_fetcher.py | 6 +- ...elim_csv_generator.py => quick_scanner.py} | 0 3 files changed, 45 insertions(+), 27 deletions(-) rename data_fetcher/{prelim_csv_generator.py => quick_scanner.py} (100%) diff --git a/data_fetcher/batch_csv_processor.py b/data_fetcher/batch_csv_processor.py index d5c21e5..ed023df 100644 --- a/data_fetcher/batch_csv_processor.py +++ b/data_fetcher/batch_csv_processor.py @@ -1,39 +1,57 @@ +import json +import isbnlib from data_fetcher import get_from_api -import json -from isbnlib import meta +def make_isbnlib_comliant(meta): + if meta: + book_info = { + "isbn": meta["ISBN-13"], + "authors": meta["Authors"], + "title": meta["Title"], + "publish_date": meta["Year"], + "number_of_pages": None, + "languages": [meta["Language"]], + } + + return book_info + else: + return meta + if __name__ == "__main__": - # fname = input("File to operate on: ") - fname = "./bokhyller/arbeidsrom_smal_hylle_5.csv" + fname = input("File to operate on: ") + # fname = "./bokhyller/arbeidsrom_smal_hylle_5.csv" + # fname = ".\\bokhyller\\arbeidsrom_smal_hylle_5" - with open(fname, "r") as f: + with open(fname + ".csv", "r") as f: fields = f.readline().strip("\n").split(", ") - books = [] - for line in f: - a = line.strip("\n").split(", ") - d = {} + values = line.strip("\n").split(", ") + book = {} for i in range(len(fields)): - d[fields[i]] = a[i] - books.append(d) + book[fields[i]] = values[i] + books.append(book) - # for b in books: - # print(b) book_infos = [] - # for book in books: - # bi = get_from_api(book["isbn"]) - # if type(bi) is dict: - # bi["bookcase"] = book["bookcase"] - # bi["shelf"] = book["shelf"] - # book_infos.append(json.dumps(bi)) for book in books: - bi = json.dumps(meta(book["isbn"])) - print(bi) - book_infos.append(bi) + print(f"Attempting to fetch information for: {book['isbn']}") + book_info = get_from_api(book["isbn"]) - # for bi in book_infos: - # print(bi) + if type(book_info) is not dict: + try: + book_info = make_isbnlib_comliant(isbnlib.meta(book["isbn"])) + except: + print(f"isbnlib failed for book: {book['isbn']}") + if not book_info: + book_info = book + + book_info["bookcase"] = book["bookcase"] + book_info["shelf"] = book["shelf"] + book_infos.append(json.dumps(book_info)) + + with open(fname + ".dat", "w") as f: + for book_info in book_infos: + f.write(book_info + "\n") diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index a510b75..90e3ddb 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -25,7 +25,7 @@ def get_isbn(): else: return None -def get_from_api(isbn) -> dict: +def get_from_api(isbn): try: json_input = json.loads(requests.get("https://openlibrary.org/isbn/"+str(isbn)+".json").text) except: @@ -49,8 +49,8 @@ def get_from_api(isbn) -> dict: "isbn": isbn, "authors": authors, "title": title, - "publishDate": publish_date, - "numberOfPages": number_of_pages, + "publish_date": publish_date, + "number_of_pages": number_of_pages, "languages": languages, } diff --git a/data_fetcher/prelim_csv_generator.py b/data_fetcher/quick_scanner.py similarity index 100% rename from data_fetcher/prelim_csv_generator.py rename to data_fetcher/quick_scanner.py