2
0
Fork 0
textout/textoutpc/builtin_tags/Video.py

110 lines
3.3 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
#******************************************************************************
# 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
2018-05-24 21:57:42 +02:00
from ..tags import TextoutBlockTag as _TextoutBlockTag
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
__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-02-19 20:13:10 +01:00
class TextoutVideoTag(_TextoutBlockTag):
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]
"""
aliases = ('[video]', '[video tiny]')
raw = True
2018-01-20 10:26:23 +01:00
def prepare(self, name, value):
""" Prepare the video tag. """
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
def _getvideo(self, url):
""" Try to get the video type for preprocessing. """
2018-01-02 18:57:04 +01:00
url = _urlparse.urlparse(url)
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"
else:
raise Exception
def preprocess(self, content):
self._url = content
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
def content_html(self):
""" 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
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.