File: pagination.py

package info (click to toggle)
python-googleapi 1.2-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,196 kB
  • ctags: 1,019
  • sloc: python: 5,827; makefile: 22; sh: 4
file content (47 lines) | stat: -rw-r--r-- 1,511 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python2.4
# -*- coding: utf-8 -*-
#
# Copyright 2010 Google Inc. All Rights Reserved.

"""Queries with paginated results against the shopping search API"""

import pprint

from apiclient.discovery import build


SHOPPING_API_VERSION = 'v1'
DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc'


def main():
  """Get and print a the entire paginated feed of public products in the United
  States.

  Pagination is controlled with the "startIndex" parameter passed to the list
  method of the resource.
  """
  client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
  resource = client.products()
  # The first request contains the information we need for the total items, and
  # page size, as well as returning the first page of results.
  request = resource.list(source='public', country='US', q=u'digital camera')
  response = request.execute()
  itemsPerPage = response['itemsPerPage']
  totalItems = response['totalItems']
  for i in range(1, totalItems, itemsPerPage):
    answer = raw_input('About to display results from %s to %s, y/(n)? ' %
                       (i, i + itemsPerPage))
    if answer.strip().lower().startswith('n'):
      # Stop if the user has had enough
      break
    else:
      # Fetch this series of results
      request = resource.list(source='public', country='US',
                              q=u'digital camera', startIndex=i)
      response = request.execute()
      pprint.pprint(response)


if __name__ == '__main__':
    main()