Skip to content

Commit

Permalink
Merge pull request #185 from Starry-OvO/develop
Browse files Browse the repository at this point in the history
Update 4.4.1
  • Loading branch information
lumina37 authored Mar 23, 2024
2 parents 8f7a8d5 + f376312 commit 6c569cb
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ tid=8537699088 text=记录一下自己人生第一次当“老师”的经历^_^
## 项目特色

+ 收录[**数十个常用API**](https://github.com/Starry-OvO/aiotieba/tree/develop/aiotieba/api)
+ 类型注解全覆盖,方法注释全覆盖,类属性注释全覆盖,内部命名统一
+ 类型注解全覆盖,方法注释全覆盖,内部命名统一
+ 支持protobuf序列化请求参数
+ 支持websocket接口
+ 与官方版本高度一致的密码学实现

## 友情链接

+ [TiebaManager(吧务管理器 有用户界面)](https://github.com/dog194/TiebaManager)
+ [TiebaLite(第三方安卓客户端 Archived)](https://github.com/HuanCheng65/TiebaLite/tree/4.0-dev)
+ [基于aiotieba的高弹性吧务审查框架](https://github.com/Starry-OvO/aiotieba-reviewer)
+ [贴吧protobuf定义文件合集(更新至12.51.7.1)](https://github.com/n0099/tbclient.protobuf)
+ [TiebaLite 第三方安卓客户端(已停更)](https://github.com/HuanCheng65/TiebaLite/tree/4.0-dev)

## 特别鸣谢

Expand Down
17 changes: 8 additions & 9 deletions aiotieba/api/get_user_contents/get_posts/_api.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import yarl

from ....const import APP_BASE_HOST, APP_SECURE_SCHEME, MAIN_VERSION
from ....const import APP_BASE_HOST, APP_SECURE_SCHEME
from ....core import Account, HttpCore, WsCore
from ....exception import TiebaServerError
from .._classdef import UserPostss
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, is_self: bool) -> bytes:
def pack_proto(account: Account, user_id: int, pn: int, rn: int, version: str) -> bytes:
req_proto = UserPostReqIdl_pb2.UserPostReqIdl()
req_proto.data.common.BDUSS = account.BDUSS
req_proto.data.common._client_version = MAIN_VERSION if is_self else UPOST_VERSION
req_proto.data.common._client_version = version
req_proto.data.user_id = user_id
req_proto.data.need_content = 1
req_proto.data.pn = pn
req_proto.data.rn = rn

return req_proto.SerializeToString()

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


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)
async def request_http(http_core: HttpCore, user_id: int, pn: int, rn: int, version: str) -> UserPostss:
data = pack_proto(http_core.account, user_id, pn, rn, version)

request = http_core.pack_proto_request(
yarl.URL.build(
Expand All @@ -48,8 +47,8 @@ async def request_http(http_core: HttpCore, user_id: int, pn: int, is_self: bool
return parse_body(body)


async def request_ws(ws_core: WsCore, user_id: int, pn: int, is_self: bool) -> UserPostss:
data = pack_proto(ws_core.account, user_id, pn, is_self)
async def request_ws(ws_core: WsCore, user_id: int, pn: int, rn: int, version: str) -> UserPostss:
data = pack_proto(ws_core.account, user_id, pn, rn, version)

response = await ws_core.send(data, CMD)
return parse_body(await response.read())
43 changes: 28 additions & 15 deletions aiotieba/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
)
from .api._classdef import UserInfo
from .config import ProxyConfig, TimeoutConfig
from .const import MAIN_VERSION
from .core import Account, HttpCore, NetCore, WsCore
from .enums import (
BawuPermType,
Expand Down Expand Up @@ -859,36 +860,48 @@ 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.UserPostss)
async def __get_user_posts(self, id_: Union[str, int], pn: int, rn: int):
if not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_

UPOST_VERSION = "8.9.8.5"

return await get_user_contents.get_posts.request_http(self._http_core, user_id, pn, rn, UPOST_VERSION)

@_try_websocket
async def get_user_posts(self, id_: Union[str, int, None] = None, pn: int = 1) -> get_user_contents.UserPostss:
async def __get_self_posts(self, pn: int, rn: int):
user = await self.get_self_info(ReqUInfo.USER_ID)
user_id = user.user_id

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

return await get_user_contents.get_posts.request_http(self._http_core, user_id, pn, rn, MAIN_VERSION)

@handle_exception(get_user_contents.UserPostss)
async def get_user_posts(
self, id_: Union[str, int, None] = None, pn: int = 1, *, rn: int = 20
) -> 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.
rn (int, optional): 请求的条目数. Defaults to 20. Max to 50.
Returns:
UserPostss: 回复列表
"""

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
return await self.__get_self_posts(pn, rn)
else:
user_id = id_

if self._ws_core.status == WsStatus.OPEN:
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_id, pn, is_self)
return await self.__get_user_posts(id_, pn, rn)

@handle_exception(get_user_contents.UserThreads)
@_try_websocket
Expand Down
2 changes: 1 addition & 1 deletion aiotieba/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MAIN_VERSION = "12.57.0.1"
MAIN_VERSION = "12.57.4.2"
POST_VERSION = "12.35.1.0"

APP_SECURE_SCHEME = "https"
Expand Down
15 changes: 8 additions & 7 deletions docs/tutorial/many_utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,26 @@ async def main() -> None:
asyncio.run(main())
```

## 拒绝所有解封申诉
## 清除旧版乱码昵称

```python
import asyncio

from aiotieba_reviewer import get_account

import aiotieba as tb


async def main() -> None:
async def main():
async with tb.Client("在此处输入你的BDUSS") as client:
fname = "待拒绝申诉的贴吧名"
while appeals := await client.get_unblock_appeals(fname, rn=30):
await client.handle_unblock_appeals(fname, [a.appeal_id for a in appeals])
user = await client.get_self_info(tb.ReqUInfo.USER_NAME)
await client.set_nickname_old(user.user_name)


asyncio.run(main())
```

## 清空default账号的粉丝列表(无法复原的危险操作,请谨慎使用!)
## 清空粉丝列表(无法复原的危险操作,请谨慎使用!)

```python
import asyncio
Expand All @@ -181,7 +182,7 @@ async def main() -> None:
asyncio.run(main())
```

## 清除default账号的所有历史回复(无法复原的危险操作,请谨慎使用!)
## 清除所有历史回复(无法复原的危险操作,请谨慎使用!)

```python
import asyncio
Expand Down
4 changes: 2 additions & 2 deletions 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.4.0"
version = "4.4.1"
description = "Asynchronous I/O Client for Baidu Tieba"
authors = [{ name = "Starry-OvO", email = "[email protected]" }]
urls = { Repository = "https://github.com/Starry-OvO/aiotieba/", Documentation = "https://aiotieba.cc/" }
Expand Down Expand Up @@ -63,7 +63,7 @@ speedup = [

[tool.pdm.dev-dependencies]
lint = ["ruff", "black"]
test = ["aiotieba[speedup]", "pytest==8.1.0", "pytest-asyncio==0.23.5", "pytest-rerunfailures==13.0"]
test = ["aiotieba[speedup]", "pytest==8.1.1", "pytest-asyncio==0.23.6", "pytest-rerunfailures==14.0"]
doc = ["mkdocs-material", "mkdocstrings[python]"]

[tool.scikit-build]
Expand Down
30 changes: 30 additions & 0 deletions tests/test_get_user_posts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

import aiotieba as tb


@pytest.mark.flaky(reruns=2, reruns_delay=5.0)
@pytest.mark.asyncio(scope="session")
async def test_get_user_posts(client: tb.Client):
user_id = 4954297652
postss = await client.get_user_posts(user_id)

##### UserPosts #####
posts = postss[0]
assert posts.fid > 0
assert posts.tid > 0

##### UserPost #####
post = posts[0]
assert len(post.contents) > 0
assert post.fid > 0
assert post.tid > 0
assert post.pid > 0
assert post.create_time > 0

##### UserInfo_u #####
user = post.user
assert user.user_id == post.author_id
assert user.portrait != ''
assert user.user_name != ''
assert user.nick_name_new != ''

0 comments on commit 6c569cb

Please sign in to comment.