# COB-NAC · cotejo D1 (denominador, pre-registro §5): celdas del RCSP en TXT (las que lee etl/cobertura_territorio.leer_rcsp, # código montado en /code) frente a la página web de la misma tabla (.aspx de hacienda.gob.es, bajada el 3-nov): # 2023 importe y 2022 número. PASA si el 100 % de las celdas comunes son iguales (≥ 30 celdas). import sys, re, html, json sys.path.insert(0, '/code') from etl import cobertura_territorio as ct filas, control = ct.leer_rcsp() txt = {(r['anio'], r['ccaa'], r['procedimiento'], r['medida']): r['valor'] for r in filas} out = {} for f, anio, medida, cab in (('web_2023_importe.aspx.html', 2023, 'importe', 'Importe'), ('web_2022_numero.aspx.html', 2022, 'numero', 'NUMERO DE CONTRATOS')): t = open(f'/w/descargas/rcsp/{f}', encoding='utf-8', errors='replace').read() pat = (r'Comunidad Autónoma:([^<]+)([^<]+)' + re.escape(cab) + r'([^<]*)') web = {} for c, p, v in re.findall(pat, t): c, p, v = html.unescape(c).strip(), html.unescape(p).strip(), v.strip() cc = ct.RCSP_FILAS.get(c, 'ES' if c == 'Total general' else None) if cc is None: continue web[(anio, cc, p, medida)] = 0 if v in ('-', '') else (float(v.replace('.', '').replace(',', '.')) if medida == 'importe' else int(v.replace('.', ''))) comunes = [k for k in web if k in txt] dist = [(k, web[k], txt[k]) for k in comunes if abs(web[k] - txt[k]) > 0.5] out[f] = {'celdas_web': len(web), 'comunes': len(comunes), 'iguales': len(comunes) - len(dist), 'distintas': dist[:20], 'solo_web': [k for k in web if k not in txt][:10]} print(f, out[f]['celdas_web'], out[f]['comunes'], out[f]['iguales'], dist[:5]) out['pasa'] = all(v['comunes'] >= 30 and v['iguales'] == v['comunes'] for v in out.values() if isinstance(v, dict)) json.dump(out, open('/w/salidas/cotejo_rcsp.json', 'w'), ensure_ascii=False, indent=1, default=str) print('PASA' if out['pasa'] else 'NO PASA')