70 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
import logging
from discord.ext import commands
from utils.news import (
fetch_rss,
format_articles,
RSS_URL_ARTICLES,
RSS_URL_POSTS,
truncate_message,
)
logger = logging.getLogger(__name__)
class News(commands.Cog):
"""Команда !news — свежие статьи и новости по AI с Habr"""
@commands.command(name="nw")
async def nw(self, ctx: commands.Context) -> None:
"""Топ-5 свежих статей и новостей по AI с Habr"""
articles, posts = await asyncio.gather(
fetch_rss(RSS_URL_ARTICLES),
fetch_rss(RSS_URL_POSTS),
)
parts: list[str] = []
# --- Статьи ---
if articles is None:
logger.warning(
"%s: !nw — не удалось получить статьи (API вернул None)", ctx.author
)
parts.append("Не удалось получить статьи.")
elif articles:
articles_text = format_articles(
articles,
"Лучшие статьи за сутки / Искусственный интеллект / Хабr",
"https://habr.com/ru/hubs/artificial_intelligence/articles/top/daily/",
)
parts.append("\n".join(articles_text))
else:
logger.info("%s: !nw — статей нет в RSS", ctx.author)
parts.append("Статей пока нет.")
# --- Посты ---
if posts is None:
logger.warning(
"%s: !nw — не удалось получить посты (API вернул None)", ctx.author
)
parts.append("\nНе удалось получить новости.")
elif posts:
posts_text = format_articles(
posts,
"Лучшие новости за сутки / Искусственный интеллект / Хабr",
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/",
)
parts.append("\n" + "\n".join(posts_text))
else:
logger.info("%s: !nw — постов нет в RSS", ctx.author)
parts.append("\nНовостей пока нет.")
message = truncate_message("\n".join(parts))
await ctx.send(message)
logger.info(
"%s: !nw выполнена (статей: %d, постов: %d)",
ctx.author,
len(articles) if articles else 0,
len(posts) if posts else 0,
)