2019-10-28 00:23:56 -04:00
|
|
|
"""
|
2019-12-07 17:26:55 -05:00
|
|
|
Weather extension for qrm
|
2019-10-28 00:23:56 -04:00
|
|
|
---
|
2020-01-06 23:27:48 -05:00
|
|
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2020-02-15 06:27:48 -05:00
|
|
|
This file is part of qrm2 and is released under the terms of
|
2020-01-31 06:50:50 -05:00
|
|
|
the GNU General Public License, version 2.
|
2019-10-28 00:23:56 -04:00
|
|
|
"""
|
|
|
|
|
2020-01-31 06:50:50 -05:00
|
|
|
|
2019-10-28 00:23:56 -04:00
|
|
|
import io
|
|
|
|
import re
|
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
import aiohttp
|
|
|
|
|
2019-10-28 00:23:56 -04:00
|
|
|
import discord
|
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import common as cmn
|
|
|
|
|
2019-10-28 00:23:56 -04:00
|
|
|
|
|
|
|
class WeatherCog(commands.Cog):
|
|
|
|
wttr_units_regex = re.compile(r"\B-([cCfF])\b")
|
|
|
|
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
2020-01-07 05:36:09 -05:00
|
|
|
self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
@commands.command(name="bandconditions", aliases=["cond", "condx", "conditions"], category=cmn.cat.weather)
|
2019-10-28 00:23:56 -04:00
|
|
|
async def _band_conditions(self, ctx: commands.Context):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Gets a solar conditions report."""
|
2020-01-20 03:17:50 -05:00
|
|
|
async with ctx.typing():
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2020-01-30 06:15:42 -05:00
|
|
|
embed.title = "Current Solar Conditions"
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.colour = cmn.colours.good
|
2020-01-30 06:15:42 -05:00
|
|
|
async with self.session.get("http://www.hamqsl.com/solarsun.php") as resp:
|
2019-12-23 17:54:20 -05:00
|
|
|
if resp.status != 200:
|
2020-01-27 00:37:52 -05:00
|
|
|
raise cmn.BotHTTPError(resp)
|
2020-01-20 03:17:50 -05:00
|
|
|
data = io.BytesIO(await resp.read())
|
2020-02-05 07:30:14 -05:00
|
|
|
embed.set_image(url="attachment://condx.png")
|
2020-01-30 06:15:42 -05:00
|
|
|
await ctx.send(embed=embed, file=discord.File(data, "condx.png"))
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2020-03-28 19:07:23 -04:00
|
|
|
@commands.group(name="weather", aliases=["wttr"], case_insensitive=True, category=cmn.cat.weather)
|
2019-10-28 00:23:56 -04:00
|
|
|
async def _weather_conditions(self, ctx: commands.Context):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Gets local weather conditions from [wttr.in](http://wttr.in/).
|
|
|
|
|
|
|
|
*Supported location types:*
|
|
|
|
city name: `paris`
|
|
|
|
any location: `~Eiffel Tower`
|
|
|
|
Unicode name of any location in any language: `Москва`
|
|
|
|
airport code (3 letters): `muc`
|
|
|
|
domain name `@stackoverflow.com`
|
|
|
|
area codes: `12345`
|
|
|
|
GPS coordinates: `-78.46,106.79`
|
|
|
|
|
|
|
|
Add a `-c` or `-f` to use Celcius or Fahrenheit: `-c YSC`"""
|
2019-10-28 00:23:56 -04:00
|
|
|
if ctx.invoked_subcommand is None:
|
|
|
|
await ctx.send_help(ctx.command)
|
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
@_weather_conditions.command(name="forecast", aliases=["fc", "future"], category=cmn.cat.weather)
|
2019-12-06 01:19:42 -05:00
|
|
|
async def _weather_conditions_forecast(self, ctx: commands.Context, *, location: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Gets local weather forecast for the next three days from [wttr.in](http://wttr.in/).
|
|
|
|
See help of the `weather` command for possible location types and options."""
|
2020-01-20 03:17:50 -05:00
|
|
|
async with ctx.typing():
|
2019-10-28 00:23:56 -04:00
|
|
|
try:
|
2019-12-06 01:19:42 -05:00
|
|
|
units_arg = re.search(self.wttr_units_regex, location).group(1)
|
2019-10-28 00:23:56 -04:00
|
|
|
except AttributeError:
|
2020-01-30 06:15:42 -05:00
|
|
|
units_arg = ""
|
|
|
|
if units_arg.lower() == "f":
|
|
|
|
units = "u"
|
|
|
|
elif units_arg.lower() == "c":
|
|
|
|
units = "m"
|
2019-10-28 00:23:56 -04:00
|
|
|
else:
|
2020-01-30 06:15:42 -05:00
|
|
|
units = ""
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
loc = self.wttr_units_regex.sub("", location).strip()
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2020-01-30 06:15:42 -05:00
|
|
|
embed.title = f"Weather Forecast for {loc}"
|
|
|
|
embed.description = "Data from [wttr.in](http://wttr.in/)."
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.colour = cmn.colours.good
|
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
loc = loc.replace(" ", "+")
|
|
|
|
async with self.session.get(f"http://wttr.in/{loc}_{units}pnFQ.png") as resp:
|
2019-12-23 17:54:20 -05:00
|
|
|
if resp.status != 200:
|
2020-01-27 00:37:52 -05:00
|
|
|
raise cmn.BotHTTPError(resp)
|
2020-01-20 03:17:50 -05:00
|
|
|
data = io.BytesIO(await resp.read())
|
2020-02-05 07:30:14 -05:00
|
|
|
embed.set_image(url="attachment://wttr_forecast.png")
|
2020-01-30 06:15:42 -05:00
|
|
|
await ctx.send(embed=embed, file=discord.File(data, "wttr_forecast.png"))
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
@_weather_conditions.command(name="now", aliases=["n"], category=cmn.cat.weather)
|
2019-12-06 01:19:42 -05:00
|
|
|
async def _weather_conditions_now(self, ctx: commands.Context, *, location: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Gets current local weather conditions from [wttr.in](http://wttr.in/).
|
|
|
|
See help of the `weather` command for possible location types and options."""
|
2020-01-20 03:17:50 -05:00
|
|
|
async with ctx.typing():
|
2019-10-28 00:23:56 -04:00
|
|
|
try:
|
2019-12-06 01:19:42 -05:00
|
|
|
units_arg = re.search(self.wttr_units_regex, location).group(1)
|
2019-10-28 00:23:56 -04:00
|
|
|
except AttributeError:
|
2020-01-30 06:15:42 -05:00
|
|
|
units_arg = ""
|
|
|
|
if units_arg.lower() == "f":
|
|
|
|
units = "u"
|
|
|
|
elif units_arg.lower() == "c":
|
|
|
|
units = "m"
|
2019-10-28 00:23:56 -04:00
|
|
|
else:
|
2020-01-30 06:15:42 -05:00
|
|
|
units = ""
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
loc = self.wttr_units_regex.sub("", location).strip()
|
2019-10-28 00:23:56 -04:00
|
|
|
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2020-01-30 06:15:42 -05:00
|
|
|
embed.title = f"Current Weather for {loc}"
|
|
|
|
embed.description = "Data from [wttr.in](http://wttr.in/)."
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.colour = cmn.colours.good
|
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
loc = loc.replace(" ", "+")
|
|
|
|
async with self.session.get(f"http://wttr.in/{loc}_0{units}pnFQ.png") as resp:
|
2019-12-23 17:54:20 -05:00
|
|
|
if resp.status != 200:
|
2020-01-27 00:37:52 -05:00
|
|
|
raise cmn.BotHTTPError(resp)
|
2020-01-20 03:17:50 -05:00
|
|
|
data = io.BytesIO(await resp.read())
|
2020-02-05 07:30:14 -05:00
|
|
|
embed.set_image(url="attachment://wttr_now.png")
|
2020-01-30 06:15:42 -05:00
|
|
|
await ctx.send(embed=embed, file=discord.File(data, "wttr_now.png"))
|
2019-10-28 00:23:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setup(bot: commands.Bot):
|
|
|
|
bot.add_cog(WeatherCog(bot))
|