|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2019 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""This example fetches the set of valid ProductBiddingCategories.""" |
| 16 | + |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +import argparse |
| 20 | +import collections |
| 21 | +import sys |
| 22 | +import six |
| 23 | +from google.ads.google_ads.client import GoogleAdsClient |
| 24 | +from google.ads.google_ads.errors import GoogleAdsException |
| 25 | + |
| 26 | +_DEFAULT_PAGE_SIZE = 1000 |
| 27 | + |
| 28 | + |
| 29 | +def display_categories(categories, prefix=''): |
| 30 | + for category in categories: |
| 31 | + print('{}{} [{}]'.format(prefix, category.name, category.id)) |
| 32 | + if not category.children: |
| 33 | + display_categories(category.children, prefix=prefix + category.name) |
| 34 | + |
| 35 | + |
| 36 | +def main(client, customer_id, page_size): |
| 37 | + ga_service = client.get_service('GoogleAdsService', version='v1') |
| 38 | + query = ('SELECT product_bidding_category_constant.localized_name, ' |
| 39 | + 'product_bidding_category_constant.product_bidding_category_constant_parent ' |
| 40 | + 'FROM product_bidding_category_constant WHERE ' |
| 41 | + 'product_bidding_category_constant.country_code IN ("US")') |
| 42 | + |
| 43 | + results = ga_service.search(customer_id, query=query, page_size=page_size) |
| 44 | + |
| 45 | + class Category: |
| 46 | + def __init__(self, name=None, id=None, children=[]): |
| 47 | + self.name = name |
| 48 | + self.id = id |
| 49 | + self.children = children |
| 50 | + |
| 51 | + all_categories = collections.defaultdict(lambda: Category()) |
| 52 | + |
| 53 | + root_categories = [] |
| 54 | + |
| 55 | + try: |
| 56 | + for row in results: |
| 57 | + product_bidding_category = row.product_bidding_category_constant |
| 58 | + |
| 59 | + category = Category(product_bidding_category.localized_name.value, |
| 60 | + product_bidding_category.resource_name) |
| 61 | + |
| 62 | + all_categories[category.id] = category |
| 63 | + parent = product_bidding_category.product_bidding_category_constant_parent |
| 64 | + parent_id = getattr(parent, 'value', None) |
| 65 | + |
| 66 | + if parent_id: |
| 67 | + all_categories[parent_id].children.append(category) |
| 68 | + else: |
| 69 | + root_categories.append(category) |
| 70 | + |
| 71 | + display_categories(root_categories) |
| 72 | + except GoogleAdsException as ex: |
| 73 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 74 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 75 | + for error in ex.failure.errors: |
| 76 | + print('\tError with message "%s".' % error.message) |
| 77 | + if error.location: |
| 78 | + for field_path_element in error.location.field_path_elements: |
| 79 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + google_ads_client = GoogleAdsClient.load_from_storage() |
| 84 | + |
| 85 | + parser = argparse.ArgumentParser( |
| 86 | + description='Get Product Bidding Category Constant') |
| 87 | + # The following argument(s) should be provided to run the example. |
| 88 | + parser.add_argument('-c', '--customer_id', type=six.text_type, |
| 89 | + required=True, help='The Google Ads customer ID.') |
| 90 | + args = parser.parse_args() |
| 91 | + |
| 92 | + main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE) |
0 commit comments