47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
import json
|
|
import os
|
|
|
|
def convert_recipe(original_recipe):
|
|
# Create a new recipe structure
|
|
new_recipe = {
|
|
"type": original_recipe["type"],
|
|
"key": {},
|
|
"pattern": original_recipe["pattern"],
|
|
"result": original_recipe["result"]
|
|
}
|
|
|
|
# Convert the key items
|
|
for key, value in original_recipe["key"].items():
|
|
new_recipe["key"][key] = {"item": value}
|
|
|
|
return new_recipe
|
|
|
|
def process_recipes_in_directory(directory):
|
|
# Iterate over all files in the specified directory
|
|
for filename in os.listdir(directory):
|
|
if filename.endswith('.json'):
|
|
file_path = os.path.join(directory, filename)
|
|
try:
|
|
# Load the original recipe
|
|
with open(file_path, 'r') as file:
|
|
original_recipe = json.load(file)
|
|
|
|
# Convert the recipe
|
|
converted_recipe = convert_recipe(original_recipe)
|
|
|
|
# Save the converted recipe to a new file
|
|
new_filename = f"converted_{filename}"
|
|
new_file_path = os.path.join(directory, new_filename)
|
|
with open(new_file_path, 'w') as new_file:
|
|
json.dump(converted_recipe, new_file, indent=2)
|
|
|
|
print(f"Converted {filename} to {new_filename}")
|
|
|
|
except Exception as e:
|
|
print(f"Error processing {filename}: {e}")
|
|
|
|
# Get the current working directory
|
|
current_directory = os.getcwd()
|
|
|
|
# Process all JSON recipes in the current directory
|
|
process_recipes_in_directory(current_directory)
|