"""Fetch a pinned CC0 orchestral subset; output original WAVs to scratch only."""
from pathlib import Path
import urllib.request,urllib.parse,json,hashlib,concurrent.futures
commit='440300901dfe9275fd84e0b7763af1f8443ae62e'
choices={
 'Flute':'Woodwinds/Flute/susNV/LDFlute_susNV_C4_v1_1.wav',
 'Oboe':'Woodwinds/Oboe/Sus/Oboe_Sus_D3_v1_Main.wav',
 'Clarinet':'Woodwinds/Clarinet/susLong/DCClar_susLong_D3_v1_rr1_sum.wav',
 'Violin':'Strings/Violin Section/susVib/VlnEns_susVib_C4_v1.wav',
 'Viola':'Strings/Viola Section/susvib/ViolaEns_susvib_C4_v1_1.wav',
 'Cello':'Strings/Cello Section/susvib/susvib_C1_v1_1.wav',
 'Harp':'Strings/Harp/KSHarp_C3_mf.wav',
 'Piano':'Keys/Upright Nr1/UR1_C3_mf_RR1.wav',
 'Glock':'Percussion/Glock/glock_medium_C5.wav',
}
import sys
dest=Path(sys.argv[1]) if len(sys.argv)>1 else Path('/tmp/xmas26-vsco-original')
dest.mkdir(parents=True,exist_ok=True)
def get(item):
 name,path=item;url='https://raw.githubusercontent.com/sgossner/VSCO-2-CE/'+commit+'/'+urllib.parse.quote(path)
 file=dest/(name+'.wav')
 if not file.exists():file.write_bytes(urllib.request.urlopen(url,timeout=45).read())
 raw=file.read_bytes();assert raw[:4]==b'RIFF'
 return dict(name=name,path=path,url=url,sha256=hashlib.sha256(raw).hexdigest(),bytes=len(raw))
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:rows=list(pool.map(get,choices.items()))
for name in ['LICENSE','README.md','Readme.txt']:
 f=dest/name
 if not f.exists():f.write_bytes(urllib.request.urlopen('https://raw.githubusercontent.com/sgossner/VSCO-2-CE/'+commit+'/'+name,timeout=30).read())
(dest/'Sources.json').write_text(json.dumps(dict(commit=commit,samples=rows),indent=2)+'\n')
print('Fetched',len(rows),'pinned CC0 samples;',sum(r['bytes'] for r in rows),'bytes')
