Skip to content

Commit

Permalink
feat: get_user_posts
Browse files Browse the repository at this point in the history
  • Loading branch information
lumina37 committed Mar 19, 2024
1 parent fa9a2b8 commit c6d3707
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 56 deletions.
11 changes: 6 additions & 5 deletions aiotieba/api/get_user_contents/get_posts/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
from .._const import CMD
from ..protobuf import UserPostReqIdl_pb2, UserPostResIdl_pb2

UPOST_VERSION = "8.9.8.5"

def pack_proto(account: Account, user_id: int, pn: int) -> bytes:

def pack_proto(account: Account, user_id: int, pn: int, is_self: bool) -> bytes:
req_proto = UserPostReqIdl_pb2.UserPostReqIdl()
req_proto.data.common.BDUSS = account.BDUSS
req_proto.data.common._client_version = MAIN_VERSION
req_proto.data.common._client_version = MAIN_VERSION if is_self else UPOST_VERSION
req_proto.data.user_id = user_id
req_proto.data.need_content = 1
req_proto.data.pn = pn
req_proto.data.is_view_card = 1

return req_proto.SerializeToString()

Expand All @@ -33,8 +34,8 @@ def parse_body(body: bytes) -> UserPostss:
return upostss


async def request_http(http_core: HttpCore, user_id: int, pn: int) -> UserPostss:
data = pack_proto(http_core.account, user_id, pn)
async def request_http(http_core: HttpCore, user_id: int, pn: int, is_self: bool) -> UserPostss:
data = pack_proto(http_core.account, user_id, pn, is_self)

request = http_core.pack_proto_request(
yarl.URL.build(
Expand Down
78 changes: 28 additions & 50 deletions aiotieba/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,90 +859,68 @@ async def get_dislike_forums(self, pn: int = 1, /, *, rn: int = 20) -> get_disli

return await get_dislike_forums.request_http(self._http_core, pn, rn)

@handle_exception(get_user_contents.UserThreads)
@_try_websocket
async def get_self_public_threads(self, pn: int = 1) -> get_user_contents.UserThreads:
"""
获取本人发布的公开状态的主题帖列表
Args:
pn (int, optional): 页码. Defaults to 1.
Returns:
UserThreads: 主题帖列表
"""

user = await self.get_self_info(ReqUInfo.USER_ID)

if self._ws_core.status == WsStatus.OPEN:
return await get_user_contents.get_threads.request_ws(self._ws_core, user.user_id, pn, public_only=True)

return await get_user_contents.get_threads.request_http(self._http_core, user.user_id, pn, public_only=True)

@handle_exception(get_user_contents.UserThreads)
@_try_websocket
async def get_self_threads(self, pn: int = 1) -> get_user_contents.UserThreads:
"""
获取本人发布的主题帖列表
Args:
pn (int, optional): 页码. Defaults to 1.
Returns:
UserThreads: 主题帖列表
"""

user = await self.get_self_info(ReqUInfo.USER_ID)

if self._ws_core.status == WsStatus.OPEN:
return await get_user_contents.get_threads.request_ws(self._ws_core, user.user_id, pn, public_only=False)

return await get_user_contents.get_threads.request_http(self._http_core, user.user_id, pn, public_only=False)

@handle_exception(get_user_contents.UserPostss)
@_try_websocket
async def get_self_posts(self, pn: int = 1) -> get_user_contents.UserPostss:
async def get_user_posts(self, id_: Union[str, int, None] = None, pn: int = 1) -> get_user_contents.UserPostss:
"""
获取本人发布的回复列表
获取用户发布的回复列表
Args:
id_ (str | int | None): 用户id user_id / user_name / portrait 优先user_id
默认为None即获取本账号信息. Defaults to None.
pn (int, optional): 页码. Defaults to 1.
Returns:
UserPostss: 回复列表
"""

user = await self.get_self_info(ReqUInfo.USER_ID)
is_self = False
if id_ is None:
user = await self.get_self_info(ReqUInfo.USER_ID)
user_id = user.user_id
is_self = True
elif not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_

if self._ws_core.status == WsStatus.OPEN:
return await get_user_contents.get_posts.request_ws(self._ws_core, user.user_id, pn)
return await get_user_contents.get_posts.request_ws(self._ws_core, user_id, pn, is_self)

return await get_user_contents.get_posts.request_http(self._http_core, user.user_id, pn)
return await get_user_contents.get_posts.request_http(self._http_core, user_id, pn, is_self)

@handle_exception(get_user_contents.UserThreads)
@_try_websocket
async def get_user_threads(self, id_: Union[str, int], pn: int = 1) -> get_user_contents.UserThreads:
async def get_user_threads(
self, id_: Union[str, int, None] = None, pn: int = 1, *, public_only: bool = False
) -> get_user_contents.UserThreads:
"""
获取用户发布的主题帖列表
Args:
id_ (str | int): 用户id user_id / user_name / portrait 优先user_id
id_ (str | int | None): 用户id user_id / user_name / portrait 优先user_id
默认为None即获取本账号信息. Defaults to None.
pn (int, optional): 页码. Defaults to 1.
public_only (bool, optional): 是否仅获取公开主题帖 该选项在获取他人主题帖时无效. Defaults to False.
Returns:
UserThreads: 主题帖列表
"""

if not isinstance(id_, int):
if id_ is None:
user = await self.get_self_info(ReqUInfo.USER_ID)
user_id = user.user_id
elif not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_

if self._ws_core.status == WsStatus.OPEN:
return await get_user_contents.get_threads.request_ws(self._ws_core, user_id, pn, public_only=True)
return await get_user_contents.get_threads.request_ws(self._ws_core, user_id, pn, public_only)

return await get_user_contents.get_threads.request_http(self._http_core, user_id, pn, public_only=True)
return await get_user_contents.get_threads.request_http(self._http_core, user_id, pn, public_only)

@handle_exception(get_replys.Replys)
@_try_websocket
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"

[project]
name = "aiotieba"
version = "4.3.1"
version = "4.4.0"
description = "Asynchronous I/O Client for Baidu Tieba"
authors = [{ name = "Starry-OvO", email = "starry.qvq@gmail.com" }]
urls = { Repository = "https://github.com/Starry-OvO/aiotieba/", Documentation = "https://aiotieba.cc/" }
Expand Down

0 comments on commit c6d3707

Please sign in to comment.