import json
import itertools
import os
from urllib import request, error
raw = open('images.json')
data = json.load(raw)
raw.close()
for res in data['resolutions']:
if not os.path.exists(res):
os.mkdir(res)
for image in data['imageNames']:
retrieved = False
for res in data['resolutions']:
try:
f = open('%s/%s_%s.jpg' % (res, image, res), 'r')
retrieved = True
break
except:
pass
if retrieved:
continue
for info in itertools.product(data['resolutions'], data['cdns']):
localpath = '%s/%s_%s.jpg' % (info[0], image, info[0])
url = '%s%s_%s.jpg' % (info[1], image, info[0])
print('Downloading file %s... ' % url, end='')
try:
response = request.urlretrieve(url, localpath)
print('Ok!')
break
except error.URLError:
print('Failed!')