"""
parser.py -- Extrahiert Text und Standort aus HTML.
"""
import re
import logging
from bs4 import BeautifulSoup
logger = logging.getLogger(__name__)
MAX_TEXT_LENGTH = 3000
def extract_text(html: str) -> str:
"""
Extrahiert bereinigten Text aus HTML.
Priorisiert: Impressum, Kontakt, About-Bereiche.
"""
try:
soup = BeautifulSoup(html, "html.parser")
# Unwichtige Tags entfernen
for tag in soup(["script", "style", "nav", "footer",
"header", "aside", "form", "iframe", "noscript"]):
tag.decompose()
text = soup.get_text(" ", strip=True)
text = re.sub(r"\s+", " ", text)
return text[:MAX_TEXT_LENGTH]
except Exception as e:
logger.debug(f"Text-Extraktion fehlgeschlagen: {e}")
return ""
def extract_location(text: str) -> str | None:
"""
Sucht Standort (Stadt, Land) im Text.
Returns:
z.B. "Muenchen, Deutschland" oder None
"""
pattern = r"\b([A-ZÄÖÜ][a-zäöü]+(?:\s[A-ZÄÖÜ][a-zäöü]+)?),\s*(Deutschland|Oesterreich|Österreich|Schweiz|Spain|Spanien|Austria|Germany|Switzerland)\b"
match = re.search(pattern, text)
if match:
return match.group(0)
return None