Files
Live-streaming/scripts/fill_skipped_groups.py
T

276 lines
9.5 KiB
Python

# -*- coding: utf-8 -*-
"""
补充处理之前跳过的项目(有子目录的多作者/多版本食材与特产)。
规则:
- 子目录名含"效率版/高效率/中效率/效率路线/效率" → 用该效率版子目录的 json 创建配置组
- 其余 → 创建空配置组(projects=[]),用户手动添加路线 json
用法:
python fill_skipped_groups.py
"""
import os
import json
import sys
BASE = r"C:\Program Files\BetterGI\BetterGI\User"
AUTOPATHING = os.path.join(BASE, "AutoPathing")
SCRIPTGROUP = os.path.join(BASE, "ScriptGroup")
IGNORE_NAMES = {"desktop.ini", "icon.ico", "Thumbs.db"}
REGION_ORDER = ["蒙德", "璃月", "稻妻", "须弥", "枫丹", "纳塔", "挪德卡莱"]
CATEGORIES = ["地方特产", "食材与炼金"]
def region_sort_key(name):
try:
return (REGION_ORDER.index(name), name)
except ValueError:
return (len(REGION_ORDER), name)
def load_config_template():
template_path = os.path.join(SCRIPTGROUP, "子探测单元.json")
with open(template_path, "r", encoding="utf-8") as f:
return json.load(f)["config"]
def get_next_group_index():
max_index = 0
for fn in os.listdir(SCRIPTGROUP):
if not fn.lower().endswith(".json"):
continue
fp = os.path.join(SCRIPTGROUP, fn)
try:
with open(fp, "r", encoding="utf-8") as f:
data = json.load(f)
idx = data.get("index", 0)
if isinstance(idx, int) and idx > max_index:
max_index = idx
except Exception:
continue
return max_index + 1
def detect_structure(category_path):
has_any_direct_json = False
for entry in os.listdir(category_path):
entry_path = os.path.join(category_path, entry)
if not os.path.isdir(entry_path):
continue
sub_entries = [e for e in os.listdir(entry_path) if e not in IGNORE_NAMES]
has_json = any(
fn.lower().endswith(".json") and os.path.isfile(os.path.join(entry_path, fn))
for fn in sub_entries
)
if has_json:
has_any_direct_json = True
break
return "two" if has_any_direct_json else "three"
def list_items(category_path, structure):
result = []
if structure == "two":
for item in os.listdir(category_path):
ip = os.path.join(category_path, item)
if not os.path.isdir(ip):
continue
result.append((item, item, ip))
result.sort(key=lambda x: x[0])
else:
for region in os.listdir(category_path):
rp = os.path.join(category_path, region)
if not os.path.isdir(rp):
continue
for item in os.listdir(rp):
ip = os.path.join(rp, item)
if not os.path.isdir(ip):
continue
result.append(((region, item), item, ip))
result.sort(key=lambda x: (region_sort_key(x[0][0]), x[0][1]))
return result
def analyze_item(item_path):
entries = os.listdir(item_path)
subdirs = [e for e in entries
if os.path.isdir(os.path.join(item_path, e)) and e not in IGNORE_NAMES]
json_files = [e for e in entries
if e.lower().endswith(".json")
and os.path.isfile(os.path.join(item_path, e))
and e not in IGNORE_NAMES]
return subdirs, sorted(json_files)
def list_jsons_in_dir(dir_path):
"""列出某目录下直接包含的 json 文件(排除系统文件)。"""
if not os.path.isdir(dir_path):
return []
result = []
for e in os.listdir(dir_path):
if e in IGNORE_NAMES:
continue
if e.lower().endswith(".json") and os.path.isfile(os.path.join(dir_path, e)):
result.append(e)
return sorted(result)
def pick_efficiency_subdir(subdirs):
"""按优先级选取效率版子目录,返回子目录名或 None。"""
priorities = ["效率版", "高效率", "中效率", "效率路线", "效率"]
for keyword in priorities:
for sd in subdirs:
if keyword in sd:
return sd
return None
def build_group(group_index, name, folder_name, json_files, config_template):
projects = []
for i, jf in enumerate(json_files, 1):
projects.append({
"name": jf,
"folderName": folder_name,
"jsScriptSettingsObject": None,
"index": i,
"type": "Pathing",
"status": "Enabled",
"schedule": "Daily",
"runNum": 1,
"allowJsNotification": True,
"allowJsHTTPHash": ""
})
return {
"index": group_index,
"name": name,
"config": config_template,
"projects": projects
}
def main():
config_template = load_config_template()
report_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"补充报告_跳过项目.txt"
)
eff_created = [] # (category, group_key, name, eff_subdir, route_count, file_path)
empty_created = [] # (category, group_key, name, subdirs, file_path)
write_errors = []
group_index = get_next_group_index()
print(f"[信息] 起始 group index: {group_index}")
for category in CATEGORIES:
category_path = os.path.join(AUTOPATHING, category)
if not os.path.isdir(category_path):
continue
structure = detect_structure(category_path)
items = list_items(category_path, structure)
for group_key, name, item_path in items:
subdirs, json_files = analyze_item(item_path)
if not subdirs:
continue # 无子目录,之前已创建,跳过
# 构造 folderName
if structure == "two":
folder_name = f"{category}\\{name}"
else:
region, _ = group_key
folder_name = f"{category}\\{region}\\{name}"
# 检查是否已存在同名配置组(避免覆盖之前创建的)
out_path = os.path.join(SCRIPTGROUP, f"{name}.json")
if os.path.exists(out_path):
# 已存在,跳过
continue
eff_subdir = pick_efficiency_subdir(subdirs)
if eff_subdir:
# 用效率版子目录的 json
eff_path = os.path.join(item_path, eff_subdir)
# 效率版子目录可能直接含 json,也可能再含子目录(一般直接含)
eff_jsons = list_jsons_in_dir(eff_path)
if not eff_jsons:
# 效率版子目录下无直接 json,创建空配置组
group = build_group(group_index, name, folder_name, [], config_template)
try:
with open(out_path, "w", encoding="utf-8") as f:
json.dump(group, f, ensure_ascii=False, indent=2)
empty_created.append((category, group_key, name, subdirs, out_path))
group_index += 1
except Exception as e:
write_errors.append((name, str(e)))
continue
group = build_group(group_index, name, folder_name, eff_jsons, config_template)
try:
with open(out_path, "w", encoding="utf-8") as f:
json.dump(group, f, ensure_ascii=False, indent=2)
eff_created.append((category, group_key, name, eff_subdir, len(eff_jsons), out_path))
group_index += 1
except Exception as e:
write_errors.append((name, str(e)))
else:
# 无效率版 → 创建空配置组
group = build_group(group_index, name, folder_name, [], config_template)
try:
with open(out_path, "w", encoding="utf-8") as f:
json.dump(group, f, ensure_ascii=False, indent=2)
empty_created.append((category, group_key, name, subdirs, out_path))
group_index += 1
except Exception as e:
write_errors.append((name, str(e)))
# 生成报告
lines = []
lines.append("=" * 70)
lines.append("BetterGI 跳过项目补充处理报告")
lines.append("=" * 70)
lines.append("")
lines.append(f"【使用效率版创建】共 {len(eff_created)} 个")
lines.append("-" * 70)
cur_cat = None
for category, gk, name, eff_sd, cnt, fp in eff_created:
if category != cur_cat:
cur_cat = category
lines.append(f"\n[{category}]")
lines.append(f" {name}")
lines.append(f" 效率版: {eff_sd} ({cnt} 条路线)")
lines.append("")
lines.append(f"【创建空配置组(需手动添加路线)】共 {len(empty_created)} 个")
lines.append("-" * 70)
cur_cat = None
for category, gk, name, subdirs, fp in empty_created:
if category != cur_cat:
cur_cat = category
lines.append(f"\n[{category}]")
lines.append(f" {name}")
lines.append(f" 候选子目录: {subdirs}")
lines.append("")
if write_errors:
lines.append("【写入错误】")
lines.append("-" * 70)
for name, err in write_errors:
lines.append(f" {name}: {err}")
lines.append("")
lines.append("=" * 70)
report = "\n".join(lines)
with open(report_path, "w", encoding="utf-8") as f:
f.write(report)
print(report)
print(f"\n报告已保存: {report_path}")
if __name__ == "__main__":
main()