Xod*_*777 1 python facebook facebook-graph-api facebook-marketing-api
具体来说,我正在尝试做一些与这个问题非常相似的事情(有同样的问题):FB Ads API (#17) User request limit connected
但是,我正在尝试在 python 中执行此操作(并且自 15 年以来 API 发生了很大变化)。这是我的代码(它把我踢出去,即使有睡眠时间) - 我想知道是否有人可以帮助我调用具有类似信息的数组,以减少我的总调用次数。
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])
Run Code Online (Sandbox Code Playgroud)
更一般地说,我打算如何在此限制范围内为我的需求(大规模更改)编码?实际上,我想编写一个代码来检查和更改我公司每个广告组中的一些选项(例如“扩展兴趣...”从关闭到开启)。我们有数百个广告集,API 文档说更改消耗的调用次数是创建调用的 10-100 倍(我两个都没有遇到,只是阅读!)。这是否只是在每次更改之间让代码休眠 60 秒的问题?他们不太清楚你在一个时间段内接到多少电话,或者检查这些电话的时间段有多宽。例如,如果是每日限制,那么睡眠将无法帮助我更改 1200 个广告组的选项。
我确实看到了有关升级的文档(https://developers.facebook.com/docs/marketing-api/access),但是在审核过程中,所有内容都基于公共(面向客户、多用户)应用程序。我想要做的就是能够从仅限桌面开发的内部脚本进行调用以进行批量更改。我找错地方了吗?
将此添加到您的代码中,您将永远不必担心 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2067 次 |
| 最近记录: |