Skip to content

Commit

Permalink
Merge pull request #258 from lumina37/develop
Browse files Browse the repository at this point in the history
Update 4.5.2
  • Loading branch information
lumina37 authored Feb 11, 2025
2 parents 6da9847 + 27eed0a commit 6676f53
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 97 deletions.
2 changes: 1 addition & 1 deletion aiotieba/api/get_comments/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ class Thread_c:
主题帖信息
Attributes:
title (str): 标题
title (str): 标题内容
fid (int): 所在吧id
fname (str): 所在贴吧名
Expand Down
2 changes: 1 addition & 1 deletion aiotieba/api/get_posts/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ class Thread_p:
Attributes:
text (str): 文本内容
contents (Contents_pt): 正文内容碎片列表
title (str): 标题
title (str): 标题内容
fid (int): 所在吧id
fname (str): 所在贴吧名
Expand Down
2 changes: 1 addition & 1 deletion aiotieba/api/get_recover_info/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from ._api import parse_body, request
from ._classdef import RecoverThread
from ._classdef import RecoverInfo
13 changes: 7 additions & 6 deletions aiotieba/api/get_recover_info/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
from ...core import HttpCore
from ...exception import TiebaServerError
from ...helper import parse_json
from ._classdef import RecoverThread
from ._classdef import RecoverInfo


def parse_body(body: bytes) -> RecoverThread:
def parse_body(body: bytes) -> RecoverInfo:
res_json = parse_json(body)
if code := res_json["no"]:
raise TiebaServerError(code, res_json["error"])

data_map = res_json["data"]
rec_thread = RecoverThread.from_tbdata(data_map)
rec_info = RecoverInfo.from_tbdata(data_map)

return rec_thread
return rec_info


async def request(http_core: HttpCore, fid: int, tid: int) -> RecoverThread:
async def request(http_core: HttpCore, fid: int, tid: int, pid: int) -> RecoverInfo:
params = [
("forum_id", fid),
("thread_id", tid),
("post_id", pid),
("type", 1),
("sub_type", 1),
("sub_type", 2 if pid else 1),
]

