Working frontpage with responsive grid. Fixed issue with part without element_id

This commit is contained in:
FrederikBaerentsen 2024-03-03 06:43:24 -05:00
parent bdf4bbef6c
commit a843e929d7
2 changed files with 40 additions and 17 deletions

14
app.py
View File

@ -13,16 +13,18 @@ def index():
pathlist = Path('./info/').rglob('*.json') pathlist = Path('./info/').rglob('*.json')
set_list = [] set_list = []
for path in pathlist: for path in pathlist:
print(str(path)) set_num = re.findall(r"\b\d+(?:-\d+)?\b",str(path))[0]
set_list.append(re.findall(r"\b\d+(?:-\d+)?\b",str(path))) with open('./static/sets/'+set_num+'/info.json') as info:
info_file = json.loads(info.read())
return set_list set_list.append(info_file)
print(set_list)
return render_template('frontpage.html',set_list=set_list)
@app.route('/<tmp>') @app.route('/<tmp>')
def sets(tmp): def sets(tmp):
with open('./sets/'+tmp+'/info.json') as info: with open('./static/sets/'+tmp+'/info.json') as info:
info_file = json.loads(info.read()) info_file = json.loads(info.read())
with open('./sets/'+tmp+'/inventory.json') as inventory: with open('./static/sets/'+tmp+'/inventory.json') as inventory:
inventory_file = json.loads(inventory.read()) inventory_file = json.loads(inventory.read())
with open('./info/'+tmp+'.json') as info: with open('./info/'+tmp+'.json') as info:
json_file = json.loads(info.read()) json_file = json.loads(info.read())

43
lego.py
View File

@ -15,10 +15,14 @@ log_name='lego.log'
logging.basicConfig(filename=log_name, level=logging.DEBUG) logging.basicConfig(filename=log_name, level=logging.DEBUG)
logging.FileHandler(log_name,mode='w') logging.FileHandler(log_name,mode='w')
set_num=sys.argv[1] if '-' not in sys.argv[1]:
online_set_num=set_num+"-1" set_num = sys.argv[1] + '-1'
else:
set_num=sys.argv[1]
set_path="./sets/" + sys.argv[1] + "/" #online_set_num=set_num+"-1"
set_path="./static/sets/" + set_num + "/"
Path('./static/parts').mkdir(parents=True, exist_ok=True) Path('./static/parts').mkdir(parents=True, exist_ok=True)
Path('./info').mkdir(parents=True, exist_ok=True) Path('./info').mkdir(parents=True, exist_ok=True)
@ -76,16 +80,33 @@ with open(set_path+'inventory.json', 'w', encoding='utf-8') as f:
# get part images if not exists # get part images if not exists
for i in response["results"]: for i in response["results"]:
if not Path("./static/parts/"+i["element_id"]+".jpg").is_file(): try:
res = requests.get(i["part"]["part_img_url"], stream = True) if i['element_id'] == None:
if not Path("./static/parts/p_"+i["part"]["part_id"]+".jpg").is_file():
res = requests.get(i["part"]["part_img_url"], stream = True)
if res.status_code == 200: if res.status_code == 200:
with open("./static/parts/"+i["element_id"]+".jpg",'wb') as f: with open("./static/parts/p_"+i["part"]["part_id"]+".jpg",'wb') as f:
shutil.copyfileobj(res.raw, f) shutil.copyfileobj(res.raw, f)
print('image saved') print('image saved')
else: else:
print('Image Couldn\'t be retrieved for set ' + set_num + ": " + i["element_id"]) if not Path("./static/parts/"+i["element_id"]+".jpg").is_file():
logging.error(set_num + ": " + i["element_id"]) res = requests.get(i["part"]["part_img_url"], stream = True)
if res.status_code == 200:
with open("./static/parts/"+i["element_id"]+".jpg",'wb') as f:
shutil.copyfileobj(res.raw, f)
print('image saved')
if not Path("./static/parts/"+i["element_id"]+".jpg").is_file():
res = requests.get(i["part"]["part_img_url"], stream = True)
if res.status_code == 200:
with open("./static/parts/"+i["element_id"]+".jpg",'wb') as f:
shutil.copyfileobj(res.raw, f)
print('image saved')
except Exception as e:
print(e)
logging.error(set_num + ": " + str(e))
# read info file with missing pieces # read info file with missing pieces