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
|
from data_fetcher import get_from_api
|
||||||
|
|
||||||
import json
|
def make_isbnlib_comliant(meta):
|
||||||
from isbnlib import 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__":
|
if __name__ == "__main__":
|
||||||
# fname = input("File to operate on: ")
|
fname = input("File to operate on: ")
|
||||||
fname = "./bokhyller/arbeidsrom_smal_hylle_5.csv"
|
# 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(", ")
|
fields = f.readline().strip("\n").split(", ")
|
||||||
|
|
||||||
books = []
|
books = []
|
||||||
|
|
||||||
for line in f:
|
for line in f:
|
||||||
a = line.strip("\n").split(", ")
|
values = line.strip("\n").split(", ")
|
||||||
d = {}
|
book = {}
|
||||||
for i in range(len(fields)):
|
for i in range(len(fields)):
|
||||||
d[fields[i]] = a[i]
|
book[fields[i]] = values[i]
|
||||||
books.append(d)
|
books.append(book)
|
||||||
|
|
||||||
# for b in books:
|
|
||||||
# print(b)
|
|
||||||
book_infos = []
|
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:
|
for book in books:
|
||||||
bi = json.dumps(meta(book["isbn"]))
|
print(f"Attempting to fetch information for: {book['isbn']}")
|
||||||
print(bi)
|
book_info = get_from_api(book["isbn"])
|
||||||
book_infos.append(bi)
|
|
||||||
|
|
||||||
# for bi in book_infos:
|
if type(book_info) is not dict:
|
||||||
# print(bi)
|
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:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_from_api(isbn) -> dict:
|
def get_from_api(isbn):
|
||||||
try:
|
try:
|
||||||
json_input = json.loads(requests.get("https://openlibrary.org/isbn/"+str(isbn)+".json").text)
|
json_input = json.loads(requests.get("https://openlibrary.org/isbn/"+str(isbn)+".json").text)
|
||||||
except:
|
except:
|
||||||
|
@ -49,8 +49,8 @@ def get_from_api(isbn) -> dict:
|
||||||
"isbn": isbn,
|
"isbn": isbn,
|
||||||
"authors": authors,
|
"authors": authors,
|
||||||
"title": title,
|
"title": title,
|
||||||
"publishDate": publish_date,
|
"publish_date": publish_date,
|
||||||
"numberOfPages": number_of_pages,
|
"number_of_pages": number_of_pages,
|
||||||
"languages": languages,
|
"languages": languages,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue