"""Retrieve three selected public neighboring-object images by ZIP range."""
import json
import sys
import zipfile
from pathlib import Path

ROOT=Path(__file__).resolve().parent.parent
sys.path.insert(0,str(ROOT/'research'))
from r16_archive_inspect import Remote

selection=json.loads((ROOT/'results/r18/selection.json').read_text())['selected_pairs']
out=Path('/tmp/diatom-neighbors');out.mkdir(exist_ok=True)
with zipfile.ZipFile(Remote()) as archive:
    for i,row in enumerate(selection,1):
        data=archive.read(row['zip_member'])  # ZIP member CRC is checked.
        (out/f'{i:02d}.jpg').write_bytes(data)
        print(i,row['image']['file_name'],len(data),'bytes',flush=True)
