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-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
|
|
|
|
|
|
|
def preprocess(self, cin):
|
|
|
|
self.w, self.h = (470, 300) if "tiny" in self.name else (560, 340)
|
|
|
|
|
|
|
|
content = cin.read()
|
|
|
|
|
|
|
|
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-05 22:13:30 +01:00
|
|
|
def process_html(self):
|
|
|
|
""" Produce the embed code for the given type. """
|
|
|
|
|
2018-01-02 18:57:04 +01:00
|
|
|
if self.type == "youtube":
|
|
|
|
return '<iframe width="{}" height="{}" ' \
|
|
|
|
'src="https://www.youtube.com/embed/{}" frameborder="0" ' \
|
2018-01-19 21:46:11 +01:00
|
|
|
'allowfullscreen></iframe>'.format(self.w, self.h, self.id)
|
2018-01-02 18:57:04 +01:00
|
|
|
elif self.type == "dailymotion":
|
|
|
|
return '<iframe frameborder="0" width="{}" height="{}" ' \
|
|
|
|
'src="https://www.dailymotion.com/embed/video/{}">' \
|
|
|
|
'</iframe>'.format(self.w, self.h, self.code)
|
|
|
|
elif self.type == "vimeo":
|
|
|
|
return '<iframe src="https://player.vimeo.com/video/{}' \
|
|
|
|
'?title=0&byline=0&portrait=0" width="{}" height="{}" ' \
|
|
|
|
'frameborder="0" webkitAllowFullScreen allowFullScreen>' \
|
2018-01-19 21:46:11 +01:00
|
|
|
'</iframe>'.format(self.code, self.w, self.h)
|
2018-01-02 18:57:04 +01:00
|
|
|
else:
|
2018-01-19 22:44:43 +01:00
|
|
|
url = _htmlescape(self.url)
|
2018-01-16 13:34:11 +01:00
|
|
|
return '<a href="{}" target="_blank" rel="noopener">{}</a>' \
|
2018-01-19 22:44:43 +01:00
|
|
|
.format(url, url)
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
# End of file.
|