Samples for Google Search API for Shopping
from Ali Afshar.
Index: samples/searchforshopping/basic.py
===================================================================
new file mode 100644
diff --git a/samples/searchforshopping/basic.py b/samples/searchforshopping/basic.py
new file mode 100644
index 0000000..96feeb6
--- /dev/null
+++ b/samples/searchforshopping/basic.py
@@ -0,0 +1,32 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Basic query against the public shopping search API"""
+
+import pprint
+
+from apiclient.discovery import build
+
+
+SHOPPING_API_VERSION = 'v1'
+DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc'
+
+
+def main():
+ """Get and print a feed of all public products available in the
+ United States.
+
+ Note: The source and country arguments are required to pass to the list
+ method.
+ """
+ client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
+ resource = client.products()
+ request = resource.list(source='public', country='US')
+ response = request.execute()
+ pprint.pprint(response)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/samples/searchforshopping/crowding.py b/samples/searchforshopping/crowding.py
new file mode 100644
index 0000000..bf1114e
--- /dev/null
+++ b/samples/searchforshopping/crowding.py
@@ -0,0 +1,48 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Query with grouping 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 feed of public products in the United States mathing a text
+ search query for 'digital camera' and grouped by the 8 top brands.
+
+ The list method of the resource should be called with the "crowdBy" parameter.
+ Each parameter should be designed as <attribute>:<occurence>, where
+ <occurrence> is the number of that <attribute> that will be used. For
+ example, to crowd by the 5 top brands, the parameter would be "brand:5". The
+ possible rules for crowding are currently:
+
+ account_id:<occurrence> (eg account_id:5)
+ brand:<occurrence> (eg brand:5)
+ condition:<occurrence> (eg condition:3)
+ gtin:<occurrence> (eg gtin:10)
+ price:<occurrence> (eg price:10)
+
+ Multiple crowding rules should be specified by separating them with a comma,
+ for example to crowd by the top 5 brands and then condition of those items,
+ the parameter should be crowdBy="brand:5,condition:3"
+ """
+ client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
+ resource = client.products()
+ # The crowdBy parameter to the list method causes the results to be grouped,
+ # in this case by the top 8 brands.
+ request = resource.list(source='public', country='US', q=u'digital camera',
+ crowdBy='brand:8')
+ response = request.execute()
+ pprint.pprint(response)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/samples/searchforshopping/fulltextsearch.py b/samples/searchforshopping/fulltextsearch.py
new file mode 100644
index 0000000..9a5fa5e
--- /dev/null
+++ b/samples/searchforshopping/fulltextsearch.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Full text search query 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 feed of all public products matching the search query
+ "digital camera".
+
+ This is achieved by using the q query parameter to the list method.
+
+ The "|" operator can be used to search for alternative search terms, for
+ example: q = 'banana|apple' will search for bananas or apples.
+
+ Search phrases such as those containing spaces can be specified by surrounding
+ them with double quotes, for example q='"mp3 player"'. This can be useful when
+ combining with the "|" operator such as q = '"mp3 player"|ipod'.
+ """
+ client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
+ resource = client.products()
+ # Note the 'q' parameter, which will contain the value of the search query
+ request = resource.list(source='public', country='US', q=u'digital camera')
+ response = request.execute()
+ pprint.pprint(response)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/samples/searchforshopping/histograms.py b/samples/searchforshopping/histograms.py
new file mode 100644
index 0000000..6d198ff
--- /dev/null
+++ b/samples/searchforshopping/histograms.py
@@ -0,0 +1,57 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Query with ranked results against the shopping search API"""
+
+from apiclient.discovery import build
+
+
+SHOPPING_API_VERSION = 'v1'
+DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc'
+
+
+def main():
+ """Get and print a histogram of the top 15 brand distribution for a search
+ query.
+
+ Histograms are created by using the "Facets" functionality of the API. A Facet
+ is a view of a certain property of products, containing a number of buckets,
+ one for each value of that property. Or concretely, for a parameter such as
+ "brand" of a product, the facets would include a facet for brand, which would
+ contain a number of buckets, one for each brand returned in the result.
+
+ A bucket contains either a value and a count, or a value and a range. In the
+ simple case of a value and a count for our example of the "brand" property,
+ the value would be the brand name, eg "sony" and the count would be the number
+ of results in the search.
+ """
+ client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
+ resource = client.products()
+ request = resource.list(source='public', country='US', q=u'digital camera',
+ facets_include='brand:15', facets_enabled=True)
+ response = request.execute()
+
+ # Pick the first and only facet for this query
+ facet = response['facets'][0]
+
+ print '\n\tHistogram for "%s":\n' % facet['property']
+
+ labels = []
+ values = []
+
+ for bucket in facet['buckets']:
+ labels.append(bucket['value'].rjust(20))
+ values.append(bucket['count'])
+
+ weighting = 50.0 / max(values)
+
+ for label, value in zip(labels, values):
+ print label, '#' * int(weighting * value), '(%s)' % value
+
+ print
+
+
+if __name__ == '__main__':
+ main()
diff --git a/samples/searchforshopping/pagination.py b/samples/searchforshopping/pagination.py
new file mode 100644
index 0000000..c938247
--- /dev/null
+++ b/samples/searchforshopping/pagination.py
@@ -0,0 +1,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()
diff --git a/samples/searchforshopping/ranking.py b/samples/searchforshopping/ranking.py
new file mode 100644
index 0000000..bf6dc0f
--- /dev/null
+++ b/samples/searchforshopping/ranking.py
@@ -0,0 +1,45 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Query with ranked 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 feed of public products in the United States mathing a text
+ search query for 'digital camera' ranked by ascending price.
+
+ The list method for the resource should be called with the "rankBy" parameter.
+ 5 parameters to rankBy are currently supported by the API. They are:
+
+ "relevancy"
+ "modificationTime:ascending"
+ "modificationTime:descending"
+ "price:ascending"
+ "price:descending"
+
+ These parameters can be combined
+
+ The default ranking is "relevancy" if the rankBy parameter is omitted.
+ """
+ client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
+ resource = client.products()
+ # The rankBy parameter to the list method causes results to be ranked, in this
+ # case by ascending price.
+ request = resource.list(source='public', country='US', q=u'digital camera',
+ rankBy='price:ascending')
+ response = request.execute()
+ pprint.pprint(response)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/samples/searchforshopping/restricting.py b/samples/searchforshopping/restricting.py
new file mode 100644
index 0000000..07a4474
--- /dev/null
+++ b/samples/searchforshopping/restricting.py
@@ -0,0 +1,43 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Query that is restricted by a parameter against the public shopping search API"""
+
+import pprint
+
+from apiclient.discovery import build
+
+
+SHOPPING_API_VERSION = 'v1'
+DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc'
+
+
+def main():
+ """Get and print a feed of all public products matching the search query
+ "digital camera", that are created by "Canon" available in the
+ United States.
+
+ The "restrictBy" parameter controls which types of results are returned.
+
+ Multiple values for a single restrictBy can be separated by the "|" operator,
+ so to look for all products created by Canon, Sony, or Apple:
+
+ restrictBy = 'brand:canon|sony|apple'
+
+ Multiple restricting parameters should be separated by a comma, so for
+ products created by Sony with the word "32GB" in the title:
+
+ restrictBy = 'brand:sony,title:32GB'
+ """
+ client = build('shopping', SHOPPING_API_VERSION, developerKey=DEVELOPER_KEY)
+ resource = client.products()
+ request = resource.list(source='public', country='US',
+ restrictBy='brand:canon', q='Digital Camera')
+ response = request.execute()
+ pprint.pprint(response)
+
+
+if __name__ == '__main__':
+ main()