diff --git a/twikit/client/client.py b/twikit/client/client.py index 053f0087..260bcc66 100644 --- a/twikit/client/client.py +++ b/twikit/client/client.py @@ -155,7 +155,7 @@ async def request( response_data = response.text if isinstance(response_data, dict) and 'errors' in response_data: - error_code = response_data['errors'][0]['code'] + error_code = response_data['errors'][0].get('code') error_message = response_data['errors'][0].get('message') if error_code in (37, 64): # Account suspended diff --git a/twikit/user.py b/twikit/user.py index 5535a346..5c7f3b80 100644 --- a/twikit/user.py +++ b/twikit/user.py @@ -88,40 +88,40 @@ class User: def __init__(self, client: Client, data: dict) -> None: self._client = client - legacy = data['legacy'] + legacy = data.get('legacy', {}) - self.id: str = data['rest_id'] - self.created_at: str = legacy['created_at'] - self.name: str = legacy['name'] - self.screen_name: str = legacy['screen_name'] - self.profile_image_url: str = legacy['profile_image_url_https'] + self.id: str = data.get('rest_id', '') + self.created_at: str = legacy.get('created_at', '') + self.name: str = legacy.get('name', '') + self.screen_name: str = legacy.get('screen_name', '') + self.profile_image_url: str = legacy.get('profile_image_url_https', '') self.profile_banner_url: str = legacy.get('profile_banner_url') self.url: str = legacy.get('url') - self.location: str = legacy['location'] - self.description: str = legacy['description'] - self.description_urls: list = legacy['entities']['description']['urls'] - self.urls: list = legacy['entities'].get('url', {}).get('urls') - self.pinned_tweet_ids: list[str] = legacy['pinned_tweet_ids_str'] - self.is_blue_verified: bool = data['is_blue_verified'] - self.verified: bool = legacy['verified'] - self.possibly_sensitive: bool = legacy['possibly_sensitive'] - self.can_dm: bool = legacy['can_dm'] - self.can_media_tag: bool = legacy['can_media_tag'] - self.want_retweets: bool = legacy['want_retweets'] - self.default_profile: bool = legacy['default_profile'] - self.default_profile_image: bool = legacy['default_profile_image'] - self.has_custom_timelines: bool = legacy['has_custom_timelines'] - self.followers_count: int = legacy['followers_count'] - self.fast_followers_count: int = legacy['fast_followers_count'] - self.normal_followers_count: int = legacy['normal_followers_count'] - self.following_count: int = legacy['friends_count'] - self.favourites_count: int = legacy['favourites_count'] - self.listed_count: int = legacy['listed_count'] - self.media_count = legacy['media_count'] - self.statuses_count: int = legacy['statuses_count'] - self.is_translator: bool = legacy['is_translator'] - self.translator_type: str = legacy['translator_type'] - self.withheld_in_countries: list[str] = legacy['withheld_in_countries'] + self.location: str = legacy.get('location', '') + self.description: str = legacy.get('description', '') + self.description_urls: list = legacy.get('entities', {}).get('description', {}).get('urls', []) + self.urls: list = legacy.get('entities', {}).get('url', {}).get('urls') + self.pinned_tweet_ids: list[str] = legacy.get('pinned_tweet_ids_str', []) + self.is_blue_verified: bool = data.get('is_blue_verified', False) + self.verified: bool = legacy.get('verified', False) + self.possibly_sensitive: bool = legacy.get('possibly_sensitive', False) + self.can_dm: bool = legacy.get('can_dm', False) + self.can_media_tag: bool = legacy.get('can_media_tag', False) + self.want_retweets: bool = legacy.get('want_retweets', False) + self.default_profile: bool = legacy.get('default_profile', False) + self.default_profile_image: bool = legacy.get('default_profile_image', False) + self.has_custom_timelines: bool = legacy.get('has_custom_timelines', False) + self.followers_count: int = legacy.get('followers_count', 0) + self.fast_followers_count: int = legacy.get('fast_followers_count', 0) + self.normal_followers_count: int = legacy.get('normal_followers_count', 0) + self.following_count: int = legacy.get('friends_count', 0) + self.favourites_count: int = legacy.get('favourites_count', 0) + self.listed_count: int = legacy.get('listed_count', 0) + self.media_count = legacy.get('media_count', 0) + self.statuses_count: int = legacy.get('statuses_count', 0) + self.is_translator: bool = legacy.get('is_translator', False) + self.translator_type: str = legacy.get('translator_type', '') + self.withheld_in_countries: list[str] = legacy.get('withheld_in_countries', []) self.protected: bool = legacy.get('protected', False) @property