"""Acquire the two official stable exports; never execute downloaded code."""
import datetime, hashlib, json, pathlib, urllib.request, zipfile
ROOT = pathlib.Path(__file__).resolve().parents[1]
records = []
for name, url in [('anage', 'https://www.genomics.senescence.info/species/dataset.zip'),
                  ('drugage', 'https://www.genomics.senescence.info/drugs/dataset.zip')]:
    target = ROOT / 'data/raw' / (name + '.zip')
    if target.exists():
        raise SystemExit(f'Refusing to overwrite frozen snapshot: {target}')
    with urllib.request.urlopen(url, timeout=60) as response:
        payload = response.read()
        final_url = response.url
    target.write_bytes(payload)
    record = dict(dataset=name, requested_url=url, final_url=final_url,
                  accessed_utc=datetime.datetime.now(datetime.timezone.utc).isoformat(),
                  bytes=len(payload), sha256=hashlib.sha256(payload).hexdigest(),
                  license='CC BY 3.0', license_url='https://genomics.senescence.info/legal.html',
                  creator='Human Ageing Genomic Resources; João Pedro de Magalhães and contributing curators', files=[])
    with zipfile.ZipFile(target) as archive:
        for info in archive.infolist():
            if info.is_dir(): continue
            # Flatten names; no archive-provided paths are trusted.
            dest = ROOT / 'data/raw' / (name + '_' + pathlib.PurePosixPath(info.filename).name)
            content = archive.read(info)
            dest.write_bytes(content)
            record['files'].append(dict(path=str(dest.relative_to(ROOT)), bytes=len(content),
                                        sha256=hashlib.sha256(content).hexdigest()))
    records.append(record)
(ROOT/'data/manifest.json').write_text(json.dumps(records, indent=2, ensure_ascii=False)+'\n')
print(json.dumps(records, indent=2, ensure_ascii=False))
