Move old scanner scripts to `worblehat/services`

This commit is contained in:
Oystein Kristoffer Tveit 2023-05-04 16:20:15 +02:00
parent 4ac110f527
commit e04d558b3a
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
3 changed files with 9 additions and 69 deletions

View File

@ -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)

View File

@ -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.