From ddade6651ac7cda19d74671a75cecb3a63c8126f Mon Sep 17 00:00:00 2001 From: Cyclic3 Date: Sat, 11 Oct 2025 23:33:25 +0100 Subject: [PATCH 1/2] Migrate from blocking "requests" to async "aiohttp" --- extensions/addons/cogs/otheraddons.py | 10 ++++------ extensions/addons/cogs/searchaddons.py | 20 +++++++++----------- extensions/staffaddons/cogs/staffaddons.py | 10 +++++----- requirements.txt | 1 - 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/extensions/addons/cogs/otheraddons.py b/extensions/addons/cogs/otheraddons.py index 56208de..ffa9713 100644 --- a/extensions/addons/cogs/otheraddons.py +++ b/extensions/addons/cogs/otheraddons.py @@ -1,6 +1,6 @@ import json # to read API json responses -import requests # to read api calls +import aiohttp import discord import discord.app_commands as app_commands import discord.ext.commands as commands @@ -297,11 +297,9 @@ async def convert_unit( "appid": itx.client.api_tokens['Open Exchange Rates'], "show_alternative": "true", } - response_api = requests.get( - "https://openexchangerates.org/api/latest.json", - params=params - ).text - data = json.loads(response_api) + async with aiohttp.ClientSession() as client: + async with client.get("https://openexchangerates.org/api/latest.json", params=params) as response: + data = await response.json() if data.get("error", 0): await itx.response.send_message( "I'm sorry, something went wrong while trying to get " diff --git a/extensions/addons/cogs/searchaddons.py b/extensions/addons/cogs/searchaddons.py index a621140..5ad63eb 100644 --- a/extensions/addons/cogs/searchaddons.py +++ b/extensions/addons/cogs/searchaddons.py @@ -1,6 +1,7 @@ import json # to read API json responses -import requests # to read api calls +import aiohttp # to read api calls +import aiohttp.client_exceptions import discord import discord.app_commands as app_commands @@ -375,11 +376,9 @@ async def equaldex(self, itx: discord.Interaction[Bot], country_id: str): "apiKey": equaldex_key, # "formatted": "true", } - response = requests.get( - "https://www.equaldex.com/api/region", - params=querystring - ) - response_api = response.text + async with aiohttp.ClientSession() as client: + async with client.get("https://www.equaldex.com/api/region", params=querystring) as response: + response_api = await response.text() # returns ->
{"regions":{...}}
<- so you need to # remove the
 and 
