From 73cc5f0d842104c56d9607fb62651100dbf79947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Kaarevik?= Date: Sun, 2 Apr 2023 00:15:14 +0200 Subject: [PATCH] added some scripts operating on csvs --- data_fetcher/batch_csv_processor.py | 39 ++++++++++++++++++ data_fetcher/prelim_csv_generator.py | 60 ++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 data_fetcher/batch_csv_processor.py create mode 100644 data_fetcher/prelim_csv_generator.py diff --git a/data_fetcher/batch_csv_processor.py b/data_fetcher/batch_csv_processor.py new file mode 100644 index 0000000..d5c21e5 --- /dev/null +++ b/data_fetcher/batch_csv_processor.py @@ -0,0 +1,39 @@ +from data_fetcher import get_from_api + +import json +from isbnlib import meta + +if __name__ == "__main__": + # fname = input("File to operate on: ") + fname = "./bokhyller/arbeidsrom_smal_hylle_5.csv" + + with open(fname, "r") as f: + fields = f.readline().strip("\n").split(", ") + + books = [] + + for line in f: + a = line.strip("\n").split(", ") + d = {} + for i in range(len(fields)): + d[fields[i]] = a[i] + books.append(d) + + # 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) + + # for bi in book_infos: + # print(bi) + diff --git a/data_fetcher/prelim_csv_generator.py b/data_fetcher/prelim_csv_generator.py new file mode 100644 index 0000000..1caf594 --- /dev/null +++ b/data_fetcher/prelim_csv_generator.py @@ -0,0 +1,60 @@ +""" +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