Given an 'isbn/0140328726', return book data from Open Library as a Python dict. Given an '/authors/OL34184A', return authors data as a Python dict. This code must work for olids with or without a leading slash ('/'). # Comment out doctests if they take too long or have results tha
(olid: str = "isbn/0140328726")
| 17 | |
| 18 | |
| 19 | def get_openlibrary_data(olid: str = "isbn/0140328726") -> dict: |
| 20 | """ |
| 21 | Given an 'isbn/0140328726', return book data from Open Library as a Python dict. |
| 22 | Given an '/authors/OL34184A', return authors data as a Python dict. |
| 23 | This code must work for olids with or without a leading slash ('/'). |
| 24 | |
| 25 | # Comment out doctests if they take too long or have results that may change |
| 26 | # >>> get_openlibrary_data(olid='isbn/0140328726') # doctest: +ELLIPSIS |
| 27 | {'publishers': ['Puffin'], 'number_of_pages': 96, 'isbn_10': ['0140328726'], ... |
| 28 | # >>> get_openlibrary_data(olid='/authors/OL7353617A') # doctest: +ELLIPSIS |
| 29 | {'name': 'Adrian Brisku', 'created': {'type': '/type/datetime', ... |
| 30 | """ |
| 31 | new_olid = olid.strip().strip("/") # Remove leading/trailing whitespace & slashes |
| 32 | if new_olid.count("/") != 1: |
| 33 | msg = f"{olid} is not a valid Open Library olid" |
| 34 | raise ValueError(msg) |
| 35 | return httpx.get( |
| 36 | f"https://openlibrary.org/{new_olid}.json", timeout=10, follow_redirects=True |
| 37 | ).json() |
| 38 | |
| 39 | |
| 40 | def summarize_book(ol_book_data: dict) -> dict: |
no test coverage detected