2018-03-16 03:13:43 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
The weather man.
|
|
|
|
"""
|
|
|
|
import requests
|
|
|
|
|
2019-12-04 18:39:19 -05:00
|
|
|
import config
|
2018-03-16 03:13:43 -04:00
|
|
|
from module import commands, example
|
|
|
|
|
2019-12-04 18:39:19 -05:00
|
|
|
URI = "https://api.openweathermap.org/data/2.5/weather"
|
2018-03-16 03:13:43 -04:00
|
|
|
|
|
|
|
@commands("weather")
|
2019-12-04 18:39:19 -05:00
|
|
|
@example('.weather Hell')
|
2018-03-16 03:13:43 -04:00
|
|
|
def weather(bot, trigger):
|
2019-12-04 18:39:19 -05:00
|
|
|
"""
|
|
|
|
Show the weather at the given location.
|
|
|
|
|
|
|
|
-m, --metric - uses metric units instead of imperial.
|
|
|
|
"""
|
2020-01-07 18:58:19 -05:00
|
|
|
if len(trigger.args) < 2:
|
2018-03-16 03:13:43 -04:00
|
|
|
return bot.reply("Weather where?")
|
2020-01-07 18:58:19 -05:00
|
|
|
location = trigger.args[1]
|
2019-12-04 18:39:19 -05:00
|
|
|
if location == '-m' or location == '--metric':
|
2020-01-07 18:58:19 -05:00
|
|
|
if len(trigger.args) < 3:
|
|
|
|
return bot.reply("Weather where?")
|
|
|
|
location = trigger.args[2]
|
2019-12-04 18:39:19 -05:00
|
|
|
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)
|