added some scripts operating on csvs
This commit is contained in:
parent
45d5a5e3bd
commit
73cc5f0d84
|
@ -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)
|
||||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue