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

Code before:
        art_json = self._search_json(
            r'window\.__preloadedData\s*=', webpage, 'media details', page_id,
            transform_source=lambda x: x.replace('undefined', 'null'))['initialData']['data']['article']

        common_info = {
            'title': remove_end(self._html_extract_title(webpage), ' - The New York Times'),
            'description': traverse_obj(art_json, (
                'sprinkledBody', 'content', ..., 'summary', 'content', ..., 'text', {str}),
                get_all=False) or self._html_search_meta(['og:description', 'twitter:description'], webpage),
            'timestamp': traverse_obj(art_json, ('firstPublished', {parse_iso8601})),
            'creator': ', '.join(
                traverse_obj(art_json, ('bylines', ..., 'creators', ..., 'displayName')))  # TODO: change to 'creators' (list)
        }

        blocks = traverse_obj(art_json, (
            'sprinkledBody', 'content', ..., ('ledeMedia', None),
            lambda _, v: v['__typename'] in ('Video', 'Audio')))

        # more than 1 video in the article, treat it as a playlist
        if len(blocks) > 1:
            entries = []
            for block in blocks:
                entries.append(merge_dicts(
                    self._extract_content_from_block(block), common_info, {
                        'thumbnails': self._extract_thumbnails(traverse_obj(
                            block, ('promotionalMedia', 'crops', ..., 'renditions', ...)))}))

            return self.playlist_result(entries, page_id, common_info.get('title'),
                                        common_info.get('description'))

        return merge_dicts(self._extract_content_from_block(blocks[0]), common_info, {
            'thumbnails': self._extract_thumbnails(traverse_obj(
                blocks[0], ('promotionalMedia', 'crops', ..., 'renditions', ...)) or traverse_obj(
                art_json, ('promotionalMedia', 'assetCrops', ..., 'renditions', ...))),
        })


class NYTimesCookingIE(InfoExtractor):
    IE_NAME = 'NYTimesCookingRecipes'
    _VALID_URL = r'https?://cooking\.nytimes\.com/recipes/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://cooking.nytimes.com/recipes/1017817-cranberry-curd-tart',
        'md5': '579e83bbe8e61e9de67f80edba8a78a8',
        'info_dict': {
            'id': '1017817',
            'ext': 'mp4',
            'title': 'Cranberry Curd Tart',
            'description': 'md5:ad77a3fc321db636256d4343c5742152',
            'timestamp': 1447804800,
            'upload_date': '20151118',
            'creator': 'David Tanis',

Review comment (refactor):
if applying the above 2 suggestions we can simplify like this (and remove below)
```suggestion
        entries = []
        for block in blocks:
            entries.append({
                **common_info,
                **self._extract_content_from_block(block),
            })

        if len(entries) > 1:
            return self.playlist_result(entries, page_id, **common_info)

        return {
            'id': page_id,
            **entries[0],
        }
```

Code after:
            'title': 'The Gamble: Can Genetically Modified Mosquitoes End Disease?',
            'description': 'md5:9ff8b47acbaf7f3ca8c732f5c815be2e',
            'timestamp': 1695960700,
            'upload_date': '20230929',
            'creator': 'Stephanie Nolen, Natalija Gormalova',
            'thumbnail': r're:https?://\w+\.nyt.com/images/.*\.jpg',
            'duration': 1322,
        },
    }, {
        'url': 'https://www.nytimes.com/2023/11/29/business/dealbook/kamala-harris-biden-voters.html',
        'md5': '3eb5ddb1d6f86254fe4f233826778737',
        'info_dict': {
            'id': '100000009202270',
            'ext': 'mp4',
            'title': 'Kamala Harris Defends Biden Policies, but Says ‘More Work’ Needed to Reach Voters',
            'description': 'md5:de4212a7e19bb89e4fb14210ca915f1f',
            'timestamp': 1701290997,
            'upload_date': '20231129',
            'uploader': 'By The New York Times',
            'creator': 'Katie Rogers',
            'thumbnail': r're:https?://\w+\.nyt.com/images/.*\.jpg',
            'duration': 97.631,
        },
        'params': {
            'skip_download': 'm3u8',
        },
    }, {
        # multiple videos in the same article
        'url': 'https://www.nytimes.com/2023/12/02/business/air-traffic-controllers-safety.html',
        'info_dict': {
            'id': 'air-traffic-controllers-safety',
            'title': 'Drunk and Asleep on the Job: Air Traffic Controllers Pushed to the Brink',
            'description': 'md5:549e5a5e935bf7d048be53ba3d2c863d',
            'upload_date': '20231202',
            'creator': 'Emily Steel, Sydney Ember',
            'timestamp': 1701511264,
        },
        'playlist_count': 3,
    }, {
        'url': 'https://www.nytimes.com/2023/12/02/business/media/netflix-squid-game-challenge.html',
        'only_matching': True,
    }]

    def _extract_content_from_block(self, block):
        details = traverse_obj(block, {
            'id': ('sourceId', {str}),
            'uploader': ('bylines', ..., 'renderedRepresentation', {str}),
            'duration': (None, (('duration', {lambda x: float_or_none(x, scale=1000)}), ('length', {int_or_none}))),
            'timestamp': ('firstPublished', {parse_iso8601}),
            'series': ('podcastSeries', {str}),
        }, get_all=False)