Compare commits
12
Commits
1b2071d78b
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6908b5441 | ||
|
|
4dee7a3c0c | ||
|
|
67aa9be3da | ||
|
|
cba14eae25 | ||
|
|
c3f8d64f0e | ||
|
|
9b9c501fa9 | ||
|
|
9ecbbbc6d8 | ||
|
|
9ba194ae97 | ||
|
|
a436bf58b7 | ||
|
|
371d3a3624 | ||
|
|
ca26977233 | ||
|
|
5a43bce45d |
@@ -2,7 +2,7 @@
|
||||
|
||||
统一接口服务。线上入口:`https://openapi.rrrosmontis.icu`
|
||||
|
||||
聚合层把 **pixiv 图片服务**与 **bilibili 视频服务**收敛到单一对外入口。三个后端进程全部只监听 `127.0.0.1`,对外仅由 nginx 暴露 80/443——即"一个入口、零暴露端口"。
|
||||
聚合层把 **pixiv 图片服务**、**bilibili 视频服务**、**dufe 银杏签到码查询服务**与 **jm 漫画下载服务**收敛到单一对外入口。五个后端进程全部只监听 `127.0.0.1`,对外仅由 nginx 暴露 80/443——即"一个入口、零暴露端口"。
|
||||
|
||||
## 架构
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
公网 443/80 nginx (server_name openapi.rrrosmontis.icu)
|
||||
├── /pixiv/get_image?id=xxx -> 127.0.0.1:9008 main.py 聚合层 -> 127.0.0.1:9007 pixiv
|
||||
├── /bilibili/get_video?bv=xxx -> 127.0.0.1:9008 main.py 聚合层 -> 127.0.0.1:9006 bilibili
|
||||
├── /dufe/ginkgostu_sign_in -> 127.0.0.1:9008 main.py 聚合层 -> 127.0.0.1:9005 dufe 签到码查询(GET|POST)
|
||||
├── /jm/get_comic?id=xxx -> 127.0.0.1:9008 main.py 聚合层 -> 127.0.0.1:9004 jm
|
||||
├── /img-master/ /img-original/ -> nginx 直接反代 i.pximg.net(无本地后端)
|
||||
└── / -> 127.0.0.1:9008(302 跳转文章页)
|
||||
```
|
||||
@@ -20,7 +22,9 @@
|
||||
|---|---|---|---|
|
||||
| openapi | `127.0.0.1:9008` | `source/main.py`(aiohttp) | 聚合层,纯转发,**唯一入口** |
|
||||
| pixiv | `127.0.0.1:9007` | `source/pixiv/pixiv.py`(Flask + waitress) | 图片代理 |
|
||||
| bilibili | `127.0.0.1:9006` | `source/bilibili/bilibili.py`(aiohttp + yt-dlp) | 视频代理 |
|
||||
| bilibili | `127.0.0.1:9006` | `source/bilibili/bilibili.py`(aiohttp,官方 API 直取 + yt-dlp 后备) | 视频代理 |
|
||||
| dufe 签到 | `127.0.0.1:9005` | `source/dufe/ginkgostu_sign_in/ginkgo_sign_in.py`(Flask + waitress 16 线程) | 银杏(DUFE)签到码查询,`GET\|POST /dufe/ginkgostu_sign_in`;`&mode=auto` 时拉取通知列表自动签到(与 bot ginkgo 同逻辑,连续重试,结果列表聚合返回) |
|
||||
| jm | `127.0.0.1:9004` | `source/jm/jm.py`(aiohttp + jmcomic) | 漫画下载打包(移植自 qqbot jm 模块),`GET /jm/get_comic?id=xxx[&range=起-止]` |
|
||||
| pixiv-token-refresh | — | `source/pixiv/refresh_token.py` | token 刷新守护(每 30–60 分钟随机) |
|
||||
| bilibili-refresh | — | `source/bilibili/refresh_cookie.py` | cookie 刷新守护(每 50–70 分钟随机) |
|
||||
|
||||
@@ -31,7 +35,7 @@
|
||||
|
||||
```
|
||||
source/
|
||||
├── main.py # 聚合层 (127.0.0.1:9008),三条路由:/ /pixiv/get_image /bilibili/get_video
|
||||
├── main.py # 聚合层 (127.0.0.1:9008),五条路由:/ /pixiv/get_image /bilibili/get_video /dufe/ginkgostu_sign_in /jm/get_comic
|
||||
├── pixiv/
|
||||
│ ├── pixiv.py # Pixiv 图片服务 (127.0.0.1:9007)
|
||||
│ ├── refresh_token.py # Pixiv OAuth token 刷新守护
|
||||
@@ -41,17 +45,27 @@ source/
|
||||
│ ├── refresh_cookie.py # cookie 定时刷新守护
|
||||
│ ├── qr_login.py # 扫码登录获取 cookie
|
||||
│ └── qr_login_fast.py
|
||||
├── dufe/
|
||||
│ └── ginkgostu_sign_in/
|
||||
│ └── ginkgo_sign_in.py # 银杏(DUFE)签到码查询 (127.0.0.1:9005, GET|POST)
|
||||
├── jm/
|
||||
│ └── jm.py # JM 漫画下载打包服务 (127.0.0.1:9004)
|
||||
└── cache/ # 运行时生成,不入库
|
||||
├── pixiv/images # 图片缓存
|
||||
└── bilibili/ # 视频缓存
|
||||
├── bilibili/ # 视频缓存
|
||||
└── jm/ # jm 临时 ZIP(TTL 10 分钟,返回后删除)
|
||||
nginx/
|
||||
└── openapi.rrrosmontis.icu.conf # 统一入口(/pixiv/ /bilibili/ /img-master/ /img-original/)
|
||||
├── openapi.rrrosmontis.icu.conf # 统一入口(/pixiv/ /bilibili/ /img-master/ /img-original/,其余路径由 location / 兜底至聚合层;含按 IP 限流)
|
||||
├── 00-limits.conf # 限流 zone(limit_req_zone rate=10r/s、limit_conn_zone、429)——缺失会导致 nginx -t 失败
|
||||
└── 00-websocket.conf # $connection_upgrade map 定义——缺失会导致 nginx -t 失败
|
||||
systemd/
|
||||
├── openapi.service # 聚合层
|
||||
├── pixiv.service # Pixiv 图片服务(waitress --threads=8)
|
||||
├── bilibili.service # bilibili 视频服务
|
||||
├── bilibili-refresh.service # cookie 刷新守护
|
||||
└── pixiv-token-refresh.service # Pixiv token 刷新守护
|
||||
├── pixiv-token-refresh.service # Pixiv token 刷新守护
|
||||
├── ginkgo_sign_in.service # 银杏(DUFE)签到码查询服务
|
||||
└── jm.service # JM 漫画下载打包服务
|
||||
docs/
|
||||
└── 部署与运维.md # 线上服务器的路径、缓存、迁移步骤与变更记录
|
||||
```
|
||||
@@ -71,7 +85,8 @@ IMAGE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
**bilibili(视频)**:以 **BV 号为键**缓存到 `source/cache/bilibili/{BV}.mp4`
|
||||
|
||||
- 命中即直接 `FileResponse`,**不再调用 yt-dlp**
|
||||
- 命中即直接 `FileResponse`,**不再下载**
|
||||
- 未命中时优先走 **官方 API 直取**(view → playurl → 媒体段;2026-10-22 起 hk 出口被页面 WAF 挑战,yt-dlp 页面提取失效),失败再回退 yt-dlp
|
||||
- 未命中才下载,且先落到缓存目录内的隐藏暂存目录 `.dl-XXXX`,成功后 `os.replace` 原子落盘
|
||||
- **无论成功 / 失败 / 超时都会 `shutil.rmtree(tmpdir)`** —— 早期实现只清理 cookie 临时文件、从不清理视频暂存目录,导致每次取视频都把完整视频永久留在 `/tmp`(无上限磁盘泄漏),已修复
|
||||
- 淘汰策略:单文件 `CACHE_TTL_SECONDS`(默认 7 天)未访问即过期;总量超过 `CACHE_MAX_BYTES`(默认 2 GiB)时按 mtime 从最旧开始删除
|
||||
@@ -84,14 +99,23 @@ curl -s 127.0.0.1:9006/bilibili/cache_info
|
||||
# {cache_dir, cache_max_bytes, cache_ttl_seconds, count, total_bytes, free_bytes, files[]}
|
||||
```
|
||||
|
||||
**jm(漫画)**:**非持久缓存**,与 bilibili 不同——ZIP 仅作临时产物
|
||||
|
||||
- 请求 `id`(+ 可选 `range=起-止`,单次 ≤30 章)→ jmcomic 下载到 `cache/jm/` 内隐藏暂存目录 `.dl-<id>-XXXX` → webp 统一转 jpg 打包 ZIP(`{标题}_{id}_ch{起}-{止}.zip`)→ HTTP 返回
|
||||
- 打包完成即 `rmtree` 图片暂存目录;ZIP 在返回后 `ZIP_TTL_SECONDS`(默认 600 秒)延时删除
|
||||
- TTL 窗口内重复请求同一 ZIP 直接复用并重新计时;每次请求附带 `_cache_sweep()` 兜底清理过期 ZIP(防进程重启残留)
|
||||
- 同一本子串行处理(per-album asyncio.Lock),并发请求同本返回 409
|
||||
- 运维接口:`curl -s 127.0.0.1:9004/jm/cache_info`
|
||||
|
||||
## 部署
|
||||
|
||||
1. **Python 环境**:三服务共用一个环境,需含 `flask`、`waitress`、`aiohttp`、`httpx`、`yt-dlp`
|
||||
1. **Python 环境**:各服务共用一个环境,需含 `flask`、`waitress`、`aiohttp`、`httpx`、`yt-dlp`、`jmcomic`、`pillow`;系统包另需 **ffmpeg**(yt-dlp 合流用,API 直取路径不需要)
|
||||
2. **凭据**:
|
||||
- 复制 `source/pixiv/tokens.template.json` 为 `source/pixiv/tokens.json`(`chmod 600`)并填入实际 pixiv token
|
||||
- 放置 `source/bilibili/cookies.txt`(`name=value` 格式),或运行 `qr_login.py` 扫码生成
|
||||
- jm 无需凭据,但要求部署机可直连源站(jmcomic 会自动更新 API 域名)
|
||||
3. **服务**:将 `systemd/*.service` 安装到 `/etc/systemd/system/`,然后
|
||||
`systemctl daemon-reload && systemctl enable --now openapi pixiv bilibili bilibili-refresh pixiv-token-refresh`
|
||||
`systemctl daemon-reload && systemctl enable --now openapi pixiv bilibili ginkgo_sign_in jm bilibili-refresh pixiv-token-refresh`
|
||||
4. **nginx**:将 `nginx/openapi.rrrosmontis.icu.conf` 放入 `/etc/nginx/conf.d/`(需存在 `$connection_upgrade` 的 map 定义),`nginx -t && systemctl reload nginx`
|
||||
5. **软链**(可选):`ln -s <部署根>/source/cache/pixiv/images /home/images`
|
||||
6. **定时任务**:无 cron/timer——两个刷新守护均为常驻循环(内部随机间隔),systemd `Restart=always` 兜底
|
||||
@@ -102,18 +126,26 @@ curl -s 127.0.0.1:9006/bilibili/cache_info
|
||||
curl -k "https://openapi.rrrosmontis.icu/pixiv/get_image?id=147113746"
|
||||
curl -k "https://openapi.rrrosmontis.icu/bilibili/get_video?bv=BV16a41157Q4"
|
||||
curl -kI "https://openapi.rrrosmontis.icu/bilibili/get_video?bv=BV16a41157Q4" # 只看头部,省流量
|
||||
curl -k "https://openapi.rrrosmontis.icu/dufe/ginkgostu_sign_in" # 无参应返回 400 JSON
|
||||
curl -k "https://openapi.rrrosmontis.icu/jm/get_comic?id=350234" -o jm.zip # 返回 ZIP
|
||||
curl -s 127.0.0.1:9006/bilibili/cache_info
|
||||
curl -s 127.0.0.1:9004/jm/cache_info
|
||||
```
|
||||
|
||||
端口暴露自检(三个后端都应"不可达",仅 443/80 通):
|
||||
端口暴露自检(五个后端都应"不可达",仅 443/80 通):
|
||||
|
||||
```bash
|
||||
for p in 9006 9007 9008; do timeout 3 bash -c "cat < /dev/null > /dev/tcp/<host>/$p" 2>/dev/null \
|
||||
for p in 9004 9005 9006 9007 9008; do timeout 3 bash -c "cat < /dev/null > /dev/tcp/<host>/$p" 2>/dev/null \
|
||||
&& echo "$p 可连通(异常)" || echo "$p 不可达(正常)"; done
|
||||
```
|
||||
|
||||
## 版本要点
|
||||
|
||||
- **2026-09-23**
|
||||
- dufe 签到服务新增 **`mode=auto`**:拉取通知列表(getMessageAll + busiMessage/list)→ 按 bot ginkgo 同款规则发现签到活动(互动消息 + 文本含"签到" + 可解析课程链接,按 signInId 去重)→ 逐个自动签到;**连续重试**(首次 + 最多 5 次,间隔 3s,对齐 bot `MAX_SIGNIN_ATTEMPTS=6 / SIGNIN_BACKOFF`;token 疑似失效时重登录一次立即重试,对齐 bot refresh 语义);**所有结果聚合为 `data.results` 列表**返回(每活动含 success/message/attempts/title/signInCode 等)
|
||||
- 非签到码签到逻辑与 bot ginkgo 比对后以 bot 为基准同步:移植限流防护体系(`check_response` / `is_rate_limited` / `safe_error` / `_LoginErrors`)、`parse_course_url` 的 courseId 放宽至 `[0-9A-Za-z]+`、`commit_sign_in` 的错误包装、`fetch_sign_in_info` 的 check_response 与外部登录态注入(`sess`/`token` 参数,auto 模式请求内复用同一登录态)
|
||||
- **2026-09-23**(早些):新增 **jm 漫画下载打包服务**(`source/jm/jm.py`,aiohttp + jmcomic,`127.0.0.1:9004`,端点 `GET /jm/get_comic?id=xxx[&range=起-止]`),移植自 qqbot 的 jm 模块(去掉 QQ/代理轮换/上传 cf 逻辑,改为 HTTP 直返 ZIP;hk 可直连源站无需代理)。ZIP 为临时产物:返回后 600 秒延时删除,TTL 窗口内同名请求直接复用。聚合层 `main.py` 注册路由(新增 `JM_BACKEND`,复用既有 `proxy()`),nginx 无需改动(`location /` 兜底)
|
||||
- **2026-09-13**:新增 **dufe 银杏签到码查询服务**(`source/dufe/ginkgostu_sign_in/ginkgo_sign_in.py`,Flask + waitress 16 线程,`127.0.0.1:9005`,端点 `GET|POST /dufe/ginkgostu_sign_in`),并在聚合层 `main.py` 注册该路由——为此新增 `proxy_forward()`(保留请求方法与请求体,原 `proxy()` 保持固定 GET 不变,零改动既有行为)。同期修复该服务两处缺陷:base64 参数补 `urllib.parse.unquote`(兼容 iOS 快捷指令的 `%3D` 编码),`commit` 判定改为 fail-closed(须 `commit.success is True` 才算签到成功)
|
||||
- **2026-09-12**
|
||||
- 端口重排 `9888/9088/19990 → 9008/9007/9006`,全部仅绑 `127.0.0.1`;pixiv 由 `0.0.0.0:9088` 收口
|
||||
- 主入口 `main.py` 从 `source/openapi/` 上移到 `source/`,systemd `WorkingDirectory` 同步为 `source/`
|
||||
|
||||
+86
-13
@@ -11,6 +11,8 @@
|
||||
|---|---|
|
||||
| `/pixiv/get_image?id=xxx` | `127.0.0.1:9008`(main.py 聚合层)→ `127.0.0.1:9007`(pixiv) |
|
||||
| `/bilibili/get_video?bv=xxx` | `127.0.0.1:9008`(main.py 聚合层)→ `127.0.0.1:9006`(bilibili) |
|
||||
| `/dufe/ginkgostu_sign_in` | `127.0.0.1:9008`(main.py 聚合层)→ `127.0.0.1:9005`(dufe 签到码查询,支持 GET\|POST;`&mode=auto` 自动签到) |
|
||||
| `/jm/get_comic?id=xxx[&range=起-止]` | `127.0.0.1:9008`(main.py 聚合层)→ `127.0.0.1:9004`(jm 漫画下载打包) |
|
||||
| `/img-master/`、`/img-original/` | nginx 直接反代 `i.pximg.net`(无本地后端) |
|
||||
| `/` | `127.0.0.1:9008`(302 跳转文章页) |
|
||||
|
||||
@@ -20,13 +22,15 @@
|
||||
|
||||
## 进程与端口
|
||||
|
||||
三个服务**全部只绑 `127.0.0.1`**,对外仅 nginx 的 443/80。
|
||||
五个服务**全部只绑 `127.0.0.1`**,对外仅 nginx 的 443/80。
|
||||
|
||||
| systemd 单元 | 源码 | 监听 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `openapi.service` | `source/main.py` | `:9008` | 聚合层(aiohttp,纯转发,唯一入口) |
|
||||
| `pixiv.service` | `source/pixiv/pixiv.py` | `:9007` | Flask,由 waitress(8 线程)承载 |
|
||||
| `bilibili.service` | `source/bilibili/bilibili.py` | `:9006` | aiohttp + yt-dlp |
|
||||
| `bilibili.service` | `source/bilibili/bilibili.py` | `:9006` | aiohttp,官方 API 直取 + yt-dlp 后备 |
|
||||
| `ginkgo_sign_in.service` | `source/dufe/ginkgostu_sign_in/ginkgo_sign_in.py` | `:9005` | 银杏(DUFE)签到码查询,Flask + waitress(16 线程),`GET\|POST` |
|
||||
| `jm.service` | `source/jm/jm.py` | `:9004` | JM 漫画下载打包,aiohttp + jmcomic |
|
||||
| `pixiv-token-refresh.service` | `source/pixiv/refresh_token.py` | — | 常驻刷新 Pixiv OAuth token |
|
||||
| `bilibili-refresh.service` | `source/bilibili/refresh_cookie.py` | — | 常驻刷新 bilibili cookie |
|
||||
|
||||
@@ -41,18 +45,24 @@
|
||||
│ │ ├── refresh_token.py token 刷新守护
|
||||
│ │ └── tokens.json Pixiv OAuth token(敏感,权限 600,不入库)
|
||||
│ ├── bilibili/
|
||||
│ │ ├── bilibili.py 视频代理(aiohttp + yt-dlp)
|
||||
│ │ ├── bilibili.py 视频代理(aiohttp,API 直取 + yt-dlp 后备)
|
||||
│ │ ├── cookies.txt bilibili cookie(敏感,不入库)
|
||||
│ │ ├── refresh_cookie.py cookie 刷新守护
|
||||
│ │ └── qr_login*.py 扫码登录工具(cookie 失效时人工重登用)
|
||||
│ ├── dufe/
|
||||
│ │ └── ginkgostu_sign_in/
|
||||
│ │ └── ginkgo_sign_in.py 银杏(DUFE)签到码查询服务(127.0.0.1:9005,GET|POST)
|
||||
│ ├── jm/
|
||||
│ │ └── jm.py JM 漫画下载打包服务(127.0.0.1:9004)
|
||||
│ └── cache/ 运行时缓存(不入库)
|
||||
│ ├── pixiv/images pixiv 图片缓存(代码中为 ../cache/pixiv/images)
|
||||
│ └── bilibili/ bilibili 视频缓存(上限 2 GiB / TTL 7 天)
|
||||
├── systemd/ 5 个单元文件的归档副本(与 /etc/systemd/system/ 保持一致)
|
||||
│ ├── bilibili/ bilibili 视频缓存(上限 2 GiB / TTL 7 天)
|
||||
│ └── jm/ jm 临时 ZIP(TTL 10 分钟,返回后删除)
|
||||
├── systemd/ 7 个单元文件的归档副本(与 /etc/systemd/system/ 保持一致)
|
||||
├── nginx/ 宝塔导出的历史存档,非生效路径
|
||||
├── cert/ SSL 证书
|
||||
├── env/ Python 环境(general + env_general.tar.gz 整包)
|
||||
└── 备份说明.txt 本文件的历史版本(旧端口/旧路径,仅供参考)
|
||||
└── 备份说明.txt 本文档在服务器上的纯文本副本(随本文档同步维护)
|
||||
|
||||
/home/images -> /home/openapi/source/cache/pixiv/images 便捷软链
|
||||
```
|
||||
@@ -64,33 +74,46 @@
|
||||
**bilibili(视频)**:以 BV 号为键缓存到 `source/cache/bilibili/{BV}.mp4`
|
||||
|
||||
- 命中即复用、不再下载
|
||||
- 未命中才调 yt-dlp,先下载到缓存目录内的隐藏暂存目录 `.dl-XXXX`,成功后原子 rename 落盘
|
||||
- 未命中时优先走 **官方 API 直取**(view → playurl → 媒体段,不受页面 WAF 影响);失败回退 yt-dlp,均写入缓存目录内的隐藏暂存目录 `.dl-XXXX`,成功后原子 rename 落盘
|
||||
- 无论成功 / 失败 / 超时都 `rmtree` 该暂存目录
|
||||
(2026-09-12 修复:原实现从不清理 tmpdir,每次取视频都会把完整视频永久留在 `/tmp`,属无上限磁盘泄漏)
|
||||
- 淘汰策略:单文件 7 天未访问过期 + 总量超过 2 GiB 时按 mtime 从最旧开始删
|
||||
- 运维接口:`curl -s 127.0.0.1:9006/bilibili/cache_info`
|
||||
|
||||
**jm(漫画)**:**非持久临时产物**,移植自 qqbot 的 jm 模块(bj `C:/call/bot/utility/jm.py`)
|
||||
|
||||
- `GET /jm/get_comic?id=xxx[&range=起-止]`:jmcomic 下载 → `cache/jm/` 内隐藏暂存目录 `.dl-<id>-XXXX` → webp 统一转 jpg 打包 ZIP(`{标题}_{id}_ch{起}-{止}.zip`)→ HTTP 直返
|
||||
- 单次上限 30 章(与 qqbot 一致,同时避免超过聚合层 300s 超时);总章节 ≤30 可省略 range 自动全量
|
||||
- 打包完成即 `rmtree` 图片暂存目录;ZIP 在返回后 600 秒(`ZIP_TTL_SECONDS`)延时删除
|
||||
- TTL 窗口内同名 ZIP 重复请求直接复用并 touch 重新计时;每次请求附带 `_cache_sweep()` 兜底清理过期 ZIP(防进程重启残留)
|
||||
- 同一本子串行处理(per-album asyncio.Lock),并发请求同本返回 409
|
||||
- hk 直连源站,无需代理(jmcomic 自动更新 API 域名);qqbot 侧的代理轮换 / 上传 cf / QQ 回复逻辑均未带入
|
||||
- 运维接口:`curl -s 127.0.0.1:9004/jm/cache_info`
|
||||
|
||||
## nginx
|
||||
|
||||
| 文件 | 作用 |
|
||||
|---|---|
|
||||
| `/etc/nginx/conf.d/openapi.rrrosmontis.icu.conf` | 统一入口站点(`proxy_pass` → `127.0.0.1:9008`) |
|
||||
| `/etc/nginx/conf.d/openapi.rrrosmontis.icu.conf` | 统一入口站点(`proxy_pass` → `127.0.0.1:9008`;含按 IP 限流,见下) |
|
||||
| `/etc/nginx/conf.d/00-limits.conf` | 限流 zone 定义(`perip` 连接数 / `perip_req` 请求速率)+ 429 状态码 |
|
||||
| `/etc/nginx/conf.d/00-websocket.conf` | `$connection_upgrade` map 定义(**必须存在**) |
|
||||
| `/home/openapi/nginx/**` | 宝塔导出的历史存档,非生效路径 |
|
||||
|
||||
**按 IP 限流(2026-09-23 启用,轻防滥用档)**:域名 DNS 直连本机(无 CDN),`$binary_remote_addr` 即真实客户端 IP,后端全绑 `127.0.0.1` 使 nginx 成为唯一入口、限流无死角。参数:`limit_req_zone ... rate=10r/s` + `burst=20 nodelay`,`limit_conn perip 10`;超出返回 **429**。验证:30 并发请求中部分被拒(429),桶恢复后正常。调整档位改 `/etc/nginx/conf.d/00-limits.conf`(速率)与站点 conf(burst/并发)后 `nginx -t && systemctl reload nginx`。
|
||||
|
||||
## Python 环境
|
||||
|
||||
- `/home/openapi/env/general` —— 三服务共用(flask / waitress / aiohttp / httpx / yt-dlp)
|
||||
- `/home/openapi/env/general` —— 五服务共用(flask / waitress / aiohttp / httpx / yt-dlp / jmcomic / pillow)
|
||||
- `/home/openapi/env/env_general.tar.gz` —— 环境整包(迁移用)
|
||||
- 注意:pixiv 现由 **waitress** 承载(`pip install waitress`,纯 Python 线程池),不再使用 Flask 开发服务器
|
||||
(开发服务器是 thread-per-request 无上限,受控线程池更适合长期运行)
|
||||
|
||||
## 迁移到新服务器
|
||||
|
||||
1. **环境**:解压 `env/env_general.tar.gz`,直接使用其 `bin/python`(依赖齐全,含 waitress)
|
||||
2. **源码**:恢复 `source/{main.py,pixiv/,bilibili/}` 到 `/home/openapi/source/`
|
||||
1. **环境**:解压 `env/env_general.tar.gz`,直接使用其 `bin/python`(依赖齐全,含 waitress);若整包过旧需补 `pip install jmcomic`;另装系统包 `ffmpeg`(yt-dlp 合流)
|
||||
2. **源码**:恢复 `source/{main.py,pixiv/,bilibili/,dufe/,jm/}` 到 `/home/openapi/source/`
|
||||
3. **systemd**:恢复 `systemd/*.service` 到 `/etc/systemd/system/`,`daemon-reload` + `enable` + `start`
|
||||
4. **nginx**:套用 `conf.d/openapi.rrrosmontis.icu.conf`(并确保 `00-websocket.conf` 的 map 存在)
|
||||
4. **nginx**:先建缓存目录 `mkdir -p /var/cache/nginx`(站点 conf 的 `proxy_cache_path` 需要父目录存在,否则 `nginx -t` 报 mkdir 失败),再套用 `nginx/` 下三个文件到 `/etc/nginx/conf.d/`:`openapi.rrrosmontis.icu.conf` + `00-websocket.conf`(`$connection_upgrade` map,**必须**)+ `00-limits.conf`(限流 zone,**必须**,否则站点 conf 的 `limit_req zone=perip_req` 会使 `nginx -t` 失败),再 `nginx -t && systemctl reload nginx`
|
||||
5. **SSL**:恢复 `cert/<域名>/` 到 `/etc/nginx/cert/<域名>/`
|
||||
6. **凭据**:放回 `source/pixiv/tokens.json`(600) 与 `source/bilibili/cookies.txt`
|
||||
7. **软链**:`ln -s /home/openapi/source/cache/pixiv/images /home/images`
|
||||
@@ -98,19 +121,69 @@
|
||||
```bash
|
||||
curl -k "https://openapi.rrrosmontis.icu/pixiv/get_image?id=xxx"
|
||||
curl -k "https://openapi.rrrosmontis.icu/bilibili/get_video?bv=BV16a41157Q4"
|
||||
curl -k "https://openapi.rrrosmontis.icu/dufe/ginkgostu_sign_in" # 无参应返回 400 JSON
|
||||
curl -k "https://openapi.rrrosmontis.icu/jm/get_comic?id=350234" -o jm.zip # 返回 ZIP
|
||||
```
|
||||
|
||||
## 日志
|
||||
|
||||
```
|
||||
/var/log/openapi.log /var/log/pixiv.log /var/log/bilibili.log
|
||||
/var/log/bilibili_refresh.log /var/log/pixiv_token_refresh.log
|
||||
/var/log/ginkgo_sign_in.log /var/log/bilibili_refresh.log /var/log/pixiv_token_refresh.log
|
||||
/var/log/jm.log
|
||||
```
|
||||
|
||||
缓存命中/落盘会在 `bilibili.log` 打印 `cache HIT / MISS / STORE`(已加 `flush`,实时可见)。
|
||||
|
||||
## 变更记录
|
||||
|
||||
**2026-10-22(服务器重装重建 + bilibili 出口 WAF 修复)**
|
||||
|
||||
- hk 重装为 Ubuntu 22.04,按本文件从零重建:nginx + acme.sh(Let's Encrypt ECC)+ Python venv + 7 个 systemd 单元 + 凭据恢复;补充恢复时的两个坑位:
|
||||
- `proxy_cache_path /var/cache/nginx/...` 要求父目录存在 → 迁移步骤补 `mkdir -p /var/cache/nginx`
|
||||
- `yt-dlp` 合流需要系统包 `ffmpeg`
|
||||
- **bilibili 修复**:hk 出口访问 `www.bilibili.com` 视频页被 WAF 下发 412/JS 挑战(浏览器 UA 触发,API 客户端 UA 可过),页面无 `__INITIAL_STATE__`,yt-dlp 页面提取必然失败;改为**官方 API 直取**(`x/web-interface/view` → `x/player/playurl` → 媒体段下载),yt-dlp 保留后备;实测公网 200/1.3MB/6.5s
|
||||
- jm/dufe/pixiv/openapi 重建后实测正常;证书由 acme.sh 自动续期(cron 每 6h 检查)
|
||||
|
||||
**2026-09-23(仓库与线上一致性核对 + 完整性补齐)**
|
||||
|
||||
- 逐文件 md5 核对:源码/main.py、systemd 全部 7 个单元、ginkgo/jm/bilibili/refresh 各模块、站点 nginx conf 均与仓库一致
|
||||
- **补录漂移**:`source/pixiv/pixiv.py` 线上版为 2026-09-17 的"图源错误码处理"增强(官方 API 404/401/403/429 分别返回明确错误、`fetch_illust_url` 返回 api_status、不再对确定性错误做无意义备用源重试),此前未入库;本次以线上版为准补录
|
||||
- **仓库补齐恢复依赖**:新增 `nginx/00-limits.conf`(限流 zone,站点 conf 引用 `perip_req`/`perip`,缺失会使 `nginx -t` 失败)与 `nginx/00-websocket.conf`(`$connection_upgrade` map,同样必须);迁移步骤第 4 步已同步
|
||||
- 凭据快照另存于本机备份目录(`tokens.json`、`cookies.txt`,0600),不入库
|
||||
|
||||
**2026-09-23(dufe mode=auto + bot 基准同步)**
|
||||
|
||||
- dufe 签到服务新增 **`&mode=auto`**(url 参数可省略):SSO 登录一次后复用登录态 → 拉取通知列表(`GET /student/message/getMessageAll` pageSize=20/readFlag=0 + `POST /student/busiMessage/list`)→ 按 bot ginkgo 同款规则发现活动(仅互动消息、文本含"签到"、jumpUrl 可解析为 `/course/detail/.../signin/<id>/<type>`,query 不入库,按 signInId 去重)→ 按时间序逐个自动签到
|
||||
- **连续重试**:每活动首次 + 最多 5 次重试、间隔 3 秒(`MAX_SIGNIN_ATTEMPTS=6` / `SIGNIN_BACKOFF=(3,3,3,3,3)`,对齐 bot);上游错误/登录错时重登录一次立即重试(对齐 bot `_call_sign_in_local` 的 refresh 语义);**除限流外任何未确认成功都消耗重试次数**(对齐 bot `complete_signin`);限流即中断本轮,已完成结果照常返回(`data.rate_limited=true`)
|
||||
- **结果聚合**:`data.results` 列表逐活动返回(activityKey/signInId/type/typeName/title/signInCode/status/success/message/attempts),另附 total/succeeded/failed/activities 计数;**`attempts` 为逐次完整记录列表**(每次尝试 `{"n", "success", "message"}`,relogin 后立即重试的那次带 `"relogin": true`;成功与重试用尽两种情况都携带全部尝试)
|
||||
- **与 bot ginkgo 非签到码签到逻辑比对后以 bot 为基准同步**:
|
||||
- 移植限流防护体系:`RateLimitedError` / `_RATE_MESSAGE` / `_RATE_CODES` / `is_rate_limited` / `_valid_retry_after` / `_LoginErrors` / `check_response` / `safe_error` / `_request`(原实现完全无限流检测)
|
||||
- `parse_course_url`:courseId 由 `\d+` 放宽为 `[0-9A-Za-z]+`,报错文案对齐
|
||||
- `commit_sign_in`:经 `_request` 包装(`check_response`),失败消息 "request to InTheSign failed: %s" % safe_error(e)(原为 "upstream request failed")
|
||||
- `fetch_sign_in_info`:getSignInInfo 响应过 `check_response`;新增 `sess`/`token` 参数注入(auto 模式请求内复用登录态,省去每活动一次 SSO);补齐 "Malformed getSignInInfo response" 非 dict 校验
|
||||
- 验证:免凭据单测(发现/过滤正反用例)+ 假凭据 401 线路测试 + bj 真账密**只读**联调(登录 OK,notices=20/busi=18,发现 12 个活动含 1 个普通签到,类型/标题解析正确);未做真实提交(避免与运行中的 bot 轮询交叉签到)
|
||||
|
||||
**2026-09-23(早些)**
|
||||
|
||||
- 新增 **jm 漫画下载打包服务**:`source/jm/jm.py` + `jm.service`(`127.0.0.1:9004`,aiohttp + jmcomic,端点 `GET /jm/get_comic?id=xxx[&range=起-止]`)
|
||||
- 移植自 qqbot 的 jm 模块(bj `C:/call/bot/utility/jm.py`):保留章节范围解析 / 文件名清洗 / webp→jpg 打包逻辑;去掉 QQ 回复、代理轮换(hk 可直连源站)、上传 cf 文件服务器三部分,改为 HTTP 直返 ZIP
|
||||
- ZIP 为临时产物:返回后 600 秒延时删除(`asyncio` 延时任务 + 每次请求 `_cache_sweep()` 兜底清理,防进程重启残留);TTL 窗口内同名请求直接复用
|
||||
- 单次上限 30 章(`MAX_CHAPTERS`,与 qqbot 一致;也避免超过聚合层 `proxy()` 的 300s 超时)
|
||||
- 在聚合层 `main.py` 注册该端点:新增 `JM_BACKEND`,复用既有 `proxy()`(固定 GET),零改动既有行为
|
||||
- nginx **无需改动**:`location /` 兜底已把 `/jm/...` 转发给 `127.0.0.1:9008`(与 dufe 相同)
|
||||
- `env/general` 新增依赖 `jmcomic`(2.7.7,`pip install jmcomic`)
|
||||
|
||||
**2026-09-13**
|
||||
|
||||
- 新增 **dufe 银杏签到码查询服务**:`source/dufe/ginkgostu_sign_in/ginkgo_sign_in.py` + `ginkgo_sign_in.service`(`127.0.0.1:9005`,Flask + waitress 16 线程,端点 `GET|POST /dufe/ginkgostu_sign_in`)
|
||||
- 在聚合层 `main.py` 注册该端点:新增 `DUFE_BACKEND` 与 `proxy_forward()`(保留请求方法/请求体;原 `proxy()` 固定 GET 行为未改动)
|
||||
- nginx **无需改动**:`location /` 兜底已把 `/dufe/...` 转发给 `127.0.0.1:9008`
|
||||
- 修复 `ginkgo_sign_in.py` 两处缺陷:
|
||||
- `_b64_decode` 先做 `urllib.parse.unquote`(iOS 快捷指令会把 base64 的 `=` 编码成 `%3D`,旧实现直接 `binascii.Error`);顺序为先 unquote、再 `" "→"+"`
|
||||
- `commit` 判定由 fail-open 改为 fail-closed:须 `isinstance(dict)` 且 `success is True`,否则一律判失败并返回 `msg = "sign-in failed: <原因>"`
|
||||
- `parse_course_url` 报错文案改为 `URL is not a sign-in page (missing /signin/<signInId>/<type>): <url>`
|
||||
|
||||
**2026-09-12**
|
||||
|
||||
- 端口重排 `9888 / 9088 / 19990 → 9008 / 9007 / 9006`(全部仅 `127.0.0.1`)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
limit_conn_zone $binary_remote_addr zone=perip:10m;
|
||||
limit_conn_zone $server_name zone=perserver:10m;
|
||||
|
||||
# 2026-09-23 新增:按 IP 请求速率限制(轻防滥用档)
|
||||
# openapi 域名 DNS 直连本机(无 CDN),$binary_remote_addr 即真实客户端 IP
|
||||
limit_req_zone $binary_remote_addr zone=perip_req:10m rate=10r/s;
|
||||
limit_req_status 429;
|
||||
limit_conn_status 429;
|
||||
@@ -0,0 +1,4 @@
|
||||
map $http_upgrade $connection_upgrade {
|
||||
default upgrade;
|
||||
'' close;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
# openapi.rrrosmontis.icu 统一入口(原 api/pixiv/apixiv.bu4.ren 合并)
|
||||
# /pixiv/ /bilibili/ -> openapi 聚合层 127.0.0.1:9888 (main.py)
|
||||
# /pixiv/ /bilibili/ -> openapi 聚合层 127.0.0.1:9008 (main.py)
|
||||
# /img-master/ /img-original/ -> i.pximg.net 图片反代(原 apixiv.bu4.ren 逻辑,方案二)
|
||||
# 2026-09-02 补 /img-original/:App API 原图 URL 为 /img-original/ 前缀,此前未配置导致 404
|
||||
|
||||
@@ -31,6 +31,10 @@ server {
|
||||
add_header Strict-Transport-Security "max-age=31536000";
|
||||
error_page 497 https://$host$request_uri;
|
||||
|
||||
# ---- 按 IP 限流(轻防滥用档,2026-09-23):10r/s burst 20、并发连接 10,超出 429 ----
|
||||
limit_req zone=perip_req burst=20 nodelay;
|
||||
limit_conn perip 10;
|
||||
|
||||
resolver 8.8.8.8 1.1.1.1 ipv6=off valid=300s;
|
||||
|
||||
# ---- 图片反代 i.pximg.net(原 apixiv.bu4.ren 逻辑,路径前缀 /img-master/)----
|
||||
@@ -85,7 +89,7 @@ server {
|
||||
|
||||
# ---- pixiv(openapi 聚合层 /pixiv/get_image)----
|
||||
location ^~ /pixiv/ {
|
||||
proxy_pass http://127.0.0.1:9888;
|
||||
proxy_pass http://127.0.0.1:9008;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Real-Port $remote_port;
|
||||
@@ -104,7 +108,7 @@ server {
|
||||
|
||||
# ---- bilibili(openapi 聚合层 /bilibili/get_video)----
|
||||
location ^~ /bilibili/ {
|
||||
proxy_pass http://127.0.0.1:9888;
|
||||
proxy_pass http://127.0.0.1:9008;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Real-Port $remote_port;
|
||||
@@ -121,9 +125,9 @@ server {
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
}
|
||||
|
||||
# ---- 其余(根路径 / )→ 9888(main.py:跳转文章页)----
|
||||
# ---- 其余(根路径 / )→ 9008(main.py:跳转文章页)----
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:9888;
|
||||
proxy_pass http://127.0.0.1:9008;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Real-Port $remote_port;
|
||||
|
||||
@@ -14,6 +14,14 @@ from aiohttp import web
|
||||
COOKIE_FILE = str(Path(__file__).parent / "cookies.txt")
|
||||
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
|
||||
|
||||
# 2026-10-22: hk 出口被 bilibili WAF 下发 412 / JS 挑战页(浏览器 UA 触发),
|
||||
# 页面里没有可用的 __INITIAL_STATE__,yt-dlp 的页面提取必然失败。
|
||||
# 实测 API 客户端 UA(yt-dlp/x、curl/x)可通过,故主路径改为官方 API 直取;
|
||||
# yt-dlp 保留为后备(其他出口/未来 WAF 策略变化时仍可工作)。
|
||||
API_UA = "yt-dlp/2026.10.21"
|
||||
VIEW_API = "https://api.bilibili.com/x/web-interface/view"
|
||||
PLAYURL_API = "https://api.bilibili.com/x/player/playurl"
|
||||
|
||||
# ---------------- 本地缓存(与 pixiv 同级:source/cache/bilibili) ----------------
|
||||
CACHE_DIR = (Path(__file__).resolve().parent / "../cache/bilibili").resolve()
|
||||
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
@@ -144,6 +152,60 @@ def decode_url(url_b64):
|
||||
return base64.b64decode(url_b64.encode()).decode()
|
||||
|
||||
|
||||
def _api_headers():
|
||||
return {"User-Agent": API_UA, "Referer": "https://www.bilibili.com/"}
|
||||
|
||||
|
||||
def _api_get(url, params, what="bilibili api"):
|
||||
import requests
|
||||
r = requests.get(url, params=params, headers=_api_headers(), timeout=30)
|
||||
r.raise_for_status()
|
||||
j = r.json()
|
||||
if not isinstance(j, dict) or j.get("code") != 0:
|
||||
raise RuntimeError("%s failed: code=%s message=%s" % (
|
||||
what, (j or {}).get("code"), (j or {}).get("message")))
|
||||
return j.get("data") or {}
|
||||
|
||||
|
||||
def _download_via_api(bvid, work_dir):
|
||||
"""官方 API 直取(view -> playurl -> 媒体段),返回落盘路径。
|
||||
|
||||
hk 出口被页面 WAF 挑战时 yt-dlp 无法工作,此路径只走 api.bilibili.com
|
||||
与媒体 CDN,实测不受该挑战影响。"""
|
||||
import requests
|
||||
|
||||
data = _api_get(VIEW_API, {"bvid": bvid}, "view")
|
||||
cid = data.get("cid")
|
||||
if not cid:
|
||||
raise RuntimeError("view api returned no cid")
|
||||
play = _api_get(PLAYURL_API, {
|
||||
"bvid": bvid, "cid": cid, "qn": 80, "fnval": 0, "fourk": 1,
|
||||
}, "playurl")
|
||||
durls = play.get("durl") or []
|
||||
if not durls:
|
||||
raise RuntimeError("playurl api returned no durl segments")
|
||||
|
||||
out = Path(work_dir) / f"{bvid}.mp4"
|
||||
try:
|
||||
with open(out, "wb") as f:
|
||||
for seg in durls:
|
||||
with requests.get(seg["url"], headers=_api_headers(),
|
||||
stream=True, timeout=120) as r:
|
||||
r.raise_for_status()
|
||||
for chunk in r.iter_content(chunk_size=256 * 1024):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
if out.stat().st_size == 0:
|
||||
raise RuntimeError("empty media file")
|
||||
return out
|
||||
except Exception:
|
||||
try:
|
||||
out.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
|
||||
|
||||
async def get_video(url_b64=None, bv=None):
|
||||
if bv:
|
||||
bvid = bv
|
||||
@@ -170,13 +232,29 @@ async def get_video(url_b64=None, bv=None):
|
||||
print(f"[bilibili] cache HIT {bvid} -> {cached.name} ({cached.stat().st_size} B)", flush=True)
|
||||
return _file_response(cached, bvid)
|
||||
|
||||
cookie_tmp = _prepare_cookie_file()
|
||||
if not cookie_tmp:
|
||||
return web.json_response({"error": "missing bilibili cookie"}, status=500)
|
||||
|
||||
print(f"[bilibili] cache MISS {bvid}, downloading...", flush=True)
|
||||
# 2) 下载到缓存目录内的隐藏临时目录(同盘,才能原子 rename 落盘)
|
||||
tmpdir = tempfile.mkdtemp(prefix=".dl-", dir=CACHE_DIR)
|
||||
cookie_tmp = None
|
||||
try:
|
||||
# 2a) 主路径:官方 API 直取(绕开页面 WAF,不依赖 cookie/yt-dlp)
|
||||
video_path = None
|
||||
api_err = None
|
||||
try:
|
||||
video_path = await asyncio.to_thread(_download_via_api, bvid, tmpdir)
|
||||
print(f"[bilibili] api download OK {bvid}", flush=True)
|
||||
except Exception as e:
|
||||
api_err = e
|
||||
print(f"[bilibili] api download failed {bvid}: {e}; fallback yt-dlp", flush=True)
|
||||
|
||||
# 2b) 后备:yt-dlp(需要 cookie 文件)
|
||||
if video_path is None:
|
||||
cookie_tmp = _prepare_cookie_file()
|
||||
if not cookie_tmp:
|
||||
return web.json_response(
|
||||
{"error": "download failed", "detail": "api path failed and no cookie for yt-dlp fallback: %s" % api_err},
|
||||
status=502,
|
||||
)
|
||||
output_template = os.path.join(tmpdir, "video.%(ext)s")
|
||||
cmd = [
|
||||
os.path.join(os.path.dirname(sys.executable), "yt-dlp"),
|
||||
@@ -188,7 +266,6 @@ async def get_video(url_b64=None, bv=None):
|
||||
"-o", output_template,
|
||||
f"https://www.bilibili.com/video/{bvid}",
|
||||
]
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
@@ -205,7 +282,7 @@ async def get_video(url_b64=None, bv=None):
|
||||
if proc.returncode != 0 and not video_files:
|
||||
err_msg = stderr.decode()[-1000:] if stderr else "unknown error"
|
||||
return web.json_response(
|
||||
{"error": "download failed", "detail": err_msg},
|
||||
{"error": "download failed", "detail": "api: %s | yt-dlp: %s" % (api_err, err_msg)},
|
||||
status=502,
|
||||
)
|
||||
|
||||
@@ -213,6 +290,7 @@ async def get_video(url_b64=None, bv=None):
|
||||
return web.json_response({"error": "no output file"}, status=502)
|
||||
|
||||
video_path = max(video_files, key=lambda p: p.stat().st_size)
|
||||
|
||||
ext = video_path.suffix.lower()
|
||||
final = CACHE_DIR / f"{bvid}{ext}"
|
||||
try:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+280
@@ -0,0 +1,280 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# jm.py — JM 漫画下载 API 模块
|
||||
# 流程: 源站下载 -> 本地临时目录打包 ZIP -> HTTP 返回 -> 延时删除
|
||||
# 移植自 qqbot 的 jm 模块(bj C:/call/bot/utility/jm.py),
|
||||
# 去掉 QQ 机器人 / 代理轮换 / 上传 cf 逻辑,改为与 pixiv/bilibili 一致的 HTTP 直返模式。
|
||||
# hk 服务器可直连源站,无需代理。
|
||||
|
||||
import asyncio
|
||||
import shutil
|
||||
import tempfile
|
||||
import time
|
||||
import unicodedata
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
# ---------------- 本地临时缓存(与 pixiv/bilibili 同级:source/cache/jm) ----------------
|
||||
CACHE_DIR = (Path(__file__).resolve().parent / "../cache/jm").resolve()
|
||||
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
MAX_CHAPTERS = 30 # 单次最多章节(与 qqbot 一致,同时避免超过聚合层 300s 超时)
|
||||
ZIP_TTL_SECONDS = 600 # ZIP 返回后保留 10 分钟再删除
|
||||
PORT = 9004
|
||||
|
||||
_image_exts = (".webp", ".jpg", ".png")
|
||||
|
||||
# 同一本子防并发(album_id -> asyncio.Lock)
|
||||
_locks = {}
|
||||
_locks_guard = asyncio.Lock()
|
||||
|
||||
|
||||
def _sanitize_filename(name):
|
||||
"""清理文件名非法字符,保留中英文/数字/常见符号"""
|
||||
cleaned = unicodedata.normalize("NFKC", name or "")
|
||||
result = []
|
||||
for ch in cleaned:
|
||||
if ch.isalnum() or ch in ("_", "-", ".", "(", ")", "[", "]", "(", ")", "【", "】"):
|
||||
result.append(ch)
|
||||
elif ch.isspace():
|
||||
result.append("_")
|
||||
else:
|
||||
result.append("-")
|
||||
s = "".join(result)
|
||||
while "__" in s:
|
||||
s = s.replace("__", "_")
|
||||
while "--" in s:
|
||||
s = s.replace("--", "-")
|
||||
return s[:80].strip("_-") or "unnamed"
|
||||
|
||||
|
||||
def _zip_files():
|
||||
return [f for f in CACHE_DIR.glob("*.zip") if f.is_file()]
|
||||
|
||||
|
||||
def _cache_sweep():
|
||||
"""兜底清理:进程重启后残留的过期 ZIP(mtime 超过 TTL 即删)"""
|
||||
now = time.time()
|
||||
for f in _zip_files():
|
||||
try:
|
||||
if now - f.stat().st_mtime > ZIP_TTL_SECONDS:
|
||||
f.unlink()
|
||||
print(f"[jm] sweep expired zip: {f.name}", flush=True)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
async def _delete_later(path, delay):
|
||||
"""返回响应后延时删除 ZIP"""
|
||||
await asyncio.sleep(delay)
|
||||
try:
|
||||
Path(path).unlink(missing_ok=True)
|
||||
print(f"[jm] ttl delete: {Path(path).name}", flush=True)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
class _ParamError(RuntimeError):
|
||||
"""参数类错误,返回 400"""
|
||||
|
||||
|
||||
def _resolve_album_plan(album_id, range_start, range_end):
|
||||
"""获取元数据并确定章节范围与 ZIP 文件名。
|
||||
返回 (title, episodes, range_start, range_end, zip_name),失败抛异常"""
|
||||
from jmcomic import JmModuleConfig
|
||||
|
||||
option = JmModuleConfig.option_class().default()
|
||||
client = option.build_jm_client()
|
||||
album = client.get_album_detail(int(album_id))
|
||||
title = getattr(album, "title", "") or str(album_id)
|
||||
episodes = getattr(album, "episode_list", []) or []
|
||||
total = len(episodes)
|
||||
if total == 0:
|
||||
raise RuntimeError("未找到任何章节")
|
||||
|
||||
if range_start is None:
|
||||
if total > MAX_CHAPTERS:
|
||||
raise _ParamError(
|
||||
f"共 {total} 章, 超过单次上限 {MAX_CHAPTERS} 章, 请用 range 参数分段下载 (如 range=1-{MAX_CHAPTERS})"
|
||||
)
|
||||
range_start, range_end = 1, total
|
||||
else:
|
||||
if range_start < 1 or range_end < range_start:
|
||||
raise _ParamError("章节范围不合法")
|
||||
if range_end - range_start + 1 > MAX_CHAPTERS:
|
||||
raise _ParamError(f"章节跨度超过 {MAX_CHAPTERS} 章, 请缩小范围")
|
||||
if range_start > total:
|
||||
raise _ParamError(f"起始章节超出总章节数 {total}")
|
||||
range_end = min(range_end, total)
|
||||
|
||||
zip_name = f"{_sanitize_filename(title)}_{album_id}_ch{range_start}-{range_end}.zip"
|
||||
return title, episodes, range_start, range_end, zip_name
|
||||
|
||||
|
||||
def _download_and_pack(album_id, plan):
|
||||
"""下载并打包 ZIP。成功返回 (zip_path, image_count, ok_ch, fail_ch),失败抛异常"""
|
||||
from jmcomic import JmModuleConfig
|
||||
from PIL import Image
|
||||
|
||||
title, episodes, range_start, range_end, zip_name = plan
|
||||
|
||||
work_dir = Path(tempfile.mkdtemp(prefix=f".dl-{album_id}-", dir=CACHE_DIR))
|
||||
try:
|
||||
# 逐章下载(hk 直连源站,无需代理)
|
||||
ok_count = 0
|
||||
fail_count = 0
|
||||
for idx in range(range_start - 1, range_end):
|
||||
photo_id = episodes[idx][0]
|
||||
try:
|
||||
dl_option = JmModuleConfig.option_class().construct({
|
||||
"dir_rule": {"rule": "Bd_Pid", "base_dir": str(work_dir)},
|
||||
"download": {"image": {"suffix": ".webp"}},
|
||||
})
|
||||
dler = JmModuleConfig.downloader_class()(dl_option)
|
||||
dler.download_photo(int(photo_id))
|
||||
ok_count += 1
|
||||
print(f"[jm] 章节下载完成: {album_id}/{photo_id}", flush=True)
|
||||
except Exception as e:
|
||||
fail_count += 1
|
||||
print(f"[jm] 章节下载失败: {album_id}/{photo_id}: {e}", flush=True)
|
||||
|
||||
# 收集图片
|
||||
images = []
|
||||
for ext in _image_exts:
|
||||
images.extend(sorted(work_dir.rglob(f"*{ext}")))
|
||||
if not images:
|
||||
raise RuntimeError("未下载到任何图片")
|
||||
|
||||
# 打包 ZIP (webp 统一转 jpg, 无密码)
|
||||
zip_path = CACHE_DIR / zip_name
|
||||
tmp_dir = Path(tempfile.mkdtemp(prefix=".pack-", dir=CACHE_DIR))
|
||||
try:
|
||||
packed = []
|
||||
for i, p in enumerate(images, 1):
|
||||
out = tmp_dir / f"{i:04d}.jpg"
|
||||
try:
|
||||
img = Image.open(str(p))
|
||||
if img.mode != "RGB":
|
||||
img = img.convert("RGB")
|
||||
img.save(str(out), "JPEG", quality=95)
|
||||
except Exception:
|
||||
out = tmp_dir / f"{i:04d}{p.suffix.lower()}"
|
||||
shutil.copyfile(str(p), str(out))
|
||||
packed.append(out)
|
||||
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
for j in packed:
|
||||
zf.write(j, j.name)
|
||||
finally:
|
||||
shutil.rmtree(tmp_dir, ignore_errors=True)
|
||||
|
||||
print(f"[jm] 打包完成: {zip_name} ({len(images)} 张, 成功 {ok_count} 章 / 失败 {fail_count} 章)", flush=True)
|
||||
return zip_path, len(images), ok_count, fail_count
|
||||
finally:
|
||||
# 无论成功/失败都回收图片临时目录
|
||||
shutil.rmtree(work_dir, ignore_errors=True)
|
||||
|
||||
|
||||
def _file_response(zip_path):
|
||||
return web.FileResponse(
|
||||
path=zip_path,
|
||||
headers={
|
||||
"Content-Type": "application/zip",
|
||||
"Content-Disposition": f'attachment; filename="{zip_path.name}"',
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def handle_jm(request):
|
||||
album_id = request.query.get("id", "").strip()
|
||||
if not album_id or not album_id.isdigit():
|
||||
return web.json_response({"error": "missing or invalid id parameter"}, status=400)
|
||||
|
||||
range_start = range_end = None
|
||||
range_str = request.query.get("range", "").strip()
|
||||
if range_str:
|
||||
import re
|
||||
m = re.fullmatch(r"(\d{1,4})-(\d{1,4})", range_str)
|
||||
if not m:
|
||||
return web.json_response(
|
||||
{"error": "invalid range format, expected: start-end (e.g. 1-30)"}, status=400
|
||||
)
|
||||
range_start, range_end = int(m.group(1)), int(m.group(2))
|
||||
|
||||
_cache_sweep()
|
||||
|
||||
# 同本子串行,防止并发重复下载
|
||||
async with _locks_guard:
|
||||
lock = _locks.setdefault(album_id, asyncio.Lock())
|
||||
if lock.locked():
|
||||
return web.json_response(
|
||||
{"error": "same album is being processed, please retry later"}, status=409
|
||||
)
|
||||
|
||||
async with lock:
|
||||
try:
|
||||
plan = await asyncio.to_thread(
|
||||
_resolve_album_plan, album_id, range_start, range_end
|
||||
)
|
||||
except _ParamError as e:
|
||||
return web.json_response({"error": str(e)}, status=400)
|
||||
except Exception as e:
|
||||
return web.json_response(
|
||||
{"error": "failed to fetch album info", "detail": str(e)[:200]}, status=502
|
||||
)
|
||||
|
||||
zip_name = plan[4]
|
||||
existing = CACHE_DIR / zip_name
|
||||
if existing.exists():
|
||||
# TTL 窗口内重复请求:直接复用并重新计时
|
||||
try:
|
||||
existing.touch()
|
||||
except OSError:
|
||||
pass
|
||||
print(f"[jm] reuse zip: {zip_name}", flush=True)
|
||||
asyncio.create_task(_delete_later(existing, ZIP_TTL_SECONDS))
|
||||
return _file_response(existing)
|
||||
|
||||
try:
|
||||
zip_path, img_count, ok_c, fail_c = await asyncio.to_thread(
|
||||
_download_and_pack, album_id, plan
|
||||
)
|
||||
except Exception as e:
|
||||
return web.json_response(
|
||||
{"error": "download or pack failed", "detail": str(e)[:200]}, status=502
|
||||
)
|
||||
|
||||
asyncio.create_task(_delete_later(zip_path, ZIP_TTL_SECONDS))
|
||||
return _file_response(zip_path)
|
||||
|
||||
|
||||
async def handle_cache_info(request):
|
||||
files = []
|
||||
total = 0
|
||||
for f in _zip_files():
|
||||
try:
|
||||
st = f.stat()
|
||||
except OSError:
|
||||
continue
|
||||
files.append({"name": f.name, "size": st.st_size, "mtime": int(st.st_mtime)})
|
||||
total += st.st_size
|
||||
return web.json_response(
|
||||
{
|
||||
"cache_dir": str(CACHE_DIR),
|
||||
"zip_ttl_seconds": ZIP_TTL_SECONDS,
|
||||
"max_chapters": MAX_CHAPTERS,
|
||||
"count": len(files),
|
||||
"total_bytes": total,
|
||||
"free_bytes": shutil.disk_usage(CACHE_DIR).free,
|
||||
"files": sorted(files, key=lambda x: -x["size"]),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
app = web.Application()
|
||||
app.router.add_get("/jm/get_comic", handle_jm)
|
||||
app.router.add_get("/jm/cache_info", handle_cache_info)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Cache directory: {CACHE_DIR}")
|
||||
web.run_app(app, host="127.0.0.1", port=PORT)
|
||||
+55
-3
@@ -1,8 +1,10 @@
|
||||
from aiohttp import web, ClientSession, ClientTimeout
|
||||
|
||||
|
||||
PIXIV_BACKEND = "http://127.0.0.1:9088"
|
||||
BILIBILI_BACKEND = "http://127.0.0.1:19990"
|
||||
PIXIV_BACKEND = "http://127.0.0.1:9007"
|
||||
BILIBILI_BACKEND = "http://127.0.0.1:9006"
|
||||
DUFE_BACKEND = "http://127.0.0.1:9005"
|
||||
JM_BACKEND = "http://127.0.0.1:9004"
|
||||
|
||||
|
||||
async def handle_pixiv(request):
|
||||
@@ -21,6 +23,22 @@ async def handle_bilibili(request):
|
||||
return await proxy(request, target)
|
||||
|
||||
|
||||
async def handle_dufe_sign_in(request):
|
||||
# ginkgo_sign_in.service(127.0.0.1:9005),端点 /dufe/ginkgostu_sign_in,支持 GET|POST
|
||||
target = f"{DUFE_BACKEND}/dufe/ginkgostu_sign_in"
|
||||
if request.query_string:
|
||||
target += "?" + request.query_string
|
||||
return await proxy_forward(request, target)
|
||||
|
||||
|
||||
async def handle_jm(request):
|
||||
qs = request.query_string
|
||||
target = f"{JM_BACKEND}/jm/get_comic"
|
||||
if qs:
|
||||
target += "?" + qs
|
||||
return await proxy(request, target)
|
||||
|
||||
|
||||
async def proxy(request, target):
|
||||
timeout = ClientTimeout(total=300)
|
||||
async with ClientSession(timeout=timeout) as session:
|
||||
@@ -42,6 +60,37 @@ async def proxy(request, target):
|
||||
)
|
||||
|
||||
|
||||
async def proxy_forward(request, target):
|
||||
# 保留请求方法与请求体的转发,供需要 POST 的后端使用(dufe 签到码查询)
|
||||
timeout = ClientTimeout(total=300)
|
||||
data = None
|
||||
fwd_headers = {}
|
||||
if request.method not in ("GET", "HEAD"):
|
||||
data = await request.read()
|
||||
ctype = request.headers.get("Content-Type")
|
||||
if ctype:
|
||||
fwd_headers["Content-Type"] = ctype
|
||||
async with ClientSession(timeout=timeout) as session:
|
||||
try:
|
||||
async with session.request(
|
||||
request.method, target, data=data, headers=fwd_headers
|
||||
) as resp:
|
||||
body = await resp.read()
|
||||
headers = {}
|
||||
for key in ("Content-Type", "Content-Disposition", "Cache-Control"):
|
||||
if key in resp.headers:
|
||||
headers[key] = resp.headers[key]
|
||||
return web.Response(
|
||||
body=body,
|
||||
status=resp.status,
|
||||
headers=headers,
|
||||
)
|
||||
except Exception as e:
|
||||
return web.json_response(
|
||||
{"error": "backend unreachable", "detail": str(e)}, status=502
|
||||
)
|
||||
|
||||
|
||||
async def handle_root(request):
|
||||
raise web.HTTPFound("https://rrrosmontis.icu/articles/index.html?id=00003")
|
||||
|
||||
@@ -50,6 +99,9 @@ app = web.Application()
|
||||
app.router.add_get("/", handle_root)
|
||||
app.router.add_get("/pixiv/get_image", handle_pixiv)
|
||||
app.router.add_get("/bilibili/get_video", handle_bilibili)
|
||||
app.router.add_get("/dufe/ginkgostu_sign_in", handle_dufe_sign_in)
|
||||
app.router.add_post("/dufe/ginkgostu_sign_in", handle_dufe_sign_in)
|
||||
app.router.add_get("/jm/get_comic", handle_jm)
|
||||
|
||||
if __name__ == "__main__":
|
||||
web.run_app(app, host="127.0.0.1", port=9888)
|
||||
web.run_app(app, host="127.0.0.1", port=9008)
|
||||
|
||||
+25
-10
@@ -31,9 +31,11 @@ def get_access_token():
|
||||
|
||||
|
||||
def fetch_illust_url(pid, page):
|
||||
"""返回 (original_url, api_status)。api_status: 200 成功 / 404 作品不存在 /
|
||||
401|403 认证失败 / 429 限流 / 0 网络等临时错误 / -1 数据异常。"""
|
||||
access_token = get_access_token()
|
||||
if not access_token:
|
||||
return None
|
||||
return None, 403
|
||||
try:
|
||||
resp = requests.get(
|
||||
f"https://{API_HOST}/v1/illust/detail",
|
||||
@@ -47,16 +49,17 @@ def fetch_illust_url(pid, page):
|
||||
verify=False
|
||||
)
|
||||
if resp.status_code != 200:
|
||||
return None
|
||||
return None, resp.status_code
|
||||
data = resp.json()
|
||||
if data.get("error"):
|
||||
return None
|
||||
return None, -1
|
||||
illust = data.get("illust")
|
||||
if not illust:
|
||||
return None
|
||||
return None, -1
|
||||
page_count = illust.get("page_count", 1)
|
||||
if page >= page_count:
|
||||
return None
|
||||
# 页码超出:作品存在但请求的分页不存在,按 404 语义处理
|
||||
return None, 404
|
||||
original_url = None
|
||||
if page_count > 1:
|
||||
meta_pages = illust.get("meta_pages")
|
||||
@@ -69,10 +72,12 @@ def fetch_illust_url(pid, page):
|
||||
if not original_url:
|
||||
original_url = illust.get("image_urls", {}).get("large")
|
||||
if not original_url:
|
||||
return None
|
||||
return original_url.replace("i.pximg.net", IMAGE_PROXY_DOMAIN)
|
||||
return None, -1
|
||||
return original_url.replace("i.pximg.net", IMAGE_PROXY_DOMAIN), 200
|
||||
except requests.RequestException:
|
||||
return None, 0 # 网络等临时错误:可继续尝试备用源
|
||||
except Exception:
|
||||
return None
|
||||
return None, -1
|
||||
|
||||
|
||||
def make_re_url(pid, page):
|
||||
@@ -146,9 +151,19 @@ def serve_image(path):
|
||||
return send_file(cached_file, mimetype=mime, max_age=3600)
|
||||
|
||||
urls_to_try = []
|
||||
official_url = fetch_illust_url(pid, page)
|
||||
official_url, api_status = fetch_illust_url(pid, page)
|
||||
if official_url:
|
||||
urls_to_try.append(official_url)
|
||||
# 明确的作品级错误直接返回,不再做无意义的备用源重试:
|
||||
# 404 = 作品不存在/已删除/分页不存在(官方 API 已裁决)
|
||||
# 401/403 = token 失效(refresh 由独立服务负责,这里明确报错)
|
||||
# 429 = pixiv API 限流(备用源同样是 pixiv 数据,重试无意义)
|
||||
if api_status == 404:
|
||||
return jsonify(error="作品不存在或已删除", code="not_found"), 404
|
||||
if api_status in (401, 403):
|
||||
return jsonify(error="pixiv 认证失败,token 可能失效", code="auth_error"), 502
|
||||
if api_status == 429:
|
||||
return jsonify(error="pixiv API 限流,请稍后再试", code="rate_limited"), 503
|
||||
urls_to_try.append(make_re_url(pid, page))
|
||||
urls_to_try.append(make_cat_url(pid, page))
|
||||
|
||||
@@ -157,7 +172,7 @@ def serve_image(path):
|
||||
if img_path:
|
||||
return send_file(img_path, mimetype=mime, max_age=3600)
|
||||
|
||||
return jsonify(error="Failed to fetch image from all sources"), 500
|
||||
return jsonify(error="所有图源均取图失败,请稍后再试", code="all_sources_failed"), 500
|
||||
|
||||
|
||||
@app.route('/health')
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
[Unit]
|
||||
Description=Ginkgo Stu Sign-in Service (dufe)
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Environment=PATH=/home/openapi/env/general/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
Type=simple
|
||||
WorkingDirectory=/home/openapi/source/dufe/ginkgostu_sign_in
|
||||
ExecStart=/home/openapi/env/general/bin/python /home/openapi/source/dufe/ginkgostu_sign_in/ginkgo_sign_in.py
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
StandardOutput=append:/var/log/ginkgo_sign_in.log
|
||||
StandardError=append:/var/log/ginkgo_sign_in.log
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,17 @@
|
||||
[Unit]
|
||||
Description=JM Comic Download API Service
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Environment=PATH=/home/openapi/env/general/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
Type=simple
|
||||
WorkingDirectory=/home/openapi/source/jm
|
||||
ExecStart=/home/openapi/env/general/bin/python /home/openapi/source/jm/jm.py
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
StandardOutput=append:/var/log/jm.log
|
||||
StandardError=append:/var/log/jm.log
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -6,8 +6,8 @@ Wants=network-online.target
|
||||
[Service]
|
||||
Environment=PATH=/home/openapi/env/general/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
Type=simple
|
||||
WorkingDirectory=/home/openapi/source/openapi
|
||||
ExecStart=/home/openapi/env/general/bin/python /home/openapi/source/openapi/main.py
|
||||
WorkingDirectory=/home/openapi/source
|
||||
ExecStart=/home/openapi/env/general/bin/python /home/openapi/source/main.py
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
StandardOutput=append:/var/log/openapi.log
|
||||
|
||||
Reference in New Issue
Block a user