Files
progcode/deploy/retry-archive.py
T

15 lines
1.2 KiB
Python

from pathlib import Path
import json,subprocess,concurrent.futures,hashlib,time
r=Path(__file__).resolve().parents[1];p=r/'docs/rebuild-v2/raw/fetch-results.json';rows=json.load(open(p))
def fetch(a):
if a['result']=='downloaded':return a
a=dict(a);url=a['requestedSnapshot'];n=hashlib.sha256(url.encode()).hexdigest()[:20];dest=r/'docs/rebuild-v2/raw'/(n+'.html');headers=dest.with_suffix('.headers')
q=subprocess.run(['curl','-sSL','--retry','1','--retry-delay','2','--max-time','45','-D',str(headers),'-o',str(dest),'-w','%{http_code}\n%{url_effective}',url],text=True,capture_output=True)
info=q.stdout.splitlines();a['curlExit']=q.returncode;a['http']=int(info[0]) if info and info[0].isdigit() else 0;a['resolvedSnapshot']=info[-1] if info else url
if q.returncode==0 and a['http']==200:
b=dest.read_bytes();a.update(result='downloaded',raw=str(dest.relative_to(r)),sha256=hashlib.sha256(b).hexdigest(),bytes=len(b));a.pop('error',None)
else:a.update(result='fetch-failed',error=q.stderr or 'HTTP '+str(a['http']))
print(a['result'],a['original'],a.get('bytes',a.get('error')),flush=True);return a
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:result=list(pool.map(fetch,rows))
p.write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n')