#!/usr/bin/env python3
"""JSTO original image recovery on a network-enabled computer.
Run: pip install requests && python find-japan/tools/recover_media_v8.py
No images are supplied within this source package. All saved content is byte-verified.
"""
import csv,hashlib,json,re,time
from pathlib import Path
from urllib.parse import urlparse
import requests
BASE=Path(__file__).resolve().parents[1]
ROOT=BASE/'recovered';ROOT.mkdir(parents=True,exist_ok=True)
with (BASE/'data/original_assets_v8.csv').open(encoding='utf-8-sig',newline='') as f:
 records=list(csv.DictReader(f))
s=requests.Session();s.headers.update({'User-Agent':'JSTO historical media preservation (no robots circumvention)'})
def extension(b):
 if b.startswith(b'\x89PNG\r\n\x1a\n'):return '.png'
 if b.startswith(b'\xff\xd8\xff'):return '.jpg'
 if b.startswith(b'RIFF') and b[8:12]==b'WEBP':return '.webp'
 if b.startswith(b'%PDF-'):return '.pdf'
 if b.startswith((b'GIF87a',b'GIF89a')):return '.gif'
 return None
saved={}
for i,r in enumerate(records):
 url=r['url'];slug=re.sub(r'[^a-zA-Z0-9_-]','_',r['article'])
 if url in saved: r.update(saved[url]);continue
 try:
  res=s.get(url,timeout=25);res.raise_for_status();b=res.content
  ext=extension(b)
  if not ext or len(b)<500:raise ValueError('not a plausible image or PDF')
  sha=hashlib.sha256(b).hexdigest();p=ROOT/f'{slug}-{sha[:12]}{ext}';p.write_bytes(b)
  r.update(file_stored='YES',image_binary_sha256=sha,local_file=p.name,download_status='success',byte_count=len(b))
  saved[url]={k:r[k] for k in ['file_stored','image_binary_sha256','local_file','download_status','byte_count']}
 except Exception as exc:r.update(download_status=f'failed: {exc}')
 time.sleep(.3)
(ROOT/'download_audit.json').write_text(json.dumps(records,ensure_ascii=False,indent=2),encoding='utf-8')
print(sum(r.get('file_stored')=='YES' for r in records),'records have downloaded binary (duplicate records counted)')
