Skip to content

Commit 3110961

Browse files
0x5cclassabbyamp
authored andcommitted
exts/propagation: Fix ?solarweather no image bug
Back to the ugly hack of downloading the image and uploading it to discord. Fixes #461
1 parent a4c8a05 commit 3110961

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1111
- Long-deprecated aliases for `?solarweather`.
1212
### Fixed
1313
- Issue where ?hamstudy would not work in direct messages (#442).
14+
- Issue where `?solarweather` would not show a picture (#461).
1415

1516

1617
## [2.8.0] - 2022-06-24

exts/propagation.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
"""
88

99

10+
from datetime import datetime
1011
from io import BytesIO
1112

12-
import aiohttp
1313
import cairosvg
14-
from datetime import datetime
14+
import httpx
1515

1616
import discord
1717
import discord.ext.commands as commands
@@ -27,15 +27,17 @@ class PropagationCog(commands.Cog):
2727

2828
def __init__(self, bot):
2929
self.bot = bot
30-
self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
30+
self.httpx_client: httpx.AsyncClient = bot.qrm.httpx_client
3131

3232
@commands.command(name="mufmap", aliases=["muf"], category=cmn.Cats.WEATHER)
3333
async def mufmap(self, ctx: commands.Context):
3434
"""Shows a world map of the Maximum Usable Frequency (MUF)."""
3535
async with ctx.typing():
36-
async with self.session.get(self.muf_url, headers={"Connection": "Upgrade", "Upgrade": "http/1.1"}) as r:
37-
svg = await r.read()
38-
out = BytesIO(cairosvg.svg2png(bytestring=svg))
36+
resp = await self.httpx_client.get(self.muf_url)
37+
await resp.aclose()
38+
if resp.status_code != 200:
39+
raise cmn.BotHTTPError(resp)
40+
out = BytesIO(cairosvg.svg2png(bytestring=await resp.aread()))
3941
file = discord.File(out, "muf_map.png")
4042
embed = cmn.embed_factory(ctx)
4143
embed.title = "Maximum Usable Frequency Map"
@@ -47,9 +49,11 @@ async def mufmap(self, ctx: commands.Context):
4749
async def fof2map(self, ctx: commands.Context):
4850
"""Shows a world map of the Critical Frequency (foF2)."""
4951
async with ctx.typing():
50-
async with self.session.get(self.fof2_url, headers={"Connection": "Upgrade", "Upgrade": "http/1.1"}) as r:
51-
svg = await r.read()
52-
out = BytesIO(cairosvg.svg2png(bytestring=svg))
52+
resp = await self.httpx_client.get(self.fof2_url)
53+
await resp.aclose()
54+
if resp.status_code != 200:
55+
raise cmn.BotHTTPError(resp)
56+
out = BytesIO(cairosvg.svg2png(bytestring=await resp.aread()))
5357
file = discord.File(out, "fof2_map.png")
5458
embed = cmn.embed_factory(ctx)
5559
embed.title = "Critical Frequency (foF2) Map"
@@ -67,15 +71,20 @@ async def grayline(self, ctx: commands.Context):
6771
embed.set_image(url=self.gl_baseurl + date_params)
6872
await ctx.send(embed=embed)
6973

70-
@commands.command(name="solarweather", aliases=["solar"],
71-
category=cmn.Cats.WEATHER)
74+
@commands.command(name="solarweather", aliases=["solar"], category=cmn.Cats.WEATHER)
7275
async def solarweather(self, ctx: commands.Context):
7376
"""Gets a solar weather report."""
77+
resp = await self.httpx_client.get(self.n0nbh_sun_url)
78+
await resp.aclose()
79+
if resp.status_code != 200:
80+
raise cmn.BotHTTPError(resp)
81+
img = BytesIO(await resp.aread())
82+
file = discord.File(img, "solarweather.png")
7483
embed = cmn.embed_factory(ctx)
7584
embed.title = "☀️ Current Solar Weather"
7685
embed.colour = cmn.colours.good
77-
embed.set_image(url=self.n0nbh_sun_url)
78-
await ctx.send(embed=embed)
86+
embed.set_image(url="attachment://solarweather.png")
87+
await ctx.send(file=file, embed=embed)
7988

8089

8190
def setup(bot: commands.Bot):

0 commit comments

Comments
 (0)