3

我正在尝试编写一个简单的脚本来使用 python 帮助程序库从 Twilio 下载呼叫详细信息。到目前为止,似乎我唯一的选择是使用 .iter() 方法来获取对子帐户进行的每次调用。这可能是一个非常大的数字。

如果我使用 .list() 资源,它似乎在任何地方都没有给我页数,所以我不知道要继续分页多长时间才能获得该时间段的所有呼叫。我错过了什么?

以下是带有代码示例的文档: http ://readthedocs.org/docs/twilio-python/en/latest/usage/basics.html

4

2 回答 2

4

目前没有很好的文档记录,但您可以使用以下 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

您可以将参数pagepage_size参数传递给list()函数以控制您看到的结果。我今天将更新文档以使这一点更清楚。

于 2011-12-05T22:30:14.607 回答
1

正如评论中提到的,上面的代码不起作用,因为 remaining_messages = client.calls.count() 总是返回 50,这对于分页绝对没用。

相反,我最终只是尝试下一页直到它失败,这相当hacky。该库应该在列表资源中真正包含 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
于 2011-12-09T23:40:25.427 回答