feat: add refresh token on 401

This commit is contained in:
firedotguy
2026-01-29 22:01:25 +03:00
parent b2f4a359e0
commit 0a78fe219c
2 changed files with 45 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
from requests.exceptions import HTTPError
from itd.users import get_user from itd.users import get_user
from itd.comments import get_comments, add_comment, delete_comment, like_comment, unlike_comment from itd.comments import get_comments, add_comment, delete_comment, like_comment, unlike_comment
from itd.hashtags import get_hastags, get_posts_by_hastag from itd.hashtags import get_hastags, get_posts_by_hastag
@@ -5,7 +7,19 @@ from itd.notifications import get_notifications, mark_as_read, mark_all_as_read,
from itd.posts import create_post, get_posts, get_post, edit_post, delete_post, pin_post, repost, view_post from itd.posts import create_post, get_posts, get_post, edit_post, delete_post, pin_post, repost, view_post
from itd.reports import report from itd.reports import report
from itd.search import search from itd.search import search
from itd.request import refresh_auth, set_cookies from itd.request import refresh_auth
def refresh_on_error(func):
def wrapper(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except HTTPError as e:
if '401' in str(e):
self.refresh_auth()
return func(self, *args, **kwargs)
raise e
return wrapper
class Client: class Client:
@@ -19,98 +33,127 @@ 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_auth(self.cookies)
else: else:
print('no cookies!') print('no cookies!')
@refresh_on_error
def get_user(self, username: str) -> dict: def get_user(self, username: str) -> dict:
return get_user(self.token, username) return get_user(self.token, username)
@refresh_on_error
def get_me(self) -> dict: def get_me(self) -> dict:
return self.get_user('me') return self.get_user('me')
@refresh_on_error
def add_comment(self, post_id: str, content: str, reply_comment_id: str | None = None): def add_comment(self, post_id: str, content: str, reply_comment_id: str | None = None):
return add_comment(self.token, post_id, content, reply_comment_id) return add_comment(self.token, post_id, content, reply_comment_id)
@refresh_on_error
def get_comments(self, post_id: str, limit: int = 20, cursor: int = 0, sort: str = 'popular'): def get_comments(self, post_id: str, limit: int = 20, cursor: int = 0, sort: str = 'popular'):
return get_comments(self.token, post_id, limit, cursor, sort) return get_comments(self.token, post_id, limit, cursor, sort)
@refresh_on_error
def like_comment(self, id: str): def like_comment(self, id: str):
return like_comment(self.token, id) return like_comment(self.token, id)
@refresh_on_error
def unlike_comment(self, id: str): def unlike_comment(self, id: str):
return unlike_comment(self.token, id) return unlike_comment(self.token, id)
@refresh_on_error
def delete_comment(self, id: str): def delete_comment(self, id: str):
return delete_comment(self.token, id) return delete_comment(self.token, id)
@refresh_on_error
def get_hastags(self, limit: int = 10): def get_hastags(self, limit: int = 10):
return get_hastags(self.token, limit) return get_hastags(self.token, limit)
@refresh_on_error
def get_posts_by_hashtag(self, hashtag: str, limit: int = 20, cursor: int = 0): def get_posts_by_hashtag(self, hashtag: str, limit: int = 20, cursor: int = 0):
return get_posts_by_hastag(self.token, hashtag, limit, cursor) return get_posts_by_hastag(self.token, hashtag, limit, cursor)
@refresh_on_error
def get_notifications(self, limit: int = 20, cursor: int = 0, type: str | None = None): def get_notifications(self, limit: int = 20, cursor: int = 0, type: str | None = None):
return get_notifications(self.token, limit, cursor, type) return get_notifications(self.token, limit, cursor, type)
@refresh_on_error
def mark_as_read(self, id: str): def mark_as_read(self, id: str):
return mark_as_read(self.token, id) return mark_as_read(self.token, id)
@refresh_on_error
def mark_all_as_read(self): def mark_all_as_read(self):
return mark_all_as_read(self.token) return mark_all_as_read(self.token)
@refresh_on_error
def get_unread_notifications_count(self): def get_unread_notifications_count(self):
return get_unread_notifications_count(self.token) return get_unread_notifications_count(self.token)
@refresh_on_error
def create_post(self, content: str, wall_recipient_id: int | None = None, attach_ids: list[str] = []): def create_post(self, content: str, wall_recipient_id: int | None = None, attach_ids: list[str] = []):
return create_post(self.token, content, wall_recipient_id, attach_ids) return create_post(self.token, content, wall_recipient_id, attach_ids)
@refresh_on_error
def get_posts(self, username: str | None = None, limit: int = 20, cursor: int = 0, sort: str = '', tab: str = ''): def get_posts(self, username: str | None = None, limit: int = 20, cursor: int = 0, sort: str = '', tab: str = ''):
return get_posts(self.token, username, limit, cursor, sort, tab) return get_posts(self.token, username, limit, cursor, sort, tab)
@refresh_on_error
def get_post(self, id: str): def get_post(self, id: str):
return get_post(self.token, id) return get_post(self.token, id)
@refresh_on_error
def edit_post(self, id: str, content: str): def edit_post(self, id: str, content: str):
return edit_post(self.token, id, content) return edit_post(self.token, id, content)
@refresh_on_error
def delete_post(self, id: str): def delete_post(self, id: str):
return delete_post(self.token, id) return delete_post(self.token, id)
@refresh_on_error
def pin_post(self, id: str): def pin_post(self, id: str):
return pin_post(self.token, id) return pin_post(self.token, id)
@refresh_on_error
def repost(self, id: str, content: str | None = None): def repost(self, id: str, content: str | None = None):
return repost(self.token, id, content) return repost(self.token, id, content)
@refresh_on_error
def view_post(self, id: str): def view_post(self, id: str):
return view_post(self.token, id) return view_post(self.token, id)
@refresh_on_error
def report(self, id: str, type: str = 'post', reason: str = 'other', description: str = ''): def report(self, id: str, type: str = 'post', reason: str = 'other', description: str = ''):
return report(self.token, id, type, reason, description) return report(self.token, id, type, reason, description)
@refresh_on_error
def report_user(self, id: str, reason: str = 'other', description: str = ''): def report_user(self, id: str, reason: str = 'other', description: str = ''):
return report(self.token, id, 'user', reason, description) return report(self.token, id, 'user', reason, description)
@refresh_on_error
def report_post(self, id: str, reason: str = 'other', description: str = ''): def report_post(self, id: str, reason: str = 'other', description: str = ''):
return report(self.token, id, 'post', reason, description) return report(self.token, id, 'post', reason, description)
@refresh_on_error
def report_comment(self, id: str, reason: str = 'other', description: str = ''): def report_comment(self, id: str, reason: str = 'other', description: str = ''):
return report(self.token, id, 'comment', reason, description) return report(self.token, id, 'comment', reason, description)
@refresh_on_error
def search(self, query: str, user_limit: int = 5, hashtag_limit: int = 5): def search(self, query: str, user_limit: int = 5, hashtag_limit: int = 5):
return search(self.token, query, user_limit, hashtag_limit) return search(self.token, query, user_limit, hashtag_limit)
@refresh_on_error
def search_user(self, query: str, limit: int = 5): def search_user(self, query: str, limit: int = 5):
return search(self.token, query, limit, 0) return search(self.token, query, limit, 0)
@refresh_on_error
def search_hashtag(self, query: str, limit: int = 5): def search_hashtag(self, query: str, limit: int = 5):
return search(self.token, query, 0, limit) return search(self.token, query, 0, limit)

View File

@@ -28,7 +28,7 @@ def set_cookies(cookies: str):
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 refresh_auth(cookies: str):
print(s.cookies.get_dict()) print('refresh')
res = s.post(f'https://xn--d1ah4a.com/api/v1/auth/refresh', timeout=10, 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",
@@ -51,5 +51,4 @@ def refresh_auth(cookies: str):
"TE": "trailers", "TE": "trailers",
}) })
res.raise_for_status() res.raise_for_status()
print(res.text)
return res.json()['accessToken'] return res.json()['accessToken']