-
Notifications
You must be signed in to change notification settings - Fork 2
/
twilio.py
56 lines (38 loc) · 1.2 KB
/
twilio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import csv
import requests
account_sid = ""
auth_pass = ""
results_per_page = 1000
num_pages = 500
start_date = "2010-04-01"
base_url = "https://api.twilio.com"
initial_page = "/" + start_date + "/Accounts/" + account_sid + "/Messages.json?PageSize=" + str(results_per_page)
response = requests.get(base_url + initial_page, auth=(account_sid, auth_pass)).json()
next_page = response['next_page_uri']
page = 0
data = response['messages']
# print response['previous_page_uri']
# print response
if not next_page == None:
while page < num_pages:
response = requests.get(base_url + next_page, auth=(account_sid, auth_pass)).json()
next_page = response['next_page_uri']
print "On page: " + str(page)
data = data + response['messages']
page += 1
if next_page == None:
print "No more pages"
break
csv_file = open('export.csv', 'w')
csvwriter = csv.writer(csv_file)
count = 0
for message in data:
if count == 0:
header = message.keys()
csvwriter.writerow(header)
count += 1
#only include outbound sms because inbound ascii/emojis throws csv writer errors
if message.values()[2] == "outbound-api":
csvwriter.writerow(message.values())
csv_file.close()
print "Done!"