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

93 lines
2.7 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re as _re
import urllib.parse as _urlparse
from .__base__ import *
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]+)$')
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]
"""
aliases = ('[video]', '[video tiny]')
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-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"
else:
raise Exception
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
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" ' \
'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>' \
'</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.