用于oembed的python库,支持自动发现和手动包含的提供程序。
pyoembed的Python项目详细描述
用于oembed的python库,支持自动发现和手动 包括供应商。
安装
$ pip install pyoembed
用法
库提供了一个函数,应该调用该函数来自动嵌入 url内容,称为oEmbed:
frompyoembedimportoEmbed,PyOembedExceptiontry:# maxwidth and maxheight are optional.data=oEmbed('http://www.youtube.com/watch?v=_PEdPBEpQfY',maxwidth=640,maxheight=480)exceptPyOembedException,e:print'An error was ocurred: %s'%e# data is a dict with keys that will depends on the media type. You should# choose how to display the content based on the data['type'] value and# the oEmbed spec ( http://oembed.com/ ).ifdata['type']=='video':printdata['html']# and it goes... Someday we will provide default renderes for each media# type.
新增供应商
pyoembed试图覆盖主要的oembed提供者,并且应该为任何 支持oEmbed自动发现的提供程序,但是 可能还不支持收藏夹提供程序:(
要添加它,请在pyoembed.providers包中创建一个文件,基于 以下示例:
frompyoembed.providersimportBaseProviderclassInstagramProvider(BaseProvider):# priority of the provider. Increase it if the provider isn't well knownpriority=10# url schemas that are supported by the provider.# re.compile'd regex are allowed.oembed_schemas=['http://instagram.com/p/*','http://instagr.am/p/*']# api endpoint that answers to oEmbed requests for the provider.oembed_endpoint='http://api.instagram.com/oembed'
您还需要向 integration_test.py文件,运行它以查看是否一切正常。
如果一切正常,请发送请求以添加提供商:
https://github.com/rafaelmartins/pyoembed/pulls
就这样!