diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eedd89b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +api diff --git a/app.py b/app.py new file mode 100644 index 0000000..f1f1c5c --- /dev/null +++ b/app.py @@ -0,0 +1,27 @@ +from flask import Flask, request, jsonify, render_template +import json + +app = Flask(__name__) + +@app.route('/') +def index(): + return render_template('index.html') + +#'Welcome to the Flask App' + + +@app.route('/saveNumber', methods=['POST']) +def save_number(): + data = request.get_json() + number = request.form.get('numberInput') + + if number is not None: + # Save number to JSON file + with open('data.json', 'w') as json_file: + json.dump({'number': number}, json_file) + return jsonify({'message': 'Number saved successfully'}), 200 + else: + return jsonify({'error': 'Invalid data provided'}), 400 + +if __name__ == '__main__': + app.run(debug=True) diff --git a/data.json b/data.json new file mode 100644 index 0000000..0dd5066 --- /dev/null +++ b/data.json @@ -0,0 +1 @@ +{"number": "10"} \ No newline at end of file diff --git a/lego.py b/lego.py new file mode 100644 index 0000000..45d88bc --- /dev/null +++ b/lego.py @@ -0,0 +1,81 @@ +import sys #using argv +import logging #logging errors + +from pathlib import Path # creating folders +import rebrick #rebrickable api + +# json things +import json + +from bs4 import BeautifulSoup + +import requests # request img from web +import shutil # save img locally + +log_name='lego.log' + +logging.basicConfig(filename=log_name, encoding='utf-8', level=logging.DEBUG) +logging.FileHandler(log_name,mode='w') + +set_num=sys.argv[1] +online_set_num=set_num+"-1" + +set_path="./sets/" + sys.argv[1] + "/" + +if Path(set_path).is_dir(): + print('Set exists, exitting') + logging.error('Set exists!') + #exit() + + +with open('api','r') as f: + api_key = f.read().replace('\n','') + +rb = rebrick.init(api_key) + +Path(set_path).mkdir(parents=True, exist_ok=True) + +# Get set info +response = json.loads(rebrick.lego.get_set(int(set_num)).read()) +with open(set_path+'info.json', 'w', encoding='utf-8') as f: + json.dump(response, f, ensure_ascii=False, indent=4) + +# save set image to folder +set_img_url = response["set_img_url"] + +res = requests.get(set_img_url, stream = True) + +if res.status_code == 200: + with open(set_path+"cover.jpg",'wb') as f: + shutil.copyfileobj(res.raw, f) +else: + print('Image Couldn\'t be retrieved for set ' + set_num) + logging.error('set_img_url: ' + set_num) + +# set inventory +response = json.loads(rebrick.lego.get_set_elements(int(set_num)).read()) +with open(set_path+'inventory.json', 'w', encoding='utf-8') as f: + json.dump(response, f, ensure_ascii=False, indent=4) + +# get part images if not exists +for i in response["results"]: + if not Path("./parts/"+i["element_id"]+".jpg").is_file(): + res = requests.get(i["part"]["part_img_url"], stream = True) + + if res.status_code == 200: + with open("./parts/"+i["element_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"]) + +# read info file with missing pieces + + +if Path("./info/"+set_num + ".json").is_file(): + with open("./info/" + set_num + ".json") as f: + data = json.load(f) + print(data) +else: + shutil.copy("set_template.json", "./info/"+set_num+".json") diff --git a/set_template.json b/set_template.json new file mode 100644 index 0000000..f27dcf7 --- /dev/null +++ b/set_template.json @@ -0,0 +1,16 @@ +{ + "count": 1, + "unit": [ + "location": "", + "minifigs": "", + "bricks": { + "missing": [ + "brick" : { + "ID": , + "color_name": , + "amount": + } + ] + } + ] +} diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..30b09e8 --- /dev/null +++ b/static/style.css @@ -0,0 +1,34 @@ +body { + font-family: Arial, sans-serif; + margin: 20px; +} + +h1 { + color: #333; +} + +form { + margin-top: 20px; +} + +label { + font-weight: bold; +} + +input[type="number"] { + padding: 5px; + margin-right: 10px; +} + +button { + padding: 8px 12px; + background-color: #007bff; + color: #fff; + border: none; + cursor: pointer; +} + +button:hover { + background-color: #0056b3; +} + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..da8704a --- /dev/null +++ b/templates/index.html @@ -0,0 +1,17 @@ + + + + + + Flask App - Insert Number + + +

Insert Number

+
+ + + +
+ + +