From e04d558b3a82f5dfecd5cac47b103e20e9305bf3 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Thu, 4 May 2023 16:20:15 +0200 Subject: [PATCH] Move old scanner scripts to `worblehat/services` --- data_fetcher/quick_scanner.py | 60 ------------------- .../services}/batch_csv_processor.py | 0 .../services}/data_fetcher.py | 18 +++--- 3 files changed, 9 insertions(+), 69 deletions(-) delete mode 100644 data_fetcher/quick_scanner.py rename {data_fetcher => worblehat/services}/batch_csv_processor.py (100%) rename {data_fetcher => worblehat/services}/data_fetcher.py (98%) diff --git a/data_fetcher/quick_scanner.py b/data_fetcher/quick_scanner.py deleted file mode 100644 index 1caf594..0000000 --- a/data_fetcher/quick_scanner.py +++ /dev/null @@ -1,60 +0,0 @@ -""" -This script is part of the initial database construction for Worblehat. -The idea is that the script will: -* Prompt the user for which bookcase and shelf we're scanning -* Scan ISBN's until the user says we're done -* Also take a note for each ISBN -* Dump a csv with the columns: ISBN, bookcase, shelf, note -""" - -from isbnlib import is_isbn10, is_isbn13 - -def validate_isbn(isbn): - if len(isbn) == 10: - return is_isbn10(isbn) - elif len(isbn) == 13: - return is_isbn13(isbn) - elif len(isbn) == 1: - return isbn - else: - return False - -def write_csv(bookcase, shelf, isbns, notes, fname="isbns"): - f = open(fname + "_" + bookcase + "_" + shelf + ".csv", "w") - f.write("isbn, note, bookcase, shelf\n") - - for isbn, note in zip(isbns, notes): - f.write(f"{isbn}, {note}, {bookcase}, {shelf}\n") - - f.close() - -if __name__ == "__main__": - bookcase = input("Bookcase: ") - shelf = input("Shelf: ") - - should_get_isbn = True - isbns = [] - notes = [] - i = 0 - print("Input q as ISBN to quit.") - while should_get_isbn: - i += 1 - has_valid_isbn = False - while not has_valid_isbn: - isbn = input(f"ISBN no. {i}: ") - has_valid_isbn = validate_isbn(isbn) - if not has_valid_isbn: - print("Invalid ISBN, trying again.") - - note = input(f"Note for ISBN {isbn}: ") - - if len(isbn) > 1: - isbns.append(isbn) - notes.append(note) - else: - should_get_isbn = False - - print(isbns) - print(notes) - - write_csv(bookcase, shelf, isbns, notes) \ No newline at end of file diff --git a/data_fetcher/batch_csv_processor.py b/worblehat/services/batch_csv_processor.py similarity index 100% rename from data_fetcher/batch_csv_processor.py rename to worblehat/services/batch_csv_processor.py diff --git a/data_fetcher/data_fetcher.py b/worblehat/services/data_fetcher.py similarity index 98% rename from data_fetcher/data_fetcher.py rename to worblehat/services/data_fetcher.py index 90e3ddb..99e14a7 100644 --- a/data_fetcher/data_fetcher.py +++ b/worblehat/services/data_fetcher.py @@ -30,21 +30,21 @@ def get_from_api(isbn): json_input = json.loads(requests.get("https://openlibrary.org/isbn/"+str(isbn)+".json").text) except: return f"Error fetching data for: {isbn}" # TODO: add more databases for fetching info from - + try: authors = json_input.get("authors") for i in range(len(authors)): authors[i] = json.loads(requests.get("https://openlibrary.org"+str(authors[i].get("key"))+".json").text).get("name") #henter navn fra api - + authors = list(set(authors)) title = json_input.get("title") publish_date = json_input.get("publish_date") number_of_pages = json_input.get("number_of_pages") languages = json_input.get("languages") - + for i in range(len(languages)): languages[i] = json.loads(requests.get("https://openlibrary.org"+str(languages[i].get("key"))+".json").text).get("name") - + book_data = { "isbn": isbn, "authors": authors, @@ -53,9 +53,9 @@ def get_from_api(isbn): "number_of_pages": number_of_pages, "languages": languages, } - + return book_data - + except: return f"Error processing data for: {isbn}" @@ -73,7 +73,7 @@ def validate_book_info_with_user(book_info) -> bool: Input: * book_info: dict holding fields of book information - + Returns: * bool: True if book is now valid, false if something went wrong """ @@ -99,13 +99,13 @@ def validate_book_info_with_user(book_info) -> bool: is_corrected = True else: print("No valid option supplied.") - + return True # Book has been corrected and is (presumably) valid elif answer == "y": return True # Book information is valid else: return False # Something went wrong - + def add_book_location(book_info) -> None: """ Prompts the user for which bookcase and shelf the book came from and adds them to the book_info dictionary.