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

Code before:
        base_hash = traverse_obj(challenge_data, (
            'v', 'a', {str}, {base64.b64decode},
            {hashlib.sha256}, {require('challenge base hash')}))

        for i in range(100):
            number = str(i).encode()
            test_hash = base_hash.copy()
            test_hash.update(number)
            if test_hash.digest() == expected_digest:
                challenge_data['d'] = base64.b64encode(number).decode()
                break
        else:
            raise ExtractorError('Unable to solve JS challenge')

        cookie_value = base64.b64encode(
            json.dumps(challenge_data, separators=(',', ':')).encode()).decode()

        # At time of writing, the cookie name was _wafchallengeid
        cookie_name = traverse_obj(webpage, (
            {find_element(id='wci', html=True)}, {extract_attributes},
            'class', {require('challenge cookie name')}))

        # Actual JS sets Max-Age=1s, but we adjust for --sleep-requests and Python slowness
        self._set_cookie(
            '.tiktok.com', cookie_name, cookie_value,
            expire_time=int(time.time()) + self.get_param('sleep_interval_requests', 0) + 2)

    def _extract_web_data_and_status(self, url, video_id, fatal=True):
        video_data, status = {}, -1

        def get_webpage(note='Downloading webpage'):
            res = self._download_webpage_handle(url, video_id, note, fatal=fatal, impersonate=True)
            if res is False:
                return False

            webpage, urlh = res
            if urllib.parse.urlparse(urlh.url).path == '/login':
                message = 'TikTok is requiring login for access to this content'
                if fatal:
                    self.raise_login_required(message)
                self.report_warning(f'{message}. {self._login_hint()}')
                return False

            return webpage

        webpage = get_webpage()
        if webpage is False:
            return video_data, status

        universal_data = self._get_universal_data(webpage, video_id)
        if not universal_data:

Review comment (refactor):
```suggestion
            expire_time=int(time.time()) + (self.get_param('sleep_interval_requests') or 0) + 2)
```
following https://github.com/yt-dlp/yt-dlp/blob/c8680b65f79cfeb23b342b70ffe1e233902f7933/yt_dlp/extractor/common.py#L871

Code after:
            {require('challenge expected digest')}))

        base_hash = traverse_obj(challenge_data, (
            'v', 'a', {str}, {base64.b64decode},
            {hashlib.sha256}, {require('challenge base hash')}))

        for i in range(1_000_001):
            number = str(i).encode()
            test_hash = base_hash.copy()
            test_hash.update(number)
            if test_hash.digest() == expected_digest:
                challenge_data['d'] = base64.b64encode(number).decode()
                break
        else:
            raise ExtractorError('Unable to solve JS challenge')

        cookie_value = base64.b64encode(
            json.dumps(challenge_data, separators=(',', ':')).encode()).decode()

        # At time of writing, the cookie name was _wafchallengeid
        cookie_name = traverse_obj(webpage, (
            {find_element(id='wci', html=True)}, {extract_attributes},
            'class', {require('challenge cookie name')}))

        # Actual JS sets Max-Age=1, but we need to adjust for --sleep-requests and Python slowness
        expire_time = int(time.time()) + (self.get_param('sleep_interval_requests') or 0) + 2
        self._set_cookie('.tiktok.com', cookie_name, cookie_value, expire_time=expire_time)

    def _extract_web_data_and_status(self, url, video_id, fatal=True):
        video_data, status = {}, -1

        def get_webpage(note='Downloading webpage'):
            res = self._download_webpage_handle(url, video_id, note, fatal=fatal, impersonate=True)
            if res is False:
                return False

            webpage, urlh = res
            if urllib.parse.urlparse(urlh.url).path == '/login':
                message = 'TikTok is requiring login for access to this content'
                if fatal:
                    self.raise_login_required(message)
                self.report_warning(f'{message}. {self._login_hint()}', video_id=video_id)
                return False

            return webpage

        webpage = get_webpage()
        if webpage is False:
            return video_data, status

        universal_data = self._get_universal_data(webpage, video_id)