from pathlib import Path
import concurrent.futures, urllib.request
root=Path(__file__).resolve().parents[1]/'public/assets'
photos={
'pulse-hero':'1552674605-db6ffd4facb5','pulse-gym':'1534438327276-14e5300c3a48','pulse-shoe':'1542291026-7eec264c27ff','pulse-training':'1517836357463-d25dfeac3438','pulse-yoga':'1518611012118-696072aa579a',
'cherie-hero':'1534528741775-53994a69daeb','cherie-perfume':'1541643600914-78b084683601','cherie-jewels':'1611652022419-a9419f74343d','cherie-bag':'1584917865442-de89df76afd3','cherie-flower':'1490750967868-88aa4486c946',
'brasa-hero':'1544025162-d76694265947','brasa-burger':'1568901346375-23c9450c58cd','brasa-salad':'1512621776951-a57141f2eefd','brasa-pizza':'1579751626657-72bc17010498','brasa-room':'1517248135467-4c7edcad34c4','brasa-dessert':'1578985545062-69928b1d9587',
'veloce-hero':'1503376780353-7e6692767b70','veloce-coupe':'1503376780353-7e6692767b70','veloce-suv':'1519641471654-76ce0107ad1b','veloce-sport':'1503736334956-4c8f8e92946d','veloce-interior':'1507136566006-cfc505b114fc',
'moon-candle':'1603006905003-be475563bc59','moon-crystal':'1515377905703-c4788e51af15','moon-journal':'1517842645767-c639042777db','moon-jewelry':'1611652022419-a9419f74343d','moon-botanical':'1490750967868-88aa4486c946'}
def get(item):
 name,pid=item; path=root/(name+'.jpg')
 if path.exists():return name+' cached'
 url='https://images.unsplash.com/photo-'+pid+'?auto=format&fit=crop&w='+('1800' if 'hero' in name else '900')+'&q=85'
 try:
  with urllib.request.urlopen(url,timeout=35) as r: path.write_bytes(r.read())
  return name+' OK'
 except Exception as e:return name+' FAILED '+str(e)
with concurrent.futures.ThreadPoolExecutor(max_workers=7) as pool:
 for result in pool.map(get,photos.items()):print(result,flush=True)
