From f1d4605af727635878d6443cc073c653609163d9 Mon Sep 17 00:00:00 2001 From: Night-stars-1 <99261160+Night-stars-1@users.noreply.github.com> Date: Sat, 25 Nov 2023 16:22:20 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BD=BF=E7=94=A8=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=89=BE=E5=88=B0=E9=94=AE=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E5=8D=87=E6=B3=9B=E7=94=A8=E6=80=A7=20(#211)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复无法构建docker镜像的问题 * Update Dockerfile * chore: 更新日志收集方法 * chore: 遵守代码规范 * chore: update issue templates * chore: 删除测试代码 * chore: 更改日志输出样式 * chore: docker生成配置时随机生成自动运行时间 * chore: trailing-whitespace * chore: 日志添加换行 * chore: 未配置验证码解决方案时直接跳过 * chore: 使用遍历方法找到键值,提升泛用性 * chore: 使用cron执行自动任务 * chore: snake_case * chore: 修改运行时间 * Update config.py --- Dockerfile | 12 +++++------ docker_start.sh | 6 ++++++ miuitask.py | 20 +----------------- pdm.lock | 49 +----------------------------------------- pyproject.toml | 1 - requirements.txt | 1 - utils/captcha.py | 55 +++++++++++++++++++++++++++++------------------- utils/config.py | 18 +--------------- 8 files changed, 48 insertions(+), 114 deletions(-) create mode 100644 docker_start.sh diff --git a/Dockerfile b/Dockerfile index e3aa39c..60ccc30 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,12 +8,12 @@ WORKDIR /srv COPY ./utils ./utils -COPY ./pyproject.toml ./pdm.lock ./miuitask.py ./ +COPY ./pyproject.toml ./pdm.lock ./miuitask.py ./docker_start.sh ./ -RUN pdm install --prod +RUN pdm install --prod && \ + echo "0 4 * * * cd /srv && pdm run /srv/miuitask.py" > /var/spool/cron/crontabs/root && \ + chmod +x docker_start.sh -VOLUME ["./data", "/srv/data"] +VOLUME ["/srv/data", "/srv/logs"] -VOLUME ["./logs", "/srv/logs"] - -CMD ["pdm", "run", "python", "miuitask.py"] +CMD ["/srv/docker_start.sh"] diff --git a/docker_start.sh b/docker_start.sh new file mode 100644 index 0000000..2e3a85b --- /dev/null +++ b/docker_start.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# 执行其他必要的启动命令 +nohup pdm run python miuitask.py + +# 以前台模式运行 crond,使得容器不会立即退出 +exec /usr/sbin/crond -f diff --git a/miuitask.py b/miuitask.py index 5884108..377040a 100644 --- a/miuitask.py +++ b/miuitask.py @@ -8,8 +8,6 @@ LastEditTime: 2023-11-24 16:23:43 import asyncio -from apscheduler.schedulers.asyncio import AsyncIOScheduler - from utils.api.login import Login from utils.api.sign import BaseSign from utils.config import ConfigManager @@ -47,20 +45,4 @@ async def main(): if __name__ == "__main__": - HOUR = _conf.preference.hour - MINUTE = _conf.preference.minute - if HOUR and MINUTE: - log.info(f"脚本将在每日{HOUR}:{MINUTE}自动运行") - # 创建一个新的事件循环 - loop = asyncio.get_event_loop() - scheduler = AsyncIOScheduler() - scheduler.add_job(main, 'cron', hour=HOUR, minute=MINUTE, id='miuitask') - scheduler.start() - try: - loop.run_forever() - except (KeyboardInterrupt, SystemExit): - pass - finally: - scheduler.shutdown() - else: - asyncio.run(main()) + asyncio.run(main()) diff --git a/pdm.lock b/pdm.lock index 844192f..15b30e1 100644 --- a/pdm.lock +++ b/pdm.lock @@ -17,17 +17,6 @@ dependencies = [ "sniffio>=1.1", ] -[[package]] -name = "apscheduler" -version = "3.10.4" -requires_python = ">=3.6" -summary = "In-process task scheduler with Cron-like capabilities" -dependencies = [ - "pytz", - "six>=1.4.0", - "tzlocal!=3.*,>=2.0", -] - [[package]] name = "certifi" version = "2023.11.17" @@ -157,11 +146,6 @@ dependencies = [ "typing-extensions!=4.7.0,>=4.6.0", ] -[[package]] -name = "pytz" -version = "2023.3.post1" -summary = "World timezone definitions, modern and historical" - [[package]] name = "pyyaml" version = "6.0.1" @@ -180,12 +164,6 @@ dependencies = [ "urllib3<3,>=1.21.1", ] -[[package]] -name = "six" -version = "1.16.0" -requires_python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" -summary = "Python 2 and 3 compatibility utilities" - [[package]] name = "sniffio" version = "1.3.0" @@ -210,15 +188,6 @@ version = "2023.3" requires_python = ">=2" summary = "Provider of IANA time zone data" -[[package]] -name = "tzlocal" -version = "5.2" -requires_python = ">=3.8" -summary = "tzinfo object for the local timezone" -dependencies = [ - "tzdata; platform_system == \"Windows\"", -] - [[package]] name = "urllib3" version = "2.1.0" @@ -235,7 +204,7 @@ summary = "A small Python utility to set file creation time on Windows" lock_version = "4.2" cross_platform = true groups = ["default"] -content_hash = "sha256:6b73f9b40974a066fb92d2c3f6ca419dc30dabc84539bf8fc5e487b077d0b78f" +content_hash = "sha256:566a9a72c24c47971354bfa0ce10ce921cde51561fd737c3fd433c69621edd5e" [metadata.files] "annotated-types 0.6.0" = [ @@ -246,10 +215,6 @@ content_hash = "sha256:6b73f9b40974a066fb92d2c3f6ca419dc30dabc84539bf8fc5e487b07 {url = "https://files.pythonhosted.org/packages/36/55/ad4de788d84a630656ece71059665e01ca793c04294c463fd84132f40fe6/anyio-4.0.0-py3-none-any.whl", hash = "sha256:cfdb2b588b9fc25ede96d8db56ed50848b0b649dca3dd1df0b11f683bb9e0b5f"}, {url = "https://files.pythonhosted.org/packages/74/17/5075225ee1abbb93cd7fc30a2d343c6a3f5f71cf388f14768a7a38256581/anyio-4.0.0.tar.gz", hash = "sha256:f7ed51751b2c2add651e5747c891b47e26d2a21be5d32d9311dfe9692f3e5d7a"}, ] -"apscheduler 3.10.4" = [ - {url = "https://files.pythonhosted.org/packages/13/b5/7af0cb920a476dccd612fbc9a21a3745fb29b1fcd74636078db8f7ba294c/APScheduler-3.10.4-py3-none-any.whl", hash = "sha256:fb91e8a768632a4756a585f79ec834e0e27aad5860bac7eaa523d9ccefd87661"}, - {url = "https://files.pythonhosted.org/packages/5e/34/5dcb368cf89f93132d9a31bd3747962a9dc874480e54333b0c09fa7d56ac/APScheduler-3.10.4.tar.gz", hash = "sha256:e6df071b27d9be898e486bc7940a7be50b4af2e9da7c08f0744a96d4bd4cef4a"}, -] "certifi 2023.11.17" = [ {url = "https://files.pythonhosted.org/packages/64/62/428ef076be88fa93716b576e4a01f919d25968913e817077a386fcbe4f42/certifi-2023.11.17-py3-none-any.whl", hash = "sha256:e036ab49d5b79556f99cfc2d9320b34cfbe5be05c5871b51de9329f0603b0474"}, {url = "https://files.pythonhosted.org/packages/d4/91/c89518dd4fe1f3a4e3f6ab7ff23cb00ef2e8c9adf99dacc618ad5e068e28/certifi-2023.11.17.tar.gz", hash = "sha256:9b469f3a900bf28dc19b8cfbf8019bf47f7fdd1a65a1d4ffb98fc14166beb4d1"}, @@ -653,10 +618,6 @@ content_hash = "sha256:6b73f9b40974a066fb92d2c3f6ca419dc30dabc84539bf8fc5e487b07 {url = "https://files.pythonhosted.org/packages/fd/81/7bcde32ca3434ec80861205d5f949fe28247af9712dd7da940f08445c45d/pydantic_core-2.14.3-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:27828f0227b54804aac6fb077b6bb48e640b5435fdd7fbf0c274093a7b78b69c"}, {url = "https://files.pythonhosted.org/packages/ff/1b/937923cfa9df349c632ddead46e57c996103f9bfe904f5ff6cc87472df6e/pydantic_core-2.14.3-cp39-none-win32.whl", hash = "sha256:caa94726791e316f0f63049ee00dff3b34a629b0d099f3b594770f7d0d8f1f56"}, ] -"pytz 2023.3.post1" = [ - {url = "https://files.pythonhosted.org/packages/32/4d/aaf7eff5deb402fd9a24a1449a8119f00d74ae9c2efa79f8ef9994261fc2/pytz-2023.3.post1-py2.py3-none-any.whl", hash = "sha256:ce42d816b81b68506614c11e8937d3aa9e41007ceb50bfdcb0749b921bf646c7"}, - {url = "https://files.pythonhosted.org/packages/69/4f/7bf883f12ad496ecc9514cd9e267b29a68b3e9629661a2bbc24f80eff168/pytz-2023.3.post1.tar.gz", hash = "sha256:7b4fddbeb94a1eba4b557da24f19fdf9db575192544270a9101d8509f9f43d7b"}, -] "pyyaml 6.0.1" = [ {url = "https://files.pythonhosted.org/packages/02/74/b2320ebe006b6a521cf929c78f12a220b9db319b38165023623ed195654b/PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"}, {url = "https://files.pythonhosted.org/packages/03/5c/c4671451b2f1d76ebe352c0945d4cd13500adb5d05f5a51ee296d80152f7/PyYAML-6.0.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:e7d73685e87afe9f3b36c799222440d6cf362062f78be1013661b00c5c6f678b"}, @@ -713,10 +674,6 @@ content_hash = "sha256:6b73f9b40974a066fb92d2c3f6ca419dc30dabc84539bf8fc5e487b07 {url = "https://files.pythonhosted.org/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"}, {url = "https://files.pythonhosted.org/packages/9d/be/10918a2eac4ae9f02f6cfe6414b7a155ccd8f7f9d4380d62fd5b955065c3/requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"}, ] -"six 1.16.0" = [ - {url = "https://files.pythonhosted.org/packages/71/39/171f1c67cd00715f190ba0b100d606d440a28c93c7714febeca8b79af85e/six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, - {url = "https://files.pythonhosted.org/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"}, -] "sniffio 1.3.0" = [ {url = "https://files.pythonhosted.org/packages/c3/a0/5dba8ed157b0136607c7f2151db695885606968d1fae123dc3391e0cfdbf/sniffio-1.3.0-py3-none-any.whl", hash = "sha256:eecefdce1e5bbfb7ad2eeaabf7c1eeb404d7757c379bd1f7e5cce9d8bf425384"}, {url = "https://files.pythonhosted.org/packages/cd/50/d49c388cae4ec10e8109b1b833fd265511840706808576df3ada99ecb0ac/sniffio-1.3.0.tar.gz", hash = "sha256:e60305c5e5d314f5389259b7f22aaa33d8f7dee49763119234af3755c55b9101"}, @@ -733,10 +690,6 @@ content_hash = "sha256:6b73f9b40974a066fb92d2c3f6ca419dc30dabc84539bf8fc5e487b07 {url = "https://files.pythonhosted.org/packages/70/e5/81f99b9fced59624562ab62a33df639a11b26c582be78864b339dafa420d/tzdata-2023.3.tar.gz", hash = "sha256:11ef1e08e54acb0d4f95bdb1be05da659673de4acbd21bf9c69e94cc5e907a3a"}, {url = "https://files.pythonhosted.org/packages/d5/fb/a79efcab32b8a1f1ddca7f35109a50e4a80d42ac1c9187ab46522b2407d7/tzdata-2023.3-py2.py3-none-any.whl", hash = "sha256:7e65763eef3120314099b6939b5546db7adce1e7d6f2e179e3df563c70511eda"}, ] -"tzlocal 5.2" = [ - {url = "https://files.pythonhosted.org/packages/04/d3/c19d65ae67636fe63953b20c2e4a8ced4497ea232c43ff8d01db16de8dc0/tzlocal-5.2.tar.gz", hash = "sha256:8d399205578f1a9342816409cc1e46a93ebd5755e39ea2d85334bea911bf0e6e"}, - {url = "https://files.pythonhosted.org/packages/97/3f/c4c51c55ff8487f2e6d0e618dba917e3c3ee2caae6cf0fbb59c9b1876f2e/tzlocal-5.2-py3-none-any.whl", hash = "sha256:49816ef2fe65ea8ac19d19aa7a1ae0551c834303d5014c6d5a62e4cbda8047b8"}, -] "urllib3 2.1.0" = [ {url = "https://files.pythonhosted.org/packages/36/dd/a6b232f449e1bc71802a5b7950dc3675d32c6dbc2a1bd6d71f065551adb6/urllib3-2.1.0.tar.gz", hash = "sha256:df7aa8afb0148fa78488e7899b2c59b5f4ffcfa82e6c54ccb9dd37c1d7b52d54"}, {url = "https://files.pythonhosted.org/packages/96/94/c31f58c7a7f470d5665935262ebd7455c7e4c7782eb525658d3dbf4b9403/urllib3-2.1.0-py3-none-any.whl", hash = "sha256:55901e917a5896a349ff771be919f8bd99aff50b79fe58fec595eb37bbc56bb3"}, diff --git a/pyproject.toml b/pyproject.toml index 1d4e5db..dc01467 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,6 @@ dependencies = [ "cryptography>=41.0.5", "pyyaml>=6.0.1", "tenacity>=8.2.3", - "apscheduler>=3.10.4", "tzdata>=2023.3", ] requires-python = ">=3.11" diff --git a/requirements.txt b/requirements.txt index 62d8bf2..822c70f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -apscheduler==3.10.4 cryptography==41.0.5 httpx==0.25.1 loguru==0.7.2 diff --git a/utils/captcha.py b/utils/captcha.py index b747101..9411a08 100644 --- a/utils/captcha.py +++ b/utils/captcha.py @@ -1,8 +1,8 @@ -""" +''' Date: 2023-11-13 19:55:22 LastEditors: Night-stars-1 nujj1042633805@gmail.com -LastEditTime: 2023-11-18 14:30:41 -""" +LastEditTime: 2023-11-25 15:59:40 +''' from .request import post from .logger import log @@ -11,30 +11,41 @@ from .data_model import ApiResultHandler, GeetestResult _conf = ConfigManager.data_obj +def find_key(data: dict, key: str): + """递归查找字典中的key""" + for dkey, dvalue in data.items(): + if dkey == key: + return dvalue + if isinstance(dvalue, dict): + find_key(dvalue, key) + return None async def get_validate(gt: str, challenge: str) -> GeetestResult: # pylint: disable=invalid-name """获取人机验证结果""" try: validate = None - params = _conf.preference.geetest_params.copy() - for key, value in params.items(): - if isinstance(value, str): - params[key] = value.format(gt=gt, challenge=challenge) - data = _conf.preference.geetest_data.copy() - for key, value in data.items(): - if isinstance(value, str): - data[key] = value.format(gt=gt, challenge=challenge) - response = await post( - _conf.preference.geetest_url, - params=params, - json=data, - ) - log.debug(response.text) - geetest_data = response.json() - geetest = ApiResultHandler(geetest_data) - challenge = geetest.data["challenge"] - validate = geetest.data["validate"] - return GeetestResult(challenge=challenge, validate=validate) + if _conf.preference.geetest_url: + params = _conf.preference.geetest_params.copy() + for key, value in params.items(): + if isinstance(value, str): + params[key] = value.format(gt=gt, challenge=challenge) + data = _conf.preference.geetest_data.copy() + for key, value in data.items(): + if isinstance(value, str): + data[key] = value.format(gt=gt, challenge=challenge) + response = await post( + _conf.preference.geetest_url, + params=params, + json=data, + ) + log.debug(response.text) + geetest_data = response.json() + geetest = ApiResultHandler(geetest_data) + challenge = find_key(geetest.data, "challenge") + validate = find_key(geetest.data, "validate") + return GeetestResult(challenge=challenge, validate=validate) + else: + return GeetestResult(challenge="", validate="") except Exception: # pylint: disable=broad-exception-caught log.exception("获取人机验证结果异常") return GeetestResult(challenge="", validate="") diff --git a/utils/config.py b/utils/config.py index b41b344..4b793bc 100644 --- a/utils/config.py +++ b/utils/config.py @@ -4,12 +4,11 @@ import platform from hashlib import md5 from json import JSONDecodeError from pathlib import Path -from random import randint from typing import Dict, List, Optional, Union import yaml from pydantic import BaseModel -from pydantic import ValidationError, ValidationInfo, field_validator +from pydantic import ValidationError, field_validator from .logger import log @@ -109,21 +108,6 @@ class Preference(BaseModel): """极验自定义params参数""" geetest_data: Dict = {} """极验自定义data参数""" - hour: Optional[int] = None - """自动执行的时间""" - minute: Optional[int] = None - """自动执行的时间""" - - @field_validator("hour", "minute") - @classmethod - def _hour(cls, value, info: ValidationInfo): # pylint: disable=no-self-argument - times = { - "hour": randint(0, 24), - "minute": randint(0, 60) - } - if ConfigManager.platform == "docker" and value is None: - return times.get(info.field_name) - return value class Config(BaseModel): """插件数据"""