Skip to content

Commit

Permalink
111
Browse files Browse the repository at this point in the history
  • Loading branch information
nooblong committed Dec 3, 2023
1 parent 79d2927 commit eb5430c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- 添加一个监听器(/addListener/包/类,须继承AsyncEvent/监听的type)
- get `http://localhost:9000/addListener/session/Session/1?url=http://localhost:8000&sessdata={{sessdata}}&bili_jct={{bili_jct}}&buvid3={{buvid3}}`
- 触发事件时会调用传过来的url
- 部分type还是要参考源码的枚举类型
- 特殊参数使用eval,注意安全
---
![bilibili-api logo](https://raw.githubusercontent.com/Nemo2011/bilibili-api/main/design/logo.png)

Expand Down
2 changes: 1 addition & 1 deletion bilibili_api/tools/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ print(sync(main()))

同时为了不让所有参数都以字符串传入,还加了类型标注,在变量后使用类似 `:int` 的方式来强制转换,目前支持 `:int` `:float` `:bool` `:parse`

其中 `:parse` 较为特殊,它的作用是解析前面这个字符串,用前面这个网址举例
其中 `:parse` 较为特殊,它的作用是解析前面这个字符串,用 前面这个网址举例

[https://aliyun.nana7mi.link/comment.get_comments(708326075350908930,comment.CommentResourceType.DYNAMIC:parse,1:int)](https://aliyun.nana7mi.link/comment.get_comments(708326075350908930,comment.CommentResourceType.DYNAMIC:parse,1:int))

Expand Down
25 changes: 19 additions & 6 deletions bilibili_api/tools/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ async def __aenter__(self):
把 params 中的参数先解析了
"""
for key, val in self.params.items():
if val.startswith("Credential"):
comma_count = 0
result = ''
for char in val:
if char == ',':
comma_count += 1
if comma_count > 2:
result += '%2C'
else:
result += char
else:
result += char
val = result
obj, err = await self.parse(val)
if err is None:
if isinstance(obj, bilibili_api.Credential):
Expand All @@ -46,12 +59,12 @@ async def __aexit__(self, type, value, trace):
async def transform(self, var: str) -> Any:
"""
类型装换函数
通过在字符串后加上 `:int` `:float` `:bool` `:parse` 等操作符来实现
Args:
var (str): 需要转换的字符串
Returns:
Any: 装换结果
"""
Expand All @@ -69,10 +82,10 @@ async def transform(self, var: str) -> Any:
async def parse(self, path: str) -> Tuple[Any, Optional[str]]:
"""
分析指令
Args:
path (str): 需要解析的 token 对应库中的路径
Returns:
Any: 最终数据 若解析失败为 None
Expand All @@ -90,7 +103,7 @@ async def parse(self, path: str) -> Tuple[Any, Optional[str]]:
async def inner() -> Optional[str]:
"""
递归取值
Returns:
str: 错误信息 若解析成功为 None
"""
Expand All @@ -114,7 +127,7 @@ async def inner() -> Optional[str]:
else:
kwargs[arg[0]] = await self.transform(arg[1])

# print(position, func, args, kwargs)
print(position, func, args, kwargs)

# 开始转移
if isinstance(position, dict):
Expand Down
17 changes: 11 additions & 6 deletions bilibili_api/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ async def get_download_url(
return result

async def my_detect(self, cid):
info = await self.get_download_url(cid=cid)
if cid is None:
info = await self.get_download_url()
else:
info = await self.get_download_url(cid=cid)
detecter = VideoDownloadURLDataDetecter(info)
return detecter.detect_best_streams()

Expand Down Expand Up @@ -573,7 +576,8 @@ async def get_public_notes_list(self, pn: int, ps: int) -> dict:
await Api(**api, credential=self.credential).update_params(**params).result
)

async def get_ai_conclusion(self, cid: Optional[int] = None, page_index: Optional[int] = None, up_mid: Optional[int] = None) -> dict:
async def get_ai_conclusion(self, cid: Optional[int] = None, page_index: Optional[int] = None,
up_mid: Optional[int] = None) -> dict:
"""
获取稿件 AI 总结结果。
Expand All @@ -596,7 +600,8 @@ async def get_ai_conclusion(self, cid: Optional[int] = None, page_index: Optiona
cid = await self.__get_cid_by_index(page_index)

api = API["info"]["ai_conclusion"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid(), "cid": cid, "up_mid": await self.get_up_mid() if up_mid is None else up_mid}
params = {"aid": self.get_aid(), "bvid": self.get_bvid(), "cid": cid,
"up_mid": await self.get_up_mid() if up_mid is None else up_mid}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
Expand Down Expand Up @@ -1072,7 +1077,7 @@ async def get_history_danmaku_index(
return (
await Api(**api, credential=self.credential).update_params(**params).result
)

async def has_liked_danmakus(
self,
page_index: Union[int, None] = None,
Expand Down Expand Up @@ -1967,13 +1972,13 @@ def __unpack(data: bytes):
real_data = []
while offset < len(data):
region_header = struct.unpack(">IIII", data[:16])
region_data = data[offset : offset + region_header[0]]
region_data = data[offset: offset + region_header[0]]
real_data.append(
{
"type": region_header[2],
"number": region_header[3],
"data": json.loads(
region_data[offset + 18 : offset + 18 + (region_header[0] - 16)]
region_data[offset + 18: offset + 18 + (region_header[0] - 16)]
),
}
)
Expand Down
44 changes: 24 additions & 20 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,27 @@ async def call_back(event):


def special_param(param_map, function_name):
# 处理评论
if function_name.lower() == "comment" and "type_" in param_map:
val = int(param_map["type_"])
obj = CommentResourceType(val)
param_map["type_"] = obj

if function_name.lower() == "search_by_type" and "search_type" in param_map:
val = int(param_map["search_type"])
obj = SearchObjectType(val)
param_map["search_type"] = obj

if function_name.lower() == "channelseries" and "type_" in param_map:
val = int(param_map["type_"])
obj = ChannelSeriesType(val)
param_map["type_"] = obj

if function_name.lower() == "get_videos" and "sort" in param_map:
val = (param_map["sort"])
obj = ChannelOrder(val)
param_map["sort"] = obj
for key, value in param_map.items():
if ":parse" in value:
process = value.replace(":parse", "")
param_map[key] = eval(process)

# if function_name.lower() == "comment" and "type_" in param_map:
# val = int(param_map["type_"])
# obj = CommentResourceType(val)
# param_map["type_"] = obj
#
# if function_name.lower() == "search_by_type" and "search_type" in param_map:
# val = int(param_map["search_type"])
# obj = SearchObjectType(val)
# param_map["search_type"] = obj
#
# if function_name.lower() == "channelseries" and "type_" in param_map:
# val = int(param_map["type_"])
# obj = ChannelSeriesType(val)
# param_map["type_"] = obj
#
# if function_name.lower() == "get_videos" and "sort" in param_map:
# val = (param_map["sort"])
# obj = ChannelOrder(val)
# param_map["sort"] = obj

0 comments on commit eb5430c

Please sign in to comment.