import urllib.request, urllib.error, http.cookiejar, json, sys BASE = "http://127.0.0.1:3000" import os TEST_PASSWORD = os.environ.get("TEST_PASSWORD", "123test") cj = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) def call(path, method="GET", data=None): body = json.dumps(data).encode() if data is not None else None req = urllib.request.Request(BASE + path, data=body, method=method) if body: req.add_header("Content-Type", "application/json") try: r = op.open(req, timeout=60) return r.status, r.read() except urllib.error.HTTPError as e: return e.code, e.read() call("/api/auth/login", "POST", {"password": TEST_PASSWORD}) st, body = call("/api/resources") songs = json.loads(body)["list"] print(f"共 {len(songs)} 首,开始补全元数据...\n") ok, fail = 0, 0 for s in songs: st, body = call("/api/music/enrich", "POST", {"file": s["file"]}) if st == 200: d = json.loads(body) song = d.get("song", {}) cover = song.get("cover", "") artist = song.get("artist", "") status = "✓ 有封面" if cover else "○ 无封面" print(f" {status} | {song.get('name','')} - {artist} | {cover}") ok += 1 else: print(f" ✗ 失败: {s['file']} -> {body[:80]}") fail += 1 print(f"\n完成: {ok} 成功, {fail} 失败") # 最终确认 st, body = call("/api/resources") songs = json.loads(body)["list"] with_cover = sum(1 for s in songs if s["albumImage"]) print(f"资源库: {len(songs)} 首,其中 {with_cover} 首有本地封面") sys.exit(0)