From 5e01cc6d8fd13bb731504a2f5670c1b9cc24421b Mon Sep 17 00:00:00 2001 From: deadzilla Date: Wed, 22 Jul 2026 22:07:17 +0500 Subject: [PATCH] =?UTF-8?q?refactor:=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=B8=20=D0=B4=D1=83=D0=B1=D0=BB=D0=B8=D1=80=D1=83=D1=8E?= =?UTF-8?q?=D1=89=D1=83=D1=8E=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20RSS?= =?UTF-8?q?=20=D0=B2=20=5Fformat=5Ffeed=5Fsection()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Блоки обработки articles и posts в !nw были почти идентичны. Вынесено в _format_feed_section() с параметризацией лейблов, сообщений об ошибках и заголовков RSS-источников. Файлы: commands/news.py --- ISSUES.md | 2 +- commands/news.py | 70 ++++++++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/ISSUES.md b/ISSUES.md index 3ea6ad4..a337401 100644 --- a/ISSUES.md +++ b/ISSUES.md @@ -81,7 +81,7 @@ - [ ] **Отсутствует `pyproject.toml`** — проект использует `requirements.txt` + `requirements-dev.txt` без единого файла конфигурации. Рекомендуется `pyproject.toml` с `[project]`, настройками ruff и pytest -- [ ] **`commands/news.py` — дублирование кода для статей и постов** — блоки обработки `articles` и `posts` почти идентичны (проверка `None`, форматирование, fallback). Вынести в вспомогательную функцию +- [x] ~~**`commands/news.py` — дублирование кода для статей и постов**~~ — вынесено в `_format_feed_section()` (`commands/news.py`) - [x] ~~**Город захардкожен в `API_URL_WEATHER`**~~ — вынесен в `WEATHER_CITY` env-переменную, шаблон `"Погода: {city}:"` (`utils/pogoda.py`, `.env.example`) diff --git a/commands/news.py b/commands/news.py index eaaf5ab..9fe5ace 100644 --- a/commands/news.py +++ b/commands/news.py @@ -1,5 +1,7 @@ import asyncio import logging +from typing import Optional + from discord.ext import commands from utils.news import ( fetch_rss, @@ -12,6 +14,29 @@ from utils.news import ( logger = logging.getLogger(__name__) +def _format_feed_section( + data: Optional[list[dict]], + author: str, + label_warning: str, + label_info: str, + msg_error: str, + msg_empty: str, + title: str, + link: str, +) -> str: + """Форматировать один RSS-раздел (статьи или посты).""" + if data is None: + logger.warning( + "%s: !nw — не удалось получить %s (API вернул None)", author, label_warning + ) + return msg_error + elif data: + return "\n".join(format_articles(data, title, link)) + else: + logger.info("%s: !nw — %s нет в RSS", author, label_info) + return msg_empty + + class News(commands.Cog): """Команда !news — свежие статьи и новости по AI с Habr""" @@ -23,41 +48,28 @@ class News(commands.Cog): fetch_rss(RSS_URL_POSTS), ) - parts: list[str] = [] - - # --- Статьи --- - if articles is None: - logger.warning( - "%s: !nw — не удалось получить статьи (API вернул None)", ctx.author - ) - parts.append("Не удалось получить статьи.") - elif articles: - articles_text = format_articles( + parts: list[str] = [ + _format_feed_section( articles, + str(ctx.author), + "статьи", + "статей", + "Не удалось получить статьи.", + "Статей пока нет.", "Лучшие статьи за сутки / Искусственный интеллект / Хаб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("Статей пока нет.") - - # --- Посты --- - if posts is None: - logger.warning( - "%s: !nw — не удалось получить посты (API вернул None)", ctx.author - ) - parts.append("\nНе удалось получить новости.") - elif posts: - posts_text = format_articles( + ), + _format_feed_section( posts, + str(ctx.author), + "посты", + "постов", + "Не удалось получить новости.", + "Новостей пока нет.", "Лучшие новости за сутки / Искусственный интеллект / Хаб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)