Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
128 lines (99 sloc) 3.76 KB
# Copyright 2015 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from bookshelf import get_model, storage
from flask import current_app
from import pubsub
import psq
import requests
# [START get_books_queue]
publisher_client = pubsub.PublisherClient()
subscriber_client = pubsub.SubscriberClient()
def get_books_queue():
project = current_app.config['PROJECT_ID']
# Create a queue specifically for processing books and pass in the
# Flask application context. This ensures that tasks will have access
# to any extensions / configuration specified to the app, such as
# models.
return psq.Queue(
publisher_client, subscriber_client, project,
'books', extra_context=current_app.app_context)
# [END get_books_queue]
# [START process_book]
def process_book(book_id):
Handles an individual Bookshelf message by looking it up in the
model, querying the Google Books API, and updating the book in the model
with the info found in the Books API.
model = get_model()
book =
if not book:
logging.warn("Could not find book with id {}".format(book_id))
if 'title' not in book:
logging.warn("Can't process book id {} without a title."
return"Looking up book with title {}".format(book[
new_book_data = query_books_api(book['title'])
if not new_book_data:
book['title'] = new_book_data.get('title')
book['author'] = ', '.join(new_book_data.get('authors', []))
book['publishedDate'] = new_book_data.get('publishedDate')
book['description'] = new_book_data.get('description')
# If the new book data has thumbnail images and there isn't currently a
# thumbnail for the book, then copy the image to cloud storage and update
# the book data.
if not book.get('imageUrl') and 'imageLinks' in new_book_data:
new_img_src = new_book_data['imageLinks']['smallThumbnail']
book['imageUrl'] = download_and_upload_image(
model.update(book, book_id)
# [END process_book]
# [START query_books_api]
def query_books_api(title):
Queries the Google Books API to find detailed information about the book
with the given title.
r = requests.get('', params={
'q': title
data = r.json()['items'][0]['volumeInfo']
return data
except KeyError:"No book found for title {}".format(title))
return None
except ValueError:"Unexpected response from books API: {}".format(r))
return None
# [END queue_books_api]
def download_and_upload_image(src, dst_filename):
Downloads an image file and then uploads it to Google Cloud Storage,
essentially re-hosting the image in GCS. Returns the public URL of the
image in GCS
r = requests.get(src)
if not r.status_code == 200:
return storage.upload_file(
r.headers.get('content-type', 'image/jpeg'))
You can’t perform that action at this time.