"""EJ2 · 52 · descarga de los documentos de modificación (§8). Contenedor ej2-fetch (única red). ≤ 30 peticiones en total, 4 s entre ellas, parada ante 403/429. Uso: 52_fetch.py c01 c02 ...""" import json, sys, time, urllib.request, os, csv MU = {o['c']: o for o in json.load(open('/w/interno/cotejo/muestra.json'))} LOG = '/w/interno/cotejo/peticiones.csv' hechas = sum(1 for _ in open(LOG)) - 1 if os.path.exists(LOG) else 0 if not os.path.exists(LOG): open(LOG, 'w').write('c,hora,estado,bytes,tipo\n') for c in sys.argv[1:]: if hechas >= 30: print('TOPE de 30 peticiones'); break o = MU[c]; dest = f'/w/interno/cotejo/docs/{c}.bin' if os.path.exists(dest): continue req = urllib.request.Request(o['doc_url'], headers={'User-Agent': 'Mozilla/5.0 (investigacion; contacto en el repositorio)'}) try: r = urllib.request.urlopen(req, timeout=60); b = r.read(); st = r.status; ct = r.headers.get('Content-Type', '') except urllib.error.HTTPError as e: b = b''; st = e.code; ct = '' except Exception as e: b = b''; st = f'error:{type(e).__name__}'; ct = '' hechas += 1 open(LOG, 'a').write(f'{c},{time.strftime("%F %T")},{st},{len(b)},{ct}\n') if b: open(dest, 'wb').write(b) print(c, st, len(b), ct, flush=True) if st in (403, 429): print('PARADA por', st); break time.sleep(4)