S-10 Komga 扫描联动

这个提交存在于:
Codex 2026-07-07 00:19:34 +08:00
父节点 ea098a79e4
当前提交 f1b2e90703
共有 4 个文件被更改,包括 58 次插入 和 16 次删除

查看文件

@ -101,6 +101,11 @@
"type": "string", "type": "string",
"default": "", "default": "",
"invisible": true "invisible": true
},
"scan_timeout_seconds": {
"description": "触发 Komga 扫描请求的超时时间,单位秒。",
"type": "int",
"default": 30
} }
} }
} }

查看文件

@ -96,6 +96,8 @@ class JMDownloadPlugin(Star):
f"发送归档文件:{self.settings.send_archive_file}", f"发送归档文件:{self.settings.send_archive_file}",
f"Komga 自动扫描:{self.settings.komga.enabled}", f"Komga 自动扫描:{self.settings.komga.enabled}",
f"Komga 配置完整:{self.settings.komga.can_scan}", f"Komga 配置完整:{self.settings.komga.can_scan}",
f"Komga 地址:{self.settings.komga.base_url or '未配置'}",
f"Komga Library ID:{self.settings.komga.library_id or '未配置'}",
] ]
yield event.plain_result("\n".join(lines)) yield event.plain_result("\n".join(lines))
event.stop_event() event.stop_event()
@ -108,6 +110,7 @@ class JMDownloadPlugin(Star):
event.stop_event() event.stop_event()
return return
await event.send(event.plain_result("正在触发 Komga 扫描..."))
result = await self.komga.scan_library() result = await self.komga.scan_library()
yield event.plain_result(result.message) yield event.plain_result(result.message)
event.stop_event() event.stop_event()

查看文件

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from urllib.parse import quote
import httpx import httpx
@ -12,6 +13,8 @@ class KomgaScanResult:
attempted: bool attempted: bool
success: bool success: bool
message: str message: str
status_code: int | None = None
endpoint: str = ""
class KomgaClient: class KomgaClient:
@ -20,31 +23,57 @@ class KomgaClient:
async def scan_library(self) -> KomgaScanResult: async def scan_library(self) -> KomgaScanResult:
if not self.settings.enabled: if not self.settings.enabled:
return KomgaScanResult(False, False, "Komga scan is disabled") return KomgaScanResult(False, False, "Komga 自动扫描未启用")
if not self.settings.can_scan: if not self.settings.can_scan:
return KomgaScanResult( return KomgaScanResult(
False, False,
False, False,
"Komga scan is not configured; set base_url, library_id, and credentials", "Komga 扫描配置不完整,请填写 base_url、library_id 和认证信息",
) )
headers = {} url = self._scan_url()
auth = None
if self.settings.api_key:
headers["X-API-Key"] = self.settings.api_key
else:
auth = (self.settings.username, self.settings.password)
url = f"{self.settings.base_url}/api/v1/libraries/{self.settings.library_id}/scan"
try: try:
async with httpx.AsyncClient(timeout=30) as client: async with httpx.AsyncClient(
response = await client.post(url, headers=headers, auth=auth) timeout=self.settings.scan_timeout_seconds,
if response.status_code == 202: ) as client:
return KomgaScanResult(True, True, "Komga scan accepted") response = await client.post(
url,
headers=self._headers(),
auth=self._auth(),
)
if response.status_code in {200, 202, 204}:
return KomgaScanResult(
True,
True,
"Komga 已接受扫描请求",
status_code=response.status_code,
endpoint=url,
)
return KomgaScanResult( return KomgaScanResult(
True, True,
False, False,
f"Komga scan failed: HTTP {response.status_code} {response.text[:200]}", f"Komga 扫描触发失败:HTTP {response.status_code} {response.text[:200]}",
status_code=response.status_code,
endpoint=url,
) )
except Exception as exc: except Exception as exc:
return KomgaScanResult(True, False, f"Komga scan failed: {exc}") return KomgaScanResult(
True,
False,
f"Komga 扫描触发异常:{exc}",
endpoint=url,
)
def _scan_url(self) -> str:
library_id = quote(self.settings.library_id, safe="")
return f"{self.settings.base_url}/api/v1/libraries/{library_id}/scan"
def _headers(self) -> dict[str, str]:
if not self.settings.api_key:
return {}
return {"X-API-Key": self.settings.api_key}
def _auth(self) -> tuple[str, str] | None:
if self.settings.api_key:
return None
return (self.settings.username, self.settings.password)

查看文件

@ -22,6 +22,7 @@ class KomgaSettings:
username: str = "" username: str = ""
password: str = "" password: str = ""
api_key: str = "" api_key: str = ""
scan_timeout_seconds: int = 30
@classmethod @classmethod
def from_config(cls, config: dict[str, Any]) -> "KomgaSettings": def from_config(cls, config: dict[str, Any]) -> "KomgaSettings":
@ -32,6 +33,10 @@ class KomgaSettings:
username=str(config.get("username", "")).strip(), username=str(config.get("username", "")).strip(),
password=str(config.get("password", "")), password=str(config.get("password", "")),
api_key=str(config.get("api_key", "")).strip(), api_key=str(config.get("api_key", "")).strip(),
scan_timeout_seconds=max(
5,
int(config.get("scan_timeout_seconds", 30) or 30),
),
) )
@property @property