From 96d251fe47514f776171680ff598be948bfd09be Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Sat, 5 Oct 2019 19:59:19 -0400 Subject: [PATCH] convert urllib code to aiohttp --- cogs/studycog.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cogs/studycog.py b/cogs/studycog.py index 6516efb..530b3c9 100644 --- a/cogs/studycog.py +++ b/cogs/studycog.py @@ -12,7 +12,7 @@ import discord.ext.commands as commands import random import json -import urllib +import aiohttp class StudyCog(commands.Cog): def __init__(self, bot): @@ -51,8 +51,11 @@ class StudyCog(commands.Cog): '(Note that only the US question pools are available).') return - with urllib.request.urlopen(f'https://hamstudy.org/pools/{selected_pool}') as url: - pool = json.loads(url.read().decode())['pool'] + async with aiohttp.ClientSession() as session: + async with session.get(f'https://hamstudy.org/pools/{selected_pool}') as resp: + if resp.status != 200: + return await ctx.send('Could not load questions...') + pool = json.loads(await resp.read())['pool'] # Select a question pool_section = random.choice(pool)['sections']