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

Code before:
                'title': title,
                'description': description,
                'duration': duration,
                'creator': creators,  # TODO: change to 'creators'
                'formats': formats,
                'subtitles': subtitles,
                'thumbnails': self._extract_thumbnails(
                    traverse_obj(json_obj, ('promotionalMedia', 'crops', ..., 'renditions', ...))),
            }

    def _json_from_graphql(self, media_id):
        # reference: `id-to-uri.js`
        video_uuid = uuid.uuid5(self._DNS_NAMESPACE, 'video')
        media_uuid = uuid.uuid5(video_uuid, media_id)

        return self._download_json(
            self._GRAPHQL_API, media_id, note='Downloading json from GRAPHQL API', data=json.dumps({
                'query': self._GRAPHQL_QUERY,
                'variables': {'id': f'nyt://video/{media_uuid}'},
            }, separators=(',', ':')).encode(), headers={
                'Content-Type': 'application/json',
                'Nyt-App-Type': 'vhs',
                'Nyt-App-Version': 'v3.52.21',
                'Nyt-Token': self._TOKEN,
                'Origin': 'https://cooking.nytimes.com',
            }, fatal=False) or {}

    def _real_extract(self, url):
        page_id = self._match_id(url)
        webpage = self._download_webpage(url, page_id)

        lead_video_id = self._search_regex(
            r'data-video-player-id="(\d+)"></div>', webpage, 'lead video', default=None)
        media_items = traverse_obj(
            get_elements_html_by_class('video-item', webpage), (..., {extract_attributes}, 'data-video-id'))
        title = self._html_search_meta(['og:title', 'twitter:title'], webpage)
        description = self._html_search_meta(['og:description', 'twitter:description'], webpage)

        if media_items:
            media_items.append(lead_video_id)
            return self.playlist_result(self._build_playlist(media_items), page_id, title, description)

        json_obj = traverse_obj(self._json_from_graphql(lead_video_id) or {}, ('data', 'video'))

        formats, subtitles = self._extract_media_from_json(lead_video_id, json_obj)

        return {
            'id': lead_video_id,
            'title': title,
            'description': description,
            'duration': int_or_none(json_obj.get('duration')),

Review comment (suggestion):
instead of performing the same traversal outside of this method both times we call it, let's handle the traversal inside the method:
```suggestion
        return traverse_obj(self._download_json(
            self._GRAPHQL_API, media_id, 'Downloading JSON from GraphQL API', data=json.dumps({
                'query': self._GRAPHQL_QUERY,
                'variables': {'id': f'nyt://video/{media_uuid}'},
            }, separators=(',', ':')).encode(), headers={
                'Content-Type': 'application/json',
                'Nyt-App-Type': 'vhs',
                'Nyt-App-Version': 'v3.52.21',
                'Nyt-Token': self._TOKEN,
                'Origin': 'https://cooking.nytimes.com',
            }, fatal=False), ('data', 'video', {dict})) or {}
```

Code after:
            'creator': 'Sue Li',
            'thumbnail': r're:https?://\w+\.nyt.com/images/.*\.jpg',
        },
    }, {
        'url': 'https://cooking.nytimes.com/recipes/1019516-overnight-oats',
        'md5': '2fe7965a3adc899913b8e25ada360823',
        'info_dict': {
            'id': '1019516',
            'ext': 'mp4',
            'timestamp': 1546387200,
            'description': 'md5:8856ce10239161bd2596ac335b9f9bfb',
            'upload_date': '20190102',
            'title': 'Overnight Oats',
            'creator': 'Genevieve Ko',
            'thumbnail': r're:https?://\w+\.nyt.com/images/.*\.jpg',
        },
    }]

    def _real_extract(self, url):
        page_id = self._match_id(url)
        webpage = self._download_webpage(url, page_id)
        recipe_data = self._search_nextjs_data(webpage, page_id)['props']['pageProps']['recipe']

        formats, subtitles = self._extract_m3u8_formats_and_subtitles(
            recipe_data['videoSrc'], page_id, 'mp4', m3u8_id='hls')

        return {
            **traverse_obj(recipe_data, {
                'id': ('id', {str_or_none}),
                'title': ('title', {str}),
                'description': ('topnote', {clean_html}),
                'timestamp': ('publishedAt', {int_or_none}),
                'creator': ('contentAttribution', 'cardByline', {str}),
            }),
            'formats': formats,
            'subtitles': subtitles,
            'thumbnails': [{'url': thumb_url} for thumb_url in traverse_obj(
                recipe_data, ('image', 'crops', 'recipe', ..., {url_or_none}))],
        }