- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathsearch_books_by_isbn.py
75 lines (60 loc) · 2.8 KB
/
search_books_by_isbn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Get book and author data from https://openlibrary.org
ISBN: https://en.wikipedia.org/wiki/International_Standard_Book_Number
"""
fromjsonimportJSONDecodeError# Workaround for requests.exceptions.JSONDecodeError
importrequests
defget_openlibrary_data(olid: str="isbn/0140328726") ->dict:
"""
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 that may change
# >>> get_openlibrary_data(olid='isbn/0140328726') # doctest: +ELLIPSIS
{'publishers': ['Puffin'], 'number_of_pages': 96, 'isbn_10': ['0140328726'], ...
# >>> get_openlibrary_data(olid='/authors/OL7353617A') # doctest: +ELLIPSIS
{'name': 'Adrian Brisku', 'created': {'type': '/type/datetime', ...
"""
new_olid=olid.strip().strip("/") # Remove leading/trailing whitespace & slashes
ifnew_olid.count("/") !=1:
msg=f"{olid} is not a valid Open Library olid"
raiseValueError(msg)
returnrequests.get(f"https://openlibrary.org/{new_olid}.json", timeout=10).json()
defsummarize_book(ol_book_data: dict) ->dict:
"""
Given Open Library book data, return a summary as a Python dict.
"""
desired_keys= {
"title": "Title",
"publish_date": "Publish date",
"authors": "Authors",
"number_of_pages": "Number of pages:",
"first_sentence": "First sentence",
"isbn_10": "ISBN (10)",
"isbn_13": "ISBN (13)",
}
data= {better_key: ol_book_data[key] forkey, better_keyindesired_keys.items()}
data["Authors"] = [
get_openlibrary_data(author["key"])["name"] forauthorindata["Authors"]
]
data["First sentence"] =data["First sentence"]["value"]
forkey, valueindata.items():
ifisinstance(value, list):
data[key] =", ".join(value)
returndata
if__name__=="__main__":
importdoctest
doctest.testmod()
whileTrue:
isbn=input("\nEnter the ISBN code to search (or 'quit' to stop): ").strip()
ifisbn.lower() in ("", "q", "quit", "exit", "stop"):
break
iflen(isbn) notin (10, 13) ornotisbn.isdigit():
print(f"Sorry, {isbn} is not a valid ISBN. Please, input a valid ISBN.")
continue
print(f"\nSearching Open Library for ISBN: {isbn}...\n")
try:
book_summary=summarize_book(get_openlibrary_data(f"isbn/{isbn}"))
print("\n".join(f"{key}: {value}"forkey, valueinbook_summary.items()))
exceptJSONDecodeError: # Workaround for requests.exceptions.RequestException:
print(f"Sorry, there are no results for ISBN: {isbn}.")