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

Code before:
            token = try_get(ytcfg, lambda x: x['ID_TOKEN'], str)
            if token:
                return token
        if webpage:
            return self._search_regex(
                r'\bID_TOKEN["\']\s*:\s*["\'](.+?)["\']', webpage,
                'identity token', default=None, fatal=False)

    def _data_sync_id_to_delegated_session_id(self, data_sync_id):
        if not data_sync_id:
            return
        # 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
        session_ids = data_sync_id.split('||')
        if len(session_ids) >= 2 and session_ids[1]:
            return session_ids[0]

    def _extract_account_syncid(self, *args):
        """
        Extract current session ID required to download private playlists of secondary channels
        @params response and/or ytcfg
        """
        # ytcfg includes channel_syncid if on secondary channel
        delegated_sid = traverse_obj(
            args, (..., 'DELEGATED_SESSION_ID'), expected_type=str, get_all=False)
        if delegated_sid:
            return delegated_sid

        data_sync_id = self._extract_data_sync_id(*args)
        return self._data_sync_id_to_delegated_session_id(data_sync_id)

    def _extract_data_sync_id(self, *args):
        """
        Extract current account dataSyncId.
        In the format DELEGATED_SESSION_ID||USER_SESSION_ID or USER_SESSION_ID||
        @params response and/or ytcfg
        """
        if data_sync_id := self._configuration_arg('data_sync_id', [None], ie_key=YoutubeIE, casesense=True)[0]:
            return data_sync_id

        return traverse_obj(
            args, (
                ..., ('DATASYNC_ID', (
                    'responseContext', 'mainAppWebResponseContext', 'datasyncId'))),
            expected_type=str, get_all=False)

    def _extract_visitor_data(self, *args):
        """
        Extracts visitorData from an API response or ytcfg
        Appears to be used to track session state
        """

Review comment (suggestion):
```suggestion
        if delegated_sid := traverse_obj(args, (..., 'DELEGATED_SESSION_ID', {str}, any)):
```

Code after:
            token = try_get(ytcfg, lambda x: x['ID_TOKEN'], str)
            if token:
                return token
        if webpage:
            return self._search_regex(
                r'\bID_TOKEN["\']\s*:\s*["\'](.+?)["\']', webpage,
                'identity token', default=None, fatal=False)

    def _data_sync_id_to_delegated_session_id(self, data_sync_id):
        if not data_sync_id:
            return
        # 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
        channel_syncid, _, user_syncid = data_sync_id.partition('||')
        if user_syncid:
            return channel_syncid

    def _extract_account_syncid(self, *args):
        """
        Extract current session ID required to download private playlists of secondary channels
        @params response and/or ytcfg
        """
        # ytcfg includes channel_syncid if on secondary channel
        if delegated_sid := traverse_obj(args, (..., 'DELEGATED_SESSION_ID', {str}, any)):
            return delegated_sid

        data_sync_id = self._extract_data_sync_id(*args)
        return self._data_sync_id_to_delegated_session_id(data_sync_id)

    def _extract_data_sync_id(self, *args):
        """
        Extract current account dataSyncId.
        In the format DELEGATED_SESSION_ID||USER_SESSION_ID or USER_SESSION_ID||
        @params response and/or ytcfg
        """
        if data_sync_id := self._configuration_arg('data_sync_id', [None], ie_key=YoutubeIE, casesense=True)[0]:
            return data_sync_id

        return traverse_obj(
            args, (..., ('DATASYNC_ID', ('responseContext', 'mainAppWebResponseContext', 'datasyncId')), {str}, any))

    def _extract_visitor_data(self, *args):
        """
        Extracts visitorData from an API response or ytcfg
        Appears to be used to track session state
        """
        if visitor_data := self._configuration_arg('visitor_data', [None], ie_key=YoutubeIE, casesense=True)[0]:
            return visitor_data
        return get_first(
            args, [('VISITOR_DATA', ('INNERTUBE_CONTEXT', 'client', 'visitorData'), ('responseContext', 'visitorData'))],
            expected_type=str)