big changes, sorry

This commit is contained in:
2025-05-27 12:22:52 +03:00
parent bb12fe5ca7
commit bcee61a817
7 changed files with 371 additions and 98 deletions

View File

@@ -474,9 +474,6 @@ def start_translation(filename: str):
product["portal_category_id"] = category["portal_id"]
product["local_category_id"] = category["id"]
# Ограничиваем количество товаров только если лимит больше 0
if app_settings["items_limit"] > 0:
products = products[: app_settings["items_limit"]]
translation_status["total_items"] = len(products)
# Создаем экземпляр переводчика
@@ -486,17 +483,24 @@ def start_translation(filename: str):
translated_products = []
for i, product in enumerate(products):
translated_product = translator.translate_product(product)
translated_products.append(translated_product)
if translated_product is not None: # ✅ фільтрація
translated_products.append(translated_product)
translation_status["processed_items"] = i + 1
# Сохраняем переведенные данные в отдельную директорию
output_filename = filename.replace(
"_products.json", "_translated_products.json"
)
with open(
os.path.join("output/translated", output_filename), "w", encoding="utf-8"
) as f:
json.dump(translated_products, f, ensure_ascii=False, indent=2)
if translated_products:
output_filename = filename.replace(
"_products.json", "_translated_products.json"
)
with open(
os.path.join("output/translated", output_filename),
"w",
encoding="utf-8",
) as f:
json.dump(translated_products, f, ensure_ascii=False, indent=2)
print(f"[OK] Збережено переклад: {output_filename}")
else:
print(f"[SKIP] Жодного перекладеного товару: {filename}. Файл не створено.")
except Exception as e:
translation_status["error"] = str(e)
@@ -546,6 +550,7 @@ def refresh_all_categories_daily():
parsing_status["is_running"] = False
time.sleep(5)
generate_full_yml()
print("[DONE] Автоматичне оновлення завершено")
@@ -577,22 +582,6 @@ def translate_all_parsed_once():
print("[DONE] Запуск перекладу завершено.")
@app.route("/update-settings", methods=["POST"])
def update_settings():
"""Обновление настроек приложения"""
try:
data = request.json
if "items_limit" in data:
items_limit = int(data["items_limit"])
if items_limit == -1 or items_limit >= 1:
app_settings["items_limit"] = items_limit
return jsonify({"success": True})
else:
return jsonify({"error": "Значение должно быть -1 или больше 0"})
except Exception as e:
return jsonify({"error": str(e)})
@app.route("/manual-translate-all", methods=["POST"])
@login_required
def manual_translate_all():
@@ -617,14 +606,23 @@ def generate_full_yml():
if not products:
continue
# Перевірка на наявність ID
if not all(
"portal_category_id" in p and "local_category_id" in p
for p in products
):
print(f"[SKIP] {file} — відсутні ID")
# Витягуємо slug із назви файлу
slug = file.replace("_translated_products.json", "")
# Шукаємо відповідну категорію за slug у URL
category_match = next((c for c in categories if slug in c["url"]), None)
if not category_match:
print(f"[SKIP] {file} — не знайдено категорію для slug: {slug}")
continue
for product in products:
product["portal_category_id"] = category_match["portal_id"]
product["local_category_id"] = category_match["id"]
print(
f"[OK] {file} — додано категорії {category_match['id']}, {category_match['portal_id']}"
)
all_products.extend(products)
if not all_products: