diff --git a/README.md b/README.md index 9a29c90..58efe22 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ fetch(c.token, 'метод', 'эндпоинт', {'данные': 'данные' ## Планы + - Форматированные сообщения об ошибках + - Логирование (через logging) - Добавление ООП (отдеьные классы по типу User или Post вместо обычного JSON) - Голосовые сообщения diff --git a/itd/client.py b/itd/client.py index f8c8b56..ba7a36b 100644 --- a/itd/client.py +++ b/itd/client.py @@ -12,7 +12,8 @@ from itd.routes.posts import create_post, get_posts, get_post, edit_post, delete from itd.routes.reports import report from itd.routes.search import search from itd.routes.files import upload_file -from itd.request import refresh_auth +from itd.routes.auth import refresh_token, change_password, logout +from itd.routes.verification import verificate, get_verification_status def refresh_on_error(func): @@ -38,12 +39,27 @@ class Client: else: raise ValueError('Provide token or cookie') - @refresh_on_error def refresh_auth(self): if self.cookies: - self.token = refresh_auth(self.cookies) + self.token = refresh_token(self.cookies) + return self.token else: - print('no cookies!') + print('no cookies') + + @refresh_on_error + def change_password(self, old: str, new: str): + if not self.cookies: + print('no cookies') + return + return change_password(self.cookies, self.token, old, new) + + @refresh_on_error + def logout(self): + if not self.cookies: + print('no cookies') + return + return logout(self.cookies) + @refresh_on_error def get_user(self, username: str) -> dict: @@ -78,6 +94,15 @@ class Client: return get_following(self.token, username) + @refresh_on_error + def verificate(self, file_url: str): + return verificate(self.token, file_url) + + @refresh_on_error + def get_verification_status(self): + return get_verification_status(self.token) + + @refresh_on_error def get_who_to_follow(self) -> dict: return get_who_to_follow(self.token) diff --git a/itd/request.py b/itd/request.py index 742df91..10d73f2 100644 --- a/itd/request.py +++ b/itd/request.py @@ -36,9 +36,8 @@ def set_cookies(cookies: str): for cookie in cookies.split('; '): s.cookies.set(cookie.split('=')[0], cookie.split('=')[-1], path='/', domain='xn--d1ah4a.com.com') -def refresh_auth(cookies: str): - print('refresh') - res = s.post(f'https://xn--d1ah4a.com/api/v1/auth/refresh', timeout=10, headers={ +def auth_fetch(cookies: str, method: str, url: str, params: dict = {}, token: str | None = None): + headers = { "Host": "xn--d1ah4a.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0", "Accept": "*/*", @@ -58,6 +57,13 @@ def refresh_auth(cookies: str): "Cache-Control": "no-cache", "Content-Length": "0", "TE": "trailers", - }) + } + if token: + headers['Authorization'] = 'Bearer ' + token + + if method == 'get': + res = s.get(f'https://xn--d1ah4a.com/api/{url}', timeout=20, params=params, headers=headers) + else: + res = s.request(method, f'https://xn--d1ah4a.com/api/{url}', timeout=20, json=params, headers=headers) res.raise_for_status() - return res.json()['accessToken'] + return res.json() diff --git a/itd/routes/auth.py b/itd/routes/auth.py new file mode 100644 index 0000000..0401467 --- /dev/null +++ b/itd/routes/auth.py @@ -0,0 +1,10 @@ +from itd.request import auth_fetch + +def refresh_token(cookies: str): + return auth_fetch(cookies, 'post', 'v1/auth/refresh')['accessToken'] + +def change_password(cookies: str, token: str, old: str, new: str): + return auth_fetch(cookies, 'post', 'v1/auth/change-password', {'newPassword': new, 'oldPassword': old}, token) + +def logout(cookies: str): + return auth_fetch(cookies, 'post', 'v1/auth/logout') diff --git a/itd/routes/users.py b/itd/routes/users.py index ac0506c..1fa7424 100644 --- a/itd/routes/users.py +++ b/itd/routes/users.py @@ -35,3 +35,4 @@ def get_followers(token: str, username: str, limit: int = 30, page: int = 1): def get_following(token: str, username: str, limit: int = 30, page: int = 1): return fetch(token, 'get', f'users/{username}/following', {'limit': limit, 'page': page}) + diff --git a/itd/routes/verification.py b/itd/routes/verification.py new file mode 100644 index 0000000..8c66426 --- /dev/null +++ b/itd/routes/verification.py @@ -0,0 +1,8 @@ +from itd.request import fetch + +def verificate(token: str, file_url: str): + # {"success":true,"request":{"id":"fc54e54f-8586-4d8c-809e-df93161f99da","userId":"9096a85b-c319-483e-8940-6921be427ad0","videoUrl":"https://943701f000610900cbe86b72234e451d.bckt.ru/videos/354f28a6-9ac7-48a6-879a-a454062b1d6b.mp4","status":"pending","rejectionReason":null,"reviewedBy":null,"reviewedAt":null,"createdAt":"2026-01-30T12:58:14.228Z","updatedAt":"2026-01-30T12:58:14.228Z"}} + return fetch(token, 'post', 'verification/submit', {'videoUrl': file_url}) + +def get_verification_status(token: str): + return fetch(token, 'get', 'verification/status') \ No newline at end of file