utils: Create basic tool to generate json files.
This commit is contained in:
5434
utils/books.json
5434
utils/books.json
File diff suppressed because it is too large
Load Diff
@@ -21,6 +21,9 @@ def get_details(book_object):
|
||||
book_object["rating"] = book.find("average_rating").text
|
||||
book_object["pages"] = book.find("num_pages").text
|
||||
book_object["image_url"] = book.find("image_url").text
|
||||
book_object["description"] = book.find("description").text
|
||||
book_object["isbn"] = book.find("isbn").text
|
||||
return True
|
||||
except urllib.error.HTTPError as e:
|
||||
print(
|
||||
"Error getting book details from GoodReads for book: {}. \nGot error: ".format(
|
||||
@@ -28,6 +31,7 @@ def get_details(book_object):
|
||||
)
|
||||
)
|
||||
print(str(e.getcode()) + " " + e.msg)
|
||||
return False
|
||||
|
||||
|
||||
def get_goodread_info(library, force):
|
||||
|
||||
@@ -1,7 +1,36 @@
|
||||
import json
|
||||
import time
|
||||
|
||||
from read_file import load
|
||||
from gooodreads import get_details
|
||||
|
||||
required_fields = ["title", "author", "url", "rating", "year", "pages", "image_url", "description"]
|
||||
|
||||
def book_has_all_fields(book):
|
||||
for required_field in required_fields:
|
||||
if required_field not in existing_book:
|
||||
return False
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
library = load("../README.md", "new")
|
||||
existing_book_names_to_details = json.load(open("books.json"))
|
||||
|
||||
for category in library:
|
||||
for book in library[category]:
|
||||
if (title := book["title"]) in existing_book_names_to_details:
|
||||
existing_book = existing_book_names_to_details[title]
|
||||
if book_has_all_fields(existing_book):
|
||||
continue
|
||||
new_book = {
|
||||
"title": title,
|
||||
}
|
||||
fetched = get_details(new_book)
|
||||
if fetched:
|
||||
print(f"✅ {title} fetched")
|
||||
existing_book_names_to_details[title] = new_book
|
||||
with open("books.json", "w") as f:
|
||||
json.dump(existing_book_names_to_details, f, sort_keys=True, indent=4, separators=(',', ': '))
|
||||
else:
|
||||
print(f"❌ Error while fetching {title}")
|
||||
time.sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user