]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/rozhlas.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / rozhlas.py
index 08ebb93e3d47043d4c7d72bb865983abfa4ca112..4a10720ab102d57875cf7a88015834c10d13e878 100644 (file)
@@ -1,5 +1,17 @@
+import itertools
+
 from .common import InfoExtractor
-from ..utils import extract_attributes, int_or_none, remove_start, traverse_obj
+from ..networking.exceptions import HTTPError
+from ..utils import (
+    ExtractorError,
+    extract_attributes,
+    int_or_none,
+    remove_start,
+    str_or_none,
+    traverse_obj,
+    unified_timestamp,
+    url_or_none,
+)
 
 
 class RozhlasIE(InfoExtractor):
@@ -11,8 +23,8 @@ class RozhlasIE(InfoExtractor):
             'id': '3421320',
             'ext': 'mp3',
             'title': 'Echo Pavla Klusáka (30.06.2015 21:00)',
-            'description': 'Osmdesátiny Terryho Rileyho jsou skvělou příležitostí proletět se elektronickými i akustickými díly zakladatatele minimalismu, který je aktivní už přes padesát let'
-        }
+            'description': 'Osmdesátiny Terryho Rileyho jsou skvělou příležitostí proletět se elektronickými i akustickými díly zakladatatele minimalismu, který je aktivní už přes padesát let',
+        },
     }, {
         'url': 'http://prehravac.rozhlas.cz/audio/3421320/embed',
         'only_matching': True,
@@ -22,7 +34,7 @@ def _real_extract(self, url):
         audio_id = self._match_id(url)
 
         webpage = self._download_webpage(
-            'http://prehravac.rozhlas.cz/audio/%s' % audio_id, audio_id)
+            f'http://prehravac.rozhlas.cz/audio/{audio_id}', audio_id)
 
         title = self._html_search_regex(
             r'<h3>(.+?)</h3>\s*<p[^>]*>.*?</p>\s*<div[^>]+id=["\']player-track',
@@ -36,7 +48,7 @@ def _real_extract(self, url):
 
         return {
             'id': audio_id,
-            'url': 'http://media.rozhlas.cz/_audio/%s.mp3' % audio_id,
+            'url': f'http://media.rozhlas.cz/_audio/{audio_id}.mp3',
             'title': title,
             'description': description,
             'duration': duration,
@@ -44,13 +56,46 @@ def _real_extract(self, url):
         }
 
 
-class RozhlasVltavaIE(InfoExtractor):
+class RozhlasBaseIE(InfoExtractor):
+    def _extract_formats(self, entry, audio_id):
+        formats = []
+        for audio in traverse_obj(entry, ('audioLinks', lambda _, v: url_or_none(v['url']))):
+            ext = audio.get('variant')
+            for retry in self.RetryManager():
+                if retry.attempt > 1:
+                    self._sleep(1, audio_id)
+                try:
+                    if ext == 'dash':
+                        formats.extend(self._extract_mpd_formats(
+                            audio['url'], audio_id, mpd_id=ext))
+                    elif ext == 'hls':
+                        formats.extend(self._extract_m3u8_formats(
+                            audio['url'], audio_id, 'm4a', m3u8_id=ext))
+                    else:
+                        formats.append({
+                            'url': audio['url'],
+                            'ext': ext,
+                            'format_id': ext,
+                            'abr': int_or_none(audio.get('bitrate')),
+                            'acodec': ext,
+                            'vcodec': 'none',
+                        })
+                except ExtractorError as e:
+                    if isinstance(e.cause, HTTPError) and e.cause.status == 429:
+                        retry.error = e.cause
+                    else:
+                        self.report_warning(e.msg)
+
+        return formats
+
+
+class RozhlasVltavaIE(RozhlasBaseIE):
     _VALID_URL = r'https?://(?:\w+\.rozhlas|english\.radio)\.cz/[\w-]+-(?P<id>\d+)'
     _TESTS = [{
         'url': 'https://wave.rozhlas.cz/papej-masicko-porcujeme-a-bilancujeme-filmy-a-serialy-ktere-letos-zabily-8891337',
         'md5': 'ba2fdbc1242fc16771c7695d271ec355',
         'info_dict': {
-            'id': 8891337,
+            'id': '8891337',
             'title': 'md5:21f99739d04ab49d8c189ec711eef4ec',
         },
         'playlist_count': 1,
@@ -65,11 +110,11 @@ class RozhlasVltavaIE(InfoExtractor):
                 'artist': 'Aleš Stuchlý',
                 'channel_id': 'radio-wave',
             },
-        }]
+        }],
     }, {
         'url': 'https://wave.rozhlas.cz/poslechnete-si-neklid-podcastovy-thriller-o-vine-strachu-a-vztahu-ktery-zasel-8554744',
         'info_dict': {
-            'id': 8554744,
+            'id': '8554744',
             'title': 'Poslechněte si Neklid. Podcastový thriller o vině, strachu a vztahu, který zašel příliš daleko',
         },
         'playlist_count': 5,
@@ -138,28 +183,44 @@ class RozhlasVltavaIE(InfoExtractor):
                 'chapter': 'Neklid #5',
                 'chapter_number': 5,
             },
