1
0
Fork 0
mirror of synced 2024-05-20 20:42:27 +12:00

Used the request method to download the image; handled the downloading failing

This commit is contained in:
Phxntxm 2017-01-23 16:35:00 -06:00
parent f65edc18ce
commit 556c22b9ad

View file

@ -55,11 +55,13 @@ def find_command(bot, command):
async def download_image(url):
"""Returns a file-like object based on the URL provided"""
headers = {'User-Agent': config.user_agent}
# Simply download the image
with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as r:
# Then wrap it in a BytesIO object, to be used like an actual file
image = io.BytesIO(await r.read())
# Simply read the image, to get the bytes
bts = await request(url, attr='read')
if bts is None:
return None
# Then wrap it in a BytesIO object, to be used like an actual file
image = io.BytesIO(await r.read())
return image
async def request(url, *, headers=None, payload=None, method='GET', attr='json'):