diff --git a/itd/client.py b/itd/client.py index d11042e..6b1ec8e 100644 --- a/itd/client.py +++ b/itd/client.py @@ -1,4 +1,5 @@ from itd.users import get_user +from itd.comments import get_comments, add_comment, delete_comment, like_comment, unlike_comment, update_comment class Client: def __init__(self, token: str): @@ -8,4 +9,23 @@ class Client: return get_user(self.token, username) def get_me(self) -> dict: - return self.get_user('me') \ No newline at end of file + return self.get_user('me') + + + def add_comment(self, post_id: int, content: str, reply_comment_id: int | None = None): + return add_comment(self.token, post_id, content, reply_comment_id) + + def get_comments(self, post_id: int, limit: int = 20, cursor: int = 1, sort: str = 'popular'): + return get_comments(self.token, post_id, limit, cursor, sort) + + def like_comment(self, id: int): + return like_comment(self.token, id) + + def unlike_comment(self, id: int): + return unlike_comment(self.token, id) + + def delete_comment(self, id: int): + return delete_comment(self.token, id) + + def update_comment(self, id: int, content: str): + return update_comment(self.token, id, content) \ No newline at end of file diff --git a/itd/comments.py b/itd/comments.py new file mode 100644 index 0000000..3d5c275 --- /dev/null +++ b/itd/comments.py @@ -0,0 +1,22 @@ +from itd.request import fetch + +def add_comment(token: str, post_id: int, content: str, reply_comment_id: int | None = None): + data = {'content': content} + if reply_comment_id: + data['replyTo'] = str(reply_comment_id) + return fetch(token, 'post', f'posts/{post_id}/comments', data) + +def get_comments(token: str, post_id: int, limit: int = 20, cursor: int = 0, sort: str = 'popular'): + return fetch(token, 'get', f'posts/{post_id}/comments', {'limit': limit, 'sort': sort, 'cursor': cursor}) + +def like_comment(token: str, comment_id: int): + return fetch(token, 'post', f'comments/{comment_id}/like') + +def unlike_comment(token: str, comment_id: int): + return fetch(token, 'delete', f'comments/{comment_id}/like') + +def delete_comment(token: str, comment_id: int): + return fetch(token, 'delete', f'comments/{comment_id}') + +def update_comment(token: str, comment_id: int, content: str): + return fetch(token, 'put', f'comments/{comment_id}', {'content': content}) \ No newline at end of file diff --git a/itd/request.py b/itd/request.py index 74707c7..f3a79b6 100644 --- a/itd/request.py +++ b/itd/request.py @@ -3,8 +3,8 @@ from requests import Session s = Session() -def fetch(token: str, method: str, url: str): - res = eval(f's.{method}')(f'https://итд.com/api/{url}', headers={ +def fetch(token: str, method: str, url: str, params: dict = {}): + res = eval(f's.{method}')(f'https://итд.com/api/{url}', params=params, headers={ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3", "Accept-Encoding": "gzip, deflate, br, zstd",