From f5ceb8927f2e7f2a9e29d62c8e4cef876f917249 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Sat, 6 Feb 2021 00:32:26 +0800 Subject: =?UTF-8?q?=F0=9F=8F=97=20=F0=9F=92=A9=20=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=95=A5b=20BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATRI/utils/__init__.py | 0 ATRI/utils/apscheduler.py | 34 ++++++++++++++++++++++++++++++++++ ATRI/utils/img.py | 23 +++++++++++++++++++++++ ATRI/utils/list.py | 14 ++++++++++++++ ATRI/utils/request.py | 26 ++++++++++++++++++++++++++ ATRI/utils/time.py | 10 ++++++++++ ATRI/utils/yaml.py | 10 ++++++++++ 7 files changed, 117 insertions(+) create mode 100644 ATRI/utils/__init__.py create mode 100644 ATRI/utils/apscheduler.py create mode 100644 ATRI/utils/img.py create mode 100644 ATRI/utils/list.py create mode 100644 ATRI/utils/request.py create mode 100644 ATRI/utils/time.py create mode 100644 ATRI/utils/yaml.py (limited to 'ATRI/utils') diff --git a/ATRI/utils/__init__.py b/ATRI/utils/__init__.py new file mode 100644 index 0000000..e69de29 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 -- cgit v1.2.3