-        }]
+        }],
+    }, {
+        'url': 'https://dvojka.rozhlas.cz/karel-siktanc-cerny-jezdec-bily-kun-napinava-pohadka-o-tajemnem-prizraku-8946969',
+        'info_dict': {
+            'id': '8946969',
+            'title': 'Karel Šiktanc: Černý jezdec, bílý kůň. Napínavá pohádka o tajemném přízraku',
+        },
+        'playlist_count': 1,
+        'playlist': [{
+            'info_dict': {
+                'id': '10631121',
+                'ext': 'm4a',
+                'title': 'Karel Šiktanc: Černý jezdec, bílý kůň. Napínavá pohádka o tajemném přízraku',
+                'description': 'Karel Šiktanc: Černý jezdec, bílý kůň',
+                'duration': 2656,
+                'artist': 'Tvůrčí skupina Drama a literatura',
+                'channel_id': 'dvojka',
+            },
+        }],
+        'params': {'skip_download': 'dash'},
     }]
 
     def _extract_video(self, entry):
-        chapter_number = int_or_none(traverse_obj(entry, ('meta', 'ga', 'contentSerialPart')))
+        audio_id = entry['meta']['ga']['contentId']
+        chapter_number = traverse_obj(entry, ('meta', 'ga', 'contentSerialPart', {int_or_none}))
+
         return {
-            'id': entry['meta']['ga']['contentId'],
-            'title': traverse_obj(entry, ('meta', 'ga', 'contentName')),
-            'description': entry.get('title'),
-            'duration': entry.get('duration'),
-            'artist': traverse_obj(entry, ('meta', 'ga', 'contentAuthor')),
-            'channel_id': traverse_obj(entry, ('meta', 'ga', 'contentCreator')),
+            'id': audio_id,
             'chapter': traverse_obj(entry, ('meta', 'ga', 'contentNameShort')) if chapter_number else None,
             'chapter_number': chapter_number,
-            'formats': [{
-                'url': audio_link['url'],
-                'ext': audio_link.get('variant'),
-                'format_id': audio_link.get('variant'),
-                'abr': audio_link.get('bitrate'),
-                'acodec': audio_link.get('variant'),
-                'vcodec': 'none',
-            } for audio_link in entry['audioLinks']],
+            'formats': self._extract_formats(entry, audio_id),
+            **traverse_obj(entry, {
+                'title': ('meta', 'ga', 'contentName'),
+                'description': 'title',
+                'duration': ('duration', {int_or_none}),
+                'artist': ('meta', 'ga', 'contentAuthor'),
+                'channel_id': ('meta', 'ga', 'contentCreator'),
+            }),
         }
 
     def _real_extract(self, url):
@@ -173,7 +234,130 @@ def _real_extract(self, url):
 
         return {
             '_type': 'playlist',
-            'id': data.get('embedId'),
+            'id': str_or_none(data.get('embedId')) or video_id,
             'title': traverse_obj(data, ('series', 'title')),
             'entries': map(self._extract_video, data['playlist']),
         }
