88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
import discord
|
||
from discord.ext import commands
|
||
import requests
|
||
|
||
|
||
class Pogoda(commands.Cog):
|
||
"""Команда !pogoda — прогноз погоды для Магнитогорска"""
|
||
|
||
def __init__(self):
|
||
self.api_url = "https://api.open-meteo.com/v1/forecast"
|
||
self.lat = 53.41
|
||
self.lon = 59.06
|
||
|
||
@commands.command(name="pogoda")
|
||
async def pogoda(self, ctx):
|
||
params = {
|
||
"latitude": self.lat,
|
||
"longitude": self.lon,
|
||
"current": "temperature_2m,weather_code,relative_humidity_2m,wind_speed_10m,pressure_msl",
|
||
"timezone": "Asia/Yekaterinburg",
|
||
}
|
||
|
||
try:
|
||
response = requests.get(self.api_url, params=params, timeout=10)
|
||
response.raise_for_status()
|
||
data = response.json()
|
||
except requests.RequestException as e:
|
||
await ctx.send(f"Ошибка при получении данных: {e}")
|
||
return
|
||
|
||
current = data.get("current", {})
|
||
if not current:
|
||
await ctx.send("Не удалось получить данные о погоде.")
|
||
return
|
||
|
||
temp = current.get("temperature_2m", "—")
|
||
weather_code = current.get("weather_code", "—")
|
||
humidity = current.get("relative_humidity_2m", "—")
|
||
wind = current.get("wind_speed_10m", "—")
|
||
pressure = current.get("pressure_msl", "—")
|
||
|
||
description = self._get_weather_description(weather_code)
|
||
pressure_mm = self._pressure_to_mmhg(pressure)
|
||
|
||
await ctx.send(
|
||
f"🌡 Температура: {temp}°C\n"
|
||
f"📝 Описание: {description}\n"
|
||
f"💧 Влажность: {humidity}%\n"
|
||
f"💨 Ветер: {wind} км/ч\n"
|
||
f"🌍 Давление: {pressure_mm} мм рт. ст."
|
||
)
|
||
|
||
def _get_weather_description(self, code):
|
||
descriptions = {
|
||
0: "Ясно",
|
||
1: "Преимущественно ясно",
|
||
2: "Переменная облачность",
|
||
3: "Пасмурно",
|
||
45: "Туман",
|
||
48: "Изморозь",
|
||
51: "Лёгкая морось",
|
||
53: "Морось",
|
||
55: "Сильная морось",
|
||
61: "Небольшой дождь",
|
||
63: "Дождь",
|
||
65: "Сильный дождь",
|
||
66: "Ледяной дождь",
|
||
67: "Сильный ледяной дождь",
|
||
71: "Небольшой снег",
|
||
73: "Снег",
|
||
75: "Сильный снег",
|
||
77: "Снежная крупа",
|
||
80: "Небольшой ливень",
|
||
81: "Ливень",
|
||
82: "Сильный ливень",
|
||
85: "Небольшой снегопад",
|
||
86: "Сильный снегопад",
|
||
95: "Гроза",
|
||
96: "Гроза с градом",
|
||
99: "Сильная гроза с градом",
|
||
}
|
||
return descriptions.get(code, "Неизвестно")
|
||
|
||
def _pressure_to_mmhg(self, hpa):
|
||
if hpa == "—":
|
||
return "—"
|
||
return round(hpa * 0.750062, 1)
|