MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / get_openlibrary_data

Function get_openlibrary_data

web_programming/search_books_by_isbn.py:19–37  ·  view source on GitHub ↗

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")

Source from the content-addressed store, hash-verified

17
18
19def 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
40def summarize_book(ol_book_data: dict) -> dict:

Callers 2

summarize_bookFunction · 0.85

Calls 3

countMethod · 0.80
jsonMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected