Initial Upload

This commit is contained in:
FrederikBaerentsen 2024-02-28 20:08:16 +01:00
parent a08513190c
commit f2884e185e
7 changed files with 177 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
api

27
app.py Normal file
View File

@ -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)

1
data.json Normal file
View File

@ -0,0 +1 @@
{"number": "10"}

81
lego.py Normal file
View File

@ -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")

16
set_template.json Normal file
View File

@ -0,0 +1,16 @@
{
"count": 1,
"unit": [
"location": "",
"minifigs": "",
"bricks": {
"missing": [
"brick" : {
"ID": ,
"color_name": ,
"amount":
}
]
}
]
}

34
static/style.css Normal file
View File

@ -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;
}

17
templates/index.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flask App - Insert Number</title>
</head>
<body>
<h1>Insert Number</h1>
<form action="/saveNumber" method="post">
<label for="numberInput">Number:</label>
<input type="number" id="numberInput" name="numberInput">
<button type="submit">Save Number</button>
</form>
</body>
</html>