Skip to content

Commit

Permalink
fixed misplaced try block (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
wihl authored Feb 27, 2020
1 parent 67843db commit af717cc
Showing 1 changed file with 54 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# Copyright 2018 Google LLC
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,8 @@
import argparse
import sys

import google.ads.google_ads.client
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException


_DEFAULT_PAGE_SIZE = 1000
Expand All @@ -32,65 +33,76 @@ def main(client, customer_id, page_size, campaign_id):
# First, retrieve all shared sets associated with the campaign.
shared_sets_query = (
'SELECT shared_set.id, shared_set.name FROM campaign_shared_set '
'WHERE campaign.id = %s' % campaign_id)
f'WHERE campaign.id = {campaign_id}')

try:
shared_set_response = ga_service.search(
customer_id, query=shared_sets_query, page_size=page_size)
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))

shared_set_ids = []
for row in shared_set_response:
shared_set = row.shared_set
shared_set_id = str(shared_set.id.value)
shared_set_ids.append(shared_set_id)
print(f'Campaign shared set ID "{shared_set_id}" and name '
f'"{shared_set.name.value}" was found.')
except GoogleAdsException as ex:
print(f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:')
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
print(f'\t\tOn field: {field_path_element.field_name}')
sys.exit(1)

shared_set_ids = []
for row in shared_set_response:
shared_set = row.shared_set
shared_set_id = str(shared_set.id.value)
shared_set_ids.append(shared_set_id)
print('Campaign shared set ID "%s" and name "%s" was found.'
% (shared_set_id, shared_set.name.value))

# Next, retrieve shared criteria for all found shared sets.
shared_criteria_query = (
'SELECT shared_criterion.type, shared_criterion.keyword.text, '
'shared_criterion.keyword.match_type, shared_set.id '
'FROM shared_criterion WHERE shared_set.id IN (%s)'
% ', '.join(shared_set_ids))
'FROM shared_criterion WHERE shared_set.id IN '
f'({", ".join(shared_set_ids)})')

try:
shared_criteria_response = ga_service.search(
customer_id, query=shared_criteria_query, page_size=page_size)
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))
except GoogleAdsException as ex:
print(f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:')
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
print(f'\t\tOn field: {field_path_element.field_name}')
sys.exit(1)

# Use the enum type to determine the enum name from the value.
keyword_match_type_enum = (
client.get_type('KeywordMatchTypeEnum', version='v2').KeywordMatchType)

criterion_ids = []
for row in shared_criteria_response:
shared_criterion = row.shared_criterion
shared_criterion_resource_name = shared_criterion.resource_name
if (shared_criterion.type ==
client.get_type('CriterionTypeEnum', version='v2').KEYWORD):
keyword = shared_criterion.keyword
print('Shared criterion with resource name "%s" for negative '
'keyword with text "%s" and match type "%s" was found.'
% (shared_criterion_resource_name, keyword.text.value,
keyword_match_type_enum.Name(keyword.match_type)))
criterion_ids.append(shared_criterion_resource_name)
try:
criterion_ids = []
for row in shared_criteria_response:
shared_criterion = row.shared_criterion
shared_criterion_resource_name = shared_criterion.resource_name
if (shared_criterion.type == client.get_type(
'CriterionTypeEnum', version='v2').KEYWORD):
keyword = shared_criterion.keyword
print('Shared criterion with resource name '
f'"{shared_criterion_resource_name}" for negative '
f'keyword with text "{keyword.text.value}" and match type'
f' "{keyword_match_type_enum.Name(keyword.match_type)}" '
'was found.')
criterion_ids.append(shared_criterion_resource_name)
except GoogleAdsException as ex:
print(f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:')
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f'\t\tOn field: {field_path_element.field_name}')
sys.exit(1)

operations = []

Expand All @@ -104,25 +116,24 @@ def main(client, customer_id, page_size, campaign_id):
try:
response = shared_criterion_service.mutate_shared_criteria(
customer_id, operations)
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))
except GoogleAdsException as ex:
print(f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:')
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
print(f'\t\tOn field: {field_path_element.field_name}')
sys.exit(1)

for result in response.results:
print('Removed shared criterion "%s".' % result.resource_name)
print(f'Removed shared criterion "{result.resource_name}".')


if __name__ == '__main__':
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = (google.ads.google_ads.client.GoogleAdsClient
.load_from_storage())
google_ads_client = GoogleAdsClient.load_from_storage()

parser = argparse.ArgumentParser(
description=('Finds shared sets, then finds and removes shared set '
Expand Down

0 comments on commit af717cc

Please sign in to comment.