From e57559877206f38ccc9af1b6c3ac780cfc1d57cc Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Wed, 15 Oct 2025 21:55:44 -0500 Subject: [PATCH] Weather plugin added --- cron.db | Bin 0 -> 16384 bytes funguy.py | 2 +- plugins/loadplugin.py | 1 + plugins/weather.py | 167 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 cron.db create mode 100644 plugins/weather.py diff --git a/cron.db b/cron.db new file mode 100644 index 0000000000000000000000000000000000000000..3cb349f22a76f963b46a51292243e336c5ecbed7 GIT binary patch literal 16384 zcmeI(OG?8)7{KvKTM;a6bWyr=>c$6Bitb!lC4$(6)u44%Vv=ew4+$Z}rQW~`^i1Bw zD>%`*vIMfLQ=KpK7iLwM2m%NofB*srAb\nExample: !weather London or !weather New York,US" + ) + logging.info("Sent usage message for !weather") + return + + location = ' '.join(args) + + try: + # Make API request to OpenWeatherMap + params = { + 'q': location, + 'appid': WEATHER_API_KEY, + 'units': 'metric' # Use metric units (Celsius) + } + + response = requests.get(WEATHER_API_URL, params=params, timeout=10) + response.raise_for_status() + + weather_data = response.json() + + # Extract relevant weather information + city_name = weather_data['name'] + country = weather_data['sys']['country'] + temp = weather_data['main']['temp'] + feels_like = weather_data['main']['feels_like'] + humidity = weather_data['main']['humidity'] + description = weather_data['weather'][0]['description'].capitalize() + wind_speed = weather_data['wind'].get('speed', 0) + + # Convert temperature to Fahrenheit for display + temp_f = (temp * 9/5) + 32 + feels_like_f = (feels_like * 9/5) + 32 + + # Get weather emoji based on condition + weather_emoji = get_weather_emoji(weather_data['weather'][0]['main']) + + # Format the weather message + weather_message = f""" +{weather_emoji} Weather for {city_name}, {country}
+Condition: {description}
+Temperature: {temp:.1f}°C ({temp_f:.1f}°F)
+Feels like: {feels_like:.1f}°C ({feels_like_f:.1f}°F)
+Humidity: {humidity}%
+Wind Speed: {wind_speed} m/s +""".strip() + + await bot.api.send_markdown_message(room.room_id, weather_message) + logging.info(f"Sent weather information for {city_name}") + + except requests.exceptions.HTTPError as e: + if e.response.status_code == 404: + await bot.api.send_text_message( + room.room_id, + f"Location '{location}' not found. Please check the spelling and try again." + ) + elif e.response.status_code == 401: + await bot.api.send_text_message( + room.room_id, + "Weather API authentication failed. Please check the API key configuration." + ) + else: + await bot.api.send_text_message( + room.room_id, + f"Error fetching weather data: HTTP {e.response.status_code}" + ) + logging.error(f"HTTP error fetching weather for '{location}': {e}") + + except requests.exceptions.RequestException as e: + await bot.api.send_text_message( + room.room_id, + f"Error connecting to weather service: {e}" + ) + logging.error(f"Request error fetching weather for '{location}': {e}") + + except (KeyError, ValueError) as e: + await bot.api.send_text_message( + room.room_id, + "Error parsing weather data. Please try again later." + ) + logging.error(f"Error parsing weather data for '{location}': {e}") + + +def get_weather_emoji(condition): + """ + Get an emoji based on weather condition. + + Args: + condition (str): Weather condition from API. + + Returns: + str: Weather emoji. + """ + weather_emojis = { + 'Clear': '☀️', + 'Clouds': '☁️', + 'Rain': '🌧️', + 'Drizzle': '🌦️', + 'Thunderstorm': '⛈️', + 'Snow': '❄️', + 'Mist': '🌫️', + 'Fog': '🌫️', + 'Haze': '🌫️', + 'Smoke': '🌫️', + 'Dust': '🌫️', + 'Sand': '🌫️', + 'Ash': '🌫️', + 'Squall': '💨', + 'Tornado': '🌪️' + } + + return weather_emojis.get(condition, '🌡️')