Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for embedding SRF external contents #42

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion feincms3/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import re
from urllib import parse

from django.utils.html import mark_safe

Expand All @@ -27,6 +28,14 @@
r"""vimeo\.com/(video/)?(channels/(.*/)?)?((.+)/review/)?(?P<code>[0-9]+)""",
re.I,
)
SRF_RE = [
re.compile(r)
for r in (
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\-]+)",
)
]


def embed_youtube(url):
Expand Down Expand Up @@ -71,7 +80,32 @@ def embed_vimeo(url):
)


_default_handlers = [embed_youtube, embed_vimeo]
def embed_srf(url):
try:
match = next(filter(None, (r.search(url) for r in SRF_RE)))
except StopIteration:
return None

d = match.groupdict()
params = parse.parse_qs(parse.urlparse(url).query)

id_ = d.get("id") or params["id"][0]
type_ = "video" if d["type"] in ("tv", "video") else "audio"
business_unit = d.get("business_unit", "srf")

start = p[0] if (p := params.get("start")) else ""
return mark_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]


def embed(url, *, handlers=_default_handlers):
Expand Down