From 3110961a3acb16d97bd709722f36475e4fd19170 Mon Sep 17 00:00:00 2001 From: 0x5c Date: Fri, 13 Jan 2023 03:37:26 -0500 Subject: [PATCH] exts/propagation: Fix ?solarweather no image bug Back to the ugly hack of downloading the image and uploading it to discord. Fixes #461 --- CHANGELOG.md | 1 + exts/propagation.py | 35 ++++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31e1062..2e63200 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/exts/propagation.py b/exts/propagation.py index 232189d..8025b87 100644 --- a/exts/propagation.py +++ b/exts/propagation.py @@ -7,11 +7,11 @@ SPDX-License-Identifier: LiLiQ-Rplus-1.1 """ +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 @@ -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" @@ -47,9 +49,11 @@ class PropagationCog(commands.Cog): 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" @@ -67,15 +71,20 @@ class PropagationCog(commands.Cog): 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):