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

Code before:
                'width': int(m.group('width')),
                'height': int(m.group('height')),
            })

    @functools.cached_property
    def is_logged_in(self):
        return bool(self._get_cookies(self._API_BASE).get('auth_token'))

    def _call_api(self, path, video_id, query={}, graphql=False):
        cookies = self._get_cookies(self._API_BASE)
        headers = {}

        csrf_cookie = cookies.get('ct0')
        if csrf_cookie:
            headers['x-csrf-token'] = csrf_cookie.value

        if self.is_logged_in:
            headers.update({
                'x-twitter-auth-type': 'OAuth2Session',
                'x-twitter-client-language': 'en',
                'x-twitter-active-user': 'yes',
            })

        last_error = None
        for bearer_token in self._TOKENS:
            for first_attempt in True, False:
                headers['Authorization'] = f'Bearer {bearer_token}'

                if not self.is_logged_in:
                    if not self._TOKENS[bearer_token]:
                        headers.pop('x-guest-token', None)
                        guest_token_response = self._download_json(
                            self._API_BASE + 'guest/activate.json', video_id,
                            'Downloading guest token', data=b'', headers=headers)

                        self._TOKENS[bearer_token] = guest_token_response.get('guest_token')
                        if not self._TOKENS[bearer_token]:
                            raise ExtractorError('Could not retrieve guest token')

                    headers['x-guest-token'] = self._TOKENS[bearer_token]

                try:
                    allowed_status = {400, 403, 404} if graphql else {403}
                    result = self._download_json(
                        (self._GRAPHQL_API_BASE if graphql else self._API_BASE) + path,
                        video_id, headers=headers, query=query, expected_status=allowed_status)

                except ExtractorError as e:
                    if last_error:
                        raise last_error


Review comment (suggestion):
```suggestion
            for first_attempt in (True, False):
```

Code after:
                'width': int(m.group('width')),
                'height': int(m.group('height')),
            })

    @functools.cached_property
    def is_logged_in(self):
        return bool(self._get_cookies(self._API_BASE).get('auth_token'))

    def _call_api(self, path, video_id, query={}, graphql=False):
        cookies = self._get_cookies(self._API_BASE)
        headers = {}

        csrf_cookie = cookies.get('ct0')
        if csrf_cookie:
            headers['x-csrf-token'] = csrf_cookie.value

        if self.is_logged_in:
            headers.update({
                'x-twitter-auth-type': 'OAuth2Session',
                'x-twitter-client-language': 'en',
                'x-twitter-active-user': 'yes',
            })

        last_error = None
        for bearer_token in self._TOKENS:
            for first_attempt in (True, False):
                headers['Authorization'] = f'Bearer {bearer_token}'

                if not self.is_logged_in:
                    if not self._TOKENS[bearer_token]:
                        headers.pop('x-guest-token', None)
                        guest_token_response = self._download_json(
                            self._API_BASE + 'guest/activate.json', video_id,
                            'Downloading guest token', data=b'', headers=headers)

                        self._TOKENS[bearer_token] = guest_token_response.get('guest_token')
                        if not self._TOKENS[bearer_token]:
                            raise ExtractorError('Could not retrieve guest token')

                    headers['x-guest-token'] = self._TOKENS[bearer_token]

                try:
                    allowed_status = {400, 403, 404} if graphql else {403}
                    result = self._download_json(
                        (self._GRAPHQL_API_BASE if graphql else self._API_BASE) + path,
                        video_id, headers=headers, query=query, expected_status=allowed_status)

                except ExtractorError as e:
                    if last_error:
                        raise last_error