def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] email = None if shopping_common.json_absent_or_false(config, 'accountSampleUser'): print('Must specify the user email to add in the samples configuration.') sys.exit(1) email = config['accountSampleUser'] # First we need to retrieve the existing set of users. response = service.accounts().get( merchantId=merchant_id, accountId=merchant_id, fields='users').execute() account = response # Add new user to existing user list. new_user = {'emailAddress': email, 'admin': False} account['users'].append(new_user) # Patch account with new user list. response = service.accounts().patch( merchantId=merchant_id, accountId=merchant_id, body=account).execute() print('Account %s was added to merchant ID %d' % (email, merchant_id))
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] datafeed_id = flags.datafeed_id try: # Changing the scheduled fetch time to 7:00. request = service.datafeeds().patch( merchantId=merchant_id, datafeedId=datafeed_id, body={'fetchSchedule': { 'hour': 7 }}) result = request.execute() print('Datafeed with ID "%s" and fetchSchedule %s was updated.' % (result['id'], str(result['fetchSchedule']))) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] shopping_common.check_mca(config, False) request = service.productstatuses().list(merchantId=merchant_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() if shopping_common.json_absent_or_false(result, 'resources'): print('No products were found.') break else: statuses = result['resources'] for status in statuses: print('- Product "%s" with title "%s":' % (status['productId'], status['title'])) if shopping_common.json_absent_or_false( status, 'dataQualityIssues'): print(' No data quality issues.') else: print(' Found %d data quality issues:' % len(status['dataQualityIssues'])) for issue in status['dataQualityIssues']: if shopping_common.json_absent_or_false( issue, 'detail'): print(' - (%s) [%s]' % (issue['severity'], issue['id'])) else: print(' - (%s) [%s] %s' % (issue['severity'], issue['id'], issue['detail'])) request = service.productstatuses().list_next(request, result)
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] product_id = flags.product_id try: # First we need to retrieve the full object, since there are no partial # updates for the products collection in Content API v2. product = service.products().get(merchantId=merchant_id, productId=product_id).execute() # Let's fix the warning about product_type and update the product. product['productType'] = 'English/Classics' # Notice that we use insert. The products service does not have an update # method. Inserting a product with an ID that already exists means the same # as doing an update. request = service.products().insert(merchantId=merchant_id, body=product) result = request.execute() print('Product with offerId "%s" and productType "%s" was updated.' % (result['offerId'], result['productType'])) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] product_ids = flags.product_ids batch = BatchHttpRequest(callback=product_updated) for product_id in product_ids: new_status = { 'availability': 'out of stock', 'price': { 'value': 3.14, 'currency': 'USD' } } # Add product update to the batch. batch.add(service.inventory().set(merchantId=merchant_id, storeCode=product_id.split(':')[0], productId=product_id, body=new_status)) try: batch.execute() except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] shopping_common.check_mca(config, True) try: request = service.accounts().list(merchantId=merchant_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() if shopping_common.json_absent_or_false(result, 'resources'): print 'No accounts were found.' else: accounts = result['resources'] for account in accounts: print('Account "%s" with name "%s" was found.' % (account['id'], account['name'])) request = service.accounts().list_next(request, result) break except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init( argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] product_id = flags.product_id new_status = { 'availability': 'out of stock', 'price': {'value': 3.00, 'currency': 'USD'}} request = service.inventory().set( merchantId=merchant_id, storeCode=product_id.split(':')[0], productId=product_id, body=new_status) try: unused_result = request.execute() print 'Product with ID "%s" was updated.' % (product_id,) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] try: request = service.products().list(merchantId=merchant_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() if 'resources' in result: products = result['resources'] for product in products: print('Product "%s" with title "%s" was found.' % (product['id'], product['title'])) request = service.products().list_next(request, result) else: print 'No products were found.' break except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] email = None if shopping_common.json_absent_or_false(config, 'accountSampleUser'): print( 'Must specify the user email to remove in the samples configuration.' ) sys.exit(1) email = config['accountSampleUser'] # First we need to retrieve the existing set of users. account = service.accounts().get(merchantId=merchant_id, accountId=merchant_id, fields='users').execute() if shopping_common.json_absent_or_false(account, 'users'): print('No users in account %d.' % merchant_id) sys.exit(1) matched = [u for u in account['users'] if u['emailAddress'] == email] if not matched: print('User %s was not found.' % email) sys.exit(1) for u in matched: account['users'].remove(u) # Patch account with new user list. service.accounts().patch(merchantId=merchant_id, accountId=merchant_id, body=account).execute() print('User %s was removed from merchant ID %d' % (email, merchant_id))
def get_product_list(argv,inven_file): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] try: request = service.products().list( merchantId=merchant_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() if shopping_common.json_absent_or_false(result, 'resources'): print('No products were found.') break else: products = result['resources'] for product in products: # print(product) # exit() inven_file.write('\t'.join([product['id'],product.get('gtin',''),product['price'].get('value',''),product['condition'],product['availability']])+'\n') print('Product "%s" was found.' % (product['id'])) request = service.products().list_next(request, result) except client.AccessTokenRefreshError: print('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] adwords_id = None if shopping_common.json_absent_or_false(config, 'accountSampleAdWordsCID'): print 'Must specify the AdWords CID to link in the samples configuration.' sys.exit(1) adwords_id = config['accountSampleAdWordsCID'] try: # First we need to retrieve the existing set of users. response = service.accounts().get(merchantId=merchant_id, accountId=merchant_id, fields='adwordsLinks').execute() account = response # Add new user to existing user list. adwords_link = {'adwordsId': adwords_id, 'status': 'active'} account.setdefault('adwordsLinks', []).append(adwords_link) # Patch account with new user list. response = service.accounts().patch(merchantId=merchant_id, accountId=merchant_id, body=account).execute() print 'AdWords ID %s was added to merchant ID %s' % (adwords_id, merchant_id) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init( argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] account_id = flags.account_id if merchant_id != account_id: shopping_common.check_mca( config, True, msg='Non-multi-client accounts can only get their own information.') status = service.accounttax().get( merchantId=merchant_id, accountId=merchant_id).execute() print('Account %s:' % status['accountId']) if shopping_common.json_absent_or_false(status, 'rules'): print('- No tax settings, so no tax is charged.') else: print('- Found %d tax rules:' % len(status['rules'])) for issue in status['rules']: if not shopping_common.json_absent_or_false(issue, 'ratePercent'): print(' - For %s in %s: %s%%' % (issue['locationId'], issue['country'], issue['ratePercent'])) if not shopping_common.json_absent_or_false(issue, 'useGlobalRate'): print(' - For %s in %s: using the global tax table rate.' % (issue['locationId'], issue['country'])) if not shopping_common.json_absent_or_false(issue, 'shippingTaxed'): print(' NOTE: Shipping charges are also taxed.')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] product_id = flags.product_id shopping_common.check_mca(config, False) try: status = service.productstatuses().get(merchantId=merchant_id, productId=product_id).execute() print('- Product "%s" with title "%s":' % (status['productId'], status['title'])) if shopping_common.json_absent_or_false(status, 'dataQualityIssues'): print ' No data quality issues.' else: print 'Found %d data quality issues:' % len( status['dataQualityIssues']) for issue in status['dataQualityIssues']: if shopping_common.json_absent_or_false(issue, 'detail'): print ' - (%s) [%s]' % (issue['severity'], issue['id']) else: print(' - (%s) [%s] %s' % (issue['severity'], issue['id'], issue['detail'])) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] shopping_common.check_mca(config, True) try: name = 'account%s' % shopping_common.get_unique_id() account = { 'name': name, 'websiteUrl': 'https://%s.example.com/' % (name, ) } # Add account. request = service.accounts().insert(merchantId=merchant_id, body=account) result = request.execute() print('Created account ID "%s" for MCA "%s".' % (result['id'], merchant_id)) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init( argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] account_id = flags.account_id if merchant_id != account_id: shopping_common.check_mca( config, True, msg='Non-multi-client accounts can only get their own information.') try: status = service.accountstatuses().get( merchantId=merchant_id, accountId=merchant_id).execute() print 'Account %s:' % status['accountId'] if shopping_common.json_absent_or_false(status, 'dataQualityIssues'): print '- No data quality issues.' else: print( '- Found %d data quality issues:' % len(status['dataQualityIssues'])) for issue in status['dataQualityIssues']: print ' - (%s) [%s]' % (issue['severity'], issue['id']) if shopping_common.json_absent_or_false(issue, 'exampleItems'): print ' - No example items.' else: print(' - Have %d examples from %d affected items:' % (len(issue['exampleItems']), issue['numItems'])) for example in issue['exampleItems']: print ' - %s: %s' % (example['itemId'], example['title']) except client.AccessTokenRefreshError: print('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init( argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] account_id = flags.account_id if not account_id: account_id = merchant_id elif merchant_id != account_id: shopping_common.check_mca( config, True, msg='Non-multi-client accounts can only get their own information.') status = service.accountstatuses().get( merchantId=merchant_id, accountId=merchant_id).execute() print('Account %s:' % status['accountId']) if shopping_common.json_absent_or_false(status, 'dataQualityIssues'): print('- No data quality issues.') else: print('- Found %d data quality issues:' % len(status['dataQualityIssues'])) for issue in status['dataQualityIssues']: print(' - (%s) [%s]' % (issue['severity'], issue['id'])) if shopping_common.json_absent_or_false(issue, 'exampleItems'): print(' - No example items.') else: print(' - Have %d examples from %d affected items:' % (len(issue['exampleItems']), issue['numItems'])) for example in issue['exampleItems']: print(' - %s: %s' % (example['itemId'], example['title']))
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] email = None if 'accountSampleUser' in config and config['accountSampleUser']: email = config['accountSampleUser'] else: print 'Must specify the user email to add in the samples configuration.' sys.exit(1) try: # First we need to retrieve the existing set of users. response = service.accounts().get(merchantId=merchant_id, accountId=merchant_id, fields='users').execute() account = response # Add new user to existing user list. new_user = {'emailAddress': email, 'admin': False} account['users'].append(new_user) # Patch account with new user list. response = service.accounts().patch(merchantId=merchant_id, accountId=merchant_id, body=account).execute() print 'Account %s was added to merchant ID %s' % (email, merchant_id) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] datafeed_ids = flags.datafeed_ids batch = { 'entries': [{ 'batchId': i, 'merchantId': merchant_id, 'method': 'delete', 'datafeedId': v, } for i, v in enumerate(datafeed_ids)], } request = service.datafeeds().custombatch(body=batch) result = request.execute() if result['kind'] == 'content#datafeedsCustomBatchResponse': for entry in result['entries']: if not shopping_common.json_absent_or_false(entry, 'errors'): print('Errors for batch entry %d:' % entry['batchId']) print( json.dumps(entry['errors'], sort_keys=True, indent=2, separators=(',', ': '))) else: print('Successfully deleted datafeed %s (batch entry %d).' % (datafeed_ids.get(entry['batchId']), entry['batchId'])) else: print('There was an error. Response: %s' % result)
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] adwords_id = None if shopping_common.json_absent_or_false(config, 'accountSampleAdWordsCID'): print( 'Must specify the AdWords CID to link in the samples configuration.' ) sys.exit(1) adwords_id = config['accountSampleAdWordsCID'] # First we need to retrieve the existing set of users. response = service.accounts().get(merchantId=merchant_id, accountId=merchant_id, fields='adwordsLinks').execute() account = response # Add new user to existing user list. adwords_link = {'adwordsId': adwords_id, 'status': 'active'} account.setdefault('adwordsLinks', []).append(adwords_link) # Patch account with new user list. response = service.accounts().patch(merchantId=merchant_id, accountId=merchant_id, body=account).execute() print('AdWords ID %d was added to merchant ID %d' % (adwords_id, merchant_id))
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] datafeed_id = flags.datafeed_id request = service.datafeeds().delete(merchantId=merchant_id, datafeedId=datafeed_id) request.execute() print('Datafeed %s was deleted.' % datafeed_id)
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] product_id = flags.product_id request = service.products().delete(merchantId=merchant_id, productId=product_id) request.execute() print('Product %s was deleted.' % product_id)
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] account_id = flags.account_id shopping_common.check_mca(config, True) request = service.accounts().delete(merchantId=merchant_id, accountId=account_id) request.execute() print('Account %s was deleted.' % account_id)
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] offer_id = 'book#%s' % shopping_common.get_unique_id() product = product_sample.create_product_sample(config, offer_id) # Add product. request = service.products().insert(merchantId=merchant_id, body=product) result = request.execute() print('Product with offerId "%s" and title "%s" was created.' % (result['offerId'], result['title']))
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] name = 'feed%s' % shopping_common.get_unique_id() datafeed = datafeed_sample.create_datafeed_sample(config, name) # Add datafeed. request = service.datafeeds().insert(merchantId=merchant_id, body=datafeed) result = request.execute() print('Datafeed with name "%s" and ID %s was created.' % (result['name'], result['id']))
def product_insert(argv, products_list, mode): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] batch = {'entries': []} position = 0 while position < len(products_list): batch['entries'] = [] product_list = products_list[position:position + BATCH_SIZE] position += BATCH_SIZE for i in range(BATCH_SIZE): if len(product_list) > i: product = create_product(config, product_list[i], mode) # Add product to the batch. batch['entries'].append({ 'batchId': i, 'merchantId': merchant_id, 'method': 'insert', 'product': product }) try: request = service.products().custombatch(body=batch) try: result = request.execute() except: traceback.print_exc() #pdb.set_trace() if result['kind'] == 'content#productsCustomBatchResponse': entries = result['entries'] for entry in entries: if not shopping_common.json_absent_or_false( entry, 'product'): product = entry['product'] print('Product with offerId "%s" was update.' % (product['offerId'])) f_log1.write( str(product['offerId'].split('_')[-1]) + '\n') elif not shopping_common.json_absent_or_false( entry, 'errors'): print(entry['errors']) else: print('There was an error. Response: %s' % (result)) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init( argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] account_id = flags.account_id shopping_common.check_mca(config, True) request = service.accounts().delete( merchantId=merchant_id, accountId=account_id) try: request.execute() print 'Account was deleted.' except client.AccessTokenRefreshError: print('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] shopping_common.check_mca(config, True) name = 'account%s' % shopping_common.get_unique_id() account = {'name': name, 'websiteUrl': 'https://%s.example.com/' % name} # Add account. request = service.accounts().insert(merchantId=merchant_id, body=account) result = request.execute() print('Created sub-account ID %s for MCA %d.' % (result['id'], merchant_id))
def main(argv): # Authenticate and construct service. service, config, flags = shopping_common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] account_id = flags.account_id shopping_common.check_mca(config, True) new_name = 'updated-account%s' % shopping_common.get_unique_id() request = service.accounts().patch(merchantId=merchant_id, accountId=account_id, body={'name': new_name}) result = request.execute() print('Account with id %s was updated with new name "%s".' % (account_id, result['name']))
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] batch = {'entries': []} for i in range(BATCH_SIZE): offer_id = 'book#%s' % shopping_common.get_unique_id() product = product_sample.create_product_sample( config, offer_id, title='This is book number %d' % (i, ), price={ 'value': '%d.50' % (i, ), 'currency': 'USD' }) # Add product to the batch. batch['entries'].append({ 'batchId': i, 'merchantId': merchant_id, 'method': 'insert', 'product': product }) try: request = service.products().custombatch(body=batch) result = request.execute() if result['kind'] == 'content#productsCustomBatchResponse': entries = result['entries'] for entry in entries: if 'product' in entry: product = entry['product'] print( 'Product with offerId "%s" and title "%s" was created.' % (product['offerId'], product['title'])) elif 'errors' in entry: print entry['errors'] else: print 'There was an error. Response: %s' % (result) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, config, _ = shopping_common.init(argv, __doc__) merchant_id = config['merchantId'] shopping_common.check_mca(config, True) try: request = service.accountstatuses().list(merchantId=merchant_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() if shopping_common.json_absent_or_false(result, 'resources'): print 'No accounts were found.' break else: statuses = result['resources'] for status in statuses: print 'Account %s:' % status['accountId'] if shopping_common.json_absent_or_false( status, 'dataQualityIssues'): print '- No data quality issues.' else: print('- Found %d data quality issues:' % len(status['dataQualityIssues'])) for issue in status['dataQualityIssues']: print ' - (%s) [%s]' % (issue['severity'], issue['id']) if shopping_common.json_absent_or_false( issue, 'exampleItems'): print ' - No example items.' else: print( ' - Have %d examples from %d affected items:' % (len(issue['exampleItems']), issue['numItems'])) for example in issue['exampleItems']: print ' - %s: %s' % (example['itemId'], example['title']) request = service.accountstatuses().list_next(request, result) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')