Files
shanhe-puzzle/scripts/download-maps.py

27 lines
1.4 KiB
Python

"""Fetch public DataV build inputs only; game never requests the network."""
import json, pathlib, urllib.request, time, concurrent.futures
ROOT=pathlib.Path(__file__).resolve().parent.parent
areas=json.load(open(ROOT/'research/areas.json'))
# Counties/urban districts are packaged per prefecture, municipalities at province level.
parents={a['parent'] for a in areas if a['level']=='district'}
entries=[a for a in areas if a['adcode'] in parents]
raw=ROOT/'data/raw';raw.mkdir(exist_ok=True,parents=True)
def fetch(a):
p=raw/(str(a['adcode'])+'.json')
if p.exists(): return
url='https://geo.datav.aliyun.com/areas_v3/bound/%s_full.json'%a['adcode']
for attempt in range(3):
try:
with urllib.request.urlopen(url,timeout=25) as r: data=r.read()
d=json.loads(data)
if not d.get('features'): raise ValueError('no features')
p.write_bytes(data);return
except Exception as e:
if attempt==2:return {'code':a['adcode'],'name':a['name'],'reason':str(e)}
time.sleep(.4)
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
failed=[r for r in pool.map(fetch,entries) if r]
(ROOT/'data/download-report.json').write_text(json.dumps({'requested':len(entries),'downloaded':len(list(raw.glob('*.json'))),'failed':failed},ensure_ascii=False,indent=2))
print('Map download:',len(entries),'requested;',len(failed),'unavailable')
print(json.dumps(failed,ensure_ascii=False))