Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Disane87 committed May 13, 2024
0 parents commit c959397
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_URL=<your spooky URL>/api/v1/filament
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
output_filaments
.env
Empty file added .history/.env_20240513105444
Empty file.
Empty file.
1 change: 1 addition & 0 deletions .history/.env_20240513105506
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_URL=https://spoolman.disane.dev/api/v1/filament
1 change: 1 addition & 0 deletions .history/.env_20240513105554.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_URL=<your spooky URL>/api/v1/filament
Empty file.
1 change: 1 addition & 0 deletions .history/.gitignore_20240513104938
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output_filaments
2 changes: 2 additions & 0 deletions .history/.gitignore_20240513105517
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
output_filaments
.env
73 changes: 73 additions & 0 deletions .history/main_20240513104500.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import requests
import json
import os
from jsonschema import validate, ValidationError

# URL to fetch the JSON Schema
schema_url = "https://raw.githubusercontent.com/Donkie/SpoolmanDB/main/filaments.schema.json"

# API URL to fetch filaments data
api_url = "https://spoolman.disane.dev/api/v1/filament"

# 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}")
73 changes: 73 additions & 0 deletions .history/main_20240513105007.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import requests
import json
import os
from jsonschema import validate, ValidationError

# URL to fetch the JSON Schema
filaments_schema_url = "https://raw.githubusercontent.com/Donkie/SpoolmanDB/main/filaments.schema.json"

# API URL to fetch filaments data
api_url = "https://spoolman.disane.dev/api/v1/filament"

# Fetch the schema from the provided URL
response_schema = requests.get(filaments_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}")
77 changes: 77 additions & 0 deletions .history/main_20240513105603.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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}")
17 changes: 17 additions & 0 deletions .history/requirements_20240513104728.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
attrs==23.2.0
beautifulsoup4==4.12.2
certifi==2023.7.22
charset-normalizer==3.3.0
distlib==0.3.8
filelock==3.13.1
idna==3.4
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
network==0.1
platformdirs==4.2.0
referencing==0.35.1
requests==2.31.0
rpds-py==0.18.1
soupsieve==2.5
urllib3==2.0.6
virtualenv==20.25.1
17 changes: 17 additions & 0 deletions .history/requirements_20240513104733.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
attrs==23.2.0
beautifulsoup4==4.12.2
certifi==2023.7.22
charset-normalizer==3.3.0
distlib==0.3.8
filelock==3.13.1
idna==3.4
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
network==0.1
platformdirs==4.2.0
referencing==0.35.1
requests==2.31.0
rpds-py==0.18.1
soupsieve==2.5
urllib3==2.0.6
virtualenv==20.25.1
77 changes: 77 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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}")
17 changes: 17 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
attrs==23.2.0
beautifulsoup4==4.12.2
certifi==2023.7.22
charset-normalizer==3.3.0
distlib==0.3.8
filelock==3.13.1
idna==3.4
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
network==0.1
platformdirs==4.2.0
referencing==0.35.1
requests==2.31.0
rpds-py==0.18.1
soupsieve==2.5
urllib3==2.0.6
virtualenv==20.25.1

0 comments on commit c959397

Please sign in to comment.