"""FUENTES-A-LA-CADENA · los originales de cada caso de la lectura INC, tal como se descargaron: las CONCESIONES de la BDNS con su CIF de 2022 a 2026 de la copia vigente (2022 del barrido del 11-nov-2026; 2023-2026 de las copias del 20 y 21-oct-2026) y de las noches del refresco incremental (nuevas_pj.jsonl.gz, registradas después), en JSON Lines con su origen; y las retiradas de las noches (ids) si las hay. Escribe priv/orig_inc// (700). Host, biblioteca estándar (patrón de ficha360/b/lectura/originales.py).""" import glob, gzip, json, os, re B = "/BDNS" INC = "/bdns" X = os.path.dirname(os.path.abspath(__file__)) sel = json.load(open(f"{X}/priv/sel_inc.json")) nifs = sorted({c["nif"] for c in sel["casos"]}) rx = re.compile("|".join(nifs)) os.umask(0o077) out = {n: {"copia": [], "incremental": []} for n in nifs} origen = {n: {"copia": set(), "incremental": set()} for n in nifs} def tok(x): return (x.get("beneficiario") or "").strip().split(" ")[0].upper() for f in sorted(glob.glob(f"{B}/barrido_2022_20261111/paginas/*.json.gz")): t = gzip.open(f, "rt").read() if rx.search(t): for x in json.loads(t)["content"]: if tok(x) in out and str(x.get("fechaConcesion", "")).startswith("2022"): out[tok(x)]["copia"].append(x); origen[tok(x)]["copia"].add("barrido de 2022 (11-nov-2026)") for f in sorted(glob.glob(f"{B}/concesiones/*/*.jsonl.gz")): for l in gzip.open(f, "rt"): if rx.search(l): x = json.loads(l) if tok(x) in out and str(x.get("fechaConcesion", "")) >= "2023": out[tok(x)]["copia"].append(x); origen[tok(x)]["copia"].add("copia de concesiones (20 y 21-oct-2026)") for f in sorted(glob.glob(f"{INC}/2*/nuevas_pj.jsonl.gz")): noche = os.path.basename(os.path.dirname(f)) for l in gzip.open(f, "rt"): if rx.search(l): x = json.loads(l) if tok(x) in out and str(x.get("fechaConcesion", "")) >= "2022": out[tok(x)]["incremental"].append(x); origen[tok(x)]["incremental"].add(f"refresco incremental, noche del {noche[6:8]}-{noche[4:6]}-{noche[:4]}") ret = set() for f in sorted(glob.glob(f"{INC}/2*/retiradas.tsv")): for l in open(f, encoding="utf-8"): p = l.strip().split("\t") if p and p[0].isdigit(): ret.add(p[0]) res = {"nifs": len(nifs), "copia": 0, "incremental": 0, "retiradas_en_casos": 0} for n in nifs: d = f"{X}/priv/orig_inc/{n}"; os.makedirs(d, exist_ok=True) for clave, xs in out[n].items(): with open(f"{d}/bdns_concesiones_{clave}.jsonl", "w", encoding="utf-8") as fh: for x in sorted(xs, key=lambda z: (str(z.get("fechaConcesion")), str(z.get("codConcesion")))): # sin urlBR (el enlace a las bases reguladoras): el lector no lo necesita y una URL dio un falso «dos apellidos» en F-INV fh.write(json.dumps({k: v for k, v in x.items() if k != "urlBR"}, ensure_ascii=False) + "\n") res[clave] += len(xs) mias = sorted(str(x.get("id")) for k in out[n] for x in out[n][k] if str(x.get("id")) in ret) with open(f"{d}/retiradas.txt", "w", encoding="utf-8") as fh: fh.write("".join(i + "\n" for i in mias)) res["retiradas_en_casos"] += len(mias) json.dump({k: sorted(v) for k, v in origen[n].items()}, open(f"{d}/origen.json", "w"), ensure_ascii=False, indent=1) print(res)