1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# 4# Copyright 2014 Google Inc. All Rights Reserved. 5 6"""Queries with paginated results against the shopping search API""" 7 8import pprint 9 10from googleapiclient.discovery import build 11 12try: 13 input = raw_input 14except NameError: 15 pass 16 17 18SHOPPING_API_VERSION = 'v1' 19DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc' 20 21 22def main(): 23 """Get and print a the entire paginated feed of public products in the United 24 States. 25 26 Pagination is controlled with the "startIndex" parameter passed to the list 27 method of the resource. 28 """ 29 client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY) 30 resource = client.products() 31 # The first request contains the information we need for the total items, and 32 # page size, as well as returning the first page of results. 33 request = resource.list(source='public', country='US', q=u'digital camera') 34 response = request.execute() 35 itemsPerPage = response['itemsPerPage'] 36 totalItems = response['totalItems'] 37 for i in range(1, totalItems, itemsPerPage): 38 answer = input('About to display results from %s to %s, y/(n)? ' % 39 (i, i + itemsPerPage)) 40 if answer.strip().lower().startswith('n'): 41 # Stop if the user has had enough 42 break 43 else: 44 # Fetch this series of results 45 request = resource.list(source='public', country='US', 46 q=u'digital camera', startIndex=i) 47 response = request.execute() 48 pprint.pprint(response) 49 50 51if __name__ == '__main__': 52 main() 53