feat(wordsearch): конструктор уровней + csv_words + word_usage + приоритет ручных в игре
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
Импорт слов из CSV в таблицу csv_words.
|
||||
|
||||
Запуск:
|
||||
python scripts/import_csv_words.py
|
||||
|
||||
Читает: words_a1.csv, words_a2.csv, words_b1.csv
|
||||
Пишет: csv_words (level_code, word_pl, translation_ru, translation_uk, translation_en)
|
||||
"""
|
||||
import asyncio
|
||||
import csv
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from database import get_pool
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
BACKEND_DIR = Path(__file__).parent.parent
|
||||
|
||||
CSV_FILES = [
|
||||
("A1", "words_a1.csv"),
|
||||
("A2", "words_a2.csv"),
|
||||
("B1", "words_b1.csv"),
|
||||
]
|
||||
|
||||
|
||||
async def import_level(conn, level_code: str, filename: str) -> int:
|
||||
"""Импортирует один CSV. Возвращает количество добавленных слов."""
|
||||
path = BACKEND_DIR / filename
|
||||
if not path.exists():
|
||||
logger.warning(f"⚠️ Файл не найден: {path} — пропускаю")
|
||||
return 0
|
||||
|
||||
count = 0
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
reader = csv.DictReader(f)
|
||||
for row in reader:
|
||||
word_pl = (row.get('polish') or '').strip().lower()
|
||||
if not word_pl:
|
||||
continue
|
||||
|
||||
translation_en = (row.get('english') or '').strip()
|
||||
translation_ru = (row.get('russian') or '').strip()
|
||||
translation_uk = (row.get('ukrainian') or '').strip()
|
||||
|
||||
await conn.execute("""
|
||||
INSERT INTO csv_words (level_code, word_pl, translation_ru, translation_uk, translation_en)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (level_code, word_pl) DO UPDATE
|
||||
SET translation_ru = EXCLUDED.translation_ru,
|
||||
translation_uk = EXCLUDED.translation_uk,
|
||||
translation_en = EXCLUDED.translation_en
|
||||
""", level_code, word_pl, translation_ru, translation_uk, translation_en)
|
||||
count += 1
|
||||
|
||||
logger.info(f"✅ {level_code} ({filename}): {count} слов импортировано")
|
||||
return count
|
||||
|
||||
|
||||
async def main():
|
||||
pool = await get_pool()
|
||||
total = 0
|
||||
async with pool.acquire() as conn:
|
||||
for level_code, filename in CSV_FILES:
|
||||
total += await import_level(conn, level_code, filename)
|
||||
|
||||
logger.info(f"🎉 Готово! Всего импортировано: {total} слов")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user