2018-01-02 18:57:04 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-02-19 19:50:26 +01:00
|
|
|
#******************************************************************************
|
|
|
|
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
|
|
# This file is part of the textoutpc project, which is MIT-licensed.
|
|
|
|
#******************************************************************************
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
import re as _re
|
|
|
|
import urllib.parse as _urlparse
|
2018-01-22 20:33:25 +01:00
|
|
|
from html import escape as _htmlescape
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-07-28 19:49:29 +02:00
|
|
|
from .. import BlockTag as _BlockTag
|
2018-07-28 19:36:43 +02:00
|
|
|
|
2018-07-28 19:49:29 +02:00
|
|
|
__all__ = ["VideoTag"]
|
2018-01-02 18:57:04 +01:00
|
|
|
|
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-07-28 19:49:29 +02:00
|
|
|
class VideoTag(_BlockTag):
|
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-24 12:33:33 +01:00
|
|
|
raw = True
|
2018-01-05 22:13:30 +01:00
|
|
|
|
2018-01-20 10:26:23 +01:00
|
|
|
def prepare(self, name, value):
|
|
|
|
""" Prepare the video tag. """
|
|
|
|
|
2018-01-24 12:33:33 +01:00
|
|
|
self._sizeclass = "video-tiny" if "tiny" in name \
|
2018-01-20 10:26:23 +01:00
|
|
|
else "video-medium"
|
2018-01-22 20:33:25 +01:00
|
|
|
self._center = False
|
2018-01-20 10:26:23 +01:00
|
|
|
|
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-22 20:33:25 +01:00
|
|
|
self._id = url.path[1:]
|
|
|
|
if not _hexcode.match(self._id):
|
2018-01-02 18:57:04 +01:00
|
|
|
raise Exception
|
2018-01-22 20:33:25 +01:00
|
|
|
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-22 20:33:25 +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
|
2018-01-22 20:33:25 +01:00
|
|
|
self._type = "youtube"
|
2018-01-19 15:15:35 +01:00
|
|
|
elif url.netloc in ('dailymotion.com', 'www.dailymotion.com'):
|
2018-01-22 20:33:25 +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-22 20:33:25 +01:00
|
|
|
self._code = url.path[1:]
|
|
|
|
if not _numcode.match(self._code):
|
2018-01-02 18:57:04 +01:00
|
|
|
raise Exception
|
2018-01-22 20:33:25 +01:00
|
|
|
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-02-11 12:01:32 +01:00
|
|
|
self._url = 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!")
|
2018-01-22 20:33:25 +01:00
|
|
|
self._type = None
|
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-22 20:33:25 +01:00
|
|
|
if not self._type:
|
|
|
|
url = _htmlescape(self._url)
|
2018-07-27 00:53:59 +02:00
|
|
|
return '<p><a href="{}">{}</a></p>'.format(url, url)
|
2018-01-20 10:26:23 +01:00
|
|
|
|
2018-01-22 20:33:25 +01:00
|
|
|
code = '<div class="video-wrapper {}{}">'.format(self._sizeclass,
|
|
|
|
" video-center" if self._center else "")
|
2018-01-20 10:26:23 +01:00
|
|
|
|
2018-01-22 20:33:25 +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-22 20:33:25 +01:00
|
|
|
'allowfullscreen></iframe>'.format(self._id)
|
|
|
|
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-22 20:33:25 +01:00
|
|
|
'</iframe>'.format(self._code)
|
|
|
|
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>' \
|
2018-01-22 20:33:25 +01:00
|
|
|
'</iframe>'.format(self._code)
|
2018-01-20 10:26:23 +01:00
|
|
|
|
|
|
|
return code + '</div>'
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-02-11 12:01:32 +01:00
|
|
|
def content_lightscript(self):
|
|
|
|
url = self._url.replace('[', '%5B').replace(']', '%5D')
|
|
|
|
return '[[image:{}]]'.format(url)
|
|
|
|
|
2018-01-02 18:57:04 +01:00
|
|
|
# End of file.
|