parts. It also has some #
\r\n strings in there for some reason...? so uh @@ -505,11 +504,10 @@ async def math(self, itx: discord.Interaction[Bot], query: str): "output": "json", } try: - api_response: WolframResult = requests.get( - "https://api.wolframalpha.com/v2/query", - params=params - ).json() - except requests.exceptions.JSONDecodeError: + async with aiohttp.ClientSession() as client: + async with client.get("https://api.wolframalpha.com/v2/query", params=params) as response: + api_response: WolframResult = await response.json() + except (aiohttp.client_exceptions.ContentTypeError, json.JSONDecodeError): await itx.followup.send( "Your input gave a malformed result! Perhaps it took " "too long to calculate...", diff --git a/extensions/staffaddons/cogs/staffaddons.py b/extensions/staffaddons/cogs/staffaddons.py index 9f30a5f..0b682ac 100644 --- a/extensions/staffaddons/cogs/staffaddons.py +++ b/extensions/staffaddons/cogs/staffaddons.py @@ -1,8 +1,7 @@ from datetime import datetime, timedelta # ^ for /delete_week_selfies (within 7 days), and /version startup # time parsing to discord unix -import requests -# to fetch from GitHub and see Rina is running the latest version +import aiohttp # to fetch from GitHub and see Rina is running the latest version import discord import discord.app_commands as app_commands @@ -171,9 +170,10 @@ async def delete_week_selfies(self, itx: discord.Interaction[Bot]): async def get_bot_version(self, itx: discord.Interaction[Bot]): # get most recently pushed bot version # noinspection LongLine - latest_rina = requests.get( - "https://raw.githubusercontent.com/TransPlace-Devs/uncute-rina/main/main.py" # noqa - ).text + async with aiohttp.ClientSession() as client: + # noqa + async with client.get("https://raw.githubusercontent.com/TransPlace-Devs/uncute-rina/main/main.py") as response: + latest_rina = await response.text() latest_version = (latest_rina .split("BOT_VERSION = \"", 1)[1] .split("\"", 1)[0]) diff --git a/requirements.txt b/requirements.txt index 3af5c74..7e4187d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,5 @@ pymongo>=4.14.0 pandas>=2.3.1 apscheduler>=3.11.0 matplotlib>=3.10.5 -requests>=2.32.3 pytest>=8.4.1 aiohttp>=3.12.15 From 19268ddb8ab7c82bfa36d1819f6cc474b4aea8f9 Mon Sep 17 00:00:00 2001 From: Cyclic3 Date: Sun, 12 Oct 2025 02:45:32 +0100 Subject: [PATCH 2/2] Collapse async with statements --- extensions/addons/cogs/otheraddons.py | 5 ++--- extensions/addons/cogs/searchaddons.py | 10 ++++------ extensions/staffaddons/cogs/staffaddons.py | 6 ++---- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/extensions/addons/cogs/otheraddons.py b/extensions/addons/cogs/otheraddons.py index ffa9713..f4c7340 100644 --- a/extensions/addons/cogs/otheraddons.py +++ b/extensions/addons/cogs/otheraddons.py @@ -297,9 +297,8 @@ async def convert_unit( "appid": itx.client.api_tokens['Open Exchange Rates'], "show_alternative": "true", } - async with aiohttp.ClientSession() as client: - async with client.get("https://openexchangerates.org/api/latest.json", params=params) as response: - data = await response.json() + async with aiohttp.ClientSession() as client, client.get("https://openexchangerates.org/api/latest.json", params=params) as response: + data = await response.json() if data.get("error", 0): await itx.response.send_message( "I'm sorry, something went wrong while trying to get " diff --git a/extensions/addons/cogs/searchaddons.py b/extensions/addons/cogs/searchaddons.py index 5ad63eb..e823df9 100644 --- a/extensions/addons/cogs/searchaddons.py +++ b/extensions/addons/cogs/searchaddons.py @@ -376,9 +376,8 @@ async def equaldex(self, itx: discord.Interaction[Bot], country_id: str): "apiKey": equaldex_key, # "formatted": "true", } - async with aiohttp.ClientSession() as client: - async with client.get("https://www.equaldex.com/api/region", params=querystring) as response: - response_api = await response.text() + async with aiohttp.ClientSession() as client, client.get("https://www.equaldex.com/api/region", params=querystring) as response: + response_api = await response.text() # returns ->
{"regions":{...}}
<- so you need to # remove the
 and 
parts. It also has some #
\r\n strings in there for some reason...? so uh @@ -504,9 +503,8 @@ async def math(self, itx: discord.Interaction[Bot], query: str): "output": "json", } try: - async with aiohttp.ClientSession() as client: - async with client.get("https://api.wolframalpha.com/v2/query", params=params) as response: - api_response: WolframResult = await response.json() + async with aiohttp.ClientSession() as client, client.get("https://api.wolframalpha.com/v2/query", params=params) as response: + api_response: WolframResult = await response.json() except (aiohttp.client_exceptions.ContentTypeError, json.JSONDecodeError): await itx.followup.send( "Your input gave a malformed result! Perhaps it took " diff --git a/extensions/staffaddons/cogs/staffaddons.py b/extensions/staffaddons/cogs/staffaddons.py index 0b682ac..e142663 100644 --- a/extensions/staffaddons/cogs/staffaddons.py +++ b/extensions/staffaddons/cogs/staffaddons.py @@ -170,10 +170,8 @@ async def delete_week_selfies(self, itx: discord.Interaction[Bot]): async def get_bot_version(self, itx: discord.Interaction[Bot]): # get most recently pushed bot version # noinspection LongLine - async with aiohttp.ClientSession() as client: - # noqa - async with client.get("https://raw.githubusercontent.com/TransPlace-Devs/uncute-rina/main/main.py") as response: - latest_rina = await response.text() + async with aiohttp.ClientSession() as client, client.get("https://raw.githubusercontent.com/TransPlace-Devs/uncute-rina/main/main.py") as response: + latest_rina = await response.text() latest_version = (latest_rina .split("BOT_VERSION = \"", 1)[1] .split("\"", 1)[0])