Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/cnn.py (Python)

Code before:
                'display_id': 'pete-buttigieg-harris-fox-newssrc-digvid',
                'description': 'md5:602a8a7e853ed5e574acd3159428c98e',
                'thumbnail': 'https://media.cnn.com/api/v1/images/stellar/prod/buttigieg-20241017040412074.jpg?c=original',
                'duration': 145.0,
                'timestamp': 1729137765.846,
                'upload_date': '20241017',
                'modified_timestamp': 1729138184.891,
                'modified_date': '20241017',
            },
            'params': {'format': 'direct'},
        }],
    }]

    def _real_extract(self, url):
        display_id = self._match_valid_url(url).group('display_id')
        webpage = self._download_webpage(url, display_id)
        app_id = traverse_obj(
            self._search_json(r'window\.env\s*=', webpage, 'window env', display_id, default={}),
            ('TOP_AUTH_SERVICE_APP_ID', {str}))

        entries = []
        for player_data in traverse_obj(webpage, (
                {find_elements(tag='div', attr='data-component-name', value='video-player', html=True)},
                ..., {extract_attributes}, all, lambda _, v: v['data-media-id'] and v['data-video-resource-parent-uri'])):
            media_id = player_data['data-media-id']
            parent_uri = player_data['data-video-resource-parent-uri']
            formats, subtitles = [], {}

            video_data = self._download_json(
                'https://fave.api.cnn.io/v1/video', media_id,
                query={
                    'id': media_id,
                    'stellarUri': parent_uri,
                })
            for direct_url in traverse_obj(video_data, ('files', ..., 'fileUri', {url_or_none})):
                resolution, bitrate = None, None
                if mobj := re.search(r'-(?P<res>\d+x\d+)_(?P<tbr>\d+)k\.mp4', direct_url):
                    resolution, bitrate = mobj.group('res', 'tbr')
                formats.append({
                    'url': direct_url,
                    'format_id': 'direct',
                    'quality': 1,
                    'tbr': int_or_none(bitrate),
                    **parse_resolution(resolution),
                })
            for sub_data in traverse_obj(video_data, (
                    'closedCaptions', 'types', lambda _, v: url_or_none(v['track']['url']), 'track')):
                subtitles.setdefault(sub_data.get('lang') or 'en', []).append({
                    'url': sub_data['url'],
                    'name': sub_data.get('label'),
                })

Review comment (refactor):
not a necessary change, but maybe we want to remove the filter for `parent_uri` here, since we could still have m3u8 formats with only the `media_id`?
```suggestion
                ..., {extract_attributes}, all, lambda _, v: v['data-media-id'])):
            media_id = player_data['data-media-id']
            parent_uri = player_data.get('data-video-resource-parent-uri')
```

Code after:
                'description': 'md5:602a8a7e853ed5e574acd3159428c98e',
                'thumbnail': 'https://media.cnn.com/api/v1/images/stellar/prod/buttigieg-20241017040412074.jpg?c=original',
                'duration': 145.0,
                'timestamp': 1729137765,
                'upload_date': '20241017',
                'modified_timestamp': 1729138184,
                'modified_date': '20241017',
            },
            'params': {'format': 'direct'},
        }],
    }]

    def _real_extract(self, url):
        display_id = self._match_valid_url(url).group('display_id')
        webpage = self._download_webpage(url, display_id)
        app_id = traverse_obj(
            self._search_json(r'window\.env\s*=', webpage, 'window env', display_id, default={}),
            ('TOP_AUTH_SERVICE_APP_ID', {str}))

        entries = []
        for player_data in traverse_obj(webpage, (
                {find_elements(tag='div', attr='data-component-name', value='video-player', html=True)},
                ..., {extract_attributes}, all, lambda _, v: v['data-media-id'])):
            media_id = player_data['data-media-id']
            parent_uri = player_data.get('data-video-resource-parent-uri')
            formats, subtitles = [], {}

            video_data = {}
            if parent_uri:
                video_data = self._download_json(
                    'https://fave.api.cnn.io/v1/video', media_id, fatal=False,
                    query={
                        'id': media_id,
                        'stellarUri': parent_uri,
                    })
                for direct_url in traverse_obj(video_data, ('files', ..., 'fileUri', {url_or_none})):
                    resolution, bitrate = None, None
                    if mobj := re.search(r'-(?P<res>\d+x\d+)_(?P<tbr>\d+)k\.mp4', direct_url):
                        resolution, bitrate = mobj.group('res', 'tbr')
                    formats.append({
                        'url': direct_url,
                        'format_id': 'direct',
                        'quality': 1,
                        'tbr': int_or_none(bitrate),
                        **parse_resolution(resolution),
                    })
                for sub_data in traverse_obj(video_data, (
                        'closedCaptions', 'types', lambda _, v: url_or_none(v['track']['url']), 'track')):
                    subtitles.setdefault(sub_data.get('lang') or 'en', []).append({
                        'url': sub_data['url'],
                        'name': sub_data.get('label'),