67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
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 = await fetch_rss(RSS_URL_ARTICLES)
|
||
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
|
||
|
||
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:
|
||
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),
|
||
len(posts) if posts else 0,
|
||
)
|