Remove iframe embeds from RSS feed
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 15s

This commit is contained in:
nstarikov 2026-09-17 09:11:22 +03:00
parent e877de1d54
commit 4585f32c72
2 changed files with 44 additions and 9 deletions

View file

@ -52,16 +52,8 @@ def video_embed_html(article: Article, rss_safe: bool = False) -> str:
return "" return ""
embed_url = vk_video_embed_url(article.video.source_url) embed_url = vk_video_embed_url(article.video.source_url)
if rss_safe: if rss_safe:
iframe = ""
if embed_url:
iframe = (
f'<p><iframe src="{escape(embed_url)}" width="1280" height="720" '
'allow="autoplay; encrypted-media; fullscreen; picture-in-picture; screen-wake-lock;" '
'frameborder="0" allowfullscreen></iframe></p>'
)
return f""" return f"""
<h2>Смотреть видео</h2> <h2>Смотреть видео</h2>
{iframe}
<p><a href="{escape(article.video.source_url)}">Смотреть исходное видео VK Видео</a></p>""".strip() <p><a href="{escape(article.video.source_url)}">Смотреть исходное видео VK Видео</a></p>""".strip()
if not embed_url: if not embed_url:
return "" return ""
@ -154,6 +146,11 @@ def cover_html(article: Article, rss_safe: bool = False) -> str:
def sanitize_rss_html(html: str) -> 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"<iframe\b[^>]*>.*?</iframe\s*>", "", html, flags=re.I | re.S)
html = re.sub(r"<iframe\b[^>]*/\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"\s+class='[^']*'", "", html) html = re.sub(r"\s+class='[^']*'", "", html)
html = re.sub(r"</?(?:section|div)\b[^>]*>", "", html, flags=re.I) html = re.sub(r"</?(?:section|div)\b[^>]*>", "", html, flags=re.I)

View file

@ -7,7 +7,7 @@ from sqlalchemy.orm import Session
from app.config import Settings from app.config import Settings
from app.database import Base from app.database import Base
from app.models import Article from app.models import Article, Video
from app.rss import build_feed_xml from app.rss import build_feed_xml
@ -41,6 +41,44 @@ class RssFeedTest(unittest.TestCase):
self.assertIn("<p>Тестовый лид.</p>", content) self.assertIn("<p>Тестовый лид.</p>", content)
self.assertIn("<p>Основной текст.</p>", content) self.assertIn("<p>Основной текст.</p>", content)
def test_rss_uses_media_player_without_iframe_embed_markup(self):
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
video = Video(
external_id="video-123",
source_url="https://vkvideo.ru/video-1_123",
title="Видео",
)
article = Article(
video=video,
slug="video-article",
title="Статья с видео",
lead="<p>Лид.</p>",
body_html=(
'<figure><iframe src="https://vkvideo.ru/video_ext.php?oid=-1&amp;id=123" '
'width="1280" height="720"></iframe></figure>'
"<p>Текст.</p>"
),
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"))
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("<iframe", content.lower())
self.assertIn("Смотреть исходное видео VK Видео", content)
self.assertIn("<media:player", xml)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()