"""LOB1 · descarga una URL pública a /out/; registra URL, fecha, bytes, md5, tipo. Pausa 3 s.""" import urllib.request, sys, datetime, hashlib, json, time, os u, dst = sys.argv[1], sys.argv[2] os.makedirs(os.path.dirname('/out/'+dst) or '/out', exist_ok=True) try: r = urllib.request.urlopen(urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0 (investigacion estadistica; datos abiertos)"}), timeout=300) b = r.read(); open('/out/'+dst, 'wb').write(b) d = dict(url=u, fichero=dst, bytes=len(b), md5=hashlib.md5(b).hexdigest(), status=r.status, ctype=r.headers.get('Content-Type'), fecha=datetime.datetime.now().isoformat()) except Exception as e: d = dict(url=u, fichero=dst, error=repr(e)[:300], fecha=datetime.datetime.now().isoformat()) print(json.dumps(d, ensure_ascii=False)); open('/out/descargas.jsonl', 'a').write(json.dumps(d, ensure_ascii=False) + '\n') time.sleep(3)