diff options
Diffstat (limited to 'ATRI/utils')
-rw-r--r-- | ATRI/utils/__init__.py | 0 | ||||
-rw-r--r-- | ATRI/utils/apscheduler.py | 34 | ||||
-rw-r--r-- | ATRI/utils/img.py | 23 | ||||
-rw-r--r-- | ATRI/utils/list.py | 14 | ||||
-rw-r--r-- | ATRI/utils/request.py | 26 | ||||
-rw-r--r-- | ATRI/utils/time.py | 10 | ||||
-rw-r--r-- | ATRI/utils/yaml.py | 10 |
7 files changed, 117 insertions, 0 deletions
diff --git a/ATRI/utils/__init__.py b/ATRI/utils/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ATRI/utils/__init__.py diff --git a/ATRI/utils/apscheduler.py b/ATRI/utils/apscheduler.py new file mode 100644 index 0000000..2680f19 --- /dev/null +++ b/ATRI/utils/apscheduler.py @@ -0,0 +1,34 @@ +# Fork from: https://github.com/nonebot/plugin-apscheduler + +import logging +from apscheduler.schedulers.asyncio import AsyncIOScheduler + +from nonebot import get_driver, export +from nonebot.log import logger, LoguruHandler + + +apscheduler_autostart: bool = True +apscheduler_config: dict = {"apscheduler.timezone": "Asia/Shanghai"} + + +driver = get_driver() +scheduler = AsyncIOScheduler() +export().scheduler = scheduler + + +async def _start_scheduler(): + if not scheduler.running: + scheduler.configure(apscheduler_config) + scheduler.start() + logger.info("Scheduler Started.") + + +if apscheduler_autostart: + driver.on_startup(_start_scheduler) + +aps_logger = logging.getLogger("apscheduler") +aps_logger.setLevel(logging.DEBUG) +aps_logger.handlers.clear() +aps_logger.addHandler(LoguruHandler()) + +from apscheduler.triggers.date import DateTrigger diff --git a/ATRI/utils/img.py b/ATRI/utils/img.py new file mode 100644 index 0000000..68a44fc --- /dev/null +++ b/ATRI/utils/img.py @@ -0,0 +1,23 @@ +import os +from PIL import ImageFile, Image + +from ATRI.exceptions import WriteError + + +def compress_image(out_file, kb=500, quality=85, k=0.9) -> str: + """将目标图片进行压缩""" + o_size = os.path.getsize(out_file) // 1024 + if o_size <= kb: + return out_file + + ImageFile.LOAD_TRUNCATED_IMAGES = True # type: ignore + while o_size > kb: + img = Image.open(out_file) + x, y = img.size + out = img.resize((int(x * k), int(y * k)), Image.ANTIALIAS) + try: + out.save(out_file, quality=quality) + except WriteError: + raise WriteError('Writing file failed!') + o_size = os.path.getsize(out_file) // 1024 + return out_file diff --git a/ATRI/utils/list.py b/ATRI/utils/list.py new file mode 100644 index 0000000..af6fb76 --- /dev/null +++ b/ATRI/utils/list.py @@ -0,0 +1,14 @@ +def count_list(lst: list, aim) -> int: + """查看指定列表中目标元素所存在的数量""" + count = 0 + for ele in lst: + if ele == aim: + count = count + 1 + return count + + +def del_list_aim(lst: list, aim) -> list: + """删除指定列表中的所有元素""" + while aim in lst: + lst.remove(aim) + return lst diff --git a/ATRI/utils/request.py b/ATRI/utils/request.py new file mode 100644 index 0000000..bf05b56 --- /dev/null +++ b/ATRI/utils/request.py @@ -0,0 +1,26 @@ +from typing import Optional +from aiohttp import ClientSession + + +async def get_text(url: str, headers: Optional[dict] = None) -> str: + """异步以 Get 方式请求 url""" + async with ClientSession() as session: + async with session.get(url, headers=headers) as r: + result = await r.text() + return result + + +async def get_bytes(url: str, headers: Optional[dict] = None) -> bytes: + """异步以 Get 方式请求 url""" + async with ClientSession() as session: + async with session.get(url, headers=headers) as r: + result = await r.read() + return result + + +async def post_bytes(url: str, params: Optional[dict] = None) -> bytes: + """异步以 Post 方式请求 url""" + async with ClientSession() as session: + async with session.post(url, params=params) as r: + result = await r.read() + return result diff --git a/ATRI/utils/time.py b/ATRI/utils/time.py new file mode 100644 index 0000000..5204ea5 --- /dev/null +++ b/ATRI/utils/time.py @@ -0,0 +1,10 @@ +from datetime import datetime + + +def now_time() -> float: + """获取当前小时整数""" + now_ = datetime.now() + hour = now_.hour + minute = now_.minute + now = hour + minute / 60 + return now diff --git a/ATRI/utils/yaml.py b/ATRI/utils/yaml.py new file mode 100644 index 0000000..59e8992 --- /dev/null +++ b/ATRI/utils/yaml.py @@ -0,0 +1,10 @@ +import yaml + +from pathlib import Path + + +def load_yml(file: Path, encoding='utf-8') -> dict: + """加载 yml 文件""" + with open(file, 'r', encoding=encoding) as f: + data = yaml.safe_load(f) + return data |