feat: add verification; add auth - change password and logout
This commit is contained in:
@@ -79,6 +79,8 @@ fetch(c.token, 'метод', 'эндпоинт', {'данные': 'данные'
|
|||||||
|
|
||||||
## Планы
|
## Планы
|
||||||
|
|
||||||
|
- Форматированные сообщения об ошибках
|
||||||
|
- Логирование (через logging)
|
||||||
- Добавление ООП (отдеьные классы по типу User или Post вместо обычного JSON)
|
- Добавление ООП (отдеьные классы по типу User или Post вместо обычного JSON)
|
||||||
- Голосовые сообщения
|
- Голосовые сообщения
|
||||||
|
|
||||||
|
|||||||
@@ -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.reports import report
|
||||||
from itd.routes.search import search
|
from itd.routes.search import search
|
||||||
from itd.routes.files import upload_file
|
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):
|
def refresh_on_error(func):
|
||||||
@@ -38,12 +39,27 @@ class Client:
|
|||||||
else:
|
else:
|
||||||
raise ValueError('Provide token or cookie')
|
raise ValueError('Provide token or cookie')
|
||||||
|
|
||||||
@refresh_on_error
|
|
||||||
def refresh_auth(self):
|
def refresh_auth(self):
|
||||||
if self.cookies:
|
if self.cookies:
|
||||||
self.token = refresh_auth(self.cookies)
|
self.token = refresh_token(self.cookies)
|
||||||
|
return self.token
|
||||||
else:
|
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
|
@refresh_on_error
|
||||||
def get_user(self, username: str) -> dict:
|
def get_user(self, username: str) -> dict:
|
||||||
@@ -78,6 +94,15 @@ class Client:
|
|||||||
return get_following(self.token, username)
|
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
|
@refresh_on_error
|
||||||
def get_who_to_follow(self) -> dict:
|
def get_who_to_follow(self) -> dict:
|
||||||
return get_who_to_follow(self.token)
|
return get_who_to_follow(self.token)
|
||||||
|
|||||||
@@ -36,9 +36,8 @@ def set_cookies(cookies: str):
|
|||||||
for cookie in cookies.split('; '):
|
for cookie in cookies.split('; '):
|
||||||
s.cookies.set(cookie.split('=')[0], cookie.split('=')[-1], path='/', domain='xn--d1ah4a.com.com')
|
s.cookies.set(cookie.split('=')[0], cookie.split('=')[-1], path='/', domain='xn--d1ah4a.com.com')
|
||||||
|
|
||||||
def refresh_auth(cookies: str):
|
def auth_fetch(cookies: str, method: str, url: str, params: dict = {}, token: str | None = None):
|
||||||
print('refresh')
|
headers = {
|
||||||
res = s.post(f'https://xn--d1ah4a.com/api/v1/auth/refresh', timeout=10, headers={
|
|
||||||
"Host": "xn--d1ah4a.com",
|
"Host": "xn--d1ah4a.com",
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0",
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0",
|
||||||
"Accept": "*/*",
|
"Accept": "*/*",
|
||||||
@@ -58,6 +57,13 @@ def refresh_auth(cookies: str):
|
|||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
"Content-Length": "0",
|
"Content-Length": "0",
|
||||||
"TE": "trailers",
|
"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()
|
res.raise_for_status()
|
||||||
return res.json()['accessToken']
|
return res.json()
|
||||||
|
|||||||
10
itd/routes/auth.py
Normal file
10
itd/routes/auth.py
Normal file
@@ -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')
|
||||||
@@ -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):
|
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})
|
return fetch(token, 'get', f'users/{username}/following', {'limit': limit, 'page': page})
|
||||||
|
|
||||||
|
|||||||
8
itd/routes/verification.py
Normal file
8
itd/routes/verification.py
Normal file
@@ -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')
|
||||||
Reference in New Issue
Block a user