-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwilio_message_export.py
More file actions
367 lines (293 loc) · 11.8 KB
/
twilio_message_export.py
File metadata and controls
367 lines (293 loc) · 11.8 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
"""
Twilio Message Bulk Export Script
Exports the last 30 days of message records from your Twilio account
and saves them to a single JSONL (JSON Lines) file.
Usage:
1. Set environment variables:
export TWILIO_ACCOUNT_SID="your_account_sid"
export TWILIO_AUTH_TOKEN="your_auth_token"
2. Run the script:
python twilio_message_export.py
Output:
- messages_export_YYYY-MM-DD_to_YYYY-MM-DD.jsonl
"""
import os
import sys
import json
import gzip
import time
from datetime import datetime, timedelta, timezone
from io import BytesIO
# Check for required packages before importing
try:
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
except ImportError:
print("Error: twilio package not installed.")
print("Please run: pip install twilio")
sys.exit(1)
try:
import requests
except ImportError:
print("Error: requests package not installed.")
print("Please run: pip install requests")
sys.exit(1)
# Configuration
POLL_INTERVAL_SECONDS = 30
MAX_POLL_ATTEMPTS = 240 # 2 hours max wait time
DOWNLOAD_RETRIES = 3
DOWNLOAD_RETRY_DELAY = 5
def get_twilio_client():
"""Initialize and return Twilio client."""
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
if not account_sid or not auth_token:
print("=" * 60)
print("ERROR: Missing Twilio credentials")
print("=" * 60)
print()
print("Please set the following environment variables:")
print()
print(" export TWILIO_ACCOUNT_SID='ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'")
print(" export TWILIO_AUTH_TOKEN='your_auth_token'")
print()
print("You can find these at: https://console.twilio.com/")
print()
sys.exit(1)
# Validate SID format
if not account_sid.startswith("AC") or len(account_sid) != 34:
print("ERROR: TWILIO_ACCOUNT_SID appears invalid.")
print(f" Got: {account_sid[:10]}...")
print(" Expected format: ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (34 characters)")
sys.exit(1)
return Client(account_sid, auth_token), account_sid, auth_token
def create_export_job(client, start_day, end_day):
"""Create a bulk export job for the specified date range."""
print(f"Creating export job for {start_day} to {end_day}...")
try:
job = client.bulkexports.v1.exports("Messages").export_custom_jobs.create(
start_day=start_day,
end_day=end_day,
friendly_name=f"Message Export {start_day} to {end_day}"
)
print(f" Job created successfully: {job.job_sid}")
return job.job_sid
except TwilioRestException as e:
print(f"ERROR: Failed to create export job")
print(f" Status: {e.status}")
print(f" Message: {e.msg}")
if e.status == 401:
print(" -> Check your TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN")
elif e.status == 429:
print(" -> Rate limited. You may have exceeded the 366 days/day limit.")
print(" -> Wait until the next UTC day and try again.")
sys.exit(1)
def get_job_status(client, job_sid):
"""Fetch and parse job status. Returns (is_complete, is_failed, status_summary)."""
try:
job = client.bulkexports.v1.exports.jobs(job_sid).fetch()
except TwilioRestException as e:
print(f" Warning: Failed to fetch job status: {e.msg}")
return False, False, "Unknown"
details = job.details or []
# Build status summary
status_counts = {}
for detail in details:
if isinstance(detail, dict):
status = detail.get("status", "Unknown")
count = detail.get("count", 0)
status_counts[status] = status_counts.get(status, 0) + count
# Determine state
is_failed = "Failed" in status_counts
total_days = sum(status_counts.values())
completed = status_counts.get("Completed", 0) + status_counts.get("CompletedEmptyRecords", 0)
is_complete = total_days > 0 and completed == total_days
# Format summary
summary_parts = [f"{status}: {count}" for status, count in status_counts.items()]
summary = ", ".join(summary_parts) if summary_parts else "Pending"
# Add queue info if available
if job.job_queue_position and job.job_queue_position != "0":
summary += f" | Queue position: {job.job_queue_position}"
return is_complete, is_failed, summary
def wait_for_job_completion(client, job_sid):
"""Poll the job status until it completes or fails."""
print("Waiting for export job to complete...")
print(" (This may take several minutes for large date ranges)")
print()
for attempt in range(MAX_POLL_ATTEMPTS):
is_complete, is_failed, summary = get_job_status(client, job_sid)
timestamp = datetime.now().strftime("%H:%M:%S")
print(f" [{timestamp}] {summary}")
if is_failed:
print()
print("ERROR: Export job failed.")
print(" This can happen due to temporary Twilio issues.")
print(" Please try running the script again.")
sys.exit(1)
if is_complete:
print()
print(" Export job completed successfully!")
return
time.sleep(POLL_INTERVAL_SECONDS)
print()
print("ERROR: Timed out waiting for export job to complete.")
print(f" Job SID: {job_sid}")
print(" The job may still be processing. You can check status at:")
print(" https://console.twilio.com/")
sys.exit(1)
def download_day_file(account_sid, auth_token, day_date):
"""Download and decompress a single day's export file with retries."""
url = f"https://bulkexports.twilio.com/v1/Exports/Messages/Days/{day_date}"
for attempt in range(DOWNLOAD_RETRIES):
try:
# Fetch the day resource to get redirect URL
response = requests.get(url, auth=(account_sid, auth_token), timeout=30)
if response.status_code == 404:
# No data for this day
return [], None
response.raise_for_status()
data = response.json()
redirect_url = data.get("redirect_to")
if not redirect_url:
# No data file available
return [], None
# Download the gzipped file
file_response = requests.get(redirect_url, timeout=120)
file_response.raise_for_status()
# Decompress and parse
with gzip.GzipFile(fileobj=BytesIO(file_response.content)) as f:
content = f.read().decode("utf-8")
# Parse JSON lines
records = []
for line in content.strip().split("\n"):
if line.strip():
try:
records.append(json.loads(line))
except json.JSONDecodeError:
continue # Skip malformed lines
return records, None
except requests.exceptions.RequestException as e:
error = str(e)
if attempt < DOWNLOAD_RETRIES - 1:
time.sleep(DOWNLOAD_RETRY_DELAY)
continue
return [], error
except gzip.BadGzipFile as e:
return [], f"Invalid gzip file: {e}"
except Exception as e:
return [], str(e)
return [], "Max retries exceeded"
def download_all_days(account_sid, auth_token, start_day, end_day, output_file):
"""Download all day files and stream directly to output file."""
print("Downloading and saving message records...")
print()
# Generate list of dates
start = datetime.strptime(start_day, "%Y-%m-%d")
end = datetime.strptime(end_day, "%Y-%m-%d")
total_records = 0
failed_days = []
with open(output_file, "w", encoding="utf-8") as f:
current = start
while current <= end:
day_str = current.strftime("%Y-%m-%d")
records, error = download_day_file(account_sid, auth_token, day_str)
if error:
print(f" {day_str}: ERROR - {error}")
failed_days.append(day_str)
elif records:
print(f" {day_str}: {len(records):,} messages")
for record in records:
f.write(json.dumps(record) + "\n")
total_records += len(records)
else:
print(f" {day_str}: 0 messages")
current += timedelta(days=1)
return total_records, failed_days
def main():
print()
print("=" * 60)
print(" Twilio Message Bulk Export")
print("=" * 60)
print()
# Calculate date range
# End day must be at least 2 days before current UTC day (Twilio requirement)
today = datetime.now(timezone.utc).date()
end_date = today - timedelta(days=2)
start_date = end_date - timedelta(days=29) # 30 days inclusive
end_day = end_date.strftime("%Y-%m-%d")
start_day = start_date.strftime("%Y-%m-%d")
print(f" Date range: {start_day} to {end_day} (30 days)")
print(f" Current UTC date: {today}")
print()
# Initialize client
client, account_sid, auth_token = get_twilio_client()
# Test authentication
print("Verifying Twilio credentials...")
try:
client.api.accounts(account_sid).fetch()
print(" Credentials verified successfully!")
print()
except TwilioRestException as e:
print(f"ERROR: Authentication failed")
print(f" {e.msg}")
print()
print("Please verify your TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN")
sys.exit(1)
# Create export job
job_sid = create_export_job(client, start_day, end_day)
print()
# Wait for completion
wait_for_job_completion(client, job_sid)
print()
# Prepare output file
output_file = f"messages_export_{start_day}_to_{end_day}.jsonl"
# Download all day files (streaming to disk to avoid memory issues)
total_records, failed_days = download_all_days(
account_sid, auth_token, start_day, end_day, output_file
)
# Summary
print()
print("=" * 60)
if failed_days:
print(" COMPLETED WITH WARNINGS")
print("=" * 60)
print()
print(f" Total messages exported: {total_records:,}")
print(f" Output file: {output_file}")
print()
print(f" WARNING: Failed to download {len(failed_days)} day(s):")
for day in failed_days:
print(f" - {day}")
print()
print(" You may want to re-run the script to retry failed days.")
elif total_records == 0:
print(" COMPLETED - NO MESSAGES FOUND")
print("=" * 60)
print()
print(f" No messages found in the date range {start_day} to {end_day}")
print()
# Remove empty file
if os.path.exists(output_file):
os.remove(output_file)
else:
print(" SUCCESS")
print("=" * 60)
print()
print(f" Total messages exported: {total_records:,}")
print(f" Output file: {output_file}")
print()
# Show file size
file_size = os.path.getsize(output_file)
if file_size > 1_000_000_000:
size_str = f"{file_size / 1_000_000_000:.2f} GB"
elif file_size > 1_000_000:
size_str = f"{file_size / 1_000_000:.2f} MB"
elif file_size > 1_000:
size_str = f"{file_size / 1_000:.2f} KB"
else:
size_str = f"{file_size} bytes"
print(f" File size: {size_str}")
print()
if __name__ == "__main__":
main()