75 lines
2.9 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 logging
import discord
from discord.ext import commands
from utils.news import (
fetch_rss,
format_articles,
RSS_URL_ARTICLES,
RSS_URL_POSTS,
truncate_embed_field,
)
logger = logging.getLogger(__name__)
class News(commands.Cog):
"""Команда !news — свежие статьи и новости по AI с Habr"""
@commands.command(name="nw")
async def nw(self, ctx):
"""Топ-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)
embed = discord.Embed(
title="Новости AI с Habr",
colour=discord.Color.orange(),
)
embed.add_field(
name="Статьи",
value=truncate_embed_field("\n".join(articles_text)),
inline=False,
)
if posts is None:
logger.warning("%s: !nw — не удалось получить посты (API вернул None)", ctx.author)
embed.add_field(
name="Новости",
value="Не удалось получить новости.",
inline=False,
)
elif posts:
posts_text = format_articles(posts,
"Лучшие новости за сутки / Искусственный интеллект / Хабr",
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/")
embed.add_field(
name="Новости",
value=truncate_embed_field("\n".join(posts_text)),
inline=False,
)
else:
logger.info("%s: !nw — постов нет в RSS", ctx.author)
embed.add_field(
name="Новости",
value="Новостей пока нет.",
inline=False,
)
await ctx.send(embed=embed)
logger.info("%s: !nw выполнена (статей: %d, постов: %d)", ctx.author, len(articles), len(posts) if posts else 0)