astrbot_plugin_jmdowloads/services/komga.py
2026-07-07 00:01:44 +08:00

50 行
1.6 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
import httpx
from .settings import KomgaSettings
@dataclass(frozen=True)
class KomgaScanResult:
attempted: bool
success: bool
message: str
class KomgaClient:
def __init__(self, settings: KomgaSettings) -> None:
self.settings = settings
async def scan_library(self) -> KomgaScanResult:
if not self.settings.enabled:
return KomgaScanResult(False, False, "Komga scan is disabled")
if not self.settings.can_scan:
return KomgaScanResult(
False,
False,
"Komga scan is not configured; set base_url, library_id, and credentials",
)
headers = {}
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:
async with httpx.AsyncClient(timeout=30) as client:
response = await client.post(url, headers=headers, auth=auth)
if response.status_code == 202:
return KomgaScanResult(True, True, "Komga scan accepted")
return KomgaScanResult(
True,
False,
f"Komga scan failed: HTTP {response.status_code} {response.text[:200]}",
)
except Exception as exc:
return KomgaScanResult(True, False, f"Komga scan failed: {exc}")