forked from TERA_p3104/Client-Datasheets
78 lines
3.8 KiB
Python
78 lines
3.8 KiB
Python
from lxml import etree
|
|
import os
|
|
|
|
# Función para generar el nombre del atributo según las condiciones
|
|
def generate_attribute_name(element_name, attribute_name):
|
|
return element_name[0].lower() + "_" + attribute_name
|
|
|
|
# Función para generar el query CREATE TABLE
|
|
def generate_create_table_query():
|
|
create_query = "CREATE TABLE IF NOT EXISTS RecipeTable (\n"
|
|
create_query += " id INT PRIMARY KEY,\n"
|
|
create_query += " "
|
|
create_query += ",\n ".join([f"id INT" if attr == 'id' else f"{generate_attribute_name('Recipe', attr)} VARCHAR(255)" for attr in recipe_attributes if attr != 'id'])
|
|
create_query += ",\n "
|
|
create_query += ",\n ".join([f"{generate_attribute_name('Material', attr)} VARCHAR(255)" for attr in material_attributes])
|
|
create_query += ",\n "
|
|
create_query += ",\n ".join([f"{generate_attribute_name('Result', attr)} VARCHAR(255)" for attr in result_attributes])
|
|
create_query += "\n);"
|
|
return create_query
|
|
|
|
|
|
# Función para generar el query INSERT INTO
|
|
def generate_insert_query(recipe_data):
|
|
recipe_values = ', '.join([f'"{recipe_data[attr]}"' if attr in recipe_data and attr != 'id' else 'NULL' for attr in recipe_attributes])
|
|
material_values = ', '.join([f'"{recipe_data.get(f"m_{attr}", "NULL")}"' for attr in material_attributes])
|
|
result_values = ', '.join([f'"{recipe_data.get(f"r_{attr}", "NULL")}"' for attr in result_attributes])
|
|
insert_query = f"INSERT INTO RecipeTable (id, {', '.join([generate_attribute_name('Recipe', attr) for attr in recipe_attributes if attr != 'id'])}, {', '.join([generate_attribute_name('Material', attr) for attr in material_attributes])}, {', '.join([generate_attribute_name('Result', attr) for attr in result_attributes])}) VALUES ({recipe_data['id']}, {recipe_values}, {material_values}, {result_values});"
|
|
return insert_query
|
|
|
|
|
|
|
|
|
|
# Definir las listas de atributos para cada elemento
|
|
recipe_attributes = ['categoryId', 'grade', 'maxSkillProf', 'subFatiguePoint', 'obtainable', 'needSkillId', 'recipeItemId', 'addOnSuccessProf', 'needSkillProf', 'unionClass']
|
|
material_attributes = ['id', 'count', 'enchant']
|
|
result_attributes = ['id', 'count', 'criticalItemId', 'criticalItemCount']
|
|
|
|
# Directorio donde se encuentran los archivos XML
|
|
directory = '.'
|
|
|
|
# Lista para almacenar los datos de cada receta
|
|
recipes_data = []
|
|
|
|
# Iterar sobre los archivos XML en el directorio
|
|
for filename in os.listdir(directory):
|
|
if filename.endswith('.xml'):
|
|
tree = etree.parse(os.path.join(directory, filename))
|
|
root = tree.getroot()
|
|
|
|
for recipe in root.findall('{https://vezel.dev/novadrop/dc/ItemProduceRecipe}Recipe'):
|
|
recipe_data = {'id': recipe.get('id')}
|
|
for attr in recipe_attributes:
|
|
recipe_data[attr] = recipe.get(attr)
|
|
|
|
materials = recipe.find('{https://vezel.dev/novadrop/dc/ItemProduceRecipe}Materials')
|
|
for material in materials.findall('{https://vezel.dev/novadrop/dc/ItemProduceRecipe}Material'):
|
|
for attr in material_attributes:
|
|
recipe_data[f'm_{attr}'] = material.get(attr)
|
|
|
|
result = recipe.find('{https://vezel.dev/novadrop/dc/ItemProduceRecipe}Result')
|
|
for attr in result_attributes:
|
|
recipe_data[f'r_{attr}'] = result.get(attr)
|
|
|
|
recipes_data.append(recipe_data)
|
|
|
|
# Generar el query CREATE TABLE
|
|
create_query = generate_create_table_query()
|
|
|
|
# Escribir el query CREATE TABLE en un archivo create.sql
|
|
with open('create.sql', 'w') as create_file:
|
|
create_file.write(create_query + '\n')
|
|
|
|
# Generar los queries INSERT INTO y escribirlos en un archivo populate.sql
|
|
with open('populate.sql', 'w') as populate_file:
|
|
for recipe_data in recipes_data:
|
|
insert_query = generate_insert_query(recipe_data)
|
|
populate_file.write(insert_query + '\n')
|