from pathlib import Path import json,hashlib,re,subprocess,concurrent.futures r=Path(__file__).resolve().parents[1];out=r/'docs/rebuild-v2/raw/media';out.mkdir(exist_ok=True) items=[] for a in json.load(open(r/'provenance/extracted-articles.json')): for i in a['images']: src=i['src'];items.append({'article':a['slug'],'original':src,'url':'https://web.archive.org/web/20220314193523id_/'+src,'role':'historical-inline'}) # Primary source of the translated interview carries the actual same screenshot. for host in ['https://blog.dlang.org','https://dlang.org/blog']: items.append({'article':'о-tilix-и-d-интервью-с-геральдом-нанном','original':'http://dlang.org/blog/wp-content/uploads/2017/08/tilix-screenshot-2-1024x589.png','url':host+'/wp-content/uploads/2017/08/tilix-screenshot-2-1024x589.png','role':'primary-source-equivalent'}) def run(x): x=dict(x);p=out/(hashlib.sha256(x['url'].encode()).hexdigest()[:20]+'.bin');q=subprocess.run(['curl','-sSL','--max-time','25','-o',str(p),'-w','%{http_code}\n%{url_effective}',x['url']],capture_output=True,text=True);meta=q.stdout.splitlines();x.update(http=meta[0] if meta else None,resolved=meta[-1] if meta else None,exit=q.returncode) b=p.read_bytes() if p.exists() else b'';valid=b.startswith((b'\x89PNG',b'\xff\xd8\xff',b'GIF8'));x.update(validImage=valid,bytes=len(b),raw=str(p.relative_to(r)) if p.exists() else None,sha256=hashlib.sha256(b).hexdigest());print('image',valid,x['article'],x['original'],flush=True) if valid: ext='.png' if b.startswith(b'\x89PNG') else '.jpg' if b.startswith(b'\xff') else '.gif';dest=r/'web/public/visuals/v2'/('recovered-'+hashlib.sha256(b).hexdigest()[:12]+ext);dest.write_bytes(b);x['publishedAsset']='/visuals/v2/'+dest.name return x with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:results=list(pool.map(run,items)) (r/'docs/rebuild-v2/media-recovery.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n')