Remove redundant step numbers from recipe instructions in UI and database
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Strip leading step numbers from instructions in MariaDB to avoid duplicate numbers in the UI.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from app.database import get_connection
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
STEP_REGEX = re.compile(r'^(?:Schritt\s*\d+[:.]?|\d+[\.\)\:])\s*', re.IGNORECASE)
|
||||
|
||||
def clean_instructions_text(text: str) -> str:
|
||||
if not text:
|
||||
return ""
|
||||
cleaned_lines = []
|
||||
for line in text.split("\n"):
|
||||
line_s = line.strip()
|
||||
if not line_s:
|
||||
continue
|
||||
cleaned = STEP_REGEX.sub('', line_s)
|
||||
cleaned_lines.append(cleaned)
|
||||
return "\n".join(cleaned_lines)
|
||||
|
||||
def main():
|
||||
conn = get_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT id, title, instructions FROM recipes ORDER BY id")
|
||||
recipes = cur.fetchall()
|
||||
|
||||
updated_count = 0
|
||||
total_lines_cleaned = 0
|
||||
|
||||
for r in recipes:
|
||||
original = r['instructions'] or ""
|
||||
cleaned = clean_instructions_text(original)
|
||||
if cleaned != original:
|
||||
cur.execute("UPDATE recipes SET instructions = %s WHERE id = %s", (cleaned, r['id']))
|
||||
updated_count += 1
|
||||
# count cleaned lines
|
||||
for orig_line in original.split("\n"):
|
||||
if STEP_REGEX.match(orig_line.strip()):
|
||||
total_lines_cleaned += 1
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
print(f"Successfully cleaned instructions across {updated_count} of {len(recipes)} recipes.")
|
||||
print(f"Total step number prefixes removed: {total_lines_cleaned}.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user