fix: !nw использует asyncio.gather() для параллельной загрузки RSS вместо последовательной
This commit is contained in:
parent
27eaa677fd
commit
bc37362959
@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from utils.news import (
|
from utils.news import (
|
||||||
@ -17,29 +18,31 @@ class News(commands.Cog):
|
|||||||
@commands.command(name="nw")
|
@commands.command(name="nw")
|
||||||
async def nw(self, ctx: commands.Context) -> None:
|
async def nw(self, ctx: commands.Context) -> None:
|
||||||
"""Топ-5 свежих статей и новостей по AI с Habr"""
|
"""Топ-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:
|
if articles is None:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"%s: !nw — не удалось получить статьи (API вернул None)", ctx.author
|
"%s: !nw — не удалось получить статьи (API вернул None)", ctx.author
|
||||||
)
|
)
|
||||||
await ctx.send("Не удалось получить новости. Попробуйте позже.")
|
parts.append("Не удалось получить статьи.")
|
||||||
return
|
elif articles:
|
||||||
|
articles_text = format_articles(
|
||||||
if not 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)
|
logger.info("%s: !nw — статей нет в RSS", ctx.author)
|
||||||
await ctx.send("Новостей пока нет.")
|
parts.append("Статей пока нет.")
|
||||||
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)]
|
|
||||||
|
|
||||||
|
# --- Посты ---
|
||||||
if posts is None:
|
if posts is None:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"%s: !nw — не удалось получить посты (API вернул None)", ctx.author
|
"%s: !nw — не удалось получить посты (API вернул None)", ctx.author
|
||||||
@ -61,6 +64,6 @@ class News(commands.Cog):
|
|||||||
logger.info(
|
logger.info(
|
||||||
"%s: !nw выполнена (статей: %d, постов: %d)",
|
"%s: !nw выполнена (статей: %d, постов: %d)",
|
||||||
ctx.author,
|
ctx.author,
|
||||||
len(articles),
|
len(articles) if articles else 0,
|
||||||
len(posts) if posts else 0,
|
len(posts) if posts else 0,
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user