From 8ef4c85bd8f145b391231a93cb9a26c32c819e72 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Wed, 22 Jan 2025 14:49:29 -0500 Subject: [PATCH] refactor: use aiohttp for requests --- commands/tools.py | 7 ++++--- commands/voice/playback.py | 4 +++- sponsorblock.py | 14 ++++++++------ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/commands/tools.py b/commands/tools.py index d30205c..89b40fc 100644 --- a/commands/tools.py +++ b/commands/tools.py @@ -1,7 +1,7 @@ import re +import aiohttp import disnake -import requests import arguments import commands @@ -31,8 +31,9 @@ async def lookup(message): return if args.application: - response = requests.get( - f"https://discord.com/api/v9/applications/{args.id}/rpc" + session = aiohttp.ClientSession() + response = await ( + await session.get(f"https://discord.com/api/v9/applications/{args.id}/rpc") ).json() if "code" in response.keys(): await utils.reply(message, "application not found!") diff --git a/commands/voice/playback.py b/commands/voice/playback.py index 55b1a79..8681176 100644 --- a/commands/voice/playback.py +++ b/commands/voice/playback.py @@ -115,7 +115,9 @@ async def fast_forward(message): seconds = args.seconds if args.sponsorblock: - video = sponsorblock.get_segments(players[message.guild.id].current.player.id) + video = await sponsorblock.get_segments( + players[message.guild.id].current.player.id + ) if not video: await utils.reply( message, "no sponsorblock segments were found for this video!" diff --git a/sponsorblock.py b/sponsorblock.py index 91c97b4..c381c36 100644 --- a/sponsorblock.py +++ b/sponsorblock.py @@ -1,21 +1,23 @@ import hashlib -import requests +import aiohttp from state import sponsorblock_cache -def get_segments(videoId: str): +async def get_segments(videoId: str): if videoId in sponsorblock_cache: return sponsorblock_cache[videoId] hashPrefix = hashlib.sha256(videoId.encode()).hexdigest()[:4] - response = requests.get( + session = aiohttp.ClientSession() + response = await session.get( f'https://sponsor.ajay.app/api/skipSegments/{hashPrefix}?categories=["music_offtopic"]' ) - print(response.text) - if response.status_code == 200 and ( - results := list(filter(lambda v: videoId == v["videoID"], response.json())) + if response.status == 200 and ( + results := list( + filter(lambda v: videoId == v["videoID"], await response.json()) + ) ): sponsorblock_cache[videoId] = results[0] return results[0]