Home | History | Annotate | Download | only in searchforshopping
      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 #
      4 # Copyright 2014 Google Inc. All Rights Reserved.
      5 
      6 '''Simple command-line example for The Google Search
      7 API for Shopping.
      8 
      9 Command-line application that does a search for products.
     10 '''
     11 from __future__ import print_function
     12 
     13 __author__ = 'aherrman (at] google.com (Andy Herrman)'
     14 
     15 from googleapiclient.discovery import build
     16 
     17 # Uncomment the next line to get very detailed logging
     18 # httplib2.debuglevel = 4
     19 
     20 
     21 def main():
     22   p = build('shopping', 'v1',
     23             developerKey='AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0')
     24 
     25   # Search over all public offers:
     26   print('Searching all public offers.')
     27   res = p.products().list(
     28       country='US',
     29       source='public',
     30       q='android t-shirt'
     31       ).execute()
     32   print_items(res['items'])
     33 
     34   # Search over a specific merchant's offers:
     35   print()
     36   print('Searching Google Store.')
     37   res = p.products().list(
     38       country='US',
     39       source='public',
     40       q='android t-shirt',
     41       restrictBy='accountId:5968952',
     42       ).execute()
     43   print_items(res['items'])
     44 
     45   # Remember the Google Id of the last product
     46   googleId = res['items'][0]['product']['googleId']
     47 
     48   # Get data for the single public offer:
     49   print()
     50   print('Getting data for offer %s' % googleId)
     51   res = p.products().get(
     52       source='public',
     53       accountId='5968952',
     54       productIdType='gid',
     55       productId=googleId
     56       ).execute()
     57   print_item(res)
     58 
     59 
     60 def print_item(item):
     61   """Displays a single item: title, merchant, link."""
     62   product = item['product']
     63   print('- %s [%s] (%s)' % (product['title'],
     64                           product['author']['name'],
     65                           product['link']))
     66 
     67 
     68 def print_items(items):
     69   """Displays a number of items."""
     70   for item in items:
     71     print_item(item)
     72 
     73 if __name__ == '__main__':
     74   main()
     75