Create duplicate sets

This commit is contained in:
FrederikBaerentsen 2024-02-29 09:22:55 -05:00
parent ff1c7e2ca1
commit 68a8eb71c1
4 changed files with 48 additions and 42 deletions

11
app.py
View File

@ -16,10 +16,6 @@ def index():
pp(json_file['unit'][0]['bricks']['missing']) pp(json_file['unit'][0]['bricks']['missing'])
return render_template('bootstrap_table.html', title=info_file['set_num']+" - "+info_file['name'], return render_template('bootstrap_table.html', title=info_file['set_num']+" - "+info_file['name'],
info_file=info_file,inventory_file=inventory_file,json_file=json_file) info_file=info_file,inventory_file=inventory_file,json_file=json_file)
#return render_template('index.html')
#'Welcome to the Flask App'
@app.route('/saveNumber', methods=['POST']) @app.route('/saveNumber', methods=['POST'])
@ -39,12 +35,9 @@ def save_number():
print(json_file['count']) print(json_file['count'])
data = '{"brick" : {"ID":"' + data1 + '","color_name": "' + data2 + '","amount":"' + number + '"}}' data = '{"brick" : {"ID":"' + data1 + '","color_name": "' + data2 + '","amount":"' + number + '"}}'
print(data)
print(json.loads(data))
json_file['unit'][0]['bricks']['missing'].append(json.loads(data)) json_file['unit'][0]['bricks']['missing'].append(json.loads(data))
print(json_file['unit'][0]['bricks'])
# Save number to JSON file
with open('./info/'+tmp+'.json', 'w') as dump_file: with open('./info/'+tmp+'.json', 'w') as dump_file:
json.dump(json_file,dump_file) json.dump(json_file,dump_file)

View File

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

58
lego.py
View File

@ -6,7 +6,7 @@ import rebrick #rebrickable api
# json things # json things
import json import json
from pprint import pprint as pp
import requests # request img from web import requests # request img from web
import shutil # save img locally import shutil # save img locally
@ -19,26 +19,44 @@ set_num=sys.argv[1]
online_set_num=set_num+"-1" online_set_num=set_num+"-1"
set_path="./sets/" + sys.argv[1] + "/" set_path="./sets/" + sys.argv[1] + "/"
Path('./static/parts').mkdir(parents=True, exist_ok=True)
if Path(set_path).is_dir(): Path('./info').mkdir(parents=True, exist_ok=True)
print('Set exists, exitting')
logging.error('Set exists!')
#exit()
with open('api','r') as f: with open('api','r') as f:
api_key = f.read().replace('\n','') api_key = f.read().replace('\n','')
rb = rebrick.init(api_key) rb = rebrick.init(api_key)
if Path(set_path).is_dir():
print('Set exists, exitting')
logging.error('Set exists!')
#exit()
Path(set_path).mkdir(parents=True, exist_ok=True) Path(set_path).mkdir(parents=True, exist_ok=True)
Path('./static/parts').mkdir(parents=True, exist_ok=True)
Path('./info').mkdir(parents=True, exist_ok=True)
# Get set info # Get set info
response = json.loads(rebrick.lego.get_set(set_num).read()) response = json.loads(rebrick.lego.get_set(set_num).read())
with open(set_path+'info.json', 'w', encoding='utf-8') as f:
json.dump(response, f, ensure_ascii=False, indent=4) if Path("./info/"+set_num + ".json").is_file():
ans = input('Set exists, would you like to add another copy (Y/N)?\n')
if ans.lower() == 'yes' or ans.lower() == 'y':
with open("./info/" + set_num + ".json",'r') as f:
data = json.load(f)
data['count'] = data['count'] + 1
tmp = {"location": "","minifigs": "","bricks": {"missing": []}}
data['unit'].append(tmp)
pp(data)
with open("./info/" + set_num + ".json",'w') as f:
json.dump(data,f,indent = 4)
else:
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 # save set image to folder
set_img_url = response["set_img_url"] set_img_url = response["set_img_url"]
@ -59,16 +77,16 @@ 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(): if not Path("./static/parts/"+i["element_id"]+".jpg").is_file():
res = requests.get(i["part"]["part_img_url"], stream = True) 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/"+i["element_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"]) print('Image Couldn\'t be retrieved for set ' + set_num + ": " + i["element_id"])
logging.error(set_num + ": " + i["element_id"]) logging.error(set_num + ": " + i["element_id"])
# read info file with missing pieces # read info file with missing pieces

View File

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