Skip to content

Instantly share code, notes, and snippets.

@miladkiaee
Forked from AO8/isbn_lookup.py
Last active February 25, 2023 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miladkiaee/9de9eb04c8f9f89d65385f1000d95efa to your computer and use it in GitHub Desktop.
Save miladkiaee/9de9eb04c8f9f89d65385f1000d95efa to your computer and use it in GitHub Desktop.
A simple ISBN lookup that uses Python and the Google Books API to display basic information about a book.
import urllib.request
import json
## import textwrap
import sqlite3
database = sqlite3.connect(':memory:')
cur = database.cursor()
cur.execute('''CREATE TABLE minilib
(i INTEGER,isbn INTEGER, title TEXT, language text)''')
N=2
index = 0
while True:
index = index + 1
base_api_link = "https://www.googleapis.com/books/v1/volumes?q=isbn:"
user_input = input("Enter ISBN: ").strip()
isbn = user_input
with urllib.request.urlopen(base_api_link + user_input) as f:
text = f.read()
### check first to see if user-input exist in templib
a = cur.execute('''SELECT * FROM minilib WHERE isbn=?''',[user_input]).fetchall()
if len(a) != 0:
print("\nBook with this ISBN exist in our minilibrary: ", a, "\n")
else:
print("\nBook with this ISBN does not exist in our minilibrary, New search required!", "\n")
print("\n searching ... \n")
decoded_text = text.decode("utf-8")
obj = json.loads(decoded_text) # deserializes decoded_text to a Python object
volume_info = obj["items"][0]
authors = obj["items"][0]["volumeInfo"]["authors"]
title = volume_info["volumeInfo"]["title"]
language = volume_info["volumeInfo"]["language"]
r = [index, user_input, title, language]
cur.execute('''INSERT INTO minilib (i, isbn, title, language)
VALUES (?,?,?,?)''', r)
print("Found: ISBN", isbn, ", Title: ", title, ", Language: ", language)
if index >= N:
cur.execute('''DELETE FROM templib WHERE rowid NOT IN
(SELECT rowid FROM minilib ORDER BY rowid LIMIT ?)''', [N])
database.commit()
status_update = input("\nEnter another ISBN? y or n: ").lower().strip()
if status_update == "n":
print("\nThank you! Have a nice day.")
break
print("\n Our minilibrary so far has following books: \n")
for row in cur.execute('SELECT * FROM minilib ORDER BY i'):
print(row)
database.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment