mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
refactor: 新增用户脚本服务器代码
This commit is contained in:
@@ -39,3 +39,4 @@ from .tools import (
|
||||
sleep_time,
|
||||
retry_limited,
|
||||
)
|
||||
from .script import ScriptServer
|
||||
|
||||
@@ -70,6 +70,7 @@ class Manager:
|
||||
folder_mode: bool,
|
||||
author_archive: bool,
|
||||
write_mtime: bool,
|
||||
script_server: bool,
|
||||
_print: bool,
|
||||
cleaner: "Cleaner",
|
||||
):
|
||||
@@ -126,6 +127,7 @@ class Manager:
|
||||
self.live_download = self.check_bool(live_download, True)
|
||||
self.author_archive = self.check_bool(author_archive, False)
|
||||
self.write_mtime = self.check_bool(write_mtime, False)
|
||||
self.script_server = self.check_bool(script_server, False)
|
||||
self.create_folder()
|
||||
|
||||
def __check_path(self, path: str) -> Path:
|
||||
@@ -282,8 +284,12 @@ class Manager:
|
||||
self.folder.mkdir(exist_ok=True)
|
||||
self.temp.mkdir(exist_ok=True)
|
||||
|
||||
def compatible(self,):
|
||||
if self.path == self.root and (
|
||||
old := self.path.parent.joinpath(self.folder.name)
|
||||
).exists() and not self.folder.exists():
|
||||
def compatible(
|
||||
self,
|
||||
):
|
||||
if (
|
||||
self.path == self.root
|
||||
and (old := self.path.parent.joinpath(self.folder.name)).exists()
|
||||
and not self.folder.exists()
|
||||
):
|
||||
move(old, self.folder)
|
||||
|
||||
47
source/module/script.py
Normal file
47
source/module/script.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from contextlib import suppress
|
||||
|
||||
from websockets import ConnectionClosed, serve
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..application import XHS
|
||||
|
||||
|
||||
class ScriptServer:
|
||||
def __init__(
|
||||
self,
|
||||
core: "XHS",
|
||||
host="0.0.0.0",
|
||||
port=5556,
|
||||
):
|
||||
self.core = core
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.server = None
|
||||
|
||||
async def handler(self, websocket):
|
||||
with suppress(ConnectionClosed):
|
||||
async for message in websocket:
|
||||
print(f"收到消息: {message}")
|
||||
await websocket.send("消息已接收")
|
||||
|
||||
async def start(self):
|
||||
"""启动服务器"""
|
||||
self.server = await serve(
|
||||
self.handler,
|
||||
self.host,
|
||||
self.port,
|
||||
)
|
||||
|
||||
async def stop(self):
|
||||
"""停止服务器"""
|
||||
if self.server:
|
||||
self.server.close()
|
||||
await self.server.wait_closed()
|
||||
|
||||
async def __aenter__(self):
|
||||
await self.start()
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
await self.stop()
|
||||
@@ -30,6 +30,7 @@ class Settings:
|
||||
"author_archive": False, # 是否按作者归档
|
||||
"write_mtime": False, # 是否写入修改时间
|
||||
"language": "zh_CN", # 语言设置
|
||||
"script_server": False, # 是否启用脚本服务器
|
||||
}
|
||||
# 根据操作系统设置编码格式
|
||||
encode = "UTF-8-SIG" if system() == "Windows" else "UTF-8"
|
||||
|
||||
@@ -25,13 +25,13 @@ HEADERS = {
|
||||
"user-agent": USERAGENT,
|
||||
}
|
||||
|
||||
MASTER = "b #fff200"
|
||||
PROMPT = "b turquoise2"
|
||||
GENERAL = "b bright_white"
|
||||
PROGRESS = "b bright_magenta"
|
||||
ERROR = "b bright_red"
|
||||
WARNING = "b bright_yellow"
|
||||
INFO = "b bright_green"
|
||||
MASTER = "#fff200"
|
||||
PROMPT = "turquoise2"
|
||||
GENERAL = "bright_white"
|
||||
PROGRESS = "bright_magenta"
|
||||
ERROR = "bright_red"
|
||||
WARNING = "bright_yellow"
|
||||
INFO = "bright_green"
|
||||
|
||||
FILE_SIGNATURES: tuple[
|
||||
tuple[
|
||||
|
||||
Reference in New Issue
Block a user