added usage of isbnlib to data fetchers
This commit is contained in:
parent
ff3d6b3a24
commit
be535daa47
|
@ -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")
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue