Skip to content

Add search book via ISBN using openlibrary.org API #5736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 1, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions web_programming/search_books_by_isbn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import requests

URL_BASE = "https://openlibrary.org"

def get_book_by_isbn(isbn: str = "0140328726") -> dict:
url = URL_BASE + "/isbn/" + isbn + ".json"
return requests.get(url, params=locals()).json()

def get_author(author_url: str = "/authors/OL34184A") -> dict:
url = URL_BASE + author_url + ".json"
return requests.get(url, params=locals()).json()

if __name__ == "__main__":

while True:
book = input("\nEnter the ISBN code to search (or CTRL+C to stop): ").strip()

if book.isnumeric() and (len(book) == 10 or len(book) == 13):

print("\nSearching...\n")

result = get_book_by_isbn(book)

keys = result.keys()
keys_title = {
'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)'
}

if result:

print("\nLoading...\n")

authors = []

for author in result['authors']:
partial_result = get_author(author['key'])
authors.append(partial_result['name'])

",".join(authors)

for key in keys_title.keys():
if (key in result.keys()):
if (key == 'first_sentence'):
print(keys_title[key], ": ", str(result[key]['value']).strip('[]'))
elif (key == 'authors'):
print(keys_title[key], ": ", str(authors).strip('[]'))
else:
print(keys_title[key], ": ", str(result[key]).strip('[]'))

else:
print("Sorry, there are no results for this search")
else:
print("Sorry, not a valid ISBN, please, inform some valid code")
break