-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
65 lines (48 loc) · 1.75 KB
/
app.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
57
58
59
60
61
62
63
64
65
import os
from twilio.rest import Client
import requests
from dotenv import load_dotenv
import time
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s - %(levelname)s - %(message)s", datefmt="%d/%m/%Y %H:%M:%S"
)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
start_time = time.time()
load_dotenv()
logger.info("Creating recordings directory")
os.makedirs("recordings", exist_ok=True)
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
if not account_sid or not auth_token:
logger.error("Please provide valid Twilio credentials.")
exit()
client = Client(account_sid, auth_token)
BASE_MEDIA_URL = f"http://api.twilio.com/2010-04-01/Accounts/{account_sid}/Recordings"
recording_list = []
try:
logger.info("Retreiving list of recordings")
recordings = client.recordings.list()
except Exception as e:
logger.error(f"Error retrieving recordings: {str(e)}")
exit()
logger.info("Saving recordings to a List")
for record in recordings:
media_url = f"{BASE_MEDIA_URL}/{record.sid}"
recording_list.append(media_url)
try:
r = requests.get(media_url, auth=(account_sid, auth_token))
r.raise_for_status()
except requests.exceptions.RequestException as req_err:
logger.error(f"Failed to download {record.sid}: {str(req_err)}")
continue
with open(f"recordings/{record.sid}.wav", "wb") as f:
f.write(r.content)
logger.info(f"{record.sid} saved successfully")
end_time = time.time()
execution_time = round(end_time - start_time, 2)
logger.info(f"It took {execution_time} seconds to download all recordings")