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

Code before:
            'description': 'md5:9f28e46df86b7eb1efe36a7d1cd46f2c',
            'upload_date': '20221205',
            'timestamp': 1670284296,
        },
    }, {
        'url': 'https://cnnespanol.cnn.com/video/ataque-misil-israel-beirut-libano-octubre-trax',
        'info_dict': {
            'id': 'me484a43722642aa00627b812fe928f2e99c6e2997',
            'ext': 'mp4',
            'display_id': 'video/ataque-misil-israel-beirut-libano-octubre-trax',
            'timestamp': 1729501452,
            'thumbnail': 'https://media.cnn.com/api/v1/images/stellar/prod/ataqeubeirut-1.jpg?c=original',
            'description': 'md5:7f4038bdc8674b17eef45cc2ebba64d0',
            'upload_date': '20241021',
            'duration': 31.0,
            'title': 'VIDEO | Israel lanza un nuevo ataque sobre Beirut',
        },
    }]

    def _real_extract(self, url):
        display_id = self._match_valid_url(url).group('display_id')
        webpage = self._download_webpage(url, display_id)
        json_ld = self._search_json_ld(webpage, display_id, fatal=False) or {}
        media_id = traverse_obj(webpage, (
            {find_element(tag='div', attr='data-component-name', value='video-player', html=True)},
            {extract_attributes}, 'data-media-id'))
        window_env = self._search_json(r'window\.env\s*=', webpage, 'window env', display_id, default={})
        formats = []
        subtitles = {}
        if media_id and (app_id := window_env.get('TOP_AUTH_SERVICE_APP_ID')):
            media_data = self._download_json(
                f'https://medium.ngtv.io/v2/media/{media_id}/desktop', media_id, fatal=False,
                query={'appId': app_id})
            if m3u8_url := traverse_obj(media_data, (
                    'media', 'desktop', 'unprotected', 'unencrypted', 'url', {url_or_none})):
                fmts, subs = self._extract_m3u8_formats_and_subtitles(
                    m3u8_url, media_id, 'mp4', m3u8_id='hls', fatal=False)
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
        if direct_url := traverse_obj(json_ld, ('url', {url_or_none})):
            formats.append({
                'url': direct_url,
                'format_id': 'direct',
                'preference': 1,
            })
        return {
            'id': media_id or display_id,
            'display_id': display_id,
            'formats': formats,
            'subtitles': subtitles,
            **json_ld,

Review comment (refactor):
Similarly, pick the headline video. If there might be no video with `data-canonical-url`, there could be a 2-stage process as above, but the tests pass as is.
```suggestion
        media_id = traverse_obj(webpage, (
            {find_elements(tag='div', attr='data-component-name', value='video-player', html=True)},
            ..., {extract_attributes}, {lambda v: v.get('data-canonical-url') and v}, 'data-media-id', any))
```
(I tested this with code using `get_elements_html_by_attribute()` and `get_all=False`, not having `find_elements()` and `any` in my dev build on this machine ATM, so I hope that'll work). 

Code after:
            'id': 'mef5f52b9e1fe28b1ad192afcbc9206ae984894b68',
            'display_id': '2024/06/11/style/king-charles-portrait-vandalized',
            'ext': 'mp4',
            'thumbnail': 'https://media.cnn.com/api/v1/images/stellar/prod/still-20701257-8846-816-still.jpg?c=original',
            'description': 'md5:19f78338ccec533db0fa8a4511012dae',
            'title': 'Video shows King Charles\' portrait being vandalized by activists',
            'timestamp': 1718113852,
            'upload_date': '20240611',
            'duration': 51.0,
            'modified_timestamp': 1718116193,
            'modified_date': '20240611',
        },
    }, {
        'url': 'https://edition.cnn.com/videos/media/2022/12/05/robin-meade-final-sign-off-broadcast-hln-mxp-contd-vpx.hln',
        'info_dict': {
            'id': 'mefba13799201b084ea3b1d0f7ca820ae94d4bb5b2',
            'display_id': 'videos/media/2022/12/05/robin-meade-final-sign-off-broadcast-hln-mxp-contd-vpx.hln',
            'ext': 'mp4',
            'thumbnail': 'https://media.cnn.com/api/v1/images/stellar/prod/221205163510-robin-meade-sign-off.jpg?c=original',
            'duration': 158.0,
            'title': 'Robin Meade signs off after HLN\'s last broadcast',
            'description': 'md5:cff3c62d18d2fbc6c5c75cb029b7353b',
            'upload_date': '20221205',
            'timestamp': 1670284296,
            'modified_timestamp': 1670332404,
            'modified_date': '20221206',
        },
        'params': {'format': 'direct'},
    }, {
        'url': 'https://cnnespanol.cnn.com/video/ataque-misil-israel-beirut-libano-octubre-trax',
        'info_dict': {
            'id': 'me484a43722642aa00627b812fe928f2e99c6e2997',
            'ext': 'mp4',
            'display_id': 'video/ataque-misil-israel-beirut-libano-octubre-trax',
            'timestamp': 1729501452,
            'thumbnail': 'https://media.cnn.com/api/v1/images/stellar/prod/ataqeubeirut-1.jpg?c=original',
            'description': 'md5:256ee7137d161f776cda429654135e52',
            'upload_date': '20241021',
            'duration': 31.0,
            'title': 'VIDEO | Israel lanza un nuevo ataque sobre Beirut',
            'modified_date': '20241021',
            'modified_timestamp': 1729501530,
        },
    }, {
        'url': 'https://edition.cnn.com/2024/10/16/politics/kamala-harris-fox-news-interview/index.html',
        'info_dict': {
            'id': '2024/10/16/politics/kamala-harris-fox-news-interview',
        },
        'playlist_count': 2,
        'playlist': [{
            'md5': '073ffab87b8bef97c9913e71cc18ef9e',