refactor: use aiohttp for requests

This commit is contained in:
Ryan 2025-01-22 14:49:29 -05:00
parent 640e750e3d
commit 8ef4c85bd8
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 15 additions and 10 deletions

View File

@ -1,7 +1,7 @@
import re import re
import aiohttp
import disnake import disnake
import requests
import arguments import arguments
import commands import commands
@ -31,8 +31,9 @@ async def lookup(message):
return return
if args.application: if args.application:
response = requests.get( session = aiohttp.ClientSession()
f"https://discord.com/api/v9/applications/{args.id}/rpc" response = await (
await session.get(f"https://discord.com/api/v9/applications/{args.id}/rpc")
).json() ).json()
if "code" in response.keys(): if "code" in response.keys():
await utils.reply(message, "application not found!") await utils.reply(message, "application not found!")

View File

@ -115,7 +115,9 @@ async def fast_forward(message):
seconds = args.seconds seconds = args.seconds
if args.sponsorblock: 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: if not video:
await utils.reply( await utils.reply(
message, "no sponsorblock segments were found for this video!" message, "no sponsorblock segments were found for this video!"

View File

@ -1,21 +1,23 @@
import hashlib import hashlib
import requests import aiohttp
from state import sponsorblock_cache from state import sponsorblock_cache
def get_segments(videoId: str): async def get_segments(videoId: str):
if videoId in sponsorblock_cache: if videoId in sponsorblock_cache:
return sponsorblock_cache[videoId] return sponsorblock_cache[videoId]
hashPrefix = hashlib.sha256(videoId.encode()).hexdigest()[:4] 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"]' f'https://sponsor.ajay.app/api/skipSegments/{hashPrefix}?categories=["music_offtopic"]'
) )
print(response.text) if response.status == 200 and (
if response.status_code == 200 and ( results := list(
results := list(filter(lambda v: videoId == v["videoID"], response.json())) filter(lambda v: videoId == v["videoID"], await response.json())
)
): ):
sponsorblock_cache[videoId] = results[0] sponsorblock_cache[videoId] = results[0]
return results[0] return results[0]