Добавить команду !morning (погода + новости) и console команду morning
This commit is contained in:
parent
6219ce42d0
commit
7df62e5e0a
@ -1,5 +1,6 @@
|
|||||||
from .pogoda import Pogoda
|
from .pogoda import Pogoda
|
||||||
from .news import News
|
from .news import News
|
||||||
from .cat import Cat
|
from .cat import Cat
|
||||||
|
from .morning import Morning
|
||||||
|
|
||||||
ALL_COMMANDS = [Pogoda, News, Cat]
|
ALL_COMMANDS = [Pogoda, News, Cat, Morning]
|
||||||
|
|||||||
67
commands/morning.py
Normal file
67
commands/morning.py
Normal file
@ -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))
|
||||||
@ -2,10 +2,12 @@ from .stop import stop
|
|||||||
from .news import news
|
from .news import news
|
||||||
from .cat import cat
|
from .cat import cat
|
||||||
from .pogoda import pogoda
|
from .pogoda import pogoda
|
||||||
|
from .morning import morning
|
||||||
|
|
||||||
ALL_CONSOLE_COMMANDS = {
|
ALL_CONSOLE_COMMANDS = {
|
||||||
"stop": stop,
|
"stop": stop,
|
||||||
"news": news,
|
"news": news,
|
||||||
"cat": cat,
|
"cat": cat,
|
||||||
"pogoda": pogoda,
|
"pogoda": pogoda,
|
||||||
|
"morning": morning,
|
||||||
}
|
}
|
||||||
|
|||||||
58
console_commands/morning.py
Normal file
58
console_commands/morning.py
Normal file
@ -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("Не удалось получить новости.")
|
||||||
Loading…
x
Reference in New Issue
Block a user