python - 克服 Facebook 营销 API 中的速率限制

标签 python facebook facebook-graph-api facebook-marketing-api

具体,我正在尝试做一些与这个问题非常相似的事情(有同样的问题):FB Ads API (#17) User request limit reached

但是,我正尝试在 python 中执行此操作(并且 API 自 15 年以来发生了很大变化)。这是我的代码(即使有 sleep 时间,它也会把我踢出去)- 我想知道是否有人可以帮助我调用具有类似信息的数组,以减少我的总调用次数。

my_account = AdAccount(ad_account_id)
camps = my_account.get_campaigns(fields=[Campaign.Field.name])

for campaign in camps[0:100]:
    time.sleep(5)
    print campaign[Campaign.Field.name]
    adsets = campaign.get_ad_sets([AdSet.Field.name, AdSet.Field.status])
    for adset in adsets:
        print '\t', adset[AdSet.Field.name]
        for stat in adset.get_insights(fields=[
        'impressions',
        'clicks',
        'spend',
        'unique_clicks',
        ]):
            for statfield in stat:
                print "\t\t%s:\t%s" % (statfield, stat[statfield])

一般,我如何意味着在这个限制内为我的需求(大规模改变)编码?实际上,我想编写一个代码来遍历并更改我公司每个广告集中的一些选项(例如,“在...时展开兴趣”从关闭到打开)。我们有数百个广告集,API 文档说更改消耗的调用比创建多 10 到 100 倍(我对两者都不感兴趣,只是阅读!)。这仅仅是一个问题,比方说,在每次更改之间让代码休眠 60 秒吗?他们不太清楚您在一个时间段内接到了多少个电话,或者检查这些电话的时间段有多长。例如,如果它是每日限制,那么 sleep 不会帮助我更改 1200 个广告集的选项。

我确实看到了有关升级的文档 (https://developers.facebook.com/docs/marketing-api/access),但是在审查过程中,一切都基于公共(public)(面向客户的多用户)应用程序。我想要做的就是能够从仅限桌面开发人员的内部脚本进行调用以进行批量更改。我找错地方了吗?

最佳答案

将此添加到您的代码中,您将永远不必担心 FB 的速率限制。 一旦接近极限,您的脚本就会自动休眠,然后在冷却后从它离开的地方恢复。享受:)

import logging
import requests as rq

#Function to find the string between two strings or characters
def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

#Function to check how close you are to the FB Rate Limit
def check_limit():
    check=rq.get('https://graph.facebook.com/v3.3/act_'+account_number+'/insights?access_token='+my_access_token)
    call=float(find_between(check.headers['x-business-use-case-usage'],'call_count":','}'))
    cpu=float(find_between(check.headers['x-business-use-case-usage'],'total_cputime":','}'))
    total=float(find_between(check.headers['x-business-use-case-usage'],'total_time":',','))
    usage=max(call,cpu,total)
    return usage

#Check if you reached 75% of the limit, if yes then back-off for 5 minutes (put this chunk in your loop, every 200-500 iterations)
if (check_limit()>75):
    print('75% Rate Limit Reached. Cooling Time 5 Minutes.')
    logging.debug('75% Rate Limit Reached. Cooling Time 5 Minutes.')
    time.sleep(300)

关于python - 克服 Facebook 营销 API 中的速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48573248/

相关文章:

Python write 直接在FTP中创建文件

javascript - 完成 FB.getLoginStatus 后,我想返回一组信息

Facebook Graph api,读取消息发送到带有页面访问 token 的页面

facebook - 我可以通过 api 获得 facebook 组墙帖子吗?

php - 有没有办法让 Facebook PHP SDK 和 Javascript SDK 进行通信

Python -- matplotlib 椭圆曲线

python - Selenium WebDriverException 尝试保存屏幕截图时出错(Python 3.6)

python - 来自 Lambda 层的 src FFmpeg 二进制文件

html - 如何为 Facebook og :image? 创建数组

php - URL 的重定向过多。在减少重定向数量之前,Facebook 将无法抓取此 URL