from pathlib import Path import urllib.request,urllib.parse,json,time,concurrent.futures,hashlib r=Path('/Users/gavrilovdev/tmp/progcode.ru/docs/rebuild-v2/raw');r.mkdir(parents=True,exist_ok=True) queries={ 'domain-all':'https://web.archive.org/cdx/search/cdx?url=progcode.ru&matchType=domain&output=json&collapse=urlkey&showResumeKey=true&limit=5000', 'clean-snapshots':'https://web.archive.org/cdx/search/cdx?url=progcode.ru&matchType=domain&output=json&filter=statuscode:200&filter=mimetype:text/html&collapse=digest&showResumeKey=true&limit=5000', 'www-html':'https://web.archive.org/cdx/search/cdx?url=www.progcode.ru/*&output=json&filter=statuscode:200&filter=mimetype:text/html&collapse=urlkey&showResumeKey=true&limit=5000', } def fetch(item): name,url=item;record={'query':url,'attemptedAt':time.time()} for attempt in range(2): try: with urllib.request.urlopen(url,timeout=55) as resp:b=resp.read();record['http']=resp.status (r/(name+'.json')).write_bytes(b);record.update(result='downloaded',bytes=len(b),sha256=hashlib.sha256(b).hexdigest());print(name,record,flush=True);return name,record except Exception as e:record['error']=str(e);time.sleep(2) print(name,record,flush=True);return name,record with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:results=dict(pool.map(fetch,queries.items())) (r/'discovery-results.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n')