diff options
author | Thomas "Cakeisalie5" Touhey <thomas@touhey.fr> | 2018-07-29 21:55:18 +0200 |
---|---|---|
committer | Thomas "Cakeisalie5" Touhey <thomas@touhey.fr> | 2018-07-29 21:55:18 +0200 |
commit | 8f7732a07f0ab6306c77d56e600a3cb11f74efa8 (patch) | |
tree | e8bc567333097e7206be0a7445975e543040cebe /textoutpc/builtin/_Video.py | |
parent | 1f80dd67724fb40dd55964457d3e6c8a8524797c (diff) |
Modified the way videos work in textoutpc.
Diffstat (limited to 'textoutpc/builtin/_Video.py')
-rwxr-xr-x | textoutpc/builtin/_Video.py | 61 |
1 files changed, 7 insertions, 54 deletions
diff --git a/textoutpc/builtin/_Video.py b/textoutpc/builtin/_Video.py index 94c199e..4601f53 100755 --- a/textoutpc/builtin/_Video.py +++ b/textoutpc/builtin/_Video.py @@ -4,7 +4,6 @@ # This file is part of the textoutpc project, which is MIT-licensed. #****************************************************************************** -import re as _re import urllib.parse as _urlparse from html import escape as _htmlescape @@ -12,10 +11,6 @@ from .. import BlockTag as _BlockTag __all__ = ["VideoTag"] -_hexcode = _re.compile('[a-zA-Z0-9_-]+') -_numcode = _re.compile('^/[0-9]+$') -_dailypath = _re.compile('^/video/([a-z0-9]+)$') - class VideoTag(_BlockTag): """ The video tag, puts a preview of the video whose URL is given. Only a few 'big' services are supported for now. @@ -72,52 +67,21 @@ class VideoTag(_BlockTag): if fl: self._float = True - def _getvideo(self, url): - """ Try to get the video type for preprocessing. """ - - url = _urlparse.urlparse(url) - if not url.scheme in ('http', 'https'): - raise Exception - - if url.netloc == "youtu.be": - self._id = url.path[1:] - if not _hexcode.match(self._id): - raise Exception - self._type = "youtube" - elif url.netloc in ('youtube.com', 'www.youtube.com'): - if url.path != '/watch': - raise Exception - self._id = _urlparse.parse_qs(url.query)['v'][0] - if not _hexcode.fullmatch(self._id): - raise Exception - self._type = "youtube" - elif url.netloc in ('dailymotion.com', 'www.dailymotion.com'): - self._code = _dailypath.match(url.path).groups()[0] - self._type = "dailymotion" - elif url.netloc in ('vimeo.com', 'www.vimeo.com'): - self._code = url.path[1:] - if not _numcode.match(self._code): - raise Exception - self._type = "vimeo" - else: - raise Exception - def preprocess(self, content): - self._url = content - try: - self._getvideo(content) + self._video = self.video(content) except: url = _urlparse.urlparse(content) if not url.scheme in ('http', 'https'): raise Exception("No allowed prefix!") - self._type = None + + self._video = content def content_html(self): """ Produce the embed code for the given type. """ - if not self._type: - url = _htmlescape(self._url) + if isinstance(self._video, str): + url = _htmlescape(self._video) return '<p><a href="{}">{}</a></p>'.format(url, url) align = "float-" + (self._align or "left") if self._align \ @@ -129,19 +93,8 @@ class VideoTag(_BlockTag): f' style="padding-bottom: {self._ratio}%"' \ if self._ratio != round(9 / 16, 4) else "") - if self._type == "youtube": - code += '<iframe ' \ - 'src="https://www.youtube.com/embed/{}" frameborder="0" ' \ - 'allowfullscreen></iframe>'.format(self._id) - elif self._type == "dailymotion": - code += '<iframe frameborder="0" ' \ - 'src="https://www.dailymotion.com/embed/video/{}">' \ - '</iframe>'.format(self._code) - elif self._type == "vimeo": - code += '<iframe src="https://player.vimeo.com/video/{}' \ - '?title=0&byline=0&portrait=0" frameborder="0" ' \ - 'webkitAllowFullScreen allowFullScreen>' \ - '</iframe>'.format(self._code) + code += '<iframe src="{}" frameborder="0" allowfullscreen>' \ + '</iframe>'.format(self._video.embed()) return code + '</div>' |