feat: add comments
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
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, update_comment
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
def __init__(self, token: str):
|
def __init__(self, token: str):
|
||||||
@@ -8,4 +9,23 @@ class Client:
|
|||||||
return get_user(self.token, username)
|
return get_user(self.token, username)
|
||||||
|
|
||||||
def get_me(self) -> dict:
|
def get_me(self) -> dict:
|
||||||
return self.get_user('me')
|
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)
|
||||||
22
itd/comments.py
Normal file
22
itd/comments.py
Normal file
@@ -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})
|
||||||
@@ -3,8 +3,8 @@ from requests import Session
|
|||||||
s = Session()
|
s = Session()
|
||||||
|
|
||||||
|
|
||||||
def fetch(token: str, method: str, url: str):
|
def fetch(token: str, method: str, url: str, params: dict = {}):
|
||||||
res = eval(f's.{method}')(f'https://итд.com/api/{url}', headers={
|
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": "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-Language": "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3",
|
||||||
"Accept-Encoding": "gzip, deflate, br, zstd",
|
"Accept-Encoding": "gzip, deflate, br, zstd",
|
||||||
|
|||||||
Reference in New Issue
Block a user