Files
dsh 53f4d00d2e docs: 完整操作流程(workflow.md)与打包示例
新增 docs/workflow.md:新吧备份、增量、ZIM 构建、Kiwix 发布、常见坑;examples/ 收录 adnmb 与小说打包脚本。
2026-11-21 15:06:34 +08:00

157 lines
6.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 操作流程(存档用)
从零备份一个贴吧、打包成 ZIM、发布到 Kiwix 的完整流程。按顺序照抄即可。
约定:
- 本地工具目录:`C:\Users\peng0\DSH\tieba-archiver`
- 本地 Python:用归档自带的 venv(已装 aiotieba / requests / libzim),
下文记作 `$py`:
`C:\Users\peng0\DSH\东方年代记吧_全量备份_2011-2026\.venv\Scripts\python.exe`
- 服务器:`peng@erin`,工具在 `/home/peng/tieba/tieba-archiver`,
venv 在 `/home/peng/tieba/venv`(已装 libzim、requests、aiotieba)
- Kiwix(podman 容器):ZIM 放 `/home/peng/containers/kiwix/`,
服务 `systemctl --user restart kiwix`,端口 **8095**
---
## 1. 新建一个吧的备份
```powershell
# 1) 建目录、放 cookie
mkdir C:\Users\peng0\DSH\某某吧_备份\data
copy C:\Users\peng0\DSH\东方年代记吧_全量备份_2011-2026\data\cookies.txt C:\Users\peng0\DSH\某某吧_备份\data\
# 2) 初始化(会自动解析 fid)
& $py tieba.py init --data C:\Users\peng0\DSH\某某吧_备份\data --kw 某某
# 3) 枚举主题列表(--pages 3 可以先试水)
& $py tieba.py enum --data C:\Users\peng0\DSH\某某吧_备份\data
# 4) 抓楼层(后台跑,rate 1.3~1.5)
& $py tieba.py crawl --data C:\Users\peng0\DSH\某某吧_备份\data --rate 1.5
```
**建议直接在 erin 上抓**(网络快、数据不用回传):
```bash
mkdir -p /home/peng/某某吧/data
scp data/cookies.txt peng@erin:/home/peng/某某吧/data/
cd /home/peng/tieba/tieba-archiver
/home/peng/tieba/venv/bin/python tieba.py init --data /home/peng/某某吧/data --kw 某某
nohup /home/peng/tieba/venv/bin/python tieba.py enum --data /home/peng/某某吧/data > enum.log 2>&1 &
nohup /home/peng/tieba/venv/bin/python tieba.py crawl --data /home/peng/某某吧/data --rate 1.5 > crawl.log 2>&1 &
tail -f crawl.log # 每 50 帖一行,失败数为 0 就是正常
```
## 2. 入库、补图、打标签、构建
```bash
cd /home/peng/tieba/tieba-archiver
PY=/home/peng/tieba/venv/bin/python
DATA=/home/peng/某某吧/data
$PY tieba.py import --data $DATA # raw/*.jsonl → tieba.db
$PY tieba.py fetch-emoji --data $DATA # 下载经典表情到 data/emoji/
$PY tieba.py images --data $DATA --workers 4
$PY tieba.py import-images --data $DATA
$PY tieba.py label --data $DATA --label "某某吧 全量备份" --version v1.0
$PY tieba.py stats --data $DATA --live # 注意 --live,否则看不到 WAL 里的新行
$PY tieba.py build-zim --data $DATA --out /home/peng/某某吧/某某吧.zim
```
然后发布(见第 4 节)。
## 3. 日常增量
```bash
$PY tieba.py update --data $DATA # 抓新帖、新楼层、新楼中楼(事件流)
$PY tieba.py verify --data $DATA --threads 200 --priority unhashed --rate 1.0
$PY tieba.py build-zim --data $DATA --out /home/peng/某某吧/某某吧.zim
```
- `verify` 会顺带补 `content_hash`、采样点赞;
- 删除/编辑要第二轮才确认(两次确认机制);
- 全量核对约 1 帖/秒,2011–2026 那个吧要 8–10 小时,建议夜里跑。
## 4. 发布到 Kiwix
```bash
# 文件名就是 Kiwix 里的 URL 名:/content/<文件名>/index.html
scp 某某吧.zim peng@erin:/home/peng/containers/kiwix/某某吧.zim
ssh peng@erin "systemctl --user restart kiwix"
curl -s -o /dev/null -w '%{http_code}\n' \
http://127.0.0.1:8095/content/某某吧/index.html # 期望 200
```
- 书名 = **ZIM 文件名**(不是元数据里的 Title);
- 页内 CSS 文件名带内容哈希(`style-<md5>.css`),改样式重建不会吃缓存;
- 客户端仍显示旧内容时:强刷 / 重启 Kiwix 客户端,或干脆换文件名(新 URL);
- 重启 Kiwix 会重扫整个库(含 28 GB 维基),慢是正常的。
## 5. 已有文本/帖子打包成 ZIM(Gemini、A岛那种)
流程:先把内容整理成 markdown/HTML → 用 `libzim` 写 ZIM(**必须在 WSL/Linux 里写**)。
参考脚本:`examples/adnmb_zim.py`(markdown 串 → ZIM)、`examples/novel_nyasama_zim.py`(抓取结果 → 小说 ZIM)。
```python
from libzim.writer import Creator, Item, StringProvider, Hint
class HtmlItem(Item):
def __init__(self, path, title, content):
super().__init__(); self.path, self.title, self.content = path, title, content
def get_path(self): return self.path
def get_title(self): return self.title
def get_mimetype(self): return "text/css" if self.path.endswith(".css") else "text/html"
def get_contentprovider(self): return StringProvider(self.content)
def get_hints(self): return {Hint.FRONT_ARTICLE: True}
with Creator("out.zim").config_indexing(True, "zho") as c:
c.set_mainpath("index.html")
c.add_item(HtmlItem("style.css", "样式", CSS))
c.add_item(HtmlItem("index.html", "标题", HTML))
for k, v in {"Name": "adnmb-32444047", "Title": "标题", "Language": "zho",
"Creator": "作者", "Publisher": "来源"}.items():
c.add_metadata(k, v)
```
命令行(WSL):
```powershell
wsl -e /opt/zimvenv/bin/python /mnt/c/Users/peng0/DSH/_adnmb_build.py
```
命名规则:**文件名带来源和串号/标识**,如 `adnmb-32444047.zim`、`gemini.zim`。
## 6. 已知的坑
| 坑 | 现象 | 处理 |
|---|---|---|
| Windows libzim >2 GiB | ZIM header 写成 `0xFFFF…`,打不开 | 单文件构建放 WSL/Linux;或 `--max-bytes 1.8G` 分卷 |
| WSL 写 `/mnt/c` | 构建卡在 `Adding checksum` | ZIM 写到 `/root/` 再 `cp` 到 C 盘 |
| 构建机缺 requests | `images` 直接退出 | `pip install requests` |
| `fetch-emoji` 个别 404 | 旧脚本 exit 1 中断流水线 | 已修(v1.2.0 后只警告) |
| `stats` 显示图片 0 | 开了 immutable 快照 | 加 `--live` |
| Kiwix 客户端缓存 | 改了样式/表情仍显示旧的 | CSS 版本化已解决样式;页面换文件名最稳 |
| 表情缺失 | `[孤独寂寞冷]` 文字标签 | `image_emoticon51–59` 全网无存,见 `docs/emoji.md` |
## 7. 服务器现状
| 路径 | 内容 |
|---|---|
| `/home/peng/tieba/data` | 东方年代记吧数据(15 GB,含 imgs) |
| `/home/peng/tieba/tieba-archiver` | 工具代码 |
| `/home/peng/tieba/venv` | Python 环境(libzim / requests / aiotieba) |
| `/home/peng/yeshenxue` | 东方夜神雪吧数据 + 工具副本 + ZIM |
| `/home/peng/containers/kiwix` | Kiwix 库:`touhou-nendaiki.zim`、`yeshenxue.zim`、`gemini.zim`、`adnmb-32444047.zim` |
## 8. 速查
```text
tieba.py 命令:init enum crawl update verify import images import-images
fetch-emoji label stats export build-zim as-of events restore rebuild
关键参数:--data 数据目录 --rate 限速 --threads N --priority unhashed
--out ZIM 输出 --limit 只打 N 帖 --no-images 纯文本
```