diff --git a/itd/client.py b/itd/client.py index 8f1f4f4..20d704e 100644 --- a/itd/client.py +++ b/itd/client.py @@ -1,4 +1,4 @@ -from warnings import deprecated +# from warnings import deprecated from uuid import UUID from _io import BufferedReader from typing import cast @@ -36,7 +36,8 @@ from itd.request import set_cookies from itd.exceptions import ( NoCookie, NoAuthData, SamePassword, InvalidOldPassword, NotFound, ValidationError, UserBanned, PendingRequestExists, Forbidden, UsernameTaken, CantFollowYourself, Unauthorized, - CantRepostYourPost, AlreadyReposted, AlreadyReported, TooLarge, PinNotOwned, NoContent + CantRepostYourPost, AlreadyReposted, AlreadyReported, TooLarge, PinNotOwned, NoContent, + AlreadyFollowing ) @@ -220,6 +221,8 @@ class Client: res = follow(self.token, username) if res.json().get('error', {}).get('code') == 'NOT_FOUND': raise NotFound('User') + if res.json().get('error', {}).get('code') == 'CONFLICT': + raise AlreadyFollowing() if res.json().get('error', {}).get('code') == 'VALIDATION_ERROR' and res.status_code == 400: raise CantFollowYourself() res.raise_for_status() @@ -292,21 +295,6 @@ class Client: return [UserFollower.model_validate(user) for user in res.json()['data']['users']], Pagination.model_validate(res.json()['data']['pagination']) - @deprecated("verificate устарел используйте verify") - @refresh_on_error - def verificate(self, file_url: str) -> Verification: - """Отправить запрос на верификацию - - Args: - file_url (str): Ссылка на видео - - Raises: - PendingRequestExists: Запрос уже отправлен - - Returns: - Verification: Верификация - """ - return self.verify(file_url) @refresh_on_error def verify(self, file_url: str) -> Verification: @@ -546,19 +534,6 @@ class Client: raise Forbidden('delete comment') res.raise_for_status() - @deprecated("get_hastags устарел используйте get_hashtags") - @refresh_on_error - def get_hastags(self, limit: int = 10) -> list[Hashtag]: - """Получить список популярных хэштэгов - - Args: - limit (int, optional): Лимит. Defaults to 10. - - Returns: - list[Hashtag]: Список хэштэгов - """ - return self.get_hashtags(limit) - @refresh_on_error def get_hashtags(self, limit: int = 10) -> list[Hashtag]: """Получить список популярных хэштэгов diff --git a/itd/exceptions.py b/itd/exceptions.py index 1d4b894..3484f11 100644 --- a/itd/exceptions.py +++ b/itd/exceptions.py @@ -102,4 +102,8 @@ class PinNotOwned(Exception): class NoContent(Exception): def __str__(self) -> str: - return 'Content or attachments required' \ No newline at end of file + return 'Content or attachments required' + +class AlreadyFollowing(Exception): + def __str__(self) -> str: + return 'Already following user' \ No newline at end of file diff --git a/itd/request.py b/itd/request.py index b943969..e052b7f 100644 --- a/itd/request.py +++ b/itd/request.py @@ -33,11 +33,13 @@ def fetch(token: str, method: str, url: str, params: dict = {}, files: dict[str, res = s.request(method.upper(), base, timeout=120 if files else 20, json=params, headers=headers, files=files) try: + if res.json().get('error') == 'Too Many Requests': + raise RateLimitExceeded(0) if res.json().get('error', {}).get('code') == 'RATE_LIMIT_EXCEEDED': raise RateLimitExceeded(res.json()['error'].get('retryAfter', 0)) if res.json().get('error', {}).get('code') == 'UNAUTHORIZED': raise Unauthorized() - except JSONDecodeError: + except (JSONDecodeError, AttributeError): pass # todo if not res.ok: @@ -79,10 +81,11 @@ def auth_fetch(cookies: str, method: str, url: str, params: dict = {}, token: st else: res = s.request(method, f'https://xn--d1ah4a.com/api/{url}', timeout=20, json=params, headers=headers) - # print(res.text) if res.text == 'UNAUTHORIZED': raise InvalidToken() try: + if res.json().get('error') == 'Too Many Requests': + raise RateLimitExceeded(0) if res.json().get('error', {}).get('code') == 'RATE_LIMIT_EXCEEDED': raise RateLimitExceeded(res.json()['error'].get('retryAfter', 0)) if res.json().get('error', {}).get('code') in ('SESSION_NOT_FOUND', 'REFRESH_TOKEN_MISSING', 'SESSION_REVOKED', 'SESSION_EXPIRED'):