+
+
+class MujRozhlasIE(RozhlasBaseIE):
+    _VALID_URL = r'https?://(?:www\.)?mujrozhlas\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
+    _TESTS = [{
+        # single episode extraction
+        'url': 'https://www.mujrozhlas.cz/vykopavky/ach-jo-zase-teleci-rizek-je-mnohem-min-cesky-nez-jsme-si-mysleli',
+        'md5': '6f8fd68663e64936623e67c152a669e0',
+        'info_dict': {
+            'id': '10787730',
+            'ext': 'mp3',
+            'title': 'Ach jo, zase to telecí! Řízek je mnohem míň český, než jsme si mysleli',
+            'description': 'md5:db7141e9caaedc9041ec7cefb9a62908',
+            'timestamp': 1684915200,
+            'modified_timestamp': 1687550432,
+            'series': 'Vykopávky',
+            'thumbnail': 'https://portal.rozhlas.cz/sites/default/files/images/84377046610af6ddc54d910b1dd7a22b.jpg',
+            'channel_id': 'radio-wave',
+            'upload_date': '20230524',
+            'modified_date': '20230623',
+        },
+    }, {
+        # serial extraction
+        'url': 'https://www.mujrozhlas.cz/radiokniha/jaroslava-janackova-pribeh-tajemneho-psani-o-pramenech-genezi-babicky',
+        'playlist_mincount': 7,
+        'info_dict': {
+            'id': 'bb2b5f4e-ffb4-35a6-a34a-046aa62d6f6b',
+            'title': 'Jaroslava Janáčková: Příběh tajemného psaní. O pramenech a genezi Babičky',
+            'description': 'md5:7434d8fac39ac9fee6df098e11dfb1be',
+        },
+    }, {
+        # show extraction
+        'url': 'https://www.mujrozhlas.cz/nespavci',
+        'playlist_mincount': 14,
+        'info_dict': {
+            'id': '09db9b37-d0f4-368c-986a-d3439f741f08',
+            'title': 'Nespavci',
+            'description': 'md5:c430adcbf9e2b9eac88b745881e814dc',
+        },
+    }, {
+        # serialPart
+        'url': 'https://www.mujrozhlas.cz/povidka/gustavo-adolfo-becquer-hora-duchu',
+        'info_dict': {
+            'id': '8889035',
+            'ext': 'm4a',
+            'title': 'Gustavo Adolfo Bécquer: Hora duchů',
+            'description': 'md5:343a15257b376c276e210b78e900ffea',
+            'chapter': 'Hora duchů a Polibek – dva tajemné příběhy Gustava Adolfa Bécquera',
+            'thumbnail': 'https://portal.rozhlas.cz/sites/default/files/images/2adfe1387fb140634be725c1ccf26214.jpg',
+            'timestamp': 1708173000,
+            'episode': 'Episode 1',
+            'episode_number': 1,
+            'series': 'Povídka',
+            'modified_date': '20240217',
+            'upload_date': '20240217',
+            'modified_timestamp': 1708173198,
+            'channel_id': 'vltava',
+        },
+        'params': {'skip_download': 'dash'},
+    }]
+
+    def _call_api(self, path, item_id, msg='API JSON'):
+        return self._download_json(
+            f'https://api.mujrozhlas.cz/{path}/{item_id}', item_id,
+            note=f'Downloading {msg}', errnote=f'Failed to download {msg}')['data']
+
+    def _extract_audio_entry(self, entry):
+        audio_id = entry['meta']['ga']['contentId']
+
+        return {
+            'id': audio_id,
+            'formats': self._extract_formats(entry['attributes'], audio_id),
+            **traverse_obj(entry, {
+                'title': ('attributes', 'title'),
+                'description': ('attributes', 'description'),
+                'episode_number': ('attributes', 'part'),
+                'series': ('attributes', 'mirroredShow', 'title'),
+                'chapter': ('attributes', 'mirroredSerial', 'title'),
+                'artist': ('meta', 'ga', 'contentAuthor'),
+                'channel_id': ('meta', 'ga', 'contentCreator'),
+                'timestamp': ('attributes', 'since', {unified_timestamp}),
+                'modified_timestamp': ('attributes', 'updated', {unified_timestamp}),
+                'thumbnail': ('attributes', 'asset', 'url', {url_or_none}),
+            }),
+        }
+
+    def _entries(self, api_url, playlist_id):
+        for page in itertools.count(1):
+            episodes = self._download_json(
+                api_url, playlist_id, note=f'Downloading episodes page {page}',
+                errnote=f'Failed to download episodes page {page}', fatal=False)
+            for episode in traverse_obj(episodes, ('data', lambda _, v: v['meta']['ga']['contentId'])):
+                yield self._extract_audio_entry(episode)
+            api_url = traverse_obj(episodes, ('links', 'next', {url_or_none}))
+            if not api_url:
+                break
+
+    def _real_extract(self, url):
+        display_id = self._match_id(url)
+        webpage = self._download_webpage(url, display_id)
+        info = self._search_json(r'\bvar\s+dl\s*=', webpage, 'info json', display_id)
+
+        entity = info['siteEntityBundle']
+
+        if entity in ('episode', 'serialPart'):
+            return self._extract_audio_entry(self._call_api(
+                'episodes', info['contentId'], 'episode info API JSON'))
+
+        elif entity in ('show', 'serial'):
+            playlist_id = info['contentShow'].split(':')[0] if entity == 'show' else info['contentId']
+            data = self._call_api(f'{entity}s', playlist_id, f'{entity} playlist JSON')
+            api_url = data['relationships']['episodes']['links']['related']
+            return self.playlist_result(
+                self._entries(api_url, playlist_id), playlist_id,
+                **traverse_obj(data, ('attributes', {
+                    'title': 'title',
+                    'description': 'description',
+                })))
+
+        else:
+            # `entity == 'person'` not implemented yet by API, ref:
+            # https://api.mujrozhlas.cz/persons/8367e456-2a57-379a-91bb-e699619bea49/participation
+            raise ExtractorError(f'Unsupported entity type "{entity}"')