"""새 aihub_A.jsonl 에 더 이상 없는 aihub 문항(등급이 A -> B/C 로 바뀐 것)을 DB 에서 status='retired' 로 바꿉니다. 삭제하지 않고 '퇴역' 표시만 하므로 풀이 기록은 그대로 남고, 학생·교사 화면에는 나오지 않습니다 (화면은 draft/published 만 읽음). python scripts\\07_retire_missing.py --file data\\aihub_normalized\\aihub_A.jsonl # 미리보기(변경 없음) python scripts\\07_retire_missing.py --file data\\aihub_normalized\\aihub_A.jsonl --apply # 실제 반영 B 그림 문항(aihub_B_sel.jsonl)을 DB 에 넣은 뒤에는 반드시 두 파일을 함께 준다 (안 그러면 B 문항이 모두 퇴역됨): python scripts\\07_retire_missing.py --file data\\aihub_normalized\\aihub_A_fixed.jsonl,data\\aihub_normalized\\aihub_B_sel.jsonl 환경변수: SUPABASE_URL, SUPABASE_SERVICE_KEY (06_load_supabase.py 와 같음) """ import argparse import json import os import sys import urllib.parse import urllib.request from pathlib import Path if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8", errors="replace") def req(method, url, key, body=None, extra=None): h = {"apikey": key, "Content-Type": "application/json", **(extra or {})} if key.count(".") == 2: h["Authorization"] = f"Bearer {key}" r = urllib.request.Request(url, data=json.dumps(body).encode() if body is not None else None, headers=h, method=method) with urllib.request.urlopen(r, timeout=60) as resp: return resp.read().decode("utf-8") def main(): ap = argparse.ArgumentParser() ap.add_argument("--file", required=True, help="적재 파일. 여러 개면 쉼표로 (예: aihub_A_fixed.jsonl,aihub_B_sel.jsonl) — " "DB 에 있는 AI Hub 문항 파일을 모두 주지 않으면 빠진 파일의 문항이 퇴역됨") ap.add_argument("--apply", action="store_true") a = ap.parse_args() url, key = os.environ.get("SUPABASE_URL", "").strip().rstrip("/"), os.environ.get("SUPABASE_SERVICE_KEY", "").strip() if not url or not key: sys.exit("SUPABASE_URL 과 SUPABASE_SERVICE_KEY 환경변수가 필요합니다.") keep = set() for one in [s.strip() for s in a.file.split(",") if s.strip()]: keep |= {json.loads(l)["source_ref"] for l in Path(one).open(encoding="utf-8") if l.strip()} base = url + "/rest/v1/questions" db, start = [], 0 while True: q = f"{base}?select=source_ref&source=eq.aihub&status=in.(draft,review)&order=source_ref" part = json.loads(req("GET", q, key, extra={"Range-Unit": "items", "Range": f"{start}-{start + 999}"})) db += [r["source_ref"] for r in part] if len(part) < 1000: break start += 1000 gone = sorted(set(db) - keep) print(f"DB 의 aihub draft/review {len(db)}개, 새 파일 {len(keep)}개 -> 퇴역 대상 {len(gone)}개") for g in gone[:20]: print(" ", g) if not a.apply: print("(미리보기입니다. 반영하려면 --apply 를 붙이세요)") return for i in range(0, len(gone), 50): chunk = gone[i:i + 50] refs = ",".join('"' + c + '"' for c in chunk) quoted = urllib.parse.quote(refs, safe=',"') req("PATCH", base + "?source=eq.aihub&source_ref=in.(" + quoted + ")", key, {"status": "retired"}, {"Prefer": "return=minimal"}) print("반영 완료") if __name__ == "__main__": main()