-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (65 loc) · 2.95 KB
/
Copy pathmain.py
File metadata and controls
77 lines (65 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import requests
import json
import os
from jsonschema import validate, ValidationError
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# URL to fetch the JSON Schema
schema_url = "https://raw.githubusercontent.com/Donkie/SpoolmanDB/main/filaments.schema.json"
# API URL to fetch filaments data (loaded from .env)
api_url = os.getenv("API_URL")
# Fetch the schema from the provided URL
response_schema = requests.get(schema_url)
schema = response_schema.json()
# Make the request to the API to fetch filaments data
response_data = requests.get(api_url)
data = response_data.json()
# Function to merge colors safely
def merge_colors(filament, new_color):
if not any(color['name'] == new_color['name'] for color in filament["colors"]):
filament["colors"].append(new_color)
# Process data and organize by manufacturer
manufacturers = {}
for item in data:
manufacturer_name = item["vendor"]["name"]
# Initialize manufacturer entry if not present
if manufacturer_name not in manufacturers:
manufacturers[manufacturer_name] = {
"manufacturer": manufacturer_name,
"filaments": []
}
# Search for existing material entry
filament = next((f for f in manufacturers[manufacturer_name]["filaments"] if f["material"] == item["material"]), None)
if filament:
# If material entry exists, just add the new color
merge_colors(filament, {"name": item["name"], "hex": item["color_hex"]})
else:
# If no material entry exists, create new
new_filament = {
"name": f"{manufacturer_name} {{color_name}}",
"material": item["material"],
"density": item.get("density", 1.0),
"weights": [{
"weight": item.get("weight", 0),
"spool_weight": item.get("spool_weight", 0)
}],
"diameters": [item.get("diameter", 1.75)],
"extruder_temp": item.get("settings_extruder_temp", 200),
"bed_temp": item.get("settings_bed_temp", 60),
"colors": [{"name": item["name"], "hex": item["color_hex"]}]
}
manufacturers[manufacturer_name]["filaments"].append(new_filament)
# Validate data against the schema and save to JSON files if valid
output_dir = 'output_filaments'
os.makedirs(output_dir, exist_ok=True)
for manufacturer, content in manufacturers.items():
try:
validate(instance=content, schema=schema)
output_filename = os.path.join(output_dir, f"{manufacturer.replace(' ', '_')}.json")
with open(output_filename, 'w') as file:
json.dump(content, file, indent=4)
print(f"Data for {manufacturer} validated successfully and saved.")
except ValidationError as ve:
print(f"Validation error for {manufacturer}: {ve.message}")
print(f"All data processed and valid data saved in directory: {output_dir}")