From 638e5c91951a7f5b3f64c537dbfa800afe93eb3c Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Mon, 30 Jul 2018 14:29:17 +0200 Subject: [PATCH] Added OpenWebVideo as a demonstration. --- textoutpc/builtin/_Video.py | 32 +++++++++++++++++--------------- textoutpc/builtin/_Videos.py | 30 ++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/textoutpc/builtin/_Video.py b/textoutpc/builtin/_Video.py index be58e5d..c57a454 100755 --- a/textoutpc/builtin/_Video.py +++ b/textoutpc/builtin/_Video.py @@ -13,7 +13,6 @@ __all__ = ["VideoTag", "YoutubeTag"] _defaultratio_w = 16 _defaultratio_h = 9 -_defaultratio = round(_defaultratio_w / _defaultratio_h, 4) class VideoTag(_BlockTag): """ The video tag, puts a preview of the video whose URL is given. @@ -49,21 +48,23 @@ class VideoTag(_BlockTag): else None self._align = None self._float = False - self._ratio = _defaultratio + self._ratio = None for arg in map(str.strip, (value or "").split('|')): if not arg: pass elif arg[0] in '0123456789:': rx, ry = _defaultratio_w, _defaultratio_h + rn = 0 rat = arg.split(':') - try: rx = int(rat[0]) + try: rx = int(rat[0]); rn += 1 except: pass - try: ry = int(rat[1]) + try: ry = int(rat[1]); rn += 1 except: pass - self._ratio = round(ry / rx, 4) + if rn: + self._ratio = round(ry / rx, 4) elif arg in _align: al, fl = _align[arg] if al != None: @@ -91,23 +92,24 @@ class VideoTag(_BlockTag): align = "float-" + (self._align or "left") if self._align \ else self._align - ratio = self._ratio - if ratio == _defaultratio: - ratio = None + if self._ratio: + ratio = self._ratio * 100 + elif hasattr(self._video, 'ratio'): + ratio = self._video.ratio * 100 else: - ratio *= 100 - iratio = int(ratio) - if ratio == iratio: - ratio = iratio - ratio = str(ratio) + ratio = round(_defaultratio_w / _defaultratio_h, 4) + iratio = int(ratio) + if ratio == iratio: + ratio = iratio + ratio = str(ratio) code = '
' \ .format(f" {self._sizeclass}" if self._sizeclass else "", f' img-{align}' if align else "", - f' style="padding-bottom: {ratio}%"' if ratio else "") + f' style="padding-bottom: {ratio}%"') code += ''.format(self._video.embed()) + ''.format(self._video.embed) return code + '
' diff --git a/textoutpc/builtin/_Videos.py b/textoutpc/builtin/_Videos.py index efe13d0..6b7cfbe 100644 --- a/textoutpc/builtin/_Videos.py +++ b/textoutpc/builtin/_Videos.py @@ -34,8 +34,7 @@ class YouTubeVideo(_Video): else: raise ValueError("unknown URL") - def embed(self): - return f"https://www.youtube.com/embed/{self._id}" + self.embed = f"https://www.youtube.com/embed/{self._id}" class DailymotionVideo(_Video): """ Get a video from Dailymotion. """ @@ -52,8 +51,7 @@ class DailymotionVideo(_Video): else: raise ValueError("unknown URL") - def embed(self): - return f"https://www.dailymotion.com/embed/video/{self._code}" + self.embed = f"https://www.dailymotion.com/embed/video/{self._code}" class VimeoVideo(_Video): """ Get a video from Vimeo. """ @@ -72,8 +70,28 @@ class VimeoVideo(_Video): else: raise ValueError("unknown URL") - def embed(self): - return f"https://player.vimeo.com/video/{self._code}" \ + self.embed = f"https://player.vimeo.com/video/{self._code}" \ "?title=0&byline=0&portrait=0" +# WARNING: This is only for demonstration sake. Do not use without a cache! +# This demonstration class uses the `embed-python` module. +# +#from embed import Embed as _Embed +# +#class OpenWebVideo(_Video): +# """ Decentralized way to gather a video data. """ +# +# def __init__(self, url): +# u = _urlparse.urlparse(url) +# if not u.scheme in ('https'): +# raise Exception +# +# embed = _Embed(url) +# embed = embed.embed +# assert embed['type'] == 'video' +# +# self.embed = embed['url'] +# if 'ratio' in embed: +# self.ratio = embed['ratio'] / 100 + # End of file.