nonebot-plugin-parsehub 1.0.2__tar.gz → 1.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/PKG-INFO +3 -2
  2. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/README.md +1 -0
  3. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/pyproject.toml +2 -2
  4. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/pyproject.toml.orig +2 -2
  5. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/SPEC.md +2 -3
  6. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/keys.py +53 -5
  7. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/service.py +5 -52
  8. nonebot_plugin_parsehub-1.0.2/src/nonebot_plugin_parsehub/bilibili.py +0 -252
  9. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/LICENSE +0 -0
  10. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/__init__.py +0 -0
  11. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/app.py +0 -0
  12. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/baidu.png +0 -0
  13. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/bilibili.png +0 -0
  14. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/coolapk.png +0 -0
  15. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/douban.png +0 -0
  16. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/douyin.png +0 -0
  17. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/facebook.png +0 -0
  18. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/instagram.png +0 -0
  19. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/kuaishou.png +0 -0
  20. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/medium.png +0 -0
  21. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/pipix.png +0 -0
  22. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/snapchat.png +0 -0
  23. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/threads.png +0 -0
  24. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/tiktok.png +0 -0
  25. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/toutiao.png +0 -0
  26. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/wechat.png +0 -0
  27. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/weibo.png +0 -0
  28. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/x.png +0 -0
  29. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/xiaoheihe.png +0 -0
  30. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/xiaohongshu.png +0 -0
  31. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/youtube.png +0 -0
  32. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/zhihu.png +0 -0
  33. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/assets/logos/zuiyou.png +0 -0
  34. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/budget.py +0 -0
  35. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/cache.py +0 -0
  36. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/card/SPEC.md +0 -0
  37. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/card/__init__.py +0 -0
  38. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/card/cover.py +0 -0
  39. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/card/fonts.py +0 -0
  40. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/card/layout.py +0 -0
  41. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/config.py +0 -0
  42. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/dedupe.py +0 -0
  43. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/delivery.py +0 -0
  44. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/errors.py +0 -0
  45. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/feedback.py +0 -0
  46. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/media.py +0 -0
  47. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/parsehub_client.py +0 -0
  48. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/paths.py +0 -0
  49. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/platforms.py +0 -0
  50. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/relay.py +0 -0
  51. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/switches.py +0 -0
  52. {nonebot_plugin_parsehub-1.0.2 → nonebot_plugin_parsehub-1.0.4}/src/nonebot_plugin_parsehub/triggers.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot-plugin-parsehub
3
- Version: 1.0.2
3
+ Version: 1.0.4
4
4
  Summary: 基于 ParseHub 的社交媒体链接解析插件:群里出现分享链接时自动解析并发送媒体
5
5
  Keywords: nonebot,nonebot2,plugin,parsehub,parser,media,onebot
6
6
  Author: ByteColtX
@@ -18,7 +18,7 @@ Requires-Dist: nonebot-plugin-localstore>=0.7.4,<1.0.0
18
18
  Requires-Dist: nonebot2>=2.5.0,<3.0.0
19
19
  Requires-Dist: numpy>=2.0.0,<3.0.0
20
20
  Requires-Dist: opencv-python-headless>=4.13.0.92
21
- Requires-Dist: parsehub>=2.3.13,<3.0.0
21
+ Requires-Dist: parsehub>=2.3.14,<3.0.0
22
22
  Requires-Dist: pillow>=12.1.0,<13.0.0
23
23
  Requires-Dist: pydantic>=2.0.0,<3.0.0
24
24
  Requires-Python: >=3.12
@@ -168,6 +168,7 @@ plugins = ["nonebot_plugin_parsehub"]
168
168
 
169
169
  ### 说明
170
170
 
171
+ - B 站链接带 `?p=N` 时解析第 N P,不带时默认 P1;时长按该 P 计算
171
172
  - 视频被协议端拒绝时只点失败表情,**不会**退回群文件
172
173
  - 投递三选一、**互不兜底**:`file` 发本机路径(协议端同机)/ `http` 发中转地址(跨机)/ `base64` 内容内联(请求体约 +1/3)
