2018-01-02 18:57:04 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import re as _re
|
|
|
|
import urllib.parse as _urlparse
|
2018-01-05 22:13:30 +01:00
|
|
|
from .__base__ import *
|
2018-01-05 03:31:33 +01:00
|
|
|
from .Link import TextoutLinkTag
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
__all__ = ["TextoutVideoTag"]
|
|
|
|
|
2018-01-19 15:15:35 +01:00
|
|
|
_hexcode = _re.compile('[a-zA-Z0-9_]+')
|
2018-01-02 18:57:04 +01:00
|
|
|
_numcode = _re.compile('^/[0-9]+$')
|
|
|
|
_dailypath = _re.compile('^/video/([a-z0-9]+)$')
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
class TextoutVideoTag(TextoutRawBlockTag):
|
2018-01-02 18:57:04 +01:00
|
|
|
""" The video tag, puts a preview of the video whose URL is given.
|
|
|
|
Only a few 'big' services are supported for now.
|
|
|
|
Example uses:
|
|
|
|
|
|
|
|
[video]video_url[/video]
|
|
|
|
[video tiny]video_url[/video tiny]
|
|
|
|
[video]https://www.youtube.com/watch?v=yhXpV8hRKxQ[/video]
|
|
|
|
"""
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
aliases = ('[video]', '[video tiny]')
|
|
|
|
|
2018-01-20 10:26:23 +01:00
|
|
|
def prepare(self, name, value):
|
|
|
|
""" Prepare the video tag. """
|
|
|
|
|
|
|
|
self.sizeclass = "video-tiny" if "tiny" in self.name \
|
|
|
|
else "video-medium"
|
|
|
|
self.center = False
|
|
|
|
|
2018-01-19 20:56:26 +01:00
|
|
|
def _getvideo(self, url):
|
|
|
|
""" Try to get the video type for preprocessing. """
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-19 21:46:11 +01:00
|
|
|
url = _urlparse.urlparse(url)
|
2018-01-19 20:56:26 +01:00
|
|
|
if not url.scheme in ('http', 'https'):
|
|
|
|
raise Exception
|
|
|
|
|
2018-01-19 15:15:35 +01:00
|
|
|
if url.netloc == "youtu.be":
|
2018-01-02 18:57:04 +01:00
|
|
|
self.id = url.path[1:]
|
|
|
|
if not _hexcode.match(self.id):
|
|
|
|
raise Exception
|
|
|
|
self.type = "youtube"
|
2018-01-19 15:15:35 +01:00
|
|
|
elif url.netloc in ('youtube.com', 'www.youtube.com'):
|
2018-01-02 18:57:04 +01:00
|
|
|
if url.path != '/watch':
|
|
|
|
raise Exception
|
2018-01-19 15:15:35 +01:00
|
|
|
self.id = _urlparse.parse_qs(url.query)['v'][0]
|
|
|
|
if not _hexcode.fullmatch(self.id):
|
2018-01-02 18:57:04 +01:00
|
|
|
raise Exception
|
|
|
|
self.type = "youtube"
|
2018-01-19 15:15:35 +01:00
|
|
|
elif url.netloc in ('dailymotion.com', 'www.dailymotion.com'):
|
2018-01-02 18:57:04 +01:00
|
|
|
self.code = _dailypath.match(url.path).groups()[0]
|
|
|
|
self.type = "dailymotion"
|
2018-01-19 15:15:35 +01:00
|
|
|
elif url.netloc in ('vimeo.com', 'www.vimeo.com'):
|
2018-01-02 18:57:04 +01:00
|
|
|
self.code = url.path[1:]
|
|
|
|
if not _numcode.match(self.code):
|
|
|
|
raise Exception
|
|
|
|
self.type = "vimeo"
|
2018-01-19 21:46:11 +01:00
|
|
|
else:
|
|
|
|
raise Exception
|
2018-01-19 20:56:26 +01:00
|
|
|
|
2018-01-21 22:08:23 +01:00
|
|
|
def preprocess(self, content):
|
2018-01-19 20:56:26 +01:00
|
|
|
try:
|
|
|
|
self._getvideo(content)
|
|
|
|
except:
|
|
|
|
url = _urlparse.urlparse(content)
|
|
|
|
if not url.scheme in ('http', 'https'):
|
|
|
|
raise Exception("No allowed prefix!")
|
|
|
|
self.type = None
|
2018-01-16 13:34:11 +01:00
|
|
|
self.url = content
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-21 22:08:23 +01:00
|
|
|
def content_html(self):
|
2018-01-05 22:13:30 +01:00
|
|
|
""" Produce the embed code for the given type. """
|
|
|
|
|
2018-01-20 10:26:23 +01:00
|
|
|
if not self.type:
|
|
|
|
url = _htmlescape(self.url)
|
|
|
|
return '<a href="{}" target="_blank" rel="noopener">{}</a>' \
|
|
|
|
.format(url, url)
|
|
|
|
|
|
|
|
code = '<div class="video-wrapper {}{}">'.format(self.sizeclass,
|
|
|
|
" video-center" if self.center else "")
|
|
|
|
|
2018-01-02 18:57:04 +01:00
|
|
|
if self.type == "youtube":
|
2018-01-20 10:26:23 +01:00
|
|
|
code += '<iframe ' \
|
2018-01-02 18:57:04 +01:00
|
|
|
'src="https://www.youtube.com/embed/{}" frameborder="0" ' \
|
2018-01-20 10:26:23 +01:00
|
|
|
'allowfullscreen></iframe>'.format(self.id)
|
2018-01-02 18:57:04 +01:00
|
|
|
elif self.type == "dailymotion":
|
2018-01-20 10:26:23 +01:00
|
|
|
code += '<iframe frameborder="0" ' \
|
2018-01-02 18:57:04 +01:00
|
|
|
'src="https://www.dailymotion.com/embed/video/{}">' \
|
2018-01-20 10:26:23 +01:00
|
|
|
'</iframe>'.format(self.code)
|
2018-01-02 18:57:04 +01:00
|
|
|
elif self.type == "vimeo":
|
2018-01-20 10:26:23 +01:00
|
|
|
code += '<iframe src="https://player.vimeo.com/video/{}' \
|
|
|
|
'?title=0&byline=0&portrait=0" frameborder="0" ' \
|
|
|
|
'webkitAllowFullScreen allowFullScreen>' \
|
|
|
|
'</iframe>'.format(self.code)
|
|
|
|
|
|
|
|
return code + '</div>'
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
# End of file.
|