Create duplicate sets

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

58
lego.py
View File

@@ -6,7 +6,7 @@ import rebrick #rebrickable api
# json things
import json
from pprint import pprint as pp
import requests # request img from web
import shutil # save img locally
@@ -19,26 +19,44 @@ 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()
Path('./static/parts').mkdir(parents=True, exist_ok=True)
Path('./info').mkdir(parents=True, exist_ok=True)
with open('api','r') as f:
api_key = f.read().replace('\n','')
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('./static/parts').mkdir(parents=True, exist_ok=True)
Path('./info').mkdir(parents=True, exist_ok=True)
# Get set info
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
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
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)
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')
else:
print('Image Couldn\'t be retrieved for set ' + set_num + ": " + i["element_id"])
logging.error(set_num + ": " + i["element_id"])
if res.status_code == 200:
with open("./static/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