exts/propagation: Fix ?solarweather no image bug

Back to the ugly hack of downloading the image and uploading it to discord.

Fixes #461
This commit is contained in:
0x5c 2023-01-13 03:37:26 -05:00 committed by classabbyamp
parent a4c8a056ac
commit 3110961a3a
2 changed files with 23 additions and 13 deletions

View File

@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Long-deprecated aliases for `?solarweather`. - Long-deprecated aliases for `?solarweather`.
### Fixed ### Fixed
- Issue where ?hamstudy would not work in direct messages (#442). - 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 ## [2.8.0] - 2022-06-24

View File

@ -7,11 +7,11 @@ SPDX-License-Identifier: LiLiQ-Rplus-1.1
""" """
from datetime import datetime
from io import BytesIO from io import BytesIO
import aiohttp
import cairosvg import cairosvg
from datetime import datetime import httpx
import discord import discord
import discord.ext.commands as commands import discord.ext.commands as commands
@ -27,15 +27,17 @@ class PropagationCog(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = 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) @commands.command(name="mufmap", aliases=["muf"], category=cmn.Cats.WEATHER)
async def mufmap(self, ctx: commands.Context): async def mufmap(self, ctx: commands.Context):
"""Shows a world map of the Maximum Usable Frequency (MUF).""" """Shows a world map of the Maximum Usable Frequency (MUF)."""
async with ctx.typing(): async with ctx.typing():
async with self.session.get(self.muf_url, headers={"Connection": "Upgrade", "Upgrade": "http/1.1"}) as r: resp = await self.httpx_client.get(self.muf_url)
svg = await r.read() await resp.aclose()
out = BytesIO(cairosvg.svg2png(bytestring=svg)) if resp.status_code != 200:
raise cmn.BotHTTPError(resp)
out = BytesIO(cairosvg.svg2png(bytestring=await resp.aread()))
file = discord.File(out, "muf_map.png") file = discord.File(out, "muf_map.png")
embed = cmn.embed_factory(ctx) embed = cmn.embed_factory(ctx)
embed.title = "Maximum Usable Frequency Map" embed.title = "Maximum Usable Frequency Map"
@ -47,9 +49,11 @@ class PropagationCog(commands.Cog):
async def fof2map(self, ctx: commands.Context): async def fof2map(self, ctx: commands.Context):
"""Shows a world map of the Critical Frequency (foF2).""" """Shows a world map of the Critical Frequency (foF2)."""
async with ctx.typing(): async with ctx.typing():
async with self.session.get(self.fof2_url, headers={"Connection": "Upgrade", "Upgrade": "http/1.1"}) as r: resp = await self.httpx_client.get(self.fof2_url)
svg = await r.read() await resp.aclose()
out = BytesIO(cairosvg.svg2png(bytestring=svg)) if resp.status_code != 200:
raise cmn.BotHTTPError(resp)
out = BytesIO(cairosvg.svg2png(bytestring=await resp.aread()))
file = discord.File(out, "fof2_map.png") file = discord.File(out, "fof2_map.png")
embed = cmn.embed_factory(ctx) embed = cmn.embed_factory(ctx)
embed.title = "Critical Frequency (foF2) Map" embed.title = "Critical Frequency (foF2) Map"
@ -67,15 +71,20 @@ class PropagationCog(commands.Cog):
embed.set_image(url=self.gl_baseurl + date_params) embed.set_image(url=self.gl_baseurl + date_params)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.command(name="solarweather", aliases=["solar"], @commands.command(name="solarweather", aliases=["solar"], category=cmn.Cats.WEATHER)
category=cmn.Cats.WEATHER)
async def solarweather(self, ctx: commands.Context): async def solarweather(self, ctx: commands.Context):
"""Gets a solar weather report.""" """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 = cmn.embed_factory(ctx)
embed.title = "☀️ Current Solar Weather" embed.title = "☀️ Current Solar Weather"
embed.colour = cmn.colours.good embed.colour = cmn.colours.good
embed.set_image(url=self.n0nbh_sun_url) embed.set_image(url="attachment://solarweather.png")
await ctx.send(embed=embed) await ctx.send(file=file, embed=embed)
def setup(bot: commands.Bot): def setup(bot: commands.Bot):