~cytrogen/kobo-manga

ref: 4e504823f4bf8d2b5f4279da3f4d4ebe98fc97ad kobo-manga/src/kobo_manga/transfer/calibre.py -rw-r--r-- 1.5 KiB
4e504823 — HallowDem Initial commit: kobo-manga pipeline a day ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""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"
            )