173
174
  - parsehub 全程同步阻塞,插件把所有调用放进 `asyncio.to_thread`;首次导入懒加载,不影响启动
@@ -139,6 +139,7 @@ plugins = ["nonebot_plugin_parsehub"]
139
139
 
140
140
  ### 说明
141
141
 
142
+ - B 站链接带 `?p=N` 时解析第 N P,不带时默认 P1;时长按该 P 计算
142
143
  - 视频被协议端拒绝时只点失败表情,**不会**退回群文件
143
144
  - 投递三选一、**互不兜底**:`file` 发本机路径(协议端同机)/ `http` 发中转地址(跨机)/ `base64` 内容内联(请求体约 +1/3)
144
145
  - parsehub 全程同步阻塞,插件把所有调用放进 `asyncio.to_thread`;首次导入懒加载,不影响启动
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nonebot-plugin-parsehub"
3
- version = "1.0.2"
3
+ version = "1.0.4"
4
4
  description = "基于 ParseHub 的社交媒体链接解析插件:群里出现分享链接时自动解析并发送媒体"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -29,7 +29,7 @@ dependencies = [
29
29
  "nonebot2>=2.5.0,<3.0.0",
30
30
  "numpy>=2.0.0,<3.0.0",
31
31
  "opencv-python-headless>=4.13.0.92",
32
- "parsehub>=2.3.13,<3.0.0",
32
+ "parsehub>=2.3.14,<3.0.0",
33
33
  "pillow>=12.1.0,<13.0.0",
34
34
  "pydantic>=2.0.0,<3.0.0",
35
35
  ]
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nonebot-plugin-parsehub"
3
- version = "1.0.2"
3
+ version = "1.0.4"
4
4
  description = "基于 ParseHub 的社交媒体链接解析插件:群里出现分享链接时自动解析并发送媒体"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -23,7 +23,7 @@ dependencies = [
23
23
  # 卡片渐变遮罩与视频抽帧是代码里直接 import 的,必须显式声明
24
24
  "numpy>=2.0.0,<3.0.0",
25
25
  "opencv-python-headless>=4.13.0.92",
26
- "parsehub>=2.3.13,<3.0.0",
26
+ "parsehub>=2.3.14,<3.0.0",
27
27
  "pillow>=12.1.0,<13.0.0",
28
28
  "pydantic>=2.0.0,<3.0.0",
29
29
  ]
@@ -36,7 +36,6 @@ tags: [nonebot, parsehub, module]
36
36
  | `delivery.py` | `Sender` 协议、三种上传模式、合并转发、`MediaReport` |
37
37
  | `feedback.py` | `Reaction` 与全部提示文案 |
38
38
  | `parsehub_client.py` | 懒加载客户端、cookie 读取 |
39
- | `bilibili.py` | B 站默认P归一与单P时长查询 |
40
39
  | `cache.py` | 作品级媒体缓存(原子写、TTL、容量) |
41
40
  | `dedupe.py` | 会话级去重 |
42
41
  | `relay.py` | 只读中转服务 |
@@ -76,8 +75,8 @@ tags: [nonebot, parsehub, module]
76
75
  - 第三方依赖全是**硬依赖**:parsehub / Pillow / numpy / opencv / localstore 缺了就是
77
76
  装错了,代码里不写 `except ImportError` 兜底,也不往别的目录写;降级只发生在产品层
78
77
  (卡片→文字、字体→系统字体、体积探测→未知)。
79
- - B 站分P由 `bilibili` 收口:解析前去掉 `p`(只解析默认P),解析后用
80
- `pages[0].duration` 覆盖 ParseHub 给的分P总时长;查不到就保持原值。
78
+ - B 站分P交给上游 ParseHub(2.3.14 起按 `p` 选分P并返回对应时长);插件只在
79
+ `keys.content_key` 里把分P写进作品 key,避免不同 P 撞缓存。
81
80
 
