96 lines
4.3 KiB
Python
96 lines
4.3 KiB
Python
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, RSS_URL_POSTS
|
||
from utils.cat import fetch_cat
|
||
|
||
|
||
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, posts, cat_url = await asyncio.gather(
|
||
fetch_weather(self.api_url),
|
||
fetch_rss(RSS_URL_ARTICLES),
|
||
fetch_rss(RSS_URL_POSTS),
|
||
fetch_cat(),
|
||
)
|
||
|
||
# --- Формируем embed ---
|
||
embed = discord.Embed(title="Доброе утро!", color=0xF4A460)
|
||
|
||
# Котик как thumbnail (маленький в углу)
|
||
if cat_url:
|
||
embed.set_thumbnail(url=cat_url)
|
||
|
||
description_lines = []
|
||
|
||
# --- Погода ---
|
||
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)
|
||
|
||
description_lines.append(
|
||
f"**Погода в Магнитогорске:**\n"
|
||
f"Температура: {temp}\u00b0C (ощущается как {feels_like}\u00b0C)\n"
|
||
f"Описание: {description}\n"
|
||
f"Влажность: {humidity}%\n"
|
||
f"Ветер: {wind} м/с\n"
|
||
f"Давление: {pressure_mm} мм рт. ст."
|
||
)
|
||
else:
|
||
description_lines.append("Не удалось получить данные о погоде.")
|
||
else:
|
||
description_lines.append("Не удалось получить данные о погоде.")
|
||
|
||
description_lines.append("") # пустая строка-разделитель
|
||
|
||
# --- Новости: статьи ---
|
||
if articles is not None:
|
||
if articles:
|
||
lines = format_articles(articles,
|
||
"Лучшие статьи за сутки / Искусственный интеллект / Хабr",
|
||
"https://habr.com/ru/hubs/artificial_intelligence/articles/top/daily/")
|
||
description_lines.append("\n".join(lines))
|
||
else:
|
||
description_lines.append("Новостей пока нет.")
|
||
else:
|
||
description_lines.append("Не удалось получить новости.")
|
||
|
||
description_lines.append("") # пустая строка-разделитель
|
||
|
||
# --- Новости: посты ---
|
||
if posts is not None:
|
||
if posts:
|
||
lines = format_articles(posts,
|
||
"Лучшие новости за сутки / Искусственный интеллект / Хабr",
|
||
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/")
|
||
description_lines.append("\n".join(lines))
|
||
else:
|
||
description_lines.append("Новостей пока нет.")
|
||
else:
|
||
description_lines.append("Не удалось получить новости.")
|
||
|
||
embed.description = "\n".join(description_lines)
|
||
|
||
await ctx.send(embed=embed)
|