Python 为多个mcc帐户提取Adwords报告

Python 为多个mcc帐户提取Adwords报告,python,google-ads-api,Python,Google Ads Api,我正在寻找一种使用AdWords API for python一次性获取多个mcc_帐户的报告的方法,通过下面的代码,我可以使用google_ads.YAML文件一次传递一个mcc帐户 有没有办法从CSV或文本文件中传递多个MCC帐户,并为其提取报告 YAML文件 代码 导入多处理 导入操作系统 从队列导入空 导入时间 导入googleads.adwords 导入gogleads.errors #重试之间的超时时间(秒)。 退避系数=5 #要生成的最大进程数。 最大进程数=多处理。cpu计数()

我正在寻找一种使用AdWords API for python一次性获取多个mcc_帐户的报告的方法,通过下面的代码,我可以使用google_ads.YAML文件一次传递一个mcc帐户

有没有办法从CSV或文本文件中传递多个MCC帐户,并为其提取报告

YAML文件

代码

导入多处理
导入操作系统
从队列导入空
导入时间
导入googleads.adwords
导入gogleads.errors
#重试之间的超时时间(秒)。
退避系数=5
#要生成的最大进程数。
最大进程数=多处理。cpu计数()
#500个错误的最大重试次数。
最大重试次数=5次
#单个API响应中要发送的最大项目数。
页面大小=100
#要将报告下载到的目录。
报告_下载_目录='。'
def下载报告(流程id、报告下载目录、客户id、,
报告(定义):
report_downloader=(googleads.adwords.AdWordsClient.LoadFromStorage('googleads.yaml'))
.GetReportDownloader())
filepath=os.path.join(报告下载目录,
'adgroup_uu%d.csv'%customer\u id]
重试\u计数=0
尽管如此:
打印(“[%d/%d]正在将客户ID“%s”的报告加载到“%s”中…”
%(进程id、重试次数、客户id、文件路径))
尝试:
使用open(filepath,'wb')作为处理程序:
report\u downloader.DownloadReport(
报告定义,输出=处理程序,
客户\客户\ id=客户\ id)
返回True,{'customerId':customer\u id}
除googleads.errors.AdWords报告错误为e外:
如果e.code==500且重试次数<最大重试次数:
睡眠时间(重试次数*退避系数)
其他:
打印('客户ID为“%s”、代码为“%d”且在“%d”之后的报告失败'
“重试次数”。%(客户id,e.code,重试次数+1))
返回(False,{'customerId':客户id,'code':e.code,
“消息”:e.message})
类ReportWorker(多处理.Process):
“”“用于下载一组客户ID的报告的工作进程。”“”
定义初始化(自我、报告下载目录、报告定义、,
输入队列、成功队列、失败队列):
“”“初始化ReportWorker。
Args:
report\u download\u directory:一个字符串,指示您所在的目录
希望下载报告。
报告定义:一个包含你想要的报告定义的dict
希望针对输入队列中的所有客户ID运行。
input_queue:一个队列实例,包含
将针对运行报表定义。
success\u queue:显示成功报告详细信息的队列实例
下载内容将保存到。
failure_queue:包含失败报告详细信息的队列实例
下载内容将保存到。
"""
超级(ReportWorker,self)。\uuuuu init\uuuuuu()
self.report\u download\u directory=报告\u download\u directory
self.report\u definition=报告定义
self.input\u queue=input\u queue
self.success\u queue=success\u queue
self.failure\u queue=failure\u queue
def运行(自):
尽管如此:
尝试:
customer\u id=self.input\u queue.get(超时=0.01)
除空外:
打破
结果=_下载报告(self.ident,self.report_下载目录,
客户(id,自我报告(定义)
(self.success\u队列如果结果[0]否则self.failure\u队列)。put(结果[1])
def GetCustomerID(客户端):
“”“检索帐户层次结构中的所有CustomerID。
请注意,您的配置文件必须指定所属的客户端\客户\ id
到AdWords manager帐户。
Args:
客户端:一个AdWordsClient实例。
提出:
例外情况:如果找不到客户ID。
返回:
包含帐户层次结构中所有CustomerID的队列实例。
"""
#对于本例,我们将使用ManagedCustomerService获取所有ID
#不属于MCC帐户的层次结构。
managed_customer_service=client.GetService('ManagedCustomerService',
版本='v201809')
偏移量=0
#获取此帐户的帐户层次结构。
选择器={
“字段”:[“客户ID”],
“谓词”:[{
“字段”:“CanManageClients”,
“运算符”:“等于”,
“值”:[假]
}],
“分页”:{
“startIndex”:str(偏移量),
“numberResults”:str(页面大小)
}
}
#使用队列来平衡进程之间的负载。
队列=多处理。队列()
更多页面=正确
而更多页面:
页面=托管客户服务。获取(选择器)
如果第页和第[‘条目’]页中的第页和‘条目’:
对于第['entries']页中的条目:
queue.put(条目['customerId'])
其他:
引发异常('无法检索任何客户ID'。)
偏移量+=页面大小
选择器['paging']['startIndex']=str(偏移量)
更多页面=偏移量# AdWordsClient configurations
adwords:
    #############################################################################
    # Required Fields                                                           #
    #############################################################################
    developer_token: XXXXXX
    #############################################################################
    # Optional Fields                                                           #
    #############################################################################
    client_customer_id: XXXX
    user_agent: XXXX
    # partial_failure: True
    # validate_only: True
    #############################################################################
    # OAuth2 Configuration                                                      #
    # Below you may provide credentials for either the installed application or #
    # service account flows. Remove or comment the lines for the flow you're    #
    # not using.                                                                #
    #############################################################################
    # The following values configure the client for the installed application
    # flow.
    client_id: XXXXX
    client_secret: XXXX
    refresh_token: XXXXX