fix: fix error 'str' object has not attribute 'get' (rate limit); add AlreadyFollowing exception on follow
This commit is contained in:
@@ -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]:
|
||||
"""Получить список популярных хэштэгов
|
||||
|
||||
@@ -103,3 +103,7 @@ class PinNotOwned(Exception):
|
||||
class NoContent(Exception):
|
||||
def __str__(self) -> str:
|
||||
return 'Content or attachments required'
|
||||
|
||||
class AlreadyFollowing(Exception):
|
||||
def __str__(self) -> str:
|
||||
return 'Already following user'
|
||||
@@ -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'):
|
||||
|
||||
Reference in New Issue
Block a user