跳转到主要内容

从给定的URL生成类似YouTube或Vimeo等服务嵌入(HTML)代码。规则在插件中指定。

项目描述

从给定的URL生成类似YouTube或Vimeo等服务嵌入(HTML)代码。规则在插件中指定。

附带以下服务的插件(按字母顺序列出)

  • Vimeo

  • 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

这就完成了。有关更多信息,请参阅使用示例部分。

使用示例

简单使用

必需导入

from vishap import render_video

渲染Vimeo代码

以下代码

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

将产生以下输出

<iframe src="//player.vimeo.com/video/45655450" width="500" height="281"
frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe>

渲染YouTube代码

以下代码

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

将产生以下输出

<iframe width="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')
)

然后会产生以下输出

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

您的CSS文件应类似于以下内容

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

注册自定义插件

class ExamplePlugin(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)

替换现有插件

如果您想替换/更新现有插件,您可以在注册表中强制注册它。

class UpdatedYoutubePlugin(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

支持

如有任何问题,请联系作者部分的电子邮件。

作者

Artur Barseghyan <artur.barseghyan@gmail.com>

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

vishap-0.1.5.tar.gz (29.6 kB 查看哈希值)

上传时间 源代码

构建发行版

vishap-0.1.5-py2.py3-none-any.whl (19.6 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持