From 4f0b7d7ef4b42d8b4dd038cab198525cb796a8e8 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sun, 13 Jun 2021 13:29:32 +0200 Subject: [PATCH] rewrite resolve() to use async instead on non-async, should fix #8 --- jeeves.py | 3 ++- jeevesbot/functions.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/jeeves.py b/jeeves.py index 96deb78..5bc0935 100755 --- a/jeeves.py +++ b/jeeves.py @@ -6,6 +6,7 @@ import logging from jeevesbot import bothelp, functions, env import gspread from oauth2client.service_account import ServiceAccountCredentials +import asyncio # setup logging logging.basicConfig(level=logging.INFO) @@ -40,7 +41,7 @@ async def on_message(message): channel = functions.checkchannel(message.channel.id) embed_url = message.content follow_url = embed_url + '.gif' - full_url = functions.resolve(follow_url) + full_url = asyncio.run(functions.resolve(follow_url)) gif_url = full_url.split('?')[0] embed = e.set_image(url=gif_url) if channel is True: diff --git a/jeevesbot/functions.py b/jeevesbot/functions.py index 531444f..668c670 100644 --- a/jeevesbot/functions.py +++ b/jeevesbot/functions.py @@ -1,12 +1,20 @@ #!/usr/bin/env python 3 from jeevesbot import env -import urllib +# import urllib +import aiohttp import dice # This function is necessary for tenor, because they do not allow linking directly to the gif and need resolving. -def resolve(url): - return urllib.request.urlopen(url).url +#def resolve(url): +# return urllib.request.urlopen(url).url + +# This function is necessary for tenor, because they do not allow linking directly to the gif and need resolving. ASYNC VERSION +async def resolve(url): + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + message = response.url + return message # use the dice module for rolling. def roll(notation): @@ -25,4 +33,4 @@ def checkchannel(channelid): if channelid in env.GIFCHANNELS: return True else: - return False + return False \ No newline at end of file