#!/usr/bin/env python3 """ The weather man. """ import requests import config from module import commands, example URI = "https://api.openweathermap.org/data/2.5/weather" @commands("weather") @example('.weather Hell') def weather(bot, trigger): """ Show the weather at the given location. -m, --metric - uses metric units instead of imperial. """ if len(trigger.args) < 2: return bot.reply("Weather where?") location = trigger.args[1] if location == '-m' or location == '--metric': if len(trigger.args) < 3: return bot.reply("Weather where?") location = trigger.args[2] units = 'metric' else: units = 'imperial' params = { 'q': location, 'appid': config.weather_api_key, 'units': units } res = requests.get(URI, params=params, verify=True) data = res.json() if data['cod'] == 404: return bot.reply("City not found. Try a different query.") city = data.get('name') country = data['sys']['country'] sky = data['weather'][0]['main'] temp = data['main']['temp'] humidity = data['main']['humidity'] wind = data['wind']['speed'] degrees = data['wind']['deg'] msg = "[\x0304Weather\x03] " msg += f"\x0310City\x03: \x0312{city}\x03, \x0312{country}\x03 | " msg += f"\x0310Sky: \x0312{sky}\x03 | " msg += f"\x0310Temp\x03: \x0308{temp}\u00B0" if units == 'imperial': msg += "F\x03 | " else: msg += "C\x03 | " msg += f"\x0310Humidity\x03: \x0308{humidity}%\x03 | " msg += f"\x0310Wind\x03: \x0308{wind}" if units == 'imperial': msg += "mph" else: msg += "m/s" if degrees: msg += f"\x03 (\x0308{degrees}\u00B0\x03)" bot.msg(msg)