Twilio Python helper library - How do you know how many pages list resource returned?

StackOverflow https://stackoverflow.com/questions/8392491

  •  28-10-2019
  •  | 
  •  

Вопрос

I'm trying to write a simple script to download call details information from Twilio using the python helper library. So far, it seems that my only option is to use .iter() method to get every call ever made for the subaccount. This could be a very large number.

If I use the .list() resource, it doesn't seem to give me a page count anywhere, so I don't know for how long to continue paging to get all calls for the time period. What am I missing?

Here are the docs with code samples: http://readthedocs.org/docs/twilio-python/en/latest/usage/basics.html

Это было полезно?

Решение 2

As mentioned in the comment, the above code did not work because remaining_messages = client.calls.count() always returns 50, making it absolutely useless for paging.

Instead, I ended up just trying next page until it fails, which is fairly hacky. The library should really include numpages in the list resource for paging.

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

Другие советы

It's not very well documented at the moment, but you can use the following API calls to page through the list:

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

You can pass in page and page_size arguments to the list() function to control which results you see. I'll update the documentation today to make this more clear.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top