2

Twilio から録音を移動してから削除するための Python コードをまとめています。スクリプトに役立つオンラインのドキュメントはたくさんありますが、ドキュメントには、認証コードとトークンを 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!")
4

2 に答える 2

0

Twilio エバンジェリストはこちら。

デフォルトでは、 Twilio Python ヘルパー ライブラリTwilioRestClientのオブジェクトは、現在の環境内で指定された環境変数を探します。これは、Windows で環境変数を設定する方法を説明する投稿です。TWILIO_ACCOUNT_SIDTWILIO_AUTH_TOKEN

https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them

アカウント Sid の環境変数を設定するには、コマンド プロンプトを開いて次のように入力します。

c:\>set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXX

資格情報を環境変数に保持することは、それらの資格情報を安全に保ち、誤ってソース管理にチェックインするなどの事態を防ぐのに役立ちます。

環境変数を使用したくない場合は、コンストラクターを介して資格情報をライブラリに直接提供することもできます。

from twilio.rest import TwilioRestClient

ACCOUNT_SID = "AXXXXXXXXXXXXXXXXX"
AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

それが役立つことを願っています。

于 2016-06-10T15:32:13.310 に答える