add a !weather command to the bot

This commit is contained in:
Brian S. Stephan 2019-10-06 10:15:06 -05:00
parent 5660af1614
commit 998153c511
1 changed files with 54 additions and 0 deletions

54
hitomi/weather.py Normal file
View File

@ -0,0 +1,54 @@
"""Commands for the weather report."""
import logging
from urllib.parse import quote
from discord.ext import commands
from hitomi import bot
from hitomi.backends import dr_botzo, DrBotzoError
logger = logging.getLogger(__name__)
class Weather(commands.Cog):
"""Commands for getting weather reports and whatnot."""
@commands.command()
async def weather(self, ctx, *, query):
"""Try to look up the weather for the provided query."""
await ctx.trigger_typing()
logger.info("query weather for: %s", query)
try:
response = dr_botzo.get(f'/weather/rpc/{quote(query)}/')
logger.debug("result of weather query: HTTP %s, %s", response.status_code, response.text)
weather = response.json()
output = (f"Weather in {weather['location']}: **{weather['current']['description']}**. "
f"**{weather['current']['temp_F']}/{weather['current']['temp_C']}**, "
f"feels like **{weather['current']['feels_like_temp_F']}/"
f"{weather['current']['feels_like_temp_C']}**. "
f"Wind **{weather['current']['wind_speed']}** "
f"from the **{weather['current']['wind_direction']}**. "
f"Visibility **{weather['current']['visibility']}**. "
f"Precipitation **{weather['current']['precipitation']}**. "
f"Pressure **{weather['current']['pressure']}**. "
f"Cloud cover **{weather['current']['cloud_cover']}**. "
f"UV index **{weather['current']['uv_index']}**. "
f"Today: {weather['today_forecast']['noteworthy']}, "
f"High {weather['today_forecast']['high_F']}/{weather['today_forecast']['high_C']}, "
f"Low {weather['today_forecast']['low_F']}/{weather['today_forecast']['low_C']}. "
f"Tomorrow: {weather['tomorrow_forecast']['noteworthy']}, "
f"High {weather['tomorrow_forecast']['high_F']}/{weather['tomorrow_forecast']['high_C']}, "
f"Low {weather['tomorrow_forecast']['low_F']}/{weather['tomorrow_forecast']['low_C']}. "
f"Day after tomorrow: {weather['day_after_tomorrow_forecast']['noteworthy']}, "
f"High {weather['day_after_tomorrow_forecast']['high_F']}/"
f"{weather['day_after_tomorrow_forecast']['high_C']}, "
f"Low {weather['day_after_tomorrow_forecast']['low_F']}/"
f"{weather['day_after_tomorrow_forecast']['low_C']}.")
await ctx.send(output)
except DrBotzoError as drex:
logger.exception("received an error from dr.botzo attempting to weather query %s: %s", query, drex)
await ctx.send(f"*{drex.detail}*")
bot.add_cog(Weather(bot))