diff --git a/commands/news.py b/commands/news.py index de64aeb..eaaf5ab 100644 --- a/commands/news.py +++ b/commands/news.py @@ -1,3 +1,4 @@ +import asyncio import logging from discord.ext import commands from utils.news import ( @@ -17,29 +18,31 @@ class News(commands.Cog): @commands.command(name="nw") async def nw(self, ctx: commands.Context) -> None: """Топ-5 свежих статей и новостей по AI с Habr""" - articles = await fetch_rss(RSS_URL_ARTICLES) + 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 ) - await ctx.send("Не удалось получить новости. Попробуйте позже.") - return - - if not articles: + 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) - await ctx.send("Новостей пока нет.") - return - - articles_text = format_articles( - articles, - "Лучшие статьи за сутки / Искусственный интеллект / Хабr", - "https://habr.com/ru/hubs/artificial_intelligence/articles/top/daily/", - ) - - posts = await fetch_rss(RSS_URL_POSTS) - - parts = ["\n".join(articles_text)] + parts.append("Статей пока нет.") + # --- Посты --- if posts is None: logger.warning( "%s: !nw — не удалось получить посты (API вернул None)", ctx.author @@ -61,6 +64,6 @@ class News(commands.Cog): logger.info( "%s: !nw выполнена (статей: %d, постов: %d)", ctx.author, - len(articles), + len(articles) if articles else 0, len(posts) if posts else 0, )