|
|
@@ -216,6 +216,25 @@ def load_existing_results(output_file: Path):
|
|
|
return prev_results, done_keys
|
|
|
|
|
|
|
|
|
+def print_statistics(counters: dict):
|
|
|
+ """统一打印统计信息"""
|
|
|
+ total_count = (
|
|
|
+ counters["activated"]
|
|
|
+ + counters["unactivated_valid"]
|
|
|
+ + counters["unactivated_invalid"]
|
|
|
+ + counters["unknown"]
|
|
|
+ )
|
|
|
+
|
|
|
+ print("\n========== 查询统计 ==========")
|
|
|
+ print(f"已激活: {counters['activated']}")
|
|
|
+ print(f" ├─ 之前已激活: {counters['previous_activated']}")
|
|
|
+ print(f" └─ 本次新激活: {counters['new_activated']}")
|
|
|
+ print(f"未激活(有效KEY): {counters['unactivated_valid']}")
|
|
|
+ print(f"未激活(无效KEY): {counters['unactivated_invalid']}")
|
|
|
+ print(f"未知 / 请求失败: {counters['unknown']}")
|
|
|
+ print(f"总计: {total_count}")
|
|
|
+
|
|
|
+
|
|
|
# ===========================
|
|
|
# 主流程
|
|
|
# ===========================
|
|
|
@@ -238,8 +257,14 @@ def main():
|
|
|
keys_file = Path(args.keys)
|
|
|
output_file = Path(args.output)
|
|
|
|
|
|
- # 1. 载入 cookie
|
|
|
- cookies = load_cookie_from_file()
|
|
|
+ # 1. 载入 cookie(对缺失/空内容给出友好提示)
|
|
|
+ try:
|
|
|
+ cookies = load_cookie_from_file()
|
|
|
+ except RuntimeError as exc:
|
|
|
+ print(f"载入 cookie.txt 失败:{exc}")
|
|
|
+ print("请在 cookie.txt 中粘贴完整的 Cookie 字符串后重试。")
|
|
|
+ return
|
|
|
+
|
|
|
print("已成功载入 cookie.txt 的登录信息\n")
|
|
|
|
|
|
# 2. 检查 key 文件
|
|
|
@@ -283,75 +308,76 @@ def main():
|
|
|
"new_activated": 0, # 本次新激活
|
|
|
}
|
|
|
|
|
|
- # 5. 重新写结果文件(避免重复行)
|
|
|
- with output_file.open("w", newline="", encoding="utf-8") as f_out:
|
|
|
- writer = csv.writer(f_out)
|
|
|
- writer.writerow(["KEY", "状态", "程序包名称", "PackageID", "备注"])
|
|
|
-
|
|
|
- for idx, key in enumerate(keys, start=1):
|
|
|
- progress = f"[{idx}/{total} {idx / total * 100:5.1f}%]"
|
|
|
-
|
|
|
- # 如果 key 在旧结果里,并且已完成(已激活 / 无效),直接复用
|
|
|
- if key in prev_results and key in done_keys:
|
|
|
- row = prev_results[key]
|
|
|
- status = row.get("状态", "")
|
|
|
- package_name = row.get("程序包名称", "")
|
|
|
- package_id = row.get("PackageID", "")
|
|
|
- note = row.get("备注", "")
|
|
|
-
|
|
|
- writer.writerow([key, status, package_name, package_id, note])
|
|
|
- classify_status(status, counters)
|
|
|
-
|
|
|
- # 统计:之前已激活
|
|
|
- if key in prev_activated_keys:
|
|
|
- counters["previous_activated"] += 1
|
|
|
-
|
|
|
- print(f"{progress} 跳过(已完成):{key} -> {status}")
|
|
|
- continue
|
|
|
-
|
|
|
- # 否则重新查询
|
|
|
- try:
|
|
|
- html = query_key(key, cookies)
|
|
|
- status, package_name, package_id, note = parse_status(html)
|
|
|
- writer.writerow([key, status, package_name or "", package_id or "", note])
|
|
|
- classify_status(status, counters)
|
|
|
-
|
|
|
- # 本次新激活(之前没激活,现在是已激活)
|
|
|
- if status.startswith("已激活") and key not in prev_activated_keys:
|
|
|
- counters["new_activated"] += 1
|
|
|
-
|
|
|
- if package_id:
|
|
|
- print(f"{progress} 查询:{key} -> {status} {package_name} ({package_id})")
|
|
|
- else:
|
|
|
- print(f"{progress} 查询:{key} -> {status}")
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- err = f"请求失败: {e}"
|
|
|
- writer.writerow([key, "请求失败", "", "", err])
|
|
|
- counters["unknown"] += 1
|
|
|
- print(f"{progress} 查询:{key} -> {err}")
|
|
|
-
|
|
|
- time.sleep(1) # 防止被风控
|
|
|
+ interrupted = False
|
|
|
+ processed = 0
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 5. 重新写结果文件(避免重复行)
|
|
|
+ with output_file.open("w", newline="", encoding="utf-8") as f_out:
|
|
|
+ writer = csv.writer(f_out)
|
|
|
+ writer.writerow(["KEY", "状态", "程序包名称", "PackageID", "备注"])
|
|
|
+ f_out.flush()
|
|
|
+
|
|
|
+ for idx, key in enumerate(keys, start=1):
|
|
|
+ progress = f"[{idx}/{total} {idx / total * 100:5.1f}%]"
|
|
|
+
|
|
|
+ # 如果 key 在旧结果里,并且已完成(已激活 / 无效),直接复用
|
|
|
+ if key in prev_results and key in done_keys:
|
|
|
+ row = prev_results[key]
|
|
|
+ status = row.get("状态", "")
|
|
|
+ package_name = row.get("程序包名称", "")
|
|
|
+ package_id = row.get("PackageID", "")
|
|
|
+ note = row.get("备注", "")
|
|
|
+
|
|
|
+ writer.writerow([key, status, package_name, package_id, note])
|
|
|
+ classify_status(status, counters)
|
|
|
+
|
|
|
+ # 统计:之前已激活
|
|
|
+ if key in prev_activated_keys:
|
|
|
+ counters["previous_activated"] += 1
|
|
|
+
|
|
|
+ print(f"{progress} 跳过(已完成):{key} -> {status}")
|
|
|
+ processed = idx
|
|
|
+ f_out.flush()
|
|
|
+ continue
|
|
|
+
|
|
|
+ # 否则重新查询
|
|
|
+ try:
|
|
|
+ html = query_key(key, cookies)
|
|
|
+ status, package_name, package_id, note = parse_status(html)
|
|
|
+ writer.writerow([key, status, package_name or "", package_id or "", note])
|
|
|
+ classify_status(status, counters)
|
|
|
+
|
|
|
+ # 本次新激活(之前没激活,现在是已激活)
|
|
|
+ if status.startswith("已激活") and key not in prev_activated_keys:
|
|
|
+ counters["new_activated"] += 1
|
|
|
+
|
|
|
+ if package_id:
|
|
|
+ print(f"{progress} 查询:{key} -> {status} {package_name} ({package_id})")
|
|
|
+ else:
|
|
|
+ print(f"{progress} 查询:{key} -> {status}")
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ err = f"请求失败: {e}"
|
|
|
+ writer.writerow([key, "请求失败", "", "", err])
|
|
|
+ counters["unknown"] += 1
|
|
|
+ print(f"{progress} 查询:{key} -> {err}")
|
|
|
+
|
|
|
+ processed = idx
|
|
|
+ f_out.flush()
|
|
|
+ time.sleep(1) # 防止被风控
|
|
|
+ except KeyboardInterrupt:
|
|
|
+ interrupted = True
|
|
|
+ print(f"\n检测到用户中断(Ctrl+C)。已处理 {processed} / {total} 个 KEY,正在整理统计信息...\n")
|
|
|
|
|
|
# ===== 统计结果展示 =====
|
|
|
- total_count = (
|
|
|
- counters["activated"]
|
|
|
- + counters["unactivated_valid"]
|
|
|
- + counters["unactivated_invalid"]
|
|
|
- + counters["unknown"]
|
|
|
- )
|
|
|
+ print_statistics(counters)
|
|
|
|
|
|
- print("\n========== 查询统计 ==========")
|
|
|
- print(f"已激活: {counters['activated']}")
|
|
|
- print(f" ├─ 之前已激活: {counters['previous_activated']}")
|
|
|
- print(f" └─ 本次新激活: {counters['new_activated']}")
|
|
|
- print(f"未激活(有效KEY): {counters['unactivated_valid']}")
|
|
|
- print(f"未激活(无效KEY): {counters['unactivated_invalid']}")
|
|
|
- print(f"未知 / 请求失败: {counters['unknown']}")
|
|
|
- print(f"总计: {total_count}")
|
|
|
-
|
|
|
- print(f"\n结果已写入:{output_file}")
|
|
|
+ if interrupted:
|
|
|
+ print(f"\n查询已被用户中断,当前结果已写入:{output_file}")
|
|
|
+ else:
|
|
|
+ print(f"\n结果已写入:{output_file}")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- main()
|
|
|
+ main()
|