LittlePaimon/Guess_voice/util.py

44 lines
1.1 KiB
Python
Raw Normal View History

import os
from pathlib import Path
2022-05-20 18:44:18 +08:00
import aiofiles
2022-05-20 18:44:18 +08:00
from utils import aiorequests
def get_path(dirname, filename):
return Path() / 'data' / 'LittlePaimon' / 'guess_voice' / dirname / filename
async def require_file(file=None,
r_mode='rb',
encoding=None,
url=None,
use_cache=True,
w_mode='wb',
timeout=30):
async def read():
async with aiofiles.open(file, r_mode, encoding=encoding) as fp:
return await fp.read()
if not any([file, url]):
raise ValueError('file or url not null')
file = file and Path(file)
if file and file.exists() and use_cache:
return await read()
if not url:
raise ValueError('url not null')
res = await aiorequests.get(url, timeout=timeout)
content = res.read()
if file:
os.makedirs(os.path.dirname(file), exist_ok=True)
async with aiofiles.open(file, w_mode, encoding=encoding) as fp:
await fp.write(content)
return content
return await read()