从给定的url生成诸如youtube或vimeo之类的服务的嵌入(html)代码。规则在插件中指定。

vishap的Python项目详细描述


从给定的url生成诸如youtube或vimeo之类的服务的嵌入(html)代码。 规则在插件中指定。

提供以下服务的插件(按字母顺序列出):

  • 维梅奥
  • YouTube

安装

使用来自pypi的最新稳定版本安装:

$ pip install vishap

或者从BitBucket安装最新的稳定版本:

$ pip install -e hg+https://bitbucket.org/barseghyanartur/vishap@stable#egg=vishap

或者从github安装最新的稳定版本:

$ pip install -e git+http://github.org/barseghyanartur/vishap@stable#egg=vishap

就这些。有关详细信息,请参见Usage and examples部分。

用法和示例

简单用法

必需的导入

fromvishapimportrender_video

渲染vimeo代码

以下代码:

printrender_video('http://vimeo.com/45655450',500,281)

将产生以下输出:

<iframesrc="//player.vimeo.com/video/45655450"width="500"height="281"frameborder="0"webkitallowfullscreenmozallowfullscreenallowfullscreen></iframe>

呈现youtube代码

以下代码:

printrender_video('http://www.youtube.com/watch?v=LIPl7PtGXNI',560,315)

将产生以下输出:

<iframewidth="560"height="315"src="//www.youtube.com/embed/LIPl7PtGXNI"frameborder="0"allowfullscreen></iframe>

填充可用区域

如果希望嵌入代码填充可用区域,请在 widthheight属性,并将其放在容器中 会伸展。

示例:

s="""<div class="video-wrapper">
{embed_code}
</div>""".format(embed_code=render_video('http://www.youtube.com/watch?v=LIPl7PtGXNI'))

然后将产生以下输出:

<iframesrc="//www.youtube.com/embed/LIPl7PtGXNI"frameborder="0"allowfullscreen></iframe>

然后,您的css文件应该类似于以下内容

.video-wrapper{width:600px;height:500px;padding:0;}.video-wrapperiframe{position:absolute;width:100%;height:100%;}

注册自定义插件

classExamplePlugin(BaseVideoPlugin):uid="example"name="Example"url_pattern="^(?P<prefix>(http\:\/\/www\.youtube\.com\/watch\?v=)|(http\:\/\/www\.youtube\.com\/v\/)|(http\:\/\/youtu\.be\/))(?P<value>[A-Za-z0-9\-=_]{11})"id_pattern="^(?P<value>[A-Za-z0-9\-=_]{11})"thumbnail_pattern="//img.youtube.com/vi/{0}/{1}.jpg"embed_code="""
    <iframe src="//www.youtube.com/embed/{video_id}" {options} frameborder="0" allowfullscreen></iframe>
    """plugin_registry.register(ExamplePlugin)
< H3>替换现有插件

如果你想替换/更新现有的插件,你可以强制注册它 登记处。

classUpdatedYoutubePlugin(BaseVideoPlugin):uid="youtube"name="Youtube"url_pattern="^(?P<prefix>(http\:\/\/www\.youtube\.com\/watch\?v=)|(http\:\/\/www\.youtube\.com\/v\/)|(http\:\/\/youtu\.be\/))(?P<value>[A-Za-z0-9\-=_]{11})"id_pattern="^(?P<value>[A-Za-z0-9\-=_]{11})"thumbnail_pattern="//img.youtube.com/vi/{0}/{1}.jpg"embed_code="""
    <iframe src="//www.youtube.com/embed/{video_id}" {options} frameborder="0" allowfullscreen></iframe>
    """plugin_registry.register(UpdatedYoutubePlugin,force=True)

django集成

安装

vishap.contrib.apps.django.vishap添加到INSTALLED_APPS

INSTALLED_APPS=(# ...'vishap.contrib.apps.django.vishap',# ...)

使用量

在您的模板中:

示例1:

{% load vishap_tags %}
{% render_video 'http://www.youtube.com/watch?v=LIPl7PtGXNI' 560 315 as rendered_video_example_1 %}
{{ rendered_video_example_1|safe }}

示例2:

{% load vishap_tags %}
{% with video_url='http://vimeo.com/41055612' video_width='500' video_height='281' %}
{% render_video video_url video_width video_height as rendered_video_example_2 %}
{{ rendered_video_example_2|safe }}
{% endwith %}

缺少插件?

缺少您喜爱的服务的插件?对项目的贡献 制作一个,它将出现在一个新的版本(将发布非常 或者请求一个特性。

许可证

gpl 2.0/lgpl 2.1

支架

如有任何问题,请通过Author部分中给出的电子邮件与我联系。

作者

艺术酒吧<;artur.barseghyan@gmail.com>;

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用于批量操作的java RESTful API分块响应   java读取在线存储的文本文件   在Java ME中将双精度舍入到小数点后5位   java查找一个数字的最接近因子   java更改JMenuBar的字体   java Kmeans聚类算法运行时间和复杂性   java是否可以阻止try catch返回null   java内容解析器指向具有正确URI的错误表   java Android Kotlin插装测试未被识别为插装测试   java TestNG@Dataprovider   在forloop和print语句中声明变量时发生java错误   java在Android Studio 3中设置JNI