From f61fa9397b21c52470497f301c9faa3d598d2ba0 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Wed, 2 Feb 2022 20:28:57 +0800 Subject: [PATCH] Initial commit --- pagermaid/__init__.py | 5 +++++ pagermaid/utils.py | 19 ++++++++++++++++++- requirements.txt | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index 70689f3..d6c009e 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -2,6 +2,7 @@ from coloredlogs import ColoredFormatter from logging import getLogger, StreamHandler, ERROR, INFO, basicConfig from datetime import datetime from os import getcwd +from apscheduler.schedulers.asyncio import AsyncIOScheduler from pagermaid.config import Config import pyromod.listen @@ -11,6 +12,10 @@ CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() help_messages = {} +scheduler = AsyncIOScheduler() +if not scheduler.running: + scheduler.configure(timezone="Asia/ShangHai") + scheduler.start() logs = getLogger(__name__) logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s" logging_handler = StreamHandler() diff --git a/pagermaid/utils.py b/pagermaid/utils.py index 3fd1ef0..65c8c72 100644 --- a/pagermaid/utils.py +++ b/pagermaid/utils.py @@ -1,6 +1,11 @@ +import subprocess +from importlib.util import find_spec +from os.path import exists +from typing import Optional + import httpx from os import remove -from sys import platform +from sys import executable from asyncio import create_subprocess_shell from asyncio.subprocess import PIPE @@ -96,6 +101,18 @@ async def execute(command, pass_error=True): return result +def pip_install(package: str, version: Optional[str] = "", alias: Optional[str] = "") -> bool: + """ Auto install extra pypi packages """ + if not alias: + # when import name is not provided, use package name + alias = package + if find_spec(alias) is None: + subprocess.call([executable, "-m", "pip", "install", f"{package}{version}"]) + if find_spec(package) is None: + return False + return True + + """ Init httpx client """ # 使用自定义 UA headers = { diff --git a/requirements.txt b/requirements.txt index 8df0ef7..5a721a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ PyYAML>=6.0 coloredlogs>=15.0.1 psutil>=5.8.0 httpx +apscheduler>=3.8.1