#!/usr/bin/env python3 from pathlib import Path import json,hashlib,re,sys,concurrent.futures,time from urllib.parse import urlsplit,parse_qsl,urlencode,quote sys.path.insert(0,str(Path(__file__).parent));from fetch import fetch ROOT=Path(__file__).resolve().parents[2];RAW=ROOT/'docs/rebuild-v2/raw' def normalized(url): u=urlsplit(url);q=[(k,v) for k,v in parse_qsl(u.query) if k!='sid'];return u.path+('?' +urlencode(sorted(q)) if q else '') d=json.loads((RAW/'cdx-domain.json').read_text());groups={} for r in d[1:]:groups.setdefault(normalized(r[2]),[]).append(dict(zip(d[0],r))) plan=[] for path,rows in groups.items(): r=rows[0];kind='page';reason=None if '/forum/' in path: kind='forum-topic' if 'viewtopic.php?' in path else 'forum-listing' if not any(x in path for x in ['viewtopic.php?','viewforum.php?','index.php']):reason='phpBB utility/auth/profile/search page, no original technical article body' if '/shop/' in path: kind='historic-product' if 'productID=' in path else 'historic-category' if not any(x in path for x in ['productID=','categoryID=','aux_page=']):reason='Historic cart/shop utility or index; no live sales are being restored; inspect category/product pages separately' if path.startswith('/stat/'):reason='Counter endpoint, excluded from publication' name='pages/'+hashlib.sha256(path.encode()).hexdigest()[:16]+'.html' plan.append(dict(originalUrl=r['original'],normalizedPath=path,type=kind,snapshot=r['timestamp'],rawFile='docs/rebuild-v2/raw/'+name,disposition='excluded-utility' if reason else 'fetch',reason=reason)) (ROOT/'docs/rebuild-v2/discovery-plan.json').write_text(json.dumps(plan,ensure_ascii=False,indent=2)+'\n');(RAW/'pages').mkdir(exist_ok=True) def work(item): dest=ROOT/item['rawFile'] if dest.exists() and dest.stat().st_size>500:return {'path':item['normalizedPath'],'cached':True} url='https://web.archive.org/web/'+item['snapshot']+'id_/'+item['originalUrl'] return fetch(url,'pages/'+dest.name,timeout=45) with concurrent.futures.ThreadPoolExecutor(max_workers=2) as ex: for out in ex.map(work,[p for p in plan if p['disposition']=='fetch']):pass print('planned',len(plan),'fetch',sum(p['disposition']=='fetch' for p in plan),flush=True)