From a843e929d7a3b13f881a8ba262e5753e0d6e4768 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sun, 3 Mar 2024 06:43:24 -0500 Subject: [PATCH] Working frontpage with responsive grid. Fixed issue with part without element_id --- app.py | 14 ++++++++------ lego.py | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app.py b/app.py index a756fc3..4548cdc 100644 --- a/app.py +++ b/app.py @@ -13,16 +13,18 @@ def index(): pathlist = Path('./info/').rglob('*.json') set_list = [] for path in pathlist: - print(str(path)) - set_list.append(re.findall(r"\b\d+(?:-\d+)?\b",str(path))) - - return set_list + set_num = re.findall(r"\b\d+(?:-\d+)?\b",str(path))[0] + with open('./static/sets/'+set_num+'/info.json') as info: + info_file = json.loads(info.read()) + set_list.append(info_file) + print(set_list) + return render_template('frontpage.html',set_list=set_list) @app.route('/') 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()) - with open('./sets/'+tmp+'/inventory.json') as inventory: + with open('./static/sets/'+tmp+'/inventory.json') as inventory: inventory_file = json.loads(inventory.read()) with open('./info/'+tmp+'.json') as info: json_file = json.loads(info.read()) diff --git a/lego.py b/lego.py index 24c3cc1..59fcaea 100644 --- a/lego.py +++ b/lego.py @@ -15,10 +15,14 @@ log_name='lego.log' logging.basicConfig(filename=log_name, level=logging.DEBUG) logging.FileHandler(log_name,mode='w') -set_num=sys.argv[1] -online_set_num=set_num+"-1" +if '-' not in sys.argv[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('./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 for i in response["results"]: - if not Path("./static/parts/"+i["element_id"]+".jpg").is_file(): - res = requests.get(i["part"]["part_img_url"], stream = True) + try: + 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: - with open("./static/parts/"+i["element_id"]+".jpg",'wb') as f: - shutil.copyfileobj(res.raw, f) - print('image saved') + if res.status_code == 200: + with open("./static/parts/p_"+i["part"]["part_id"]+".jpg",'wb') as f: + shutil.copyfileobj(res.raw, f) + print('image saved') else: - print('Image Couldn\'t be retrieved for set ' + set_num + ": " + i["element_id"]) - logging.error(set_num + ": " + i["element_id"]) + 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') + 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