• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12
13SHOPPING_API_VERSION = 'v1'
14DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc'
15
16
17def main():
18  """Get and print a the entire paginated feed of public products in the United
19  States.
20
21  Pagination is controlled with the "startIndex" parameter passed to the list
22  method of the resource.
23  """
24  client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
25  resource = client.products()
26  # The first request contains the information we need for the total items, and
27  # page size, as well as returning the first page of results.
28  request = resource.list(source='public', country='US', q=u'digital camera')
29  response = request.execute()
30  itemsPerPage = response['itemsPerPage']
31  totalItems = response['totalItems']
32  for i in range(1, totalItems, itemsPerPage):
33    answer = raw_input('About to display results from %s to %s, y/(n)? ' %
34                       (i, i + itemsPerPage))
35    if answer.strip().lower().startswith('n'):
36      # Stop if the user has had enough
37      break
38    else:
39      # Fetch this series of results
40      request = resource.list(source='public', country='US',
41                              q=u'digital camera', startIndex=i)
42      response = request.execute()
43      pprint.pprint(response)
44
45
46if __name__ == '__main__':
47    main()
48