nsfw-api/api/functions.py

21 lines
614 B
Python
Raw Permalink Normal View History

2024-07-19 20:10:58 +00:00
from random import randint
import aiohttp
import aiofiles
MAX_IMAGE_SIZE = 5 * 1000000
async def download_image(url):
file_name = f"{randint(6969, 6999)}.jpg"
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status == 200:
if int(resp.headers['Content-Length']) > MAX_IMAGE_SIZE:
return False
f = await aiofiles.open(file_name, mode='wb')
await f.write(await resp.read())
await f.close()
else:
return False
return file_name