From 525169ef77307568bbce4ba64648b4af0ab88d33 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Wed, 24 Apr 2024 20:24:04 +0200 Subject: [PATCH] Added sets.csv download, added missing minifig image error, added auto download of nil and nil_mf images, started proper fix for error in set numbers --- app.py | 69 ++++++++++++++++++++++++++++--------------- lego.sh | 12 +++++++- templates/create.html | 17 +++++++---- templates/table.html | 4 +++ 4 files changed, 72 insertions(+), 30 deletions(-) diff --git a/app.py b/app.py index b4bf980..43e9410 100644 --- a/app.py +++ b/app.py @@ -32,10 +32,32 @@ def test_disconnect(): def start_task(data): input_value = data.get('inputField') print(input_value) - # Start the task in a separate thread to avoid blocking the server + + + + input_value = input_value.replace(" ","") + if '-' not in input_value: + input_value = input_value + '-1' + + + + total_set_file = np.genfromtxt("sets.csv",delimiter=",",dtype="str",usecols=(0)) + print(total_set_file) + + if input_value not in total_set_file: + print('ERROR: ' + input_value) + # Reload create.html with error message + socketio.emit('task_failed', {'error': 'set_num'}, namespace='/progress') + return render_template('create.html',error=input_value) + + + # Start the task in a separate thread to avoid blocking the serve + print('starting servers') thread = Thread(target=new_set, args=(input_value,)) thread.start() + #return redirect('/') + @app.route('/delete/',methods=['POST', 'GET']) def delete(tmp): @@ -63,7 +85,8 @@ def new_set(set_num): total_parts = 4 - set_num = set_num.replace(" ","") + + # add_duplicate = request.form.get('addDuplicate', False) == 'true' # Do something with the input value and the checkbox value # print("Input value:", set_num) @@ -71,8 +94,7 @@ def new_set(set_num): # You can perform any further processing or redirect to another page conn = sqlite3.connect('app.db') cursor = conn.cursor() - if '-' not in set_num: - set_num = set_num + '-1' + print ("Adding set: " + set_num) with open('api','r') as f: @@ -83,15 +105,16 @@ def new_set(set_num): # Get Set info and add to SQL response = '' - try: - response = json.loads(rebrick.lego.get_set(set_num).read()) + # try: + response = json.loads(rebrick.lego.get_set(set_num).read()) - except Exception as e: - #print(e.code) - if e.code == 404: - return render_template('create.html',error=set_num) + # except Exception as e: + # #print(e.code) + # if e.code == 404: + # return render_template('create.html',error=set_num) count+=1 + socketio.emit('update_progress', {'progress': int(count/total_parts*100)}, namespace='/progress') cursor.execute('''INSERT INTO sets ( set_num, name, @@ -208,18 +231,20 @@ def new_set(set_num): #print('Saving set image:',end='') if not Path("./static/minifigs/"+set_num+".jpg").is_file(): - res = requests.get(set_img_url, stream = True) - count+=1 - socketio.emit('update_progress', {'progress': int(count/total_parts*100)}, namespace='/progress') - if res.status_code == 200: - with open("./static/minifigs/"+set_num+".jpg",'wb') as f: - shutil.copyfileobj(res.raw, f) - #print(' OK') + if set_img_url is not None: + res = requests.get(set_img_url, stream = True) + count+=1 + socketio.emit('update_progress', {'progress': int(count/total_parts*100)}, namespace='/progress') + if res.status_code == 200: + with open("./static/minifigs/"+set_num+".jpg",'wb') as f: + shutil.copyfileobj(res.raw, f) + #print(' OK') + else: + #print('Image Couldn\'t be retrieved for set ' + set_num) + logging.error('set_img_url: ' + set_num) + #print(' ERROR') else: - #print('Image Couldn\'t be retrieved for set ' + set_num) - logging.error('set_img_url: ' + set_num) - #print(' ERROR') - + print(i) cursor.execute('''INSERT INTO minifigures ( fig_num, @@ -329,8 +354,6 @@ def create(): global count - - print('Count: ' + str(count)) diff --git a/lego.sh b/lego.sh index f97779d..3d40954 100644 --- a/lego.sh +++ b/lego.sh @@ -1,4 +1,14 @@ #!/bin/bash wget https://cdn.rebrickable.com/media/downloads/themes.csv.gz -gzip -d themes.csv.gz +gzip -f -d themes.csv.gz + + +wget https://cdn.rebrickable.com/media/downloads/sets.csv.gz +gzip -f -d sets.csv.gz + + +cd static/ +wget https://rebrickable.com/static/img/nil_mf.jpg +wget https://rebrickable.com/static/img/nil.png +cd .. diff --git a/templates/create.html b/templates/create.html index 89793ca..912c014 100644 --- a/templates/create.html +++ b/templates/create.html @@ -1,6 +1,6 @@ - @@ -49,13 +49,11 @@ - {% if error is defined %} + + - {% else %} - - {% endif %} +