"""Embedding videos and other 3rd party content without oEmbed."""importrefromurllibimportparsefromdjango.utils.htmlimportmark_safe__all__=("embed_youtube","embed_vimeo","embed")YOUTUBE_RE=re.compile(r"""youtu(\.?)be(\.com)?/ # match youtube's domains (\#/)? # for mobile urls (embed/|shorts/|watch/|live/)? # match the embed/shorts/watch url syntax (v/)? (watch\?v=)? # match the youtube page url (ytscreeningroom\?v=)? (feeds/api/videos/)? (user\S*[^\w\-\s])? (?P<code>[\w\-]{11})[a-z0-9;:@?&%=+/\$_.-]* # match and extract """,re.I|re.X,)VIMEO_RE=re.compile(r"""vimeo\.com/(video/)?(channels/(.*/)?)?((.+)/review/)?(?P<code>[0-9]+)""",re.I,)SRF_RE=[re.compile(r)forrin(r"(https?:)?\/\/(www\.)?(srf\.ch\/play|tp\.srgssr\.ch\/p)\/(?P<type>radio|tv).*?id=.*",r"(https?:)?\/\/(www\.)?(srf\.ch\/play|tp\.srgssr\.ch\/p)\/(?P<type>radio|tv)/redirect/detail/(?P<id>[\w\-]+)",r"(https?:)?\/\/(www\.)?(srf\.ch\/play|tp\.srgssr\.ch\/p)\/.*?urn=urn:(?P<business_unit>srf|rtr|rts):(?P<type>.*?):(?P<id>[\w\-]+)",)]
[docs]defembed_youtube(url):""" Return HTML for embedding YouTube videos or ``None``, if argument isn't a YouTube link. The YouTube ``<iframe>`` is wrapped in a ``<div class="responsive-embed widescreen youtube">`` element. """match=YOUTUBE_RE.search(url)ifnotmatch:returnNoned=match.groupdict()returnmark_safe(f'<div class="responsive-embed widescreen youtube">'f'<iframe src="https://www.youtube.com/embed/{d["code"]}?rel=0"'f' frameborder="0" allow="autoplay; fullscreen" allowfullscreen="">'f"</iframe>"f"</div>")
[docs]defembed_vimeo(url):""" Return HTML for embedding Vimeo videos or ``None``, if argument isn't a Vimeo link. The Vimeo ``<iframe>`` is wrapped in a ``<div class="responsive-embed widescreen vimeo">`` element. """match=VIMEO_RE.search(url)ifnotmatch:returnNoned=match.groupdict()returnmark_safe(f'<div class="responsive-embed widescreen vimeo">'f'<iframe src="https://player.vimeo.com/video/{d["code"]}"'f' frameborder="0" allow="autoplay; fullscreen" allowfullscreen="">'f"</iframe>"f"</div>")
defembed_srf(url):""" Return HTML for embedding videos from SRF """try:match=next(filter(None,(r.search(url)forrinSRF_RE)))exceptStopIteration:returnNoned=match.groupdict()params=parse.parse_qs(parse.urlparse(url).query)id_=d.get("id")orparams["id"][0]type_="video"ifd["type"]in("tv","video")else"audio"business_unit=d.get("business_unit","srf")start=p[0]if(p:=params.get("start"))else""returnmark_safe('<div class="responsive-embed widescreen srfplay">'"<iframe "f"src='//srf.ch/play/embed?urn=urn:{business_unit}:{type_}:{id_}&start={start}' "f"allowfullscreen frameborder='0' name='' "'allow="geolocation *; autoplay; '"'encrypted-media\"></iframe>""</div>")_default_handlers=[embed_youtube,embed_vimeo,embed_srf]
[docs]defembed(url,*,handlers=_default_handlers):"""embed(url, *, handlers=[embed_youtube, embed_vimeo, embed_srf]) Run a selection of embedding handlers and return the first value, or ``None`` if URL couldn't be processed by any handler. You could write your own handler converting the URL argument into a plain old anchor element or maybe even :func:`feincms3.plugins.external.oembed_html` if you wanted to fall back to oEmbed. """forhandlerinhandlers:html=handler(url)ifhtmlisnotNone:returnhtml