request = http_core.pack_web_get_request(
Expand Down
72 changes: 31 additions & 41 deletions aiotieba/api/get_recover_info/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


@dcs.dataclass
class FragText_rt:
class FragText_ri:
"""
纯文本碎片
Expand All @@ -24,13 +24,13 @@ class FragText_rt:
text: str = ""

@staticmethod
def from_tbdata(data_map: Mapping) -> FragText_rt:
def from_tbdata(data_map: Mapping) -> FragText_ri:
text = data_map["value"]
return FragText_rt(text)
return FragText_ri(text)


@dcs.dataclass
class FragImage_rt:
class FragImage_ri:
"""
图像碎片
Expand All @@ -47,18 +47,18 @@ class FragImage_rt:
hash: str = ""

@staticmethod
def from_tbdata(data_map: Mapping) -> FragImage_rt:
def from_tbdata(data_map: Mapping) -> FragImage_ri:
src = data_map["url"]
show_width = int(data_map["width"])
show_height = int(data_map["height"])

hash_ = _IMAGEHASH_EXP.search(src).group(1)

return FragImage_rt(src, show_width, show_height, hash_)
return FragImage_ri(src, show_width, show_height, hash_)


@dcs.dataclass
class Contents_rt(Containers[TypeFragment]):
class Contents_ri(Containers[TypeFragment]):
"""
内容碎片列表
Expand All @@ -72,21 +72,21 @@ class Contents_rt(Containers[TypeFragment]):
"""

texts: list[TypeFragText] = dcs.field(default_factory=list, repr=False)
imgs: list[FragImage_rt] = dcs.field(default_factory=list, repr=False)
imgs: list[FragImage_ri] = dcs.field(default_factory=list, repr=False)

@staticmethod
def from_tbdata(data_map: Mapping) -> Contents_rt:
def from_tbdata(data_map: Mapping) -> Contents_ri:
content_maps = data_map["content_detail"]

texts = []
imgs = [FragImage_rt.from_tbdata(m) for m in data_map["all_pics"]]
imgs = [FragImage_ri.from_tbdata(m) for m in data_map["all_pics"]]

def _frags():
for cmap in content_maps:
_type = cmap["type"]
# 1纯文本
if _type == 1:
frag = FragText_rt.from_tbdata(cmap)
frag = FragText_ri.from_tbdata(cmap)
texts.append(frag)
yield frag
elif _type == 3:
Expand All @@ -99,7 +99,7 @@ def _frags():
objs = list(_frags())
objs += imgs

return Contents_rt(objs, texts, imgs)
return Contents_ri(objs, texts, imgs)

@cached_property
def text(self) -> str:
Expand All @@ -108,7 +108,7 @@ def text(self) -> str:


@dcs.dataclass
class UserInfo_rt:
class UserInfo_ri:
"""
用户信息
Expand All @@ -127,18 +127,18 @@ class UserInfo_rt:
nick_name_new: str = ""

@staticmethod
def from_tbdata(data_map: Mapping) -> UserInfo_rt:
def from_tbdata(data_map: Mapping) -> UserInfo_ri:
portrait = data_map["portrait"]
if "?" in portrait:
portrait = portrait[:-13]
user_name = data_map["user_name"]
nick_name_new = data_map["show_nickname"]
return UserInfo_rt(portrait, user_name, nick_name_new)
return UserInfo_ri(portrait, user_name, nick_name_new)

def __str__(self) -> str:
return self.user_name or self.portrait

def __eq__(self, obj: UserInfo_rt) -> bool:
def __eq__(self, obj: UserInfo_ri) -> bool:
return self.portrait == obj.portrait

def __hash__(self) -> int:
Expand All @@ -161,51 +161,41 @@ def log_name(self) -> str:


@dcs.dataclass
class RecoverThread(TbErrorExt):
class RecoverInfo(TbErrorExt):
"""
待恢复主题帖信息
待恢复帖子信息
Attributes:
user_id (int): user_id
portrait (str): portrait
user_name (str): 用户名
text (str): 文本内容
contents (Contents_ri): 正文内容碎片列表
title (str): 标题内容
log_name (str): 用于在日志中记录用户信息
tid (int): 所在主题帖id
pid (int): 回复id
user (UserInfo_ri): 发布者的用户信息
"""

contents: Contents_rt = dcs.field(default_factory=Contents_rt)
contents: Contents_ri = dcs.field(default_factory=Contents_ri)
title: str = ""

tid: int = 0
pid: int = 0
user: UserInfo_rt = dcs.field(default_factory=UserInfo_rt)

view_num: int = 0
reply_num: int = 0
share_num: int = 0
agree: int = 0
disagree: int = 0
user: UserInfo_ri = dcs.field(default_factory=UserInfo_ri)

@staticmethod
def from_tbdata(data_map: Mapping) -> RecoverThread:
def from_tbdata(data_map: Mapping) -> RecoverInfo:
thread_info = data_map["thread_info"]

contents = Contents_rt.from_tbdata(thread_info)
contents = Contents_ri.from_tbdata(thread_info)
title = thread_info["title"]

tid = thread_info["thread_id"]
pid = thread_info["post_id"]
user = UserInfo_rt.from_tbdata(data_map["user_info"])

view_num = thread_info["read_num"]
reply_num = thread_info["comment_num"]
share_num = thread_info["share_num"]
agree = thread_info["agree_num"]
disagree = thread_info["disagree_num"]
user = UserInfo_ri.from_tbdata(data_map["user_info"])

return RecoverThread(contents, title, tid, pid, user, view_num, reply_num, share_num, agree, disagree)
return RecoverInfo(contents, title, tid, pid, user)

def __eq__(self, obj: RecoverThread) -> bool:
def __eq__(self, obj: RecoverInfo) -> bool:
return self.pid == obj.pid

def __hash__(self) -> int:
Expand Down
2 changes: 1 addition & 1 deletion aiotieba/api/get_threads/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ class Thread:
Attributes:
text (str): 文本内容
contents (Contents_t): 正文内容碎片列表
title (str): 标题
title (str): 标题内容
fid (int): 所在吧id
fname (str): 所在贴吧名
Expand Down
2 changes: 1 addition & 1 deletion aiotieba/api/get_user_contents/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class UserThread:
Attributes:
text (str): 文本内容
contents (Contents_ut): 正文内容碎片列表
title (str): 标题
title (str): 标题内容
fid (int): 所在吧id
fname (str): 所在贴吧名
Expand Down
2 changes: 1 addition & 1 deletion aiotieba/api/profile/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class Thread_pf:
Attributes:
text (str): 文本内容
contents (Contents_pf): 正文内容碎片列表
title (str): 标题
title (str): 标题内容
fid (int): 所在吧id
fname (str): 所在贴吧名
Expand Down
2 changes: 1 addition & 1 deletion aiotieba/api/search_exact/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ExactSearch:
Attributes:
text (str): 文本内容
title (str): 标题
title (str): 标题内容
fname (str): 所在贴吧名
tid (int): 所在主题帖id
Expand Down
9 changes: 5 additions & 4 deletions aiotieba/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,25 +1346,26 @@ async def get_recovers(

return await get_recovers.request(self._http_core, fid, user_id, pn, rn)

@handle_exception(get_recover_info.RecoverThread)
async def get_recover_info(self, fname_or_fid: str | int, tid: int) -> get_recover_info.RecoverThread:
@handle_exception(get_recover_info.RecoverInfo)
async def get_recover_info(self, fname_or_fid: str | int, tid: int, pid: int = 0) -> get_recover_info.RecoverInfo:
"""
获取单个待恢复帖子信息
Args:
fname_or_fid (str | int): 目标贴吧的贴吧名或fid 优先fid
tid (int): 主题帖id
pid (int, optional): 回复id 若为0则获取主题帖信息. Defaults to 0.
Returns:
RecoverThread: 待恢复帖子信息
RecoverInfo: 待恢复帖子信息
Note:
本接口需要有目标贴吧的吧务身份
"""

fid = fname_or_fid if isinstance(fname_or_fid, int) else await self.__get_fid(fname_or_fid)

return await get_recover_info.request(self._http_core, fid, tid)
return await get_recover_info.request(self._http_core, fid, tid, pid)

@handle_exception(get_bawu_userlogs.Userlogs)
async def get_bawu_userlogs(
Expand Down
1 change: 1 addition & 0 deletions aiotieba/helper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def handle_exception(
"""

def wrapper(func):
@functools.wraps(func)
async def awrapper(self, *args, **kwargs):
def _log(log_level: int, err: Exception | None = None) -> None:
logger = get_logger()
Expand Down
24 changes: 23 additions & 1 deletion docs/tutorial/many_utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ async def sign(BDUSS_key: str, *, retry_times: int = 0):
if success:
break
# 签到
await client.sign_forums() # 先一键签到
retry_list: list[str] = []
for pn in range(1, 9999):
forums = await client.get_self_follow_forums(pn)
retry_list += [forum.fname for forum in forums]
retry_list += [forum.fname for forum in forums if not forum.is_signed]
if not forums.has_more:
break
for _ in range(retry_times + 1):
Expand All @@ -67,6 +68,27 @@ async def main():
await sign("在此处输入另一个待签到账号的BDUSS", retry_times=3)


asyncio.run(main())
```

## 批量封禁

```python
import asyncio

import aiotieba as tb


async def main():
async with tb.Client("在此处输入你的BDUSS") as client:
for uid in [
1111,
2222,
3333,
]:
await client.block("xxx", uid, day=10)


asyncio.run(main())
```

Expand Down
35 changes: 0 additions & 35 deletions docs/tutorial/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,41 +201,6 @@ asyncio.run(main())
账号1: AAAA, 账号2: BBBB
```

## 使用STOKEN

只有少数接口需要用到`STOKEN`,例如用于获取自身关注吧列表的`get_self_follow_forums`

### 样例代码

本样例将获取并打印当前账号的一部分关注吧列表

```python
import asyncio

import aiotieba as tb

BDUSS = "在这里输入你账号的BDUSS"
STOKEN = "在这里输入你账号的网页端STOKEN"


async def main():
account = tb.Account(BDUSS, STOKEN)
async with tb.Client(account=account) as client:
forums = await client.get_self_follow_forums()
print(forums[:3])


asyncio.run(main())
```

### 期望结果

如果你的[`BDUSS`](#bduss)`STOKEN`均填写无误,你会获得类似下面这样的结果

```log
[SelfFollowForum(fid=1999, fname='启动!', level=1), SelfFollowForum(fid=1999, fname='启动!', level=1), SelfFollowForum(fid=1999, fname='启动!', level=1)]
```

## Account的序列化与反序列化

该功能可以用于导出账号参数
Expand Down
Loading

0 comments on commit 6676f53

Please sign in to comment.