我正在整理一些 python 代码来移动然后从 Twilio 中删除录音。网上有很多文档可以帮助编写脚本,但文档说我需要将授权代码和令牌添加为 windows 变量。该文档显示了如何到达正确的位置以添加这些变量,但没有准确显示要输入的内容、输入的位置或所需的确切格式。我对这一切都很陌生。在我的 Windows 10 机器上 - 在新的变量窗口中 - 它要求输入“变量名”和“变量值”。我需要确切地知道我输入了什么以及它应该采用的格式。任何帮助将不胜感激。谢谢!
创建此代码的大部分信息来自https://www.twilio.com/blog/2016/05/bulk-delete-your-twilio-recordings-with-python.html
from twilio.rest import TwilioRestClient
import csv
import threading
from queue import Queue
from datetime import date
import os
import requests
from requests.auth import HTTPBasicAuth
# Ensure your environmental variables have these configured
account_sid = "{{ myaccountSID }}"
auth_token = "{{ myToken }}"
# Initialize Twilio Client
client = TwilioRestClient(account_sid, auth_token)
# Create a lock to serialize console output
lock = threading.Lock()
# The work method includes a print statement to indicate progress
def do_work(recording_sid):
client.recordings.delete(recording_sid)
# Make sure the whole print completes or
# threads can mix up output in one line.
with lock:
print(threading.current_thread().name, "has deleted", recording_sid)
def do_work(recording):
data = requests.get(recording.uri, auth=HTTPBasicAuth(),
stream=True)
# Create a .wav file and stream the recording to improve performance.
with open(recording.sid + '.wav', 'wb') as fd:
for chunk in data.iter_content(1):
fd.write(chunk)
client.recordings.delete(recording.sid)
# Make sure the whole print completes or threads
# can mix up output in one line.
with lock:
print(threading.current_thread().name,
"has downloaded to the local folder and "
"has been deleted off Twilio", recording_sid)
que.task_done()
# Create the queue and thread pool.
# The range value controls the number of threads you run.
que = Queue()
for idx in range(20):
thread = threading.Thread(target=worker)
# thread dies when main thread (only non-daemon thread) exits.
thread.daemon = True
thread.start()
# Open up a CSV file to dump the results of deleted recordings into
with open('recordings.csv', 'w') as csvfile:
record_writer = csv.writer(csvfile, delimiter=',')
# Let's create the header row
record_writer.writerow(["Recording SID", "Duration", "Date", "Call SID"])
# You can use a date filter if needed. e.g. before=date(2016, 5, 30)
for recording in client.recordings.iter(before=date(2016, 5, 30)):
record_writer.writerow([recording.sid, recording.duration,
recording.date_updated, recording.call_sid])
que.put(recording)
que.join() # block until all tasks are done
print("All done!")