Twilio Python助手库 - 你怎么知道有多少页面列表资源返回?
问题描述:
我想写一个简单的脚本来使用python帮助程序库从Twilio下载调用详细信息。到目前为止,似乎我唯一的选择是使用.iter()方法来获取有关该子帐户的每个调用。这可能是一个非常大的数字。Twilio Python助手库 - 你怎么知道有多少页面列表资源返回?
如果我使用.list()资源,它似乎没有给我一个页面计数的任何地方,所以我不知道多久才能继续分页以获取该时间段内的所有呼叫。我错过了什么?
下面是一些代码样本文档: http://readthedocs.org/docs/twilio-python/en/latest/usage/basics.html
答
正如在评论中提到的那样,上面的代码不起作用,因为remaining_messages = client.calls.count()总是返回50,这对于分页来说绝对是无用的。
相反,我最终只是尝试下一页,直到它失败,这是相当黑客。该库应该在列表资源中真正包含numpages以进行分页。
import twilio.rest
import csv
account = <ACCOUNT_SID>
token = <ACCOUNT_TOKEN>
client = twilio.rest.TwilioRestClient(account, token)
csvout = open("calls.csv","wb")
writer = csv.writer(csvout)
current_page = 0
page_size = 50
started_after = "20111208"
test = True
while test:
try:
calls_page = client.calls.list(page=current_page, page_size=page_size, started_after=started_after)
for calls in calls_page:
writer.writerow((calls.sid, calls.to, calls.duration, calls.start_time))
current_page += 1
except:
test = False
答
这不是此刻非常有据可查的,但你可以通过列表使用下面的API调用页面:
import twilio.rest
client = twilio.rest.TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
# iterating vars
remaining_messages = client.calls.count()
current_page = 0
page_size = 50 # any number here up to 1000, although paging may be slow...
while remaining_messages > 0:
calls_page = client.calls.list(page=current_page, page_size=page_size)
# do something with the calls_page object...
remaining_messages -= page_size
current_page += 1
你可以通过page
和page_size
参数传递给list()
函数以控制您看到的结果。我今天会更新文档,以使其更加清晰。
不正确的计数是现在已修复的临时API回归。原代码现在应该可以工作。 – amb
嗯,是的,它是固定的,但如果你考虑它返回的是什么,你很可能会看到我遇到的问题。对于一个子帐户,我查询它返回了338452,而我在寻找的特定范围和标准的结果集大约为96000.如果我使用了指定的方法,那么我最终会试图页面化,但通过额外的242,000个条目实际上要求。规定的分页算法只适用于完整的数据集,而不是过滤的算法。让我知道它是否没有意义。谢谢! – Sologoub