"""Calibre 传输
通过 calibredb 命令行工具将 KEPUB 导入 Calibre 书库。
"""
import subprocess
from pathlib import Path
class CalibreTransfer:
"""通过 calibredb CLI 将 KEPUB 导入 Calibre。"""
def transfer(self, kepub_paths: list[Path]) -> list[Path]:
"""将 .kepub.epub 文件导入 Calibre 书库。
Args:
kepub_paths: 要导入的文件列表
Returns:
成功导入的文件列表
Raises:
RuntimeError: calibredb 不可用
"""
self._check_calibredb()
imported = []
for path in kepub_paths:
result = subprocess.run(
["calibredb", "add", str(path)],
capture_output=True,
text=True,
)
if result.returncode == 0:
imported.append(path)
else:
print(f" [!] 导入失败: {path.name}")
if result.stderr:
print(f" {result.stderr.strip()}")
return imported
def _check_calibredb(self) -> None:
"""检查 calibredb 是否可用。"""
try:
subprocess.run(
["calibredb", "--version"],
capture_output=True,
timeout=10,
)
except FileNotFoundError:
raise RuntimeError(
"未找到 calibredb 命令。"
"请安装 Calibre 并确认 calibredb 在系统 PATH 中。"
"下载地址: https://calibre-ebook.com/download"
)