fix: !nw использует asyncio.gather() для параллельной загрузки RSS вместо последовательной

This commit is contained in:
deadzilla 2026-07-11 23:47:00 +05:00
parent 27eaa677fd
commit bc37362959

View File

@ -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:
logger.info("%s: !nw — статей нет в RSS", ctx.author)
await ctx.send("Новостей пока нет.")
return
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("Статей пока нет.")
posts = await fetch_rss(RSS_URL_POSTS)
parts = ["\n".join(articles_text)]
# --- Посты ---
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,
)