From 3f921680cf8b5760862d663b24c67a0da9c95966 Mon Sep 17 00:00:00 2001 From: deadzilla Date: Mon, 25 May 2026 23:58:54 +0500 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=20=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=81=D1=82=D0=B5=D0=B9=20(=D0=B7=D0=B0?= =?UTF-8?q?=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BE=D0=BA/=D0=B4=D0=B0=D1=82?= =?UTF-8?q?=D0=B0+=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B0),=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=B1=D0=BB=D0=BE=D0=BA?= =?UTF-8?q?=20=D0=BD=D0=BE=D0=B2=D0=BE=D1=81=D1=82=D0=B5=D0=B9=20=D1=81=20?= =?UTF-8?q?habr.com/ru/rss/hubs/artificial=5Fintelligence/news/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/news.py | 37 +++++++++++++++++++++++++---------- console_commands/news.py | 42 ++++++++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/commands/news.py b/commands/news.py index 69491a5..296c992 100644 --- a/commands/news.py +++ b/commands/news.py @@ -4,7 +4,8 @@ import requests from xml.etree import ElementTree -RSS_URL = "https://habr.com/ru/rss/hubs/artificial_intelligence/articles/rated10/?fl=ru" +RSS_URL_ARTICLES = "https://habr.com/ru/rss/hubs/artificial_intelligence/articles/top/daily/?fl=ru" +RSS_URL_POSTS = "https://habr.com/ru/rss/hubs/artificial_intelligence/news/top/daily/?fl=ru" class News(commands.Cog): @@ -13,7 +14,7 @@ class News(commands.Cog): @commands.command(name="news") async def news(self, ctx): """Топ-5 свежих статей по AI с Habr""" - articles = self._fetch_rss() + articles = self._fetch_rss(RSS_URL_ARTICLES) if articles is None: await ctx.send("Не удалось получить новости. Попробуйте позже.") return @@ -24,10 +25,10 @@ class News(commands.Cog): await self._format_and_send(ctx, articles) - def _fetch_rss(self): + def _fetch_rss(self, url): """Скачать и распарсить RSS-ленту (RSS 2.0 / Atom).""" try: - response = requests.get(RSS_URL, timeout=10) + response = requests.get(url, timeout=10) response.raise_for_status() root = ElementTree.fromstring(response.content) @@ -81,7 +82,7 @@ class News(commands.Cog): async def _format_and_send(self, ctx, articles): """Сформировать текст и отправить в чат.""" - lines = ["**🤖 AI-новости с Habr**"] + lines = ["**Лучшие статьи за сутки / Искусственный интеллект / Хабr**\n\n"] for i, article in enumerate(articles[:5], 1): from datetime import datetime date_str = "" @@ -92,14 +93,30 @@ class News(commands.Cog): date_str = dt.strftime("%d.%m.%Y") except ValueError: date_str = article["pub_date"][:10].replace("-", ".") - tags_str = ", ".join(article["tags"][:3]) if article["tags"] else "" title = article["title"] if len(title) > 60: title = title[:60] + "..." - lines.append(f"{i}. {title}") - lines.append(f" {article['creator']} | {date_str} | {tags_str} ") - link = article["link"].replace("https://", "") - lines.append(f" {link}") + lines.append(f"{title}\n{date_str} <{article['link']}>") + + # Второй блок: посты + posts = self._fetch_rss(RSS_URL_POSTS) + if posts: + lines.append("") + lines.append("**Лучшие новости за сутки / Искусственный интеллект / Хабr**\n\n") + for i, article in enumerate(posts[:5], 1): + from datetime import datetime + date_str = "" + if article["pub_date"]: + try: + d = article["pub_date"].replace(" GMT", " +0000") + dt = datetime.strptime(d, "%a, %d %b %Y %H:%M:%S %z") + date_str = dt.strftime("%d.%m.%Y") + except ValueError: + date_str = article["pub_date"][:10].replace("-", ".") + title = article["title"] + if len(title) > 60: + title = title[:60] + "..." + lines.append(f"{title}\n{date_str} <{article['link']}>") message = "\n".join(lines).rstrip() await ctx.send(message, allowed_mentions=discord.AllowedMentions.none()) diff --git a/console_commands/news.py b/console_commands/news.py index d31efcc..11e35ff 100644 --- a/console_commands/news.py +++ b/console_commands/news.py @@ -2,12 +2,13 @@ import requests from xml.etree import ElementTree -RSS_URL = "https://habr.com/ru/rss/hubs/artificial_intelligence/articles/rated10/?fl=ru" +RSS_URL_ARTICLES = "https://habr.com/ru/rss/hubs/artificial_intelligence/articles/top/daily/?fl=ru" +RSS_URL_POSTS = "https://habr.com/ru/rss/hubs/artificial_intelligence/news/top/daily/?fl=ru" def news(stop_event, bot): """Вывести топ-5 свежих статей по AI с Habr""" - articles = _fetch_rss() + articles = _fetch_rss(RSS_URL_ARTICLES) if articles is None: print("Не удалось получить новости.") return @@ -17,7 +18,9 @@ def news(stop_event, bot): return from datetime import datetime - print("**AI-новости с Habr**") + print("**Лучшие статьи за сутки / Искусственный интеллект / Хабr**") + print("") + print() for i, article in enumerate(articles[:5], 1): date_str = "" if article["pub_date"]: @@ -27,21 +30,40 @@ def news(stop_event, bot): date_str = dt.strftime("%d.%m.%Y") except ValueError: date_str = article["pub_date"][:10].replace("-", ".") - tags_str = ", ".join(article["tags"][:3]) if article["tags"] else "" - link = article["link"].replace("https://", "") title = article["title"] if len(title) > 60: title = title[:60] + "..." - print(f"{i}. {title}") - print(f" {article['creator']} | {date_str} | {tags_str}") - print(f" {link}") + print(f"{title}\n {date_str} {article['link']}") + print("────────────────────────────────────────") print() + # Второй блок: посты + posts = _fetch_rss(RSS_URL_POSTS) + if posts: + print("**Лучшие новости за сутки / Искусственный интеллект / Хабr**") + print("") + print() + for i, article in enumerate(posts[:5], 1): + date_str = "" + if article["pub_date"]: + try: + d = article["pub_date"].replace(" GMT", " +0000") + dt = datetime.strptime(d, "%a, %d %b %Y %H:%M:%S %z") + date_str = dt.strftime("%d.%m.%Y") + except ValueError: + date_str = article["pub_date"][:10].replace("-", ".") + title = article["title"] + if len(title) > 60: + title = title[:60] + "..." + print(f"{title}\n {date_str} {article['link']}") + print("────────────────────────────────────────") + print() -def _fetch_rss(): + +def _fetch_rss(url): """Скачать и распарсить RSS-ленту (RSS 2.0 / Atom).""" try: - response = requests.get(RSS_URL, timeout=10) + response = requests.get(url, timeout=10) response.raise_for_status() root = ElementTree.fromstring(response.content)