From 9a1d225b9b3f114bbea52fc1d53820d69e8b97cd Mon Sep 17 00:00:00 2001 From: Myned Date: Sat, 14 Oct 2017 22:37:57 -0400 Subject: [PATCH] Fixed r.read() raising ClientConnectionError Context manager was closing the request before methods could be run on it --- src/main/cogs/owner.py | 2 ++ src/main/run.py | 1 + src/main/utils/scraper.py | 4 ++-- src/main/utils/utils.py | 20 +++++++++++++++----- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/cogs/owner.py b/src/main/cogs/owner.py index 453d357..3a65307 100644 --- a/src/main/cogs/owner.py +++ b/src/main/cogs/owner.py @@ -32,6 +32,7 @@ class Bot: # loop = self.bot.loop.all_tasks() # for task in loop: # task.cancel() + u.close() await self.bot.logout() await self.bot.close() print('\n/ / / / / / / / / / / /\nD I S C O N N E C T E D\n\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\\n') @@ -48,6 +49,7 @@ class Bot: # loop = self.bot.loop.all_tasks() # for task in loop: # task.cancel() + u.close() await self.bot.logout() await self.bot.close() os.execl(sys.executable, 'python3', 'run.py') diff --git a/src/main/run.py b/src/main/run.py index 1c0b986..0d9feb7 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -41,6 +41,7 @@ async def on_ready(): @bot.event async def on_error(error): + u.close() await bot.logout() await bot.close() print('\n! ! ! ! !\nE R R O R : {}\n! ! ! ! !\n'.format(error), file=sys.stderr) diff --git a/src/main/utils/scraper.py b/src/main/utils/scraper.py index 4d7aef6..5f0f0f3 100644 --- a/src/main/utils/scraper.py +++ b/src/main/utils/scraper.py @@ -6,10 +6,10 @@ from utils import utils as u async def check_match(url): - r = await u.fetch('http://iqdb.harry.lu/?url={}'.format(url)) + content = await u.fetch('http://iqdb.harry.lu', params={'url': url}) try: - value = BeautifulSoup(await r.read(), 'html.parser').find_all('a')[1].get('href') + value = BeautifulSoup(content, 'html.parser').find_all('a')[1].get('href') except IndexError: raise exc.MatchError diff --git a/src/main/utils/utils.py b/src/main/utils/utils.py index 7fcc9f8..9b6ad51 100644 --- a/src/main/utils/utils.py +++ b/src/main/utils/utils.py @@ -68,10 +68,20 @@ async def clear(obj, interval=10 * 60, replace=None): obj = replace asyncio.sleep(interval) +session = aiohttp.ClientSession() + + +def close(): + global session + + if session: + session.close() + async def fetch(url, *, params={}, json=False): - async with aiohttp.ClientSession() as session: - async with session.get(url, params=params, headers={'user-agent': 'Modumind/0.0.1 (Myned)'}) as r: - if json is True: - return await r.json() - return r + global session + + async with session.get(url, params=params, headers={'user-agent': 'Modumind/0.0.1 (Myned)'}) as r: + if json is True: + return await r.json() + return await r.read()