|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2020 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 code example gets information about all video and image files.""" |
| 16 | + |
| 17 | + |
| 18 | +import argparse |
| 19 | +import sys |
| 20 | +from google.ads.google_ads.client import GoogleAdsClient |
| 21 | +from google.ads.google_ads.errors import GoogleAdsException |
| 22 | + |
| 23 | +_DEFAULT_PAGE_SIZE = 1000 |
| 24 | + |
| 25 | + |
| 26 | +def main(client, customer_id, page_size): |
| 27 | + """Main method, to run this code example as a standalone application.""" |
| 28 | + ga_service = client.get_service('GoogleAdsService', version='v3') |
| 29 | + |
| 30 | + # Creates a query that will retrieve all video and image files. |
| 31 | + query = ('SELECT media_file.id, media_file.name, media_file.type ' |
| 32 | + 'FROM media_file ORDER BY media_file.id') |
| 33 | + |
| 34 | + # Issues a search request by specifying page size. |
| 35 | + results = ga_service.search(customer_id, query=query, page_size=page_size) |
| 36 | + |
| 37 | + media_type_enum = client.get_type('MediaTypeEnum', version='v3').MediaType |
| 38 | + |
| 39 | + # Iterates over all rows and prints the information about each media file. |
| 40 | + try: |
| 41 | + for row in results: |
| 42 | + media_file = row.media_file |
| 43 | + print(f'Media file with ID {media_file.id.value}, ' |
| 44 | + f'name "{media_file.name.value}", ' |
| 45 | + f'type {media_type_enum.Name(media_file.type)} was found.') |
| 46 | + except GoogleAdsException as ex: |
| 47 | + print(f'Request with ID "{ex.request_id}" failed with status ' |
| 48 | + f'"{ex.error.code().name}" and includes the following errors:') |
| 49 | + for error in ex.failure.errors: |
| 50 | + print(f'\tError with message "{error.message}".') |
| 51 | + if error.location: |
| 52 | + for field_path_element in error.location.field_path_elements: |
| 53 | + print(f'\t\tOn field: {field_path_element.field_name}') |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 59 | + # home directory if none is specified. |
| 60 | + google_ads_client = GoogleAdsClient.load_from_storage() |
| 61 | + |
| 62 | + parser = argparse.ArgumentParser( |
| 63 | + description='List all videos and images for specified customer.') |
| 64 | + # The following argument(s) should be provided to run the example. |
| 65 | + parser.add_argument('-c', '--customer_id', type=str, |
| 66 | + required=True, help='The Google Ads customer ID.') |
| 67 | + args = parser.parse_args() |
| 68 | + |
| 69 | + main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE) |
0 commit comments