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

Code before:

    def _extract_ytcfg(self, video_id, webpage):
        return self._parse_json(
            self._search_regex(
                r'ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;', webpage, 'ytcfg',
                default='{}'), video_id, fatal=False) or {}

    def __extract_client_version(self, ytcfg):
        return try_get(ytcfg, lambda x: x['INNERTUBE_CLIENT_VERSION'], compat_str) or self._YT_WEB_CLIENT_VERSION

    def _extract_context(self, ytcfg=None):
        context = try_get(ytcfg, lambda x: x['INNERTUBE_CONTEXT'], dict)
        if context:
            return context

        # Recreate the client context (required)
        client_version = self.__extract_client_version(ytcfg)
        client_name = try_get(ytcfg, lambda x: x['INNERTUBE_CLIENT_NAME'], compat_str) or 'WEB'
        context = {
            'client': {
                'clientName': client_name,
                'clientVersion': client_version,
            }
        }
        visitor_data = try_get(ytcfg, lambda x: x['VISITOR_DATA'],
                               compat_str)
        if visitor_data:
            context['client']['visitorData'] = visitor_data
        return context

    def _generate_api_headers(self, ytcfg=None, identity_token=None, account_syncid=None, visitor_data=None):
        headers = {
            'X-YouTube-Client-Name': '1',
            'X-YouTube-Client-Version': self.__extract_client_version(ytcfg),
        }
        if identity_token:
            headers['x-youtube-identity-token'] = identity_token
        if account_syncid:
            headers['X-Goog-PageId'] = account_syncid
            headers['X-Goog-AuthUser'] = 0
        if visitor_data:
            headers['x-goog-visitor-id'] = visitor_data
        auth = self._generate_sapisidhash_header()
        if auth is not None:
            headers['Authorization'] = auth
            headers['X-Origin'] = 'https://www.youtube.com'  # What about music.youtube.com etc.?
        return headers

    def _extract_video(self, renderer):
        video_id = renderer.get('videoId')
        title = try_get(

Review comment (suggestion):
```suggestion
        visitor_data = try_get(ytcfg, lambda x: x['VISITOR_DATA'], compat_str)
```

Code after:
            or '').split("||")
        if len(sync_ids) >= 2 and sync_ids[1]:
            # datasyncid is of the form "channel_syncid||user_syncid" for secondary channel
            # and just "user_syncid||" for primary channel. We only want the channel_syncid
            return sync_ids[0]

    def _extract_ytcfg(self, video_id, webpage):
        return self._parse_json(
            self._search_regex(
                r'ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;', webpage, 'ytcfg',
                default='{}'), video_id, fatal=False) or {}

    def __extract_client_version(self, ytcfg):
        return try_get(ytcfg, lambda x: x['INNERTUBE_CLIENT_VERSION'], compat_str) or self._YT_WEB_CLIENT_VERSION

    def _extract_context(self, ytcfg=None):
        context = try_get(ytcfg, lambda x: x['INNERTUBE_CONTEXT'], dict)
        if context:
            return context

        # Recreate the client context (required)
        client_version = self.__extract_client_version(ytcfg)
        client_name = try_get(ytcfg, lambda x: x['INNERTUBE_CLIENT_NAME'], compat_str) or 'WEB'
        context = {
            'client': {
                'clientName': client_name,
                'clientVersion': client_version,
            }
        }
        visitor_data = try_get(ytcfg, lambda x: x['VISITOR_DATA'], compat_str)
        if visitor_data:
            context['client']['visitorData'] = visitor_data
        return context

    def _generate_api_headers(self, ytcfg=None, identity_token=None, account_syncid=None, visitor_data=None):
        headers = {
            'X-YouTube-Client-Name': '1',
            'X-YouTube-Client-Version': self.__extract_client_version(ytcfg),
        }
        if identity_token:
            headers['x-youtube-identity-token'] = identity_token
        if account_syncid:
            headers['X-Goog-PageId'] = account_syncid
            headers['X-Goog-AuthUser'] = 0
        if visitor_data:
            headers['x-goog-visitor-id'] = visitor_data
        auth = self._generate_sapisidhash_header()
        if auth is not None:
            headers['Authorization'] = auth
            headers['X-Origin'] = 'https://www.youtube.com'
        return headers