# -*- coding: utf-8 -*-
"""공공기관3 L/M/N/O 채우기 (엔진 _공공기관2_phase234 재사용·OUTDIR override)
사용: python _phase234.py {num.기관폴더의 기관명} ...  (인자=기관명 또는 번호)"""
import sys, io, os, importlib.util, glob, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
SCRIPTS = r"D:\01.프로젝트\DB수집\_스크립트"
OUT = r"D:\01.프로젝트\DB수집\작업파일\공공기관3"
spec = importlib.util.spec_from_file_location("p234", os.path.join(SCRIPTS, "_공공기관2_phase234.py"))
p234 = importlib.util.module_from_spec(spec); spec.loader.exec_module(p234)
p234.OUTDIR = OUT
# 폴더 스캔으로 (num,name) 목록
folders = {}
for d in glob.glob(os.path.join(OUT, "*.*")):
    if os.path.isdir(d):
        m = re.match(r"(\d+)\.(.+)", os.path.basename(d))
        if m: folders[m.group(2)] = (int(m.group(1)), m.group(2))
args = sys.argv[1:]
targets = []
for a in args:
    for nm,(num,name) in folders.items():
        if a==nm or a==str(num): targets.append((num,name))
if not args:
    targets = sorted(folders.values())
for num,name in targets:
    try:
        p234.run_site(name, num, p234.BODY_SEL)
    except Exception as e:
        import traceback; print(f"[{num}.{name}] 실패 {e}"); traceback.print_exc()
