# Proprietary License Agreement # Copyright (c) 2024-29 CodWiz # Permission is hereby granted to any person obtaining a copy of this software and associated documentation files (the "Software"), to use the Software for personal and non-commercial purposes, subject to the following conditions: # 1. The Software may not be modified, altered, or otherwise changed in any way without the explicit written permission of the author. # 2. Redistribution of the Software, in original or modified form, is strictly prohibited without the explicit written permission of the author. # 3. The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall the author or copyright holder be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the Software or the use or other dealings in the Software. # 4. Any use of the Software must include the above copyright notice and this permission notice in all copies or substantial portions of the Software. # 5. By using the Software, you agree to be bound by the terms and conditions of this license. # For any inquiries or requests for permissions, please contact codwiz@yandex.ru. # --------------------------------------------------------------------------------- # Name: Weather # Description: Advanced weather module with detailed information # Author: @hikka_mods # --------------------------------------------------------------------------------- # meta developer: @hikka_mods # scope: api Weather # scope: api Weather 0.0.1 # --------------------------------------------------------------------------------- import logging from dataclasses import dataclass from datetime import datetime from typing import Dict, List, Union import requests from .. import loader, utils logger = logging.getLogger(__name__) DEFAULT_FORECAST_DAYS = 3 DEFAULT_HOURLY_INDEX = 4 WEATHER_API_URL = "https://wttr.in/{city}?format=j1&lang=en" @dataclass class WeatherCondition: """Represents a weather condition with its emoji.""" condition: str emoji: str @dataclass class WindDirection: """Represents a wind direction with its description.""" direction: str description: str @dataclass class ForecastDay: """Represents a single day's weather forecast.""" date: str emoji: str condition: str temp_min: str temp_max: str wind_speed: str wind_direction: str WEATHER_EMOJI: List[WeatherCondition] = [ WeatherCondition("clear", "☀️"), WeatherCondition("sunny", "☀️"), WeatherCondition( "partly cloudy", "⛅️" ), WeatherCondition("cloudy", "☁️☁️"), WeatherCondition("overcast", "☁️"), WeatherCondition("mist", "😶‍🌫️"), WeatherCondition("fog", "😶‍🌫️"), WeatherCondition("light rain", "🌦"), WeatherCondition("rain", "🌧"), WeatherCondition("heavy rain", ""), WeatherCondition( "thunderstorm", "" ), WeatherCondition("snow", "🌨"), WeatherCondition("heavy snow", "❄️"), WeatherCondition("sleet", "🌨"), WeatherCondition("wind", "💨"), ] WIND_DIRECTIONS: List[WindDirection] = [ WindDirection("N", "⬆️ North"), WindDirection("NE", "↗️ Northeast"), WindDirection("E", "➡️ East"), WindDirection("SE", "↘️ Southeast"), WindDirection("S", "⬇️ South"), WindDirection("SW", "↙️ Southwest"), WindDirection("W", "⬅️ West"), WindDirection("NW", "↖️ Northwest"), ] WIND_DIRECTIONS_RU: List[WindDirection] = [ WindDirection("N", "⬆️ Северный"), WindDirection("NE", "↗️ Северо-восточный"), WindDirection("E", "➡️ Восточный"), WindDirection("SE", "↘️ Юго-восточный"), WindDirection("S", "⬇️ Южный"), WindDirection("SW", "↙️ Юго-западный"), WindDirection("W", "⬅️ Западный"), WindDirection("NW", "↖️ Северо-западный"), ] @loader.tds class Weather(loader.Module): """Advanced weather module with detailed information""" strings = { "name": "Weather", "no_city": "🚫 Please specify a city", "invalid_city": "🚫 City not found", "loading": "🔄 Fetching weather data for {}...", "error": " Error retrieving weather data", "default_city": " Default city set to: {city}", "weather_text": """{emoji} Weather: {location} 📊 Current conditions: ├ 🌡 Temperature: {temp}°C ├– Feels like: {feels_like}°C ├ 💧 Humidity: {humidity}% ├ 💨 Wind: {wind_speed} km/h {wind_direction} ├ 🌪 Pressure: {pressure} mmHg ├ 👁 Visibility: {visibility} km └ ☁️ Cloudiness: {clouds} 🌅 Time: ├ 🌅 Sunrise: {sunrise} ├ 🌇 Sunset: {sunset} └ ⏱ Local time: {local_time} 📅 Forecast for {forecast_days} days: {forecast} ⏰ Updated: {updated}""", "forecast_day": """{date} {emoji} ├ 🌡 Temperature: {temp_min}°C ... {temp_max}°C └ 💨 Wind: {wind_speed} km/h {wind_direction} """, } strings_ru = { "no_city": "🚫 Пожалуйста, укажите город", "invalid_city": "🚫 Город не найден", "loading": "🔄 Получаю метеоданные для {}...", "default_city": " Город по умолчанию установлен: {city}", "error": " Ошибка при получении данных о погоде", "weather_text": """{emoji} Погода: {location} 📊 Текущие условия: ├ 🌡 Температура: {temp}°C ├– Ощущается как: {feels_like}°C ├ 💧 Влажность: {humidity}% ├ 💨 Ветер: {wind_speed} км/ч {wind_direction} ├ 🌪 Давление: {pressure} мм.рт.ст ├ 👁 Видимость: {visibility} км └ ☁️ Облачность: {clouds} 🌅 Время: ├ 🌅 Восход: {sunrise} ├ 🌇 Закат: {sunset} └ ⏱ Местное время: {local_time} 📅 Прогноз на {forecast_days} дня: {forecast} ⏰ Обновлено: {updated}""", "forecast_day": """{date} {emoji} ├ 🌡 Температура: {temp_min}°C ... {temp_max}°C └ 💨 Ветер: {wind_speed} км/ч {wind_direction} """, } def __init__(self): self.config = loader.ModuleConfig( loader.ConfigValue( "default_city", None, lambda: "Default city for weather command", ), loader.ConfigValue( "language", "ru", lambda: "Language for weather output (en/ru)", ), ) def get_weather_emoji(self, condition: str) -> str: """Get emoji for weather conditions""" condition = condition.lower() for item in WEATHER_EMOJI: if item.condition in condition: return item.emoji return "🌡" def get_wind_direction(self, direction: str) -> str: """Get wind direction description""" lang = self.config["language"] directions = WIND_DIRECTIONS_RU if lang == "ru" else WIND_DIRECTIONS for item in directions: if item.direction == direction.upper(): return item.description return direction async def get_weather_data(self, city: str) -> Union[Dict, None]: """Get weather data from wttr.in""" lang = self.config["language"] url = WEATHER_API_URL.format(city=city) if lang == "ru": url = f"https://wttr.in/{city}?format=j1&lang=ru" try: response = await utils.run_sync(requests.get, url) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: logger.error(f"Failed to fetch weather data for {city}: {e}") return None except Exception as e: logger.exception(f"Error fetching weather data: {e}") return None def format_forecast(self, forecast_data: list) -> str: """Format weather forecast for multiple days.""" forecast_text = "" for day in forecast_data: hourly = day["hourly"][DEFAULT_HOURLY_INDEX] forecast_day = ForecastDay( date=day["date"], emoji=self.get_weather_emoji(hourly["weatherDesc"][0]["value"]), condition=hourly["weatherDesc"][0]["value"], temp_min=day["mintempC"], temp_max=day["maxtempC"], wind_speed=hourly["windspeedKmph"], wind_direction=self.get_wind_direction(hourly["winddir16Point"]), ) forecast_text += self.strings("forecast_day").format( date=forecast_day.date, emoji=forecast_day.emoji, condition=forecast_day.condition, temp_min=forecast_day.temp_min, temp_max=forecast_day.temp_max, wind_speed=forecast_day.wind_speed, wind_direction=forecast_day.wind_direction, ) return forecast_text async def process_weather_data(self, weather_data: Dict) -> str: """Process weather data and format the text.""" current = weather_data["current_condition"][0] forecast = weather_data["weather"] location = ( f"{weather_data['nearest_area'][0]['areaName'][0]['value']}, " f"{weather_data['nearest_area'][0]['country'][0]['value']}" ) forecast_text = self.format_forecast(forecast[:DEFAULT_FORECAST_DAYS]) return self.strings("weather_text").format( location=location, emoji=self.get_weather_emoji(current["weatherDesc"][0]["value"]), temp=current["temp_C"], feels_like=current["FeelsLikeC"], humidity=current["humidity"], wind_speed=current["windspeedKmph"], wind_direction=self.get_wind_direction(current["winddir16Point"]), pressure=current["pressure"], visibility=current["visibility"], clouds=current["weatherDesc"][0]["value"], sunrise=forecast[0]["astronomy"][0]["sunrise"], sunset=forecast[0]["astronomy"][0]["sunset"], local_time=current["observation_time"], forecast=forecast_text, forecast_days=DEFAULT_FORECAST_DAYS, updated=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), ) @loader.command( ru_doc="Узнайте погоду для указанного города", en_doc="Get the weather for the specified city", ) async def weather(self, message): city = utils.get_args_raw(message) or self.config["default_city"] if not city: await utils.answer(message, self.strings("no_city")) return await utils.answer(message, self.strings("loading").format(city)) weather_data = await self.get_weather_data(city) if not weather_data: await utils.answer(message, self.strings("error")) return try: weather_text = await self.process_weather_data(weather_data) await utils.answer(message, weather_text) except Exception as e: logger.exception(f"Error processing weather data: {e}") await utils.answer(message, self.strings("error")) @loader.command( ru_doc="Установите город по умолчанию для определения погоды", en_doc="Set the default city for weather", ) async def weatherset(self, message): city = utils.get_args_raw(message) if not city: await utils.answer(message, self.strings("no_city")) return weather_data = await self.get_weather_data(city) if not weather_data: await utils.answer(message, self.strings("invalid_city")) return self.config["default_city"] = city await utils.answer(message, self.strings("default_city").format(city=city))