使用Python从Twilio下载媒体文件 [英] Downloading media files from Twilio in Python

查看:118
本文介绍了使用Python从Twilio下载媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载发送到我的Twilio帐户的所有媒体,并且无法终生解决如何访问实际图像的问题.

I'm trying to download all the media that is sent to my Twilio account and cannot for the life of me figure out how to access the actual images.

from twilio.rest import Client
import requests
from operator import itemgetter
import json

ACCOUNT_SID = "xxxxxxx"
AUTH_TOKEN = "xxxxxxxx"

client = Client(ACCOUNT_SID, AUTH_TOKEN)


# builds a list of messages and media uris
messages = client.messages.list(from_="+19999999999")
msgs = []
for m in messages:
    line = [m.from_, m.to, m.body, m.sid, m.subresource_uris['media']]
    line = [str(x) for x in line]
    msgs.append(line)

# with list of all messages:
msgs = sorted(msgs, key=itemgetter(0))
for m in msgs:
    # get media list for each message that has one, else catch exception
    try:
        medias = client.messages(m[3]).media.list()
        # returns Twilio.Api.V2010.MediaInstance and i'm stuck
        for med in medias:
            print client.messages(m[3]).media(med.sid).fetch()
    except Exception as e:
        pass

我只是失去了,无法找到的文档中找到具体的例子.我什至不知道我是否要关闭,还是要离开.预先感谢!

I am just lost and can't find any concrete examples in the documentation. I really can't even tell if I'm close, or waaaaaaaaaaay off. Thanks in advance!

解决方案,感谢philnash 从twilio.rest导入客户端 汇入要求 导入json

SOLUTION Thanks to philnash from twilio.rest import Client import requests import json

# Find these values at https://twilio.com/user/account
ACCOUNT_SID = "xxxxx"
AUTH_TOKEN = "xxxxxx"
BASE_URL = "https://%s:%s@api.twilio.com" % (ACCOUNT_SID, AUTH_TOKEN)

client = Client(ACCOUNT_SID, AUTH_TOKEN)


# with list of all messages:
messages = client.messages.list(from_="+1999999999")
for m in messages:
    sid = m.sid
    # get media list for each message that has one, else catch exception
    try:
        message = client.messages(sid).fetch()
        print message.body
        medias = message.media.list()
        # returns Twilio.Api.V2010.MediaInstance and i'm stuck
        for media in medias:
            media_instance = client.messages(sid).media(media.sid).fetch()
            uri = requests.get(BASE_URL + media_instance.uri).json()
            uri2 = requests.get(BASE_URL + uri['uri'].replace('.json', ''))
            with open(media_instance.uri.split("/")[-1].replace(".json", ".png"), "wb") as f:
                f.write(uri2.content)
                f.close()
    except Exception as e:
        print e

推荐答案

此处是Twilio开发人员的福音.

Twilio developer evangelist here.

从帮助程序库获取Media URI时,它是资源的json表示形式,并以.json结尾.要获取原始资源,您只需要剥离.json扩展名.您可以使用该URL下载图像.

When you get the Media URI from the helper library, it is the json representation of the resource and ends in .json. To get the raw resource you need only to strip the .json extension. You can use that URL to download the image.

这篇关于使用Python从Twilio下载媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