29
29
import uuid
30
30
31
31
32
- from batch_job_util import BatchJobHelper
33
- from batch_job_util import BatchJobServiceIdGenerator
34
32
from googleads import adwords
35
33
36
34
37
- # Set logging configuration
35
+ # Set logging configuration.
38
36
logging .basicConfig (level = logging .INFO )
39
37
logging .getLogger ('suds.transport' ).setLevel (logging .DEBUG )
40
38
47
45
48
46
49
47
def main (client , number_of_campaigns , number_of_adgroups , number_of_keywords ):
50
- # Initialize BatchJobHelper
51
- batch_job_helper = BatchJobHelper ( client )
48
+ # Initialize BatchJobHelper.
49
+ batch_job_helper = client . GetBatchJobHelper ( )
52
50
51
+ # Create a BatchJob.
53
52
batch_job = AddBatchJob (client )
53
+ # Retrieve the URL used to upload the BatchJob operations.
54
54
upload_url = batch_job ['uploadUrl' ]['url' ]
55
55
batch_job_id = batch_job ['id' ]
56
-
57
56
logging .info ('Created BatchJob with ID "%d", status "%s", and upload URL '
58
57
'"%s"' , batch_job ['id' ], batch_job ['status' ], upload_url )
59
58
60
- operations = BuildUploadUrlOperations (batch_job_helper , number_of_campaigns ,
61
- number_of_adgroups , number_of_keywords )
59
+ # Generate operations to upload.
60
+ budget_operations = BuildBudgetOperations (batch_job_helper )
61
+ campaign_operations = BuildCampaignOperations (
62
+ batch_job_helper , budget_operations , number_of_campaigns )
63
+ campaign_criterion_operations = BuildCampaignCriterionOperations (
64
+ campaign_operations )
65
+ adgroup_operations = BuildAdGroupOperations (
66
+ batch_job_helper , campaign_operations , number_of_adgroups )
67
+ adgroup_criterion_operations = BuildAdGroupCriterionOperations (
68
+ adgroup_operations , number_of_keywords )
69
+ adgroup_ad_operations = BuildAdGroupAdOperations (adgroup_operations )
62
70
71
+ # Upload operations.
63
72
batch_job_helper .UploadBatchJobOperations (
64
- upload_url , * operations )
73
+ upload_url , budget_operations , campaign_operations ,
74
+ campaign_criterion_operations , adgroup_operations ,
75
+ adgroup_criterion_operations , adgroup_ad_operations )
65
76
77
+ # Download and display results.
66
78
download_url = GetBatchJobDownloadUrlWhenReady (client , batch_job_id )
67
79
response = urllib2 .urlopen (download_url )
68
80
logging .info ('Downloaded following response:\n %s' , response .read ())
@@ -79,7 +91,7 @@ def AddBatchJob(client):
79
91
"""
80
92
# Initialize appropriate service.
81
93
batch_job_service = client .GetService ('BatchJobService' , version = 'v201509' )
82
- # Create a BatchJob
94
+ # Create a BatchJob.
83
95
batch_job_operations = [{
84
96
'operand' : {},
85
97
'operator' : 'ADD'
@@ -160,7 +172,7 @@ def BuildAdGroupCriterionOperations(adgroup_operations, number_of_keywords=1):
160
172
return criterion_operations
161
173
162
174
163
- def BuildAdGroupOperations (batch_id_generator ,
175
+ def BuildAdGroupOperations (batch_job_helper ,
164
176
campaign_operations , number_of_adgroups = 1 ):
165
177
"""Builds the operations adding desired number of AdGroups to given Campaigns.
166
178
@@ -169,7 +181,7 @@ def BuildAdGroupOperations(batch_id_generator,
169
181
BatchJobService.
170
182
171
183
Args:
172
- batch_id_generator : a BatchJobServiceIdGenerator instance.
184
+ batch_job_helper : a BatchJobHelper instance.
173
185
campaign_operations: a list containing the operations that will add
174
186
Campaigns.
175
187
number_of_adgroups: an int defining the number of AdGroups to be created per
@@ -189,7 +201,7 @@ def BuildAdGroupOperations(batch_id_generator,
189
201
'xsi_type' : 'AdGroupOperation' ,
190
202
'operand' : {
191
203
'campaignId' : campaign_operation ['operand' ]['id' ],
192
- 'id' : batch_id_generator .GetId (),
204
+ 'id' : batch_job_helper .GetId (),
193
205
'name' : 'Batch Ad Group #%s' % uuid .uuid4 (),
194
206
'biddingStrategyConfiguration' : {
195
207
'bids' : [
@@ -210,15 +222,15 @@ def BuildAdGroupOperations(batch_id_generator,
210
222
return adgroup_operations
211
223
212
224
213
- def BuildBudgetOperations (batch_id_generator ):
225
+ def BuildBudgetOperations (batch_job_helper ):
214
226
"""Builds the operations needed to create a new Budget.
215
227
216
228
Note: When the Budget is created, it will have a different Id than the one
217
229
generated here as a temporary Id. This is just used to identify it in the
218
230
BatchJobService.
219
231
220
232
Args:
221
- batch_id_generator : a BatchJobServiceIdGenerator instance.
233
+ batch_job_helper : a BatchJobHelper instance.
222
234
223
235
Returns:
224
236
a list containing the operation that will create a new Budget.
@@ -234,7 +246,7 @@ def BuildBudgetOperations(batch_id_generator):
234
246
'name' : 'Batch budget #%s' % uuid .uuid4 (),
235
247
# This is a temporary Id used by the BatchJobService to identify the
236
248
# Budget for operations that require a budgetId.
237
- 'budgetId' : batch_id_generator .GetId (),
249
+ 'budgetId' : batch_job_helper .GetId (),
238
250
'amount' : {
239
251
'microAmount' : '50000000'
240
252
},
@@ -282,7 +294,7 @@ def BuildCampaignCriterionOperations(campaign_operations):
282
294
return criterion_operations
283
295
284
296
285
- def BuildCampaignOperations (batch_id_generator ,
297
+ def BuildCampaignOperations (batch_job_helper ,
286
298
budget_operations , number_of_campaigns = 1 ):
287
299
"""Builds the operations needed to create a new Campaign.
288
300
@@ -291,7 +303,7 @@ def BuildCampaignOperations(batch_id_generator,
291
303
BatchJobService.
292
304
293
305
Args:
294
- batch_id_generator : a BatchJobServiceIdGenerator instance.
306
+ batch_job_helper : a BatchJobHelper instance.
295
307
budget_operations: a list containing the operation that will add the budget
296
308
used by these Campaigns.
297
309
number_of_campaigns: an int number defining the number of campaigns to be
@@ -316,7 +328,7 @@ def BuildCampaignOperations(batch_id_generator,
316
328
'status' : 'PAUSED' ,
317
329
# This is a temporary Id used by the BatchJobService to identify
318
330
# the Campaigns for operations that require a campaignId.
319
- 'id' : batch_id_generator .GetId (),
331
+ 'id' : batch_job_helper .GetId (),
320
332
'advertisingChannelType' : 'SEARCH' ,
321
333
# Note that only the budgetId is required
322
334
'budget' : {
@@ -333,70 +345,6 @@ def BuildCampaignOperations(batch_id_generator,
333
345
return campaign_operations
334
346
335
347
336
- def BuildUploadUrlOperations (batch_job_helper , number_of_campaigns ,
337
- number_of_adgroups , number_of_keywords ):
338
- """Builds a list of operations that will be uploaded to the BatchJobService.
339
-
340
- Args:
341
- batch_job_helper: a BatchJobHelper instance used to construct operations.
342
- number_of_campaigns: an int defining the number of Campaigns to be created.
343
- number_of_adgroups: an int defining the number of AdGroups to be created.
344
- number_of_keywords: an int defining the number of Keywords to be created.
345
-
346
- Returns:
347
- A tuple of str, where each is a set of operations for distinct services.
348
- """
349
- # Initialize BatchJobServiceIdGenerator.
350
- batch_id_generator = BatchJobServiceIdGenerator ()
351
-
352
- # Build BudgetOperations and retrieve in XML form.
353
- budget_operations = BuildBudgetOperations (batch_id_generator )
354
- budget_request_xml = batch_job_helper .GenerateRawRequestXML (
355
- budget_operations , 'BudgetService' )
356
- budget_operations_xml = batch_job_helper .ExtractOperations (
357
- budget_request_xml )
358
- # Build CampaignOperations and retrieve in XML form.
359
- campaign_operations = BuildCampaignOperations (
360
- batch_id_generator , budget_operations , number_of_campaigns )
361
- campaign_request_xml = batch_job_helper .GenerateRawRequestXML (
362
- campaign_operations , 'CampaignService' )
363
- campaign_operations_xml = batch_job_helper .ExtractOperations (
364
- campaign_request_xml )
365
- # Build CampaignCriterionOperations and retrieve in XML form.
366
- campaign_criterion_operations = BuildCampaignCriterionOperations (
367
- campaign_operations )
368
- campaign_criterion_request_xml = batch_job_helper .GenerateRawRequestXML (
369
- campaign_criterion_operations , 'CampaignCriterionService' )
370
- campaign_criterion_operations_xml = (
371
- batch_job_helper .ExtractOperations (
372
- campaign_criterion_request_xml ))
373
- # Build AdGroupOperations and retrieve in XML form.
374
- adgroup_operations = BuildAdGroupOperations (
375
- batch_id_generator , campaign_operations , number_of_adgroups )
376
- adgroup_request_xml = batch_job_helper .GenerateRawRequestXML (
377
- adgroup_operations , 'AdGroupService' )
378
- adgroup_operations_xml = batch_job_helper .ExtractOperations (
379
- adgroup_request_xml )
380
- # Build AdGroupCriterionOperations and retrieve in XML form.
381
- adgroup_criterion_operations = BuildAdGroupCriterionOperations (
382
- adgroup_operations , number_of_keywords )
383
- adgroup_criterion_request_xml = batch_job_helper .GenerateRawRequestXML (
384
- adgroup_criterion_operations , 'AdGroupCriterionService' )
385
- adgroup_criterion_operations_xml = (
386
- batch_job_helper .ExtractOperations (
387
- adgroup_criterion_request_xml ))
388
- # Build AdGroupAdOperations and retrieve in XML form.
389
- adgroup_ad_operations = BuildAdGroupAdOperations (adgroup_operations )
390
- adgroup_ad_request_xml = batch_job_helper .GenerateRawRequestXML (
391
- adgroup_ad_operations , 'AdGroupAdService' )
392
- adgroup_ad_operations_xml = batch_job_helper .ExtractOperations (
393
- adgroup_ad_request_xml )
394
-
395
- return (budget_operations_xml , campaign_operations_xml ,
396
- campaign_criterion_operations_xml , adgroup_operations_xml ,
397
- adgroup_criterion_operations_xml , adgroup_ad_operations_xml )
398
-
399
-
400
348
def GetBatchJob (client , batch_job_id ):
401
349
"""Retrieves the BatchJob with the given id.
402
350
0 commit comments