#!/usr/bin/env python3 """ Phase 3: scaffold canonical JSON for specific verse keys (e.g. the 6:74 acceptance-test family) using the same record shape as ingest_pages.py. Never overwrites existing raw files. """ import json import sys import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parent.parent RAW_DIR = ROOT / "raw" ACCEPTANCE_SET = ["2:133", "14:41", "9:114", "60:4"] + [f"19:{n}" for n in range(41, 49)] def fetch_json(url): req = urllib.request.Request(url, headers={"User-Agent": "quran-md-ingestor/0.2"}) with urllib.request.urlopen(req, timeout=30) as resp: return json.loads(resp.read().decode("utf-8")) def scaffold(vk): s, a = (int(x) for x in vk.split(":")) out = RAW_DIR / f"{s:03d}-{a:03d}.json" if out.exists(): print(f"{vk}: exists, preserved") return data = fetch_json( f"https://api.quran.com/api/v4/verses/by_key/{vk}?language=en&words=true&translations=20" f"&fields=text_uthmani,chapter_id,verse_number,verse_key" ) v = data.get("verse", {}) if not v: print(f"{vk}: NOT FOUND") return trans = "" if v.get("translations"): trans = v["translations"][0].get("text", "") chapter = data.get("chapter", {}) record = { "schema_version": "2.0.0", "verse_key": vk, "surah_number": s, "ayah_number": a, "surah_name_en": chapter.get("name_simple", f"Surah {s}"), "surah_name_ar": chapter.get("name_arabic", ""), "page": v.get("page", 0), "revelation_type": chapter.get("revelation_place", "").title().replace("Makkah", "Meccan").replace("Madinah", "Medinan") or "Meccan", "revelation_order": s, "arabic_text": v.get("text_uthmani", ""), "translations": {"sahih_international": trans, "qarai": ""}, "roots": [], "themes": [], "disputed": False, "dispute_types": [], "asbab_al_nuzul": {"summary": "", "sources": []}, "sunni_perspectives": { "tafsir_summary": "", "tafsir_sources": [], "hadiths": [] }, "shia_perspectives": { "tafsir_summary": "", "tafsir_sources": [], "hadiths": [] }, "isnad_matn_audit": { "chain_comparison": "", "internal_quranic_consistency": "", "logical_historical_audit": "", "anti_bias_note": "" }, "data_flags": ["needs_review"] } out.write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding="utf-8") print(f"{vk}: scaffolded") def main(): RAW_DIR.mkdir(exist_ok=True) keys = sys.argv[1:] or ACCEPTANCE_SET for vk in keys: scaffold(vk) if __name__ == "__main__": main()