<s> """<STR_LIT>""" <EOL> import random <EOL> import time <EOL> import urllib2 <EOL> import uuid <EOL> from googleads import adwords <EOL> NUMBER_OF_CAMPAIGNS_TO_ADD = <NUM_LIT:2> <EOL> NUMBER_OF_ADGROUPS_TO_ADD = <NUM_LIT:2> <EOL> NUMBER_OF_KEYWORDS_TO_ADD = <NUM_LIT:5> <EOL> MAX_POLL_ATTEMPTS = <NUM_LIT:5> <EOL> PENDING_STATUSES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def main ( client , number_of_campaigns , number_of_adgroups , number_of_keywords ) : <EOL> batch_job_helper = client . GetBatchJobHelper ( version = '<STR_LIT>' ) <EOL> batch_job = AddBatchJob ( client ) <EOL> upload_url = batch_job [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] <EOL> batch_job_id = batch_job [ '<STR_LIT:id>' ] <EOL> print '<STR_LIT>' % ( <EOL> batch_job [ '<STR_LIT:id>' ] , batch_job [ '<STR_LIT:status>' ] , upload_url ) <EOL> budget_operations = BuildBudgetOperations ( batch_job_helper ) <EOL> campaign_operations = BuildCampaignOperations ( <EOL> batch_job_helper , budget_operations , number_of_campaigns ) <EOL> campaign_criterion_operations = BuildCampaignCriterionOperations ( <EOL> campaign_operations ) <EOL> adgroup_operations = BuildAdGroupOperations ( <EOL> batch_job_helper , campaign_operations , number_of_adgroups ) <EOL> adgroup_criterion_operations = BuildAdGroupCriterionOperations ( <EOL> adgroup_operations , number_of_keywords ) <EOL> adgroup_ad_operations = BuildAdGroupAdOperations ( adgroup_operations ) <EOL> batch_job_helper . UploadOperations ( <EOL> upload_url , budget_operations , campaign_operations , <EOL> campaign_criterion_operations , adgroup_operations , <EOL> adgroup_criterion_operations , adgroup_ad_operations ) <EOL> download_url = GetBatchJobDownloadUrlWhenReady ( client , batch_job_id ) <EOL> response = urllib2 . urlopen ( download_url ) . read ( ) <EOL> PrintResponse ( batch_job_helper , response ) <EOL> def AddBatchJob ( client ) : <EOL> """<STR_LIT>""" <EOL> batch_job_service = client . GetService ( '<STR_LIT>' , version = '<STR_LIT>' ) <EOL> batch_job_operations = [ { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ] <EOL> return batch_job_service . mutate ( batch_job_operations ) [ '<STR_LIT:value>' ] [ <NUM_LIT:0> ] <EOL> def BuildAdGroupAdOperations ( adgroup_operations ) : <EOL> """<STR_LIT>""" <EOL> adgroup_ad_operations = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : adgroup_operation [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for adgroup_operation in adgroup_operations ] <EOL> return adgroup_ad_operations <EOL> def BuildAdGroupCriterionOperations ( adgroup_operations , number_of_keywords = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> criterion_operations = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : adgroup_operation [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' % ( i , '<STR_LIT>' if i % <NUM_LIT:2> == <NUM_LIT:0> else '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for adgroup_operation in adgroup_operations <EOL> for i in range ( number_of_keywords ) ] <EOL> return criterion_operations <EOL> def BuildAdGroupOperations ( batch_job_helper , <EOL> campaign_operations , number_of_adgroups = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> adgroup_operations = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : campaign_operation [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:id>' : batch_job_helper . GetId ( ) , <EOL> '<STR_LIT:name>' : '<STR_LIT>' % uuid . uuid4 ( ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } <EOL> } <EOL> ] <EOL> } <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for campaign_operation in campaign_operations <EOL> for _ in range ( number_of_adgroups ) ] <EOL> return adgroup_operations <EOL> def BuildBudgetOperations ( batch_job_helper ) : <EOL> """<STR_LIT>""" <EOL> budget_operations = [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' % uuid . uuid4 ( ) , <EOL> '<STR_LIT>' : batch_job_helper . GetId ( ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ] <EOL> return budget_operations <EOL> def BuildCampaignCriterionOperations ( campaign_operations ) : <EOL> """<STR_LIT>""" <EOL> criterion_operations = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : campaign_operation [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for campaign_operation in campaign_operations ] <EOL> return criterion_operations <EOL> def BuildCampaignOperations ( batch_job_helper , <EOL> budget_operations , number_of_campaigns = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> budget_id = budget_operations [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> campaign_operations = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' % uuid . uuid4 ( ) , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : batch_job_helper . GetId ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : budget_id <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for _ in range ( number_of_campaigns ) ] <EOL> return campaign_operations <EOL> def GetBatchJob ( client , batch_job_id ) : <EOL> """<STR_LIT>""" <EOL> batch_job_service = client . GetService ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> selector = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ batch_job_id ] <EOL> } <EOL> ] <EOL> } <EOL> return batch_job_service . get ( selector ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def GetBatchJobDownloadUrlWhenReady ( client , batch_job_id , <EOL> max_poll_attempts = MAX_POLL_ATTEMPTS ) : <EOL> """<STR_LIT>""" <EOL> batch_job = GetBatchJob ( client , batch_job_id ) <EOL> poll_attempt = <NUM_LIT:0> <EOL> while ( poll_attempt in range ( max_poll_attempts ) and <EOL> batch_job [ '<STR_LIT:status>' ] in PENDING_STATUSES ) : <EOL> sleep_interval = ( <NUM_LIT:30> * ( <NUM_LIT:2> ** poll_attempt ) + <EOL> ( random . randint ( <NUM_LIT:0> , <NUM_LIT> ) / <NUM_LIT:1000> ) ) <EOL> print '<STR_LIT>' % sleep_interval <EOL> time . sleep ( sleep_interval ) <EOL> batch_job = GetBatchJob ( client , batch_job_id ) <EOL> poll_attempt += <NUM_LIT:1> <EOL> if '<STR_LIT>' in batch_job : <EOL> url = batch_job [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] <EOL> print ( '<STR_LIT>' <EOL> % ( batch_job [ '<STR_LIT:id>' ] , batch_job [ '<STR_LIT:status>' ] , url ) ) <EOL> return url <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def PrintResponse ( batch_job_helper , response_xml ) : <EOL> """<STR_LIT>""" <EOL> response = batch_job_helper . ParseResponse ( response_xml ) <EOL> if '<STR_LIT>' in response [ '<STR_LIT>' ] : <EOL> for data in response [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in data : <EOL> print '<STR_LIT>' % data [ '<STR_LIT:index>' ] <EOL> print '<STR_LIT>' % data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> print '<STR_LIT>' % data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> print '<STR_LIT>' % data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> print '<STR_LIT>' % data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> print '<STR_LIT>' % data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if '<STR_LIT:result>' in data : <EOL> print '<STR_LIT>' % data [ '<STR_LIT:index>' ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> adwords_client = adwords . AdWordsClient . LoadFromStorage ( ) <EOL> main ( adwords_client , NUMBER_OF_CAMPAIGNS_TO_ADD , NUMBER_OF_ADGROUPS_TO_ADD , <EOL> NUMBER_OF_KEYWORDS_TO_ADD ) </s>
