refactor: 拆分 4 个巨石模块为包,清理过时注释并删除作废文档

为什么:单文件 1100~1900 行的巨石模块让「改一处要通读全文」;同时注释里残留
大量已删台账的编号,以及「Flask/FastAPI 双栈迁移期」这类已失实的架构描述,
会误导后来者按不复存在的架构去改代码。

拆分(对外导入路径不变,行为零变更):
- music_downloader.py  -> music_downloader/(models/downloader/cover/_http/_log/_concurrency)
- auth_system.py       -> auth_system/(db/security/users/credits/ledger/mail/cards/backup...)
- music_api.py         -> music_api/(api/client/crypto/qr/cookies/compat/constants/errors)
- services/runtime.py  -> services/(api_service/_shared/_taskcore/downloads/batch/podcast/
  streaming/tasks);services/runtime.py 保留为兼容门面

兼容性做法:各包用 __init__.py 以 __all__ 原样再导出全部符号,既有调用点
(auth_system.xxx / services.xxx / from music_api import Y)零改动;路径常量改为经包属性
动态读取(auth_system.DB_PATH),否则测试的 monkeypatch 会静默失效并写到生产库。

修复 9 处代码损伤(上一轮注释清理把 `()` 当「空括号残留」删掉所致;用「与拆分前逐字
等价」校验逐一定位,静态检查只会报出其中 4 处):
- core/config.py  ).strip() or '*'
- core/state.py   threading.Lock() x2
- qr_login.py     datetime.now() / .strip()
- music_downloader/downloader.py  .lower()
- services/podcast.py  set() / time.time() x2

清理与删除:
- 删除 部署文档.md / 功能介绍.md / README_私有包警告.txt
- 清理指向已删台账与文档的过时编号注释;把「迁移期 / 两栈 / 双框架 / 已迁移」等
  失实描述改写为现状描述(保留 `<-> Flask /path` 映射表等仍有价值的契约来源说明)
- _mkpkg.py:公开包剔除清单去掉已不存在的文档名;私有包警告改为自包含
- pyproject.toml:补 pythonpath = ["."],使裸跑 pytest 不再 import core 失败

验证:ruff 全通过;pyright 0 errors / 0 warnings(102 文件);pytest 144 passed;
真实 uvicorn 冒烟 6 条路由符合预期且生产 data/ 指纹未变;忽略注释的逐字等价性校验通过。
This commit is contained in:
snowy-yang committed 2026-09-26 12:37:48 +08:00
1 parent 838d635bc5
commit 6288a98ecc
89 files changed
+5806 -5112

No files matched your search

+10 -10
View File
@@ -48,7 +48,7 @@ class PodcastConstants:
DEFAULT_HOT_CATE_IDS = [6, 3, 8, 10001]
# 第 97 条(对抗复审补漏):与 music_api 同口径的上游 JSON 响应体上限
# 与 music_api 同口径的上游 JSON 响应体上限
_MAX_UPSTREAM_JSON_BYTES = 8 * 1024 * 1024
@@ -178,10 +178,10 @@ _SHORTLINK_CACHE: dict[str, str] = {}
def _resolve_short_link(url: str) -> str:
"""解析 163cn.tv 短链,返回跳转后的真实链接(T4:仅接受整串短链,子串不触发请求)"""
"""解析 163cn.tv 短链,返回跳转后的真实链接(仅接受整串短链,子串不触发请求)"""
if not re.match(r'^https?://(www\.)?163cn\.tv/\S+$', str(url or '').strip(), re.IGNORECASE):
return url
# 第 91 条(P3):带浏览器 UA(短链 CDN 对默认 UA 区别对待)+ 模块级缓存
# 带浏览器 UA(短链 CDN 对默认 UA 区别对待)+ 模块级缓存
#(此前仅音乐路径有缓存,播客同一分享链接每次解析都发真实 GET)
global _SHORTLINK_CACHE
cached = _SHORTLINK_CACHE.get(url)
@@ -197,7 +197,7 @@ def _resolve_short_link(url: str) -> str:
finally:
if resp is not None:
resp.close()
# 第 99 条:与 main._expand_shortlink 同口径——展开失败(200 无 Location/5xx,
# 短链展开口径——展开失败(200 无 Location/5xx,
# real==url)不写缓存,一次 CDN 抖动不再让该链接整个缓存存续期必然失败
if real == url:
return real
@@ -208,7 +208,7 @@ def _resolve_short_link(url: str) -> str:
def extract_podcast_ref(raw: str) -> dict[str, str]:
"""从用户输入中提取播客引用(T4:正规 URL 解析,替代子串匹配与非数字剔除)
"""从用户输入中提取播客引用(正规 URL 解析,替代子串匹配与非数字剔除)
Returns:
{'kind': 'radio'|'program'|'unknown', 'id': '...'}
@@ -375,7 +375,7 @@ def get_categories() -> list[dict[str, Any]]:
except json.JSONDecodeError as e:
raise APIException(f"解析播客分类响应失败: {e}") from e
if not isinstance(result, dict):
# 第 97 条(P3):非 dict 响应(null/数组)显式报错,不再 AttributeError
# 非 dict 响应(null/数组)显式报错,不再 AttributeError
raise APIException('解析播客分类响应失败(非标准 JSON 对象)')
cats = []
for c in (result.get('categories') or []):
@@ -399,7 +399,7 @@ def get_hot_radios(cookies: dict[str, str] | None = None,
"""获取分类下热门播客
Args:
cate_id: 播客分类ID;F84:用户明确指定分类时只查该分类
cate_id: 播客分类ID;用户明确指定分类时只查该分类
(即便为空也不悄悄混入其他分类的内容),未指定时按默认分类依次尝试
Returns:
@@ -427,7 +427,7 @@ def get_hot_radios(cookies: dict[str, str] | None = None,
if radios:
return radios
if user_cate:
break # F84:指定分类为空就返回空,不注入默认分类内容
break # 指定分类为空就返回空,不注入默认分类内容
return []
@@ -443,7 +443,7 @@ def resolve_episode_url(song_id: Any, cookies: dict[str, str] | None = None,
Returns:
{'url','level','type','size','br','md5'} 或 None(全部失败)
"""
# F83:降级阶梯只向下不向上——原实现指定 standard 会回退到更高的 exhigh
# 降级阶梯只向下不向上——原实现指定 standard 会回退到更高的 exhigh
_FALLDOWN_LADDER = ['hires', 'lossless', 'exhigh', 'standard']
order: list[str] = []
level = str(level or 'auto').strip()
@@ -526,7 +526,7 @@ def list_radio_all_program_ids(radio_id: Any, cookies: dict[str, str] | None = N
"""拉取电台全部单集(用于整台打包,正序),返回 [{'id','name','main_song_id','serial_num','cover_url'}]
最多拉取 max_count 集,防止超大电台拖垮磁盘/任务。
F98:按节目 ID 去重;一页没有任何新增(短页/重复页/列表变动)即有界停止,
按节目 ID 去重;一页没有任何新增(短页/重复页/列表变动)即有界停止,
不再固定 offset+=100 盲目翻页。
"""
out: list[dict[str, Any]] = []