Compare commits
No commits in common. "998153c5119e4add14d8b04e7d5848f930a67cc3" and "cfb7cdf6b27464daf7f371a1fed154827145c8aa" have entirely different histories.
998153c511
...
cfb7cdf6b2
@ -22,24 +22,18 @@ class DrBotzoError(requests.HTTPError):
|
|||||||
class DrBotzoBackend(object):
|
class DrBotzoBackend(object):
|
||||||
"""Basic HTTP requests API, wrapped with some authentication and config stuff."""
|
"""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):
|
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."""
|
"""Wrap requests.post with authentication and hostname settings."""
|
||||||
try:
|
try:
|
||||||
response = requests.request(method, urljoin(config.DR_BOTZO_BACKEND_HOST, url),
|
response = requests.post(urljoin(config.DR_BOTZO_BACKEND_HOST, url),
|
||||||
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
|
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response
|
return response
|
||||||
except requests.ConnectionError as cex:
|
except requests.ConnectionError as cex:
|
||||||
logger.exception("received a connection error during %s %s", method, url)
|
logger.exception("received a connection error during POST %s", url)
|
||||||
raise DrBotzoError(cex, detail="A connection error occurred.")
|
raise DrBotzoError(cex, detail="A connection error occurred.")
|
||||||
except requests.HTTPError as httpex:
|
except requests.HTTPError as httpex:
|
||||||
logger.exception("received an HTTP error during %s %s", method, url)
|
logger.exception("received an HTTP error during POST %s", url)
|
||||||
try:
|
try:
|
||||||
detail = httpex.response.json()['detail']
|
detail = httpex.response.json()['detail']
|
||||||
raise DrBotzoError(httpex, detail=detail)
|
raise DrBotzoError(httpex, detail=detail)
|
||||||
|
@ -1,54 +0,0 @@
|
|||||||
"""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))
|
|
Loading…
x
Reference in New Issue
Block a user