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

Code before:
        # Enforce language and tz for extraction
        client_context = traverse_obj(context, 'client', expected_type=dict, default={})
        client_context.update({'hl': self._preferred_lang or 'en', 'timeZone': 'UTC', 'utcOffsetMinutes': 0})
        return context

    @staticmethod
    def _make_sid_authorization(scheme, sid, origin, additional_parts):
        timestamp = str(round(time.time()))

        hash_parts = []
        if additional_parts:
            hash_parts.append(':'.join(additional_parts.values()))
        hash_parts.extend([timestamp, sid, origin])
        sidhash = hashlib.sha1(' '.join(hash_parts).encode()).hexdigest()

        parts = [timestamp, sidhash]
        if additional_parts:
            parts.append(''.join(additional_parts.keys()))

        return f'{scheme} {"_".join(parts)}'

    _SAPISID = _3PSAPISID = _1PSAPISID = None

    def _load_sid_cookies(self):

        yt_cookies = self._get_cookies('https://www.youtube.com')
        if self._SAPISID is None:
            # Sometimes SAPISID cookie isn't present but __Secure-3PAPISID is.
            # YouTube also falls back to __Secure-3PAPISID if SAPISID is missing.
            # See: https://github.com/yt-dlp/yt-dlp/issues/393

            sapisid_cookie = dict_get(
                yt_cookies, ('SAPISID', '__Secure-3PAPISID'))
            if sapisid_cookie and sapisid_cookie.value:
                self._SAPISID = sapisid_cookie.value

        if self._1PSAPISID is None:
            _1papisid_cookie = yt_cookies.get('__Secure-1PAPISID')
            if _1papisid_cookie and _1papisid_cookie.value:
                self._1PSAPISID = _1papisid_cookie.value

        if self._3PSAPISID is None:
            _3papisid_cookie = yt_cookies.get('__Secure-3PAPISID')
            if _3papisid_cookie and _3papisid_cookie.value:
                self._3PSAPISID = _3papisid_cookie.value

    def _generate_sid_authorization(self, origin='https://www.youtube.com', user_session_id=None):
        """
        Generate API Session ID Authorization for Innertube requests. Assumes all requests are secure (https).
        @param origin: Origin URL
        @param user_session_id: Optional User Session ID

Review comment (suggestion):
Is there any reason for the `is None` conditionals instead of just a truthy check? Can the cookie values be empty strings? And do we want them to remain that way instead of overwriting them?

IMO we could condense this method a bit:
```py
    def _load_sid_cookies(self):
        yt_cookies = self._get_cookies('https://www.youtube.com')
        yt_sapisid = try_call(lambda: yt_cookies['SAPISID'].value)
        yt_3papisid = try_call(lambda: yt_cookies['__Secure-3PAPISID'].value)

        if self._SAPISID is None:
            # Sometimes SAPISID cookie isn't present but __Secure-3PAPISID is.
            # YouTube also falls back to __Secure-3PAPISID if SAPISID is missing.
            # See: https://github.com/yt-dlp/yt-dlp/issues/393
            self._SAPISID = yt_sapisid or yt_3papisid

        if self._1PSAPISID is None:
            self._1PSAPISID = try_call(lambda: yt_cookies['__Secure-1PAPISID'].value)

        if self._3PSAPISID is None:
            self._3PSAPISID = yt_3papisid
```

Code after:
        client_context = traverse_obj(context, 'client', expected_type=dict, default={})
        client_context.update({'hl': self._preferred_lang or 'en', 'timeZone': 'UTC', 'utcOffsetMinutes': 0})
        return context

    @staticmethod
    def _make_sid_authorization(scheme, sid, origin, additional_parts):
        timestamp = str(round(time.time()))

        hash_parts = []
        if additional_parts:
            hash_parts.append(':'.join(additional_parts.values()))
        hash_parts.extend([timestamp, sid, origin])
        sidhash = hashlib.sha1(' '.join(hash_parts).encode()).hexdigest()

        parts = [timestamp, sidhash]
        if additional_parts:
            parts.append(''.join(additional_parts))

        return f'{scheme} {"_".join(parts)}'

    def _get_sid_cookies(self):
        """
        Get SAPISID, 1PSAPISID, 3PSAPISID cookie values
        @returns sapisid, 1psapisid, 3psapisid
        """
        yt_cookies = self._get_cookies('https://www.youtube.com')
        yt_sapisid = try_call(lambda: yt_cookies['SAPISID'].value)
        yt_3papisid = try_call(lambda: yt_cookies['__Secure-3PAPISID'].value)
        yt_1papisid = try_call(lambda: yt_cookies['__Secure-1PAPISID'].value)

        # Sometimes SAPISID cookie isn't present but __Secure-3PAPISID is.
        # YouTube also falls back to __Secure-3PAPISID if SAPISID is missing.
        # See: https://github.com/yt-dlp/yt-dlp/issues/393

        return yt_sapisid or yt_3papisid, yt_1papisid, yt_3papisid

    def _get_sid_authorization_header(self, origin='https://www.youtube.com', user_session_id=None):
        """
        Generate API Session ID Authorization for Innertube requests. Assumes all requests are secure (https).
        @param origin: Origin URL
        @param user_session_id: Optional User Session ID
        @return: Authorization header value
        """

        authorizations = []
        additional_parts = {}
        if user_session_id:
            additional_parts['u'] = user_session_id

        yt_sapisid, yt_1psapisid, yt_3psapisid = self._get_sid_cookies()