From 4ec82e5448fbe91704e75be461ca2ccf06f6ba6a Mon Sep 17 00:00:00 2001 From: Jackson Isaac Date: Mon, 19 Oct 2020 22:40:05 +0200 Subject: [PATCH] Use openlibrary api when goodreads returns nophoto --- utils/update_json_files.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/utils/update_json_files.py b/utils/update_json_files.py index 9c89d14..baaaaf0 100644 --- a/utils/update_json_files.py +++ b/utils/update_json_files.py @@ -33,6 +33,25 @@ def clean_category(category_raw): if "## " in category_raw: return category_raw[3:] +def validate_bookcover(book_details): + """ + Check if goodreads returns a nophoto + Use open library to fetch the book cover + based on ISBN + + Args: + book_details: Book info returned as json by goodreads API + + Returns: + This API checks for book cover, and returns with a valid + bookcover if nophoto found on goodreads, using openlibrary + """ + no_photo_url='https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png' + open_library_url='http://covers.openlibrary.org/b/isbn/book_isbn-M.jpg' + + if (book_details['image_url'] == no_photo_url): + book_details['image_url'] = open_library_url.replace('book_isbn', book_details['isbn']) + return book_details if __name__ == "__main__": library = load("../README.md", "new") @@ -55,6 +74,7 @@ if __name__ == "__main__": fetched = get_details(new_book) if fetched: print(f"✅ {title}") + new_book = validate_bookcover(new_book) existing_book_names_to_details[title] = new_book with open("book_name_to_details.json", "w") as f: json.dump( @@ -64,7 +84,7 @@ if __name__ == "__main__": indent=4, separators=(",", ": "), ) - + book_list = [] for _, book in existing_book_names_to_details.items(): book_list.append(book)