mirror of
https://github.com/Nick80835/microbot
synced 2025-08-22 10:09:40 +00:00
31 lines
989 B
Python
31 lines
989 B
Python
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 chunk := await response.content.read(4096):
|
|
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) -> tuple[bool, int]:
|
|
async with self.aioclient.get(url) as response:
|
|
size = int(response.headers["content-length"])
|
|
return size >= 20000000, size
|