2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-22 18:19:16 +00:00
microbot/ubot/cache.py
2023-10-16 09:54:04 -04:00

37 lines
1.1 KiB
Python

# SPDX-License-Identifier: GPL-2.0-or-later
import mimetypes
import os
import aiofiles
class Cache():
if not os.path.exists("ubot/cache"):
os.mkdir("ubot/cache")
def __init__(self, aioclient):
self.aioclient = aioclient
async def cache_file(self, url: str, filename: str):
async with self.aioclient.get(url) as response:
file_extension = mimetypes.guess_extension(response.headers["content-type"])
async with aiofiles.open(f"ubot/cache/{filename}{file_extension}", mode="wb") as cache_file:
while True:
chunk = await response.content.read(4096)
if not chunk:
break
await cache_file.write(chunk)
return f"ubot/cache/{filename}{file_extension}"
def remove_cache(self, filename: str):
os.remove(filename)
async def is_cache_required(self, url: str) -> bool:
async with self.aioclient.get(url) as response:
return int(response.headers["content-length"]) >= 20000000