diff --git a/dzen-assistant/app/rss.py b/dzen-assistant/app/rss.py index fb838e0..67e576e 100644 --- a/dzen-assistant/app/rss.py +++ b/dzen-assistant/app/rss.py @@ -160,16 +160,40 @@ def sanitize_rss_html(html: str) -> str: return html +def _is_audience_question_paragraph(paragraph_html: str) -> bool: + text = re.sub(r"<[^>]+>", " ", paragraph_html, flags=re.S) + text = re.sub(r"\s+", " ", text).strip().lower() + return text.startswith("ваше мнение:") and "комментар" in text + + def split_audience_question_html(html: str) -> tuple[str, str]: - match = re.search( - r'(
]*class=["\'][^"\']*\baudience-question\b[^"\']*["\'][^>]*>.*?
)', - html or "", - flags=re.I | re.S, + """Extract one audience question and remove accidental duplicate copies.""" + source = html or "" + paragraphs = list(re.finditer(r"]*>.*?
", source, flags=re.I | re.S)) + question_match = next( + ( + match + for match in paragraphs + if "audience-question" in match.group(0).lower() + or _is_audience_question_paragraph(match.group(0)) + ), + None, ) - if not match: - return html or "", "" - body_html = f"{html[:match.start()]}{html[match.end():]}".strip() - return body_html, match.group(1).strip() + if not question_match: + return source, "" + + question_html = question_match.group(0).strip() + body_parts: list[str] = [] + cursor = 0 + for match in paragraphs: + if ( + "audience-question" in match.group(0).lower() + or _is_audience_question_paragraph(match.group(0)) + ): + body_parts.append(source[cursor:match.start()]) + cursor = match.end() + body_parts.append(source[cursor:]) + return "".join(body_parts).strip(), question_html def media_player_xml(article: Article) -> str: diff --git a/dzen-assistant/app/worker.py b/dzen-assistant/app/worker.py index 89f9ba7..83ca7e0 100644 --- a/dzen-assistant/app/worker.py +++ b/dzen-assistant/app/worker.py @@ -20,7 +20,7 @@ from app.models import AppSetting, Article, Frame, Source, Video from app.models import ImageGenerationBatch, ImageGenerationJob from app.image_generation import ImageGenerationError, PolzaClient, error_from_status, extract_result_url, save_remote_image from app.quality import validate_article_content, validate_transcript_text -from app.rss import VIDEO_PLACEHOLDER +from app.rss import VIDEO_PLACEHOLDER, split_audience_question_html settings = get_settings() @@ -1244,9 +1244,11 @@ def neutralize_first_person_text(text: str) -> str: def article_body_html(item: dict[str, Any], frames: list[Frame], title: str, video: Video) -> str: body_html = generated_content_to_html(str(item.get("body") or "")) body_html = inject_section_frames(body_html, frames, title) + body_html, _ = split_audience_question_html(body_html) + conclusion_html, _ = split_audience_question_html(generated_content_to_html(str(item.get("conclusion") or ""))) parts = [ body_html, - generated_content_to_html(str(item.get("conclusion") or "")), + conclusion_html, f"{VIDEO_PLACEHOLDER}
", audience_question_html(item, title), ]