I have a list of dictionaries and I'm filling it out as I search a JSON url. The problem is that JSON (provided by the Google Books API) is not always complete. This is a search for books and from what I saw, all of them have id, title and authors, but not all of them have imageLinks. Here's a JSON link as an example: Search for Harry Potter.
Note that it always returns 10 results, in this example there are 10 IDs, 10 titles, 10 authors, but only 4 imageLinks.
@app.route('/search', methods=["GET", "POST"])
@login_required
def search():
if request.method == "POST":
while True:
try:
seek = request.form.get("seek")
url = f'https://www.googleapis.com/books/v1/volumes?q={seek}'
response = requests.get(url)
response.raise_for_status()
search = response.json()
seek = search['items']
infobooks = []
for i in range(len(seek)):
infobooks.append({
"book_id": seek[i]['id'],
"thumbnail": seek[i]['volumeInfo']['imageLinks']['thumbnail'],
"title": seek[i]['volumeInfo']['title'],
"authors": seek[i]['volumeInfo']['authors']
})
return render_template("index.html", infobooks=infobooks)
except (requests.RequestException, KeyError, TypeError, ValueError):
continue
else:
return render_template("index.html")
The method I used and that I'm demonstrating above, I can find 10 imageLinks (thumbnails) but it takes a long time! Anyone have any suggestions for this request not take so long? Or some way I can insert a "Book Without Cover" image when I can't find an imageLink? (not what I would like, but it's better than having to wait for the results)