bookfetch 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bookfetch/__init__.py +3 -0
- bookfetch/cli.py +169 -0
- bookfetch/model.py +64 -0
- bookfetch/sources/__init__.py +49 -0
- bookfetch/sources/base.py +24 -0
- bookfetch/sources/ctext.py +151 -0
- bookfetch/sources/github.py +152 -0
- bookfetch/sources/libgen.py +147 -0
- bookfetch/sources/wikisource.py +239 -0
- bookfetch/util/__init__.py +92 -0
- bookfetch/util/epub.py +156 -0
- bookfetch/util/simplify.py +19 -0
- bookfetch/util/splitters.py +72 -0
- bookfetch-0.3.0.dist-info/METADATA +146 -0
- bookfetch-0.3.0.dist-info/RECORD +17 -0
- bookfetch-0.3.0.dist-info/WHEEL +4 -0
- bookfetch-0.3.0.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Chapter splitting for Chinese classics text.
|
|
2
|
+
|
|
3
|
+
Text sources (a raw github txt, a ctext page) have no explicit chapter
|
|
4
|
+
structure. Real public-domain classics mark sections with standalone title
|
|
5
|
+
lines — most commonly 《書名》 wrappers (《論天干》《卷之三》), numbered 卷/章
|
|
6
|
+
headers, and bare 序/跋/凡例 markers. ``split_headings`` finds those lines
|
|
7
|
+
and returns ordered Chapter slices of the input; the original text is fully
|
|
8
|
+
preserved (heading lines stay in the body; renderers skip the duplicate).
|
|
9
|
+
|
|
10
|
+
Returns [] when no structural heading is found, so callers fall back to a
|
|
11
|
+
single whole-text chapter — splitting must never destroy content.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import re
|
|
17
|
+
|
|
18
|
+
from ..model import Chapter
|
|
19
|
+
|
|
20
|
+
# 1) 《論天干》-style standalone title lines (the dominant marker in classics)
|
|
21
|
+
_ANGLE_RE = re.compile(r"^《([^《》]{1,60})》$")
|
|
22
|
+
# 2) numbered volume/chapter headers: 卷一 / 第3卷 / 卷之十二 / 第廿四節
|
|
23
|
+
_NUM_RE = re.compile(
|
|
24
|
+
r"^(?:第\s*[0-9一二三四五六七八九十百零〇]+\s*[卷章節节回篇部集]|"
|
|
25
|
+
r"[卷章節节回篇部集]\s*(?:之\s*)?(?:[0-9一二三四五六七八九十百]+))$"
|
|
26
|
+
)
|
|
27
|
+
# 3) bare structural markers (序/跋/凡例/...) as standalone short lines
|
|
28
|
+
_BARE = {
|
|
29
|
+
"卷首", "序", "自序", "原序", "跋", "後記", "后记", "凡例",
|
|
30
|
+
"目錄", "目录", "附錄", "附录", "補遺", "补遗", "題記", "题记",
|
|
31
|
+
}
|
|
32
|
+
_SENT_PUNCT = set(",。;:、!?!?;:,.;:") # a real sentence never lacks these
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _heading(line: str) -> str | None:
|
|
36
|
+
s = line.strip()
|
|
37
|
+
if not s or len(s) > 80:
|
|
38
|
+
return None
|
|
39
|
+
m = _ANGLE_RE.match(s)
|
|
40
|
+
if m:
|
|
41
|
+
return m.group(1).strip()
|
|
42
|
+
if _NUM_RE.match(s):
|
|
43
|
+
return s
|
|
44
|
+
if s in _BARE:
|
|
45
|
+
return s
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def split_headings(lines: list[str]) -> list[Chapter]:
|
|
50
|
+
"""Slice lines into chapters at standalone title lines.
|
|
51
|
+
|
|
52
|
+
A heading line opens its chapter (it stays in the chapter text so the
|
|
53
|
+
concatenation reproduces the input exactly); any leading lines before the
|
|
54
|
+
first heading are folded into chapter one. Empty result = no structure.
|
|
55
|
+
"""
|
|
56
|
+
heads: list[tuple[int, str]] = []
|
|
57
|
+
for i, line in enumerate(lines):
|
|
58
|
+
t = _heading(line)
|
|
59
|
+
if t is not None:
|
|
60
|
+
heads.append((i, t))
|
|
61
|
+
if not heads:
|
|
62
|
+
return []
|
|
63
|
+
chapters: list[Chapter] = []
|
|
64
|
+
for k, (idx, title) in enumerate(heads):
|
|
65
|
+
end = heads[k + 1][0] if k + 1 < len(heads) else len(lines)
|
|
66
|
+
text = "\n".join(lines[idx:end])
|
|
67
|
+
chapters.append(Chapter(title=title, text=text))
|
|
68
|
+
# fold preamble (lines before the first heading) into chapter one
|
|
69
|
+
first = heads[0][0]
|
|
70
|
+
if first > 0:
|
|
71
|
+
chapters[0].text = "\n".join(lines[:first]) + "\n" + chapters[0].text
|
|
72
|
+
return chapters
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: bookfetch
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Agent-friendly ebook finder CLI: routes book queries to working sources (Chinese classics first).
|
|
5
|
+
Author-email: Helios Wei <helioswei1995@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: agent,chinese,classics,cli,ctext,ebook
|
|
8
|
+
Classifier: Environment :: Console
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Provides-Extra: simp
|
|
14
|
+
Requires-Dist: opencc>=1.4; extra == 'simp'
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# bookfetch
|
|
18
|
+
|
|
19
|
+
给 Agent 用的电子书查找与下载 CLI:把书名丢给它,它自动把请求路由到**实际能下到书**的源。
|
|
20
|
+
|
|
21
|
+
设计出发点(实测结论):现成的 agent 找书技能几乎全部绑定 Z-Library / Libgen,而这两者对**中文古籍基本无效**(不收录 / 账号墙 / Cloudflare 墙)。中文古籍真正能用的源是 ctext.org(中国哲学书电子化计划:免费、带标点、国内直连)——没人把它做成 agent 工具,于是有了 bookfetch。
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
bookfetch search 渊海子平 # 跨源搜索,输出 JSON
|
|
25
|
+
bookfetch get ctext 727782 # 下载整本书到当前目录
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 特性
|
|
29
|
+
|
|
30
|
+
- 书源路由:按书种/语言分发到可用源,单个源故障不影响整体(errors 独立上报)
|
|
31
|
+
- **EPUB / 章节感知**:`--format epub` 零依赖生成手机可读的 epub(含目录);`--split`
|
|
32
|
+
在 txt 中插入章节分隔;古籍《》/卷/序跋类标题行自动识别为章节
|
|
33
|
+
- JSON 优先输出:stdout 只吐结构化 JSON,agent 直接解析;`--human` 给人看
|
|
34
|
+
- 礼貌抓取:内置限速 + 重试退避 + 编码回退(GBK/Big5→UTF-8)
|
|
35
|
+
- **零运行时依赖**:纯 Python 标准库,任何环境装完即用(简体转换是可选扩展)
|
|
36
|
+
- 离线可测:解析测试基于真实抓包样本(fixtures),不依赖线上
|
|
37
|
+
|
|
38
|
+
## 安装
|
|
39
|
+
|
|
40
|
+
需要 Python >= 3.10。
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# 推荐:uv
|
|
44
|
+
uv tool install git+https://github.com/Helioswei/bookfetch.git
|
|
45
|
+
|
|
46
|
+
# 或 pip
|
|
47
|
+
pip install git+https://github.com/Helioswei/bookfetch.git
|
|
48
|
+
|
|
49
|
+
# 本地开发
|
|
50
|
+
uv sync && uv run bookfetch search 论语
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 用法
|
|
54
|
+
|
|
55
|
+
### 搜索
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
bookfetch search <书名>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
输出(JSON,字段稳定,供 agent 消费):
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"cmd": "search",
|
|
66
|
+
"query": "渊海子平",
|
|
67
|
+
"results": [
|
|
68
|
+
{
|
|
69
|
+
"source": "ctext",
|
|
70
|
+
"id": "727782",
|
|
71
|
+
"title": "淵海子平",
|
|
72
|
+
"url": "https://ctext.org/wiki.pl?if=gb&res=727782",
|
|
73
|
+
"subtitle": "維基文字版:開放共同編輯的資料。",
|
|
74
|
+
"format_hint": "txt",
|
|
75
|
+
"extra": { "author": "徐子平" }
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
"count": 1,
|
|
79
|
+
"errors": {}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`--source ctext` 限定源(可重复);`--limit N` 限制条数;`--human` 输出人类可读列表。
|
|
84
|
+
|
|
85
|
+
### 下载
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
bookfetch get ctext 727782 --out ./books # 默认 txt(整本合并)
|
|
89
|
+
bookfetch get ctext 727782 --format epub --out ./books # 手机友好的 epub(自动分章+目录)
|
|
90
|
+
bookfetch get ctext 727782 --split --out ./books # txt 中插入 === 章节 === 分隔
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
把 id 对应的整本书下载为 UTF-8 纯文本(ctext 的书会自动按序抓取全部章节并拼接)。
|
|
94
|
+
`epub` 与 `--split` 的章节来自源结构(ctext 分页)或《》/卷/序跋类标题行自动识别;
|
|
95
|
+
正文一字不改,标题行仅在阅读视图去重。
|
|
96
|
+
|
|
97
|
+
#### 繁转简(可选)
|
|
98
|
+
|
|
99
|
+
默认保留古籍繁体原文;需要简体版时加 `--simplify`(需先装可选依赖):
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
uv tool install bookfetch --extra simp # 或 pip install 'bookfetch[simp]'
|
|
103
|
+
bookfetch get ctext 727782 --out ./books --simplify
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
转换基于 OpenCC(t2s),文件与文件名会一并转为简体。古籍存在异体字/通假字,
|
|
107
|
+
转换非 100% 保真,学术用途请以原文为准。
|
|
108
|
+
|
|
109
|
+
## 已支持的书源
|
|
110
|
+
|
|
111
|
+
| 源 | 覆盖 | 说明 |
|
|
112
|
+
|---|---|---|
|
|
113
|
+
| ctext | 中文古籍(免费全文、带标点) | 书目检索 + 多章节整本下载 |
|
|
114
|
+
| github | 公版中文古籍文本仓库 | 精选仓库树索引(7 天缓存),raw 直连下载 |
|
|
115
|
+
| wikisource | 中文/英文公版书(含现代公版:鲁迅等) | MediaWiki API + 渲染页解析,目录自动展开整本;大陆访问需代理 |
|
|
116
|
+
| libgen | 英文现代书(原文件 epub/pdf) | 探活式镜像链(域名轮换频繁),当前镜像不可达时会明确报错;需代理 |
|
|
117
|
+
|
|
118
|
+
> 网络提示:ctext/github 大陆直连可用;wikisource(Wikimedia)与 libgen 大陆直连不通,
|
|
119
|
+
> 需能访问对应站点的网络环境(如代理),本工具遵循系统 http_proxy/https_proxy 环境变量。
|
|
120
|
+
|
|
121
|
+
## 源与合规
|
|
122
|
+
|
|
123
|
+
- bookfetch 是**路由与下载工具**,不存储、不重新分发任何书籍内容;下载物只落在使用者本地
|
|
124
|
+
- 各源内容版权归原作者/整理者所有。公版内容可自由使用;**仍在版权期内的内容,请使用者自行确认下载与使用的合法性**
|
|
125
|
+
- 抓取行为遵守各源访问条款:公开页面、内置限速、不做任何绕过(登录墙/验证码/反爬规避)
|
|
126
|
+
- 测试 fixture 为各源页面/结构的极小样本,仅用于解析测试,来源记录见 tests/fixtures/README.md
|
|
127
|
+
- 任何权利方认为本工具对某源的使用不妥,请提 issue,我们会调整或移除该源
|
|
128
|
+
|
|
129
|
+
## 开发与测试
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
uv sync --group dev
|
|
133
|
+
uv run pytest -q # 离线测试,基于 tests/fixtures 真实抓包样本
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 路线图
|
|
137
|
+
|
|
138
|
+
- [x] M1: ctext 源 + search/get CLI(2026-09-03 完成)
|
|
139
|
+
- [x] M2: github 古籍源 + OpenCC 简繁转换 + 合规声明(2026-09-03 完成)
|
|
140
|
+
- [x] M3: EPUB 转换 + 章节切分(2026-09-04 完成,零依赖手写 zip+xhtml)
|
|
141
|
+
- [x] M4: wikisource 中/英公版源 + libgen 探活镜像链 + 白话注解 spike(2026-09-04 完成;白话注解判定放弃,见 PRD)
|
|
142
|
+
- [ ] 规划中: SKILL.md agent 外壳 + PyPI 发布
|
|
143
|
+
|
|
144
|
+
## 许可
|
|
145
|
+
|
|
146
|
+
MIT。只面向公版/开放文本(ctext 收录均为公版古籍)。请遵守各源的访问条款。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
bookfetch/__init__.py,sha256=dF-q5LWUX3J8k0Cc7M3rzV2o45rSgPRkQI3FqEVZ1pg,76
|
|
2
|
+
bookfetch/cli.py,sha256=gWqxnFoLunZEoZQGv5bxL_FmYsAzT-XS9Zp-9OKLS7k,6568
|
|
3
|
+
bookfetch/model.py,sha256=bH7IcYuDwrLUUQCIeyrh_4OY2ZXnHZ2qVKM54YPXjtU,1664
|
|
4
|
+
bookfetch/sources/__init__.py,sha256=wdWS9jHS0AmDzgzH9dg9LMkQwg6Vj2kmZY1rOjEeE-I,1256
|
|
5
|
+
bookfetch/sources/base.py,sha256=YPUReFgIpJ9F65sTEywoa_QqVM30mZe6U10HMrEru_U,739
|
|
6
|
+
bookfetch/sources/ctext.py,sha256=YbOy-W6Dm57A3ANf99Bs4CFxFjQUdmbsXB9ny18jhdg,5482
|
|
7
|
+
bookfetch/sources/github.py,sha256=pxACaMbHSEp7VVcaxe_IdDJHf9QJtRJSHv7BrWA4Cu4,5191
|
|
8
|
+
bookfetch/sources/libgen.py,sha256=izgYttnMZbd2hOfbl2-AH4wZymb1YoNBTa91K7YxiGw,5042
|
|
9
|
+
bookfetch/sources/wikisource.py,sha256=dMhqs_oB-ExWZt8E7eVdzmeuc9lqJ9SPcFuxbTIJV6M,8538
|
|
10
|
+
bookfetch/util/__init__.py,sha256=SgBHSkdRsqkdYLwCqTyTkl-0yvbc1s6vmLiXxbxzXFk,3128
|
|
11
|
+
bookfetch/util/epub.py,sha256=xjo41lSZkXFGC8jE0sMLx5gkG51GvzuobINEGYk08Uw,5180
|
|
12
|
+
bookfetch/util/simplify.py,sha256=eUu_hgJmOsUyJ9M0fQbU9cWChiqEklctF-2qr2Ef0NE,653
|
|
13
|
+
bookfetch/util/splitters.py,sha256=mgM9OoHf1t3WBw51wb2Qup1cVmK69TArP6HDcNXEnYA,2800
|
|
14
|
+
bookfetch-0.3.0.dist-info/METADATA,sha256=FDFAf1SoKn8wne_JcNs6-FdkarmADrmBLPWDBRGE5HQ,5997
|
|
15
|
+
bookfetch-0.3.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
16
|
+
bookfetch-0.3.0.dist-info/entry_points.txt,sha256=an19TQ4qsR7UgUH3A4grSUNMDZbf_KOeQ31vwHM9DTo,49
|
|
17
|
+
bookfetch-0.3.0.dist-info/RECORD,,
|