Compare commits

...

2 Commits

2 changed files with 64 additions and 4 deletions

View File

@ -22,18 +22,24 @@ class DrBotzoError(requests.HTTPError):
class DrBotzoBackend(object):
"""Basic HTTP requests API, wrapped with some authentication and config stuff."""
def get(self, url, **kwargs):
return self.request('GET', url, **kwargs)
def post(self, url, **kwargs):
return self.request('POST', url, **kwargs)
def request(self, method, url, **kwargs):
"""Wrap requests.post with authentication and hostname settings."""
try:
response = requests.post(urljoin(config.DR_BOTZO_BACKEND_HOST, url),
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
response = requests.request(method, urljoin(config.DR_BOTZO_BACKEND_HOST, url),
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
response.raise_for_status()
return response
except requests.ConnectionError as cex:
logger.exception("received a connection error during POST %s", url)
logger.exception("received a connection error during %s %s", method, url)
raise DrBotzoError(cex, detail="A connection error occurred.")
except requests.HTTPError as httpex:
logger.exception("received an HTTP error during POST %s", url)
logger.exception("received an HTTP error during %s %s", method, url)
try:
detail = httpex.response.json()['detail']
raise DrBotzoError(httpex, detail=detail)

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))