-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathembedding.py
More file actions
128 lines (106 loc) · 3.9 KB
/
Copy pathembedding.py
File metadata and controls
128 lines (106 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Embedding videos and other 3rd party content without oEmbed.
"""
import re
from urllib import parse
from django.utils.html import mark_safe
__all__ = ("embed", "embed_vimeo", "embed_youtube")
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.IGNORECASE | re.VERBOSE,
)
VIMEO_RE = re.compile(
r"""vimeo\.com/(video/)?(channels/(.*/)?)?((.+)/review/)?(?P<code>[0-9]+)""",
re.IGNORECASE,
)
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):
"""
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)
if not match:
return None
d = match.groupdict()
return mark_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" '
f'referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="">'
f"</iframe>"
f"</div>"
)
def embed_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)
if not match:
return None
d = match.groupdict()
return mark_safe(
f'<div class="responsive-embed widescreen vimeo">'
f'<iframe src="https://player.vimeo.com/video/{d["code"]}?dnt=1"'
f' frameborder="0" allow="autoplay; fullscreen" allowfullscreen="">'
f"</iframe>"
f"</div>"
)
def embed_srf(url):
"""
Return HTML for embedding videos from SRF
"""
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):
"""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.
"""
for handler in handlers:
html = handler(url)
if html is not None:
return html