82
81
  ## Known limitations
83
82
 
@@ -14,7 +14,7 @@
14
14
  from __future__ import annotations
15
15
 
16
16
  import re
17
- from urllib.parse import urlsplit
17
+ from urllib.parse import parse_qsl, urlsplit
18
18
 
19
19
  # 预筛白名单:只认这些域名后缀(含子域名)。
20
20
  ALLOWED_HOSTS: tuple[str, ...] = (
@@ -88,6 +88,44 @@ _ID_PATTERNS: tuple[tuple[str, re.Pattern[str]], ...] = (
88
88
  # 归一化时要去掉的主机名前缀(m.bilibili.com 与 www.bilibili.com 是同一个站)。
89
89
  _HOST_PREFIXES = ("www.", "m.", "mobile.")
90
90
 
91
+ # B 站短链没有作品 ID,但分P要进 key,否则同一个短链的不同 P 会撞缓存。
92
+ _BILIBILI_SHORT_HOSTS = ("b23.tv", "bili2233.cn")
93
+
94
+
95
+ def _bilibili_part(url: str) -> int:
96
+ """取 B 站链接里的分P序号。
97
+
98
+ Args:
99
+ url: 链接。
100
+
101
+ Returns:
102
+ 大于 0 的分P序号;没写或非法时为默认P(1)。
103
+ """
104
+ for key, value in parse_qsl(urlsplit(url).query, keep_blank_values=True):
105
+ if key.lower() != "p":
106
+ continue
107
+ try:
108
+ part = int(value)
109
+ except (TypeError, ValueError):
110
+ return 1
111
+ return part if part > 0 else 1
112
+ return 1
113
+
114
+
115
+ def _is_bilibili_short_host(host: str) -> bool:
116
+ """判断主机是不是 B 站短链站。
117
+
118
+ Args:
119
+ host: 已小写的主机名。
120
+
121
+ Returns:
122
+ 命中返回 True。
123
+ """
124
+ return any(
125
+ host == item or host.endswith("." + item)
126
+ for item in _BILIBILI_SHORT_HOSTS
127
+ )
128
+
91
129
 
92
130
  def extract_urls(text: str) -> list[str]:
93
131
  """从消息文本里挑出可能受支持的链接。
@@ -134,8 +172,8 @@ def content_key(url: str) -> str:
134
172
  认得出作品 ID 的平台直接用 ``平台:id``,这样同一个作品的两种写法能对上——比如
135
173
  抖音的 ``v.douyin.com/xxx`` 和 ``iesdouyin.com/share/video/<id>``、B 站的
136
174
  ``b23.tv/xxx`` 和 ``bilibili.com/video/BVxxxx``(解析结果里的 ``raw_url``
137
- 已经是短链展开后的地址)。认不出来的退化成「主机 + 路径」,丢掉查询串和末尾
138
- 斜杠。
175
+ 已经是短链展开后的地址)。B 站带 ``p`` 时把分P也写进 key,避免不同 P
176
+ 撞缓存。认不出来的退化成「主机 + 路径」,丢掉查询串和末尾斜杠。
139
177
 
140
178
  Args:
141
179
  url: 链接,可以是用户发的原始链接,也可以是 ParseHub 给的 ``raw_url``。
@@ -146,7 +184,12 @@ def content_key(url: str) -> str:
146
184
  for name, pattern in _ID_PATTERNS:
147
185
  match = pattern.search(url)
148
186
  if match:
149
- return f"{name}:{match.group(1).lower()}"
187
+ key = f"{name}:{match.group(1).lower()}"
188
+ if name == "bilibili":
189
+ part = _bilibili_part(url)
190
+ if part > 1:
191
+ key += f":p{part}"
192
+ return key
150
193
 
151
194
  parsed = urlsplit(url)
152
195
  host = parsed.netloc.lower()
@@ -154,4 +197,9 @@ def content_key(url: str) -> str:
154
197
  if host.startswith(prefix):
155
198
  host = host[len(prefix) :]
156
199
  break
157
- return f"{host}{parsed.path.rstrip('/')}".lower()
200
+ key = f"{host}{parsed.path.rstrip('/')}".lower()
201
+ if _is_bilibili_short_host(host):
202
+ part = _bilibili_part(url)
203
+ if part > 1:
204
+ key += f":p{part}"
205
+ return key
@@ -31,7 +31,7 @@ from typing import TYPE_CHECKING, Any
31
31
  from nonebot import logger
32
32
  from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
33
33
 
34
- from . import bilibili, errors, feedback, keys, media, platforms
34
+ from . import errors, feedback, keys, media, platforms
35
35
  from .budget import Budget
36
36
  from .cache import CacheEntry
37
37
  from .delivery import MediaReport, Sender
@@ -253,8 +253,6 @@ async def _deliver( # noqa: C901 - 顺序编排本身就是一条长流水线
253
253
  本次处理的结局。
254
254
  """
255
255
  settings = app.settings
256
- # B 站只解析默认P:去掉 p 参数,避免用户发的 ?p=2 被当成这次要发的分P。
257
- parse_url = bilibili.default_part_url(url)
258
256
 
259
257
  # 本地已经有这个作品的媒体就直接发,省掉解析、下载和画卡。
260
258
  cached = await asyncio.to_thread(app.cache.get, pre_key)
@@ -262,7 +260,7 @@ async def _deliver( # noqa: C901 - 顺序编排本身就是一条长流水线
262
260
  return await _deliver_cached(app, sender, event, cached, pre_key)
263
261
 
264
262
  try:
265
- supported = await app.client.is_supported(parse_url)
263
+ supported = await app.client.is_supported(url)
266
264
  except Exception as exc: # noqa: BLE001 - 判断失败就交给解析去报错
267
265
  logger.warning(
268
266
  f"[parsehub] 平台支持判断失败 {url}: {type(exc).__name__}: {exc}"
@@ -278,20 +276,10 @@ async def _deliver( # noqa: C901 - 顺序编排本身就是一条长流水线
278
276
  cookie = await asyncio.to_thread(platform_cookie, platform, settings.cookie)
279
277
  logger.debug(f"[parsehub] {platform} 代理={proxy or '直连'}")
280
278
 
281
- # 短链展开后可能带 p,必须在解析前再归一一次;展开失败就用原链接。
282
- try:
283
- async with budget.timeout():
284
- parse_url = await bilibili.resolve_default_part_url(url, proxy)
285
- except Exception as exc: # noqa: BLE001 - 展开失败交给 ParseHub 自己处理
286
- logger.debug(
287
- f"[parsehub] 归一 B 站默认P失败: {type(exc).__name__}: {exc}"
288
- )
289
- parse_url = bilibili.default_part_url(url)
290
-
291
279
  started = time.monotonic()
292
280
  try:
293
281
  async with budget.timeout():
294
- parsed = await app.client.parse(parse_url, proxy, cookie)
282
+ parsed = await app.client.parse(url, proxy, cookie)
295
283
  except Exception as exc: # noqa: BLE001 - parsehub 异常类型很多,统一兜底
296
284
  failure = errors.translate(exc)
297
285
  logger.warning(
@@ -302,7 +290,7 @@ async def _deliver( # noqa: C901 - 顺序编排本身就是一条长流水线
302
290
  notice = feedback.failure_notice(exc)
303
291
  return Result(Outcome.PARSE_FAILED, notice=notice, keys=(pre_key,))
304
292
 
305
- post_key = keys.content_key(getattr(parsed, "raw_url", "") or parse_url)
293
+ post_key = keys.content_key(getattr(parsed, "raw_url", "") or url)
306
294
  all_keys = tuple(dict.fromkeys([pre_key, post_key]))
307
295
  # 平台 id 以解析结果为准(client 可能拿不到),时长单位也跟着它。
308
296
  platform_id = str(
@@ -323,14 +311,6 @@ async def _deliver( # noqa: C901 - 顺序编排本身就是一条长流水线
323
311
  logger.info(f"[parsehub] {post_key} 在本会话刚发过,跳过发送")
324
312
  return Result(Outcome.DUPLICATE, keys=all_keys)
325
313
 
326
- # ParseHub 的 B 站结果里 duration 是所有分P的总和,这里用默认P的时长覆盖。
327
- if platform_id.lower() == "bilibili":
328
- lookup_url = getattr(parsed, "raw_url", "") or parse_url
329
- page_seconds = await _bilibili_seconds(
330
- lookup_url, proxy, cookie, budget
331
- )
332
- bilibili.apply_duration(parsed, page_seconds)
333
-
334
314
  refs = media.refs_from(parsed)
335
315
  seconds = platforms.duration_seconds(
336
316
  platform_id, (ref.duration for ref in refs)
@@ -390,7 +370,7 @@ async def _deliver( # noqa: C901 - 顺序编排本身就是一条长流水线
390
370
  proxy,
391
371
  allow_text=False,
392
372
  ),
393
- _download(app, parse_url, proxy, cookie, budget, parsed),
373
+ _download(app, url, proxy, cookie, budget, parsed),
394
374
  )
395
375
  if files is None:
396
376
  return Result(
@@ -428,33 +408,6 @@ async def _deliver( # noqa: C901 - 顺序编排本身就是一条长流水线
428
408
  return Result(outcome, report=report, keys=all_keys)
429
409
 
430
410
 
431
- async def _bilibili_seconds(
432
- url: str,
433
- proxy: str,
434
- cookie: str,
435
- budget: Budget,
436
- ) -> int:
437
- """查询 B 站默认P的时长,失败时返回 0。
438
-
439
- Args:
440
- url: 已展开的 B 站视频链接。
441
- proxy: 解析代理。
442
- cookie: 登录态 cookie。
443
- budget: 本次工作的超时预算。
444
-
445
- Returns:
446
- 默认P的秒数;查询失败或不是视频时返回 0。
447
- """
448
- try:
449
- async with budget.timeout():
450
- return await bilibili.default_part_seconds(url, proxy, cookie)
451
- except Exception as exc: # noqa: BLE001 - 查时长失败不能影响主流程
452
- logger.debug(
453
- f"[parsehub] 查询 B 站默认P时长失败: {type(exc).__name__}: {exc}"
454
- )
455
- return 0
456
-
457
-
458
411
  async def _deliver_cached(
459
412
  app: App,
460
413
  sender: Sender,
@@ -1,252 +0,0 @@
1
- # Copyright 2026 ByteColtX
2
- # SPDX-License-Identifier: MIT
3
-
4
- """B 站分P兼容:只解析默认P,并按默认P的时长判断。
5
-
6
- ParseHub 的 B 站解析器在 ``p=1`` 时直接取 ``View.duration``,而 B 站这个字段
7
- 是**所有分P的时长总和**;分P越多,插件越容易误判成超长视频。这里在解析前后各
8
- 补一刀:
9
-
10
- 1. 解析前去掉 ``p`` 参数,让 ParseHub 始终解析默认P;
11
- 2. 解析后向 B 站查一次 ``pages[0].duration``,覆盖结果里的总时长。
12
-
13
- 第 2 步失败时保持原样(退化成旧行为),不影响解析和下载。
14
- """
15
-
16
- from __future__ import annotations
17
-
18
- import re
19
- from typing import Any
20
- from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
21
-
22
- import httpx
23
- from nonebot import logger
24
-
25
- # B 站视频 ID。BV 号大小写敏感,av 号只在路径或参数里认。
26
- _BV_PATTERN = re.compile(r"(BV[0-9A-Za-z]{10})")
27
- _AV_PATTERN = re.compile(r"(?:/|=)av(\d+)", re.IGNORECASE)
28
- _AID_PATTERN = re.compile(r"[?&]aid=(\d+)")
29
-
30
- # 需要做默认P归一的主机。短链没有 ID,但同样可能带 p 参数。
31
- _VIDEO_HOSTS = ("bilibili.com", "b23.tv", "bili2233.cn")
32
- _SHORT_HOSTS = ("b23.tv", "bili2233.cn")
33
-
34
- _VIEW_API = "https://api.bilibili.com/x/web-interface/view"
35
- _USER_AGENT = (
36
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
37
- "(KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
38
- )
39
- _TIMEOUT_SECONDS = 5.0
40
-
41
-
42
- def _video_params(url: str) -> dict[str, str] | None:
43
- """从链接里取出 B 站视频 ID。
44
-
45
- Args:
46
- url: 可能是完整链或短链展开后的链接。
47
-
48
- Returns:
49
- ``{"bvid": ...}`` 或 ``{"aid": ...}``;认不出来时返回 None。
50
- """
51
- match = _BV_PATTERN.search(url)
52
- if match:
53
- return {"bvid": match.group(1)}
54
- match = _AV_PATTERN.search(url)
55
- if match:
56
- return {"aid": match.group(1)}
57
- match = _AID_PATTERN.search(url)
58
- if match:
59
- return {"aid": match.group(1)}
60
- return None
61
-
62
-
63
- def _is_video_host(host: str) -> bool:
64
- """判断主机是不是 B 站视频站(含短链站)。
65
-
66
- Args:
67
- host: 已小写的主机名。
68
-
69
- Returns:
70
- 命中返回 True。
71
- """
72
- return any(
73
- host == item or host.endswith("." + item) for item in _VIDEO_HOSTS
74
- )
75
-
76
-
77
- def _is_short_host(host: str) -> bool:
78
- """判断主机是不是 B 站短链站。
79
-
80
- Args:
81
- host: 已小写的主机名。
82
-
83
- Returns:
84
- 命中返回 True。
85
- """
86
- return any(
87
- host == item or host.endswith("." + item) for item in _SHORT_HOSTS
88
- )
89
-
90
-
91
- def default_part_url(url: str) -> str:
92
- """去掉 B 站视频链接里的 ``p`` 参数。
93
-
94
- 去掉后 ParseHub 会解析默认P(P1);对短链也先去掉,展开后即使带 ``p``
95
- 也不会被保留。
96
-
97
- Args:
98
- url: 待解析的链接。
99
-
100
- Returns:
101
- 去掉 ``p`` 后的链接;非 B 站视频链接原样返回。
102
- """
103
- parsed = urlsplit(url)
104
- host = (parsed.hostname or "").lower()
105
- if not _is_video_host(host):
106
- return url
107
- if not _is_short_host(host) and _video_params(url) is None:
108
- return url
109
- query = [
110
- (key, value)
111
- for key, value in parse_qsl(parsed.query, keep_blank_values=True)
112
- if key.lower() != "p"
113
- ]
114
- return urlunsplit(parsed._replace(query=urlencode(query)))
115
-
116
-
117
- async def resolve_default_part_url(url: str, proxy: str = "") -> str:
118
- """展开短链后再去掉 ``p``,确保短链也只解析默认P。
119
-
120
- 短链本身没有 ID,ParseHub 展开时会把重定向目标里的 ``p`` 保留下来;这里先
121
- 自己展开一次,拿到完整链后统一去掉 ``p``。展开失败就退回原链接。
122
-
123
- Args:
124
- url: 用户发的链接。
125
- proxy: 展开短链用的代理。
126
-
127
- Returns:
128
- 指向默认P的完整链;非 B 站短链原样返回。
129
- """
130
- parsed = urlsplit(url)
131
- host = (parsed.hostname or "").lower()
132
- if not _is_short_host(host):
133
- return default_part_url(url)
134
- try:
135
- async with _client(proxy) as client:
136
- response = await client.get(
137
- url,
138
- headers={"User-Agent": _USER_AGENT},
139
- follow_redirects=True,
140
- )
141
- response.raise_for_status()
142
- resolved = str(response.url)
143
- except Exception as exc: # noqa: BLE001 - 展开失败交给 ParseHub 自己处理
144
- logger.debug(
145
- f"[parsehub] 展开 B 站短链失败: {type(exc).__name__}: {exc}"
146
- )
147
- return default_part_url(url)
148
- # 重定向到非视频页时把短链还给 ParseHub,让它按原链接选解析器。
149
- if _video_params(resolved) is None:
150
- return default_part_url(url)
151
- return default_part_url(resolved)
152
-
153
-
154
- def default_page_seconds(data: Any) -> int: # noqa: ANN401 - B 站接口 JSON
155
- """从 B 站 ``view`` 接口的 ``data`` 里取默认P时长。
156
-
157
- Args:
158
- data: 接口返回的 ``data`` 字段。
159
-
160
- Returns:
161
- 默认P(``pages[0]``)的秒数;拿不到时退回总时长,再拿不到返回 0。
162
- """
163
- if not isinstance(data, dict):
164
- return 0
165
- pages = data.get("pages")
166
- if isinstance(pages, list) and pages and isinstance(pages[0], dict):
167
- try:
168
- return max(int(pages[0].get("duration") or 0), 0)
169
- except (TypeError, ValueError):
170
- pass
171
- try:
172
- return max(int(data.get("duration") or 0), 0)
173
- except (TypeError, ValueError):
174
- return 0
175
-
176
-
177
- def _client(proxy: str) -> httpx.AsyncClient:
178
- """构造查接口用的 HTTP 客户端(测试里替换成不联网的假实现)。
179
-
180
- Args:
181
- proxy: 代理地址,空字符串表示直连。
182
-
183
- Returns:
184
- 异步 HTTP 客户端。
185
- """
186
- return httpx.AsyncClient(
187
- proxy=proxy or None,
188
- timeout=_TIMEOUT_SECONDS,
189
- follow_redirects=True,
190
- )
191
-
192
-
193
- async def default_part_seconds(
194
- url: str,
195
- proxy: str = "",
196
- cookie: str = "",
197
- ) -> int:
198
- """查询 B 站视频默认P的时长。
199
-
200
- Args:
201
- url: 视频链接(完整链或已展开的短链)。
202
- proxy: 解析代理,空字符串表示直连。
203
- cookie: 登录态 cookie,空字符串表示不带。
204
-
205
- Returns:
206
- 默认P的秒数;不是 B 站视频或请求失败时返回 0。
207
- """
208
- params = _video_params(url)
209
- if params is None:
210
- return 0
211
- headers = {
212
- "User-Agent": _USER_AGENT,
213
- "Referer": "https://www.bilibili.com/",
214
- }
215
- if cookie:
216
- headers["Cookie"] = cookie
217
- try:
218
- async with _client(proxy) as client:
219
- response = await client.get(
220
- _VIEW_API, params=params, headers=headers
221
- )
222
- response.raise_for_status()
223
- payload = response.json()
224
- except Exception as exc: # noqa: BLE001 - 查时长失败不能影响解析
225
- logger.debug(
226
- f"[parsehub] 查询 B 站默认P时长失败: {type(exc).__name__}: {exc}"
227
- )
228
- return 0
229
- data = payload.get("data") if isinstance(payload, dict) else None
230
- return default_page_seconds(data)
231
-
232
-
233
- def apply_duration(result: Any, seconds: int) -> None: # noqa: ANN401 - parsehub 结果
234
- """把默认P时长写回解析结果里的第一个媒体。
235
-
236
- 卡片和时长上限都从 ``result.media[].duration`` 读,改这里两边一起生效。
237
-
238
- Args:
239
- result: ParseHub 的解析结果。
240
- seconds: 默认P的秒数。
241
- """
242
- if seconds <= 0:
243
- return
244
- media = getattr(result, "media", None)
245
- items = list(media) if isinstance(media, (list, tuple)) else [media]
246
- for item in items:
247
- if item is not None and hasattr(item, "duration"):
248
- try:
249
- item.duration = seconds
250
- except (AttributeError, TypeError):
251
- pass
252
- return