[ie/dropout] Fix extraction (#12102)
[yt-dlp.git] / yt_dlp / extractor / orf.py
blob12c4a210415d8f2999915fe49fbb7ae6b2540e72
1 import base64
2 import re
4 from .common import InfoExtractor
5 from ..utils import (
6 clean_html,
7 determine_ext,
8 float_or_none,
9 int_or_none,
10 make_archive_id,
11 mimetype2ext,
12 orderedSet,
13 parse_age_limit,
14 parse_iso8601,
15 remove_end,
16 str_or_none,
17 strip_jsonp,
18 try_call,
19 unified_strdate,
20 url_or_none,
22 from ..utils.traversal import traverse_obj
25 class ORFRadioIE(InfoExtractor):
26 IE_NAME = 'orf:radio'
28 STATION_INFO = {
29 'fm4': ('fm4', 'fm4', 'orffm4'),
30 'noe': ('noe', 'oe2n', 'orfnoe'),
31 'wien': ('wie', 'oe2w', 'orfwie'),
32 'burgenland': ('bgl', 'oe2b', 'orfbgl'),
33 'ooe': ('ooe', 'oe2o', 'orfooe'),
34 'steiermark': ('stm', 'oe2st', 'orfstm'),
35 'kaernten': ('ktn', 'oe2k', 'orfktn'),
36 'salzburg': ('sbg', 'oe2s', 'orfsbg'),
37 'tirol': ('tir', 'oe2t', 'orftir'),
38 'vorarlberg': ('vbg', 'oe2v', 'orfvbg'),
39 'oe3': ('oe3', 'oe3', 'orfoe3'),
40 'oe1': ('oe1', 'oe1', 'orfoe1'),
42 _STATION_RE = '|'.join(map(re.escape, STATION_INFO.keys()))
44 _VALID_URL = rf'''(?x)
45 https?://(?:
46 (?P<station>{_STATION_RE})\.orf\.at/player|
47 radiothek\.orf\.at/(?P<station2>{_STATION_RE})
48 )/(?P<date>[0-9]+)/(?P<show>\w+)'''
50 _TESTS = [{
51 'url': 'https://radiothek.orf.at/ooe/20220801/OGMO',
52 'info_dict': {
53 'id': 'OGMO',
54 'title': 'Guten Morgen OÖ',
55 'description': 'md5:a3f6083399ef92b8cbe2d421b180835a',
57 'playlist': [{
58 'md5': 'f33147d954a326e338ea52572c2810e8',
59 'info_dict': {
60 'id': '2022-08-01_0459_tl_66_7DaysMon1_319062',
61 'ext': 'mp3',
62 'title': 'Guten Morgen OÖ',
63 'upload_date': '20220801',
64 'duration': 18000,
65 'timestamp': 1659322789,
66 'description': 'md5:a3f6083399ef92b8cbe2d421b180835a',
68 }],
69 }, {
70 'url': 'https://ooe.orf.at/player/20220801/OGMO',
71 'info_dict': {
72 'id': 'OGMO',
73 'title': 'Guten Morgen OÖ',
74 'description': 'md5:a3f6083399ef92b8cbe2d421b180835a',
76 'playlist': [{
77 'md5': 'f33147d954a326e338ea52572c2810e8',
78 'info_dict': {
79 'id': '2022-08-01_0459_tl_66_7DaysMon1_319062',
80 'ext': 'mp3',
81 'title': 'Guten Morgen OÖ',
82 'upload_date': '20220801',
83 'duration': 18000,
84 'timestamp': 1659322789,
85 'description': 'md5:a3f6083399ef92b8cbe2d421b180835a',
87 }],
88 }, {
89 'url': 'http://fm4.orf.at/player/20170107/4CC',
90 'only_matching': True,
91 }, {
92 'url': 'https://noe.orf.at/player/20200423/NGM',
93 'only_matching': True,
94 }, {
95 'url': 'https://wien.orf.at/player/20200423/WGUM',
96 'only_matching': True,
97 }, {
98 'url': 'https://burgenland.orf.at/player/20200423/BGM',
99 'only_matching': True,
100 }, {
101 'url': 'https://steiermark.orf.at/player/20200423/STGMS',
102 'only_matching': True,
103 }, {
104 'url': 'https://kaernten.orf.at/player/20200423/KGUMO',
105 'only_matching': True,
106 }, {
107 'url': 'https://salzburg.orf.at/player/20200423/SGUM',
108 'only_matching': True,
109 }, {
110 'url': 'https://tirol.orf.at/player/20200423/TGUMO',
111 'only_matching': True,
112 }, {
113 'url': 'https://vorarlberg.orf.at/player/20200423/VGUM',
114 'only_matching': True,
115 }, {
116 'url': 'https://oe3.orf.at/player/20200424/3WEK',
117 'only_matching': True,
118 }, {
119 'url': 'http://oe1.orf.at/player/20170108/456544',
120 'md5': '34d8a6e67ea888293741c86a099b745b',
121 'info_dict': {
122 'id': '2017-01-08_0759_tl_51_7DaysSun6_256141',
123 'ext': 'mp3',
124 'title': 'Morgenjournal',
125 'duration': 609,
126 'timestamp': 1483858796,
127 'upload_date': '20170108',
129 'skip': 'Shows from ORF radios are only available for 7 days.',
132 def _entries(self, data, station):
133 _, loop_station, old_ie = self.STATION_INFO[station]
134 for info in data['streams']:
135 item_id = info.get('loopStreamId')
136 if not item_id:
137 continue
138 video_id = item_id.replace('.mp3', '')
139 yield {
140 'id': video_id,
141 'ext': 'mp3',
142 'url': f'https://loopstream01.apa.at/?channel={loop_station}&id={item_id}',
143 '_old_archive_ids': [make_archive_id(old_ie, video_id)],
144 'title': data.get('title'),
145 'description': clean_html(data.get('subtitle')),
146 'duration': try_call(lambda: (info['end'] - info['start']) / 1000),
147 'timestamp': int_or_none(info.get('start'), scale=1000),
148 'series': data.get('programTitle'),
151 def _real_extract(self, url):
152 station, station2, show_date, show_id = self._match_valid_url(url).group('station', 'station2', 'date', 'show')
153 api_station, _, _ = self.STATION_INFO[station or station2]
154 data = self._download_json(
155 f'http://audioapi.orf.at/{api_station}/api/json/current/broadcast/{show_id}/{show_date}', show_id)
157 return self.playlist_result(
158 self._entries(data, station or station2), show_id, data.get('title'), clean_html(data.get('subtitle')))
161 class ORFPodcastIE(InfoExtractor):
162 IE_NAME = 'orf:podcast'
163 _STATION_RE = '|'.join(map(re.escape, (
164 'bgl', 'fm4', 'ktn', 'noe', 'oe1', 'oe3',
165 'ooe', 'sbg', 'stm', 'tir', 'tv', 'vbg', 'wie')))
166 _VALID_URL = rf'https?://sound\.orf\.at/podcast/(?P<station>{_STATION_RE})/(?P<show>[\w-]+)/(?P<id>[\w-]+)'
167 _TESTS = [{
168 'url': 'https://sound.orf.at/podcast/oe3/fruehstueck-bei-mir/nicolas-stockhammer-15102023',
169 'md5': '526a5700e03d271a1505386a8721ab9b',
170 'info_dict': {
171 'id': 'nicolas-stockhammer-15102023',
172 'ext': 'mp3',
173 'title': 'Nicolas Stockhammer (15.10.2023)',
174 'duration': 3396.0,
175 'series': 'Frühstück bei mir',
177 'skip': 'ORF podcasts are only available for a limited time',
180 def _real_extract(self, url):
181 station, show, show_id = self._match_valid_url(url).group('station', 'show', 'id')
182 data = self._download_json(
183 f'https://audioapi.orf.at/radiothek/api/2.0/podcast/{station}/{show}/{show_id}', show_id)
185 return {
186 'id': show_id,
187 'ext': 'mp3',
188 'vcodec': 'none',
189 **traverse_obj(data, ('payload', {
190 'url': ('enclosures', 0, 'url'),
191 'ext': ('enclosures', 0, 'type', {mimetype2ext}),
192 'title': 'title',
193 'description': ('description', {clean_html}),
194 'duration': ('duration', {float_or_none(scale=1000)}),
195 'series': ('podcast', 'title'),
196 })),
200 class ORFIPTVIE(InfoExtractor):
201 IE_NAME = 'orf:iptv'
202 IE_DESC = 'iptv.ORF.at'
203 _VALID_URL = r'https?://iptv\.orf\.at/(?:#/)?stories/(?P<id>\d+)'
205 _TEST = {
206 'url': 'http://iptv.orf.at/stories/2275236/',
207 'md5': 'c8b22af4718a4b4af58342529453e3e5',
208 'info_dict': {
209 'id': '350612',
210 'ext': 'flv',
211 'title': 'Weitere Evakuierungen um Vulkan Calbuco',
212 'description': 'md5:d689c959bdbcf04efeddedbf2299d633',
213 'duration': 68.197,
214 'thumbnail': r're:^https?://.*\.jpg$',
215 'upload_date': '20150425',
219 def _real_extract(self, url):
220 story_id = self._match_id(url)
222 webpage = self._download_webpage(
223 f'http://iptv.orf.at/stories/{story_id}', story_id)
225 video_id = self._search_regex(
226 r'data-video(?:id)?="(\d+)"', webpage, 'video id')
228 data = self._download_json(
229 f'http://bits.orf.at/filehandler/static-api/json/current/data.json?file={video_id}',
230 video_id)[0]
232 duration = float_or_none(data['duration'], 1000)
234 video = data['sources']['default']
235 load_balancer_url = video['loadBalancerUrl']
236 abr = int_or_none(video.get('audioBitrate'))
237 vbr = int_or_none(video.get('bitrate'))
238 fps = int_or_none(video.get('videoFps'))
239 width = int_or_none(video.get('videoWidth'))
240 height = int_or_none(video.get('videoHeight'))
241 thumbnail = video.get('preview')
243 rendition = self._download_json(
244 load_balancer_url, video_id, transform_source=strip_jsonp)
246 f = {
247 'abr': abr,
248 'vbr': vbr,
249 'fps': fps,
250 'width': width,
251 'height': height,
254 formats = []
255 for format_id, format_url in rendition['redirect'].items():
256 if format_id == 'rtmp':
257 ff = f.copy()
258 ff.update({
259 'url': format_url,
260 'format_id': format_id,
262 formats.append(ff)
263 elif determine_ext(format_url) == 'f4m':
264 formats.extend(self._extract_f4m_formats(
265 format_url, video_id, f4m_id=format_id))
266 elif determine_ext(format_url) == 'm3u8':
267 formats.extend(self._extract_m3u8_formats(
268 format_url, video_id, 'mp4', m3u8_id=format_id))
269 else:
270 continue
272 title = remove_end(self._og_search_title(webpage), ' - iptv.ORF.at')
273 description = self._og_search_description(webpage)
274 upload_date = unified_strdate(self._html_search_meta(
275 'dc.date', webpage, 'upload date'))
277 return {
278 'id': video_id,
279 'title': title,
280 'description': description,
281 'duration': duration,
282 'thumbnail': thumbnail,
283 'upload_date': upload_date,
284 'formats': formats,
288 class ORFFM4StoryIE(InfoExtractor):
289 IE_NAME = 'orf:fm4:story'
290 IE_DESC = 'fm4.orf.at stories'
291 _VALID_URL = r'https?://fm4\.orf\.at/stories/(?P<id>\d+)'
293 _TEST = {
294 'url': 'http://fm4.orf.at/stories/2865738/',
295 'playlist': [{
296 'md5': 'e1c2c706c45c7b34cf478bbf409907ca',
297 'info_dict': {
298 'id': '547792',
299 'ext': 'flv',
300 'title': 'Manu Delago und Inner Tongue live',
301 'description': 'Manu Delago und Inner Tongue haben bei der FM4 Soundpark Session live alles gegeben. Hier gibt es Fotos und die gesamte Session als Video.',
302 'duration': 1748.52,
303 'thumbnail': r're:^https?://.*\.jpg$',
304 'upload_date': '20170913',
306 }, {
307 'md5': 'c6dd2179731f86f4f55a7b49899d515f',
308 'info_dict': {
309 'id': '547798',
310 'ext': 'flv',
311 'title': 'Manu Delago und Inner Tongue live (2)',
312 'duration': 1504.08,
313 'thumbnail': r're:^https?://.*\.jpg$',
314 'upload_date': '20170913',
315 'description': 'Manu Delago und Inner Tongue haben bei der FM4 Soundpark Session live alles gegeben. Hier gibt es Fotos und die gesamte Session als Video.',
320 def _real_extract(self, url):
321 story_id = self._match_id(url)
322 webpage = self._download_webpage(url, story_id)
324 entries = []
325 all_ids = orderedSet(re.findall(r'data-video(?:id)?="(\d+)"', webpage))
326 for idx, video_id in enumerate(all_ids):
327 data = self._download_json(
328 f'http://bits.orf.at/filehandler/static-api/json/current/data.json?file={video_id}',
329 video_id)[0]
331 duration = float_or_none(data['duration'], 1000)
333 video = data['sources']['q8c']
334 load_balancer_url = video['loadBalancerUrl']
335 abr = int_or_none(video.get('audioBitrate'))
336 vbr = int_or_none(video.get('bitrate'))
337 fps = int_or_none(video.get('videoFps'))
338 width = int_or_none(video.get('videoWidth'))
339 height = int_or_none(video.get('videoHeight'))
340 thumbnail = video.get('preview')
342 rendition = self._download_json(
343 load_balancer_url, video_id, transform_source=strip_jsonp)
345 f = {
346 'abr': abr,
347 'vbr': vbr,
348 'fps': fps,
349 'width': width,
350 'height': height,
353 formats = []
354 for format_id, format_url in rendition['redirect'].items():
355 if format_id == 'rtmp':
356 ff = f.copy()
357 ff.update({
358 'url': format_url,
359 'format_id': format_id,
361 formats.append(ff)
362 elif determine_ext(format_url) == 'f4m':
363 formats.extend(self._extract_f4m_formats(
364 format_url, video_id, f4m_id=format_id))
365 elif determine_ext(format_url) == 'm3u8':
366 formats.extend(self._extract_m3u8_formats(
367 format_url, video_id, 'mp4', m3u8_id=format_id))
368 else:
369 continue
371 title = remove_end(self._og_search_title(webpage), ' - fm4.ORF.at')
372 if idx >= 1:
373 # Titles are duplicates, make them unique
374 title += ' (' + str(idx + 1) + ')'
375 description = self._og_search_description(webpage)
376 upload_date = unified_strdate(self._html_search_meta(
377 'dc.date', webpage, 'upload date'))
379 entries.append({
380 'id': video_id,
381 'title': title,
382 'description': description,
383 'duration': duration,
384 'thumbnail': thumbnail,
385 'upload_date': upload_date,
386 'formats': formats,
389 return self.playlist_result(entries)
392 class ORFONIE(InfoExtractor):
393 IE_NAME = 'orf:on'
394 _VALID_URL = r'https?://on\.orf\.at/video/(?P<id>\d+)(?:/(?P<segment>\d+))?'
395 _TESTS = [{
396 'url': 'https://on.orf.at/video/14210000/school-of-champions-48',
397 'info_dict': {
398 'id': '14210000',
399 'ext': 'mp4',
400 'duration': 2651.08,
401 'thumbnail': 'https://api-tvthek.orf.at/assets/segments/0167/98/thumb_16697671_segments_highlight_teaser.jpeg',
402 'title': 'School of Champions (4/8)',
403 'description': 'md5:d09ad279fc2e8502611e7648484b6afd',
404 'media_type': 'episode',
405 'timestamp': 1706558922,
406 'upload_date': '20240129',
407 'release_timestamp': 1706472362,
408 'release_date': '20240128',
409 'modified_timestamp': 1712756663,
410 'modified_date': '20240410',
411 '_old_archive_ids': ['orftvthek 14210000'],
413 }, {
414 'url': 'https://on.orf.at/video/3220355',
415 'md5': 'f94d98e667cf9a3851317efb4e136662',
416 'info_dict': {
417 'id': '3220355',
418 'ext': 'mp4',
419 'duration': 445.04,
420 'thumbnail': 'https://api-tvthek.orf.at/assets/segments/0002/60/thumb_159573_segments_highlight_teaser.png',
421 'title': '50 Jahre Burgenland: Der Festumzug',
422 'description': 'md5:1560bf855119544ee8c4fa5376a2a6b0',
423 'media_type': 'episode',
424 'timestamp': 52916400,
425 'upload_date': '19710905',
426 'release_timestamp': 52916400,
427 'release_date': '19710905',
428 'modified_timestamp': 1498536049,
429 'modified_date': '20170627',
430 '_old_archive_ids': ['orftvthek 3220355'],
432 }, {
433 # Video with multiple segments selecting the second segment
434 'url': 'https://on.orf.at/video/14226549/15639808/jugendbande-einbrueche-aus-langeweile',
435 'md5': '90f4ebff86b4580837b8a361d0232a9e',
436 'info_dict': {
437 'id': '15639808',
438 'ext': 'mp4',
439 'duration': 97.707,
440 'thumbnail': 'https://api-tvthek.orf.at/assets/segments/0175/43/thumb_17442704_segments_highlight_teaser.jpg',
441 'title': 'Jugendbande: Einbrüche aus Langeweile',
442 'description': 'md5:193df0bf0d91cf16830c211078097120',
443 'media_type': 'segment',
444 'timestamp': 1715792400,
445 'upload_date': '20240515',
446 'modified_timestamp': 1715794394,
447 'modified_date': '20240515',
448 '_old_archive_ids': ['orftvthek 15639808'],
450 'params': {'noplaylist': True},
451 }, {
452 # Video with multiple segments and no combined version
453 'url': 'https://on.orf.at/video/14227864/formel-1-grosser-preis-von-monaco-2024',
454 'info_dict': {
455 '_type': 'multi_video',
456 'id': '14227864',
457 'duration': 18410.52,
458 'thumbnail': 'https://api-tvthek.orf.at/assets/segments/0176/04/thumb_17503881_segments_highlight_teaser.jpg',
459 'title': 'Formel 1: Großer Preis von Monaco 2024',
460 'description': 'md5:aeeb010710ccf70ce28ccb4482243d4f',
461 'media_type': 'episode',
462 'timestamp': 1716721200,
463 'upload_date': '20240526',
464 'release_timestamp': 1716721802,
465 'release_date': '20240526',
466 'modified_timestamp': 1716967501,
467 'modified_date': '20240529',
469 'playlist_count': 42,
470 }, {
471 # Video with multiple segments, but with combined version
472 'url': 'https://on.orf.at/video/14228172',
473 'info_dict': {
474 'id': '14228172',
475 'ext': 'mp4',
476 'duration': 3294.878,
477 'thumbnail': 'https://api-tvthek.orf.at/assets/segments/0176/17/thumb_17516455_segments_highlight_teaser.jpg',
478 'title': 'Willkommen Österreich mit Stermann & Grissemann',
479 'description': 'md5:5de034d033a9c27f989343be3bbd4839',
480 'media_type': 'episode',
481 'timestamp': 1716926584,
482 'upload_date': '20240528',
483 'release_timestamp': 1716919202,
484 'release_date': '20240528',
485 'modified_timestamp': 1716968045,
486 'modified_date': '20240529',
487 '_old_archive_ids': ['orftvthek 14228172'],
491 @staticmethod
492 def _parse_metadata(api_json):
493 return traverse_obj(api_json, {
494 'id': ('id', {int}, {str_or_none}),
495 'age_limit': ('age_classification', {parse_age_limit}),
496 'duration': ('exact_duration', {float_or_none(scale=1000)}),
497 'title': (('title', 'headline'), {str}),
498 'description': (('description', 'teaser_text'), {str}),
499 'media_type': ('video_type', {str}),
500 'thumbnail': ('_embedded', 'image', 'public_urls', 'highlight_teaser', 'url', {url_or_none}),
501 'timestamp': (('date', 'episode_date'), {parse_iso8601}),
502 'release_timestamp': ('release_date', {parse_iso8601}),
503 'modified_timestamp': ('updated_at', {parse_iso8601}),
504 }, get_all=False)
506 def _extract_video_info(self, video_id, api_json):
507 formats, subtitles = [], {}
508 for manifest_type in traverse_obj(api_json, ('sources', {dict.keys}, ...)):
509 for manifest_url in traverse_obj(api_json, ('sources', manifest_type, ..., 'src', {url_or_none})):
510 if manifest_type == 'hls':
511 fmts, subs = self._extract_m3u8_formats_and_subtitles(
512 manifest_url, video_id, fatal=False, m3u8_id='hls')
513 elif manifest_type == 'dash':
514 fmts, subs = self._extract_mpd_formats_and_subtitles(
515 manifest_url, video_id, fatal=False, mpd_id='dash')
516 else:
517 continue
518 formats.extend(fmts)
519 self._merge_subtitles(subs, target=subtitles)
521 for sub_url in traverse_obj(api_json, (
522 '_embedded', 'subtitle',
523 ('xml_url', 'sami_url', 'stl_url', 'ttml_url', 'srt_url', 'vtt_url'), {url_or_none})):
524 self._merge_subtitles({'de': [{'url': sub_url}]}, target=subtitles)
526 return {
527 'id': video_id,
528 'formats': formats,
529 'subtitles': subtitles,
530 '_old_archive_ids': [make_archive_id('ORFTVthek', video_id)],
531 **self._parse_metadata(api_json),
534 def _real_extract(self, url):
535 video_id, segment_id = self._match_valid_url(url).group('id', 'segment')
537 encrypted_id = base64.b64encode(f'3dSlfek03nsLKdj4Jsd{video_id}'.encode()).decode()
538 api_json = self._download_json(
539 f'https://api-tvthek.orf.at/api/v4.3/public/episode/encrypted/{encrypted_id}', video_id)
541 if traverse_obj(api_json, 'is_drm_protected'):
542 self.report_drm(video_id)
544 segments = traverse_obj(api_json, ('_embedded', 'segments', lambda _, v: v['id']))
545 selected_segment = traverse_obj(segments, (lambda _, v: str(v['id']) == segment_id, any))
547 # selected_segment will be falsy if input URL did not include a valid segment_id
548 if selected_segment and not self._yes_playlist(video_id, segment_id, playlist_label='episode', video_label='segment'):
549 return self._extract_video_info(segment_id, selected_segment)
551 # Even some segmented videos have an unsegmented version available in API response root
552 if (self._configuration_arg('prefer_segments_playlist')
553 or not traverse_obj(api_json, ('sources', ..., ..., 'src', {url_or_none}))):
554 return self.playlist_result(
555 (self._extract_video_info(str(segment['id']), segment) for segment in segments),
556 video_id, **self._parse_metadata(api_json), multi_video=True)
558 return self._extract_video_info(video_id, api_json)