Skip to content

Commit 67d5ff3

Browse files
committed
Add first-run preflight: detect missing API key / placeholder creators with actionable guidance instead of silent empty-data failure
1 parent 0d45731 commit 67d5ff3

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

scripts/viralens.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,56 @@ def run(script, args=None, optional=False):
4545
return True
4646

4747

48+
def preflight():
49+
"""抓取前的友好自检:缺密钥 / 还没改创作者清单时,直接给出下一步,
50+
而不是让后面的抓取逐条静默失败、最后拿空数据去分析。"""
51+
problems = []
52+
53+
# 1) API 密钥:config_local.py 存在且至少填了一个
54+
has_key = False
55+
try:
56+
import config_local
57+
if getattr(config_local, "SESSDATA", "") or getattr(config_local, "YOUTUBE_API_KEY", ""):
58+
has_key = True
59+
except ImportError:
60+
pass
61+
if not has_key:
62+
problems.append(
63+
"还没配置 API 密钥。\n"
64+
" 1) 复制 scripts/config_local.example.py 为 scripts/config_local.py\n"
65+
" 2) 填入 SESSDATA(B站)或 YOUTUBE_API_KEY(YouTube),至少一个\n"
66+
" config_local.py 已被 .gitignore 排除,不会上传。"
67+
)
68+
69+
# 2) 创作者清单:是否还停在示例占位
70+
try:
71+
from creators import CREATORS
72+
real = [
73+
c for c in CREATORS
74+
if "示例" not in c.get("name", "")
75+
and not str(c.get("alias", "")).startswith("demo_")
76+
and c.get("name") != "Example Channel"
77+
]
78+
if not real:
79+
problems.append(
80+
"创作者清单还是示例占位。\n"
81+
" 编辑 scripts/creators.py,换成你想分析的 B站 / YouTube 创作者;\n"
82+
" 想保留私有清单又不进 git,就放进 scripts/creators_local.py(自动覆盖)。"
83+
)
84+
except Exception:
85+
pass
86+
87+
if problems:
88+
print("\n" + "─" * 60)
89+
print("⚠ 还差一步(viralens 启动自检):")
90+
for i, p in enumerate(problems, 1):
91+
print(f"\n {i}. {p}")
92+
print("\n" + "─" * 60)
93+
print("把上面配好,再跑一次就行。")
94+
print("已经有 data/ 数据、只想看分析?加 --no-fetch 跳过抓取。")
95+
sys.exit(1)
96+
97+
4898
def main():
4999
argv = sys.argv[1:]
50100
if "-h" in argv or "--help" in argv:
@@ -63,6 +113,7 @@ def main():
63113
if no_fetch:
64114
print("\n(--no-fetch:跳过抓取,直接用 data/ 里已有的数据)")
65115
else:
116+
preflight()
66117
run("fetch_multi.py", ["--force"] if force else [])
67118

68119
if report_mode:

0 commit comments

Comments
 (0)