diff --git a/app/main.py b/app/main.py index 8c01d6d..2361d6a 100644 --- a/app/main.py +++ b/app/main.py @@ -34,7 +34,6 @@ app.add_middleware(AuthMiddleware) settings = get_settings() mimetypes.add_type("image/webp", ".webp") app.mount("/media", StaticFiles(directory=settings.media_path, check_dir=False), name="media") -app.mount("/videos", StaticFiles(directory=settings.videos_path, check_dir=False), name="videos") app.mount("/posts-static", StaticFiles(directory=settings.posts_path, check_dir=False), name="posts-static") admin_dist = Path("/app/admin_dist") diff --git a/app/rss.py b/app/rss.py index 81abc2a..0ec219a 100644 --- a/app/rss.py +++ b/app/rss.py @@ -1,8 +1,7 @@ from datetime import datetime, timezone from html import escape, unescape -from pathlib import Path import re -from urllib.parse import quote, urlencode +from urllib.parse import urlencode from sqlalchemy import select from sqlalchemy.orm import Session @@ -48,22 +47,21 @@ def vk_video_embed_url(source_url: str) -> str | None: return f"https://vkvideo.ru/video_ext.php?{urlencode(params)}" -def video_embed_html(article: Article, rss_safe: bool = False, video_url: str | None = None) -> str: +def video_embed_html(article: Article, rss_safe: bool = False) -> str: if not article.video or not article.video.source_url: return "" embed_url = vk_video_embed_url(article.video.source_url) if rss_safe: - if video_url: - video = ( - f'" + iframe = "" + if embed_url: + iframe = ( + f'

' ) - else: - video = "" return f"""

Смотреть видео

-{video} +{iframe}

Смотреть исходное видео VK Видео

""".strip() if not embed_url: return "" @@ -77,19 +75,10 @@ def video_embed_html(article: Article, rss_safe: bool = False, video_url: str | """.strip() -def render_video_placeholder( - article: Article, - html: str, - rss_safe: bool = False, - video_url: str | None = None, -) -> tuple[str, bool]: +def render_video_placeholder(article: Article, html: str, rss_safe: bool = False) -> tuple[str, bool]: if VIDEO_PLACEHOLDER not in html: return html, False - return html.replace( - VIDEO_PLACEHOLDER, - video_embed_html(article, rss_safe=rss_safe, video_url=video_url), - 1, - ), True + return html.replace(VIDEO_PLACEHOLDER, video_embed_html(article, rss_safe=rss_safe), 1), True def premium_cta_html() -> str: @@ -165,11 +154,6 @@ def cover_html(article: Article, rss_safe: bool = False) -> str: def sanitize_rss_html(html: str) -> str: - # Dzen converts arbitrary iframes into an internal atomic:embed marker. - # RSS already carries the same video through media:player, so omit iframe - # markup here while keeping it available on the public article page. - html = re.sub(r"]*>.*?", "", html, flags=re.I | re.S) - html = re.sub(r"]*/\s*>", "", html, flags=re.I | re.S) html = re.sub(r"\s+class=\"[^\"]*\"", "", html) html = re.sub(r"\s+class='[^']*'", "", html) html = re.sub(r"]*>", "", html, flags=re.I) @@ -239,26 +223,16 @@ def media_player_xml(article: Article) -> str: return f'' -def render_article_html( - article: Article, - include_video_embed: bool = True, - rss_safe: bool = False, - video_url: str | None = None, -) -> str: +def render_article_html(article: Article, include_video_embed: bool = True, rss_safe: bool = False) -> str: lead_html, lead_question_html = split_audience_question_html(article.lead) body_html = strip_article_cover_html(article.body_html) body_html, audience_question_html = split_audience_question_html(body_html) audience_question_html = audience_question_html or lead_question_html - body_html, has_video_placeholder = render_video_placeholder( - article, - body_html, - rss_safe=rss_safe, - video_url=video_url, - ) + body_html, has_video_placeholder = render_video_placeholder(article, body_html, rss_safe=rss_safe) body_html = sanitize_rss_html(body_html) if rss_safe else body_html parts = [lead_html, cover_html(article, rss_safe=rss_safe), body_html] if include_video_embed and not has_video_placeholder: - parts.append(video_embed_html(article, rss_safe=rss_safe, video_url=video_url)) + parts.append(video_embed_html(article, rss_safe=rss_safe)) if audience_question_html: parts.append(audience_question_html) if article.cta_html: @@ -268,16 +242,6 @@ def render_article_html( return sanitize_rss_html(html) if rss_safe else html -def local_video_public_url(article: Article, settings: Settings) -> str | None: - """Return a public MP4 URL only when the downloaded source still exists.""" - if not article.video or not article.video.local_video_path: - return None - path = Path(article.video.local_video_path) - if not path.is_file() or path.suffix.lower() != ".mp4": - return None - return f"{settings.public_base_url.rstrip('/')}/videos/{article.video.id}/{quote(path.name)}" - - def build_feed_xml(session: Session, settings: Settings) -> str: now = datetime.now(timezone.utc) articles = session.scalars( @@ -290,11 +254,7 @@ def build_feed_xml(session: Session, settings: Settings) -> str: items: list[str] = [] for article in articles: link = article_url(settings, article) - content = render_article_html( - article, - rss_safe=True, - video_url=local_video_public_url(article, settings), - ) + content = render_article_html(article, rss_safe=True) frames = [] if article.video_id: frames = session.scalars( @@ -309,6 +269,7 @@ def build_feed_xml(session: Session, settings: Settings) -> str: enclosure = f'' elif frames and frames[0].public_url: enclosure = f'' + media_player = media_player_xml(article) items.append( f""" @@ -320,6 +281,7 @@ def build_feed_xml(session: Session, settings: Settings) -> str: index comment-all nonadult + {media_player} {enclosure} diff --git a/tests/test_rss.py b/tests/test_rss.py index 32281a7..a7f91a4 100644 --- a/tests/test_rss.py +++ b/tests/test_rss.py @@ -1,7 +1,5 @@ import unittest from datetime import datetime, timedelta, timezone -from pathlib import Path -from tempfile import TemporaryDirectory from xml.etree import ElementTree from sqlalchemy import create_engine @@ -43,49 +41,40 @@ class RssFeedTest(unittest.TestCase): self.assertIn("

Тестовый лид.

", content) self.assertIn("

Основной текст.

", content) - def test_rss_uses_native_mp4_video_without_iframe_embed_markup(self): + def test_rss_keeps_vk_embed_markup_for_video(self): engine = create_engine("sqlite:///:memory:") Base.metadata.create_all(engine) - with TemporaryDirectory() as temp_dir: - video_path = Path(temp_dir) / "video.mp4" - video_path.write_bytes(b"test") - video = Video( - external_id="video-123", - source_url="https://vkvideo.ru/video-1_123", - title="Видео", - local_video_path=str(video_path), - ) - article = Article( - video=video, - slug="video-article", - title="Статья с видео", - lead="

Лид.

", - body_html=( - '
' - "

Текст.

" - ), - cta_html="", - feature_image_url="", - status="published", - rss_guid="video-guid", - published_at=datetime.now(timezone.utc) - timedelta(minutes=1), - ) + video = Video( + external_id="video-123", + source_url="https://vkvideo.ru/video-1_123", + title="Видео", + ) + article = Article( + video=video, + slug="video-article", + title="Статья с видео", + lead="

Лид.

", + body_html="

Текст.

", + cta_html="", + feature_image_url="", + status="published", + rss_guid="video-guid", + published_at=datetime.now(timezone.utc) - timedelta(minutes=1), + ) - with Session(engine) as session: - session.add(article) - session.commit() - xml = build_feed_xml(session, Settings(public_base_url="https://example.test")) + with Session(engine) as session: + session.add(article) + session.commit() + xml = build_feed_xml(session, Settings(public_base_url="https://example.test")) root = ElementTree.fromstring(xml) item = root.find("./channel/item") self.assertIsNotNone(item) content = item.findtext("{http://purl.org/rss/1.0/modules/content/}encoded") or "" - self.assertNotIn("