检索具有分页功能的FB广告API的异步广告洞察结果

时间:2017-08-14 13:41:32

标签: python facebook facebook-ads-api

我正在使用facebook-python-ads-sdk对FB见解API进行异步调用described

params = {
    "time_increment": 1,
    "level": "ad",
    "date_preset": "last_28d",
    "breakdowns": "hourly_stats_aggregated_by_advertiser_time_zone",
    "limit": 1000
}

job = AdAccount("id").get_insights_async(params=params)
result_cursor = wait_for_async_job(job)
results = [item for item in result_cursor]

def wait_for_async_job(job):
    for _ in range(TIMEOUT):
        time.sleep(1)
        job = job.remote_read()
        status = job[AdReportRun.Field.async_status]
        if status == "Job Completed":
            return job.get_result()

因此,检索last_28d的见解的工作在几分钟内完成,然而,对结果的分页可能需要长达一个小时!

这是对异步作业进行分页的正确方法吗?

3 个答案:

答案 0 :(得分:6)

我发布了答案,因此它可以帮助其他具有相同问题的开发人员。

修改:

browseEncrypt.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {


        File selectedFile = chooseEncrypt.showOpenDialog(primaryStage);
        if (selectedFile != null) {
            encryptPath.setText(selectedFile.getPath());
            encryptedFile = selectedFile;//Add This!
            primaryStage.show();//Not sure why this is here?
        }
    }
});

为:

var ul = document.getElementById('ul-id');

ul.addEventListener('mouseover', function(e) {
  
  if (e.target.nodeName.toUpperCase() !== "LI") return;
  document.getElementById("" + e.target.id + e.target.id).style.opacity = "0.6";
});

这将跳过1000的跳跃结果,而不是默认的25。

上述改变为我们节省了30分钟。

答案 1 :(得分:0)

今天我遇到了同样的问题,这段代码对我有用

from facebook_business.adobjects.campaign import Campaign
from facebook_business.adobjects.adreportrun import AdReportRun
from facebook_business.adobjects.adsinsights import AdsInsights
import time

campaign = Campaign(<CAMPAIGN_ID>)

params = {
    'level': AdsInsights.Level.campaign,
}
async_job = campaign.get_insights(params=params, is_async=True)
async_job.api_get()
while while async_job[AdReportRun.Field.async_status]!= 'Job Completed':
     time.sleep(1)
     async_job.api_get()
time.sleep(1)
print(async_job.get_result())

答案 2 :(得分:0)

替换

return job.get_result(params={"limit": 1000})

list(async_job.get_result())

始终获得所有回复。 get_result() 是一个迭代器。