]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/mediastream.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / mediastream.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 clean_html,
6 filter_dict,
7 parse_qs,
8 remove_end,
9 traverse_obj,
10 update_url_query,
11 urljoin,
12 )
13
14
15 class MediaStreamBaseIE(InfoExtractor):
16 _EMBED_BASE_URL = 'https://mdstrm.com/embed'
17 _BASE_URL_RE = r'https?://mdstrm\.com/(?:embed|live-stream)'
18
19 def _extract_mediastream_urls(self, webpage):
20 yield from traverse_obj(list(self._yield_json_ld(webpage, None, fatal=False)), (
21 lambda _, v: v['@type'] == 'VideoObject', ('embedUrl', 'contentUrl'),
22 {lambda x: x if re.match(rf'{self._BASE_URL_RE}/\w+', x) else None}))
23
24 for mobj in re.finditer(r'<script[^>]+>[^>]*playerMdStream\.mdstreamVideo\(\s*[\'"](?P<video_id>\w+)', webpage):
25 yield f'{self._EMBED_BASE_URL}/{mobj.group("video_id")}'
26
27 yield from re.findall(
28 rf'<iframe[^>]+\bsrc="({self._BASE_URL_RE}/\w+)', webpage)
29
30 for mobj in re.finditer(
31 r'''(?x)
32 <(?:div|ps-mediastream)[^>]+
33 (class="[^"]*MediaStreamVideoPlayer)[^"]*"[^>]+
34 data-video-id="(?P<video_id>\w+)"
35 (?:\s*data-video-type="(?P<video_type>[^"]+))?
36 (?:[^>]*>\s*<div[^>]+\1[^"]*"[^>]+data-mediastream=["\'][^>]+
37 https://mdstrm\.com/(?P<live>live-stream))?
38 ''', webpage):
39
40 video_type = 'live-stream' if mobj.group('video_type') == 'live' or mobj.group('live') else 'embed'
41 yield f'https://mdstrm.com/{video_type}/{mobj.group("video_id")}'
42
43
44 class MediaStreamIE(MediaStreamBaseIE):
45 _VALID_URL = MediaStreamBaseIE._BASE_URL_RE + r'/(?P<id>\w+)'
46
47 _TESTS = [{
48 'url': 'https://mdstrm.com/embed/6318e3f1d1d316083ae48831',
49 'md5': '97b4f2634b8e8612cc574dfcd504df05',
50 'info_dict': {
51 'id': '6318e3f1d1d316083ae48831',
52 'title': 'Video: Así fue el despido de Thomas Tuchel del Chelsea',
53 'description': 'md5:358ce1e1396010d50a1ece1be3633c95',
54 'thumbnail': r're:^https?://[^?#]+6318e3f1d1d316083ae48831',
55 'ext': 'mp4',
56 },
57 'params': {'skip_download': 'm3u8'},
58 }]
59
60 _WEBPAGE_TESTS = [{
61 'url': 'https://www.multimedios.com/video/costa-rica-tv-en-vivo/v2616',
62 'info_dict': {
63 'id': '5a7b1e63a8da282c34d65445',
64 'title': 're:mmtv-costarica',
65 'description': 'mmtv-costarica',
66 'thumbnail': 're:^https?://[^?#]+5a7b1e63a8da282c34d65445',
67 'ext': 'mp4',
68 'live_status': 'is_live',
69 },
70 'params': {'skip_download': 'Livestream'},
71 }, {
72 'url': 'https://www.multimedios.com/television/clases-de-llaves-y-castigos-quien-sabe-mas',
73 'md5': 'de31f0b1ecc321fb35bf22d58734ea40',
74 'info_dict': {
75 'id': '63731bab8ec9b308a2c9ed28',
76 'title': 'Clases de llaves y castigos ¿Quién sabe más?',
77 'description': 'md5:1b49aa1ee5a4b32fbd66104b2d629e9d',
78 'thumbnail': 're:^https?://[^?#]+63731bab8ec9b308a2c9ed28',
79 'ext': 'mp4',
80 },
81 'params': {'skip_download': 'm3u8'},
82 }, {
83 'url': 'https://www.americatv.com.pe/videos/esto-es-guerra/facundo-gonzalez-sufrio-fuerte-golpe-durante-competencia-frente-hugo-garcia-eeg-noticia-139120',
84 'info_dict': {
85 'id': '63756df1c638b008a5659dec',
86 'title': 'Facundo González sufrió fuerte golpe durante competencia frente a Hugo García en EEG',
87 'description': 'md5:9490c034264afd756eef7b2c3adee69e',
88 'thumbnail': 're:^https?://[^?#]+63756df1c638b008a5659dec',
89 'ext': 'mp4',
90 },
91 'params': {'skip_download': 'm3u8'},
92 }, {
93 'url': 'https://www.americatv.com.pe/videos/al-fondo-hay-sitio/nuevas-lomas-town-bernardo-mata-se-enfrento-sujeto-luchar-amor-macarena-noticia-139083',
94 'info_dict': {
95 'id': '637307669609130f74cd3a6e',
96 'title': 'Las Nuevas Lomas Town: Bernardo De La Mata se enfrentó a sujeto para luchar por el amor de Macarena',
97 'description': 'md5:60d71772f1e1496923539ae58aa17124',
98 'thumbnail': 're:^https?://[^?#]+637307669609130f74cd3a6e',
99 'ext': 'mp4',
100 },
101 'params': {'skip_download': 'm3u8'},
102 }]
103
104 def _extract_from_webpage(self, url, webpage):
105 for embed_url in self._extract_mediastream_urls(webpage):
106 yield self.url_result(embed_url, MediaStreamIE, None)
107
108 def _real_extract(self, url):
109 video_id = self._match_id(url)
110 webpage = self._download_webpage(url, video_id)
111
112 for message in [
113 'Debido a tu ubicación no puedes ver el contenido',
114 'You are not allowed to watch this video: Geo Fencing Restriction',
115 'Este contenido no está disponible en tu zona geográfica.',
116 'El contenido sólo está disponible dentro de',
117 ]:
118 if message in webpage:
119 self.raise_geo_restricted()
120
121 player_config = self._search_json(r'window\.MDSTRM\.OPTIONS\s*=', webpage, 'metadata', video_id)
122
123 formats, subtitles = [], {}
124 for video_format in player_config['src']:
125 if video_format == 'hls':
126 params = {
127 'at': 'web-app',
128 'access_token': traverse_obj(parse_qs(url), ('access_token', 0)),
129 }
130 for name, key in (('MDSTRMUID', 'uid'), ('MDSTRMSID', 'sid'), ('MDSTRMPID', 'pid'), ('VERSION', 'av')):
131 params[key] = self._search_regex(
132 rf'window\.{name}\s*=\s*["\']([^"\']+)["\'];', webpage, key, default=None)
133
134 fmts, subs = self._extract_m3u8_formats_and_subtitles(
135 update_url_query(player_config['src'][video_format], filter_dict(params)), video_id)
136 formats.extend(fmts)
137 self._merge_subtitles(subs, target=subtitles)
138 elif video_format == 'mpd':
139 fmts, subs = self._extract_mpd_formats_and_subtitles(player_config['src'][video_format], video_id)
140 formats.extend(fmts)
141 self._merge_subtitles(subs, target=subtitles)
142 else:
143 formats.append({
144 'url': player_config['src'][video_format],
145 })
146
147 return {
148 'id': video_id,
149 'title': self._og_search_title(webpage) or player_config.get('title'),
150 'description': self._og_search_description(webpage),
151 'formats': formats,
152 'subtitles': subtitles,
153 'is_live': player_config.get('type') == 'live',
154 'thumbnail': self._og_search_thumbnail(webpage),
155 }
156
157
158 class WinSportsVideoIE(MediaStreamBaseIE):
159 _VALID_URL = r'https?://www\.winsports\.co/videos/(?P<id>[\w-]+)'
160
161 _TESTS = [{
162 'url': 'https://www.winsports.co/videos/siempre-castellanos-gran-atajada-del-portero-cardenal-para-evitar-la-caida-de-su-arco-60536',
163 'info_dict': {
164 'id': '62dc8357162c4b0821fcfb3c',
165 'display_id': 'siempre-castellanos-gran-atajada-del-portero-cardenal-para-evitar-la-caida-de-su-arco-60536',
166 'title': '¡Siempre Castellanos! Gran atajada del portero \'cardenal\' para evitar la caída de su arco',
167 'description': 'md5:eb811b2b2882bdc59431732c06b905f2',
168 'thumbnail': r're:^https?://[^?#]+62dc8357162c4b0821fcfb3c',
169 'ext': 'mp4',
170 },
171 'params': {'skip_download': 'm3u8'},
172 }, {
173 'url': 'https://www.winsports.co/videos/observa-aqui-los-goles-del-empate-entre-tolima-y-nacional-60548',
174 'info_dict': {
175 'id': '62dcb875ef12a5526790b552',
176 'display_id': 'observa-aqui-los-goles-del-empate-entre-tolima-y-nacional-60548',
177 'title': 'Observa aquí los goles del empate entre Tolima y Nacional',
178 'description': 'md5:b19402ba6e46558b93fd24b873eea9c9',
179 'thumbnail': r're:^https?://[^?#]+62dcb875ef12a5526790b552',
180 'ext': 'mp4',
181 },
182 'params': {'skip_download': 'm3u8'},
183 }, {
184 'url': 'https://www.winsports.co/videos/equidad-vuelve-defender-su-arco-de-remates-de-junior',
185 'info_dict': {
186 'id': '63fa7eca72f1741ad3a4d515',
187 'display_id': 'equidad-vuelve-defender-su-arco-de-remates-de-junior',
188 'title': '⚽ Equidad vuelve a defender su arco de remates de Junior',
189 'description': 'Remate de Sierra',
190 'thumbnail': r're:^https?://[^?#]+63fa7eca72f1741ad3a4d515',
191 'ext': 'mp4',
192 },
193 'params': {'skip_download': 'm3u8'},
194 }, {
195 'url': 'https://www.winsports.co/videos/bucaramanga-se-quedo-con-el-grito-de-gol-en-la-garganta',
196 'info_dict': {
197 'id': '6402adb62bbf3b18d454e1b0',
198 'display_id': 'bucaramanga-se-quedo-con-el-grito-de-gol-en-la-garganta',
199 'title': '⚽Bucaramanga se quedó con el grito de gol en la garganta',
200 'description': 'Gol anulado Bucaramanga',
201 'thumbnail': r're:^https?://[^?#]+6402adb62bbf3b18d454e1b0',
202 'ext': 'mp4',
203 },
204 'params': {'skip_download': 'm3u8'},
205 }]
206
207 def _real_extract(self, url):
208 display_id = self._match_id(url)
209 webpage = self._download_webpage(url, display_id)
210 data = self._search_json(
211 r'<script\s*[^>]+data-drupal-selector="drupal-settings-json">', webpage, 'data', display_id)
212
213 mediastream_url = urljoin(f'{self._EMBED_BASE_URL}/', (
214 traverse_obj(data, (
215 (('settings', 'mediastream_formatter', ..., 'mediastream_id'), 'url'), {str}), get_all=False)
216 or next(self._extract_mediastream_urls(webpage), None)))
217
218 if not mediastream_url:
219 self.raise_no_formats('No MediaStream embed found in webpage')
220
221 title = clean_html(remove_end(
222 self._search_json_ld(webpage, display_id, expected_type='VideoObject', default={}).get('title')
223 or self._og_search_title(webpage), '| Win Sports'))
224
225 return self.url_result(
226 mediastream_url, MediaStreamIE, display_id, url_transparent=True, display_id=display_id, video_title=title)