From 57a630ede831fa24be9f471ebfe40a2fafd0a71b Mon Sep 17 00:00:00 2001 From: LeeCQ Date: Sun, 3 Mar 2024 22:46:26 +0800 Subject: [PATCH] =?UTF-8?q?Worker=20=E5=9C=A8=E5=AE=8C=E6=88=90=E6=97=B6?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E5=B9=B3=E5=9D=87=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alist_sync/common.py | 16 ++++++++++++ alist_sync/config.py | 6 +---- alist_sync/d_worker.py | 9 ++++++- alist_sync/notice/{email.py => _email.py} | 0 alist_sync/notice/_webhook.py | 30 +++++++++++++++++++++++ alist_sync/notice/webhook.py | 0 alist_sync/version.py | 2 +- 7 files changed, 56 insertions(+), 7 deletions(-) rename alist_sync/notice/{email.py => _email.py} (100%) create mode 100644 alist_sync/notice/_webhook.py delete mode 100644 alist_sync/notice/webhook.py diff --git a/alist_sync/common.py b/alist_sync/common.py index 31df0b0..5f6b4cb 100644 --- a/alist_sync/common.py +++ b/alist_sync/common.py @@ -1,5 +1,6 @@ import asyncio import builtins +import datetime import hashlib import logging import os @@ -94,6 +95,21 @@ def timeout_input(msg, default, timeout=3): return default +def transfer_speed(size, start: datetime.datetime, end: datetime.datetime) -> str: + """转换速度""" + speed = size * 2 / (end - start).seconds + if speed < 1024: + return f"{speed}B/s" + speed /= 1024 + if speed < 1024: + return f"{speed:.2f}KB/s" + speed /= 1024 + if speed < 1024: + return f"{speed:.2f}MB/s" + speed /= 1024 + return f"{speed:.2f}GB/s" + + if __name__ == "__main__": from pydantic import BaseModel diff --git a/alist_sync/config.py b/alist_sync/config.py index 980d174..baefb09 100644 --- a/alist_sync/config.py +++ b/alist_sync/config.py @@ -249,9 +249,5 @@ def dump_to_mongodb(self): if __name__ == "__main__": - # config = create_config() - # print(config) - # print(config.cache_dir) - # print(config.mongodb) - # print(config.notify) + print(Config.model_json_schema()) diff --git a/alist_sync/d_worker.py b/alist_sync/d_worker.py index 2a8caac..2288e5d 100644 --- a/alist_sync/d_worker.py +++ b/alist_sync/d_worker.py @@ -14,7 +14,7 @@ from alist_sdk.path_lib import AbsAlistPathType, AlistPath from alist_sync.config import create_config -from alist_sync.common import sha1, prefix_in_threads +from alist_sync.common import sha1, prefix_in_threads, transfer_speed from alist_sync.err import WorkerError, RetryError from alist_sync.thread_pool import MyThreadPoolExecutor from alist_sync.version import __version__ @@ -116,6 +116,13 @@ def update(self, **field: Any): logger.info(f"Worker[{self.short_id}] is {self.status}.") self.done_at = datetime.datetime.now() sync_config.handle.create_log(self) + if self.status == "done": + logger.info( + f"Worker[{self.short_id}] " + f"{self.source_path} -> {self.target_path} " + f"平均传输速度: " + f"{transfer_speed(self.file_size, self.done_at, self.created_at)}" + ) return sync_config.handle.delete_worker(self.id) return sync_config.handle.update_worker(self, *field.keys()) diff --git a/alist_sync/notice/email.py b/alist_sync/notice/_email.py similarity index 100% rename from alist_sync/notice/email.py rename to alist_sync/notice/_email.py diff --git a/alist_sync/notice/_webhook.py b/alist_sync/notice/_webhook.py new file mode 100644 index 0000000..5578e61 --- /dev/null +++ b/alist_sync/notice/_webhook.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +""" +@File Name : _webhook.py +@Author : LeeCQ +@Date-Time : 2024/3/3 21:47 + +""" +import os + +from httpx import Client + + +class Webhook: + def __init__(self, url: str, headers: dict[str, str] = None): + self.client = Client(base_url=url, headers=headers) + + def send(self, data: str): + return self.client.post( + "", + json={ + "msg_type": "text", + "content": {"text":data}, + }, + ) + + +if __name__ == "__main__": + webhook = Webhook(os.getenv("WEBHOOK_URL", "")) + print(webhook.send("test message.").text) diff --git a/alist_sync/notice/webhook.py b/alist_sync/notice/webhook.py deleted file mode 100644 index e69de29..0000000 diff --git a/alist_sync/version.py b/alist_sync/version.py index 950b881..8df0420 100644 --- a/alist_sync/version.py +++ b/alist_sync/version.py @@ -3,7 +3,7 @@ """ 0.1.0-a*: - [] 使用Workers架构重构,使用多线程,不再使用协程 + [*] 使用Workers架构重构,使用多线程,不再使用协程 [] New 通知 - email [] New 通知 - webhook