"""VC1 · 53 · lectura de las fichas descargadas: texto plano y presencia de los NIF, importes y estados de A0 y F. Uso: python3 -I py/53_fichas.py -> interno/cotejo/fichas_lectura.csv""" import csv, sys, re, html from html.parser import HTMLParser W = sys.argv[1] class P(HTMLParser): def __init__(s): super().__init__(); s.t = []; s.skip = 0 def handle_starttag(s, tag, a): if tag in ('script', 'style'): s.skip += 1 def handle_endtag(s, tag): if tag in ('script', 'style'): s.skip -= 1 def handle_data(s, d): if not s.skip: s.t.append(d) def texto(p): try: b = open(p, 'rb').read().decode('utf-8', 'replace') except FileNotFoundError: return '' q = P(); q.feed(b); return re.sub(r'\s+', ' ', ' '.join(q.t)) def fmt(v): try: v = float(v) except: return [] e = f"{v:,.2f}"; es = e.replace(',', 'X').replace('.', ',').replace('X', '.') return [es, es.replace('.', ''), e, f"{v:.2f}".replace('.', ',')] out = [] for m in csv.DictReader(open(f'{W}/interno/cotejo/muestra.csv')): t = texto(f"{W}/interno/cotejo/fichas/{m['caso']}.html"); tu = t.upper() legible = len(t) > 3000 and ('EXPEDIENTE' in tu) r = {'caso': m['caso'], 'bytes_texto': len(t), 'legible': legible} r['nif0_en_ficha'] = any(n in tu for n in m['nifs0'].split() if len(n) > 5 and not n.startswith('N:')) r['nifF_en_ficha'] = any(n in tu for n in m['nifsF'].split() if len(n) > 5 and not n.startswith('N:')) r['imp0_en_ficha'] = any(f in t for f in fmt(m['imp0'])) r['impF_en_ficha'] = any(f in t for f in fmt(m['impF'])) est = re.findall(r'Estado de la Licitaci[oó]n\s*:?\s*([A-ZÁÉÍÓÚa-záéíóú ]{3,30}?)(?= [A-Z][a-z]|$)', t) r['estado_ficha'] = est[0].strip() if est else '' for w_ in ('Anulada', 'Desierto', 'Desistimiento', 'Renuncia', 'Resuelta', 'Adjudicada', 'Formalizado'): r['txt_' + w_] = w_.upper() in tu r['extracto'] = t[:0] out.append(r) with open(f'{W}/interno/cotejo/fichas_lectura.csv', 'w', newline='') as fh: w = csv.DictWriter(fh, fieldnames=list(out[0].keys())); w.writeheader(); w.writerows(out) for r in out: print({k: v for k, v in r.items() if k != 'extracto'})