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

Code before:
        },
        'params': {'skip_download': 'm3u8'},
    }, {
        'url': 'https://zen.yandex.ru/media/id/606fd806cc13cb3c58c05cf5/novyi-samsung-fold-3-moskvich-barahlit-612f93b7f8d48e7e945792a2?from=channel&rid=2286618386.482.1630817595976.42360',
        'only_matching': True,
    }, {
        'url': 'https://dzen.ru/media/id/606fd806cc13cb3c58c05cf5/novyi-samsung-fold-3-moskvich-barahlit-612f93b7f8d48e7e945792a2?from=channel&rid=2286618386.482.1630817595976.42360',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        redirect = self._search_json(r'var it\s*=', webpage, 'redirect', id, default={}).get('retpath')
        if redirect:
            video_id = self._match_id(redirect)
            webpage = self._download_webpage(redirect, video_id, note='Redirecting')
        data_json = self._search_json(
            r'("data"\s*:|data\s*=)', webpage, 'metadata', video_id, contains_pattern=r'{["\']_*serverState_*video.+}')
        serverstate = self._search_regex(r'(_+serverState_+video-site_[^_]+_+)',
                                         webpage, 'server state')
        uploader = self._search_regex(r'(<a\s*class=["\']card-channel-link[^"\']+["\'][^>]+>)',
                                      webpage, 'uploader', default='<a>')
        uploader_name = extract_attributes(uploader).get('aria-label')
        item_id = try_get(data_json, lambda x: x[serverstate]['videoViewer']['openedItemId'], str)
        video_json = try_get(data_json, lambda x: x[serverstate]['videoViewer']['items'][item_id], dict)
        stream_urls = try_get(video_json, lambda x: x['video']['streams'])
        formats, subtitles = [], {}
        for s_url in stream_urls:
            ext = determine_ext(s_url)
            if ext == 'mpd':
                fmts, subs = self._extract_mpd_formats_and_subtitles(s_url, video_id, mpd_id='dash')
            elif ext == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(s_url, video_id, 'mp4')
            formats.extend(fmts)
            subtitles = self._merge_subtitles(subtitles, subs)
        return {
            'id': video_id,
            'title': video_json.get('title') or self._og_search_title(webpage),
            'formats': formats,
            'subtitles': subtitles,
            'duration': int_or_none(video_json.get('duration')),
            'view_count': int_or_none(video_json.get('views')),
            'timestamp': int_or_none(video_json.get('publicationDate')),
            'uploader': uploader_name or data_json.get('authorName') or try_get(data_json, lambda x: x['publisher']['name']),
            'description': video_json.get('description') or self._og_search_description(webpage),
            'thumbnail': self._og_search_thumbnail(webpage) or try_get(data_json, lambda x: x['og']['imageUrl']),
        }


class ZenYandexChannelIE(InfoExtractor):

Review comment (refactor):
Let's use new/better helpers for edited code

```suggestion
        item_id = traverse_obj(data_json, (serverstate, 'videoViewer', 'openedItemId', {str}))
        video_json = traverse_obj(data_json, (serverstate, 'videoViewer', 'items', item_id, {dict}))
```

Code after:
        },
        'params': {'skip_download': 'm3u8'},
    }, {
        'url': 'https://zen.yandex.ru/media/id/606fd806cc13cb3c58c05cf5/novyi-samsung-fold-3-moskvich-barahlit-612f93b7f8d48e7e945792a2?from=channel&rid=2286618386.482.1630817595976.42360',
        'only_matching': True,
    }, {
        'url': 'https://dzen.ru/media/id/606fd806cc13cb3c58c05cf5/novyi-samsung-fold-3-moskvich-barahlit-612f93b7f8d48e7e945792a2?from=channel&rid=2286618386.482.1630817595976.42360',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        redirect = self._search_json(r'var it\s*=', webpage, 'redirect', id, default={}).get('retpath')
        if redirect:
            video_id = self._match_id(redirect)
            webpage = self._download_webpage(redirect, video_id, note='Redirecting')
        data_json = self._search_json(
            r'("data"\s*:|data\s*=)', webpage, 'metadata', video_id, contains_pattern=r'{["\']_*serverState_*video.+}')
        serverstate = self._search_regex(r'(_+serverState_+video-site_[^_]+_+)', webpage, 'server state')
        uploader = self._search_regex(r'(<a\s*class=["\']card-channel-link[^"\']+["\'][^>]+>)',
                                      webpage, 'uploader', default='<a>')
        uploader_name = extract_attributes(uploader).get('aria-label')
        item_id = traverse_obj(data_json, (serverstate, 'videoViewer', 'openedItemId', {str}))
        video_json = traverse_obj(data_json, (serverstate, 'videoViewer', 'items', item_id, {dict})) or {}

        formats, subtitles = [], {}
        for s_url in traverse_obj(video_json, ('video', 'streams', ..., {url_or_none})):
            ext = determine_ext(s_url)
            if ext == 'mpd':
                fmts, subs = self._extract_mpd_formats_and_subtitles(s_url, video_id, mpd_id='dash')
            elif ext == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(s_url, video_id, 'mp4')
            formats.extend(fmts)
            subtitles = self._merge_subtitles(subtitles, subs)
        return {
            'id': video_id,
            'title': video_json.get('title') or self._og_search_title(webpage),
            'formats': formats,
            'subtitles': subtitles,
            'duration': int_or_none(video_json.get('duration')),
            'view_count': int_or_none(video_json.get('views')),
            'timestamp': int_or_none(video_json.get('publicationDate')),
            'uploader': uploader_name or data_json.get('authorName') or try_get(data_json, lambda x: x['publisher']['name']),
            'description': video_json.get('description') or self._og_search_description(webpage),
            'thumbnail': self._og_search_thumbnail(webpage) or try_get(data_json, lambda x: x['og']['imageUrl']),
        }


class ZenYandexChannelIE(InfoExtractor):
    _VALID_URL = r'https?://(zen\.yandex|dzen)\.ru/(?!media|video)(?:id/)?(?P<id>[a-z0-9-_]+)'