""".strip()
def is_po_suti_dela_article(article: Article) -> bool:
if not article.video:
return False
text = f"{article.video.title or ''}\n{article.video.description or ''}".lower()
return "по сути дела" in text
def cultural_initiatives_html() -> str:
return """
""".strip()
def cover_html(article: Article, rss_safe: bool = False) -> str:
if not article.video or not article.video.thumbnail_url:
return ""
existing_html = "\n".join(part for part in [article.lead, article.body_html] if part)
if "article-cover" in existing_html or article.video.thumbnail_url in existing_html:
return ""
figure_class = "" if rss_safe else ' class="article-cover"'
return (
f''
f"{escape(article.title)}"
)
def sanitize_rss_html(html: str) -> str:
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)
return html
def media_player_xml(article: Article) -> str:
if not article.video or not article.video.source_url:
return ""
embed_url = vk_video_embed_url(article.video.source_url)
if not embed_url:
return ""
return f''
def render_article_html(article: Article, include_video_embed: bool = True, rss_safe: bool = False) -> str:
premium_html = premium_cta_rss_html() if rss_safe else premium_cta_html()
cultural_html = cultural_initiatives_rss_html() if rss_safe else cultural_initiatives_html()
body_html = sanitize_rss_html(article.body_html) if rss_safe else article.body_html
parts = [article.lead, cover_html(article, rss_safe=rss_safe), body_html]
if is_po_suti_dela_article(article):
parts.append(cultural_html)
parts.append(premium_html)
if include_video_embed:
parts.append(video_embed_html(article, rss_safe=rss_safe))
html = "\n".join(part for part in parts if part).strip()
html = strip_leading_duplicate_title(html, article.title)
return sanitize_rss_html(html) if rss_safe else html
def build_feed_xml(session: Session, settings: Settings) -> str:
now = datetime.now(timezone.utc)
articles = session.scalars(
select(Article)
.where(Article.status == "published", Article.published_at.is_not(None), Article.published_at <= now)
.order_by(Article.published_at.desc(), Article.id.desc())
.limit(settings.rss_limit)
).all()
items: list[str] = []
for article in articles:
link = article_url(settings, article)
content = f"
{escape(article.title)}
\n{render_article_html(article, rss_safe=True)}"
frames = []
if article.video_id:
frames = session.scalars(
select(Frame)
.where(Frame.video_id == article.video_id, Frame.selected == True) # noqa: E712
.order_by(Frame.score.desc(), Frame.id.asc())
.limit(1)
).all()
enclosure = ""
cover_url = article.video.thumbnail_url if article.video and article.video.thumbnail_url else ""
if cover_url:
enclosure = f''
elif frames and frames[0].public_url:
enclosure = f''
media_player = media_player_xml(article)
items.append(
f"""
{escape(article.title)}
{escape(link)}
{escape(article.rss_guid or link)}{rfc822(article.published_at)}format-articleindexcomment-allnonadult
{media_player}
{enclosure}
""".strip()
)
return f"""
Николай Стариков
{escape(settings.public_base_url)}
Автоматические материалы канала Николая Старикова для Дзенаru{rfc822(datetime.now(timezone.utc))}
{chr(10).join(items)}
"""