From cc6ed5c183f628b8b357656217e30ee76f57af45 Mon Sep 17 00:00:00 2001 From: deadzilla Date: Tue, 26 May 2026 19:47:56 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=83=20!morning?= =?UTF-8?q?=20(=D0=BF=D0=BE=D0=B3=D0=BE=D0=B4=D0=B0=20+=20=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE=D1=81=D1=82=D0=B8)=20=D0=B8=20console=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=83=20morning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/__init__.py | 3 +- commands/morning.py | 67 ++++++++++++++++++++++++++++++++++++ console_commands/__init__.py | 2 ++ console_commands/morning.py | 58 +++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 commands/morning.py create mode 100644 console_commands/morning.py diff --git a/commands/__init__.py b/commands/__init__.py index 4f3058e..2aacde6 100644 --- a/commands/__init__.py +++ b/commands/__init__.py @@ -1,5 +1,6 @@ from .pogoda import Pogoda from .news import News from .cat import Cat +from .morning import Morning -ALL_COMMANDS = [Pogoda, News, Cat] +ALL_COMMANDS = [Pogoda, News, Cat, Morning] diff --git a/commands/morning.py b/commands/morning.py new file mode 100644 index 0000000..ee995e3 --- /dev/null +++ b/commands/morning.py @@ -0,0 +1,67 @@ +import asyncio + +import discord +from discord.ext import commands +from utils.pogoda import fetch_weather, pressure_to_mmhg, translate_weather +from utils.news import fetch_rss, format_articles, RSS_URL_ARTICLES + + +class Morning(commands.Cog): + """Команда !morning — погода и новости утром""" + + def __init__(self): + self.api_url = "https://wttr.in/Magnitogorsk?format=j1&lang=ru" + + @commands.command(name="morning") + async def morning(self, ctx): + """Погода и лучшие статьи за сутки""" + # Параллельный запрос погоды и новостей + weather_data, articles = await asyncio.gather( + fetch_weather(self.api_url), + fetch_rss(RSS_URL_ARTICLES), + ) + + parts = ["Доброе утро!\n"] + + # --- Погода --- + if weather_data is not None: + current = weather_data.get("current_condition", [{}])[0] + if current: + temp = current.get("temp_C", "\u2014") + feels_like = current.get("FeelsLikeC", "\u2014") + description = translate_weather(current.get("weatherDesc", [{}])[0].get("value", "\u2014")) + humidity = current.get("humidity", "\u2014") + wind_kmh = current.get("windspeedKmph", "\u2014") + try: + wind = round(int(wind_kmh) / 3.6, 1) if wind_kmh != "\u2014" else "\u2014" + except (ValueError, TypeError): + wind = "\u2014" + pressure_mb = current.get("pressure", "\u2014") + pressure_mm = pressure_to_mmhg(pressure_mb) + + parts.append( + f"**Погода в Магнитогорске:**\n" + f"Температура: {temp}\u00b0C (ощущается как {feels_like}\u00b0C)\n" + f"Описание: {description}\n" + f"Влажность: {humidity}%\n" + f"Ветер: {wind} м/с\n" + f"Давление: {pressure_mm} мм рт. ст.\n" + ) + else: + parts.append("Не удалось получить данные о погоде.\n") + else: + parts.append("Не удалось получить данные о погоде.\n") + + # --- Новости --- + if articles is not None: + if articles: + lines = format_articles(articles, + "Лучшие статьи за сутки / Искусственный интеллект / Хабr", + "https://habr.com/ru/hubs/artificial_intelligence/articles/top/daily/") + parts.append("\n" + "\n".join(lines).rstrip()) + else: + parts.append("\nНовостей пока нет.") + else: + parts.append("\nНе удалось получить новости.") + + await ctx.send("\n".join(parts)) diff --git a/console_commands/__init__.py b/console_commands/__init__.py index 881d014..d4911d2 100644 --- a/console_commands/__init__.py +++ b/console_commands/__init__.py @@ -2,10 +2,12 @@ from .stop import stop from .news import news from .cat import cat from .pogoda import pogoda +from .morning import morning ALL_CONSOLE_COMMANDS = { "stop": stop, "news": news, "cat": cat, "pogoda": pogoda, + "morning": morning, } diff --git a/console_commands/morning.py b/console_commands/morning.py new file mode 100644 index 0000000..db021e4 --- /dev/null +++ b/console_commands/morning.py @@ -0,0 +1,58 @@ +import asyncio + +from utils.pogoda import fetch_weather, pressure_to_mmhg, translate_weather +from utils.news import fetch_rss, format_articles, RSS_URL_ARTICLES + + +async def morning(stop_event, bot): + """Вывести погоду и лучшие статьи за сутки""" + api_url = "https://wttr.in/Magnitogorsk?format=j1&lang=ru" + + # Параллельный запрос погоды и новостей + weather_data, articles = await asyncio.gather( + fetch_weather(api_url), + fetch_rss(RSS_URL_ARTICLES), + ) + + print("Доброе утро!\n") + + # --- Погода --- + if weather_data is not None: + current = weather_data.get("current_condition", [{}])[0] + if current: + temp = current.get("temp_C", "—") + feels_like = current.get("FeelsLikeC", "—") + description = translate_weather(current.get("weatherDesc", [{}])[0].get("value", "—")) + humidity = current.get("humidity", "—") + wind_kmh = current.get("windspeedKmph", "—") + try: + wind = round(int(wind_kmh) / 3.6, 1) if wind_kmh != "—" else "—" + except (ValueError, TypeError): + wind = "—" + pressure_mb = current.get("pressure", "—") + pressure_mm = pressure_to_mmhg(pressure_mb) + + print(f"**Погода в Магнитогорске:**") + print(f"Температура: {temp}°C (ощущается как {feels_like}°C)") + print(f"Описание: {description}") + print(f"Влажность: {humidity}%") + print(f"Ветер: {wind} м/с") + print(f"Давление: {pressure_mm} мм рт. ст.") + else: + print("Не удалось получить данные о погоде.") + else: + print("Не удалось получить данные о погоде.") + + print() + + # --- Новости --- + if articles is not None: + if articles: + lines = format_articles(articles, + "Лучшие статьи за сутки / Искусственный интеллект / Хабr", + "https://habr.com/ru/hubs/artificial_intelligence/articles/top/daily/") + print("\n".join(lines)) + else: + print("Новостей пока нет.") + else: + print("Не удалось получить новости.")