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

Code before:
            'host_abi': 'armeabi-v7a',
            'locale': 'en',
            'ac2': 'wifi5g',
            'uoo': '1',
            'op_region': 'US',
            'build_number': self._APP_VERSION,
            'region': 'US',
            'ts': int(time.time()),
            'iid': iid,
            'device_id': random.randint(7250000000000000000, 7351147085025500000),
            'openudid': ''.join(random.choices('0123456789abcdef', k=16)),
        }

    def _call_api(self, ep, query, video_id, fatal=True,
                  note='Downloading API JSON', errnote='Unable to download API page'):
        if not self._WORKING_APP_IID:
            if iid := self._configuration_arg('iid', [None], ie_key=TikTokIE)[0]:
                self._WORKING_APP_IID = iid
                self.write_debug(f'Imported app install id {iid!r} from extractor argument')

        if self._WORKING_APP_IID:
            real_query = self._build_api_query(query, self._WORKING_APP_IID)
            return self._call_api_impl(ep, real_query, video_id, fatal, note, errnote)

        max_tries = len(self._APP_INSTALL_IDS)
        for count, iid in enumerate(random.sample(self._APP_INSTALL_IDS, max_tries), start=1):
            real_query = self._build_api_query(query, iid)
            self.write_debug(f'iid={iid}')
            try:
                res = self._call_api_impl(ep, real_query, video_id, fatal, note, errnote)
                self._WORKING_APP_IID = iid
                return res
            except ExtractorError as e:
                if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0:
                    message = str(e.cause or e.msg)
                    if count == max_tries:
                        if fatal:
                            raise
                        else:
                            self.report_warning(message)
                            return
                    self.report_warning(f'{message}. Retrying... (attempt {count} of {max_tries})')
                    continue
                raise

    def _extract_aweme_app(self, aweme_id):
        feed_list = self._call_api(
            'feed', {'aweme_id': aweme_id}, aweme_id, note='Downloading video feed',
            errnote='Unable to download video feed').get('aweme_list') or []
        aweme_detail = next((aweme for aweme in feed_list if str(aweme.get('aweme_id')) == aweme_id), None)
        if not aweme_detail:

Review comment (refactor):
https://github.com/yt-dlp/yt-dlp/pull/9548#discussion_r1543188731 would simplify this to:

```suggestion
        iids = self._configuration_arg('iid', self._APP_INSTALL_IDS, ie_key=TikTokIE):
        max_tries = len(iids)
        for count, iid in enumerate(random.sample(iids, max_tries), start=1):
```

Code after:
            'version_code': ''.join((f'{int(v):02d}' for v in self._APP_INFO['app_version'].split('.'))),
            'version_name': self._APP_INFO['app_version'],
            'manifest_version_code': self._APP_INFO['manifest_app_version'],
            'update_version_code': self._APP_INFO['manifest_app_version'],
            'ab_version': self._APP_INFO['app_version'],
            'resolution': '1080*2400',
            'dpi': 420,
            'device_type': 'Pixel 7',
            'device_brand': 'Google',
            'language': 'en',
            'os_api': '29',
            'os_version': '13',
            'ac': 'wifi',
            'is_pad': '0',
            'current_region': 'US',
            'app_type': 'normal',
            'sys_region': 'US',
            'last_install_time': int(time.time()) - random.randint(86400, 1123200),
            'timezone_name': 'America/New_York',
            'residence': 'US',
            'app_language': 'en',
            'timezone_offset': '-14400',
            'host_abi': 'armeabi-v7a',
            'locale': 'en',
            'ac2': 'wifi5g',
            'uoo': '1',
            'op_region': 'US',
            'build_number': self._APP_INFO['app_version'],
            'region': 'US',
            'ts': int(time.time()),
            'iid': self._APP_INFO['iid'],
            'device_id': random.randint(7250000000000000000, 7351147085025500000),
            'openudid': ''.join(random.choices('0123456789abcdef', k=16)),
        }

    def _call_api(self, ep, query, video_id, fatal=True,
                  note='Downloading API JSON', errnote='Unable to download API page'):
        if not self._APP_INFO and not self._get_next_app_info():
            message = 'No working app info is available'
            if fatal:
                raise ExtractorError(message, expected=True)
            else:
                self.report_warning(message)
                return

        max_tries = len(self._APP_INFO_POOL) + 1  # _APP_INFO_POOL + _APP_INFO
        for count in itertools.count(1):
            self.write_debug(str(self._APP_INFO))
            real_query = self._build_api_query(query)
            try:
                return self._call_api_impl(ep, real_query, video_id, fatal, note, errnote)