Skip to content

Commit

Permalink
exts/propagation: Fix ?solarweather no image bug
Browse files Browse the repository at this point in the history
Back to the ugly hack of downloading the image and uploading it to discord.

Fixes #461
  • Loading branch information
0x5c authored and classabbyamp committed Jan 13, 2023
1 parent a4c8a05 commit 3110961
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Long-deprecated aliases for `?solarweather`.
### Fixed
- Issue where ?hamstudy would not work in direct messages (#442).
- Issue where `?solarweather` would not show a picture (#461).


## [2.8.0] - 2022-06-24
Expand Down
35 changes: 22 additions & 13 deletions exts/propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"""


from datetime import datetime
from io import BytesIO

import aiohttp
import cairosvg
from datetime import datetime
import httpx

import discord
import discord.ext.commands as commands
Expand All @@ -27,15 +27,17 @@ class PropagationCog(commands.Cog):

def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
self.httpx_client: httpx.AsyncClient = bot.qrm.httpx_client

@commands.command(name="mufmap", aliases=["muf"], category=cmn.Cats.WEATHER)
async def mufmap(self, ctx: commands.Context):
"""Shows a world map of the Maximum Usable Frequency (MUF)."""
async with ctx.typing():
async with self.session.get(self.muf_url, headers={"Connection": "Upgrade", "Upgrade": "http/1.1"}) as r:
svg = await r.read()
out = BytesIO(cairosvg.svg2png(bytestring=svg))
resp = await self.httpx_client.get(self.muf_url)
await resp.aclose()
if resp.status_code != 200:
raise cmn.BotHTTPError(resp)
out = BytesIO(cairosvg.svg2png(bytestring=await resp.aread()))
file = discord.File(out, "muf_map.png")
embed = cmn.embed_factory(ctx)
embed.title = "Maximum Usable Frequency Map"
Expand All @@ -47,9 +49,11 @@ async def mufmap(self, ctx: commands.Context):
async def fof2map(self, ctx: commands.Context):
"""Shows a world map of the Critical Frequency (foF2)."""
async with ctx.typing():
async with self.session.get(self.fof2_url, headers={"Connection": "Upgrade", "Upgrade": "http/1.1"}) as r:
svg = await r.read()
out = BytesIO(cairosvg.svg2png(bytestring=svg))
resp = await self.httpx_client.get(self.fof2_url)
await resp.aclose()
if resp.status_code != 200:
raise cmn.BotHTTPError(resp)
out = BytesIO(cairosvg.svg2png(bytestring=await resp.aread()))
file = discord.File(out, "fof2_map.png")
embed = cmn.embed_factory(ctx)
embed.title = "Critical Frequency (foF2) Map"
Expand All @@ -67,15 +71,20 @@ async def grayline(self, ctx: commands.Context):
embed.set_image(url=self.gl_baseurl + date_params)
await ctx.send(embed=embed)

@commands.command(name="solarweather", aliases=["solar"],
category=cmn.Cats.WEATHER)
@commands.command(name="solarweather", aliases=["solar"], category=cmn.Cats.WEATHER)
async def solarweather(self, ctx: commands.Context):
"""Gets a solar weather report."""
resp = await self.httpx_client.get(self.n0nbh_sun_url)
await resp.aclose()
if resp.status_code != 200:
raise cmn.BotHTTPError(resp)
img = BytesIO(await resp.aread())
file = discord.File(img, "solarweather.png")
embed = cmn.embed_factory(ctx)
embed.title = "☀️ Current Solar Weather"
embed.colour = cmn.colours.good
embed.set_image(url=self.n0nbh_sun_url)
await ctx.send(embed=embed)
embed.set_image(url="attachment://solarweather.png")
await ctx.send(file=file, embed=embed)


def setup(bot: commands.Bot):
Expand Down

0 comments on commit 3110961

Please sign in to comment.