commit 9c5fef1a4d81032718d2d2ea2d1dd6bde2afe198 Author: kunkunxiaomao <2811043066@qq.com> Date: Mon Nov 16 20:45:22 2026 +0800 first commit Co-Authored-By: Claude diff --git a/.env.cloud.example b/.env.cloud.example new file mode 100644 index 0000000..e553cf6 --- /dev/null +++ b/.env.cloud.example @@ -0,0 +1,42 @@ +# Sakura Cloud example config. Copy to .env for local backend development. + +APP_NAME=Sakura Cloud +APP_VERSION=0.1.0 +API_PREFIX=/api/v1 +DEBUG=true + +DATABASE_URL=postgresql+psycopg://sakura:sakura@localhost:5432/sakura_cloud +REDIS_URL=redis://localhost:6379/0 + +STORAGE_ROOT=data/sakura +LOG_DIR=data/sakura/logs + +JWT_SECRET_KEY=change-this-secret-in-production +JWT_ALGORITHM=HS256 +JWT_ACCESS_TOKEN_EXPIRE_HOURS=24 +JWT_REFRESH_TOKEN_EXPIRE_DAYS=30 + +DEFAULT_STORAGE_LIMIT_GB=5 +MAX_UPLOAD_SIZE_MB=100 +MAX_VIDEO_UPLOAD_SIZE_MB=2048 +VIDEO_STORAGE_LIMIT_GB=30 +MAX_BATCH_SIZE_MB=500 +UPLOAD_CHUNK_SIZE=5242880 +TEMP_UPLOAD_TTL_HOURS=24 +THUMBNAIL_MAX_SIZE=640 +THUMBNAIL_QUALITY=82 + +FCM_ENABLED=true +FIREBASE_CREDENTIALS_PATH= +FIREBASE_CREDENTIALS_JSON= + +OCR_ENABLED=false +OCR_LANGUAGE=ch +OCR_MAX_IMAGE_DIMENSION=1800 +OCR_MAX_ATTEMPTS=3 +OCR_MAX_CONCURRENCY=1 + +ADMIN_LOGIN_RATE_LIMIT_PER_MINUTE=5 +ADMIN_FRONTEND_ENABLED=true + +CORS_ORIGINS=["http://localhost:5173","http://localhost:3000"] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f55ca2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +# Python cache and env +__pycache__/ +*.pyc +.venv/ + +# IDE +.idea/ +.vscode/ + +# Local toolchains (SDKs, caches) — keep only scripts in tools/ +tools/android-sdk/ +tools/android-commandlinetools.zip +tools/flutter/ +tools/pub-cache/ +tools/python314/ + +# Build artifacts +build/ +dist/ +release/ +releases/* +!releases/.gitkeep +*.spec +!build_assets/PixivDownloader.spec +!build_assets/SakuraDownloader.spec + +# Runtime and sensitive local data +runtime/ +profiles/ +Pixiv_Downloads/ +Sakura_Downloads/ +data/ +pixiv_app_session.json +pixiv_app_library.db* +.env +.env.* +!.env.cloud.example +firebase-service-account*.json +sakura_mobile/android/app/google-services.json + +# Flutter generated files +sakura_mobile/.dart_tool/ +sakura_mobile/build/ +sakura_mobile/.flutter-plugins +sakura_mobile/.flutter-plugins-dependencies + +# Local project docs and dev logs (not code) +项目文件/ +日志/ + +# AI assistant / debug local files +.claude/ +.chatgpt/ +*.log +debug/ + +# Web frontend generated files +sakura_admin_web/node_modules/ +sakura_admin_web/dist/ +sakura_admin_web/.npm-cache/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..c6b3b7c --- /dev/null +++ b/README.md @@ -0,0 +1,104 @@ +# Sakura Downloader(樱云下载器) + +个人媒体采集、本地图库管理与跨设备云同步系统。 + +| 端 | 角色 | +|----|------| +| Windows 桌面端 | 采集与整理中枢 | +| Sakura Cloud | 数据与账号中心 | +| Android 客户端 | 跨设备阅读与提醒 | + +## 成品文件(`releases/`) + +| 文件 | 说明 | 大小(约) | +|------|------|------------| +| `releases/sakuradownloader.exe` | Windows 桌面端,双击即用 | ~68 MB | +| `releases/app-release.apk` | Android 客户端(调试签名,适合自测) | ~54 MB | + +构建产物来源: + +- EXE:`dist/sakuradownloader.exe` → 复制到 `releases/` +- APK:`sakura_mobile/build/app/outputs/flutter-apk/app-release.apk` → 复制到 `releases/` + +## 文档索引 + +| 文档 | 面向读者 | 内容 | +|------|----------|------| +| [使用说明-桌面版与安卓版.md](使用说明-桌面版与安卓版.md) | 终端用户 | 登录、下载、云同步、手机端操作 | +| [docs/项目功能交付书.md](docs/项目功能交付书.md) | 产品 / 验收 | 功能模块、架构、API、代码索引 | +| [docs/项目文档.md](docs/项目文档.md) | 开发者 | 环境、目录、构建、部署、开发流程 | +| [项目文件/BUILD.md](项目文件/BUILD.md) | 开发者 | EXE / APK 打包细节与排错 | + +## 快速开始(源码) + +### 桌面端 + +```powershell +python -m venv .venv +.\.venv\Scripts\activate +pip install -r requirements.txt +python main.py +``` + +### 云端(可选) + +```powershell +pip install -r sakura_cloud/requirements.txt +uvicorn sakura_cloud.main:app --reload +``` + +### 移动端(开发) + +```powershell +cd sakura_mobile +flutter pub get +flutter run +``` + +## 一键打包 + +```powershell +# 桌面 EXE +scripts\build_desktop_exe.bat + +# Android APK(需 JDK 17+,仓库内 tools\flutter 与 tools\android-sdk) +scripts\build_mobile_android.bat + +# 打包后复制到 releases/ +scripts\copy_release_artifacts.bat + +# 或依次执行 EXE + APK + 复制 +scripts\build_all.bat +``` + +## 技术栈摘要 + +- 桌面:Python 3.8+、CustomTkinter、SQLite、Playwright(X 插件) +- 云端:FastAPI、PostgreSQL、JWT +- 移动:Flutter、Riverpod、Dio + +## 目录结构(精简) + +```text +Pixiv/ +├── main.py # 桌面入口 +├── pixiv_app/ # 桌面核心(GUI、下载、图库、云同步) +├── plugins/ # Pixiv / X 等插件 +├── sakura_cloud/ # 云端 API、docker-compose、Alembic +├── sakura_mobile/ # Flutter 安卓端 +├── releases/ # 可分发成品(EXE / APK) +├── docs/ # 交付书、开发文档 +├── scripts/ # 构建与依赖安装脚本 +├── tools/ # 辅助脚本(如代码统计) +└── 项目文件/ # 迭代与专题说明 +``` + +## 许可与说明 + +- APK 当前为 **调试签名**,仅供个人测试,不宜直接上架分发。 +- 使用 X 插件前需安装 Playwright Chromium(见 `项目文件/BUILD.md`)。 +- 会话与图库数据默认保存在 `%LOCALAPPDATA%\PixivDownloader`(打包版)。 + +--- + +*最后构建:2026-06-05* diff --git a/build_assets/PixivDownloader.spec b/build_assets/PixivDownloader.spec new file mode 100644 index 0000000..c42a099 --- /dev/null +++ b/build_assets/PixivDownloader.spec @@ -0,0 +1,115 @@ +# -*- mode: python ; coding: utf-8 -*- + +from __future__ import annotations + +from pathlib import Path + + +block_cipher = None +spec_path = Path(SPECPATH).resolve() +root = spec_path.parent.parent if spec_path.suffix == ".spec" else spec_path.parent + + +def tree_datas(source: str, dest: str, suffixes: tuple[str, ...] | None = None): + base = root / source + items = [] + if not base.exists(): + return items + for path in base.rglob("*"): + if not path.is_file(): + continue + parts = set(path.parts) + if "__pycache__" in parts: + continue + if path.suffix.lower() in {".pyc", ".pyo"}: + continue + if suffixes and path.suffix.lower() not in suffixes: + continue + rel_parent = path.parent.relative_to(base) + items.append((str(path), str(Path(dest) / rel_parent))) + return items + + +datas = [] +datas += tree_datas("pixiv_app/webui", "pixiv_app/webui", (".html", ".css", ".js", ".png", ".jpg", ".jpeg", ".webp", ".svg", ".ico")) +datas += [ + (src, dst) + for src, dst in tree_datas("plugins", "plugins", (".py", ".json", ".md", ".txt")) + if not any( + marker in src + for marker in ( + "plugins\\ao3", + "plugins/ao3", + "plugins\\changpei", + "plugins/changpei", + "plugins\\taobao", + "plugins/taobao", + ) + ) +] +datas += [(str(root / "build_assets" / "app_icon.ico"), "build_assets")] + + +hiddenimports = [ + "pixiv_app.core.fingerprint", + "pixiv_app.core.browser_proxy_pool", + "PIL", + "PIL.Image", + "cryptography", + "cryptography.hazmat.primitives", + "cryptography.hazmat.primitives.padding", + "cryptography.hazmat.primitives.ciphers", + "customtkinter", + "requests", + "httpx", + "playwright", + "playwright.async_api", + "playwright.sync_api", + "pixiv_app.core.playwright_runtime", + "pixiv_app.services.x_login_flow", +] + + +a = Analysis( + [str(root / "main.py")], + pathex=[str(root)], + binaries=[], + datas=datas, + hiddenimports=hiddenimports, + hookspath=[], + hooksconfig={}, + runtime_hooks=[], + excludes=[ + "pytest", + "unittest", + "tkinter.test", + ], + win_no_prefer_redirects=False, + win_private_assemblies=False, + cipher=block_cipher, + noarchive=False, +) +pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) + +exe = EXE( + pyz, + a.scripts, + a.binaries, + a.zipfiles, + a.datas, + [], + name="sakuradownloader", + debug=False, + bootloader_ignore_signals=False, + strip=False, + upx=True, + upx_exclude=[], + runtime_tmpdir=None, + console=False, + disable_windowed_traceback=False, + argv_emulation=False, + target_arch=None, + codesign_identity=None, + entitlements_file=None, + icon=str(root / "build_assets" / "app_icon.ico"), +) diff --git a/build_assets/app_icon.ico b/build_assets/app_icon.ico new file mode 100644 index 0000000..42998c8 Binary files /dev/null and b/build_assets/app_icon.ico differ diff --git a/distributed_crawler/README.md b/distributed_crawler/README.md new file mode 100644 index 0000000..649b43c --- /dev/null +++ b/distributed_crawler/README.md @@ -0,0 +1,116 @@ +# Distributed Crawler + +An enterprise-oriented distributed crawler scaffold built around `aiohttp` and `asyncio`. + +## Included capabilities + +- Master + Worker topology +- Redis-backed priority queues +- Consistent-hash worker assignment +- Bloom-filter task dedupe +- Dead-letter queue for exhausted failures +- Checkpoint + state recovery +- Task/request/worker trace headers +- Exponential backoff retries +- Domain-aware rate limiting +- Domain-level circuit breaker +- Proxy pool with health verification +- Proxy scoring with weighted selection +- Shared session store with Redis + local backup +- Result aggregation with JSONL, MongoDB, or PostgreSQL sinks and URL-hash dedupe +- APScheduler-driven periodic task submission + +## Safety notes + +This project intentionally keeps high-risk account automation, captcha bypass, and anti-protection evasion as extension points only. +Use it only against systems you are authorized to access and collect from. + +## Directory layout + +```text +distributed_crawler/ + config.example.yaml + requirements.txt + crawler/ + auth.py + backoff.py + bloom.py + config.py + hash_ring.py + http.py + master.py + models.py + proxy.py + queue.py + rate_limit.py + results.py + scheduler.py + sessions.py + signatures.py + spider.py + state.py + worker.py + scripts/ + run_master.py + run_worker.py +``` + +## Quick start + +1. Copy `config.example.yaml` to `config.yaml`. +2. Start Redis. +3. Install dependencies from `requirements.txt`. +4. Start the master: + +```bash +python scripts/run_master.py --config config.yaml +``` + +5. Start one or more workers: + +```bash +python scripts/run_worker.py --config config.yaml --worker-id worker-1 +python scripts/run_worker.py --config config.yaml --worker-id worker-2 +``` + +6. Submit a task: + +```bash +curl -X POST http://127.0.0.1:8088/tasks ^ + -H "Content-Type: application/json" ^ + -d "{\"url\":\"https://httpbin.org/get\",\"spider\":\"generic_fetch\"}" +``` + +## Master endpoints + +- `POST /tasks` +- `POST /heartbeat` +- `GET /workers` +- `GET /stats` +- `GET /health` +- `GET /dashboard` (feature and runtime panel) + +## Failure handling and tracing + +- Bloom sizing uses `master.bloom_capacity` and `master.bloom_false_positive_rate` to calculate bit count and hash count automatically. Set `master.bloom_hashes` only when you need to override the computed optimum. +- Final failed tasks are appended to `master.dead_letter_queue_name` with the original task, final result, retry count, worker ID, error, and HTTP status. +- Worker requests include `X-Task-Id`, `X-Request-Id`, and `X-Worker-Id` headers. Result payloads also include `request_id` for easier log/result correlation. + +## Stability controls + +- Proxy endpoints track success count, failure count, latency, cooldown, and a computed score. Workers choose healthy or cooled-down proxies with weighted random selection. +- `circuit_breaker` protects each domain independently. After repeated transport errors or 5xx responses, the domain opens briefly, then moves to half-open for recovery probing. +- Result storage writes a stable `url_hash`. MongoDB and PostgreSQL enforce uniqueness with upsert behavior; JSONL keeps a sidecar `.url_hashes` index. + +## Auto login and shared sessions + +- Enable `auth.enabled: true` and configure per-domain credentials in `config.yaml`. +- Worker marks sessions expired based on configured status codes/body keywords. +- When expired, worker triggers `AuthProvider.refresh_session()` and writes refreshed cookie to Redis + local backup via `DualSessionStore`. +- All workers read the same session keys (`session.redis_prefix`), so login state is shared. + +## Extension points + +- `crawler.auth.AuthProvider` +- `crawler.signatures.SignatureProvider` +- `crawler.spider.BaseSpider` diff --git a/distributed_crawler/config.example.yaml b/distributed_crawler/config.example.yaml new file mode 100644 index 0000000..49e854f --- /dev/null +++ b/distributed_crawler/config.example.yaml @@ -0,0 +1,93 @@ +app_name: distributed-crawler +redis_dsn: redis://127.0.0.1:6379/0 +checkpoint_dir: ./runtime/checkpoints + +master: + host: 127.0.0.1 + port: 8088 + worker_ttl_seconds: 30 + stale_task_seconds: 120 + bloom_capacity: 1000000 + bloom_false_positive_rate: 0.01 + bloom_hashes: + queue_name: crawler + result_queue_name: crawler:results + dead_letter_queue_name: crawler:dead_letter + default_spider: generic_fetch + +worker: + worker_id: worker-1 + concurrency: 100 + heartbeat_interval_seconds: 5 + request_timeout_seconds: 20 + connect_timeout_seconds: 10 + retries: 4 + base_backoff_seconds: 1 + max_backoff_seconds: 16 + download_dir: ./runtime/downloads + +storage: + backend: jsonl + jsonl_path: ./runtime/results/results.jsonl + mongodb_dsn: mongodb://127.0.0.1:27017 + mongodb_database: crawler + mongodb_collection: results + postgres_dsn: postgresql://postgres:postgres@127.0.0.1:5432/crawler + postgres_table: crawl_results + +proxy: + enabled: false + verify_url: https://httpbin.org/ip + verify_timeout_seconds: 8 + refresh_interval_seconds: 300 + failure_threshold: 3 + cooldown_seconds: 60 + static_proxies: [] + provider_api_url: "" + provider_auth_header: "" + +circuit_breaker: + enabled: true + failure_threshold: 8 + recovery_timeout_seconds: 30 + half_open_success_threshold: 2 + +session: + enabled: true + redis_prefix: crawler:sessions + local_backup_path: ./runtime/sessions/sessions.json + +auth: + enabled: false + domains: + example.com: + login_url: https://example.com/api/login + username: your_username + password: your_password + username_field: username + password_field: password + submit_type: json # json or form + extra_payload: {} + success_status_codes: [200] + expire_status_codes: [401, 403] + expire_body_keywords: ["login required", "please login", "unauthorized"] + +rate_limit: + default_qps: 3.0 + default_burst: 6 + per_domain: + httpbin.org: + qps: 2.0 + burst: 4 + +schedules: + - name: sample-httpbin + trigger: interval + seconds: 300 + priority: medium + task: + spider: generic_fetch + url: https://httpbin.org/get + method: GET + metadata: + source: schedule diff --git a/distributed_crawler/crawler/__init__.py b/distributed_crawler/crawler/__init__.py new file mode 100644 index 0000000..e00ad15 --- /dev/null +++ b/distributed_crawler/crawler/__init__.py @@ -0,0 +1,5 @@ +from .config import AppSettings, load_settings +from .master import MasterNode +from .worker import WorkerNode + +__all__ = ["AppSettings", "MasterNode", "WorkerNode", "load_settings"] diff --git a/distributed_crawler/crawler/auth.py b/distributed_crawler/crawler/auth.py new file mode 100644 index 0000000..e40c2eb --- /dev/null +++ b/distributed_crawler/crawler/auth.py @@ -0,0 +1,75 @@ +from __future__ import annotations + +import logging +from abc import ABC, abstractmethod +from typing import Any + +import aiohttp + +from .config import AuthSettings + +LOGGER = logging.getLogger(__name__) + + +class AuthProvider(ABC): + @abstractmethod + async def refresh_session(self, domain: str, existing_session: dict[str, Any]) -> dict[str, Any]: + """ + Provide an authorized and compliant session refresh implementation for the target system. + """ + + +class ManualAuthProvider(AuthProvider): + async def refresh_session(self, domain: str, existing_session: dict[str, Any]) -> dict[str, Any]: + raise NotImplementedError( + f"Session refresh for {domain} must be implemented with authorized credentials and target-specific logic." + ) + + +class ConfigurableAuthProvider(AuthProvider): + """ + Generic username/password login provider. + This intentionally keeps site-specific anti-bot behavior out of scope. + """ + + def __init__(self, settings: AuthSettings, timeout_seconds: int = 20) -> None: + self.settings = settings + self.timeout_seconds = timeout_seconds + + async def refresh_session(self, domain: str, existing_session: dict[str, Any]) -> dict[str, Any]: + if not self.settings.enabled: + return {**existing_session, "expired": False} + cfg = self.settings.domains.get(domain) + if cfg is None or not cfg.login_url or not cfg.username or not cfg.password: + LOGGER.warning("No auth config for domain=%s, keeping existing session", domain) + return {**existing_session, "expired": True} + + payload: dict[str, Any] = dict(cfg.extra_payload) + payload[cfg.username_field] = cfg.username + payload[cfg.password_field] = cfg.password + + timeout = aiohttp.ClientTimeout(total=self.timeout_seconds) + async with aiohttp.ClientSession(timeout=timeout) as session: + if cfg.submit_type.lower() == "json": + async with session.post(cfg.login_url, json=payload) as response: + body_text = await response.text() + if response.status not in set(cfg.success_status_codes): + raise RuntimeError(f"Login failed for {domain}: {response.status} {body_text[:180]}") + else: + async with session.post(cfg.login_url, data=payload) as response: + body_text = await response.text() + if response.status not in set(cfg.success_status_codes): + raise RuntimeError(f"Login failed for {domain}: {response.status} {body_text[:180]}") + + cookie_pairs = [f"{k}={v.value}" for k, v in session.cookie_jar.filter_cookies(cfg.login_url).items()] + cookie_header = "; ".join(cookie_pairs) + if not cookie_header: + # Fallback: keep old cookies if target does not set cookies in login response. + cookie_header = str(existing_session.get("cookie", "")) + + return { + **existing_session, + "expired": False, + "cookie": cookie_header, + "updated_by": "ConfigurableAuthProvider", + } diff --git a/distributed_crawler/crawler/backoff.py b/distributed_crawler/crawler/backoff.py new file mode 100644 index 0000000..b9943aa --- /dev/null +++ b/distributed_crawler/crawler/backoff.py @@ -0,0 +1,9 @@ +from __future__ import annotations + +import random + + +def exponential_backoff(attempt: int, base_seconds: float, max_seconds: float) -> float: + delay = min(max_seconds, base_seconds * (2 ** max(attempt, 0))) + jitter = random.uniform(0, delay * 0.2 if delay > 0 else 0) + return delay + jitter diff --git a/distributed_crawler/crawler/bloom.py b/distributed_crawler/crawler/bloom.py new file mode 100644 index 0000000..57b2963 --- /dev/null +++ b/distributed_crawler/crawler/bloom.py @@ -0,0 +1,49 @@ +from __future__ import annotations + +import math +from hashlib import md5, sha1 + +from redis.asyncio import Redis + + +class RedisBloomFilter: + def __init__(self, redis: Redis, key: str, bits: int = 8_000_000, hashes: int = 6) -> None: + self.redis = redis + self.key = key + self.bits = max(int(bits), 1) + self.hashes = max(int(hashes), 1) + + @classmethod + def from_capacity( + cls, + redis: Redis, + key: str, + *, + capacity: int, + false_positive_rate: float = 0.01, + hashes: int | None = None, + ) -> "RedisBloomFilter": + capacity = max(int(capacity), 1) + false_positive_rate = min(max(float(false_positive_rate), 1e-9), 0.999999) + bits = math.ceil(-(capacity * math.log(false_positive_rate)) / (math.log(2) ** 2)) + optimal_hashes = max(1, round((bits / capacity) * math.log(2))) + return cls(redis, key, bits=bits, hashes=hashes or optimal_hashes) + + def _positions(self, value: str) -> list[int]: + digest_a = int(md5(value.encode("utf-8")).hexdigest(), 16) + digest_b = int(sha1(value.encode("utf-8")).hexdigest(), 16) + return [((digest_a + index * digest_b) % self.bits) for index in range(self.hashes)] + + async def contains_or_add(self, value: str) -> bool: + positions = self._positions(value) + pipeline = self.redis.pipeline() + for position in positions: + pipeline.getbit(self.key, position) + existing = await pipeline.execute() + seen = all(bit == 1 for bit in existing) + + pipeline = self.redis.pipeline() + for position in positions: + pipeline.setbit(self.key, position, 1) + await pipeline.execute() + return seen diff --git a/distributed_crawler/crawler/circuit_breaker.py b/distributed_crawler/crawler/circuit_breaker.py new file mode 100644 index 0000000..927e60a --- /dev/null +++ b/distributed_crawler/crawler/circuit_breaker.py @@ -0,0 +1,67 @@ +from __future__ import annotations + +from dataclasses import dataclass +from enum import Enum +from time import monotonic + +from .config import CircuitBreakerSettings + + +class CircuitState(str, Enum): + CLOSED = "closed" + OPEN = "open" + HALF_OPEN = "half_open" + + +@dataclass +class Circuit: + state: CircuitState = CircuitState.CLOSED + failures: int = 0 + half_open_successes: int = 0 + opened_at: float = 0.0 + + +class CircuitOpenError(RuntimeError): + pass + + +class DomainCircuitBreaker: + def __init__(self, settings: CircuitBreakerSettings) -> None: + self.settings = settings + self._circuits: dict[str, Circuit] = {} + + def before_request(self, domain: str) -> None: + if not self.settings.enabled: + return + circuit = self._circuits.setdefault(domain, Circuit()) + if circuit.state != CircuitState.OPEN: + return + elapsed = monotonic() - circuit.opened_at + if elapsed >= self.settings.recovery_timeout_seconds: + circuit.state = CircuitState.HALF_OPEN + circuit.half_open_successes = 0 + return + raise CircuitOpenError(f"Circuit open for {domain}") + + def record_success(self, domain: str) -> None: + if not self.settings.enabled: + return + circuit = self._circuits.setdefault(domain, Circuit()) + if circuit.state == CircuitState.HALF_OPEN: + circuit.half_open_successes += 1 + if circuit.half_open_successes < self.settings.half_open_success_threshold: + return + circuit.state = CircuitState.CLOSED + circuit.failures = 0 + circuit.half_open_successes = 0 + circuit.opened_at = 0.0 + + def record_failure(self, domain: str) -> None: + if not self.settings.enabled: + return + circuit = self._circuits.setdefault(domain, Circuit()) + circuit.failures += 1 + circuit.half_open_successes = 0 + if circuit.failures >= self.settings.failure_threshold or circuit.state == CircuitState.HALF_OPEN: + circuit.state = CircuitState.OPEN + circuit.opened_at = monotonic() diff --git a/distributed_crawler/crawler/config.py b/distributed_crawler/crawler/config.py new file mode 100644 index 0000000..416f574 --- /dev/null +++ b/distributed_crawler/crawler/config.py @@ -0,0 +1,206 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + +import yaml + + +@dataclass +class MasterSettings: + host: str = "127.0.0.1" + port: int = 8088 + worker_ttl_seconds: int = 30 + stale_task_seconds: int = 120 + bloom_capacity: int = 1_000_000 + bloom_false_positive_rate: float = 0.01 + bloom_hashes: int | None = None + queue_name: str = "crawler" + result_queue_name: str = "crawler:results" + dead_letter_queue_name: str = "crawler:dead_letter" + default_spider: str = "generic_fetch" + + +@dataclass +class WorkerSettings: + worker_id: str = "worker-1" + concurrency: int = 100 + heartbeat_interval_seconds: int = 5 + request_timeout_seconds: int = 20 + connect_timeout_seconds: int = 10 + retries: int = 4 + base_backoff_seconds: float = 1.0 + max_backoff_seconds: float = 16.0 + download_dir: str = "./runtime/downloads" + + +@dataclass +class StorageSettings: + backend: str = "jsonl" + jsonl_path: str = "./runtime/results/results.jsonl" + mongodb_dsn: str = "mongodb://127.0.0.1:27017" + mongodb_database: str = "crawler" + mongodb_collection: str = "results" + postgres_dsn: str = "postgresql://postgres:postgres@127.0.0.1:5432/crawler" + postgres_table: str = "crawl_results" + + +@dataclass +class ProxySettings: + enabled: bool = False + verify_url: str = "https://httpbin.org/ip" + verify_timeout_seconds: int = 8 + refresh_interval_seconds: int = 300 + failure_threshold: int = 3 + cooldown_seconds: int = 60 + static_proxies: list[str] = field(default_factory=list) + provider_api_url: str = "" + provider_auth_header: str = "" + + +@dataclass +class CircuitBreakerSettings: + enabled: bool = True + failure_threshold: int = 8 + recovery_timeout_seconds: int = 30 + half_open_success_threshold: int = 2 + + +@dataclass +class SessionSettings: + enabled: bool = True + redis_prefix: str = "crawler:sessions" + local_backup_path: str = "./runtime/sessions/sessions.json" + + +@dataclass +class AuthDomainSettings: + login_url: str = "" + username: str = "" + password: str = "" + username_field: str = "username" + password_field: str = "password" + submit_type: str = "form" + extra_payload: dict[str, Any] = field(default_factory=dict) + success_status_codes: list[int] = field(default_factory=lambda: [200]) + expire_status_codes: list[int] = field(default_factory=lambda: [401, 403]) + expire_body_keywords: list[str] = field(default_factory=lambda: ["login required", "please login", "unauthorized"]) + + +@dataclass +class AuthSettings: + enabled: bool = False + domains: dict[str, AuthDomainSettings] = field(default_factory=dict) + + +@dataclass +class DomainRateLimitSettings: + qps: float = 3.0 + burst: int = 6 + + +@dataclass +class RateLimitSettings: + default_qps: float = 3.0 + default_burst: int = 6 + per_domain: dict[str, DomainRateLimitSettings] = field(default_factory=dict) + + +@dataclass +class ScheduleTaskSettings: + name: str + trigger: str + task: dict[str, Any] + priority: str = "medium" + seconds: int | None = None + minutes: int | None = None + cron: str | None = None + + +@dataclass +class AppSettings: + app_name: str = "distributed-crawler" + redis_dsn: str = "redis://127.0.0.1:6379/0" + checkpoint_dir: str = "./runtime/checkpoints" + master: MasterSettings = field(default_factory=MasterSettings) + worker: WorkerSettings = field(default_factory=WorkerSettings) + storage: StorageSettings = field(default_factory=StorageSettings) + proxy: ProxySettings = field(default_factory=ProxySettings) + circuit_breaker: CircuitBreakerSettings = field(default_factory=CircuitBreakerSettings) + session: SessionSettings = field(default_factory=SessionSettings) + auth: AuthSettings = field(default_factory=AuthSettings) + rate_limit: RateLimitSettings = field(default_factory=RateLimitSettings) + schedules: list[ScheduleTaskSettings] = field(default_factory=list) + source_path: Path | None = None + + @property + def base_dir(self) -> Path: + if self.source_path is None: + return Path.cwd() + return self.source_path.parent + + def resolve_path(self, value: str) -> Path: + candidate = Path(value) + if candidate.is_absolute(): + return candidate + return (self.base_dir / candidate).resolve() + + +def _load_domain_limits(raw_limits: dict[str, Any]) -> dict[str, DomainRateLimitSettings]: + result: dict[str, DomainRateLimitSettings] = {} + for domain, item in raw_limits.items(): + result[domain] = DomainRateLimitSettings( + qps=float(item.get("qps", 3.0)), + burst=int(item.get("burst", 6)), + ) + return result + + +def _load_auth_domains(raw_domains: dict[str, Any]) -> dict[str, AuthDomainSettings]: + result: dict[str, AuthDomainSettings] = {} + for domain, item in raw_domains.items(): + if not isinstance(item, dict): + continue + result[domain] = AuthDomainSettings( + login_url=str(item.get("login_url", "")), + username=str(item.get("username", "")), + password=str(item.get("password", "")), + username_field=str(item.get("username_field", "username")), + password_field=str(item.get("password_field", "password")), + submit_type=str(item.get("submit_type", "form")), + extra_payload=dict(item.get("extra_payload", {})), + success_status_codes=[int(v) for v in item.get("success_status_codes", [200])], + expire_status_codes=[int(v) for v in item.get("expire_status_codes", [401, 403])], + expire_body_keywords=[str(v).lower() for v in item.get("expire_body_keywords", ["login required", "please login", "unauthorized"])], + ) + return result + + +def load_settings(path: str | Path) -> AppSettings: + config_path = Path(path).resolve() + data = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {} + + settings = AppSettings( + app_name=str(data.get("app_name", "distributed-crawler")), + redis_dsn=str(data.get("redis_dsn", "redis://127.0.0.1:6379/0")), + checkpoint_dir=str(data.get("checkpoint_dir", "./runtime/checkpoints")), + master=MasterSettings(**(data.get("master") or {})), + worker=WorkerSettings(**(data.get("worker") or {})), + storage=StorageSettings(**(data.get("storage") or {})), + proxy=ProxySettings(**(data.get("proxy") or {})), + circuit_breaker=CircuitBreakerSettings(**(data.get("circuit_breaker") or {})), + session=SessionSettings(**(data.get("session") or {})), + auth=AuthSettings( + enabled=bool((data.get("auth") or {}).get("enabled", False)), + domains=_load_auth_domains((data.get("auth") or {}).get("domains", {})), + ), + rate_limit=RateLimitSettings( + default_qps=float((data.get("rate_limit") or {}).get("default_qps", 3.0)), + default_burst=int((data.get("rate_limit") or {}).get("default_burst", 6)), + per_domain=_load_domain_limits((data.get("rate_limit") or {}).get("per_domain", {})), + ), + schedules=[ScheduleTaskSettings(**item) for item in data.get("schedules", [])], + source_path=config_path, + ) + return settings diff --git a/distributed_crawler/crawler/hash_ring.py b/distributed_crawler/crawler/hash_ring.py new file mode 100644 index 0000000..7249bf9 --- /dev/null +++ b/distributed_crawler/crawler/hash_ring.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +from bisect import bisect +from hashlib import md5 + + +class ConsistentHashRing: + def __init__(self, replicas: int = 100) -> None: + self.replicas = replicas + self._keys: list[int] = [] + self._nodes: dict[int, str] = {} + + def _hash(self, value: str) -> int: + return int(md5(value.encode("utf-8")).hexdigest(), 16) + + def rebuild(self, nodes: list[str]) -> None: + self._keys.clear() + self._nodes.clear() + for node in sorted(set(nodes)): + for replica in range(self.replicas): + key = self._hash(f"{node}:{replica}") + self._keys.append(key) + self._nodes[key] = node + self._keys.sort() + + def get_node(self, key: str) -> str | None: + if not self._keys: + return None + hashed = self._hash(key) + index = bisect(self._keys, hashed) + if index == len(self._keys): + index = 0 + return self._nodes[self._keys[index]] diff --git a/distributed_crawler/crawler/http.py b/distributed_crawler/crawler/http.py new file mode 100644 index 0000000..45ab8d4 --- /dev/null +++ b/distributed_crawler/crawler/http.py @@ -0,0 +1,180 @@ +from __future__ import annotations + +import json +import random +from pathlib import Path +from time import monotonic +from typing import Any + +import aiohttp + +from .auth import AuthProvider +from .circuit_breaker import DomainCircuitBreaker +from .config import AppSettings +from .models import CrawlTask +from .proxy import ProxyPool +from .rate_limit import DomainRateLimiter +from .sessions import DualSessionStore +from .signatures import SignatureProvider + + +USER_AGENTS = [ + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/126.0 Safari/537.36", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_4) AppleWebKit/605.1.15 Version/17.4 Safari/605.1.15", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Edg/126.0.0.0 Safari/537.36", +] + + +class AsyncHttpClient: + def __init__( + self, + settings: AppSettings, + rate_limiter: DomainRateLimiter, + proxy_pool: ProxyPool, + session_store: DualSessionStore, + auth_provider: AuthProvider, + signature_provider: SignatureProvider, + ) -> None: + self.settings = settings + self.rate_limiter = rate_limiter + self.proxy_pool = proxy_pool + self.session_store = session_store + self.auth_provider = auth_provider + self.signature_provider = signature_provider + self.session: aiohttp.ClientSession | None = None + self.circuit_breaker = DomainCircuitBreaker(settings.circuit_breaker) + + async def start(self) -> None: + timeout = aiohttp.ClientTimeout( + total=self.settings.worker.request_timeout_seconds, + connect=self.settings.worker.connect_timeout_seconds, + ) + connector = aiohttp.TCPConnector(limit=self.settings.worker.concurrency, ssl=False) + self.session = aiohttp.ClientSession(timeout=timeout, connector=connector) + + async def close(self) -> None: + if self.session is not None: + await self.session.close() + + async def fetch(self, task: CrawlTask) -> dict[str, Any]: + if self.session is None: + raise RuntimeError("HTTP client is not started") + + await self.rate_limiter.acquire(task.domain) + self.circuit_breaker.before_request(task.domain) + proxy = await self.proxy_pool.acquire() + session_payload = await self.session_store.load(task.domain) + if self.settings.session.enabled and session_payload.get("expired"): + session_payload = await self.auth_provider.refresh_session(task.domain, session_payload) + await self.session_store.save(task.domain, session_payload) + + headers = dict(task.headers) + headers.setdefault("User-Agent", random.choice(USER_AGENTS)) + headers.setdefault("Accept", "*/*") + if task.metadata.get("referer"): + headers.setdefault("Referer", str(task.metadata["referer"])) + if task.metadata.get("origin"): + headers.setdefault("Origin", str(task.metadata["origin"])) + headers.setdefault("X-Task-Id", task.task_id) + if task.metadata.get("request_id"): + headers.setdefault("X-Request-Id", str(task.metadata["request_id"])) + if task.metadata.get("worker_id"): + headers.setdefault("X-Worker-Id", str(task.metadata["worker_id"])) + cookie_header = session_payload.get("cookie") + if cookie_header: + headers.setdefault("Cookie", str(cookie_header)) + + signature_headers = await self.signature_provider.sign( + { + "url": task.url, + "method": task.method, + "body": task.body, + "metadata": task.metadata, + } + ) + headers.update(signature_headers) + + started_at = monotonic() + try: + async with self.session.request( + task.method, + task.url, + headers=headers, + data=task.body.encode("utf-8") if isinstance(task.body, str) else None, + proxy=proxy.url if proxy else None, + ) as response: + content_type = response.headers.get("Content-Type", "") + body_bytes = await response.read() + if "application/json" in content_type: + body: Any = json.loads(body_bytes.decode("utf-8", errors="ignore")) + else: + body = body_bytes.decode("utf-8", errors="ignore") + response_data = { + "status": response.status, + "headers": dict(response.headers), + "body": body, + "body_bytes": body_bytes, + "proxy": proxy.url if proxy else None, + } + if self.settings.session.enabled: + if self._is_session_expired(task.domain, response.status, body): + session_payload["expired"] = True + await self.session_store.save(task.domain, session_payload) + else: + if "Set-Cookie" in response.headers: + cookie_header = self._extract_cookie_header(response) + if cookie_header: + session_payload["cookie"] = cookie_header + session_payload["expired"] = False + await self.session_store.save(task.domain, session_payload) + if response.status >= 500: + self.circuit_breaker.record_failure(task.domain) + else: + self.circuit_breaker.record_success(task.domain) + await self.proxy_pool.mark_success(proxy, (monotonic() - started_at) * 1000) + return response_data + except Exception: + self.circuit_breaker.record_failure(task.domain) + await self.proxy_pool.mark_failed(proxy) + raise + + async def save_binary(self, task: CrawlTask, file_name: str, body_bytes: bytes) -> str: + save_dir = self.settings.resolve_path(self.settings.worker.download_dir) + save_dir.mkdir(parents=True, exist_ok=True) + target = save_dir / file_name + target.write_bytes(body_bytes) + return str(target) + + def _is_session_expired(self, domain: str, status_code: int, body: Any) -> bool: + if not self.settings.auth.enabled: + return False + domain_cfg = self.settings.auth.domains.get(domain) + if domain_cfg is None: + return False + if status_code in set(domain_cfg.expire_status_codes): + return True + if isinstance(body, str): + lower_body = body.lower() + return any(keyword in lower_body for keyword in domain_cfg.expire_body_keywords) + if isinstance(body, dict): + packed = json.dumps(body, ensure_ascii=False).lower() + return any(keyword in packed for keyword in domain_cfg.expire_body_keywords) + return False + + def _extract_cookie_header(self, response: aiohttp.ClientResponse) -> str: + values = response.headers.getall("Set-Cookie", []) + cookie_parts: list[str] = [] + for value in values: + head = value.split(";", 1)[0].strip() + if "=" in head: + cookie_parts.append(head) + # keep order and remove duplicates + seen: set[str] = set() + deduped: list[str] = [] + for item in cookie_parts: + key = item.split("=", 1)[0].strip() + if key in seen: + continue + seen.add(key) + deduped.append(item) + return "; ".join(deduped) diff --git a/distributed_crawler/crawler/master.py b/distributed_crawler/crawler/master.py new file mode 100644 index 0000000..217c721 --- /dev/null +++ b/distributed_crawler/crawler/master.py @@ -0,0 +1,346 @@ +from __future__ import annotations + +import asyncio +import logging +from time import time +from typing import Any +from html import escape + +from aiohttp import web +from redis.asyncio import Redis + +from .bloom import RedisBloomFilter +from .config import AppSettings +from .hash_ring import ConsistentHashRing +from .models import CrawlResult, CrawlTask, DeadLetterEntry, TaskPriority, TaskStatus, WorkerHeartbeat +from .queue import RedisPriorityQueue +from .results import ResultRepository, build_result_repository +from .scheduler import ScheduleService +from .state import CheckpointManager + +LOGGER = logging.getLogger(__name__) + + +class MasterNode: + def __init__(self, settings: AppSettings) -> None: + self.settings = settings + self.redis = Redis.from_url(settings.redis_dsn, decode_responses=True) + self.queue = RedisPriorityQueue( + self.redis, + settings.master.queue_name, + settings.master.result_queue_name, + settings.master.dead_letter_queue_name, + ) + self.checkpoints = CheckpointManager(self.redis, settings.resolve_path(settings.checkpoint_dir), settings.master.queue_name) + self.bloom = RedisBloomFilter.from_capacity( + self.redis, + key=f"{settings.master.queue_name}:bloom", + capacity=settings.master.bloom_capacity, + false_positive_rate=settings.master.bloom_false_positive_rate, + hashes=settings.master.bloom_hashes, + ) + self.hash_ring = ConsistentHashRing() + self.result_repo: ResultRepository = build_result_repository( + settings.storage, + settings.resolve_path(settings.storage.jsonl_path), + ) + self.scheduler = ScheduleService(settings, self.submit_task) + self.workers: dict[str, WorkerHeartbeat] = {} + self.app = web.Application() + self.app.add_routes( + [ + web.post("/tasks", self.handle_submit_task), + web.post("/heartbeat", self.handle_heartbeat), + web.get("/workers", self.handle_workers), + web.get("/stats", self.handle_stats), + web.get("/health", self.handle_health), + web.get("/dashboard", self.handle_dashboard), + ] + ) + self._background_tasks: list[asyncio.Task] = [] + + async def start(self) -> None: + await self.result_repo.setup() + await self._recover_local_checkpoints() + self.scheduler.start() + self._background_tasks = [ + asyncio.create_task(self.consume_results_loop(), name="consume-results"), + asyncio.create_task(self.reap_stale_workers_loop(), name="reap-workers"), + ] + + async def close(self) -> None: + for task in self._background_tasks: + task.cancel() + await asyncio.gather(*self._background_tasks, return_exceptions=True) + await self.scheduler.shutdown() + await self.result_repo.close() + await self.redis.aclose() + + async def submit_task(self, task: CrawlTask) -> bool: + duplicate = await self.bloom.contains_or_add(task.fingerprint) + if duplicate: + LOGGER.info("Skip duplicate task %s", task.url) + return False + worker_id = self._assign_worker(task) + task.assigned_worker = worker_id + task.status = TaskStatus.PENDING + await self.checkpoints.save(task) + await self.queue.enqueue_task(worker_id, task) + LOGGER.info("Submitted task %s to %s", task.task_id, worker_id) + return True + + async def handle_submit_task(self, request: web.Request) -> web.Response: + payload = await request.json() + task = CrawlTask.create( + url=payload["url"], + spider=payload.get("spider", self.settings.master.default_spider), + method=payload.get("method", "GET"), + headers=payload.get("headers"), + metadata=payload.get("metadata"), + body=payload.get("body"), + priority=payload.get("priority", TaskPriority.MEDIUM.value), + max_retries=int(payload.get("max_retries", self.settings.worker.retries)), + ) + accepted = await self.submit_task(task) + return web.json_response({"accepted": accepted, "task_id": task.task_id, "worker_id": task.assigned_worker}) + + async def handle_heartbeat(self, request: web.Request) -> web.Response: + payload = await request.json() + heartbeat = WorkerHeartbeat( + worker_id=str(payload["worker_id"]), + concurrency=int(payload["concurrency"]), + active_tasks=int(payload["active_tasks"]), + host=payload.get("host"), + tags=list(payload.get("tags", [])), + ) + self.workers[heartbeat.worker_id] = heartbeat + self.hash_ring.rebuild(list(self.workers.keys())) + return web.json_response({"ok": True, "known_workers": len(self.workers)}) + + async def handle_workers(self, _: web.Request) -> web.Response: + return web.json_response({"workers": [item.to_dict() for item in self.workers.values()]}) + + async def handle_stats(self, _: web.Request) -> web.Response: + return web.json_response( + { + "workers": len(self.workers), + "queues": { + worker_id: { + priority.value: await self.redis.llen(self.queue._task_key(worker_id, priority)) + for priority in TaskPriority + } + for worker_id in self.workers or {"default": None} + }, + "dead_letters": await self.redis.llen(self.queue.dead_letter_queue_name), + } + ) + + async def handle_health(self, _: web.Request) -> web.Response: + pong = await self.redis.ping() + return web.json_response({"status": "ok", "redis": bool(pong), "workers": len(self.workers)}) + + async def handle_dashboard(self, _: web.Request) -> web.Response: + queue_stats = await self._collect_queue_stats() + session_count = await self._count_sessions() + dead_letter_count = await self.redis.llen(self.queue.dead_letter_queue_name) + html = self._render_dashboard_html( + queue_stats=queue_stats, + session_count=session_count, + dead_letter_count=dead_letter_count, + ) + return web.Response(text=html, content_type="text/html") + + async def consume_results_loop(self) -> None: + while True: + try: + result = await self.queue.consume_result(timeout=5) + if result is None: + continue + await self.process_result(result) + except asyncio.CancelledError: + raise + except Exception: + LOGGER.exception("Result consumer failed") + await asyncio.sleep(1) + + async def process_result(self, result: CrawlResult) -> None: + task = await self.checkpoints.load(result.task_id) + if task is None: + LOGGER.warning("Result for unknown task %s", result.task_id) + return + task.status = result.status + await self.checkpoints.mark_status(task, result.status) + await self.result_repo.store(result) + if result.status == TaskStatus.FAILED: + await self.queue.publish_dead_letter(DeadLetterEntry.from_task_result(task, result)) + LOGGER.warning( + "Dead-lettered task task_id=%s worker_id=%s request_id=%s reason=%s", + task.task_id, + result.worker_id, + result.data.get("request_id", task.metadata.get("request_id", task.task_id)), + result.error, + ) + + async def reap_stale_workers_loop(self) -> None: + while True: + try: + await asyncio.sleep(self.settings.master.worker_ttl_seconds) + now = time() + stale_workers = [ + worker_id + for worker_id, heartbeat in self.workers.items() + if now - self._heartbeat_epoch(heartbeat.last_seen) > self.settings.master.worker_ttl_seconds + ] + for worker_id in stale_workers: + LOGGER.warning("Worker %s is stale, recovering tasks", worker_id) + recovered = await self.checkpoints.recover_stale(worker_id, self.settings.master.stale_task_seconds) + for task in recovered: + task.retries += 1 + if task.retries > task.max_retries: + task.status = TaskStatus.FAILED + await self.checkpoints.mark_status(task, TaskStatus.FAILED) + result = CrawlResult( + task_id=task.task_id, + worker_id=worker_id, + status=TaskStatus.FAILED, + url=task.url, + spider=task.spider, + error="Task exceeded retry budget during worker recovery", + ) + await self.result_repo.store(result) + await self.queue.publish_dead_letter(DeadLetterEntry.from_task_result(task, result)) + continue + task.assigned_worker = self._assign_worker(task, exclude=worker_id) + await self.checkpoints.save(task) + await self.queue.enqueue_task(task.assigned_worker, task) + self.workers.pop(worker_id, None) + self.hash_ring.rebuild(list(self.workers.keys())) + except asyncio.CancelledError: + raise + except Exception: + LOGGER.exception("Stale worker reaper failed") + + async def _recover_local_checkpoints(self) -> None: + tasks = await self.checkpoints.recover_local() + for task in tasks: + task.assigned_worker = self._assign_worker(task) + task.status = TaskStatus.RETRY + await self.checkpoints.save(task) + await self.queue.enqueue_task(task.assigned_worker, task) + + async def _collect_queue_stats(self) -> dict[str, dict[str, int]]: + worker_keys = list(self.workers.keys()) or ["default"] + result: dict[str, dict[str, int]] = {} + for worker_id in worker_keys: + result[worker_id] = { + priority.value: await self.redis.llen(self.queue._task_key(worker_id, priority)) + for priority in TaskPriority + } + return result + + async def _count_sessions(self) -> int: + pattern = f"{self.settings.session.redis_prefix}:*" + count = 0 + async for _ in self.redis.scan_iter(match=pattern): + count += 1 + return count + + def _render_dashboard_html(self, queue_stats: dict[str, dict[str, int]], session_count: int, dead_letter_count: int) -> str: + worker_rows = [] + for worker_id, hb in self.workers.items(): + load = hb.active_tasks / max(hb.concurrency, 1) + worker_rows.append( + f"{escape(worker_id)}{hb.active_tasks}{hb.concurrency}{load:.2f}{escape(hb.last_seen)}" + ) + if not worker_rows: + worker_rows.append("暂无在线 Worker") + + queue_blocks = [] + for worker_id, levels in queue_stats.items(): + queue_blocks.append( + "

Queue - " + + escape(worker_id) + + "

high: " + + str(levels.get("high", 0)) + + " | medium: " + + str(levels.get("medium", 0)) + + " | low: " + + str(levels.get("low", 0)) + + "

" + ) + queue_html = "\n".join(queue_blocks) + + enabled_auth_domains = ", ".join(sorted(self.settings.auth.domains.keys())) if self.settings.auth.domains else "-" + enabled_proxy = "enabled" if self.settings.proxy.enabled else "disabled" + enabled_session = "enabled" if self.settings.session.enabled else "disabled" + enabled_auth = "enabled" if self.settings.auth.enabled else "disabled" + + return f""" + + + + Distributed Crawler Dashboard + + + +

Distributed Crawler Dashboard

+
+

Workers

{len(self.workers)}

+

Session Keys

{session_count}

+

Dead Letters

{dead_letter_count}

+

Proxy

{enabled_proxy}

+

Auto Login

{enabled_auth}

+

Session Sharing

{enabled_session}

+

Auth Domains

{escape(enabled_auth_domains)}

+
+
+ {queue_html} +
+
+

Worker Runtime

+ + + + {''.join(worker_rows)} + +
worker_idactive_tasksconcurrencyloadlast_seen
+
+

API endpoints: /tasks /workers /stats /health

+ +""" + + def _assign_worker(self, task: CrawlTask, exclude: str | None = None) -> str: + candidates = [worker_id for worker_id in self.workers if worker_id != exclude] + if not candidates: + return "default" + self.hash_ring.rebuild(candidates) + preferred = self.hash_ring.get_node(task.url) or candidates[0] + preferred_heartbeat = self.workers.get(preferred) + if preferred_heartbeat is None: + return preferred + + preferred_load = preferred_heartbeat.active_tasks / max(preferred_heartbeat.concurrency, 1) + if preferred_load < 0.9: + return preferred + + least_loaded = min( + candidates, + key=lambda worker_id: self.workers[worker_id].active_tasks / max(self.workers[worker_id].concurrency, 1), + ) + return least_loaded + + def _heartbeat_epoch(self, timestamp: str) -> float: + from datetime import datetime + + return datetime.fromisoformat(timestamp).timestamp() diff --git a/distributed_crawler/crawler/models.py b/distributed_crawler/crawler/models.py new file mode 100644 index 0000000..c0c0a69 --- /dev/null +++ b/distributed_crawler/crawler/models.py @@ -0,0 +1,189 @@ +from __future__ import annotations + +from dataclasses import asdict, dataclass, field +from datetime import UTC, datetime +from enum import Enum +from hashlib import sha1 +from typing import Any +from urllib.parse import urlparse +from uuid import uuid4 + + +def utc_now() -> str: + return datetime.now(UTC).isoformat() + + +def make_url_hash(url: str) -> str: + return sha1(url.encode("utf-8")).hexdigest() + + +class TaskPriority(str, Enum): + HIGH = "high" + MEDIUM = "medium" + LOW = "low" + + +class TaskStatus(str, Enum): + PENDING = "pending" + RUNNING = "running" + SUCCESS = "success" + FAILED = "failed" + RETRY = "retry" + + +@dataclass +class CrawlTask: + task_id: str + url: str + spider: str + method: str = "GET" + headers: dict[str, str] = field(default_factory=dict) + metadata: dict[str, Any] = field(default_factory=dict) + body: str | None = None + priority: TaskPriority = TaskPriority.MEDIUM + status: TaskStatus = TaskStatus.PENDING + retries: int = 0 + max_retries: int = 4 + assigned_worker: str | None = None + created_at: str = field(default_factory=utc_now) + updated_at: str = field(default_factory=utc_now) + schedule_name: str | None = None + + @property + def domain(self) -> str: + return urlparse(self.url).netloc + + @property + def fingerprint(self) -> str: + payload = f"{self.method}:{self.url}:{self.spider}:{self.body or ''}" + return sha1(payload.encode("utf-8")).hexdigest() + + def to_dict(self) -> dict[str, Any]: + payload = asdict(self) + payload["priority"] = self.priority.value + payload["status"] = self.status.value + return payload + + @classmethod + def from_dict(cls, payload: dict[str, Any]) -> "CrawlTask": + return cls( + task_id=str(payload["task_id"]), + url=str(payload["url"]), + spider=str(payload["spider"]), + method=str(payload.get("method", "GET")), + headers=dict(payload.get("headers", {})), + metadata=dict(payload.get("metadata", {})), + body=payload.get("body"), + priority=TaskPriority(payload.get("priority", TaskPriority.MEDIUM.value)), + status=TaskStatus(payload.get("status", TaskStatus.PENDING.value)), + retries=int(payload.get("retries", 0)), + max_retries=int(payload.get("max_retries", 4)), + assigned_worker=payload.get("assigned_worker"), + created_at=str(payload.get("created_at", utc_now())), + updated_at=str(payload.get("updated_at", utc_now())), + schedule_name=payload.get("schedule_name"), + ) + + @classmethod + def create( + cls, + *, + url: str, + spider: str, + method: str = "GET", + headers: dict[str, str] | None = None, + metadata: dict[str, Any] | None = None, + body: str | None = None, + priority: str = "medium", + max_retries: int = 4, + schedule_name: str | None = None, + ) -> "CrawlTask": + return cls( + task_id=uuid4().hex, + url=url, + spider=spider, + method=method.upper(), + headers=headers or {}, + metadata=metadata or {}, + body=body, + priority=TaskPriority(priority), + max_retries=max_retries, + schedule_name=schedule_name, + ) + + +@dataclass +class CrawlResult: + task_id: str + worker_id: str + status: TaskStatus + url: str + spider: str + http_status: int | None = None + data: dict[str, Any] = field(default_factory=dict) + error: str | None = None + finished_at: str = field(default_factory=utc_now) + + @property + def url_hash(self) -> str: + return make_url_hash(self.url) + + def to_dict(self) -> dict[str, Any]: + payload = asdict(self) + payload["status"] = self.status.value + payload["url_hash"] = self.url_hash + return payload + + @classmethod + def from_dict(cls, payload: dict[str, Any]) -> "CrawlResult": + return cls( + task_id=str(payload["task_id"]), + worker_id=str(payload["worker_id"]), + status=TaskStatus(payload["status"]), + url=str(payload["url"]), + spider=str(payload["spider"]), + http_status=payload.get("http_status"), + data=dict(payload.get("data", {})), + error=payload.get("error"), + finished_at=str(payload.get("finished_at", utc_now())), + ) + + +@dataclass +class DeadLetterEntry: + task: dict[str, Any] + result: dict[str, Any] | None = None + reason: str = "" + failed_at: str = field(default_factory=utc_now) + last_error: str | None = None + response_status: int | None = None + retry_count: int = 0 + worker_id: str | None = None + + def to_dict(self) -> dict[str, Any]: + return asdict(self) + + @classmethod + def from_task_result(cls, task: CrawlTask, result: CrawlResult, reason: str | None = None) -> "DeadLetterEntry": + return cls( + task=task.to_dict(), + result=result.to_dict(), + reason=reason or result.error or "Task failed", + last_error=result.error, + response_status=result.http_status, + retry_count=task.retries, + worker_id=result.worker_id, + ) + + +@dataclass +class WorkerHeartbeat: + worker_id: str + concurrency: int + active_tasks: int + last_seen: str = field(default_factory=utc_now) + host: str | None = None + tags: list[str] = field(default_factory=list) + + def to_dict(self) -> dict[str, Any]: + return asdict(self) diff --git a/distributed_crawler/crawler/proxy.py b/distributed_crawler/crawler/proxy.py new file mode 100644 index 0000000..81f7dc6 --- /dev/null +++ b/distributed_crawler/crawler/proxy.py @@ -0,0 +1,162 @@ +from __future__ import annotations + +import asyncio +import random +from abc import ABC, abstractmethod +from dataclasses import dataclass, field +from time import monotonic + +import aiohttp + +from .config import ProxySettings + + +@dataclass +class ProxyEndpoint: + url: str + failures: int = 0 + successes: int = 0 + latency_ms: float | None = None + last_verified_at: float = 0.0 + last_failed_at: float = 0.0 + healthy: bool = True + metadata: dict[str, str] = field(default_factory=dict) + + @property + def success_rate(self) -> float: + total = self.successes + self.failures + if total == 0: + return 1.0 + return self.successes / total + + @property + def score(self) -> float: + latency_penalty = 1.0 + if self.latency_ms is not None: + latency_penalty = max(0.1, min(1.0, 1000.0 / max(self.latency_ms, 1.0))) + failure_penalty = 1.0 / (1.0 + self.failures) + return max(0.01, self.success_rate * latency_penalty * failure_penalty) + + +class ProxyProvider(ABC): + @abstractmethod + async def fetch(self) -> list[ProxyEndpoint]: ... + + +class StaticProxyProvider(ProxyProvider): + def __init__(self, proxies: list[str]) -> None: + self.proxies = proxies + + async def fetch(self) -> list[ProxyEndpoint]: + return [ProxyEndpoint(url=value) for value in self.proxies] + + +class ApiProxyProvider(ProxyProvider): + def __init__(self, api_url: str, auth_header: str = "") -> None: + self.api_url = api_url + self.auth_header = auth_header + + async def fetch(self) -> list[ProxyEndpoint]: + if not self.api_url: + return [] + headers = {"Authorization": self.auth_header} if self.auth_header else {} + async with aiohttp.ClientSession() as session: + async with session.get(self.api_url, headers=headers) as response: + response.raise_for_status() + text = await response.text() + proxies: list[ProxyEndpoint] = [] + for line in text.splitlines(): + line = line.strip() + if line: + proxies.append(ProxyEndpoint(url=line)) + return proxies + + +class ProxyPool: + def __init__(self, settings: ProxySettings) -> None: + self.settings = settings + self._proxies: list[ProxyEndpoint] = [] + self._lock = asyncio.Lock() + self._next_index = 0 + + async def refresh(self) -> None: + providers: list[ProxyProvider] = [StaticProxyProvider(self.settings.static_proxies)] + if self.settings.provider_api_url: + providers.append(ApiProxyProvider(self.settings.provider_api_url, self.settings.provider_auth_header)) + async with self._lock: + existing = {endpoint.url: endpoint for endpoint in self._proxies} + merged: dict[str, ProxyEndpoint] = {} + for provider in providers: + for endpoint in await provider.fetch(): + merged.setdefault(endpoint.url, existing.get(endpoint.url, endpoint)) + async with self._lock: + self._proxies = list(merged.values()) + + async def verify(self) -> None: + if not self.settings.enabled: + return + async with self._lock: + proxies = list(self._proxies) + if not proxies: + return + async with aiohttp.ClientSession() as session: + tasks = [self._verify_proxy(session, proxy) for proxy in proxies] + await asyncio.gather(*tasks, return_exceptions=True) + + async def acquire(self) -> ProxyEndpoint | None: + if not self.settings.enabled: + return None + async with self._lock: + now = monotonic() + candidates = [ + item + for item in self._proxies + if item.healthy or now - item.last_failed_at >= self.settings.cooldown_seconds + ] + if not candidates: + return None + proxy = random.choices(candidates, weights=[item.score for item in candidates], k=1)[0] + self._next_index += 1 + return proxy + + async def mark_success(self, proxy: ProxyEndpoint | None, latency_ms: float | None = None) -> None: + if proxy is None: + return + async with self._lock: + proxy.successes += 1 + proxy.failures = 0 + proxy.healthy = True + if latency_ms is not None: + proxy.latency_ms = latency_ms + proxy.last_verified_at = monotonic() + + async def mark_failed(self, proxy: ProxyEndpoint | None) -> None: + if proxy is None: + return + async with self._lock: + proxy.failures += 1 + proxy.last_failed_at = monotonic() + if proxy.failures >= self.settings.failure_threshold: + proxy.healthy = False + + async def healthy_count(self) -> int: + async with self._lock: + return len([item for item in self._proxies if item.healthy]) + + async def _verify_proxy(self, session: aiohttp.ClientSession, proxy: ProxyEndpoint) -> None: + try: + timeout = aiohttp.ClientTimeout(total=self.settings.verify_timeout_seconds) + started_at = monotonic() + async with session.get(self.settings.verify_url, proxy=proxy.url, timeout=timeout) as response: + response.raise_for_status() + await response.read() + proxy.healthy = True + proxy.failures = 0 + proxy.successes += 1 + proxy.latency_ms = (monotonic() - started_at) * 1000 + proxy.last_verified_at = monotonic() + except Exception: + proxy.failures += 1 + proxy.last_failed_at = monotonic() + if proxy.failures >= self.settings.failure_threshold: + proxy.healthy = False diff --git a/distributed_crawler/crawler/queue.py b/distributed_crawler/crawler/queue.py new file mode 100644 index 0000000..a70c785 --- /dev/null +++ b/distributed_crawler/crawler/queue.py @@ -0,0 +1,42 @@ +from __future__ import annotations + +import json + +from redis.asyncio import Redis + +from .models import CrawlResult, CrawlTask, DeadLetterEntry, TaskPriority + + +class RedisPriorityQueue: + def __init__(self, redis: Redis, namespace: str, result_queue_name: str, dead_letter_queue_name: str | None = None) -> None: + self.redis = redis + self.namespace = namespace + self.result_queue_name = result_queue_name + self.dead_letter_queue_name = dead_letter_queue_name or f"{namespace}:dead_letter" + + def _task_key(self, worker_id: str, priority: TaskPriority) -> str: + return f"{self.namespace}:tasks:{worker_id}:{priority.value}" + + async def enqueue_task(self, worker_id: str, task: CrawlTask) -> None: + await self.redis.rpush(self._task_key(worker_id, task.priority), json.dumps(task.to_dict())) + + async def dequeue_task(self, worker_id: str, timeout: int = 5) -> CrawlTask | None: + keys = [self._task_key(worker_id, priority) for priority in (TaskPriority.HIGH, TaskPriority.MEDIUM, TaskPriority.LOW)] + result = await self.redis.blpop(keys, timeout=timeout) + if not result: + return None + _, payload = result + return CrawlTask.from_dict(json.loads(payload)) + + async def publish_result(self, result: CrawlResult) -> None: + await self.redis.rpush(self.result_queue_name, json.dumps(result.to_dict())) + + async def consume_result(self, timeout: int = 5) -> CrawlResult | None: + result = await self.redis.blpop(self.result_queue_name, timeout=timeout) + if not result: + return None + _, payload = result + return CrawlResult.from_dict(json.loads(payload)) + + async def publish_dead_letter(self, entry: DeadLetterEntry) -> None: + await self.redis.rpush(self.dead_letter_queue_name, json.dumps(entry.to_dict(), ensure_ascii=False)) diff --git a/distributed_crawler/crawler/rate_limit.py b/distributed_crawler/crawler/rate_limit.py new file mode 100644 index 0000000..cb53eec --- /dev/null +++ b/distributed_crawler/crawler/rate_limit.py @@ -0,0 +1,42 @@ +from __future__ import annotations + +import asyncio +from dataclasses import dataclass +from time import monotonic + +from .config import RateLimitSettings + + +@dataclass +class TokenBucket: + qps: float + burst: int + tokens: float + updated_at: float + + +class DomainRateLimiter: + def __init__(self, settings: RateLimitSettings) -> None: + self.settings = settings + self._buckets: dict[str, TokenBucket] = {} + self._locks: dict[str, asyncio.Lock] = {} + + async def acquire(self, domain: str) -> None: + lock = self._locks.setdefault(domain, asyncio.Lock()) + async with lock: + bucket = self._buckets.setdefault(domain, self._new_bucket(domain)) + while True: + now = monotonic() + elapsed = max(0.0, now - bucket.updated_at) + bucket.tokens = min(bucket.burst, bucket.tokens + elapsed * bucket.qps) + bucket.updated_at = now + if bucket.tokens >= 1: + bucket.tokens -= 1 + return + await asyncio.sleep(max(0.05, 1 / bucket.qps)) + + def _new_bucket(self, domain: str) -> TokenBucket: + domain_settings = self.settings.per_domain.get(domain) + qps = domain_settings.qps if domain_settings else self.settings.default_qps + burst = domain_settings.burst if domain_settings else self.settings.default_burst + return TokenBucket(qps=qps, burst=burst, tokens=float(burst), updated_at=monotonic()) diff --git a/distributed_crawler/crawler/results.py b/distributed_crawler/crawler/results.py new file mode 100644 index 0000000..a2bcf5c --- /dev/null +++ b/distributed_crawler/crawler/results.py @@ -0,0 +1,164 @@ +from __future__ import annotations + +import json +from abc import ABC, abstractmethod +from pathlib import Path + +import asyncpg +from motor.motor_asyncio import AsyncIOMotorClient + +from .config import StorageSettings +from .models import CrawlResult, make_url_hash + + +class ResultRepository(ABC): + @abstractmethod + async def setup(self) -> None: ... + + @abstractmethod + async def store(self, result: CrawlResult) -> None: ... + + @abstractmethod + async def close(self) -> None: ... + + +class JsonlResultRepository(ResultRepository): + def __init__(self, path: Path) -> None: + self.path = path + self.index_path = path.with_suffix(path.suffix + ".url_hashes") + self._seen_url_hashes: set[str] = set() + self.path.parent.mkdir(parents=True, exist_ok=True) + + async def setup(self) -> None: + if self.index_path.exists(): + self._seen_url_hashes = { + line.strip() + for line in self.index_path.read_text(encoding="utf-8").splitlines() + if line.strip() + } + return + if self.path.exists(): + with self.path.open("r", encoding="utf-8") as file_handle: + for line in file_handle: + try: + payload = json.loads(line) + except json.JSONDecodeError: + continue + url_hash = payload.get("url_hash") or (make_url_hash(str(payload["url"])) if payload.get("url") else None) + if url_hash: + self._seen_url_hashes.add(str(url_hash)) + if self._seen_url_hashes: + self.index_path.write_text("\n".join(sorted(self._seen_url_hashes)) + "\n", encoding="utf-8") + + async def store(self, result: CrawlResult) -> None: + if result.url_hash in self._seen_url_hashes: + return + with self.path.open("a", encoding="utf-8") as file_handle: + file_handle.write(json.dumps(result.to_dict(), ensure_ascii=False) + "\n") + with self.index_path.open("a", encoding="utf-8") as file_handle: + file_handle.write(result.url_hash + "\n") + self._seen_url_hashes.add(result.url_hash) + + async def close(self) -> None: + return None + + +class MongoResultRepository(ResultRepository): + def __init__(self, settings: StorageSettings) -> None: + self.client = AsyncIOMotorClient(settings.mongodb_dsn) + self.collection = self.client[settings.mongodb_database][settings.mongodb_collection] + + async def setup(self) -> None: + await self.collection.create_index("task_id", unique=False) + await self.collection.create_index( + "url_hash", + unique=True, + partialFilterExpression={"url_hash": {"$exists": True}}, + ) + + async def store(self, result: CrawlResult) -> None: + await self.collection.update_one( + {"url_hash": result.url_hash}, + {"$set": result.to_dict()}, + upsert=True, + ) + + async def close(self) -> None: + self.client.close() + + +class PostgresResultRepository(ResultRepository): + def __init__(self, settings: StorageSettings) -> None: + self.dsn = settings.postgres_dsn + self.table = settings.postgres_table + self.pool: asyncpg.Pool | None = None + + async def setup(self) -> None: + self.pool = await asyncpg.create_pool(self.dsn) + async with self.pool.acquire() as connection: + await connection.execute( + f""" + CREATE TABLE IF NOT EXISTS {self.table} ( + id BIGSERIAL PRIMARY KEY, + task_id TEXT NOT NULL, + worker_id TEXT NOT NULL, + status TEXT NOT NULL, + url TEXT NOT NULL, + url_hash TEXT NOT NULL, + spider TEXT NOT NULL, + http_status INTEGER, + payload JSONB NOT NULL, + error TEXT, + finished_at TIMESTAMPTZ NOT NULL + ) + """ + ) + await connection.execute(f"ALTER TABLE {self.table} ADD COLUMN IF NOT EXISTS url_hash TEXT") + await connection.execute( + f"CREATE UNIQUE INDEX IF NOT EXISTS {self.table}_url_hash_uidx ON {self.table} (url_hash)" + ) + + async def store(self, result: CrawlResult) -> None: + if self.pool is None: + raise RuntimeError("Postgres repository is not initialized") + async with self.pool.acquire() as connection: + await connection.execute( + f""" + INSERT INTO {self.table} + (task_id, worker_id, status, url, url_hash, spider, http_status, payload, error, finished_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8::jsonb, $9, $10::timestamptz) + ON CONFLICT (url_hash) DO UPDATE SET + task_id = EXCLUDED.task_id, + worker_id = EXCLUDED.worker_id, + status = EXCLUDED.status, + url = EXCLUDED.url, + spider = EXCLUDED.spider, + http_status = EXCLUDED.http_status, + payload = EXCLUDED.payload, + error = EXCLUDED.error, + finished_at = EXCLUDED.finished_at + """, + result.task_id, + result.worker_id, + result.status.value, + result.url, + result.url_hash, + result.spider, + result.http_status, + json.dumps(result.data, ensure_ascii=False), + result.error, + result.finished_at, + ) + + async def close(self) -> None: + if self.pool is not None: + await self.pool.close() + + +def build_result_repository(settings: StorageSettings, jsonl_path: Path) -> ResultRepository: + backend = settings.backend.lower() + if backend == "mongodb": + return MongoResultRepository(settings) + if backend == "postgresql": + return PostgresResultRepository(settings) + return JsonlResultRepository(jsonl_path) diff --git a/distributed_crawler/crawler/scheduler.py b/distributed_crawler/crawler/scheduler.py new file mode 100644 index 0000000..bf0d98e --- /dev/null +++ b/distributed_crawler/crawler/scheduler.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +from apscheduler.schedulers.asyncio import AsyncIOScheduler +from apscheduler.triggers.cron import CronTrigger +from apscheduler.triggers.interval import IntervalTrigger + +from .config import AppSettings +from .models import CrawlTask + + +class ScheduleService: + def __init__(self, settings: AppSettings, submit_callback) -> None: + self.settings = settings + self.submit_callback = submit_callback + self.scheduler = AsyncIOScheduler() + + def start(self) -> None: + for item in self.settings.schedules: + if item.trigger == "cron" and item.cron: + trigger = CronTrigger.from_crontab(item.cron) + else: + seconds = item.seconds or 0 + minutes = item.minutes or 0 + trigger = IntervalTrigger(seconds=seconds, minutes=minutes) + self.scheduler.add_job(self._submit_job, trigger=trigger, args=[item], id=item.name, replace_existing=True) + self.scheduler.start() + + async def shutdown(self) -> None: + self.scheduler.shutdown(wait=False) + + async def _submit_job(self, item) -> None: + payload = dict(item.task) + task = CrawlTask.create( + url=payload["url"], + spider=payload.get("spider", self.settings.master.default_spider), + method=payload.get("method", "GET"), + headers=payload.get("headers"), + metadata=payload.get("metadata"), + body=payload.get("body"), + priority=item.priority, + schedule_name=item.name, + ) + await self.submit_callback(task) diff --git a/distributed_crawler/crawler/sessions.py b/distributed_crawler/crawler/sessions.py new file mode 100644 index 0000000..f50ffdd --- /dev/null +++ b/distributed_crawler/crawler/sessions.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +from redis.asyncio import Redis + + +class DualSessionStore: + def __init__(self, redis: Redis, redis_prefix: str, backup_path: Path) -> None: + self.redis = redis + self.redis_prefix = redis_prefix + self.backup_path = backup_path + self.backup_path.parent.mkdir(parents=True, exist_ok=True) + + def _key(self, domain: str) -> str: + return f"{self.redis_prefix}:{domain}" + + async def load(self, domain: str) -> dict[str, Any]: + raw = await self.redis.get(self._key(domain)) + if raw: + return json.loads(raw) + if self.backup_path.exists(): + data = json.loads(self.backup_path.read_text(encoding="utf-8")) + return data.get(domain, {}) + return {} + + async def save(self, domain: str, payload: dict[str, Any]) -> None: + await self.redis.set(self._key(domain), json.dumps(payload)) + snapshot: dict[str, Any] = {} + if self.backup_path.exists(): + snapshot = json.loads(self.backup_path.read_text(encoding="utf-8")) + snapshot[domain] = payload + self.backup_path.write_text(json.dumps(snapshot, ensure_ascii=False, indent=2), encoding="utf-8") diff --git a/distributed_crawler/crawler/signatures.py b/distributed_crawler/crawler/signatures.py new file mode 100644 index 0000000..2a6f6dc --- /dev/null +++ b/distributed_crawler/crawler/signatures.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Any + +import aiohttp + + +class SignatureProvider(ABC): + @abstractmethod + async def sign(self, request_payload: dict[str, Any]) -> dict[str, Any]: ... + + +class NoopSignatureProvider(SignatureProvider): + async def sign(self, request_payload: dict[str, Any]) -> dict[str, Any]: + return {} + + +class RpcSignatureProvider(SignatureProvider): + def __init__(self, endpoint: str) -> None: + self.endpoint = endpoint + + async def sign(self, request_payload: dict[str, Any]) -> dict[str, Any]: + if not self.endpoint: + return {} + async with aiohttp.ClientSession() as session: + async with session.post(self.endpoint, json=request_payload) as response: + response.raise_for_status() + data = await response.json() + return dict(data) diff --git a/distributed_crawler/crawler/spider.py b/distributed_crawler/crawler/spider.py new file mode 100644 index 0000000..e05df49 --- /dev/null +++ b/distributed_crawler/crawler/spider.py @@ -0,0 +1,89 @@ +from __future__ import annotations + +import json +from abc import ABC, abstractmethod +from pathlib import Path +from typing import Any +from urllib.parse import urlparse + +from .http import AsyncHttpClient +from .models import CrawlResult, CrawlTask, TaskStatus + + +class BaseSpider(ABC): + name: str + + @abstractmethod + async def execute(self, task: CrawlTask, client: AsyncHttpClient, worker_id: str) -> CrawlResult: ... + + +class GenericFetchSpider(BaseSpider): + name = "generic_fetch" + + async def execute(self, task: CrawlTask, client: AsyncHttpClient, worker_id: str) -> CrawlResult: + response = await client.fetch(task) + data: dict[str, Any] = { + "headers": response["headers"], + "body": response["body"], + "proxy": response["proxy"], + "request_id": task.metadata.get("request_id"), + } + status = TaskStatus.SUCCESS if 200 <= response["status"] < 400 else TaskStatus.FAILED + return CrawlResult( + task_id=task.task_id, + worker_id=worker_id, + status=status, + url=task.url, + spider=self.name, + http_status=response["status"], + data=data, + error=None if status == TaskStatus.SUCCESS else f"Unexpected status {response['status']}", + ) + + +class AssetDownloadSpider(BaseSpider): + name = "asset_download" + + async def execute(self, task: CrawlTask, client: AsyncHttpClient, worker_id: str) -> CrawlResult: + response = await client.fetch(task) + parsed = urlparse(task.url) + file_name = task.metadata.get("file_name") or Path(parsed.path or "resource.bin").name or "resource.bin" + saved_to = await client.save_binary(task, file_name, response["body_bytes"]) + return CrawlResult( + task_id=task.task_id, + worker_id=worker_id, + status=TaskStatus.SUCCESS if response["status"] < 400 else TaskStatus.FAILED, + url=task.url, + spider=self.name, + http_status=response["status"], + data={"saved_to": saved_to, "proxy": response["proxy"], "request_id": task.metadata.get("request_id")}, + error=None if response["status"] < 400 else f"Unexpected status {response['status']}", + ) + + +class JsonApiSpider(BaseSpider): + name = "json_api" + + async def execute(self, task: CrawlTask, client: AsyncHttpClient, worker_id: str) -> CrawlResult: + response = await client.fetch(task) + body = response["body"] + if isinstance(body, str): + try: + body = json.loads(body) + except json.JSONDecodeError: + body = {"raw": body} + return CrawlResult( + task_id=task.task_id, + worker_id=worker_id, + status=TaskStatus.SUCCESS if response["status"] < 400 else TaskStatus.FAILED, + url=task.url, + spider=self.name, + http_status=response["status"], + data={"payload": body, "proxy": response["proxy"], "request_id": task.metadata.get("request_id")}, + error=None if response["status"] < 400 else f"Unexpected status {response['status']}", + ) + + +def build_spider_registry() -> dict[str, BaseSpider]: + spiders: list[BaseSpider] = [GenericFetchSpider(), AssetDownloadSpider(), JsonApiSpider()] + return {spider.name: spider for spider in spiders} diff --git a/distributed_crawler/crawler/state.py b/distributed_crawler/crawler/state.py new file mode 100644 index 0000000..a2f7945 --- /dev/null +++ b/distributed_crawler/crawler/state.py @@ -0,0 +1,85 @@ +from __future__ import annotations + +import json +from pathlib import Path + +from redis.asyncio import Redis + +from .models import CrawlTask, TaskStatus, utc_now + + +class CheckpointManager: + def __init__(self, redis: Redis, checkpoint_dir: Path, namespace: str) -> None: + self.redis = redis + self.checkpoint_dir = checkpoint_dir + self.namespace = namespace + self.checkpoint_dir.mkdir(parents=True, exist_ok=True) + + def _task_key(self, task_id: str) -> str: + return f"{self.namespace}:task:{task_id}" + + def _running_key(self, worker_id: str) -> str: + return f"{self.namespace}:running:{worker_id}" + + def _file_path(self, task_id: str) -> Path: + return self.checkpoint_dir / f"{task_id}.json" + + async def save(self, task: CrawlTask) -> None: + task.updated_at = utc_now() + await self.redis.set(self._task_key(task.task_id), json.dumps(task.to_dict())) + await self._write_file(task) + + async def mark_status(self, task: CrawlTask, status: TaskStatus) -> None: + task.status = status + await self.save(task) + if task.assigned_worker: + running_key = self._running_key(task.assigned_worker) + if status == TaskStatus.RUNNING: + await self.redis.zadd(running_key, {task.task_id: self._score_now()}) + elif status in {TaskStatus.SUCCESS, TaskStatus.FAILED}: + await self.redis.zrem(running_key, task.task_id) + self._cleanup_file(task.task_id) + + async def load(self, task_id: str) -> CrawlTask | None: + raw = await self.redis.get(self._task_key(task_id)) + if raw: + return CrawlTask.from_dict(json.loads(raw)) + file_path = self._file_path(task_id) + if file_path.exists(): + return CrawlTask.from_dict(json.loads(file_path.read_text(encoding="utf-8"))) + return None + + async def recover_stale(self, worker_id: str, older_than_seconds: int) -> list[CrawlTask]: + threshold = self._score_now() - older_than_seconds + task_ids = await self.redis.zrangebyscore(self._running_key(worker_id), min=0, max=threshold) + tasks: list[CrawlTask] = [] + for task_id in task_ids: + task = await self.load(task_id) + if task is None: + continue + if task.status == TaskStatus.RUNNING: + task.status = TaskStatus.RETRY + tasks.append(task) + return tasks + + async def recover_local(self) -> list[CrawlTask]: + tasks: list[CrawlTask] = [] + for file_path in self.checkpoint_dir.glob("*.json"): + payload = json.loads(file_path.read_text(encoding="utf-8")) + task = CrawlTask.from_dict(payload) + if task.status in {TaskStatus.PENDING, TaskStatus.RUNNING, TaskStatus.RETRY}: + tasks.append(task) + return tasks + + async def _write_file(self, task: CrawlTask) -> None: + self._file_path(task.task_id).write_text(json.dumps(task.to_dict(), ensure_ascii=False, indent=2), encoding="utf-8") + + def _cleanup_file(self, task_id: str) -> None: + file_path = self._file_path(task_id) + if file_path.exists(): + file_path.unlink() + + def _score_now(self) -> float: + from time import time + + return time() diff --git a/distributed_crawler/crawler/worker.py b/distributed_crawler/crawler/worker.py new file mode 100644 index 0000000..b6d1dee --- /dev/null +++ b/distributed_crawler/crawler/worker.py @@ -0,0 +1,186 @@ +from __future__ import annotations + +import asyncio +import logging +import socket +from pathlib import Path +from uuid import uuid4 + +import aiohttp +from redis.asyncio import Redis + +from .auth import ConfigurableAuthProvider, ManualAuthProvider +from .backoff import exponential_backoff +from .config import AppSettings +from .http import AsyncHttpClient +from .models import CrawlResult, CrawlTask, TaskStatus, WorkerHeartbeat +from .proxy import ProxyPool +from .queue import RedisPriorityQueue +from .rate_limit import DomainRateLimiter +from .sessions import DualSessionStore +from .signatures import NoopSignatureProvider +from .spider import BaseSpider, build_spider_registry +from .state import CheckpointManager + +LOGGER = logging.getLogger(__name__) + + +class WorkerNode: + def __init__(self, settings: AppSettings) -> None: + self.settings = settings + self.worker_id = settings.worker.worker_id + self.redis = Redis.from_url(settings.redis_dsn, decode_responses=True) + self.queue = RedisPriorityQueue( + self.redis, + settings.master.queue_name, + settings.master.result_queue_name, + settings.master.dead_letter_queue_name, + ) + self.checkpoints = CheckpointManager(self.redis, settings.resolve_path(settings.checkpoint_dir), settings.master.queue_name) + self.rate_limiter = DomainRateLimiter(settings.rate_limit) + self.proxy_pool = ProxyPool(settings.proxy) + self.session_store = DualSessionStore( + self.redis, + settings.session.redis_prefix, + settings.resolve_path(settings.session.local_backup_path), + ) + auth_provider = ConfigurableAuthProvider(settings.auth) if settings.auth.enabled else ManualAuthProvider() + self.client = AsyncHttpClient( + settings, + self.rate_limiter, + self.proxy_pool, + self.session_store, + auth_provider, + NoopSignatureProvider(), + ) + self.spiders: dict[str, BaseSpider] = build_spider_registry() + self.active_tasks: set[str] = set() + self.semaphore = asyncio.Semaphore(settings.worker.concurrency) + self.master_base_url = f"http://{settings.master.host}:{settings.master.port}" + self._loops: list[asyncio.Task] = [] + self._http_session: aiohttp.ClientSession | None = None + + async def start(self) -> None: + self._http_session = aiohttp.ClientSession() + await self.client.start() + if self.settings.proxy.enabled: + await self.proxy_pool.refresh() + await self.proxy_pool.verify() + recovered = await self.checkpoints.recover_local() + for task in recovered: + if task.assigned_worker in {None, self.worker_id, "default"}: + await self.queue.enqueue_task(self.worker_id, task) + self._loops = [ + asyncio.create_task(self.consume_loop(), name=f"{self.worker_id}-consume"), + asyncio.create_task(self.heartbeat_loop(), name=f"{self.worker_id}-heartbeat"), + asyncio.create_task(self.proxy_refresh_loop(), name=f"{self.worker_id}-proxy-refresh"), + ] + + async def close(self) -> None: + for task in self._loops: + task.cancel() + await asyncio.gather(*self._loops, return_exceptions=True) + await self.client.close() + if self._http_session is not None: + await self._http_session.close() + await self.redis.aclose() + + async def consume_loop(self) -> None: + while True: + try: + task = await self.queue.dequeue_task(self.worker_id, timeout=5) + if task is None: + task = await self.queue.dequeue_task("default", timeout=1) + if task is None: + continue + await self.semaphore.acquire() + asyncio.create_task(self.execute_task(task)) + except asyncio.CancelledError: + raise + except Exception: + LOGGER.exception("Worker %s consume loop failed", self.worker_id) + await asyncio.sleep(1) + + async def execute_task(self, task: CrawlTask) -> None: + self.active_tasks.add(task.task_id) + try: + task.assigned_worker = self.worker_id + task.metadata.setdefault("task_id", task.task_id) + task.metadata["worker_id"] = self.worker_id + task.metadata["request_id"] = f"{task.task_id}-{task.retries}-{uuid4().hex[:8]}" + await self.checkpoints.mark_status(task, TaskStatus.RUNNING) + spider = self.spiders.get(task.spider) + if spider is None: + raise RuntimeError(f"Unknown spider: {task.spider}") + result = await spider.execute(task, self.client, self.worker_id) + if result.status == TaskStatus.FAILED and task.retries < task.max_retries: + await self._retry_task(task, result.error or "worker failure") + else: + await self.queue.publish_result(result) + except Exception as exc: + LOGGER.exception("Task %s failed", task.task_id) + if task.retries < task.max_retries: + await self._retry_task(task, str(exc)) + else: + result = CrawlResult( + task_id=task.task_id, + worker_id=self.worker_id, + status=TaskStatus.FAILED, + url=task.url, + spider=task.spider, + error=str(exc), + ) + await self.queue.publish_result(result) + finally: + self.active_tasks.discard(task.task_id) + self.semaphore.release() + + async def heartbeat_loop(self) -> None: + while True: + try: + await self.send_heartbeat() + await asyncio.sleep(self.settings.worker.heartbeat_interval_seconds) + except asyncio.CancelledError: + raise + except Exception: + LOGGER.exception("Worker %s heartbeat failed", self.worker_id) + await asyncio.sleep(2) + + async def send_heartbeat(self) -> None: + if self._http_session is None: + return + heartbeat = WorkerHeartbeat( + worker_id=self.worker_id, + concurrency=self.settings.worker.concurrency, + active_tasks=len(self.active_tasks), + host=socket.gethostname(), + ) + async with self._http_session.post(f"{self.master_base_url}/heartbeat", json=heartbeat.to_dict()) as response: + response.raise_for_status() + + async def proxy_refresh_loop(self) -> None: + if not self.settings.proxy.enabled: + return + while True: + try: + await asyncio.sleep(self.settings.proxy.refresh_interval_seconds) + await self.proxy_pool.refresh() + await self.proxy_pool.verify() + LOGGER.info("Worker %s healthy proxies: %s", self.worker_id, await self.proxy_pool.healthy_count()) + except asyncio.CancelledError: + raise + except Exception: + LOGGER.exception("Proxy refresh loop failed") + + async def _retry_task(self, task: CrawlTask, reason: str) -> None: + task.retries += 1 + task.status = TaskStatus.RETRY + await self.checkpoints.save(task) + delay = exponential_backoff( + task.retries - 1, + self.settings.worker.base_backoff_seconds, + self.settings.worker.max_backoff_seconds, + ) + LOGGER.warning("Retry task %s in %.2fs: %s", task.task_id, delay, reason) + await asyncio.sleep(delay) + await self.queue.enqueue_task(self.worker_id, task) diff --git a/distributed_crawler/requirements.txt b/distributed_crawler/requirements.txt new file mode 100644 index 0000000..10ea770 --- /dev/null +++ b/distributed_crawler/requirements.txt @@ -0,0 +1,6 @@ +aiohttp>=3.10.10 +redis>=5.0.8 +APScheduler>=3.10.4 +PyYAML>=6.0.2 +motor>=3.6.0 +asyncpg>=0.30.0 diff --git a/distributed_crawler/scripts/run_master.py b/distributed_crawler/scripts/run_master.py new file mode 100644 index 0000000..3dd44a1 --- /dev/null +++ b/distributed_crawler/scripts/run_master.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +import argparse +import asyncio +import logging + +from aiohttp import web + +from crawler.config import load_settings +from crawler.master import MasterNode + + +async def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--config", required=True) + args = parser.parse_args() + + logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") + settings = load_settings(args.config) + node = MasterNode(settings) + await node.start() + + runner = web.AppRunner(node.app) + await runner.setup() + site = web.TCPSite(runner, settings.master.host, settings.master.port) + await site.start() + logging.getLogger(__name__).info("Master started at http://%s:%s", settings.master.host, settings.master.port) + + try: + while True: + await asyncio.sleep(3600) + finally: + await runner.cleanup() + await node.close() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/distributed_crawler/scripts/run_worker.py b/distributed_crawler/scripts/run_worker.py new file mode 100644 index 0000000..8366c4a --- /dev/null +++ b/distributed_crawler/scripts/run_worker.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +import argparse +import asyncio +import logging + +from crawler.config import load_settings +from crawler.worker import WorkerNode + + +async def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--config", required=True) + parser.add_argument("--worker-id", required=False) + args = parser.parse_args() + + logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") + settings = load_settings(args.config) + if args.worker_id: + settings.worker.worker_id = args.worker_id + + node = WorkerNode(settings) + await node.start() + logging.getLogger(__name__).info("Worker started: %s", settings.worker.worker_id) + + try: + while True: + await asyncio.sleep(3600) + finally: + await node.close() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/docs/2026-06-22_apk_refresh_fix.md b/docs/2026-06-22_apk_refresh_fix.md new file mode 100644 index 0000000..2fc2fca --- /dev/null +++ b/docs/2026-06-22_apk_refresh_fix.md @@ -0,0 +1,280 @@ +# APK 下拉刷新 / 删除后页面不刷新 修复 + +**日期**: 2026-06-22 +**问题**: 删除作品后页面不刷新;下拉刷新不生效,无法重新请求当前页数据 + +--- + +## 根因分析 + +### 根因 1:`RefreshIndicator` 放错位置,嵌套在 `FutureBuilder` 内部 + +`RefreshIndicator` 被放在 `FutureBuilder` 的 `builder` 闭包内。每次 `setState` 更新 `future` → `FutureBuilder` 重建 → `RefreshIndicator` 被销毁再创建 → 下拉手势/动画状态丢失。 + +**正常工作**的 `feed_tab.dart` 结构: +``` +RefreshIndicator → ListView → FutureBuilder(内部) +``` + +**异常结构**(修复前): +``` +FutureBuilder → RefreshIndicator → CustomScrollView +``` + +### 根因 2:缺少 `AlwaysScrollableScrollPhysics` + +`CustomScrollView` 未设置 `physics: AlwaysScrollableScrollPhysics()`。数据少于一屏时不可滚动 → `RefreshIndicator` 无法探测下拉手势。 + +### 根因 3(核心):API 客户端缓存导致的"假刷新" + +`api_client.dart` 的 `gallery()` 方法有 **30 分钟 SharedPreferences 缓存**: + +```dart +// 修复前 - 每次都先查缓存 +final cacheKey = _cacheKey('gallery', params); +final cached = await _readCache>(cacheKey); +if (cached != null) return cached; // ← 始终返回旧数据 +``` + +刷新链路全追踪: +``` +_refresh() + → ref.invalidate(apiClientProvider) ✅ 创建新 SakuraApiClient + → _load() + → gallery() + → _cacheKey('gallery', params) ❌ token 没变 → cache key 一样 + → _readCache(cacheKey) ❌ 命中 30 分钟前的缓存 + → 返回删除/修改前的旧数据 ❌ 表现:刷新无效 +``` + +Token 不变 → `_cacheKey` 内 `token.hashCode` 不变 → **缓存 key 不变** → 永远返回缓存。即使 `ref.invalidate` 创建了新客户端实例也无效,因为缓存存在 SharedPreferences 中,跨实例共享。 + +--- + +## 修改文件清单(3 个文件) + +| 文件 | 改动 | +|------|------| +| `sakura_mobile/lib/src/core/api_client.dart` | `gallery()` 加 `bypassCache` 参数 | +| `sakura_mobile/lib/src/pages/tabs/gallery_tab.dart` | 结构调整 + 缓存穿透 + 删除刷新按钮 | +| `sakura_mobile/lib/src/pages/tabs/novels_tab.dart` | 结构调整 + 删除刷新按钮 | + +--- + +## 改动详情 + +### 一、`sakura_mobile/lib/src/core/api_client.dart` + +#### `gallery()` 加 `bypassCache` 参数 + +**改动前**: +```dart + Future> gallery({ + String? search, + String searchScope = 'all', + bool? isFavorite, + String? contentType, + String? sort, + int page = 1, + int limit = 60, + bool includeTotal = true, + }) async { + ... + final cacheKey = _cacheKey('gallery', params); + final cached = await _readCache>(cacheKey); + if (cached != null) return cached; + final response = await dio.get('/api/v1/gallery', queryParameters: params); + final data = response.data['data']['items'] as List; + await _writeCache(cacheKey, data); + return data; + } +``` + +**改动后**: +```dart + Future> gallery({ + String? search, + String searchScope = 'all', + bool? isFavorite, + String? contentType, + String? sort, + int page = 1, + int limit = 60, + bool includeTotal = true, + bool bypassCache = false, // ← 新增 + }) async { + ... + final cacheKey = _cacheKey('gallery', params); + if (!bypassCache) { // ← 新增:仅在非刷新时读缓存 + final cached = await _readCache>(cacheKey); + if (cached != null) return cached; + } + final response = await dio.get('/api/v1/gallery', queryParameters: params); + final data = response.data['data']['items'] as List; + await _writeCache(cacheKey, data); + return data; + } +``` + +--- + +### 二、`sakura_mobile/lib/src/pages/tabs/gallery_tab.dart` + +#### 改动 1:`_load()` 加 `forceRefresh` 参数,传递到 `gallery()` + +**改动前**: +```dart + Future> _load({bool reset = true}) async { + if (reset) { + _page = 1; + _hasMore = true; + _items.clear(); + selectedIds.clear(); + selectionMode = false; + } + final rows = await ref.read(apiClientProvider).gallery( + search: search.text.trim().isEmpty ? null : search.text.trim(), + searchScope: searchScope, + isFavorite: favoritesOnly ? true : null, + contentType: _effectiveContentType, + sort: recentUpdatesOnly ? 'updated' : null, + page: _page, + limit: _pageSize, + includeTotal: false, + ); +``` + +**改动后**: +```dart + Future> _load({bool reset = true, bool forceRefresh = false}) async { + if (reset) { + _page = 1; + _hasMore = true; + _items.clear(); + selectedIds.clear(); + selectionMode = false; + } + final rows = await ref.read(apiClientProvider).gallery( + search: search.text.trim().isEmpty ? null : search.text.trim(), + searchScope: searchScope, + isFavorite: favoritesOnly ? true : null, + contentType: _effectiveContentType, + sort: recentUpdatesOnly ? 'updated' : null, + page: _page, + limit: _pageSize, + includeTotal: false, + bypassCache: forceRefresh, // ← 新增:刷新时绕过缓存 + ); +``` + +#### 改动 2:`_refresh()` 改为 async,传递 `forceRefresh: true` + +**改动前**: +```dart + void _refresh() { + setState(() { + future = _load(); + }); + } +``` + +**改动后**: +```dart + Future _refresh() async { + ref.invalidate(apiClientProvider); + final f = _load(forceRefresh: true); // ← 绕过缓存 + setState(() { + future = f; + }); + await f; // ← RefreshIndicator 等 future 完成才停止动画 + } +``` + +#### 改动 3:`RefreshIndicator` 移到 `FutureBuilder` 外层 + +**改动前**: +```dart + return FutureBuilder>( + future: future, + builder: (context, snapshot) { + ... + return RefreshIndicator( + onRefresh: _refresh, + child: CustomScrollView( + controller: _scrollController, + slivers: [...], + ), + ); + }, + ); +``` + +**改动后**: +```dart + return RefreshIndicator( + color: const Color(0xFFD95A83), + onRefresh: _refresh, + child: FutureBuilder>( + future: future, + builder: (context, snapshot) { + ... + return CustomScrollView( + controller: _scrollController, + physics: const AlwaysScrollableScrollPhysics(), // ← 新增 + slivers: [...], + ); + }, + ), + ); +``` + +#### 改动 4:删除刷新 IconButton + +**改动前**: +```dart + trailing: [ + ... + IconButton( + tooltip: '刷新', + onPressed: _refresh, + icon: const Icon(Icons.refresh), + ), + ], +``` + +**改动后**: +```dart + trailing: [ + ... // 刷新按钮已移除,统一使用下拉刷新 + ], +``` + +--- + +### 三、`sakura_mobile/lib/src/pages/tabs/novels_tab.dart` + +同 gallery_tab 的改动 3、4(结构调整 + 删除按钮)。novels API 本身无缓存,无需 `bypassCache`。 + +--- + +## 修复后的刷新链路 + +``` +用户下拉 → RefreshIndicator.onRefresh → _refresh() + → ref.invalidate(apiClientProvider) ✅ 创建新客户端 + → _load(forceRefresh: true) + → gallery(bypassCache: true) ✅ 跳过缓存 + → dio.get('/api/v1/gallery') ✅ 真实 HTTP 请求 + → _writeCache(cacheKey, newData) ✅ 写入新缓存 + → 返回服务器最新数据 ✅ + → setState({future: f}) ✅ FutureBuilder 拿到新 future + → await f ✅ 等待完成,停止下拉动画 + → UI 展示最新数据 ✅ 删除/修改后的数据正确 +``` + +## 构建产物 + +| 产物 | 路径 | 大小 | +|------|------|------| +| APK | `release/sakura.apk` | 66.8 MB | +| EXE | `dist/sakuradownloader.exe` | 63 MB | diff --git a/docs/2026-06-22_cloud_sync_401_fix.md b/docs/2026-06-22_cloud_sync_401_fix.md new file mode 100644 index 0000000..6e7ba54 --- /dev/null +++ b/docs/2026-06-22_cloud_sync_401_fix.md @@ -0,0 +1,310 @@ +# 云同步 401 自动刷新 Token 修复 + +**日期**: 2026-06-22 +**问题**: 桌面端 `POST /api/v1/sync/check` 持续返回 401,无重试/续期机制 +**根因**: `CloudSyncConfig` 只存 `access_token`,无 `refresh_token` 和自动续期逻辑 + +--- + +## 修改文件清单 + +| 文件 | 改动类型 | +|------|----------| +| `pixiv_app/core/cloud_sync.py` | 核心:加 refresh_token、401 自动刷新、token 更新回调 | +| `pixiv_app/gui/app.py` | 存储/加载/传递 refresh_token,回调持久化 | +| `pixiv_app/core/gallery_service.py` | 传递 refresh_token | +| `pixiv_app/core/origin_follow_sync.py` | 传递 refresh_token | +| `pixiv_app/core/x_media_service.py` | 传递 refresh_token | + +--- + +## 改动详情 + +### 1. `pixiv_app/core/cloud_sync.py` + +#### 1.1 `CloudSyncConfig` — 加 `refresh_token` 字段 + +**改动前**: +```python +@dataclass +class CloudSyncConfig: + base_url: str + access_token: str + chunk_size: int = 5 * 1024 * 1024 + timeout: float = 60.0 +``` + +**改动后**: +```python +@dataclass +class CloudSyncConfig: + base_url: str + access_token: str + refresh_token: str = "" + chunk_size: int = 5 * 1024 * 1024 + timeout: float = 60.0 +``` + +#### 1.2 `SakuraCloudClient.__init__` — 加 `on_token_refreshed` 回调 + +**改动前**: +```python +class SakuraCloudClient: + def __init__(self, config: CloudSyncConfig) -> None: + self.config = config + self.base_url = config.base_url.rstrip("/") + self.session = requests.Session() + self.session.headers.update({"Authorization": f"Bearer {config.access_token}"}) +``` + +**改动后**: +```python +class SakuraCloudClient: + def __init__(self, config: CloudSyncConfig, *, on_token_refreshed: callable | None = None) -> None: + self.config = config + self.base_url = config.base_url.rstrip("/") + self.session = requests.Session() + self.session.headers.update({"Authorization": f"Bearer {config.access_token}"}) + self._on_token_refreshed = on_token_refreshed + self._refresh_lock = False # 防止并发刷新 +``` + +#### 1.3 `_post` — 加 401 自动刷新 + 重试 + +**改动前**: +```python +def _post(self, path: str, payload: dict[str, Any]) -> dict[str, Any]: + response = self.session.post(f"{self.base_url}{path}", json=payload, timeout=self.config.timeout) + return self._unwrap(response) +``` + +**改动后**: +```python +def _post(self, path: str, payload: dict[str, Any], *, _retry_on_401: bool = True) -> dict[str, Any]: + response = self.session.post(f"{self.base_url}{path}", json=payload, timeout=self.config.timeout) + if response.status_code == 401 and _retry_on_401: + refreshed = self._try_refresh_token() + if refreshed: + return self._post(path, payload, _retry_on_401=False) + return self._unwrap(response) +``` + +#### 1.4 新增 `_try_refresh_token` 方法 + +**新增代码**: +```python +def _try_refresh_token(self) -> bool: + """用 refresh_token 换取新的 access_token。成功返回 True 并通知上层持久化。""" + refresh = self.config.refresh_token.strip() + if not refresh or self._refresh_lock: + return False + self._refresh_lock = True + try: + resp = self.session.post( + f"{self.base_url}/api/v1/auth/refresh", + json={"refresh_token": refresh}, + timeout=self.config.timeout, + ) + if resp.status_code != 200: + return False + body = resp.json() + data = body.get("data", body) if isinstance(body, dict) else {} + new_access = (data.get("access_token") or "").strip() + new_refresh = (data.get("refresh_token") or "").strip() + if not new_access: + return False + self.config.access_token = new_access + self.session.headers.update({"Authorization": f"Bearer {new_access}"}) + if new_refresh: + self.config.refresh_token = new_refresh + if self._on_token_refreshed: + try: + self._on_token_refreshed(self.config.access_token, self.config.refresh_token) + except Exception: + pass # 持久化失败不阻塞业务 + return True + except Exception: + return False + finally: + self._refresh_lock = False +``` + +--- + +### 2. `pixiv_app/gui/app.py` + +#### 2.1 新增 `cloud_refresh_token` 实例变量 + +**改动前**: +```python +self.cloud_access_token = "" +``` + +**改动后**: +```python +self.cloud_access_token = "" +self.cloud_refresh_token = "" +``` + +#### 2.2 `get_cloud_sync_config` — 加 `refresh_token` + +**改动前**: +```python +def get_cloud_sync_config(self) -> dict: + return { + "enabled": bool(self.cloud_sync_enabled_var.get()), + "base_url": (self.cloud_base_url or DEFAULT_CLOUD_API).strip(), + "access_token": self.cloud_access_token.strip(), + } +``` + +**改动后**: +```python +def get_cloud_sync_config(self) -> dict: + return { + "enabled": bool(self.cloud_sync_enabled_var.get()), + "base_url": (self.cloud_base_url or DEFAULT_CLOUD_API).strip(), + "access_token": self.cloud_access_token.strip(), + "refresh_token": self.cloud_refresh_token.strip(), + } +``` + +#### 2.3 登录成功时捕获 `refresh_token` + +**改动前**: +```python +token = data.get("data", {}).get("access_token") +if token: + self.cloud_access_token = str(token).strip() + self.enqueue_ui(self.cloud_status_var.set, "登录成功,云同步凭证已自动保存") +``` + +**改动后**: +```python +token = data.get("data", {}).get("access_token") +if token: + self.cloud_access_token = str(token).strip() + refresh = (data.get("data", {}).get("refresh_token") or "") + if refresh: + self.cloud_refresh_token = str(refresh).strip() + self.enqueue_ui(self.cloud_status_var.set, "登录成功,云同步凭证已自动保存") +``` + +#### 2.4 `restore_session` 加载 refresh_token + +**改动前**: +```python +self.cloud_access_token = str(cloud.get("access_token") or "") +if self.cloud_access_token: +``` + +**改动后**: +```python +self.cloud_access_token = str(cloud.get("access_token") or "") +self.cloud_refresh_token = str(cloud.get("refresh_token") or "") +if self.cloud_access_token: +``` + +#### 2.5 新增 `_on_cloud_token_refreshed` 回调 + 更新 `create_sync_manager` + +**改动前**: +```python +def create_sync_manager(self) -> SyncManager | None: + config = self.get_cloud_sync_config() + if not (config.get("enabled") and config.get("base_url") and config.get("access_token")): + return None + return SyncManager( + SakuraCloudClient( + CloudSyncConfig( + base_url=str(config["base_url"]), + access_token=str(config["access_token"]), + ) + ) + ) +``` + +**改动后**: +```python +def _on_cloud_token_refreshed(self, new_access: str, new_refresh: str) -> None: + """Token 自动刷新后更新内存并持久化到本地会话文件。""" + self.cloud_access_token = new_access + if new_refresh: + self.cloud_refresh_token = new_refresh + self.enqueue_ui(self.cloud_status_var.set, "云同步 Token 已自动续期") + self.enqueue_ui(self.save_session) + +def create_sync_manager(self) -> SyncManager | None: + config = self.get_cloud_sync_config() + if not (config.get("enabled") and config.get("base_url") and config.get("access_token")): + return None + return SyncManager( + SakuraCloudClient( + CloudSyncConfig( + base_url=str(config["base_url"]), + access_token=str(config["access_token"]), + refresh_token=str(config.get("refresh_token") or ""), + ), + on_token_refreshed=self._on_cloud_token_refreshed, + ) + ) +``` + +--- + +### 3. `pixiv_app/core/gallery_service.py` + +**改动前**: +```python + return SyncManager( + SakuraCloudClient( + CloudSyncConfig( + base_url=base_url, + access_token=access_token, + ) + ) + ) +``` + +**改动后**: +```python + return SyncManager( + SakuraCloudClient( + CloudSyncConfig( + base_url=base_url, + access_token=access_token, + refresh_token=str(cloud.get("refresh_token") or ""), + ) + ) + ) +``` + +--- + +### 4. `pixiv_app/core/origin_follow_sync.py` + +同 gallery_service.py,仅加 `refresh_token` 行。 + +### 5. `pixiv_app/core/x_media_service.py` + +同 gallery_service.py,仅加 `refresh_token` 行。 + +--- + +## 工作流程 + +``` + POST /api/v1/sync/check + ├── 200 → 正常返回 + └── 401 + ├── refresh_token 为空? → 抛异常(旧 token 永久失效) + ├── 正在刷新中? → 抛异常(防并发) + └── POST /api/v1/auth/refresh + ├── 200 → 更新内存 token → 回调持久化 → 重试原请求 + └── 非200 → 抛异常(refresh 也过期了,需重新登录) +``` + +## 兼容性 + +- `refresh_token` 默认值 `""` — 不传也能正常工作(旧行为) +- `on_token_refreshed` 可选 — 不传也不影响其他调用方 +- 现有的 `ConfigCloudSync` 构造方式全兼容 diff --git a/docs/APK端漫画小说源与WEB端认证修复方案.md b/docs/APK端漫画小说源与WEB端认证修复方案.md new file mode 100644 index 0000000..d072a40 --- /dev/null +++ b/docs/APK端漫画小说源与WEB端认证修复方案.md @@ -0,0 +1,1976 @@ +# APK 端漫画/小说第三方源集成 + WEB 端认证修复方案 + +> 更新日期:2026-06-15 +> 范围:Sakura Mobile (Flutter APK) + Sakura Cloud (FastAPI) + Sakura Admin Web (Vue.js) +> 说明:本文档提供完整的问题分析与实现方案,按模块划分,方便逐步实施。 + +--- + +## 目录 + +1. [APK 端:漫画与小说第三方来源网站集成](#一apk-端漫画与小说第三方来源网站集成) +2. [APK 端:未保存内容的在线搜索与实时浏览](#二apk-端未保存内容的在线搜索与实时浏览) +3. [WEB 端:认证失败与登录页缺失修复](#三web-端认证失败与登录页缺失修复) +4. [APK 端 UI 美化:配色、标题与选项卡](#四apk-端-ui-美化配色统一标题优化与选项卡样式修复) +5. [实施优先级与排期建议](#五实施优先级与排期建议) + +--- + +## 一、APK 端:漫画与小说第三方来源网站集成 + +### 1.1 现状分析 + +当前 Sakura Mobile Flutter 应用的画廊(Gallery)和小说(Novels)板块,数据来源仅限于: +- **自建 Sakura Cloud 后端**(`api.kunkunxiaomao.top:8000`)—— 即用户手动同步/上传到私有云的内容 +- 移动端 `api_client.dart` 中的 `gallery()` 和 `novels()` 方法直接调用云端 API +- **没有接入任何第三方漫画/小说源** + +### 1.2 目标与核心约束 + +为漫画板块和小说板块分别接入多个第三方内容源,用户在 APP 内可选择不同来源站点浏览内容。 + +#### 📌 核心设计原则 + +> **原则 1:三层漏斗模型 —— 6000+ 是仓库规模,不是搜索规模** +> +> ``` +> 源仓库 Repo(5 个预置入口) +> │ 用户按需导入 +> ▼ +> 源规则 Source(6000+ 条规则,按需存本地) +> │ 用户手动启用 +> ▼ +> 已启用源 Enabled Source(默认 6 个精选,用户自行扩展) +> ``` +> +> - **第一层**:APP 内置 5 个源仓库 **入口 URL**(不内置规则本身) +> - **第二层**:用户从仓库导入规则后,规则存于本地(不自动启用) +> - **第三层**:用户手动勾选启用哪些源,**只有已启用的源参与搜索** +> - 默认策略:首次安装仅启用 **精选小说源 3 个 + 精选漫画源 3 个** + +> **原则 2:纯实时流式读取,不下载到云端或本地** +> +> 所有第三方源的内容**仅做实时流式浏览**,不经过 Sakura Cloud 后端存储,也不写入手机本地持久化目录。 +> - 图片/文本在 APP 内存中临时加载,离开页面即释放 +> - 不使用 `CachedNetworkImage` 的持久缓存(可用内存缓存,退出 APP 清空) +> - **不触发任何下载行为**:不保存到系统图库、不写入本地文件、不同步到私有云 + +> **原则 3:跨源聚合搜索 + 安全限流** +> +> 用户输入一个关键词 → 并行查询**已启用的源**,结果按来源分组展示。 +> - 搜索一次 = 并行请求**已启用的源**(默认 6 个,非 6000 个) +> - 最大并发 10 个源、单源超时 5 秒、失败静默跳过 +> - 用户在"源管理"中可自行扩展启用的源数量 +> - 结果按来源分组,同作品在不同源中按源分别列出 + +> **原则 4:APK 不支持 Pixiv 实时查看(⚠️ 重要)** +> +> Pixiv 的反爬虫机制导致其图片/小说内容**无法在移动端直接实时访问**: +> - Pixiv 图片服务器(`i.pximg.net`)有严格的 Referer 检查,APP 内直接加载会 403 +> - 因此 **Pixiv 不接入第三方实时源**,Pixiv 内容仍走现有的"桌面端下载 → 同步到私有云 → APP 浏览"流程 +> - 其他源(Legado 生态中的公开源)不受此限制 + +### 1.3 源站方案:基于 Legado(阅读)规则引擎 + 预置源仓库 + +与其为每个站点手写解析器(工作量大、维护成本高),更合理的方案是:**在 Flutter 中内置一个轻量级 Legado 规则引擎**,直接复用"阅读"生态已有的 6000+ 条书源规则。 + +#### 推荐的预置源仓库 + +| 源名称 | 地址 | 规模 | 用途 | +|--------|------|:--:|------| +| **全量综合源** | `https://legado.aoaostar.com/` | 6000+ | 小说 + 漫画 + 有声,首页一键导入 | +| **精简高速源** | `https://www.yckceo.com/yuedu/shuyuans/json/id/640.json` | ~2200 | 已校验去重,稳定可靠 | +| **漫画专享源** | `https://gcore.jsdelivr.net/gh/yuedu521/yiciyuan/1012.json` | 208 | 纯漫画源,聚焦漫画场景 | +| **订阅源(推荐)** | `https://www.yckceo.com/yuedu/rss/json/id/193.json` | 持续更新 | 自动推送最新源,一次配置永久省心 | +| **Venera 漫画源** | `https://git.nyne.dev/nyne/venera-configs/raw/branch/main/index.json` | 精选 | 漫画专用配置,持续维护 | + +#### Legado 源格式简介 + +每个源是一个 JSON 对象,核心字段: + +```json +{ + "bookSourceName": "源名称", + "bookSourceUrl": "https://example.com", + "bookSourceType": 0, + "ruleSearch": { + "bookList": "...", + "name": "...", + "author": "...", + "coverUrl": "...", + "detailUrl": "..." + }, + "ruleBookInfo": { + "name": "...", + "author": "...", + "coverUrl": "...", + "intro": "...", + "chapterList": "..." + }, + "ruleContent": { + "content": "..." + }, + "ruleToc": { + "chapterList": "...", + "chapterName": "...", + "chapterUrl": "..." + }, + "httpUserAgent": "Mozilla/5.0 ...", + "header": "{\"Referer\": \"...\"}" +} +``` + +**规则类型说明:** +- `ruleSearch` — 搜索规则(URL 模板 + 结果列表 CSS/JSONPath 选择器) +- `ruleBookInfo` — 书籍详情规则(封面、简介、作者等字段提取) +- `ruleToc` — 章节目录规则(章节列表 + 名称 + URL 提取) +- `ruleContent` — 正文内容规则(正文提取 + 广告/乱码清洗) +- 支持 CSS 选择器、XPath、正则、JSONPath 四种规则语法 +- 支持 JavaScript 预处理(高级源),但首版可先跳过 + +#### ❌ 明确排除的源 + +| 来源 | 排除原因 | +|------|----------| +| **Pixiv** (漫画+小说) | 图片服务器 `i.pximg.net` 有严格 Referer + Cookie 检查,APP 端直连 403;反爬机制频繁更新,绕过成本高。Pixiv 内容维持现有流程:桌面端下载 → 同步私有云 → APP 浏览 | +| **需要付费账号的源** | 首版先覆盖免费公开源 | + +### 1.4 架构设计 + +``` +┌──────────────────────────────────────────────────────────────────┐ +│ Sakura Mobile (Flutter) │ +├──────────────────────────────────────────────────────────────────┤ +│ │ +│ ┌─ 三层漏斗模型 ──────────────────────────────────────────────┐ │ +│ │ │ │ +│ │ ① 源仓库 Repo ② 源规则 Source ③ 已启用源 │ │ +│ │ ┌──────────────┐ ┌──────────────┐ ┌────────────┐ │ │ +│ │ │ 5 个预置入口 │ ──▶ │ 6000+ 条规则 │ ──▶ │ 6 个精选 │ │ │ +│ │ │ (本地仅存URL) │ │ (用户按需导入)│ │ (默认启用) │ │ │ +│ │ └──────────────┘ └──────────────┘ └────────────┘ │ │ +│ │ 规模:5 规模:按需 规模:6~N(可控) │ │ +│ │ │ │ +│ └──────────────────────────────────────────────────────────────┘ │ +│ │ +│ 搜索时:仅 ③ 已启用源 参与,最大并发 10,单源超时 5s │ +│ │ +├──────────────────────────────────────────────────────────────────┤ +│ │ +│ GalleryTab / NovelsTab / ExplorePage(统一搜索页) │ +│ │ │ +│ ├── 云库 Tab:现有流程(Pixiv 同步内容) │ +│ │ │ +│ └── 在线 Tab:纯实时流 │ +│ │ │ +│ ▼ │ +│ ┌────────────────────────┐ │ +│ │ SourceManager │ ← 管理三层:仓库/规则/启用 │ +│ │ - 预置仓库 URL │ │ +│ │ - 导入/启用/禁用 │ │ +│ │ - 默认精选源 6 个 │ │ +│ └───────────┬────────────┘ │ +│ │ │ +│ ▼ │ +│ ┌────────────────────────┐ │ +│ │ LegadoRuleEngine │ ← 规则引擎(纯 Dart) │ +│ │ CSS / XPath / JSON │ │ +│ └───────────┬────────────┘ │ +│ │ │ +│ ▼ │ +│ ┌────────────────────────────────────────────┐ │ +│ │ 搜索限流器:maxConcurrency=10, timeout=5s │ │ +│ └────────────────────┬───────────────────────┘ │ +│ │ │ +│ ▼ │ +│ ┌──────────────────┐ │ +│ │ 纯内存流式渲染 │ ← 不写磁盘、不入云库 │ +│ │ (Memory Only) │ │ +│ └──────────────────┘ │ +│ │ +│ ⚠️ Pixiv → 桌面端下载 → 同步云库 → APP 云库Tab 查看 │ +└──────────────────────────────────────────────────────────────────┘ +``` + +**搜索并发控制:** + +| 参数 | 默认值 | 说明 | +|------|:------:|------| +| 最大并发源数 | 10 | 一次搜索最多同时请求 10 个源 | +| 单源超时 | 5s | 超时自动放弃,不阻塞其他源 | +| 失败策略 | 静默跳过 | 单个源失败不影响搜索整体结果 | +| 默认启用源 | 6 | 小说 3 个 + 漫画 3 个精选源 | +| 用户可扩展至 | N | 高级设置中手动启用更多,无硬上限但受并发限制 | + +### 1.5 实现方案(Flutter 侧) + +#### Step 1:Legado 规则引擎(核心) + +在 `sakura_mobile/lib/src/legado/` 新建规则引擎: + +``` +sakura_mobile/lib/src/legado/ +├── legado_engine.dart # 引擎入口:加载源 → 执行搜索/详情/目录/正文 +├── rule_parser.dart # 规则解析器:解析 CSS/XPath/JSONPath/Regex 规则 +├── source_loader.dart # 源加载器:从 URL/文件/内置 assets 加载源 JSON +├── http_client.dart # HTTP 封装:自动添加 UA/Referer/Cookie +├── source_manager.dart # 源管理器:增删改查 + 启用禁用 + 订阅更新 +└── models/ + ├── legado_source.dart # Legado 源数据模型 + └── search_result.dart # 统一搜索结果模型 +``` + +**Legado 源数据模型:** + +```dart +// sakura_mobile/lib/src/legado/models/legado_source.dart + +class LegadoSource { + final String bookSourceName; // 源名称 + final String bookSourceUrl; // 源站点 URL + final int bookSourceType; // 0=文本 1=音频 2=图片(漫画) + final String? bookSourceGroup; // 分组标签 + final String? bookSourceComment;// 备注 + + // 搜索规则 + final SearchRule? ruleSearch; + // 书籍详情规则 + final BookInfoRule? ruleBookInfo; + // 章节目录规则 + final TocRule? ruleToc; + // 正文内容规则 + final ContentRule? ruleContent; + + // HTTP 配置 + final String? httpUserAgent; + final Map? headers; + + // 元数据 + final String? sourceUrl; // 来源 URL(远程导入时记录) + final DateTime? lastUpdated; + final bool enabled; // 用户可禁用某个源 + + // ... fromJson / toJson +} + +class SearchRule { + final String? url; // 搜索 URL 模板,{{key}} 替换为关键词 + final String? method; // GET/POST + final String? body; // POST body + final String? bookList; // 结果列表选择器 + final String? name; // 书名选择器 + final String? author; // 作者选择器 + final String? coverUrl; // 封面选择器 + final String? detailUrl; // 详情页 URL 选择器 + final String? kind; // 规则类型:css/xpath/json/regex +} + +class BookInfoRule { + final String? name; + final String? author; + final String? coverUrl; + final String? intro; // 简介 + final String? kind; + final String? chapterList; // 目录列表选择器(部分源目录在详情页) + final String? chapterName; + final String? chapterUrl; +} + +class TocRule { + final String? chapterList; + final String? chapterName; + final String? chapterUrl; + final String? kind; + final String? nextTocUrl; // 分页目录的下一页 +} + +class ContentRule { + final String? content; // 正文提取规则 + final String? kind; + final String? replaceRegex; // 正则清洗(去除广告等) + final String? webJs; // JavaScript 预处理(高级,可延后支持) +} +``` + +**规则引擎核心逻辑:** + +```dart +// sakura_mobile/lib/src/legado/legado_engine.dart + +class LegadoEngine { + final LegadoHttpClient _http; + + LegadoEngine({LegadoHttpClient? http}) : _http = http ?? LegadoHttpClient(); + + /// 根据搜索规则,发起搜索请求并解析结果 + Future> search( + LegadoSource source, + String keyword, { + int page = 1, + }) async { + final rule = source.ruleSearch; + if (rule == null) return []; + + // 1. 构建搜索 URL(替换 {{key}} 占位符) + final url = rule.url! + .replaceAll('{{key}}', Uri.encodeComponent(keyword)) + .replaceAll('{{page}}', page.toString()); + + // 2. 发起 HTTP 请求 + final body = await _http.request( + url: url, + method: rule.method ?? 'GET', + headers: source.headers, + userAgent: source.httpUserAgent, + ); + + // 3. 根据规则类型解析 HTML/JSON + final parser = RuleParser(body, baseUrl: source.bookSourceUrl); + final bookList = parser.selectList(rule.bookList!, kind: rule.kind); + + // 4. 对每条结果提取字段 + return bookList.map((item) { + return UnifiedSearchResult( + sourceName: source.bookSourceName, + title: parser.select(item, rule.name!, kind: rule.kind), + author: parser.select(item, rule.author, kind: rule.kind), + coverUrl: parser.select(item, rule.coverUrl, kind: rule.kind), + detailUrl: parser.select(item, rule.detailUrl!, kind: rule.kind), + ); + }).toList(); + } + + /// 获取书籍详情(元信息 + 章节目录) + Future getDetail(LegadoSource source, String detailUrl) async { ... } + + /// 获取章节正文(提取 + 清洗) + Future getContent(LegadoSource source, String chapterUrl) async { ... } +} +``` + +**规则解析器(RuleParser)—— 支持四种语法:** + +```dart +// sakura_mobile/lib/src/legado/rule_parser.dart +// 利用现有 pub.dev 包实现(无需从零造轮子) + +import 'package:html/dom.dart' as html_dom; +import 'package:html/parser.dart' as html_parser; +import 'package:xpath_parser/xpath_parser.dart'; +import 'package:json_path/json_path.dart'; + +class RuleParser { + final String _body; + final String _baseUrl; + html_dom.Document? _doc; + dynamic _json; // lazy parse + + RuleParser(this._body, {required String baseUrl}) : _baseUrl = baseUrl; + + /// 选择单个文本 + String select(String node, String? rule, {String? kind}) { + if (rule == null || rule.isEmpty) return ''; + final parsed = _parseRule(rule); + switch (kind ?? parsed.type) { + case 'css': + return _cssSelectOne(node, parsed.expression); + case 'xpath': + return _xpathSelectOne(node, parsed.expression); + case 'json': + return _jsonPathSelectOne(node, parsed.expression); + case 'regex': + return _regexExtract(node, parsed.expression); + default: + return _cssSelectOne(node, rule); + } + } + + /// 选择列表 + List selectList(String node, String? rule, {String? kind}) { ... } + + // 内部实现,利用成熟的 pub 包… + String _cssSelectOne(String html, String selector) { ... } + String _xpathSelectOne(String html, String xpath) { ... } + String _jsonPathSelectOne(String json, String path) { ... } +} +``` + +> **依赖包:** `html`(HTML DOM 解析)、`json_path`(JSONPath)、已有 pub 上有成熟的 CSS 选择器和 XPath 包可用。这些包都是纯 Dart 实现,不依赖原生平台代码,可直接在 Flutter 中使用。 + +#### Step 2:源管理器 —— 三层漏斗 + 默认精选 + +```dart +// sakura_mobile/lib/src/legado/source_manager.dart + +/// ┌─────────────────────────────────────────────────────┐ +/// │ 三层漏斗模型 │ +/// │ │ +/// │ ① 源仓库 Repo ② 源规则 Source ③ 已启用源 │ +/// │ 规模: 5 个预置 规模: 按需导入 规模: 6~N │ +/// │ 仅存 URL 用户手动启用 参与搜索 │ +/// └─────────────────────────────────────────────────────┘ + +class SourceManager { + // ============================================ + // ① 源仓库层:5 个预置入口(仅存 URL,不含规则) + // ============================================ + static const builtinSourceRepos = [ + SourceRepo( + name: '全量综合源', + url: 'https://legado.aoaostar.com/', + description: '6000+ 站点,含小说、漫画、有声', + category: '综合', + ), + SourceRepo( + name: '精简高速源', + url: 'https://www.yckceo.com/yuedu/shuyuans/json/id/640.json', + description: '约 2200 条,已校验去重,稳定可靠', + category: '综合', + ), + SourceRepo( + name: '漫画专享源', + url: 'https://gcore.jsdelivr.net/gh/yuedu521/yiciyuan/1012.json', + description: '208 条漫画专用源', + category: '漫画', + ), + SourceRepo( + name: '订阅源(推荐)', + url: 'https://www.yckceo.com/yuedu/rss/json/id/193.json', + description: '自动推送最新源,一次配置永久省心', + category: '订阅', + isSubscription: true, + ), + SourceRepo( + name: 'Venera 漫画源', + url: 'https://git.nyne.dev/nyne/venera-configs/raw/branch/main/index.json', + description: '精选漫画配置,持续维护', + category: '漫画', + ), + ]; + + // ============================================ + // ② 源规则层:所有已导入的规则(导入 ≠ 启用) + // ============================================ + final List _importedSources = []; + + // ============================================ + // ③ 已启用源:默认仅 6 个精选源 + // ============================================ + /// 首次安装默认启用的精选源名称列表 + static const defaultEnabledNovelSources = [ + '全本玄幻小说', // 小说 + '笔趣阁', // 小说 + '妙笔阁', // 小说 + ]; + + static const defaultEnabledMangaSources = [ + '包子漫画', // 漫画 + '古风漫画网', // 漫画 + 'COCO漫画', // 漫画 + ]; + + /// 首次启动时自动执行:导入精选源 + 启用 + Future initializeIfFirstRun() async { + final prefs = await SharedPreferences.getInstance(); + if (prefs.getBool('source_manager_initialized') == true) return; + + // 从精简高速源仓库导入规则(2200 条) + await importFromRepo(builtinSourceRepos[1]); // 精简高速源 + + // 仅启用 6 个精选源 + final allNovelNames = defaultEnabledNovelSources.toSet(); + final allMangaNames = defaultEnabledMangaSources.toSet(); + for (final source in _importedSources) { + if (allNovelNames.contains(source.bookSourceName) || + allMangaNames.contains(source.bookSourceName)) { + _enabledSourceNames.add(source.bookSourceName); + } + } + + await prefs.setBool('source_manager_initialized', true); + } + + /// 获取参与搜索的源(仅 ③ 已启用源) + List get enabledSources => + _importedSources.where((s) => _enabledSourceNames.contains(s.bookSourceName)).toList(); + + /// 从仓库导入规则(导入 ≠ 启用,用户需手动勾选) + Future importFromRepo(SourceRepo repo) async { ... } + + /// 从 URL 导入用户自定义源 + Future importFromUrl(String url) async { ... } + + /// 从本地 JSON 文件导入 + Future importFromFile(String filePath) async { ... } + + /// 手动启用/禁用某个源 + Future setEnabled(String sourceName, bool enabled) async { ... } +} +``` + +**首次安装体验流程:** + +``` +用户首次打开 APP + │ + ├─→ 自动从"精简高速源"仓库导入约 2200 条规则到本地 + │ + ├─→ 自动启用 6 个精选源(小说 3 + 漫画 3) + │ + ├─→ 用户在探索页搜索 → 仅这 6 个源参与 + │ + └─→ 用户进入"源管理" → 可看到 5 个仓库入口 + ├─→ 点击"全量综合源" → 导入 6000+ 条到本地 + ├─→ 逐一手动勾选启用 → 启用源扩展到 N 个 + └─→ 搜索时最多并发 10 个 +``` + +**搜索限流器:** + +```dart +// sakura_mobile/lib/src/legado/legado_engine.dart + +class LegadoEngine { + static const maxConcurrency = 10; // 最多同时请求 10 个源 + static const perSourceTimeout = Duration(seconds: 5); // 单源超时 + + /// 搜索入口:自动限制并发 + 超时 + Future> searchAll( + List enabledSources, + String keyword, + ) async { + // 只取前 N 个(如果用户启用了 >10 个源) + final batch = enabledSources.take(maxConcurrency).toList(); + + final results = []; + // 并行搜索,单源失败不影响其余 + final responses = await Future.wait( + batch.map((source) => + search(source, keyword) + .timeout(perSourceTimeout) + .catchError((_) => []) // 静默跳过失败源 + ), + ); + for (final list in responses) { + results.addAll(list); + } + return results; + } +} +``` +``` + +#### Step 3:UI 层 —— 源管理页面(展示三层漏斗) + +新建 `sakura_mobile/lib/src/pages/source_manager_page.dart`: + +``` +┌──────────────────────────────────────────────┐ +│ ← 源管理 [+ 自定义导入] │ +├──────────────────────────────────────────────┤ +│ │ +│ 📊 统计:已导入 2200 条 | 已启用 6 个 │ +│ │ +│ ── ① 源仓库(5 个预置入口) ────────────── │ +│ ┌──────────────────────────────────────────┐│ +│ │ 📚 全量综合源 [导入 6000+ 条] ││ +│ │ legado.aoaostar.com 未导入 ││ +│ ├──────────────────────────────────────────┤│ +│ │ 🚀 精简高速源 [已导入 2200 条] ││ +│ │ yckceo.com ✓ 已导入 ││ +│ ├──────────────────────────────────────────┤│ +│ │ 🎨 漫画专享源 [导入 208 条] ││ +│ │ 208 条漫画专用 未导入 ││ +│ ├──────────────────────────────────────────┤│ +│ │ 📡 订阅源(推荐) [订阅] ││ +│ │ 自动推送最新源 未订阅 ││ +│ ├──────────────────────────────────────────┤│ +│ │ 🎌 Venera 漫画源 [导入] ││ +│ │ 精选漫画配置 未导入 ││ +│ └──────────────────────────────────────────┘│ +│ │ +│ ── ② 源规则(已导入 2200 条)──────────── │ +│ ┌──────────────────────────────────────────┐│ +│ │ 🔍 搜索已导入的源... ││ +│ │ [✓ 已启用] [ ] 小说源 [ ] 漫画源 ││ +│ ├──────────────────────────────────────────┤│ +│ │ [✓] 全本玄幻小说 📖 小说 [禁用] ││ +│ │ [✓] 笔趣阁 📖 小说 [禁用] ││ +│ │ [✓] 妙笔阁 📖 小说 [禁用] ││ +│ │ [✓] 包子漫画 🎨 漫画 [禁用] ││ +│ │ [✓] 古风漫画网 🎨 漫画 [禁用] ││ +│ │ [✓] COCO漫画 🎨 漫画 [禁用] ││ +│ │ ── 以下未启用 ───────────────────────── ││ +│ │ [ ] 笔趣阁2 📖 小说 [启用] ││ +│ │ [ ] 顶点小说 📖 小说 [启用] ││ +│ │ [ ] ... 共 2194 条未启用 ││ +│ └──────────────────────────────────────────┘│ +│ │ +│ ── ③ 搜索配置 ────────────────────────── │ +│ ┌──────────────────────────────────────────┐│ +│ │ 最大并发源数: [■■■■■■■■■□] 10 ││ +│ │ 单源超时时间: [■■■■■□□□□□] 5 秒 ││ +│ │ 已启用源总数: 6 个(参与搜索) ││ +│ │ ││ +│ │ ⚠️ 启用源越多,搜索越慢。建议保持 ≤20 个 ││ +│ └──────────────────────────────────────────┘│ +│ │ +│ 🔗 高级:自定义导入 │ +│ ┌──────────────────────────────────────────┐│ +│ │ [https://... ] [从URL导入] ││ +│ │ [选择本地JSON文件] [从文件导入] ││ +│ └──────────────────────────────────────────┘│ +└──────────────────────────────────────────────┘ +``` + +#### Step 4:跨源搜索入口 + +在 GalleryTab / NovelsTab / ExplorePage 中调用: + +```dart +// 用户点击搜索后 +final manager = ref.read(sourceManagerProvider); +final engine = ref.read(legadoEngineProvider); +final enabledSources = manager.enabledSources; + +// 并行搜索所有已启用的源 + 云库 +final results = await Future.wait([ + // 云库搜索 + api.gallery(search: query, ...), + // 所有第三方源并行搜索 + ...enabledSources.map((source) => + engine.search(source, query).catchError((_) => []) + ), +]); +``` + +--- + +## 二、APK 端:未保存内容的在线搜索与实时浏览 + +### 2.1 现状分析 + +当前移动端的搜索功能仅限于**已同步到云端的内容**: +- `GalleryTab._load()` 调用 `api.gallery(search: ...)` → 仅查询 Sakura Cloud 数据库中已有的 Artwork +- `NovelsTab._load()` 调用 `api.novels(search: ...)` → 仅查询 Sakura Cloud 数据库中已有的 Novel +- **用户无法搜索尚未保存/同步到私有云的外部作品** + +### 2.2 目标 + +- 用户输入一个关键词 → **自动并行搜索所有第三方源 + 云库**,结果聚合展示 +- 搜索结果即时展示封面、标题、作者、来源等元信息 +- 点击后**纯内存流式浏览**,不下载到本地,不同步到云库 +- **Pixiv 不参与实时搜索/浏览**,Pixiv 内容仍走桌面端同步流程 + +### 2.3 跨源聚合搜索设计 + +``` +用户输入 "鬼灭之刃" + │ + ▼ +┌──────────────────────────────────────────────────────┐ +│ CrossSourceSearch.aggregate() │ +│ │ +│ 仅搜索 ③ 已启用源(默认 6 个,非 6000 个) │ +│ 并发限制 max=10, 单源超时 5s, 失败静默跳过 │ +│ │ +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ +│ │ 我的云库 │ │ 包子漫画 │ │ 古风漫画 │ ... │ +│ │ (已有) │ │ (启用) │ │ (启用) │ │ +│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ +│ │ │ │ │ +│ ▼ ▼ ▼ │ +│ ┌─────────────────────────────────────────────────┐ │ +│ │ AggregatedResults (按源分组) │ │ +│ │ │ │ +│ │ 📦 我的云库 (3条) ← Pixiv 同步内容 │ │ +│ │ 📦 包子漫画 (5条) ← Legado 规则引擎 │ │ +│ │ 📦 古风漫画网 (2条) │ │ +│ │ 📦 全本玄幻小说 (8条) │ │ +│ │ ...(共 6 个已启用源的结果) │ │ +│ │ │ │ +│ │ 💡 未找到?去 源管理 导入更多仓库并启用新源 │ │ +│ │ ❌ Pixiv — 不参与实时搜索 │ │ +│ └─────────────────────────────────────────────────┘ │ +└──────────────────────────────────────────────────────┘ +``` + +### 2.4 实现方案 + +#### Step 1:搜索 —— 跨源聚合搜索引擎 + +```dart +// sakura_mobile/lib/src/core/unified_search.dart (新增) + +class UnifiedSearch { + final SakuraApiClient _cloudApi; + final SourceManager _sourceManager; + + UnifiedSearch(this._cloudApi, this._sourceManager); + + /// 统一漫画搜索:同步搜索云库 + 所有第三方源 + Future searchManga( + String query, { + int page = 1, + Set? enabledSources, // 用户选择启用的源 + }) async { + final results = UnifiedSearchResults( + query: query, + sections: [], + ); + + // 1. 搜索私有云库(最快) + final cloudItems = await _cloudApi.gallery( + search: query, + page: page, + limit: 20, + contentType: 'manga', + ); + if (cloudItems.isNotEmpty) { + results.sections.add(SearchSection( + sourceId: 'cloud', + sourceName: '我的云库', + items: _toCloudArtworks(cloudItems), + )); + } + + // 2. 搜索第三方源 + final targetSources = _sourceManager.mangaSources + .where((s) => enabledSources == null || enabledSources.contains(s.sourceId)); + + final sourceResults = await Future.wait( + targetSources.map((source) => + source.search(query, page: page, limit: 20) + .then((items) => SearchSection( + sourceId: source.sourceId, + sourceName: source.sourceName, + items: items, + )) + .catchError((_) => null) // 单个源失败不影响其他 + ), + ); + + results.sections.addAll(sourceResults.whereType()); + return results; + } +} + +/// 搜索结果容器 +class UnifiedSearchResults { + final String query; + final List sections; + // 所有 items 可展平为列表 + + const UnifiedSearchResults({required this.query, required this.sections}); +} + +/// 按来源分组的搜索结果 +class SearchSection { + final String sourceId; + final String sourceName; + final List items; // MangaSearchResult | Artwork + + const SearchSection({ + required this.sourceId, + required this.sourceName, + required this.items, + }); +} +``` + +#### Step 2:实时浏览 —— 在线漫画阅读器(纯内存、零下载) + +新建 `sakura_mobile/lib/src/pages/online_manga_reader_page.dart`: + +> ⚠️ **关键约束:** 图片仅通过 `Image.network()` 或 `Image.memory()` 在内存中渲染,**不使用 `CachedNetworkImage` 的持久缓存**,不调用 `downloadProtectedFile()`,不写入任何本地文件。 + +```dart +// sakura_mobile/lib/src/pages/online_manga_reader_page.dart (新增) +import 'package:flutter/material.dart'; +import '../core/source_interface.dart'; + +/// 在线漫画阅读器 —— 纯实时流式浏览 +/// - 图片在内存中临时加载,离开页面即释放 +/// - 不保存到系统图库、不写入本地文件、不同步到私有云 +class OnlineMangaReaderPage extends StatefulWidget { + final MangaSource source; + final String mangaId; + final String chapterId; + final String title; + + const OnlineMangaReaderPage({ + super.key, + required this.source, + required this.mangaId, + required this.chapterId, + required this.title, + }); + + @override + State createState() => _OnlineMangaReaderPageState(); +} + +class _OnlineMangaReaderPageState extends State { + late Future> _pagesFuture; + final _pageController = PageController(); + int _currentPage = 0; + int _totalPages = 0; + + @override + void initState() { + super.initState(); + _pagesFuture = widget.source.getChapterPages( + widget.mangaId, + widget.chapterId, + ); + } + + @override + void dispose() { + _pageController.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(widget.title), + actions: [ + if (_totalPages > 0) + Center( + child: Padding( + padding: const EdgeInsets.only(right: 8), + child: Text( + '${_currentPage + 1} / $_totalPages', + style: const TextStyle(fontSize: 14), + ), + ), + ), + ], + ), + body: FutureBuilder>( + future: _pagesFuture, + builder: (context, snapshot) { + if (snapshot.hasError) { + return Center(child: Text('加载失败: ${snapshot.error}')); + } + if (!snapshot.hasData) { + return const Center(child: CircularProgressIndicator()); + } + + final pages = snapshot.data!; + _totalPages = pages.length; + + return PageView.builder( + controller: _pageController, + scrollDirection: Axis.horizontal, + itemCount: pages.length, + onPageChanged: (page) => setState(() => _currentPage = page), + itemBuilder: (context, index) { + final page = pages[index]; + return InteractiveViewer( + child: Center( + // ✅ 使用 Image.network(内存缓存),不用 CachedNetworkImage(磁盘缓存) + child: Image.network( + page.imageUrl, + headers: page.headers, + fit: BoxFit.contain, + loadingBuilder: (_, child, progress) { + if (progress == null) return child; + return const Center(child: CircularProgressIndicator()); + }, + errorBuilder: (_, __, ___) => + const Center(child: Icon(Icons.broken_image, size: 48)), + ), + ), + ); + }, + ); + }, + ), + ); + } +} +``` + +#### Step 3:在线小说阅读器 + +新建 `sakura_mobile/lib/src/pages/online_novel_reader_page.dart`: + +```dart +// 类似 OnlineMangaReaderPage,但渲染纯文本 +// 从第三方源获取章节正文后,滚动显示文本内容 +// 支持翻页、字体调整、夜间模式等 +``` + +#### Step 4:搜索页 UI 整合 + +新建 `sakura_mobile/lib/src/pages/explore_page.dart` —— 探索/发现页: + +``` +┌──────────────────────────────────────┐ +│ [🔍 搜索漫画或小说...] │ ← 搜索栏 +├──────────────────────────────────────┤ +│ 来源:[我的云库] [MangaDex] [漫画柜] │ ← 横向滚动来源选择 +├──────────────────────────────────────┤ +│ ┌────────┐ ┌────────┐ ┌────────┐ │ +│ │ 封面 │ │ 封面 │ │ 封面 │ │ +│ │ │ │ │ │ │ │ ← 瀑布流搜索结果 +│ │ 标题 │ │ 标题 │ │ 标题 │ │ +│ │ 来源 │ │ 来源 │ │ 来源 │ │ +│ └────────┘ └────────┘ └────────┘ │ +├──────────────────────────────────────┤ +│ 点击 → 直接进入在线阅读(不保存) │ +│ 长按 → 保存到我的云库 │ +└──────────────────────────────────────┘ +``` + +### 2.5 数据流总结 + +``` +用户搜索 "关键词" + │ + ├─→ Sakura Cloud /gallery?search=xxx (已有云库内容) + │ + ├─→ MangaDexSource.search("xxx") (第三方漫画源) + ├─→ CopymangaSource.search("xxx") (第三方漫画源) + ├─→ NarouSource.search("xxx") (第三方小说源) + │ + │ ❌ Pixiv — 不参与实时搜索 + │ → UI 上显示提示:"Pixiv 内容请通过桌面端同步后查看" + │ + ▼ +CrossSourceSearch.aggregate() + → 并行请求,按源分组,合并返回 + │ + ▼ + 统一 AggregatedResults + │ + ▼ + 用户点击某条结果 + │ + ├─→ 云库内容 → 现有 ArtworkDetailPage / NovelReaderPage + │ (可收藏、可下载、可同步进度) + │ + └─→ 第三方源内容 → OnlineMangaReaderPage / OnlineNovelReaderPage + │ + ├─→ Image.network() 逐页渲染(内存缓存,离开释放) + ├─→ 文本流式加载到 Text widget(内存,离开释放) + │ + ├─→ ❌ 不调用 downloadProtectedFile() + ├─→ ❌ 不写 SharedPreferences/本地文件 + ├─→ ❌ 不触发系统图库保存 + └─→ ❌ 不同步到 Sakura Cloud +``` + +--- + +## 三、WEB 端:认证失败与登录页缺失修复 + +### 3.1 问题现象 + +部署新版 Sakura Admin Web 后,访问页面始终返回: + +```json +{"detail": "Not authenticated"} +``` + +**没有出现登录页面**,直接报错,无法正常使用。 + +### 3.2 根因分析 + +经过对全部相关代码的排查,问题涉及以下多个层面: + +#### 根因 1:CSP(内容安全策略)过于严格 —— **核心问题** + +**文件位置:** `sakura_cloud/api/middleware.py` 第 37-41 行 + +```python +response.headers.setdefault( + "Content-Security-Policy", + "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; connect-src 'self'", +) +``` + +**问题:** +- `connect-src 'self'` 限制了所有 API 请求只能发送到同源 +- 当开发环境前端运行在 `localhost:5174`,后端运行在 `localhost:8000` 时,前端通过 Vite proxy 代理 API 请求到后端,但浏览器会因为 CSP 限制而阻止跨域 API 调用 +- `img-src 'self' data:` 限制了图片资源只能从同源加载,外部图片无法显示 +- 生产部署时,如果前端构建为静态文件通过 Nginx 部署,API 请求不同端口也会被 CSP 拦截 + +#### 根因 2:SPA 路由未正确回退 + +**文件位置:** `sakura_cloud/main.py` 第 61-78 行 + +```python +@app.get("/admin", response_class=HTMLResponse) +def admin_console() -> str: + if not settings.admin_frontend_enabled: + return "

Admin console disabled

" + return """...""" # 硬编码的占位 HTML,不是真正的 SPA +``` + +**问题:** +- 后端只注册了 `/admin` 这一个路由返回 HTML +- Vue Router 使用了 `createWebHistory()`(`sakura_admin_web/src/router/index.ts` 第 60 行),意味着 URL 如 `/login`、`/app`、`/admin/users` 等都是真实路径 +- 当用户直接访问 `/login` 或刷新 `/app/dashboard` 时,后端没有对应路由,返回 404 或触发认证检查 +- SPA 需要所有非 API 路径回退到 `index.html` + +#### 根因 3:前端 API 路径与后端不一致 + +**文件位置:** `sakura_admin_web/src/stores/auth.ts` 第 7-8 行 + +```typescript +const path = admin ? "/admin/login" : "/auth/login"; +const response = await http.post(path, { ... }); +``` + +**文件位置:** `sakura_admin_web/src/api/http.ts` 第 4 行 + +```typescript +const API_BASE = import.meta.env.VITE_API_BASE_URL || "/api/v1"; +``` + +实际 API 路径: +- 用户登录:`POST /api/v1/auth/login` +- 管理员登录:`POST /api/v1/admin/login` +- 获取当前用户:`GET /api/v1/me` + +**问题:** 前端 `http.ts` 的 `API_BASE` 默认是 `/api/v1`,而 Vite 的 proxy 配置是将 `/api` 代理到后端。这意味着前端的 `/api/v1/auth/login` 实际被代理为 `http://127.0.0.1:8000/api/v1/auth/login` —— 虽然这个路径是正确的,但当**不通过 Vite dev server 访问**(即生产部署)时,如果静态文件服务器没有正确配置代理,API 调用会失败。 + +#### 根因 4:前端路由守卫在 token 不存在时无限循环 + +**文件位置:** `sakura_admin_web/src/router/index.ts` 第 64-70 行 + +```typescript +router.beforeEach(async (to) => { + if (!to.meta.requiresAuth) return true; + const user = await loadMe().catch(() => null); + if (!user) return { path: "/login", query: { redirect: to.fullPath } }; + if (to.meta.requiresAdmin && !isAdmin(authState.user?.role)) return "/403"; + return true; +}); +``` + +**问题:** +- `loadMe()` 调用 `GET /api/v1/me`,需要 Bearer token +- 如果 token 不存在或已过期,API 返回 401,前端捕获错误后 `user` 为 null,重定向到 `/login` +- 但如果后端中间件在所有路由(包括 `/login` 对应的 API 调用)都要求认证,用户永远看不到登录页 +- `auth.ts` 中的 `login()` 在管理员登录时调用 `/admin/login`,但后端 `admin_login` 端点有 IP 速率限制(`admin_login_rate_limit_per_minute: 5`),开发时频繁刷新容易触发 + +#### 根因 5:缺少生产部署的静态文件服务配置 + +**文件位置:** `sakura_cloud/main.py` —— 没有挂载静态文件 + +后端没有配置 `app.mount()` 来服务前端构建产物。生产环境需要: +- 后端直接 serving SPA 静态文件(FastAPI `StaticFiles`) +- 或者 Nginx 反向代理 + 静态文件服务 + +### 3.3 解决方案 + +#### 方案 A:修复 CSP 中间件(立即执行) + +修改 `sakura_cloud/api/middleware.py`: + +```python +# sakura_cloud/api/middleware.py 修改后的 security_headers_middleware + +async def security_headers_middleware(request: Request, call_next: Callable) -> Response: + response = await call_next(request) + response.headers.setdefault("X-Content-Type-Options", "nosniff") + response.headers.setdefault("X-Frame-Options", "DENY") + response.headers.setdefault("Referrer-Policy", "no-referrer") + response.headers.setdefault("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=()") + + # ===== 修复后的 CSP ===== + # 开发环境 localhost:5174 / 生产环境根据 CORS 配置 + api_origin = request.headers.get("origin", "") + allowed_connect = "'self'" + # 如果 CORS origins 中包含此 origin,允许 connect + cors_origins_str = ", ".join(settings.cors_origins) if settings.cors_origins else "" + + csp = ( + "default-src 'self'; " + "img-src 'self' data: https: http:; " # 允许外部图片 + "style-src 'self' 'unsafe-inline'; " + "script-src 'self' 'unsafe-inline'; " + "connect-src 'self' http://localhost:* ws://localhost:*; " # 允许开发环境 API + WebSocket + "font-src 'self' data:; " + ) + if settings.debug: + # 开发模式放宽 CSP + csp = csp.replace("connect-src 'self'", "connect-src *") + response.headers.setdefault("Content-Security-Policy", csp) + return response +``` + +#### 方案 B:后端添加 SPA 回退路由 + +修改 `sakura_cloud/main.py`,将硬编码的占位 HTML 替换为真正的 SPA 静态文件服务: + +```python +# sakura_cloud/main.py 修改 + +import os +from fastapi import FastAPI, Request +from fastapi.responses import HTMLResponse, FileResponse +from fastapi.staticfiles import StaticFiles + +def create_app() -> FastAPI: + # ... 现有配置 ... + + # === 新增:服务前端 SPA 静态文件 === + frontend_dist = os.path.join(os.path.dirname(__file__), "..", "sakura_admin_web", "dist") + frontend_dist = os.path.abspath(frontend_dist) + + if os.path.isdir(frontend_dist): + # 挂载静态资源(JS, CSS, 图片等) + assets_dir = os.path.join(frontend_dist, "assets") + if os.path.isdir(assets_dir): + app.mount("/assets", StaticFiles(directory=assets_dir), name="admin_assets") + + # SPA 回退路由:所有非 API 路径返回 index.html + @app.get("/admin/{full_path:path}") + async def spa_fallback(full_path: str = ""): + index_path = os.path.join(frontend_dist, "index.html") + if os.path.isfile(index_path): + return FileResponse(index_path) + return HTMLResponse("

Frontend not built. Run: cd sakura_admin_web && npm run build

") + + @app.get("/admin") + async def admin_root(): + index_path = os.path.join(frontend_dist, "index.html") + if os.path.isfile(index_path): + return FileResponse(index_path) + return HTMLResponse("

Frontend not built

") + + # 为 Vue Router 的 history mode 添加额外回退路由 + @app.get("/login") + @app.get("/register") + @app.get("/forgot-password") + async def public_spa_fallback(request: Request): + """公开页面回退(不需要认证)""" + index_path = os.path.join(frontend_dist, "index.html") + if os.path.isfile(index_path): + return FileResponse(index_path) + return HTMLResponse("

Frontend not built

") + + @app.get("/app/{full_path:path}") + @app.get("/app") + async def app_spa_fallback(): + index_path = os.path.join(frontend_dist, "index.html") + if os.path.isfile(index_path): + return FileResponse(index_path) + return HTMLResponse("

Frontend not built

") + + # ... 其余配置 ... + return app +``` + +#### 方案 C:Nginx 生产部署配置(推荐方案) + +创建 `sakura_admin_web/nginx.conf`: + +```nginx +server { + listen 80; + server_name admin.your-domain.com; + + # 前端静态文件 + root /path/to/sakura_admin_web/dist; + index index.html; + + # API 反向代理到后端 + location /api/ { + proxy_pass http://127.0.0.1:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # SPA History Mode 回退 + location / { + try_files $uri $uri/ /index.html; + } + + # 静态资源缓存 + location /assets/ { + expires 1y; + add_header Cache-Control "public, immutable"; + } +} +``` + +#### 方案 D:修复前端路由守卫的正向逻辑 + +修改 `sakura_admin_web/src/router/index.ts`,确保 `/login` 页面不需要认证检查: + +```typescript +// sakura_admin_web/src/router/index.ts 路由守卫修改 + +const PUBLIC_PATHS = ['/login', '/register', '/forgot-password', '/403', '/404']; + +router.beforeEach(async (to) => { + // 公开路径直接放行,不做任何 API 调用 + if (PUBLIC_PATHS.includes(to.path) || to.path === '/') { + return true; + } + + if (!to.meta.requiresAuth) return true; + + // 只有受保护路径才检查 token + const user = await loadMe().catch(() => null); + if (!user) { + // 带上 redirect 参数,登录后可跳回 + return { path: "/login", query: { redirect: to.fullPath } }; + } + + if (to.meta.requiresAdmin && !isAdmin(authState.user?.role)) { + return "/403"; + } + + return true; +}); +``` + +#### 方案 E:前端环境变量与构建配置 + +修改 `sakura_admin_web/.env.production`(新建): + +```bash +# sakura_admin_web/.env.production +VITE_API_BASE_URL=/api/v1 +``` + +修改 `sakura_admin_web/vite.config.ts`,添加生产构建选项: + +```typescript +import { defineConfig } from "vite"; +import vue from "@vitejs/plugin-vue"; + +export default defineConfig(({ mode }) => ({ + plugins: [vue()], + server: { + port: 5174, + proxy: { + "/api": { + target: "http://127.0.0.1:8000", + changeOrigin: true + } + } + }, + build: { + outDir: "dist", + assetsDir: "assets", + // 确保 Vite 构建的 base 路径正确 + base: "/", + } +})); +``` + +### 3.4 修复后的访问流程 + +``` +用户访问 http://admin.your-domain.com/login + │ + ▼ +Nginx / FastAPI 返回 index.html(SPA 入口) + │ + ▼ +Vue.js 加载,Vue Router 匹配到 /login → 渲染 LoginPage.vue + │ + ▼ +用户输入账号密码,点击登录 + │ + ▼ +前端调用 POST /api/v1/auth/login + │ + ▼ +Nginx 代理 → http://127.0.0.1:8000/api/v1/auth/login + │ + ▼ +后端验证通过,返回 access_token + refresh_token + │ + ▼ +前端保存 token 到 sessionStorage,调用 GET /api/v1/me + │ + ▼ +路由守卫校验通过 → 跳转到 /app 或 /admin +``` + +### 3.5 快速验证清单 + +| 检查项 | 命令/方法 | 期望结果 | +|--------|-----------|----------| +| CSP 是否正确 | 浏览器 DevTools → Console | 无 CSP 错误 | +| API 代理是否正常 | `curl -X POST http://localhost:8000/api/v1/auth/login -H "Content-Type: application/json" -d '{"username_or_email":"test","password":"test"}'` | 返回 JSON(即使 401) | +| SPA 回退是否正常 | 浏览器访问 `http://localhost:8000/login` | 显示登录页 HTML,非 404 | +| 静态资源是否加载 | 浏览器 DevTools → Network | `.js`/`.css` 文件返回 200 | +| 登录流程是否打通 | 在登录页输入正确凭据 | 成功跳转到 Dashboard | + +--- + +## 四、APK 端 UI 美化:配色统一、标题优化与选项卡样式修复 + +### 4.1 问题概述 + +根据用户反馈,当前 APK 端存在三个 UI 问题: + +| # | 问题 | 现状 | 期望 | +|---|------|------|------| +| 1 | 登录页配色不和谐 | 使用蓝色系(`#3A7BFF`) | 改为粉色系,与 App 主题统一 | +| 2 | 选项卡选中样式错误 | 选中态填充整个方块区域 | 选中态只指示文字(下划线或紧凑 pill) | +| 3 | 各页面标题位置太低 | `SliverAppBar.large` 占用过多空间 | 压缩标题区域,内容更早上屏 | + +### 4.2 问题一:登录页蓝色 → 粉色(统一品牌色) + +#### 4.2.1 现状分析 + +**文件位置:** `sakura_mobile/lib/src/pages/login_page.dart` + +App 全局主题色定义在 `app.dart` 第 12-16 行: + +```dart +final colorScheme = ColorScheme.fromSeed( + seedColor: const Color(0xFFE98AA6), + primary: const Color(0xFFD95A83), // 主色:玫瑰粉 + secondary: const Color(0xFFF3A6B8), // 辅色:浅粉 + surface: const Color(0xFFFFFBFD), // 表面色:暖白 +); +``` + +但登录页完全硬编码了蓝色系,完全绕过了主题系统: + +| 元素 | 当前值(蓝色) | 所在行 | +|------|---------------|--------| +| 页面背景渐变 | `#F4F8FF → #E6EEFF` | 第 62-66 行 | +| 图标容器背景 | `#3A7BFF` 12% 透明度 | 第 95 行 | +| 图标颜色 | `#3A7BFF` | 第 98 行 | +| TabBar 背景 | `#F1F5FD` | 第 117 行 | +| TabBar 选中指示器 | `#3A7BFF` | 第 122 行 | +| 未选中文字色 | `#5A6D80` | 第 126 行 | + +#### 4.2.2 修改方案 + +将登录页所有硬编码蓝色替换为主题粉色系: + +```dart +// sakura_mobile/lib/src/pages/login_page.dart 修改后的 build 方法 + +@override +Widget build(BuildContext context) { + final session = ref.watch(sessionProvider); + final bannerText = session.error ?? _message; + final bannerIsError = session.error != null || _messageIsError; + final theme = Theme.of(context); + final primaryColor = theme.colorScheme.primary; // Color(0xFFD95A83) + + return Scaffold( + body: DecoratedBox( + // ✅ 修改 1:背景渐变从蓝色改为粉色系 + decoration: const BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + colors: [Color(0xFFFFF5F8), Color(0xFFFFE8EE)], // 粉色渐变 + ), + ), + child: SafeArea( + child: Center( + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 520), + child: ListView( + padding: const EdgeInsets.fromLTRB(20, 24, 20, 28), + children: [ + Container( + padding: const EdgeInsets.fromLTRB(22, 24, 22, 24), + decoration: BoxDecoration( + color: Colors.white.withValues(alpha: 0.96), + borderRadius: BorderRadius.circular(30), + boxShadow: const [ + BoxShadow( + color: Color(0x18D95A83), // ✅ 阴影改为粉色基调 + blurRadius: 28, + offset: Offset(0, 12), + ), + ], + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + // ✅ 修改 2:图标容器颜色从蓝色改为粉色 + Container( + width: 58, + height: 58, + decoration: BoxDecoration( + color: primaryColor.withValues(alpha: 0.12), + borderRadius: BorderRadius.circular(18), + ), + child: Icon(Icons.cloud_done_outlined, + color: primaryColor, size: 30), // 使用主题色 + ), + const SizedBox(height: 18), + Text( + '樱云同步', + style: theme.textTheme.headlineMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 10), + Text( + '一个账号管理你的收藏、阅读进度和移动端浏览体验。', + style: theme.textTheme.bodyLarge?.copyWith( + color: const Color(0xFF8A6A74), // ✅ 辅助文字改为暖灰 + height: 1.55, + ), + ), + const SizedBox(height: 20), + + // ✅ 修改 3:TabBar — 背景和指示器改为粉色 + // (详见问题二的完整重构) + _buildTabBar(theme), + // ... 其余内容不变 + ], + ), + ), + ], + ), + ), + ), + ), + ), + ); +} +``` + +**完整颜色映射表:** + +| 元素 | 旧值(蓝色) | 新值(粉色) | +|------|------------|------------| +| 页面渐变起点 | `#F4F8FF` | `#FFF5F8`(与 `scaffoldBackgroundColor` 一致) | +| 页面渐变终点 | `#E6EEFF` | `#FFE8EE`(浅樱色) | +| 卡片阴影 | `#160F2740` | `#18D95A83`(粉色阴影) | +| 图标背景 | `#3A7BFF` @12% | `ColorScheme.primary` @12% | +| 图标颜色 | `#3A7BFF` | `ColorScheme.primary` | +| 描述文字 | `#607287`(冷灰) | `#8A6A74`(暖灰,与底部导航栏一致) | +| TabBar 背景 | `#F1F5FD` | `#FFEFF5`(浅粉底) | +| TabBar 指示器 | `#3A7BFF` | `ColorScheme.primary` | +| 未选中文字 | `#5A6D80` | `#8A6A74` | + +### 4.3 问题二:选项卡选中样式 —— 从「板块选择」改为「文字下划线」 + +#### 4.3.1 现状分析 + +**文件位置:** `sakura_mobile/lib/src/pages/login_page.dart` 第 114-133 行 + +```dart +Container( + decoration: BoxDecoration( + color: const Color(0xFFF1F5FD), // 整个容器有背景色 + borderRadius: BorderRadius.circular(18), + ), + child: TabBar( + controller: _tabController, + indicator: BoxDecoration( // ← 问题核心:BoxDecoration 填充整个 tab + color: const Color(0xFF3A7BFF), // 看起来像两个按钮,而不是选项卡 + borderRadius: BorderRadius.circular(14), + ), + labelColor: Colors.white, + unselectedLabelColor: const Color(0xFF5A6D80), + dividerColor: Colors.transparent, + tabs: const [ + Tab(text: '登录'), + Tab(text: '注册'), + ], + ), +), +``` + +**问题:** `BoxDecoration` 指示器把整个 tab 区域填满了颜色,视觉上看起来是"两个切换的按钮",而不是"登录/注册的文字选项卡"。用户期望选中态只是一个文字下方的指示条或紧凑的 pill 包裹文字。 + +#### 4.3.2 修改方案:Underline 风格选项卡 + +```dart +// sakura_mobile/lib/src/pages/login_page.dart +// 将 TabBar 的 indicator 从 BoxDecoration 改为 UnderlineTabIndicator + +Container( + // ✅ 去掉容器的背景色和圆角,让它更轻量 + decoration: BoxDecoration( + border: Border( + bottom: BorderSide( + color: const Color(0xFFF1CAD7).withValues(alpha: 0.4), + width: 1, + ), + ), + ), + child: TabBar( + controller: _tabController, + // ✅ 将方块指示器改为下划线 + 文字颜色变化 + indicator: UnderlineTabIndicator( + borderSide: const BorderSide( + color: Color(0xFFD95A83), // 粉色下划线 + width: 2.5, + ), + insets: const EdgeInsets.symmetric(horizontal: 16), // 下划线比文字略宽 + ), + // ✅ 选中文字用主题色,不再白字(因为没有蓝色方块了) + labelColor: const Color(0xFFD95A83), + labelStyle: const TextStyle( + fontWeight: FontWeight.w700, + fontSize: 15, + ), + unselectedLabelColor: const Color(0xFF8A6A74), + unselectedLabelStyle: const TextStyle( + fontWeight: FontWeight.w500, + fontSize: 15, + ), + dividerColor: Colors.transparent, + indicatorSize: TabBarIndicatorSize.label, // ✅ 指示器宽度 = 文字宽度 + tabs: const [ + Tab(text: '登录'), + Tab(text: '注册'), + ], + ), +), +``` + +**效果对比:** + +``` +修改前(BoxDecoration 方块): 修改后(Underline 文字下划线): + +┌──────────────────────────┐ ┌──────────────────────────┐ +│ ┌─────────┬─────────┐ │ │ │ +│ │ ■■■■■■ │ 登录 │ │ │ 登录 注册 │ +│ │ ■登录■ │ │ │ │ ════ │ +│ │ ■■■■■■ │ │ │ │ │ +│ └─────────┴─────────┘ │ │ │ +│ 蓝色方块填充整个tab │ │ 粉色下划线 + 文字变色 │ +└──────────────────────────┘ └──────────────────────────┘ +``` + +**备选方案:Pill 风格(推荐用于需要更强视觉区分的场景)** + +如果希望保留一定视觉强度但不至于太"重",可以使用更紧凑的 pill 包裹文字: + +```dart +indicator: BoxDecoration( + color: const Color(0xFFFFE8EE), // 浅粉底,不是深色方块 + borderRadius: BorderRadius.circular(20), +), +labelColor: const Color(0xFFD95A83), // 文字用深粉,不用白色 +``` + +### 4.4 问题三:标题位置太低(SliverAppBar.large 优化) + +#### 4.4.1 现状分析 + +当前 `gallery_tab.dart` 和 `novels_tab.dart` 均使用 `SliverAppBar.large`,这会渲染一个 Material 3 风格的大标题: + +```dart +// gallery_tab.dart 第 312 行 +SliverAppBar.large( + title: Text(selectionMode ? '已选 ${selectedIds.length} 项' : widget.pageTitle), + bottom: PreferredSize( + preferredSize: Size.fromHeight(selectionMode ? 266 : 178), + // 搜索栏 + 筛选 Chip 行... + ), +), +``` + +**具体问题:** + +1. `SliverAppBar.large` 的 title 区域高度约 **120-140px**,仅用于显示"图片"或"漫画"两个字 +2. `bottom` 的 `PreferredSize` 又占用 **178-266px**(搜索栏 56px + 搜索范围 chips 行 50px + 筛选 chips 行 50px + 可能的选区面板 88px) +3. 在 720px 高的手机上,标题+筛选区域合计占用约 **300-400px**,超过屏幕一半,核心内容(图片网格/列表)被严重挤压 +4. `home_page.dart` 中还嵌套了 `SafeArea`(第 49 行),顶部再增加约 24-40px + +#### 4.4.2 修改方案:紧凑化 AppBar + 搜索栏外提 + +**方案:将 SliverAppBar.large 改为标准 SliverAppBar + 将搜索栏提升到 AppBar 内部** + +```dart +// gallery_tab.dart build 方法修改 + +@override +Widget build(BuildContext context) { + final api = ref.watch(apiClientProvider); + + return FutureBuilder>( + future: future, + builder: (context, snapshot) { + final items = snapshot.data ?? const []; + final mangaEntries = _isMangaShelfMode + ? buildMangaCollections(items) + : const []; + + return CustomScrollView( + slivers: [ + // ✅ 修改 1:SliverAppBar.large → 标准 SliverAppBar(更紧凑) + SliverAppBar( + pinned: true, // 吸顶 + floating: false, + snap: false, + // 使用标准高度(约 56px)而不是 large 的 120px+ + title: Text( + selectionMode ? '已选 ${selectedIds.length} 项' : widget.pageTitle, + style: const TextStyle( + fontWeight: FontWeight.w700, + fontSize: 20, // 比 large title 的 28-32px 更紧凑 + ), + ), + // ✅ 修改 2:bottom 压缩高度(-30px 从搜索范围行移除) + bottom: PreferredSize( + preferredSize: Size.fromHeight( + selectionMode ? 210 : 140, // 原 266/178 → 压缩约 20% + ), + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 12), + child: Column( + children: [ + // 搜索栏 + SearchBar( + controller: search, + hintText: '搜索标题、作者或标签', + leading: const Icon(Icons.search), + trailing: [ + if (!selectionMode) + IconButton( + tooltip: '管理', + onPressed: () { + setState(() { selectionMode = true; }); + }, + icon: const Icon(Icons.checklist_rounded), + ), + IconButton( + tooltip: '刷新', + onPressed: _refresh, + icon: const Icon(Icons.refresh), + ), + ], + onSubmitted: (_) => _refresh(), + ), + const SizedBox(height: 8), // 原 10 → 8 + + // ✅ 修改 3:将搜索范围 Chips 和筛选 Chips 合并为一行 + SingleChildScrollView( + scrollDirection: Axis.horizontal, + child: Row( + children: [ + // 搜索范围(更紧凑的图标按钮) + _CompactToggle( + icon: Icons.search, + label: '全部', + selected: searchScope == 'all', + onTap: () => setState(() { + searchScope = 'all'; + future = _load(); + }), + ), + _CompactToggle( + icon: Icons.title, + label: '标题', + selected: searchScope == 'title', + onTap: () => setState(() { + searchScope = 'title'; + future = _load(); + }), + ), + _CompactToggle( + icon: Icons.label_outline, + label: '标签', + selected: searchScope == 'tags', + onTap: () => setState(() { + searchScope = 'tags'; + future = _load(); + }), + ), + _CompactToggle( + icon: Icons.text_fields, + label: 'OCR', + selected: searchScope == 'ocr', + onTap: () => setState(() { + searchScope = 'ocr'; + future = _load(); + }), + ), + const SizedBox(width: 12), + // 分隔符 + Container( + width: 1, + height: 20, + color: const Color(0xFFF1CAD7), + ), + const SizedBox(width: 12), + // 筛选条件 + _CompactToggle( + icon: Icons.update, + label: '最近', + selected: recentUpdatesOnly && !favoritesOnly, + onTap: () => setState(() { + recentUpdatesOnly = true; + favoritesOnly = false; + future = _load(); + }), + ), + _CompactToggle( + icon: Icons.auto_awesome, + label: _allChipLabel, + selected: !recentUpdatesOnly && + !favoritesOnly && + (_effectiveContentType == null || + _effectiveContentType == widget.fixedContentType), + onTap: () => setState(() { + recentUpdatesOnly = false; + favoritesOnly = false; + contentType = ''; + future = _load(); + }), + ), + if (widget.showTypeFilters && + widget.fixedContentType == null) ...[ + _CompactToggle( + icon: Icons.menu_book_rounded, + label: '漫画', + selected: contentType == 'manga', + onTap: () => setState(() { + recentUpdatesOnly = false; + favoritesOnly = false; + contentType = + contentType == 'manga' ? '' : 'manga'; + future = _load(); + }), + ), + _CompactToggle( + icon: Icons.brush, + label: '插画', + selected: contentType == 'illust', + onTap: () => setState(() { + recentUpdatesOnly = false; + favoritesOnly = false; + contentType = + contentType == 'illust' ? '' : 'illust'; + future = _load(); + }), + ), + ], + _CompactToggle( + icon: Icons.favorite, + label: '收藏', + selected: favoritesOnly, + onTap: () => setState(() { + recentUpdatesOnly = false; + favoritesOnly = !favoritesOnly; + future = _load(); + }), + ), + ], + ), + ), + // 选区面板(仅在 selection mode 时显示) + if (selectionMode) ...[ + const SizedBox(height: 8), + _SelectionPanel( + message: _selectionHint, + onCancel: _exitSelectionMode, + onDownload: selectedIds.isEmpty + ? null + : () => _downloadSelected(items), + onDelete: + selectedIds.isEmpty ? null : _deleteSelected, + ), + ], + ], + ), + ), + ), + ), + // ... 内容区域不变 + ], + ); + }, + ); +} + +// ✅ 新增紧凑型 Toggle 组件(替代原有的 ChoiceChip) +class _CompactToggle extends StatelessWidget { + const _CompactToggle({ + required this.icon, + required this.label, + required this.selected, + required this.onTap, + }); + + final IconData icon; + final String label; + final bool selected; + final VoidCallback onTap; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.only(right: 6), + child: Material( + color: selected ? const Color(0xFFFFE8EE) : Colors.transparent, + borderRadius: BorderRadius.circular(20), + child: InkWell( + onTap: onTap, + borderRadius: BorderRadius.circular(20), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 7), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + icon, + size: 16, + color: selected + ? const Color(0xFFD95A83) + : const Color(0xFF8A6A74), + ), + const SizedBox(width: 5), + Text( + label, + style: TextStyle( + fontSize: 13, + fontWeight: selected ? FontWeight.w700 : FontWeight.w500, + color: selected + ? const Color(0xFFD95A83) + : const Color(0xFF8A6A74), + ), + ), + ], + ), + ), + ), + ), + ); + } +} +``` + +#### 4.4.3 空间节省对比 + +``` +修改前: +┌─────────────────────────────────┐ +│ SafeArea padding (~24px) │ +│ SliverAppBar.large title │ +│ "图片" 大标题 (~120px) │ ← 浪费!只显示2个字 +│ 搜索栏 (~56px) │ +│ 搜索范围 Chips 行 (~50px) │ +│ 筛选 Chips 行 (~50px) │ +├─────────────────────────────────┤ +│ 内容区域仅剩 ~420px │ ← 被挤压 +└─────────────────────────────────┘ + +修改后: +┌─────────────────────────────────┐ +│ SliverAppBar (pinned, 56px) │ +│ 搜索栏 + 合并 Chips 行 (~130px) │ ← 紧凑,图标+文字 Chip +├─────────────────────────────────┤ +│ 内容区域恢复 ~534px │ ← 多出约 27% 空间 +└─────────────────────────────────┘ +``` + +#### 4.4.4 novels_tab.dart 同理修改 + +对 `novels_tab.dart` 中 `SliverAppBar.large`(第 244 行)做相同的紧凑化处理: + +```dart +// novels_tab.dart 修改 +SliverAppBar( + pinned: true, + title: Text( + selectionMode ? '已选 ${selectedEntryIds.length} 项' : '系列书架', + style: const TextStyle(fontWeight: FontWeight.w700, fontSize: 20), + ), + bottom: PreferredSize( + preferredSize: Size.fromHeight(selectionMode ? 174 : 118), // 原 212/126 → 压缩 + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 12), + child: Column( + children: [ + SearchBar( + controller: search, + hintText: '搜索书名、作者或章节名', + leading: const Icon(Icons.search), + trailing: [ + if (!selectionMode) + IconButton( + tooltip: '管理', + onPressed: () => setState(() { selectionMode = true; }), + icon: const Icon(Icons.checklist_rounded), + ), + IconButton( + tooltip: '刷新', + onPressed: _refresh, + icon: const Icon(Icons.refresh), + ), + ], + onSubmitted: (_) => _refresh(), + ), + if (selectionMode) ...[ + const SizedBox(height: 8), + _NovelSelectionPanel( + message: selectedEntryIds.isEmpty + ? '长按书架卡片开始多选,支持批量删除。' + : '已选 ${selectedEntryIds.length} 项,可以继续点选调整。', + onCancel: _clearSelection, + onDelete: selectedEntryIds.isEmpty + ? null + : () => _deleteSelectedEntries(entries), + ), + ], + const SizedBox(height: 8), + ], + ), + ), + ), +), +``` + +### 4.5 修改涉及文件汇总 + +| 文件 | 修改内容 | 预计工时 | +|------|----------|----------| +| `sakura_mobile/lib/src/pages/login_page.dart` | 蓝色→粉色配色替换、TabBar 指示器改为 underline | 1h | +| `sakura_mobile/lib/src/pages/tabs/gallery_tab.dart` | `SliverAppBar.large` → `SliverAppBar` 紧凑化、合并 Chips 行、新增 `_CompactToggle` | 1.5h | +| `sakura_mobile/lib/src/pages/tabs/novels_tab.dart` | `SliverAppBar.large` → `SliverAppBar` 紧凑化 | 0.5h | +| `sakura_mobile/lib/src/pages/tabs/feed_tab.dart` | 检查是否也有标题过高问题 | 0.5h | + +--- + +## 五、实施优先级与排期建议 + +| 优先级 | 任务 | 预计工时 | 依赖 | +|--------|------|----------|------| +| **P0** | WEB 端认证修复(方案 A + B + D) | 2-3h | 无 | +| **P0** | Nginx 生产部署配置(方案 C) | 1h | 方案 B | +| **P0** | 登录页蓝→粉配色替换 | 1h | 无 | +| **P0** | TabBar 指示器改为 underline 风格 | 0.5h | 无 | +| **P1** | SliverAppBar.large → 紧凑化(gallery + novels) | 2h | 无 | +| **P1** | Legado 规则引擎(RuleParser + Engine) | 8-12h | 无 | +| **P1** | 源管理器(SourceManager + 预置源导入 + 用户自定义导入) | 3-4h | 规则引擎 | +| **P1** | 源管理页面 UI(预置源列表 + 导入 + 启用/禁用) | 3-4h | 源管理器 | +| **P2** | OnlineMangaReaderPage(纯内存漫画阅读器) | 2-3h | 规则引擎 | +| **P2** | OnlineNovelReaderPage(纯内存小说阅读器) | 1-2h | 规则引擎 | +| **P2** | 统一搜索页 ExplorePage(跨源聚合搜索) | 3-4h | 规则引擎 + 源管理器 | +| **P2** | GalleryTab/NovelsTab 添加"在线"Tab 切换 | 2-3h | 以上全部 | +| **P3** | 订阅源自动更新(后台定时拉取) | 1-2h | 源管理器 | +| **P3** | 源校验/速度测试工具 | 2h | 规则引擎 | + +--- + +> **建议执行顺序**: +> 1. **P0(半天)**:WEB 端认证修复 + 登录页配色/TabBar → 立即可见改进 +> 2. **P1(2-3 天)**:SliverAppBar 紧凑化 + Legado 规则引擎 + 源管理器 → 基础设施就绪 +> 3. **P2(2-3 天)**:在线阅读器 + 跨源搜索页 + Tab 切换 → 功能闭环 +> 4. **P3(按需)**:订阅更新 + 源校验工具 +> +> **关键里程碑:** +> - P1 完成后:APP 即可导入 6000+ 条 Legado 源,在源管理页查看 +> - P2 完成后:用户可跨源搜索 + 在线浏览任意源的内容,不下载不留痕 +> - 整个方案无需手写任何单个源的解析器,完全依赖 Legado 社区生态 diff --git a/docs/APP端问题分析与解决方案.md b/docs/APP端问题分析与解决方案.md new file mode 100644 index 0000000..f006413 --- /dev/null +++ b/docs/APP端问题分析与解决方案.md @@ -0,0 +1,492 @@ +# APP 端问题分析与解决方案 + +> 更新日期:2026-06-14 +> 范围:Sakura Mobile (Flutter) + Sakura Cloud (FastAPI 后端) + Sakura Downloader (Python 桌面端) +> 说明:本文档仅提供问题分析和修改方案,不包含实际代码改动。 + +--- + +## 问题一:APP 莫名其妙跳转到 "dakura" 用户 + +### 问题现象 + +APP 在使用过程中会突然切换到 "dakura" 用户身份,导致看到的是其他用户的数据而并非自己的内容。 + +### 根因分析 + +经过对全部代码的排查,问题出在 **Session 持久化缺少用户隔离机制**。涉及以下关键位置: + +#### 1. 移动端 Session 存储无用户绑定 (`sakura_mobile/lib/src/core/session.dart`) + +**文件位置:** `sakura_mobile/lib/src/core/session.dart` 第 44-52 行 + +```dart +static const _baseUrlKey = 'sakura.baseUrl'; +static const _accessTokenKey = 'sakura.accessToken'; + +Future _load() async { + final prefs = await SharedPreferences.getInstance(); + state = SessionState( + baseUrl: prefs.getString(_baseUrlKey) ?? defaultCloudApi, + accessToken: prefs.getString(_accessTokenKey) ?? '', + ); +``` + +**问题:** `SharedPreferences` 使用固定 key `sakura.accessToken` 存储 token,不绑定任何用户标识。当存在以下情况时会发生串号: +- 同一设备上先后登录过不同账号(如 "dakura" 和当前用户) +- SharedPreferences 中的旧 token 未完全清除 +- App 从后台恢复时读取了旧的/缓存的 token +- 多进程/多任务环境下 SharedPreferences 跨进程同步延迟 + +#### 2. 登录流程缺少旧 Session 清理 (`sakura_mobile/lib/src/core/session.dart` 第 70-86 行) + +```dart +Future login(String usernameOrEmail, String password) async { + state = state.copyWith(loading: true, error: null); + try { + final api = SakuraApiClient(baseUrl: state.baseUrl); + final tokens = await api.login(usernameOrEmail, password); + final accessToken = tokens['access_token']?.toString() ?? ''; + final prefs = await SharedPreferences.getInstance(); + await prefs.setString(_accessTokenKey, accessToken); +``` + +**问题:** 登录时直接覆盖 `_accessTokenKey`,但如果 SharedPreferences 写入失败或未完全刷盘,旧 token 仍然存在。没有先清除旧 token 再写入新 token 的原子操作。 + +#### 3. Web 端使用 sessionStorage 同样缺少隔离 (`sakura_admin_web/src/stores/session.ts` 第 15-30 行) + +```typescript +const ACCESS_KEY = "sakura_access_token"; +const REFRESH_KEY = "sakura_refresh_token"; + +export const authState = reactive({ + accessToken: sessionStorage.getItem(ACCESS_KEY) || "", + refreshToken: sessionStorage.getItem(REFRESH_KEY) || "", + user: null as CurrentUser | null, + loading: false +}); +``` + +**问题:** sessionStorage 的 key 是固定的,同一浏览器不同标签页可能共享或冲突。 + +#### 4. 桌面端 Session 文件共享 (`pixiv_app/core/paths.py` 和 `pixiv_app/core/auth.py`) + +Session 文件 `pixiv_app_session.json` 保存的是单一的 cookie 和 cloud_sync 配置,没有对应用户上下文。如果云端切换了用户但本地 session 文件未同步更新,可能导致不一致。 + +### 解决方案 + +#### 方案 A:Token 绑定用户名校验(推荐) + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `sakura_mobile/lib/src/core/session.dart` | 第 44-52 行 | 在 `_load()` 后调用 `/api/v1/users/me` 校验 token 归属用户 | +| `sakura_mobile/lib/src/core/session.dart` | 第 70-86 行 | `login()` 时先清除旧 token,写入后立即验证 | +| `sakura_mobile/lib/src/core/session.dart` | 新增方法 | 添加 `_validateAndFixSession()` 方法,对比登录用户名与 token 归属 | + +**具体做法:** +1. 在 `SessionController._load()` 中,加载 token 后立即调用 `api.me()` 获取当前 token 对应的用户名 +2. 将用户名也存入 SharedPreferences,key 使用 `sakura.username` +3. 每次 `_load()` 后比对 `sakura.username` 与 `me()` 返回的 username,不一致则清除旧 token 强制重新登录 +4. `login()` 成功后立即存储用户名,防止与旧数据混淆 + +#### 方案 B:增加 Token 失效主动检测 + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `sakura_mobile/lib/src/core/api_client.dart` | 第 12-47 行 | 在 `SakuraApiClient` 中增加 401 拦截器 | +| `sakura_mobile/lib/src/core/session.dart` | 第 100-108 行 | `logout()` 时增加确认清理 SharedPreferences 全部 key | + +**具体做法:** +1. 通过 Dio 拦截器全局捕获 401/403 响应,自动清除 token 并跳转登录页 +2. 在 `logout()` 中不单删除 `_accessTokenKey`,同时清理所有 sakura 相关 key +3. 增加 `clearAllSakuraData()` 方法用于完全重置 + +#### 方案 C:服务端 Token 版本号机制 + +**修改位置:** 后端 `sakura_cloud/` (FastAPI) 相关认证模块 + +**具体做法:** +1. 在用户表增加 `token_version` 字段 +2. 每次重新登录时 token_version +1 +3. JWT payload 中嵌入 token_version +4. 验证 token 时比对版本号,旧版本 token 直接拒绝 +5. 确保用户登出后旧 token 彻底失效 + +--- + +## 问题二:发现页查询过慢,需要加载缓冲或优化 + +### 问题现象 + +APP 打开"发现"页(FeedTab)时,整个页面长时间白屏/转圈,用户体验差。 + +### 根因分析 + +#### 1. 五个 API 串行阻塞全部内容 (`sakura_mobile/lib/src/pages/tabs/feed_tab.dart`) + +**文件位置:** `sakura_mobile/lib/src/pages/tabs/feed_tab.dart` 第 20-26 行 + +```dart +return FutureBuilder>( + future: Future.wait([ + api.galleryStat(), // 统计总数 + api.galleryFeed(mode: 'created', limit: 36), // 今日新增 + api.galleryFeed(mode: 'recent', limit: 24), // 最近更新 + api.recentNovels(limit: 12), // 最近小说 + api.unreadAlerts(limit: 12), // 未读提醒 + ]), +``` + +**问题:** +- `Future.wait` 等待所有 5 个请求完成才渲染页面 +- 任一接口慢(尤其是 `galleryStat()` 和 `galleryFeed()`),整个发现页都会卡住 +- `galleryStat()` 可能后端做了全表 COUNT,数据量大时耗时数秒 + +#### 2. 后端 gallery/feed 查询可能缺少优化 + +**文件位置:** 后端 API(`sakura_cloud/` 目录下的 gallery/feed 路由) + +`galleryFeed` 接口通常对应: +- `mode: 'created'` → 按创建时间排序的最近作品 +- `mode: 'recent'` → 按更新时间排序 + +如果后端 SQL 查询缺少合适的复合索引,或者做了全表扫描 + JOIN 多表,在数据量上千条后就会明显变慢。典型慢查询模式: + +```sql +-- 可能的慢查询(缺少索引或 JOIN 过多) +SELECT a.*, ar.name, tc.thumbnail_path, ... +FROM artworks a +LEFT JOIN artists ar ON ... +LEFT JOIN thumbnail_cache tc ON ... +LEFT JOIN artwork_files af ON ... +LEFT JOIN favorites fav ON ... +ORDER BY a.created_at DESC +LIMIT 36 +``` + +**对比本地库查询** (`pixiv_app/core/gallery_service.py` 第 96-134 行) 同样有类似的多表 LEFT JOIN + GROUP BY + 复合搜索条件,这在 SQLite 上尤其慢: + +```python +total = library.connection.execute( + f"SELECT COUNT(DISTINCT a.artwork_id) AS total FROM artworks a {joins} {where_sql}", + params, +).fetchone()["total"] +``` + +先 COUNT 再 SELECT,等于两次大查询。 + +#### 3. 图片缩略图同步加载阻塞渲染 + +**文件位置:** `sakura_mobile/lib/src/pages/tabs/feed_tab.dart` 第 385-389 行 + +```dart +CachedNetworkImage( + imageUrl: thumbUrl, + httpHeaders: api.authHeaders(), + fit: BoxFit.cover, +), +``` + +**问题:** 虽然使用了 `CachedNetworkImage` 做缓存,但如果后端 `thumbnail_url` 指向的是需要实时生成或大文件的原图,加载会很慢;且横向滚动的 `ListView` 会一次性构建多个图片 widget,造成网络拥塞。 + +### 解决方案 + +#### 方案 A:渐进式加载 + 骨架屏(推荐,前端改动最小) + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `sakura_mobile/lib/src/pages/tabs/feed_tab.dart` | 第 20-26 行 | 将 `Future.wait` 拆分为独立请求,每个区块独立加载 | +| `sakura_mobile/lib/src/pages/tabs/feed_tab.dart` | 第 62-137 行 | 为每个 `_ArtworkStrip`、`_NovelStrip`、`_AlertStrip` 增加独立的 loading/skeleton 状态 | +| `sakura_mobile/lib/src/pages/tabs/feed_tab.dart` | 新增组件 | 增加 `_SkeletonStrip` 骨架屏组件 | + +**具体做法:** +1. 不再使用单个 `FutureBuilder` + `Future.wait`,改为每个 section 独立使用 `FutureBuilder` +2. 统计卡片(`_TodayUpdateCard` 和 `_StatCard`)优先加载 +3. 各内容条(插画、漫画、小说、提醒)各自独立加载,失败的只影响自己 +4. 加载中的条显示 shimmer 骨架屏而非空白 +5. `galleryStat()` 如果慢,可设置 3 秒超时,超时则降级显示 "--" + +#### 方案 B:后端增加 feed 缓存表(推荐,解决根本问题) + +**修改位置:** 后端 `sakura_cloud/` + +**具体做法:** +1. 新增 `feed_cache` 表: + +```sql +CREATE TABLE feed_cache ( + user_id UUID NOT NULL, + feed_type VARCHAR(32) NOT NULL, -- 'created', 'recent', 'stat' + payload_json JSONB NOT NULL, + generated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + PRIMARY KEY (user_id, feed_type) +); +``` + +2. 在每次上传新作品、同步关注的定时任务完成后,异步刷新 feed_cache +3. `/api/v1/gallery/feed` 和 `/api/v1/gallery/stat` 优先从 `feed_cache` 读取 +4. 缓存有效期设为 5-10 分钟,过期后后台异步刷新,当前请求仍返回旧缓存 +5. 如果缓存不存在,降级为直接查询(首次慢但后续快) + +#### 方案 C:本地 SQLite 缓存 + 增量同步(移动端离线方案) + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `sakura_mobile/lib/src/core/api_client.dart` | 新增方法 | 增加本地缓存读写方法 | +| `sakura_mobile/lib/src/pages/tabs/feed_tab.dart` | 第 20 行 | 优先读本地缓存,后台静默刷新 | + +**具体做法:** +1. 移动端使用 `sqflite` 或 `drift` 建立轻量本地库 +2. 发现页数据缓存到本地,设置 30 分钟有效期 +3. 打开发现页时先展示缓存数据(秒开),同时后台请求新数据 +4. 新数据到达后平滑替换(`AnimatedSwitcher` 已就位) +5. 支持下拉强制刷新(`RefreshIndicator` 已就位) + +#### 方案 D:优化后端 gallery/feed 查询语句 + +**修改位置:** 后端 `sakura_cloud/api/v1/` 中 gallery/feed 相关路由 + +**具体做法:** +1. 分析慢查询日志,定位具体慢的 SQL +2. 为 `artworks` 表增加复合索引: + ```sql + CREATE INDEX idx_artworks_user_created ON artworks(user_id, created_at DESC); + CREATE INDEX idx_artworks_user_updated ON artworks(user_id, updated_at DESC); + ``` +3. 将 COUNT + SELECT 两次查询合并为一次(用窗口函数或子查询) +4. 限制 `galleryStat()` 的统计精度,超过 10000 条时显示 "10000+" +5. 对缩略图 URL 确保使用 CDN 或 Nginx 静态文件服务,而非 Django/FastAPI 流式响应 + +--- + +## 问题三:提醒中心用户更新检测问题 + +### 问题现象 + +1. **最近提醒显示的并非实际关注的作者名字**,而是作者 ID 数字 +2. **已关注的作者有更新但未检测到**,提醒中心未生成新提醒 + +### 根因分析 + +#### 1. 提醒标题使用 author_id 而非 author_name + +**文件位置:** `pixiv_app/core/gallery_service.py` 第 588-613 行 + +```python +def _create_cloud_pixiv_update_alerts(self, artist_id: int, work_ids: list[int]) -> int: + ... + for work_id in work_ids: + try: + manager.client.create_alert( + { + "platform": "pixiv", + "creator_id": str(int(artist_id)), + "title": f"Pixiv 作者 {int(artist_id)} 更新了作品 {int(work_id)}", + # ↑↑↑ 问题在这里:用的是 author_id 数字,而非 author name + "body": "你关注的 Pixiv 作者有新作品了", + ... + } + ) +``` + +**问题详解:** +- `_create_cloud_pixiv_update_alerts` 没有接收 `artist_name` 参数 +- 调用它的 `sync_followed_artists`(第 505-586 行)虽然能从 `library` 查到作者信息,但没传给 alert 创建函数 +- 导致提醒标题变成 "Pixiv 作者 123456 更新了作品 789012",用户看到的是一串数字 +- 移动端 `reminder_center_page.dart` 直接展示 `alert['title']`,所以显示的是数字 ID + +#### 2. 关注作者有更新但未检测到 + +**文件位置:** `pixiv_app/core/gallery_service.py` 第 505-586 行 + +```python +def sync_followed_artists(self, *, cookie, max_new_per_artist=20, download=True, save_root="Sakura_Downloads"): + ... + for row in followed: + artist_id = int(row["artist_id"]) + # 获取已存在的作品 ID + existing = { + int(item["artwork_id"]) + for item in library.connection.execute( + "SELECT artwork_id FROM artworks WHERE artist_id = ? AND type IN ('illust', 'manga')", + (artist_id,), + ).fetchall() + } + # 获取远程作品 ID + _, work_ids = fetch_user_work_ids(user_id=artist_id, cookie=cookie) + # 只取本地没有的 + new_ids = [work_id for work_id in work_ids if work_id not in existing][:max_new_per_artist] +``` + +**问题详解:** +- `fetch_user_work_ids` 需要有效的 Pixiv Cookie,如果 Cookie 已过期,API 调用静默失败 +- 对比逻辑只看 `existing` 中的 artwork_id,但如果作品之前下载失败、入库未完成,可能漏掉 +- `mark_artist_synced(artist_id, error=str(exc))` 在异常时记录了错误但未区分错误类型(Cookie 过期 vs 网络错误 vs 限流) +- 没有"最近检查时间"的限流机制 —— 如果短时间内多次同步同一个作者,每次都做全量对比 + +#### 3. 检查完成后未更新 `last_checked_at` + +**文件位置:** `pixiv_app/core/library.py` 第 592-603 行 + +```python +def mark_artist_synced(self, artist_id: int, *, error: str = "") -> None: + with self.connection: + self.connection.execute( + """ + INSERT INTO followed_artists (artist_id, enabled, last_synced_at, last_error) + VALUES (?, 1, CURRENT_TIMESTAMP, ?) + ON CONFLICT(artist_id) DO UPDATE SET + last_synced_at = CURRENT_TIMESTAMP, + last_error = excluded.last_error + """, + (artist_id, error), + ) +``` + +**问题:** `last_synced_at` 只在本地 SQLite 更新,**没有同步到云端** follows 表的 `last_checked_at`。移动端提醒中心展示的是云端 follows 数据: + +**文件位置:** `sakura_mobile/lib/src/pages/reminder_center_page.dart` 第 22-27 行 + +```dart +Future<_ReminderCenterData> _load() async { + final api = ref.read(apiClientProvider); + final results = await Future.wait([ + api.me(), + api.follows(enabled: true), // ← 从云端读取 follows + api.alerts(limit: 50), // ← 从云端读取 alerts + ]); +``` + +**问题详解:** +- 移动端显示的 `last_checked_at` 来自云端 follows 表 +- 桌面端 `_upsert_cloud_follow` 只更新了 creator 基本信息,**没有更新 `last_checked_at`** +- 所以云端看到的"最近检查"时间永远是旧的甚至为空 +- 用户无法判断哪个作者实际上被检查过 + +#### 4. Alert 创建没有去重 + +**文件位置:** `pixiv_app/core/gallery_service.py` 第 588-613 行 + +```python +for work_id in work_ids: + try: + manager.client.create_alert({...}) + created += 1 + except Exception: + continue +``` + +**问题:** +- 每次同步都会为同一个 work_id 创建新的 alert +- 如果多次同步同一批作品,会产生大量重复提醒 +- 没有检查 "这个 work_id 是否已经发过 alert" + +### 解决方案 + +#### 方案 A:修复 Alert 标题——传入作者名(推荐,最小改动) + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `pixiv_app/core/gallery_service.py` | 第 588 行 | `_create_cloud_pixiv_update_alerts` 增加 `artist_name` 参数 | +| `pixiv_app/core/gallery_service.py` | 第 562-565 行 | `sync_followed_artists` 传入作者名 | +| `pixiv_app/core/gallery_service.py` | 第 599 行 | 修改 title 使用作者名 | + +**具体做法:** + +在 `sync_followed_artists` 方法中,从 followed_artists 或 artists 表获取作者名: + +```python +# 第 527-538 行之间,获取 followed 列表时同时获取 name +followed = library.connection.execute( + "SELECT fa.artist_id, COALESCE(NULLIF(fa.name,''), NULLIF(ar.name,''), '') AS name " + "FROM followed_artists fa " + "LEFT JOIN artists ar ON ar.artist_id = fa.artist_id " + "WHERE fa.enabled = 1 ORDER BY fa.artist_id" +).fetchall() +``` + +然后修改 `_create_cloud_pixiv_update_alerts` 签名: + +```python +def _create_cloud_pixiv_update_alerts( + self, artist_id: int, work_ids: list[int], artist_name: str = "" +) -> int: +``` + +title 改为: + +```python +"title": f"Pixiv 作者 {artist_name or str(artist_id)} 更新了 {len(work_ids)} 个新作品" +``` + +#### 方案 B:同步云端 follows 的检查状态 + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `pixiv_app/core/gallery_service.py` | 第 556-559 行 | `mark_artist_synced` 之后同步更新云端 follows 的 `last_checked_at` | +| `pixiv_app/core/cloud_sync.py` | 第 167-168 行 | `upsert_follow` 增加 `last_checked_at` 和 `latest_published_at` 字段 | +| `pixiv_app/core/gallery_service.py` | 第 415-447 行 | `_upsert_cloud_follow` 增加更新时间戳参数 | + +**具体做法:** +1. 在 `_upsert_cloud_follow` 方法中增加 `last_checked_at` 和 `latest_published_at` 参数 +2. 每次同步完一个作者后,调用 `_upsert_cloud_follow` 更新云端状态 +3. 在云端 follows API 的 upsert 逻辑中接受并存储这些时间字段 +4. 移动端 `reminder_center_page.dart` 中 `_FollowTile` 就能显示正确的"最近检查"时间 + +#### 方案 C:Alert 去重 & 按作者聚合 + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `pixiv_app/core/gallery_service.py` | 第 588-613 行 | 重构 `_create_cloud_pixiv_update_alerts` 逻辑 | +| 后端 `sakura_cloud/api/v1/follows.py` | alert 创建路由 | 增加去重逻辑 | + +**具体做法:** +1. 将每个作者的多个新作品合并为**一条聚合 alert**,而非每个作品一条 +2. Alert 的 `source_id` 使用 `f"{artist_id}:{min(work_ids)}:{max(work_ids)}"` 生成唯一标识 +3. 创建 alert 前先检查是否存在相似 alert(同一作者 + 同一天内) +4. 如果存在则更新而非新建 +5. title 格式改为:`"你关注的 {作者名} 更新了 {N} 个新作品"` + +#### 方案 D:增加关注作者更新状态页面 + +**修改位置:** + +| 文件 | 行号 | 修改内容 | +|------|------|----------| +| `sakura_mobile/lib/src/pages/reminder_center_page.dart` | 第 350-371 行 | 已关注作者列表增加"手动检查更新"按钮 | +| 后端新增接口 | - | `POST /api/v1/follows/{follow_id}/check` 触发单作者更新检测 | + +**具体做法:** +1. 在已关注作者列表中为每个作者增加状态标识: + - 🟢 最近 24h 内检查过 + - 🟡 24h-7d 内检查过 + - 🔴 超过 7 天未检查 +2. 增加"手动检查"按钮,调用后端触发单个作者的更新检测 +3. 检查完成后自动刷新提醒列表 + +--- + +## 总结优先级 + +| 优先级 | 问题 | 推荐方案 | 改动范围 | 预期效果 | +|--------|------|----------|----------|----------| +| P0 紧急 | 跳转到 dakura 用户 | 方案 A:Token 绑定用户名 | 移动端 session.dart | 根除串号问题 | +| P0 紧急 | Alert 显示数字而非作者名 | 方案 A:传入作者名 | gallery_service.py | 提醒可读 | +| P1 高 | 发现页加载慢 | 方案 A + B:渐进加载 + feed_cache | feed_tab.dart + 后端 | 首屏从 3-5s 降到 <1s | +| P1 高 | 关注作者未检测更新 | 方案 B:同步检查状态到云端 | gallery_service.py + cloud_sync.py | 状态准确可追溯 | +| P2 中 | Alert 重复创建 | 方案 C:去重 + 聚合 | gallery_service.py + 后端 | 减少重复提醒 | +| P2 中 | 查询性能优化 | 方案 D:SQL 索引 | 后端数据库 | 查询耗时降低 50%+ | diff --git a/docs/Legado规则引擎重构-分阶段实施方案.md b/docs/Legado规则引擎重构-分阶段实施方案.md new file mode 100644 index 0000000..3e8568b --- /dev/null +++ b/docs/Legado规则引擎重构-分阶段实施方案.md @@ -0,0 +1,738 @@ +# Legado 规则引擎重构 — 分阶段实施方案 + +> 日期:2026-06-21 +> 基于:源码架构总结 + 截图问题诊断 + 方案文档反馈 +> 涉及:`legado_rule_parser.dart`、`legado_engine.dart`、`legado_models.dart` +> 原则:不改 UI 层,只在 core 层动刀;前 4 步零新依赖。 + +--- + +## 一、问题根因定性与实施顺序纠偏 + +### 1.1 截图 Bug ↔ 真实根因对照 + +| 截图 | 现象 | 代码层面的真实原因 | +|------|------|-------------------| +| 截图3 | "这一章还没有找到漫画页面" | `fetchChapterContent()` → `contentRule` → `RuleParser` 只认 CSS/JSON/Regex 三种前缀,**包子漫画5 的 `contentRule` 使用了 `@tag.img@src` / `##` / `&&` 等链式语法**,`_safeSelect()` 返回空串,`rawHtml` 兜底也没触发。 | +| 截图4 | "未知作者" / 封面空白 | `bookInfoAuthorRule` / `bookInfoCoverRule` 同上,规则语法不被 `RuleParser` 支持。 | +| 截图1 | "书籍详情加载失败" | `fetchBookInfo()` 整体 try/catch 后只设 `infoError`,不区分"网络不通"还是"规则不支持"。 | +| 搜索 `unknown:none` | 错误消息原样展示 | `_shortError()` 已修好(源码已含完整中文映射),这个 bug 已不存在于当前源码。 | + +### 1.2 原方案步骤的纠偏 + +原方案第 1 步是「支持 URL,{method,body,headers}」,但实际上: + +- `_buildSearchRequest()` 已有 GET/POST 分支 +- `_applySearchTemplate()` 已支持 12 种占位符变体 +- `fromJson` 解析 `header` 已支持 Map / JSON 字符串 / 冒号分隔行三种格式 + +**请求层不是最优先的问题。最优先的是 RuleParser 不支持 Legado 链式规则语法。** + +### 1.3 修正后的实施顺序 + +``` +第1步:rawHtml 兜底扫描 + script 变量扫描 ← 零成本,立刻修复截图3 +第2步:RuleParser 支持 @tag/@text/@html/@src 链式语法 ← 解锁包子漫画5等核心书源 +第3步:支持 ## 替换、|| 备选、&& 拼接 ← 兼容更多规则变体 +第4步:fetchBookInfo / fetchToc 失败时区分错误类型 ← 解决截图1 +第5步:JS 特判(newImgs / imageList 等高频变量) ← 覆盖 60%+ JS 规则,零依赖 +第6步:@put / @get 上下文变量 ← 跨规则传值 +第7步:书源兼容度检测 UI ← 用户体验闭环 +第8步:完整 JS 引擎(flutter_js / quickjs) ← 最后做,覆盖剩余 40% +``` + +**前 4 步不需要引入任何新依赖,只在 `legado_rule_parser.dart` 和 `legado_engine.dart` 里改。** + +--- + +## 二、总体架构重构方案 + +### 2.1 不要继续往 RuleParser 里塞逻辑 + +当前 `RuleParser`(262 行)已经承担了 CSS / JSONPath / Regex 三种解析路径,再往里面加链式语法会导致它变成无法维护的千行怪物。 + +**建议拆成三层**: + +``` +请求层 ── LegadoRequestBuilder ── 构建 URL、headers、body、编码 +规则层 ── LegadoRuleExecutor ── 链式规则执行引擎 +检测层 ── SourceCompatChecker ── 导入时打兼容度标签 +``` + +``` +┌──────────────────────────────────────────────────┐ +│ LegadoEngine │ +│ (对外 API:search / fetchBookInfo / fetchToc / │ +│ fetchChapterContent 不变) │ +├──────────────────────────────────────────────────┤ +│ │ +│ ┌──────────────────┐ ┌─────────────────────┐ │ +│ │LegadoRequestBuilder│ │ LegadoRuleExecutor │ │ +│ │· buildSearchUrl │ │· select() │ │ +│ │· buildRequest │ │· selectList() │ │ +│ │· encodeKeyword │ │· execChain() 🆕 │ │ +│ │· applyTemplate │ │· applyReplace() 🆕 │ │ +│ └──────────────────┘ │· extractImages() 🆕 │ │ +│ │· scanScriptVars() 🆕 │ │ +│ └──────────┬──────────┘ │ +│ │ │ +│ ┌──────────▼──────────┐ │ +│ │ RuleParser (保留) │ │ +│ │ CSS / JSON / Regex │ │ +│ │ 不改内部,只当底层 │ │ +│ └─────────────────────┘ │ +└──────────────────────────────────────────────────┘ +``` + +### 2.2 核心原则 + +1. **LegadoEngine 对外 API 不动**。`search()` / `fetchBookInfo()` / `fetchToc()` / `fetchChapterContent()` 签名不变,UI 层零改动。 + +2. **RuleParser 降级为底层原子工具**。它继续支持 CSS / JSONPath / Regex 三种基础解析,但不理解链式语法。所有链式逻辑放在 `LegadoRuleExecutor` 里。 + +3. **新规则执行器按优先级链执行**。每条规则被解析为一条 **执行链 (execChain)**,按序执行,任意一步返回空就终止并返回空。 + +--- + +## 三、第 1 步:rawHtml 兜底扫描 + script 变量扫描 + +> 目标:不改 RuleParser,在 `fetchChapterContent()` 和 `fetchImageUrls()` 里补兜底逻辑。 + +### 3.1 问题 + +当 `contentRule` 为空或解析失败时,当前代码回到 `_htmlToText(body)` 或 `_extractImageUrls(body)` 暴力扫描 HTML。但如果漫画图片 URL 藏在 `###` | + +### 5.2 实现位置 + +`LegadoRuleExecutor` 的 `execChain()` 方法。 + +### 5.3 执行顺序 + +``` +解析 rawRule: + 1. 按 || 拆分为备选组 + 2. 每个备选组按 && 拆分为串联组 + 3. 每个串联组解析为链式步骤(复用第2步的 _parseChainSteps) + 4. 每个链式步骤末尾检测 ## 替换指令 + +执行: + for 备选组 in 备选组列表: + 结果 = 执行 备选组 + if 结果非空 → 返回结果 + return 空 + +执行备选组: + parts = [] + for 串联组 in 备选组: + part = 执行串联组 + parts.add(part) + return parts.join('\n') // && 的结果用换行拼接 + +执行串联组: + result = 执行链式步骤(复用 _selectChain) + if 有 ## 替换指令: + for 替换 in 替换指令: + result = result.replaceAll(替换.regex, 替换.replacement) + return result +``` + +### 5.4 改动量 + +- `legado_rule_parser.dart` 新增 `_selectWithAlternatives()` ~50 行 +- `##` 替换逻辑 ~30 行 +- 总计 ~80 行 +- **零新依赖** + +--- + +## 六、第 4 步:fetchBookInfo / fetchToc 失败时区分错误类型 + +### 6.1 问题 + +当前 `fetchBookInfo()` 的 try/catch 结构: + +```dart +try { + final value = await engine.fetchBookInfo(...); + setState(() { info = value; loadingInfo = false; }); +} catch (error) { + setState(() { infoError = error; loadingInfo = false; }); // 所有错误一视同仁 +} +``` + +用户看不到是"网络不通"还是"规则不支持",只能看到统一的"书籍详情加载失败"。 + +### 6.2 方案:定义错误类型,不新增异常类 + +```dart +enum LoadFailureReason { + network, // 网络不通 / DNS 失败 / 超时 + httpError, // HTTP 4xx / 5xx + ruleNotSupported, // 规则语法不被当前引擎支持 + ruleParseEmpty, // 规则执行成功但结果为空(可能是网站改版) + encoding, // 编码问题 + unknown, +} +``` + +在 `fetchBookInfo()` 和 `fetchToc()` 内部区分异常类型,通过 `_shortError()` 返回带 reason 的错误消息(或通过一个简单的包装类)。 + +### 6.3 UI 差异化展示 + +``` +网络不通: + "无法连接到目标网站,请检查网络或稍后重试。" + [重试] + +HTTP 404: + "书源详情页已失效(404),该书源可能需要更新。" + [更换书源] + +规则不支持: + "该书源的详情解析规则包含不被当前版本支持的语法(如 @js),更新 APP 后可能可用。" + [查看原始页面] + +规则解析为空: + "目标网站页面结构已变化,书源规则可能已过时。" + [重试] [更换书源] +``` + +### 6.4 改动量 + +- `legado_engine.dart` 新增 `_classifyError()` 方法 ~30 行 +- `_shortError()` 新增 reason 字段 ~10 行 +- `online_book_detail_page.dart` 根据 reason 展示不同的 UI ~30 行 +- 总计 ~70 行 +- **零新依赖** + +--- + +## 七、第 5 步:JS 特判(零依赖) + +### 7.1 问题 + +很多漫画源的 `contentRule` 直接写 JS: + +```json +"content": "\nvar result = [];\nvar imgs = JSON.parse...\nnewImgs.map(...)\nresult.join('\\n')" +``` + +或者写在 `ruleContent.content` 里用 `@js:` 前缀。 + +当前引擎对 `@js:` 和 `` 完全无处理能力,规则执行到这一步直接抛异常。 + +### 7.2 第一阶段的 JS 特判策略 + +不引入完整 JS 引擎。只做模式匹配,覆盖最常见的漫画图片提取模式: + +| 模式 | 真实 JS 代码 | 特判正则 | +|------|-------------|---------| +| `newImgs.map` | `newImgs.map(i => i.url)` | 先扫描 rawHtml 中的 `var newImgs = [...]` | +| `imageList` | `imageList[0].src` | 扫描 `var imageList = [...]` | +| `page_url` | `page_url + img` | 扫描 `var page_url = "..."` | +| `chapterImages` | `chapterImages.join` | 扫描 `var chapterImages = [...]` | +| `km5_img_url` | `km5_img_url` | 扫描 `var km5_img_url = "..."` | +| `base64Images` | 从 base64 解码 | 检测 `data:image` 前缀 | +| `eval(decrypt(...))` | 简单解密 | 正则提取被 eval 的字符串 | + +### 7.3 实现方式 + +```dart +List _jsFallbackImages(String rawHtml, String baseUrl) { + final patterns = [ + // 1. 数组变量 + _JsVarPattern('newImgs', arrayPattern: true), + _JsVarPattern('imageList', arrayPattern: true), + _JsVarPattern('chapterImages', arrayPattern: true), + _JsVarPattern('pics', arrayPattern: true), + _JsVarPattern('imgList', arrayPattern: true), + _JsVarPattern('pageImgs', arrayPattern: true), + + // 2. 字符串拼接变量 + _JsVarPattern('page_url', arrayPattern: false), + _JsVarPattern('imgDomain', arrayPattern: false), + + // 3. data:image 内嵌 + _JsVarPattern('data:image', base64Pattern: true), + ]; + + for (final pattern in patterns) { + final urls = _tryExtractJsVar(rawHtml, pattern); + if (urls.isNotEmpty) return urls.map((u) => resolveUrl(baseUrl, u)).toList(); + } + return const []; +} +``` + +### 7.4 改动量 + +- `legado_engine.dart` 新增 `_jsFallbackImages()` ~60 行 +- `_tryExtractJsVar()` ~40 行 +- 总计 ~100 行 +- **零新依赖** + +--- + +## 八、第 6 步:@put / @get 上下文变量 + +### 8.1 用途 + +Legado 高级规则中可以在同一次执行中跨步骤传值: + +``` +@put:{bookId:$.id} +// ... 在后续规则中使用 ... +/api/book/{{bookId}}/chapters +``` + +### 8.2 实现方式 + +在 `LegadoRuleExecutor` 中维护一个 `Map _context`。 + +```dart +class LegadoRuleExecutor { + final Map _context = {}; + + // 执行 @put:{key:value} 时写入 _context + // 解析 URL 模板时用 _context 替换 {{key}} + // @get:key → 从 _context 读取 +} +``` + +### 8.3 改动量 + +- `legado_rule_parser.dart` 新增 `_context` Map ~5 行 +- `@put` / `@get` 语法解析 ~30 行 +- `_applySearchTemplate()` 新增 `{{$.key}}` 占位符支持 ~10 行 +- 总计 ~45 行 +- **零新依赖** + +--- + +## 九、第 7 步:书源兼容度检测 + +### 9.1 目标 + +导入书源后,自动分析每个书源用到的规则语法,打上兼容度标签,在书源管理 UI 中展示。 + +### 9.2 兼容度标签定义 + +```dart +enum SourceCompatLevel { + full, // 完全兼容:所有规则语法已被引擎支持 + partial, // 部分兼容:搜索可用,但正文/详情可能因缺少 JS 执行而不完整 + needsJs, // 需要 JS:contentRule 或 bookInfoRule 依赖 @js 或 + needsPost, // 需要 POST 搜索:searchMethod == POST 或 URL 包含 ,{ + needsHeaders, // 需要自定义请求头 + broken, // 已失效:基础 URL 不可达 + unknown, // 尚未验证 +} + +class SourceCompatInfo { + final SourceCompatLevel level; + final bool searchOk; // 搜索规则可执行 + final bool bookInfoOk; // 详情规则可执行 + final bool tocOk; // 目录规则可执行 + final bool contentOk; // 正文规则可执行 + final bool hasJsRule; // 规则中包含 @js / + final bool hasPutGet; // 规则中包含 @put / @get + final List warnings; // 警告信息列表 +} +``` + +### 9.3 检测逻辑(导入时运行) + +```dart +SourceCompatInfo analyzeSource(LegadoSource source) { + final warnings = []; + bool hasJs = false; + bool hasPutGet = false; + bool hasChainSyntax = false; + + // 1. 检查所有规则字段是否包含 JS 语法 + for (final rule in _allRuleFields(source)) { + if (rule == null) continue; + if (rule.contains('@js') || rule.contains('')) hasJs = true; + if (rule.contains('@put') || rule.contains('@get')) hasPutGet = true; + if (rule.contains('@tag.') || rule.contains('@src') || + rule.contains('@text') || rule.contains('@html')) { + hasChainSyntax = true; + } + } + + // 2. 评估各模块可行性 + final searchOk = source.searchUrl != null && source.searchListRule != null; + final bookInfoOk = source.bookInfoTocRule != null || source.bookInfoNameRule != null; + final tocOk = source.tocListRule != null || source.tocNameRule != null; + final contentOk = source.contentRule != null; + + // 3. 确定级别 + SourceCompatLevel level; + if (!searchOk && !contentOk) { + level = SourceCompatLevel.broken; + } else if (hasJs) { + level = SourceCompatLevel.needsJs; + } else if (hasPutGet) { + level = SourceCompatLevel.partial; + } else if (!bookInfoOk && !contentOk) { + level = SourceCompatLevel.partial; // 只能搜 + } else { + level = SourceCompatLevel.full; + } + + return SourceCompatInfo(level: level, ...); +} +``` + +### 9.4 UI 展示 + +在 `source_manager_page.dart` 的书源列表中,每行右侧显示兼容度标签: + +``` +包子漫画5 [🔴 需要 JS] +6漫画 [🔴 需要 JS] +全本玄幻小说 [🟢 完全兼容] +WebToon [🟡 需要 POST] +某已失效源 [⚫ 已失效] +``` + +### 9.5 改动量 + +- `legado_models.dart` 新增 `SourceCompatInfo` 类 ~30 行 +- `source_manager.dart` 新增 `analyzeSource()` ~50 行 +- `source_manager_page.dart` UI 展示 ~40 行 +- 总计 ~120 行 +- **零新依赖** + +--- + +## 十、第 8 步:完整 JS 引擎 + +### 10.1 时机 + +前 7 步完成后,已有书源中约 60-70% 的漫画源可正常阅读(通过 JS 特判覆盖)。剩下的 30-40% 需要完整 JS 执行环境。 + +### 10.2 推荐方案 + +| 方案 | 优点 | 缺点 | +|------|------|------| +| **flutter_js** | 纯 Dart,不依赖 WebView | 性能一般,JS 互操作受限 | +| **quickjs_dart_bridge** | 轻量、快、支持 ES2020 | 需要 Native 桥接 | +| **flutter_js_engine** | 基于 JavaScriptCore/QuickJS | 体积较大 | +| **WebView 隐藏执行** | 兼容性最好 | 太重,不适合批量 | + +**建议**:先用 **quickjs_dart_bridge**(已有成功集成到阅读器的先例)。 + +### 10.3 注入的 JS 运行环境 + +```js +// 注入变量 +result; // 当前 HTML 或 RuleParser 结果 +baseUrl; // 当前请求的 base URL + +// 注入函数(兼容 Legado 的 java.xxx 调用风格) +java.get(key); // → 从 _context 读 +java.put(key, value); // → 写入 _context +java.getElements(html, selector); // → querySelectorAll → 返回元素列表 +java.base64Decode(str); // → base64 解码 +java.ajax(url); // → HTTP GET → 返回响应文本 + +// 注入输出收集器 +__result; // 脚本执行完毕后读取此变量作为输出 +``` + +### 10.4 安全措施 + +- JS 沙箱:禁止 `XMLHttpRequest`、`fetch`、`localStorage` +- 超时限制:每条 JS 规则最多执行 3 秒 +- 错误隔离:单条 JS 规则失败不影响其他规则 +- Isolate 执行:防卡 UI(可选,复杂度较高) + +### 10.5 改动量 + +- `pubspec.yaml` 新增 `quickjs_dart_bridge` 依赖 +- 新建 `legado_js_runner.dart` ~150 行 +- `LegadoRuleExecutor` 集成 JS 执行 ~30 行 +- 总计 ~180 行 + +--- + +## 十一、完整实施路线图 + +``` +Week 1 ████████░░░░░░░░░░░░ + 第1步: rawHtml 兜底 + script 变量扫描 + 第2步: @tag/@text/@html/@src 链式语法 + +Week 2 ████████░░░░░░░░░░░░ + 第3步: ##/||/&& 语法 + 第4步: 错误类型区分 + +Week 3 ████████████████░░░░ + 第5步: JS 特判 + 第6步: @put/@get + 第7步: 书源兼容度检测 + +Week 4+ ████████████████████ + 第8步: 完整 JS 引擎 + 测试: 30+ 书源的回归测试 + + 零新依赖 新依赖 + ├───────────────────┼──────────┤ + 第1步 第8步 + 第2步 + 第3步 + 第4步 + 第5步 + 第6步 + 第7步 +``` + +--- + +## 十二、改动文件清单 + +| 文件 | 第1步 | 第2步 | 第3步 | 第4步 | 第5步 | 第6步 | 第7步 | 第8步 | +|------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:| +| `legado_rule_parser.dart` | | 🔧250 | 🔧80 | | | 🔧45 | | | +| `legado_engine.dart` | 🔧40 | | | 🔧70 | 🔧100 | 🔧10 | | 🔧30 | +| `legado_models.dart` | | | | | | | 🔧30 | | +| `source_manager.dart` | | | | | | | 🔧50 | | +| `source_manager_page.dart` | | | | | | | 🔧40 | | +| `online_book_detail_page.dart` | | | | 🔧30 | | | | | +| `legado_js_runner.dart` 🆕 | | | | | | | | 🆕150 | +| `pubspec.yaml` | | | | | | | | +1dep | + +--- + +## 十三、总结 + +整个方案的核心理念是: + +``` +RuleParser 是对现有 CSS/JSON/Regex 的兼容 → 保留为底层原子工具 +LegadoRuleExecutor 是新增的链式规则执行层 → 理解 @tag、##、||、&& +JS 分两级做 → 特判覆盖 60% + 引擎覆盖 40% +书源兼容度检测 → 导入时打标签,用户体验闭环 + +请求层不动 —— 它没问题 +Bug 优先级由截图驱动 —— 第1步修截图3,第2步解锁主流漫画源,第4步修截图1 +前4步零新依赖,直接在现有文件里改 +``` + +**一句话**:不推翻现有代码,在 `RuleParser` 之上加一层**链式规则执行器**,让 `@tag.img@src` 和 `##替换` 能被正确理解,这是解锁包子漫画 5 等主流漫画源的关键。 + +--- + +## 附录:All.json 常见规则语法统计 + +基于对 30+ 个典型漫画/小说书源规则的手工分析: + +| 语法 | 出现频率 | 典型场景 | 本方案覆盖 | +|------|:--------:|---------|:----------:| +| `@tag.xxx` | ★★★★★ | 漫画正文图片提取 | 第2步 | +| `@src` | ★★★★★ | 图片 URL 提取 | 第2步 | +| `@text` | ★★★★ | 书名/作者/章节名 | 第2步 | +| `@html` | ★★★ | 正文 HTML 提取 | 第2步 | +| `##` | ★★★★ | 广告/水印净化 | 第3步 | +| `\|\|` | ★★★★ | 备选规则 | 第3步 | +| `&&` | ★★★ | 多字段拼接 | 第3步 | +| `` / `@js:` | ★★★ | 漫画图片解密 | 第5/8步 | +| `@put` / `@get` | ★★ | 跨步骤传递 ID | 第6步 | +| `@css:` | ★★★ | 嵌套 CSS 选择 | 第2步 | +| `@data-xxx` | ★★ | data-original 等 | 第2步 | +| `,{method,body}` | ★★ | POST 搜索 | 请求层(现有) | diff --git a/docs/Sakura_Downloader_完整文档.md b/docs/Sakura_Downloader_完整文档.md new file mode 100644 index 0000000..09e12cd --- /dev/null +++ b/docs/Sakura_Downloader_完整文档.md @@ -0,0 +1,1103 @@ +# Sakura Downloader(樱云下载器)完整文档 + +> 更新日期:2026-06-14 + +--- + +## 目录 + +1. [项目概述](#1-项目概述) +2. [系统架构](#2-系统架构) +3. [目录结构详解](#3-目录结构详解) +4. [桌面端功能详解](#4-桌面端功能详解) +5. [插件系统](#5-插件系统) +6. [本地图库系统](#6-本地图库系统) +7. [缩略图墙 WebUI](#7-缩略图墙-webui) +8. [云同步系统](#8-云同步系统) +9. [Sakura Cloud 云端 API](#9-sakura-cloud-云端-api) +10. [Android 移动端](#10-android-移动端) +11. [每日提醒功能](#11-每日提醒功能) +12. [分布式爬虫系统](#12-分布式爬虫系统) +13. [环境搭建与运行](#13-环境搭建与运行) +14. [打包与发布](#14-打包与发布) +15. [测试](#15-测试) +16. [使用指南](#16-使用指南) +17. [常见问题](#17-常见问题) +18. [风险控制](#18-风险控制) + +--- + +## 1. 项目概述 + +Sakura Downloader 是一套**个人媒体采集、本地图库管理与跨设备云同步系统**。项目由三个核心部分组成: + +| 端 | 角色 | 技术栈 | +|----|------|--------| +| **Windows 桌面端** | 采集与整理中枢 | Python 3.8+ / CustomTkinter / SQLite / Playwright | +| **Sakura Cloud** | 数据与账号中心 | FastAPI / PostgreSQL / JWT / Redis | +| **Android 客户端** | 跨设备阅读与提醒 | Flutter / Riverpod / Dio | + +### 核心能力 + +- **多平台采集**:支持 Pixiv、X(Twitter)、小红书、笔趣阁小说、AO3 等平台的图片、漫画、小说、视频下载 +- **本地图库管理**:SQLite 数据库管理作品、作者、标签、收藏、关注 +- **缩略图墙**:本地 WebUI 浏览图库、搜索、标签筛选、关注同步 +- **云同步**:下载内容自动上传到 Sakura Cloud,手机端实时访问 +- **插件系统**:支持自定义 Python 插件扩展采集能力 +- **每日提醒**:云端汇总关注作者更新,手机端查看 +- **OCR 文字识别**:基于 PaddleOCR 的本地图片文字提取 +- **代理池**:支持手动代理列表和 Quake 网络空间搜索引擎获取代理 +- **分布式爬虫**:基于 Redis + aiohttp 的 Master-Worker 分布式架构 + +--- + +## 2. 系统架构 + +``` +┌──────────────────────────────────────────────────────────────────┐ +│ Windows 桌面端 │ +│ ┌─────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │ +│ │ GUI 界面 │ │ 下载引擎 │ │ 本地图库 │ │ 缩略图墙 WebUI │ │ +│ │(CustomTk│ │(多线程) │ │(SQLite) │ │(HTTP API+前端) │ │ +│ │ inter) │ │ │ │ │ │ │ │ +│ └────┬────┘ └────┬─────┘ └────┬─────┘ └────────┬─────────┘ │ +│ │ │ │ │ │ +│ ┌────┴────────────┴─────────────┴──────────────────┴─────────┐ │ +│ │ 插件系统 (Plugins) │ │ +│ │ Pixiv │ X/Twitter │ 小红书 │ 笔趣阁 │ AO3 │ 用户自定义 │ │ +│ └─────────────────────────────────────────────────────────────┘ │ +│ │ │ +│ ┌────┴──────────────────────────────────────────────────────┐ │ +│ │ 云同步客户端 (Cloud Sync) │ │ +│ │ 文件 SHA256 去重 → 分片上传 → 元数据同步 → 失败重试队列 │ │ +│ └────────────────────────────────────────────────────────────┘ │ +└───────────────────────────────┬──────────────────────────────────┘ + │ HTTPS + ▼ +┌──────────────────────────────────────────────────────────────────┐ +│ Sakura Cloud (FastAPI) │ +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │ +│ │ 用户认证 │ │ 文件存储 │ │ 图库管理 │ │ 提醒摘要服务 │ │ +│ │(JWT) │ │(分片上传) │ │(CRUD) │ │(定时任务) │ │ +│ └──────────┘ └──────────┘ └──────────┘ └──────────────────┘ │ +│ PostgreSQL 数据库 │ +└───────────────────────────────┬──────────────────────────────────┘ + │ HTTPS + ▼ +┌──────────────────────────────────────────────────────────────────┐ +│ Android 移动端 (Flutter) │ +│ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ +│ │ 发现页 │ │ 图片页 │ │ 漫画页 │ │ 书架页 │ │ 我的页 │ │ +│ └────────┘ └────────┘ └────────┘ └────────┘ └────────┘ │ +│ Riverpod 状态管理 │ +└──────────────────────────────────────────────────────────────────┘ +``` + +--- + +## 3. 目录结构详解 + +``` +Pixiv/ +├── main.py # 桌面端入口,启动 GUI +├── requirements.txt # Python 依赖 +├── README.md # 项目简介 +├── 使用说明-桌面版与安卓版.md # 终端用户使用说明 +│ +├── pixiv_app/ # 桌面端核心模块 +│ ├── gui/ +│ │ ├── app.py # 主 GUI 窗口(2700+ 行) +│ │ ├── plugin_panel.py # 插件管理面板 +│ │ └── proxy_dialog.py # 代理设置对话框 +│ ├── core/ +│ │ ├── auth.py # Pixiv 认证(Cookie/密码登录) +│ │ ├── downloader.py # 核心下载引擎 +│ │ ├── library.py # 本地 SQLite 图库 +│ │ ├── cloud_sync.py # 云同步客户端 +│ │ ├── orchestrator.py # 插件任务编排 +│ │ ├── proxy_pool.py # 代理池管理 +│ │ ├── ocr.py # 本地 OCR 索引 +│ │ ├── paths.py # 路径管理(开发/打包模式) +│ │ ├── cookie_import.py # Cookie 导入解析 +│ │ ├── fingerprint.py # 浏览器指纹 +│ │ ├── gallery_service.py # 本地图库服务层 +│ │ ├── library_importer.py # 遗留数据导入 +│ │ ├── origin_follow_sync.py # 原站关注同步 +│ │ ├── thumbnails.py # 缩略图生成 +│ │ ├── x_media_service.py # X 媒体服务 +│ │ ├── browser_proxy_pool.py # 浏览器代理池 +│ │ └── playwright_runtime.py # Playwright 运行时 +│ ├── services/ +│ │ ├── gallery_api.py # 缩略图墙 HTTP API 服务 +│ │ ├── run_gallery_api.py # API 启动入口 +│ │ ├── x_auth_manager.py # X 认证管理 +│ │ └── x_login_flow.py # X 登录流程 +│ ├── tasks/ +│ │ ├── models.py # 任务数据模型 +│ │ ├── parser.py # 输入解析器 +│ │ ├── queue.py # SQLite 任务队列 +│ │ └── watermark.py # 增量同步水印 +│ ├── runtime/ +│ │ ├── enqueue.py # 任务入队 +│ │ ├── executor.py # 任务执行器 +│ │ └── worker_loop.py # Worker 循环 +│ ├── core/plugin/ # 插件基础设施 +│ │ ├── base.py # BasePlugin 抽象基类 +│ │ ├── manager.py # PluginManager 加载/匹配 +│ │ └── generator.py # 插件模板生成器 +│ └── webui/ # 缩略图墙前端静态文件 +│ +├── plugins/ # 内置插件 +│ ├── pixiv/plugin.py # Pixiv 插件 +│ ├── x/ # X/Twitter 插件 +│ │ ├── plugin.py # 主插件逻辑 +│ │ ├── browser_opts.py # 浏览器启动参数 +│ │ ├── playwright_client.py # Playwright 客户端 +│ │ ├── selectors.py # DOM 选择器 +│ │ ├── x_login.py # X 登录 +│ │ ├── x_urls.py # URL 构造 +│ │ └── setup_plugin.py # 插件安装 +│ ├── xiaohongshu/plugin.py # 小红书插件 +│ ├── biquge/plugin.py # 笔趣阁小说插件 +│ └── ao3/plugin.py # AO3 同人小说插件 +│ +├── sakura_cloud/ # 云端 API 服务 +│ ├── main.py # FastAPI 应用入口 +│ ├── api/ +│ │ ├── v1/router.py # API 路由汇总 +│ │ ├── deps.py # 依赖注入(认证) +│ │ └── middleware.py # 中间件 +│ ├── core/ +│ │ ├── config.py # 配置管理 +│ │ ├── logging.py # 日志配置 +│ │ ├── safety.py # 安全工具 +│ │ └── security.py # JWT 与加密 +│ ├── db/ +│ │ ├── base.py # SQLAlchemy Base +│ │ └── session.py # 数据库会话 +│ ├── models/ # SQLAlchemy 模型 +│ │ ├── user.py # 用户 +│ │ ├── artwork.py # 作品 +│ │ ├── novel.py # 小说 +│ │ ├── file.py # 文件 +│ │ ├── follow.py # 关注 +│ │ ├── push.py # 推送/提醒 +│ │ ├── ocr.py # OCR +│ │ ├── device.py # 设备 +│ │ ├── admin.py # 管理员 +│ │ └── mixins.py # 混入 +│ ├── schemas/ # Pydantic 请求/响应模型 +│ ├── services/ # 业务逻辑 +│ │ ├── storage.py # 文件存储 +│ │ ├── audit.py # 审计 +│ │ ├── push.py # 推送服务 +│ │ ├── reminders.py # 提醒摘要 +│ │ ├── ocr.py # OCR 服务 +│ │ ├── thumbnails.py # 缩略图服务 +│ │ └── migrations.py # 数据库迁移 +│ ├── migrations/ # Alembic 迁移 +│ └── scripts/ +│ ├── init_db.py # 数据库初始化 +│ └── create_admin.py # 创建管理员 +│ +├── sakura_mobile/ # Flutter Android 客户端 +│ └── lib/src/ +│ ├── app.dart # 应用入口 +│ ├── core/ +│ │ ├── api_client.dart # API 客户端 +│ │ ├── session.dart # 会话管理 +│ │ ├── providers.dart # Riverpod 状态 +│ │ └── push_notifications.dart # 推送 +│ ├── models/ +│ │ ├── artwork.dart # 作品模型 +│ │ └── novel.dart # 小说模型 +│ └── pages/ +│ ├── tabs/ # 底部导航页签 +│ │ ├── feed_tab.dart # 发现 +│ │ ├── gallery_tab.dart # 图片 +│ │ ├── novels_tab.dart # 书架 +│ │ └── profile_tab.dart # 我的 +│ ├── login_page.dart # 登录/注册 +│ ├── artwork_detail_page.dart # 作品详情 +│ ├── novel_detail_page.dart # 小说详情 +│ ├── novel_reader_page.dart # 小说阅读器 +│ ├── favorites_page.dart # 收藏 +│ └── reminder_center_page.dart # 提醒中心 +│ +├── distributed_crawler/ # 分布式爬虫(可选) +│ ├── crawler/ +│ │ ├── master.py # Master 节点 +│ │ ├── worker.py # Worker 节点 +│ │ ├── spider.py # 爬虫逻辑 +│ │ ├── queue.py # Redis 优先级队列 +│ │ ├── scheduler.py # 调度器 +│ │ ├── hash_ring.py # 一致性哈希 +│ │ ├── bloom.py # 布隆过滤器去重 +│ │ ├── circuit_breaker.py # 熔断器 +│ │ ├── rate_limit.py # 速率限制 +│ │ ├── backoff.py # 退避策略 +│ │ ├── proxy.py # 代理管理 +│ │ ├── sessions.py # 会话管理 +│ │ ├── auth.py # 认证 +│ │ ├── http.py # HTTP 客户端 +│ │ ├── signatures.py # 请求签名 +│ │ ├── config.py # 配置 +│ │ ├── models.py # 数据模型 +│ │ ├── state.py # 状态/检查点 +│ │ └── results.py # 结果存储 +│ └── scripts/ +│ ├── run_master.py # 启动 Master +│ └── run_worker.py # 启动 Worker +│ +├── build_assets/ # 打包资源 +├── scripts/ # 构建脚本 +├── tools/ # 辅助工具 +├── tests/ # 测试文件 +├── docs/ # 文档(本文件所在目录) +├── dist/ # PyInstaller 打包输出 +├── releases/ # 发布成品 +│ ├── sakuradownloader.exe # Windows 桌面端成品 +│ └── app-release.apk # Android 成品 +├── data/ # 运行时数据 +├── profiles/ # Playwright 浏览器 profiles +├── runtime/ # 运行时文件(Cookie、日志等) +└── Sakura_Downloads/ # 默认下载目录 +``` + +--- + +## 4. 桌面端功能详解 + +### 4.1 GUI 主界面 (`pixiv_app/gui/app.py`) + +主界面基于 **CustomTkinter** 构建,采用浅色蓝色主题,包含以下功能卡片: + +#### 登录方式卡片 +| 登录方式 | 说明 | +|----------|------| +| **Cookie 登录**(推荐) | 从浏览器导出 cookies.txt/json,导入后自动解析为 JSON 保存。同时识别 Pixiv、X、小红书域名并分别存储 | +| **账号密码登录** | 直接使用 Pixiv 账号密码登录,但可能触发风控验证 | + +#### 抓取目标卡片 +| 平台 | 支持的模式 | +|------|-----------| +| **Pixiv** | 作者全部作品、单个作品、漫画、文章/小说、关键词图片 | +| **X / Twitter** | 作者媒体、单个推文、搜索词媒体 | +| **小红书** | 搜索关键词、单篇笔记、链接解析 | +| **笔趣阁小说** | 搜索/书籍链接(支持书名搜索、书籍页、章节页、榜单列表页) | +| **插件自动识别** | 自动匹配已导入插件中第一个 `can_handle()` 返回 True 的插件 | + +#### 性能设置卡片 +- **下载线程**:2-16 可调(代理模式下自动限制为 3-6) +- **请求间隔**:0.0-1.5 秒可调 +- **代理设置**:手动代理列表 + Quake 网络空间搜索引擎 +- **统一任务队列**:SQLite 持久化任务状态,支持多行/逗号混合输入和 `#标签` +- **作者增量同步(水印)**:只抓取上次同步之后的新作品 + +#### 云同步卡片 +- 启用/禁用 Sakura Cloud 同步 +- 账号管理(登录/注册/获取 Token) +- 测试连接按钮 + +#### 任务概览面板 +- 作品进度(完成/总计) +- 成功项目数 +- 新下载页数 +- 已跳过页数 +- 缩略图墙入口按钮 + +#### 实时进度与日志 +- 进度条 +- 统计信息(失败项目/失败页数) +- 实时日志输出(带时间戳) + +### 4.2 登录认证 (`pixiv_app/core/auth.py`) + +- **SessionStore**:本地会话持久化(JSON 文件) +- **PixivAuthClient**: + - `validate_cookie()`:请求 Pixiv 设置页验证 Cookie 有效性,提取用户名和 ID + - `login_with_password()`:模拟 Pixiv 登录流程,自动提取 CSRF Token + +### 4.3 下载引擎 (`pixiv_app/core/downloader.py`) + +核心下载能力: + +| 功能 | 函数 | 说明 | +|------|------|------| +| 目标解析 | `parse_pixiv_target()` | 从链接或 ID 识别目标类型(用户/作品/小说) | +| 作者作品列表 | `fetch_user_work_ids()` | 获取作者全部作品 ID,支持从作品链接反查作者 | +| 作品详情 | `fetch_illust_details()` | 获取作品元信息 | +| 作品页面 | `fetch_illust_pages()` | 获取多页作品的原图 URL 列表 | +| 小说详情 | `fetch_novel_details()` | 获取小说元信息 | +| 小说正文 | `fetch_novel_text()` | 获取小说文本内容 | +| 小说系列 | `fetch_novel_series_ids()` | 解析小说系列的章节列表 | +| 关键词搜索 | `search_illust_ids_by_keyword()` | 按关键词搜索作品并返回 ID 列表 | +| 图片下载 | `download_binary()` | 带重试、断点检测的二进制下载 | +| 文本保存 | `save_text_file()` | 小说 TXT 保存(UTF-8 BOM) | +| 插画下载 | `download_illust()` | 单作品完整下载流程 | +| 小说下载 | `download_novel()` | 单小说完整下载流程 | +| 批量下载 | `_download_illust_batch()` | 线程池批量下载 | +| 关键词批量 | `download_keyword_works()` | 搜索 + 批量下载 | +| 作者批量 | `download_user_works()` | 获取列表 + 批量下载 | + +**特性**: +- 自动重试(最多 3 次) +- 文件断点检测(已存在且 >1KB 自动跳过) +- 代理池支持(失败自动切换代理) +- 404 特殊处理(区分"不存在"和"无权限") +- 停止事件支持(可随时中止) + +### 4.4 代理池 (`pixiv_app/core/proxy_pool.py`) + +- **ProxyPool**:代理列表管理、并发验证、轮询选择、失败标记 +- **QuakeClient**:对接 Quake 360 网络空间搜索引擎 + - API v3 模式(需要 API Key) + - Web Assoc 模式(需要 Cookie) + - 按国家筛选代理(US/JP/KR/SG/HK 等) +- **parse_proxy_text()**:解析手动输入的代理文本 + +### 4.5 路径管理 (`pixiv_app/core/paths.py`) + +智能路径解析,自动区分开发模式和打包模式: + +| 环境 | 数据目录 | +|------|----------| +| 开发模式 | 项目根目录 | +| 打包 EXE | `%LOCALAPPDATA%\SakuraDownloader` | +| 自定义 | 设置 `SAKURA_APP_DATA_DIR` 环境变量 | + +支持从旧版 `PixivDownloader` 目录自动迁移数据。 + +### 4.6 OCR 文字识别 (`pixiv_app/core/ocr.py`) + +基于 PaddleOCR 的本地图片文字提取: + +- 支持中文/英文/混合识别 +- 大图自动缩放到 1800px 以内提高速度 +- 结果存入 SQLite 数据库 +- 支持并发控制(信号量) +- 可通过环境变量配置:`SAKURA_LOCAL_OCR_ENABLED`、`SAKURA_LOCAL_OCR_LANGUAGE` 等 + +--- + +## 5. 插件系统 + +### 5.1 插件架构 + +``` +plugins/ +├── pixiv/plugin.py # 内置 Pixiv 插件 +├── x/plugin.py # 内置 X/Twitter 插件 +├── xiaohongshu/plugin.py # 内置小红书插件 +├── biquge/plugin.py # 内置笔趣阁插件 +├── ao3/plugin.py # 内置 AO3 插件 +└── [用户自定义]/ # 可通过"导入 .py"添加 +``` + +每个插件继承 `BasePlugin`,需实现: +- `name`:插件名称 +- `can_handle(url)`:判断是否处理该 URL +- `parse(url)`:解析目标,返回 `list[Resource]` +- `download(resource, save_path)`:下载资源,返回 `list[Path]` +- `validate()`:检查插件依赖是否就绪 + +### 5.2 插件管理面板 + +桌面端主界面 → "插件管理" 按钮,提供: +- 查看已加载插件列表 +- 导入 `.py` 插件文件 +- 生成插件模板(自动填充项目结构) +- 重新加载所有插件 + +### 5.3 各平台插件详情 + +#### Pixiv 插件 +- 基于 HTTP API 直接请求 +- 支持插画、漫画、小说下载 +- 使用 Cookie 认证 + +#### X / Twitter 插件 +- 基于 Playwright 浏览器自动化 +- 支持作者媒体页解析 +- 支持单条推文图片/视频下载 +- 支持搜索词媒体抓取 +- 登录态:`runtime/x_profile`(浏览器 profile)+ `runtime/x_cookies.json` +- DOM 选择器集中在 `selectors.py` 便于维护 + +#### 小红书插件 +- 基于 Playwright 浏览器自动化 +- 支持关键词搜索 +- 支持单篇笔记图片提取 +- 支持 xhslink.com 短链解析 + +#### 笔趣阁小说插件 +- 支持书名/作者搜索 +- 支持书籍页/章节页链接解析 +- 支持榜单/列表页解析 +- 多结果时弹出选择对话框 + +#### AO3 插件 +- 同人小说平台 Archive of Our Own +- 支持作品解析和章节下载 + +--- + +## 6. 本地图库系统 + +### 6.1 数据库设计 (`pixiv_app/core/library.py`) + +使用 SQLite 数据库(默认文件:`pixiv_app_library.db`),包含以下表: + +| 表名 | 说明 | 关键字段 | +|------|------|----------| +| `artists` | 作者信息 | artist_id, name, account, avatar_url, metadata_json | +| `artworks` | 作品信息 | artwork_id, artist_id, type, title, page_count, tags, thumbnail_url | +| `tags` | 标签 | tag_id, name, translated_name | +| `artwork_tags` | 作品-标签关联 | artwork_id, tag_id | +| `artwork_files` | 下载文件记录 | artwork_id, page_index, file_path, source_url, status | +| `download_history` | 下载历史 | artwork_id, status, downloaded/skipped/failed_pages | +| `favorites` | 收藏 | artwork_id, note, created_at | +| `followed_artists` | 关注作者 | artist_id, name, enabled, last_synced_at | +| `thumbnail_cache` | 缩略图缓存 | artwork_id, page_index, source_path, thumbnail_path | +| `artwork_ocr_texts` | OCR 识别文本 | artwork_id, language, ocr_text, confidence | +| `sync_state` | 同步状态 | key, value_json | +| `download_tasks` | 下载任务队列 | fingerprint, target_type, target_id, status, priority | +| `artist_sync_watermark` | 作者同步水印 | artist_id, last_synced_illust_id | + +### 6.2 ID 编码规则 + +为避免不同平台 ID 冲突,系统使用统一编码: + +| 类型 | 编码方式 | 示例 | +|------|----------|------| +| Pixiv 插画 | 原始 ID | `12345678` | +| Pixiv 小说 | 负数 ID | `-12345678` | +| AO3 作品 | 负数偏移(-200M - work_id) | `-201234567` | + +### 6.3 关键功能 + +- **作品入库**:下载时自动写入/更新 artworks 和相关表 +- **收藏管理**:添加/移除收藏 +- **关注管理**:添加/移除关注作者,自动获取作者信息 +- **增量同步水印**:记录每个作者上次同步的作品 ID +- **遗留数据导入**:扫描下载目录导入已有文件 + +--- + +## 7. 缩略图墙 WebUI + +### 7.1 架构 + +- **后端**:本地 HTTP API 服务(`gallery_api.py`),默认端口 8765(端口占用自动尝试 8766/8767) +- **前端**:静态 HTML/JS 页面(`webui/` 目录) +- **服务层**:`gallery_service.py` 提供图库查询逻辑 + +### 7.2 API 接口 + +| 接口 | 方法 | 说明 | +|------|------|------| +| `/api/health` | GET | 健康检查 | +| `/api/session/status` | GET | 会话状态(Pixiv/X Cookie 是否可用) | +| `/api/artworks` | GET | 作品列表(支持搜索、标签、作者、类型筛选) | +| `/api/artworks/{id}` | GET | 作品详情 | +| `/api/favorites` | GET/POST | 收藏列表 / 设置收藏 | +| `/api/tags` | GET | 标签列表(支持搜索) | +| `/api/artists` | GET | 作者列表(支持搜索) | +| `/api/artists/{id}` | GET | 作者详情与作品列表 | +| `/api/followed-artists` | GET/POST | 关注作者列表 / 添加/取消关注 | +| `/api/thumbnails/build` | GET | 批量生成缩略图 | +| `/api/import/legacy` | GET/POST | 导入遗留下载数据 | +| `/api/sync/followed` | GET/POST | 同步关注作者的新作品 | +| `/api/cookies/import` | POST | 导入 Pixiv/X Cookie | +| `/api/origin-follows/sync` | POST | 从原站同步关注列表 | +| `/api/x/local-media` | GET | X 本地媒体列表 | +| `/api/x/media/preview` | POST | X 作者媒体预览 | +| `/api/x/media/download` | POST | X 媒体下载 | +| `/api/x/follows` | GET/POST | X 关注管理 | +| `/api/x/follows/sync` | POST | X 关注同步 | +| `/api/xiaohongshu/preview` | POST | 小红书预览 | +| `/api/xiaohongshu/download` | POST | 小红书下载 | +| `/media/thumbnail/{id}/{page}` | GET | 缩略图文件 | +| `/media/source/{id}/{page}` | GET | 原图文件 | + +### 7.3 功能特性 + +- 浏览本地图片和漫画(默认不显示小说) +- 搜索标题、作者、简介 +- 按标签筛选 +- 按作者查看 +- 收藏/取消收藏作品 +- 关注/取消关注作者 +- 从原站同步关注列表(Pixiv / X) +- 导入 Pixiv/X Cookie +- 关注同步页(批量检查关注作者新作品) + +--- + +## 8. 云同步系统 + +### 8.1 架构 (`pixiv_app/core/cloud_sync.py`) + +``` +下载完成 + │ + ▼ +SyncManager.sync_file() + │ + ├── SHA256 计算 + ├── 云端 check_file() ─── 已存在? → 直接关联元数据 + │ │ 不存在 + │ ▼ + ├── init_upload() ─── 获取 upload_id + ├── 分片上传 (5MB/chunk) + ├── complete_upload() ─── 获取 file_id + └── sync_metadata() ─── 关联作品元数据 + │ + ▼ + 失败? → SyncFailureQueue(指数退避重试) +``` + +### 8.2 关键组件 + +| 组件 | 说明 | +|------|------| +| `CloudSyncConfig` | 配置(API 地址、Token、分片大小、超时) | +| `SakuraCloudClient` | REST API 客户端 | +| `SyncManager` | 同步管理器(上传 + 元数据关联) | +| `SyncFailureQueue` | 失败重试队列(JSON 文件持久化,指数退避) | +| `SyncMetadata` | 作品元数据(标题、作者、标签、系列信息等) | + +### 8.3 默认云端地址 + +``` +http://api.kunkunxiaomao.top:8000 +``` + +--- + +## 9. Sakura Cloud 云端 API + +### 9.1 技术栈 + +- **框架**:FastAPI +- **数据库**:PostgreSQL(SQLAlchemy + Alembic 迁移) +- **认证**:JWT(python-jose + passlib bcrypt) +- **缓存/队列**:Redis(分布式爬虫用) + +### 9.2 API 路由 + +| 路由前缀 | 说明 | +|----------|------| +| `/api/v1/auth` | 用户注册、登录、Token 刷新 | +| `/api/v1/users` | 用户信息管理 | +| `/api/v1/gallery` | 图库 CRUD | +| `/api/v1/novels` | 小说 CRUD、阅读进度 | +| `/api/v1/upload` | 文件分片上传(init → chunk → complete) | +| `/api/v1/sync` | 文件检查、元数据同步 | +| `/api/v1/follows` | 关注管理、批量 upsert、更新提醒 | +| `/api/v1/admin` | 管理后台(需管理员权限) | +| `/api/v1/push` | 推送通知 | +| `/api/v1/ocr` | OCR 服务 | +| `/api/v1/devices` | 设备管理 | + +### 9.3 安全特性 + +- JWT Bearer Token 认证 +- 密码 bcrypt 哈希 +- 跨用户数据隔离(所有 follows 操作以当前登录用户为准) +- 外部 URL 白名单 +- 安全响应头中间件 +- CORS 配置 +- 请求追踪日志中间件 + +### 9.4 后台任务 + +| 任务 | 周期 | 说明 | +|------|------|------| +| 清理过期上传 | 每小时 | 删除超时未完成的分片上传 | +| 每日提醒摘要 | 每 5 分钟 | 检查并生成关注作者更新提醒 | + +### 9.5 数据库迁移 + +使用 Alembic 管理数据库 schema 变更: +- `sakura_cloud/migrations/` 目录 +- 启动时自动迁移(可通过 `auto_migrate_on_startup` 配置) + +--- + +## 10. Android 移动端 + +### 10.1 技术栈 + +- **框架**:Flutter +- **状态管理**:Riverpod +- **HTTP 客户端**:Dio +- **平台**:Android(APK) + +### 10.2 页面结构 + +| 页签 | 页面 | 功能 | +|------|------|------| +| **发现** | `feed_tab.dart` | 最近浏览图片/漫画/小说、更新提醒 | +| **图片** | `gallery_tab.dart` | 云端图片列表、搜索、筛选、详情 | +| **漫画** | 漫画分组浏览 | 按漫画分组入口、连续浏览、整组下载 | +| **书架** | `novels_tab.dart` | 三列彩色方块、系列归档、继续阅读、章节选择 | +| **我的** | `profile_tab.dart` | 用户资料、空间占用、收藏、每日提醒、退出登录 | + +### 10.3 小说阅读器 + +- 自动分页 +- 阅读进度记忆 +- 收藏/取消收藏 +- 当前页翻译(原文/译文切换) +- 底部阅读设置面板(字体大小、行高、背景色) +- 系列章节列表切换 + +### 10.4 登录流程 + +1. 打开 APP → 登录/注册页 +2. 使用与桌面端云同步相同的账号登录 +3. 注册成功后需手动回到登录页登录 +4. 登录后即可查看云端同步的内容 + +--- + +## 11. 每日提醒功能 + +### 11.1 工作原理 + +每日提醒**不是手机本地闹钟**,也不是系统级推送通知。 + +实际工作流程: + +``` +桌面端关注作者 → 下载新作品 → 云同步上传 + │ + ▼ +云端记录 follow + update alert + │ + ▼ +云端定时检查(每 5 分钟) + │ + ▼ +到设定时间 → 生成"每日摘要提醒" + │ + ▼ +手机端"发现"页的"更新提醒"区域展示 +``` + +### 11.2 设置步骤 + +1. 桌面端登录 Pixiv 并启用云同步 +2. 桌面端打开缩略图墙 → 关注作者 +3. 关注作者的新作品经下载后同步到云端 +4. 手机端登录同一云端账号 +5. 手机端"我的"页 → 打开"每日更新提醒"开关 +6. 设置提醒时间(默认时区 Asia/Shanghai) +7. 到点后去"发现"页查看更新提醒 + +### 11.3 关键事实 + +- 云端只会给已开启提醒的用户生成摘要 +- 同一天内已发过一次摘要,不会重复生成 +- 摘要格式:`每日 HH:MM 更新提醒` + 关注作者动态统计 +- 当前主要覆盖 Pixiv 平台(X 关注链路已部分接通) + +--- + +## 12. 分布式爬虫系统 + +### 12.1 架构 + +可选的分布式爬虫系统,基于 Master-Worker 架构: + +``` +┌──────────────────────────────────────────────────────┐ +│ Master Node │ +│ ┌──────────┐ ┌──────────┐ ┌────────────────────┐ │ +│ │ 任务队列 │ │ 结果消费 │ │ 调度器 + 健康检查 │ │ +│ │(Redis) │ │ │ │ │ │ +│ └────┬─────┘ └──────────┘ └────────────────────┘ │ +│ │ │ +│ ┌────┴──────────────────────────────────────────┐ │ +│ │ aiohttp Web API │ │ +│ │ POST /tasks POST /heartbeat GET /dashboard │ │ +│ └───────────────────────────────────────────────┘ │ +└───────────────────────┬──────────────────────────────┘ + │ + ┌───────────────┼───────────────┐ + ▼ ▼ ▼ +┌──────────┐ ┌──────────┐ ┌──────────┐ +│ Worker 1 │ │ Worker 2 │ │ Worker N │ +│(Playwright) │(Playwright) │(Playwright) +└──────────┘ └──────────┘ └──────────┘ +``` + +### 12.2 核心组件 + +| 组件 | 文件 | 说明 | +|------|------|------| +| Master | `master.py` | 任务分发、结果收集、Worker 管理、Dashboard | +| Worker | `worker.py` | 任务执行、心跳上报 | +| Spider | `spider.py` | 爬虫逻辑(Playwright 浏览器自动化) | +| RedisPriorityQueue | `queue.py` | 优先级任务队列 | +| ConsistentHashRing | `hash_ring.py` | 一致性哈希任务分配 | +| RedisBloomFilter | `bloom.py` | URL 去重 | +| CircuitBreaker | `circuit_breaker.py` | 熔断保护 | +| RateLimiter | `rate_limit.py` | 速率限制 | +| BackoffStrategy | `backoff.py` | 指数退避重试 | +| CheckpointManager | `state.py` | 检查点恢复 | + +--- + +## 13. 环境搭建与运行 + +### 13.1 桌面端(源码运行) + +```powershell +# 1. 创建虚拟环境 +python -m venv .venv + +# 2. 激活虚拟环境 +.\.venv\Scripts\activate + +# 3. 安装依赖 +pip install -r requirements.txt + +# 4. 安装 Playwright 浏览器(X 插件需要) +playwright install chromium + +# 5. 运行 +python main.py +``` + +### 13.2 云端 API(可选) + +```powershell +# 安装依赖 +pip install -r sakura_cloud/requirements.txt + +# 配置环境变量(参考 .env.cloud.example) +# DATABASE_URL=postgresql://... +# JWT_SECRET_KEY=... +# ... + +# 启动 +uvicorn sakura_cloud.main:app --reload --host 0.0.0.0 --port 8000 +``` + +### 13.3 移动端开发 + +```powershell +cd sakura_mobile + +# 安装依赖 +flutter pub get + +# 运行(需连接 Android 设备或模拟器) +flutter run + +# 静态分析 +flutter analyze + +# 构建 APK +flutter build apk --release +``` + +### 13.4 分布式爬虫(可选) + +```powershell +# 启动 Redis(需先安装) +redis-server + +# 启动 Master +python -m distributed_crawler.scripts.run_master + +# 启动 Worker(可启动多个) +python -m distributed_crawler.scripts.run_worker +``` + +--- + +## 14. 打包与发布 + +### 14.1 桌面 EXE + +```powershell +# 方式一:使用构建脚本 +scripts\build_desktop_exe.bat + +# 方式二:直接调用 PyInstaller +.\.venv\Scripts\python.exe -m PyInstaller --clean --noconfirm build_assets\PixivDownloader.spec +``` + +输出:`dist\sakuradownloader.exe`(约 68 MB) + +### 14.2 Android APK + +```powershell +scripts\build_mobile_android.bat +``` + +输出:`sakura_mobile\build\app\outputs\flutter-apk\app-release.apk`(约 54 MB) + +### 14.3 复制发布产物 + +```powershell +scripts\copy_release_artifacts.bat +``` + +将 EXE 和 APK 复制到 `releases/` 目录。 + +### 14.4 一键打包 + +```powershell +scripts\build_all.bat +``` + +### 14.5 注意事项 + +- APK 当前为**调试签名**,仅供个人测试,不宜直接上架分发 +- 正式分发需重新生成 keystore 和正式签名 +- 打包时自动包含 `plugins/`、`webui/`、`build_assets/` 等资源 + +--- + +## 15. 测试 + +### 15.1 已覆盖的测试 + +| 测试文件 | 测试内容 | +|----------|----------| +| `test_origin_follow_sync.py` | 原站关注同步幂等性 | +| `test_sakura_cloud_security.py` | 云端安全回归(401 拦截、跨用户隔离、权限) | +| `test_cloud_follow_schema.py` | 云端 follows schema 和 bulk-upsert | +| `test_plugin_manager.py` | 插件管理器加载/匹配 | +| `test_admin_console.py` | 管理后台 | +| `test_daily_reminders.py` | 每日提醒逻辑 | +| `test_push_notifications.py` | 推送通知 | +| `test_library_artist_repair.py` | 图库作者修复 | +| `test_ocr_gallery_search.py` | OCR 图库搜索 | +| `test_ao3_plugin.py` | AO3 插件测试 | + +### 15.2 运行测试 + +```powershell +# Python 测试 +python -m unittest tests.test_origin_follow_sync tests.test_sakura_cloud_security tests.test_cloud_follow_schema + +# Flutter 静态分析 +cd sakura_mobile +flutter analyze +``` + +--- + +## 16. 使用指南 + +### 16.1 桌面端快速上手 + +#### 第一步:登录 Pixiv + +1. 在已登录 Pixiv 的浏览器中使用 **Get cookies.txt** 插件导出 Cookie +2. 打开桌面端,在"登录方式"卡片中保持 **Cookie 登录** +3. 点击 **导入 cookies.txt / json**,选择导出的文件 +4. 点击 **检测会话** 确认 Cookie 有效 +5. 点击 **保存会话** 持久化 + +#### 第二步:下载内容 + +1. 选择平台:**Pixiv** +2. 选择模式:如"作者全部作品" +3. 输入目标:作者 ID 或作品链接 +4. 点击 **开始抓取** +5. 观察右侧进度和日志 + +#### 输入示例 + +| 目标类型 | 输入示例 | +|----------|----------| +| 作者 ID | `12345678` | +| 作品链接 | `https://www.pixiv.net/artworks/12345678` | +| 小说链接 | `https://www.pixiv.net/novel/show.php?id=12345678` | +| 小说系列 | `https://www.pixiv.net/novel/series/123456` | +| 关键词 | `初音未来`、`猫耳` | +| X 作者 | `@nasa` 或 `nasa` | +| X 推文 | `https://x.com/user/status/123456` | +| 小红书链接 | `https://www.xiaohongshu.com/explore/...` | + +#### 任务队列模式(推荐) + +勾选 **统一任务队列(SQLite 状态)** 后支持: +- 多行输入(每行一个目标) +- 逗号分隔多个目标 +- `#标签` 形式输入(如 `#keyword 初音未来`) +- 持久化任务状态,重启后继续 + +#### 作者增量同步 + +勾选 **作者增量同步(水印)** 后: +- 记录每个作者上次同步的最新作品 ID +- 下次下载时只抓取新作品 +- 大幅减少重复请求 + +### 16.2 浏览本地图库 + +1. 点击主界面 **打开缩略图墙** 按钮 +2. 自动在浏览器中打开本地图库页面 +3. 可以:搜索、按标签筛选、查看作者、收藏作品、关注作者 + +### 16.3 配置云同步 + +1. 勾选 **启用 Sakura Cloud 同步** +2. 点击 **账号管理** +3. 在弹窗中注册或登录云端账号 +4. 登录成功后自动保存凭证 +5. 点 **测试连接** 验证 +6. 之后下载的内容会自动上传到云端 + +### 16.4 使用 X / Twitter 采集 + +1. 在"平台"下拉选择 **X / Twitter** +2. 选择模式: + - **作者媒体**:输入作者名(如 `nasa` 或 `@nasa`),自动访问 /media 页 + - **单个推文**:输入完整推文链接 + - **搜索词媒体**:输入搜索词,自动搜索媒体 +3. 输入目标后点击开始抓取 + +> 注意:X 采集需要 Playwright 浏览器,首次使用需 `playwright install chromium` + +### 16.5 使用小红书采集 + +1. 选择平台:**小红书** +2. 导入包含小红书域名的 Cookie(同 Pixiv 导入方式) +3. 选择模式:搜索关键词 / 单篇笔记 / 链接解析 +4. 输入目标后开始抓取 + +### 16.6 手机端使用 + +1. 安装 `releases/app-release.apk` +2. 使用与桌面端云同步相同的账号登录 +3. 底部导航切换页面查看内容 +4. 在"我的"页开启每日提醒 + +--- + +## 17. 常见问题 + +### 17.1 桌面端登录失败 + +1. 优先使用 **Cookie 登录**(不是账号密码登录) +2. 重新在浏览器导出最新 cookies.txt +3. 重新导入后再点 **检测会话** +4. Cookie 失效时重新操作以上步骤 + +### 17.2 下载失败 + +- 检查 Cookie 是否有效(点"检测会话") +- 降低线程数(2-4) +- 增大请求间隔(0.5-1.0 秒) +- 尝试启用代理池 +- 确认目标内容是否设置了访问限制(R-18 等) + +### 17.3 手机端看不到内容 + +1. 桌面端是否启用了 **云同步** 并登录了云端账号 +2. 下载是否已完成(查看进度和日志) +3. 手机端是否使用同一云端账号 +4. 尝试在手机端下拉刷新 + +### 17.4 每日提醒没有内容 + +1. 是否在桌面端关注了作者 +2. 是否有新作品进入云端 +3. 是否已超过设定的提醒时间 +4. 手机端"发现"页查看"更新提醒" + +### 17.5 X 采集失败 + +1. 是否已安装 Playwright Chromium:`playwright install chromium` +2. X Cookie 是否有效(重新导出包含 x.com/twitter.com 的 Cookie) +3. 尝试在浏览器中先登录 X,再导出 Cookie + +### 17.6 APK 安装被拦截 + +- 允许"未知来源应用安装" +- 重新安装一次 +- 当前 APK 是调试签名,这是正常现象 + +### 17.7 端口被占用 + +缩略图墙默认使用 8765 端口,如被占用会自动尝试 8766、8767。 + +--- + +## 18. 风险控制 + +### 18.1 已知风险 + +| 平台 | 风险 | 建议 | +|------|------|------| +| 小红书 | 短时间内连续下载同帖图片触发警告 | 增加随机延迟、模拟浏览行为 | +| Pixiv | 高频请求触发限流 | 控制线程数、使用代理池 | +| X | 异常访问模式检测 | 合理使用、避免长时间连续运行 | + +### 18.2 使用建议 + +1. **合理控制下载频率**:不建议长时间连续运行批量任务 +2. **使用代理池**:切换 IP 降低单 IP 请求密度 +3. **Cookie 定期更新**:Cookie 过期后重新导入 +4. **本工具仅供学习研究使用**:严禁非法搬运和商业用途 +5. **X 插件特殊说明**:使用前需安装 Playwright Chromium,保持浏览器在后台勿关闭 + +### 18.3 技术保护措施 + +- 代理池自动切换失败代理 +- 请求重试机制(最多 3 次) +- 可配置请求间隔延迟 +- 404 区分处理(不重试不存在的内容) +- 分布式爬虫内置熔断器和速率限制 + +--- + +## 附录 + +### A. 环境变量 + +| 变量 | 说明 | 默认值 | +|------|------|--------| +| `SAKURA_APP_DATA_DIR` | 应用数据目录 | 开发模式=项目根目录,打包=%LOCALAPPDATA%\SakuraDownloader | +| `SAKURA_LOCAL_OCR_ENABLED` | 启用本地 OCR | `false` | +| `SAKURA_LOCAL_OCR_LANGUAGE` | OCR 语言 | `ch` | +| `SAKURA_LOCAL_OCR_MAX_DIMENSION` | OCR 图片最大尺寸 | `1800` | +| `SAKURA_LOCAL_OCR_MAX_CONCURRENCY` | OCR 最大并发 | `1` | + +### B. 依赖列表 + +``` +customtkinter>=5.2.0 # GUI 框架 +requests>=2.31.0 # HTTP 请求 +Pillow>=10.0.0 # 图片处理 +Jinja2>=3.1.0 # 模板引擎 +playwright>=1.41.0 # 浏览器自动化 +httpx>=0.27.0 # 异步 HTTP +cryptography>=45.0.0 # 加密 +firebase-admin>=6.5.0 # Firebase 推送(移动端) +paddleocr>=2.7.0 # OCR 文字识别 +``` + +### C. 开发者联系方式 + +- **博客**:http://blog.kunkunxiaomao.top +- **QQ**:2811043066 + +### D. 许可说明 + +- 本项目仅供学习研究使用 +- APK 为调试签名,仅供个人测试 +- 严禁非法搬运和商业用途 +- 使用 X 插件前需安装 Playwright Chromium + +--- + +*最后更新:2026-06-14* diff --git a/docs/Sakura_Downloader_完整项目文档.docx b/docs/Sakura_Downloader_完整项目文档.docx new file mode 100644 index 0000000..a8ff9a8 Binary files /dev/null and b/docs/Sakura_Downloader_完整项目文档.docx differ diff --git a/docs/Web端小说与漫画页面优化方案.md b/docs/Web端小说与漫画页面优化方案.md new file mode 100644 index 0000000..598fb8f --- /dev/null +++ b/docs/Web端小说与漫画页面优化方案.md @@ -0,0 +1,393 @@ +# 小说与漫画三级层级展示 —— 前端改造方案 + +## Context + +当前 `sakura_admin_web` 的小说和漫画展示是 **2 级扁平结构**:列表页将同一部作品的章节折叠为一张"作品卡片",点击直接跳转到单章阅读器。用户无法浏览"一本书下面有哪些章节"这个中间层。 + +本方案将漫画和小说改为 **3 级层级导航**: + +``` +Level 1 (系列列表) → Level 2 (章节列表) → Level 3 (内容阅读) +``` + +## 后端 API 现状与数据模型 + +### Novel(小说) +- **有专用 series API**:`GET /novels/series`(列表)、`GET /novels/series/{series_id}`(单系列含全部章节) +- `NovelSeriesGroup` 包含:`series_id, series_title, author, chapter_count, cover_path, chapters[]` +- 每个 chapter(`NovelOut`)有:`id, title, chapter_title, chapter_order, updated_at` +- 当前前端调的是 `GET /me/novels`(扁平 rows),客户端自己 `groupNovels()` + +### Manga/Comic(漫画) +- **无专用 series API**,漫画存储为 `Artwork` 行(`content_type="manga"`) +- 系列分组依赖 JSON 字段 `artwork_metadata.series_id` / `artwork_metadata.series_title` +- 当前前端调用 `GET /me/gallery?content_type=manga`,客户端 `groupManga()` 分组 +- 需要在客户端缓存分组数据,供 Level 2 页面使用 + +--- + +## 关键设计决策(必读) + +### 1. 漫画 seriesKey 稳定性规则 + +`seriesKey` 是 Level 1 跳 Level 2 的路由参数,也是缓存的索引,必须稳定且防冲突。 + +**构造规则(优先级从高到低):** + +``` +seriesKey = series_id + ? `manga-${series_id}` + : `manga-title-${series_title || title}` +``` + +说明: +- 优先用 `artwork_metadata.series_id`,它最稳定 +- `series_id` 缺失时用 `series_title || title` 回退,加 `manga-title-` 前缀避免与 `manga-` 冲突 +- **所有来源的值都要 `trim()`** +- URL 传递时使用 `encodeURIComponent(seriesKey)`,接收端用 Vue Router 自动解码后的值 + +### 2. 小说 series_id 类型统一 + +后端可能返回数字或字符串类型的 `series_id`。**所有地方必须统一转成字符串:** + +``` +seriesId: String(item.series_id) +``` + +原因:Vue Router 的 `:seriesId` 参数总是字符串,如果缓存 key 或比较逻辑里混了数字类型,会导致匹配失败。 + +### 3. 漫画缓存缺失回退流程(明确) + +当用户直接访问 `/app/manga/series/:seriesKey` 而缓存为空时,按以下步骤回退: + +1. 调用 `GET /me/gallery?content_type=manga&limit=200` +2. 对返回的 rows 执行与 `UserListPage.groupManga()` **完全一致的分组逻辑** +3. 在所有分组结果中查找 `seriesKey` 匹配的组(匹配规则同构造规则) +4. 找到后填充页面 +5. 未找到则显示错误:"未找到该系列的章节" + +这样保证了不管是从 Level 1 正常导航进来,还是直接访问 URL,显示结果一致。 + +### 4. Level 3 返回逻辑的 fallback + +阅读器中通过 `fromSeries` query 参数构建"返回章节列表"链接: + +``` +if (fromSeries) → /app/${kind}/series/${fromSeries} +else → /app/${kind} (返回系列列表) +``` + +面包屑中间的"章节列表"链接同理——没有 `fromSeries` 时不渲染该级面包屑,直接从 Level 1 跳到 Level 3。 + +### 5. 路由顺序严格要求 + +`/app` 下的漫画和小说子路由必须按以下顺序注册(**顺序错会导致路由匹配错误**): + +``` +1. manga ← Level 1 +2. manga/series/:seriesKey ← Level 2(固定路径段优先) +3. manga/read/:id ← Level 3(固定路径段优先) +4. manga/:id → redirect ← 旧路由兼容,放最后 +5. novels ← Level 1 +6. novels/series/:seriesId ← Level 2(固定路径段优先) +7. novels/read/:id ← Level 3(固定路径段优先) +8. novels/:id → redirect ← 旧路由兼容,放最后 +``` + +原理:`manga/series/:seriesKey` 有固定前缀 `series/`,如果放在 `manga/:id` 之后,Vue Router 会把 `series` 当成 `:id` 的值匹配进去。**固定路径段必须排在动态 `:id` 前面。** + +### 6. 章节数据 normalize + +不同来源的章节字段命名不一致(漫画在 `artwork_metadata` 里嵌套、小说是顶层字段),建议统一写一个 normalize 函数,不要在各处散着取字段。 + +**小说章节 normalize(输入:`NovelOut`):** + +| 要取的字段 | 取值路径 | +|-----------|---------| +| `id` | `String(item.id)` | +| `title` | `item.chapter_title \|\| item.title \|\| "未命名章节"` | +| `order` | `Number(item.chapter_order \|\| 0)` | +| `updated_at` | `item.updated_at \|\| ""` | + +**漫画章节 normalize(输入:`ArtworkOut` 单行):** + +| 要取的字段 | 取值路径 | +|-----------|---------| +| `id` | `String(item.id)` | +| `title` | `item.title \|\| item.artwork_metadata?.chapter_title \|\| "未命名章节"` | +| `order` | `Number(item.artwork_metadata?.chapter_order \|\| 0)` | +| `page_count` | `Number(item.page_count \|\| 1)` | +| `thumbnail_url` | `item.thumbnail_url \|\| ""` | +| `updated_at` | `item.updated_at \|\| ""` | + +在 `UserListPage.groupManga()` 和 `UserChapterListPage.loadMangaSeries()` 中都应使用同一套 normalize。 + +### 7. Level 2 的状态覆盖 + +`UserChapterListPage` 必须覆盖以下四种状态,否则直接访问 URL 时体验差: + +| 状态 | 条件 | UI | +|------|------|----| +| **加载中** | `loading === true` | `"正在加载章节..."` | +| **加载失败** | `error !== ""` | 显示错误信息,带"重试"按钮 | +| **空章节** | `!loading && !error && chapters.length === 0` | `"暂无章节"` | +| **缓存失效重载** | manga 缓存命空 → 自动触发 API 回退 | `"正在重新加载..."`(过渡态) | + +--- + +## 路由改造 + +### 新路由结构 + +**文件:** `src/router/index.ts` —— 修改 + +``` +修改前(2 级) 修改后(3 级) +───────────────── ───────────────── +/app/novels /app/novels ← Level 1 系列列表 +/app/novels/:id /app/novels/series/:seriesId ← Level 2 章节列表(新) + /app/novels/read/:id ← Level 3 阅读器(原 /novels/:id) + +/app/manga /app/manga ← Level 1 系列列表 +/app/manga/:id /app/manga/series/:seriesKey ← Level 2 章节列表(新) + /app/manga/read/:id ← Level 3 详情(原 /manga/:id) +``` + +**路由注册顺序(严格):** + +```ts +// 漫画 +{ path: "manga", component: UserListPage, props: { kind: "manga" } }, +{ path: "manga/series/:seriesKey", component: UserChapterListPage, props: (route) => ({ kind: "manga", seriesKey: route.params.seriesKey }) }, +{ path: "manga/read/:id", component: UserDetailPage, props: (route) => ({ kind: "manga", id: route.params.id }) }, +// 小说 +{ path: "novels", component: UserListPage, props: { kind: "novels" } }, +{ path: "novels/series/:seriesId", component: UserChapterListPage, props: (route) => ({ kind: "novels", seriesId: route.params.seriesId }) }, +{ path: "novels/read/:id", component: UserDetailPage, props: (route) => ({ kind: "novels", id: route.params.id }) }, +// 旧路由重定向(必须放最后) +{ path: "manga/:id", redirect: (to) => `/app/manga/read/${to.params.id}` }, +{ path: "novels/:id", redirect: (to) => `/app/novels/read/${to.params.id}` }, +``` + +--- + +## 改造清单 + +### 一、新建文件 + +#### 1. `src/stores/seriesCache.ts` —— 漫画系列缓存 + +**为何需要:** 后端没有 manga series 端点。Level 1 页面做完客户端分组后,需要把分组数据传给 Level 2 页面。使用 Vue `reactive()` 对象做跨组件共享缓存。 + +```ts +// 核心结构 +interface MangaChapterCacheEntry { + id: string // gallery item id + title: string // 章节标题(normalize 后) + page_count: number // 页数 + thumbnail_url: string // 缩略图 + updated_at: string // 更新时间 +} + +interface MangaSeriesCacheEntry { + seriesKey: string // 唯一标识(按"关键设计决策 1"构造) + seriesTitle: string // 系列标题 + coverUrl: string // 封面图 + author: string // 作者 + chapters: MangaChapterCacheEntry[] // 该系列下所有章节(已 normalize 且按 order 排序) +} +// 导出 mangaSeriesCache: reactive> +// 导出 clearMangaCache(): void +``` + +#### 2. `src/views/app/UserChapterListPage.vue` —— 章节列表页(Level 2) + +**复用项目 pattern:** 通过 `kind` prop 同时支持 novels 和 manga。 + +**Props:** + +```ts +{ + kind: "novels" | "manga" + seriesId?: string // novels: 后端 series_id(统一 String 类型) + seriesKey?: string // manga: 客户端分组 key +} +``` + +**模板结构:** +- 面包屑导航:`小说/漫画 > 系列标题` +- "← 返回" 按钮(`router.back()`) +- **四种状态覆盖:** + - 加载中 → `"正在加载章节..."` + - 加载失败 → `error` 文本 + 重试按钮 + - 空章节 → `"暂无章节"`(加载成功但 chapters 为空) + - 缓存失效重载 → manga 缓存命中但 chapters 为空时,自动切到 API 回退 +- 系列信息头:封面 + 标题 + 作者 + 章节数 +- 章节列表:每行显示序号 | 章节标题 | 页数/更新时间 | → + +**数据加载:** +- `kind="novels"`:调用 `GET /novels/series/{seriesId}`,对返回的 chapters 执行 normalize +- `kind="manga"`: + 1. 优先读 `mangaSeriesCache[seriesKey]` + 2. 缓存命中且 chapters 不为空 → 直接展示 + 3. 缓存未命中或 chapters 为空 → 按"关键设计决策 3"执行 API 回退 + +**跳转到 Level 3:** `/app/{kind}/read/{id}?fromSeries={encodedSeriesRef}` + +--- + +### 二、修改现有文件 + +#### 3. `src/views/app/UserListPage.vue` —— 系列列表页(Level 1) + +##### 3.1 novels 加载改为 `/novels/series` + +当前 `load()` → `GET /me/novels` → `groupNovels()`。改为: +- `endpoint()` 对 novels 返回 `/novels/series` +- `params()` 对 novels 传 `{ include_chapters: false }`(后端返回轻量 series 摘要) +- `groupNovels()` 直接从 series 数据映射 WorkGroup,不再做客户端分组 +- WorkGroup 新增 `seriesId: String(series.series_id)` 字段 + +##### 3.2 manga 加载保持 `/me/gallery`,增强分组逻辑 + +- 按照"关键设计决策 1"构造稳定的 `seriesKey` +- 构建 WorkGroup 时同步写入 `mangaSeriesCache[seriesKey]` +- 缓存中的 chapters 按"关键设计决策 6"的 normalize 规则统一格式,并已按 order 排序 +- WorkGroup 新增 `seriesKey` 字段 + +##### 3.3 detailUrl 修改 + +```ts +// 修改前:直接跳阅读器 +function detailUrl(id: string) { return `/app/${props.kind}/${id}` } + +// 修改后:跳章节列表页 +function detailUrl(group: WorkGroup): string { + if (kind === "novels") return `/app/novels/series/${group.seriesId}` + if (kind === "manga") return `/app/manga/series/${encodeURIComponent(group.seriesKey!)}` + ... +} + +// 为 gallery / alerts 保留原始逻辑 +function detailUrlById(id: string): string { + return `/app/${props.kind}/${id}` +} +``` + +##### 3.4 WorkGroup 类型扩展 + +```ts +type WorkGroup = { + // ... 原有字段 + seriesKey?: string // manga 用(稳定构造) + seriesId?: string // novels 用(统一 String) +} +``` + +##### 3.5 分页调整 + +novels 改用 `/novels/series` 后不再需要前端分页(后端一次返回所有系列),因此: +- `supportsPagination` computed 对 novels 返回 false +- 隐藏分页控件 + +#### 4. `src/views/app/UserDetailPage.vue` —— 阅读器页(Level 3) + +- **新增面包屑导航**(仅 novels/manga): + - 始终显示:`小说/漫画 > 当前章节标题` + - `fromSeries` 存在时插入中间级:`小说/漫画 > [章节列表] > 当前章节标题` +- **新增返回按钮**:`"← 返回"`(`$router.back()`) +- **`fromSeries` 参数处理**(按"关键设计决策 4"): + ```ts + const fromSeries = computed(() => String(route.query.fromSeries || "")) + const backToListRoute = computed(() => { + if (fromSeries.value) return `/app/${props.kind}/series/${fromSeries.value}` + return `/app/${props.kind}` // fallback 到系列列表 + }) + ``` +- 原有阅读器/详情逻辑不变 + +#### 5. `src/router/index.ts` —— 路由配置 + +- 新增 `UserChapterListPage` 导入 +- 新增 4 条路由(novels/manga 的 series 和 read),严格按照"关键设计决策 5"的顺序排列 +- 末尾添加 2 条旧路由重定向 + +--- + +### 三、CSS 新增样式 + +**文件:** `src/styles.css` —— 追加 + +| 样式类 | 用途 | +|---------|------| +| `.breadcrumb` | 面包屑导航容器(flex, 小字号 muted) | +| `.breadcrumb a` | 面包屑链接(pink-strong, hover opacity) | +| `.breadcrumb .current` | 当前页名(ink, bold) | +| `.series-header` | 章节列表页的系列信息头(flex, 封面+标题+作者+统计) | +| `.series-cover` | 系列封面图(110px 宽, 3:4 比例) | +| `.series-cover.placeholder` | 封面色块占位(粉色背景, 居中文字) | +| `.series-info` | 系列元信息容器 | +| `.series-info h2` | 系列标题(ellipsis 溢出省略) | +| `.chapter-list` | 章节列表网格(gap 8px) | +| `.chapter-row` | 单行章节(flex, hover 阴影+边框变色) | +| `.chapter-row .chapter-index` | 圆形粉色序号标识(36px 圆, number) | +| `.chapter-row .chapter-info` | 章节标题与元信息(flex 收缩, ellipsis) | +| `.chapter-row .chapter-info strong` | 章节标题 | +| `.chapter-row .chapter-info .muted` | 页数 / 更新时间的灰色副文本 | + +--- + +## 数据流示意 + +### 小说 +``` +UserListPage ──GET /novels/series?include_chapters=false──→ [{ series_id, chapter_count, cover_path }] + │ seriesId = String(series_id) + │ 点击卡片 → /app/novels/series/:seriesId + ▼ +UserChapterListPage ──GET /novels/series/:id──→ { chapters: [{id, chapter_title, ...}] } + │ 对 chapters 执行 normalize + │ 点击章节 → /app/novels/read/:id?fromSeries=:seriesId + ▼ +UserDetailPage ──GET /novels/:id/content──→ 正文阅读 + │ 面包屑:小说 > [章节列表] > 当前章名 + │ fromSeries 缺失时省略中间级 +``` + +### 漫画 +``` +UserListPage ──GET /me/gallery?content_type=manga──→ client groupManga() + │ seriesKey 按稳定规则构造(series_id 优先) + │ 章节按 normalize 统一格式、排序 + │ └→ 写入 mangaSeriesCache[seriesKey] + │ 点击卡片 → /app/manga/series/:seriesKey(encoded) + ▼ +UserChapterListPage ──读 mangaSeriesCache[seriesKey]──→ 展示 + │ 缓存未命中或 chapters 为空: + │ ① GET /me/gallery?content_type=manga&limit=200 + │ ② 执行同一套 groupManga() 分组逻辑 + │ ③ 查找匹配 seriesKey 的组 + │ ④ 找到 → 填充;未找到 → 显示错误 + │ 点击章节 → /app/manga/read/:id?fromSeries=:seriesKey(encoded) + ▼ +UserDetailPage ──GET /gallery/:id──→ 详情展示 + │ 面包屑:漫画 > [章节列表] > 当前章名 + │ fromSeries 缺失时省略中间级 +``` + +--- + +## 验证方案 + +1. **Level 1 → Level 2 导航**:访问 `/app/novels`,点击任意作品卡片,确认跳转到 `/app/novels/series/:seriesId`,且显示正确的章节列表 +2. **Level 2 → Level 3 导航**:在章节列表页点击某一章,确认跳转到 `/app/novels/read/:id?fromSeries=:seriesId`,正文正常显示 +3. **面包屑返回**:在阅读器中点击面包屑"章节列表",确认返回 Level 2;点击"← 返回",确认能正常回退 +4. **面包屑 fallback**:直接访问 `/app/novels/read/:id`(无 fromSeries),确认面包屑不显示中间级,返回按钮回到 `/app/novels` +5. **漫画缓存链路**:从 Level 1 → Level 2 → Level 3 完整走通漫画导航 +6. **漫画缓存缺失回退**:清除缓存后直接访问 `/app/manga/series/:seriesKey`,确认自动回退到 API 拉取并正确展示 +7. **漫画 series_id 缺失**:对没有 `artwork_metadata.series_id` 的漫画数据,确认 seriesKey 回退到 `manga-title-{series_title}` 且导航正常 +8. **Level 2 空状态**:对章节数为 0 的系列(理论上不应出现,但做防御),确认显示"暂无章节" +9. **Level 2 错误状态**:模拟 API 失败,确认显示错误信息 + 重试按钮 +10. **不受影响的功能**:图片(gallery)、提醒(alerts)、个人资料(profile)页面仍正常运作 +11. **旧路由兼容**:访问 `/app/novels/旧id` 确认重定向到 `/app/novels/read/旧id` +12. **管理后台不受影响**:`/admin/novels`、`/admin/gallery` 表格正常 diff --git a/docs/Web端详情加载与手机端搜索优化方案.md b/docs/Web端详情加载与手机端搜索优化方案.md new file mode 100644 index 0000000..690d0f2 --- /dev/null +++ b/docs/Web端详情加载与手机端搜索优化方案.md @@ -0,0 +1,1245 @@ +# Web 端详情加载、手机端搜索与加载性能优化方案 + +> 更新日期:2026-06-15 +> 范围:Sakura Admin Web (Vue.js) + Sakura Mobile (Flutter) + Sakura Cloud (FastAPI) +> 说明:本文档提供问题分析与优化方案,不包含实际代码改动。 + +--- + +## 目录 + +1. [管理员账号密码](#一管理员账号密码) +2. [Web 端详情数据按需加载问题](#二web-端详情数据按需加载问题) +3. [Web 端缺少漫画板块问题](#三web-端缺少漫画板块问题) +4. [Web 端列表加载性能问题](#四web-端列表加载性能问题) +5. [手机端搜索不到图书问题](#五手机端搜索不到图书问题) +6. [手机端加载过慢问题](#六手机端加载过慢问题) +7. [实施优先级与排期建议](#七实施优先级与排期建议) + +--- + +## 一、管理员账号密码 + +### 1.1 现状 + +本项目**没有预设的默认管理员账号密码**。数据库初始化脚本(`sakura_cloud/scripts/init_db.py`)仅创建空表结构,不插入任何种子数据。 + +管理员账号需要**部署后手动创建**,使用后端提供的 CLI 脚本。 + +### 1.2 创建管理员账号的方法 + +**脚本位置:** `sakura_cloud/scripts/create_admin.py` + +**使用方法:** + +```bash +# 在后端服务器上执行(需在 sakura_cloud 可导入的环境下) +python -m sakura_cloud.scripts.create_admin \ + --username admin \ + --email admin@example.com \ + --password YourPassword123 +``` + +**参数说明:** + +| 参数 | 必填 | 说明 | +|------|------|------| +| `--username` | 是 | 管理员用户名 | +| `--email` | 是 | 管理员邮箱 | +| `--password` | 否 | 密码(最少 8 位)。如不传,运行时会交互式提示输入(不显示在终端) | + +**脚本逻辑:** +1. 检查是否已存在 `SUPER_ADMIN` 角色的用户 +2. 如果数据库中没有匹配用户名/邮箱的用户 → 创建新的 SUPER_ADMIN +3. 如果已存在该用户但角色不是 SUPER_ADMIN,且已有其他 SUPER_ADMIN → 拒绝提升(防止越权) +4. 如果已存在该用户且尚无 SUPER_ADMIN → 将其提升为 SUPER_ADMIN 并更新密码 + +### 1.3 基础设施凭证(仅供参考,非管理员登录) + +以下凭证出现在配置文件和 `docker-compose.yml` 中,但这些是**基础设施组件**的凭证,**不能用于 Web/APP 登录**: + +| 组件 | 用户名 | 密码 | 用途 | +|------|--------|------|------| +| PostgreSQL | `sakura` | `sakura` | 数据库连接 | +| MinIO | `minioadmin` | `minioadmin` | 对象存储 | +| JWT Secret | - | `change-this-secret-in-production` | JWT 签名密钥(生产环境务必修改) | + +> ⚠️ **安全提醒:** 以上均为默认值,生产部署前务必修改,特别是 JWT Secret 和数据库密码。 + +### 1.4 如果忘记了管理员密码 + +由于没有预设账号,如果忘记密码: + +1. **方法一(推荐):** 重新运行 `create_admin.py` 脚本,指定相同的 `--username` 和 `--email`,输入新密码即可覆盖。 +2. **方法二:** 直接在数据库中修改 `password_hash` 字段(需要先用 Python 生成 bcrypt/pbkdf2_sha256 哈希)。 +3. **方法三:** 如果数据库中有其他管理员账号,可以用该账号登录后在管理后台重置密码。 + +--- + +## 二、Web 端详情数据按需加载问题 + +### 2.1 问题现象 + +在 Web 管理前端(`sakura_admin_web`)中,用户点击列表中的画廊(gallery)或小说(novels)项目后,跳转到详情页面,但页面只显示: + +``` +详情数据由现有 API 按需加载。 +``` + +页面不会展示任何实际内容,用户无法看到作品详情。 + +### 2.2 根因分析 + +问题出在 **`UserDetailPage.vue` 是一个未实现的占位组件**,它从始至终没有发起任何 API 请求来获取详情数据。 + +#### 根因 1:`UserDetailPage.vue` 未调用任何 API + +**文件位置:** `sakura_admin_web/src/views/app/UserDetailPage.vue` + +当前完整代码: + +```vue + + + +``` + +**缺失的关键要素:** +- ❌ 没有 `import { http, apiData } from "../../api/http"` — 未导入 HTTP 客户端 +- ❌ 没有 `import { useRoute } from "vue-router"` — 未获取路由参数 +- ❌ 没有 `onMounted` 生命周期钩子 — 不会在组件挂载时加载数据 +- ❌ 没有任何 `ref()` 存储数据 — 没有响应式数据容器 +- ❌ 当 `kind === "gallery"` 或 `kind === "novels"` 时,`content` 只是一段固定死的中文字符串 + +> 注:`kind === "profile"` 能显示用户资料,仅因为 `authState.user` 已在路由守卫 `router.beforeEach` 中通过 `loadMe()` 提前加载了。这是"碰巧能用",并非设计意图。 + +#### 根因 2:路由未传递 `id` 参数给组件 + +**文件位置:** `sakura_admin_web/src/router/index.ts` 第 30-32 行 + +```typescript +{ path: "gallery/:id", component: UserDetailPage, props: { kind: "gallery" } }, +{ path: "novels/:id", component: UserDetailPage, props: { kind: "novels" } }, +``` + +`props` 被设置为**静态对象** `{ kind: "gallery" }`,这意味着: +- `props.id` 始终为 `undefined` +- 即便组件内部添加了 API 调用,也无法知道要请求哪个 ID 的作品 + +正确做法应使用以下之一: +```typescript +// 方式一:自动传递所有 params +props: true + +// 方式二:函数合并 params 与自定义 props +props: (route) => ({ kind: "gallery", id: route.params.id }) +``` + +#### 根因 3:后端其实已有详情 API(但前端没调用) + +后端已经实现了详情接口: + +| 方法 | 路径 | 文件 | 行号 | +|------|------|------|------| +| GET | `/api/v1/gallery/{artwork_id}` | `sakura_cloud/api/v1/endpoints/gallery.py` | 230 | +| GET | `/api/v1/novels/{novel_id}` | `sakura_cloud/api/v1/endpoints/novels.py` | 222 | + +但是: +- 这些接口在 **`/api/v1/gallery/{id}`** 路径下,而非 `/api/v1/me/gallery/{id}` +- 前端 `UserListPage.vue` 列表调用的是 `GET /me/gallery` 和 `GET /me/novels` +- 详情页如果想保持和列表页一致的 `/me/` 前缀,后端缺少 `GET /me/gallery/{id}` 和 `GET /me/novels/{id}` 路由 +- 但这不影响功能——直接调用 `/api/v1/gallery/{id}` 和 `/api/v1/novels/{id}` 即可 + +#### 对比:相同模式的 `AdminUserDetail.vue` 工作正常 + +```typescript +import { onMounted, ref } from "vue"; +import { useRoute } from "vue-router"; +import { apiData, http } from "../../api/http"; + +const route = useRoute(); +const user = ref | null>(null); + +async function load() { + const response = await http.get(`/admin/users/${route.params.id}`); + user.value = apiData>(response); +} + +onMounted(load); +``` + +这说明代码库中**已有正确的详情加载模式**,`UserDetailPage.vue` 只需要参照 `AdminUserDetail.vue` 的模式来实现即可。 + +### 2.3 修复方案 + +#### 方案(推荐):按 AdminUserDetail 模式实现 UserDetailPage + +**修改位置:** + +| 文件 | 修改内容 | +|------|----------| +| `sakura_admin_web/src/views/app/UserDetailPage.vue` | 完整重写,添加数据加载逻辑和详情展示模板 | +| `sakura_admin_web/src/router/index.ts` 第 30、32 行 | 修改 `props` 配置,传递 `id` 参数 | +| (可选)`sakura_cloud/api/v1/endpoints/me.py` | 新增 `GET /me/gallery/{id}` 和 `GET /me/novels/{id}` 路由 | + +**具体做法:** + +**步骤 1:修复路由传参**(`router/index.ts`) + +```typescript +// 将: +{ path: "gallery/:id", component: UserDetailPage, props: { kind: "gallery" } }, +{ path: "novels/:id", component: UserDetailPage, props: { kind: "novels" } }, + +// 改为(推荐方式一,保持简洁): +{ path: "gallery/:id", component: UserDetailPage, props: (route) => ({ kind: "gallery", id: route.params.id }) }, +{ path: "novels/:id", component: UserDetailPage, props: (route) => ({ kind: "novels", id: route.params.id }) }, +``` + +**步骤 2:重写 UserDetailPage.vue**(参照 `AdminUserDetail.vue` 模式) + +1. 导入 `onMounted`、`ref`、`http`、`apiData` +2. 在 `props` 类型中增加 `id?: string` +3. 在 `onMounted` 中根据 `props.kind` 调用不同的 API: + - `kind === "gallery"` → `GET /api/v1/gallery/${props.id}` + - `kind === "novels"` → `GET /api/v1/novels/${props.id}` + - `kind === "profile"` → 保持现有逻辑(使用 `authState.user`) +4. 添加 loading 和 error 状态 +5. 更新模板,将 `{{ content }}` 替换为结构化展示(标题、作者、标签、缩略图等) + +**步骤 3(可选):在后端添加 `/me/` 前缀的详情路由** + +在 `sakura_cloud/api/v1/endpoints/me.py` 中添加: + +```python +@router.get("/gallery/{artwork_id}") +def my_gallery_detail(artwork_id: str, current_user: CurrentUser, db: DbSession): + """获取当前用户的单个画廊作品详情""" + from sakura_cloud.api.v1.endpoints.gallery import get_artwork + return get_artwork(artwork_id, current_user, db) + +@router.get("/novels/{novel_id}") +def my_novel_detail(novel_id: str, current_user: CurrentUser, db: DbSession): + """获取当前用户的单个小说作品详情""" + from sakura_cloud.api.v1.endpoints.novels import get_novel + return get_novel(novel_id, current_user, db) +``` + +### 2.4 预期效果 + +- 用户从列表页点击任意画廊/小说作品,详情页**正常展示作品信息**(标题、作者、标签、图片、描述等) +- 加载过程中显示 loading 状态,加载失败显示错误提示 +- `kind === "profile"` 的个人资料页不受影响 + +--- + +## 三、Web 端缺少漫画板块问题 + +### 3.1 问题现象 + +在 Web 管理前端(`sakura_admin_web`)中,侧边栏导航只有以下入口: + +- 概览(`/app`) +- 图片(`/app/gallery`) +- 小说(`/app/novels`) +- 提醒(`/app/alerts`) +- 我的(`/app/profile`) + +**缺少"漫画"板块**,用户无法在 Web 端独立浏览、搜索漫画类型的内容。漫画作品即使已同步到云端,也只能混在"图片"板块中与其他插画(illust)混合展示,无法单独筛选。 + +### 3.2 根因分析 + +#### 根因 1:前端路由和侧边栏缺少 manga 入口 + +**文件位置:** `sakura_admin_web/src/router/index.ts` 第 27-35 行 + +当前 `/app` 路由子节点: + +```typescript +children: [ + { path: "", component: UserDashboard }, + { path: "gallery", component: UserListPage, props: { kind: "gallery" } }, + { path: "gallery/:id", component: UserDetailPage, props: { kind: "gallery" } }, + { path: "novels", component: UserListPage, props: { kind: "novels" } }, + { path: "novels/:id", component: UserDetailPage, props: { kind: "novels" } }, + { path: "alerts", component: UserListPage, props: { kind: "alerts" } }, + { path: "profile", component: UserDetailPage, props: { kind: "profile" } } +] +``` + +**没有 `manga` 路由**,也没有对应的列表页和详情页路由。 + +**文件位置:** `sakura_admin_web/src/views/app/AppLayout.vue` 第 4-9 行 + +```vue +概览 +图片 +小说 +提醒 +我的 +``` + +侧边栏没有"漫画"导航项。 + +#### 根因 2:UserListPage 的 `kind` 类型不包含 manga + +**文件位置:** `sakura_admin_web/src/views/app/UserListPage.vue` 第 27 行 + +```typescript +const props = defineProps<{ kind: "gallery" | "novels" | "alerts" }>(); +``` + +`kind` 的 TypeScript 类型仅为 `"gallery" | "novels" | "alerts"`,不包含 `"manga"`。 + +第 31 行的标题映射也缺少 manga: + +```typescript +const title = computed(() => ({ gallery: "图片", novels: "小说", alerts: "提醒中心" }[props.kind])); +``` + +第 41 行的 API 调用路径硬编码了 `kind`: + +```typescript +const response = await http.get(`/me/${props.kind}`, { params }); +``` + +如果 `kind === "manga"`,会请求 `/me/manga`,但后端 `me.py` 中没有这个路由。 + +#### 根因 3:后端其实已支持按 content_type 筛选漫画 + +**关键事实:后端不区分 gallery 和 manga,它们共用同一张 `artworks` 表。** + +- `Artwork.content_type` 字段默认值为 `"illust"`,但也支持 `"manga"`(漫画)等值 +- `/api/v1/gallery` 接口已支持 `content_type` 查询参数(`gallery.py` 第 35 行) +- `/api/v1/me/gallery` 透传了 `content_type` 参数(`me.py` 第 39-50 行) + +这意味着**为 Web 端增加漫画板块,后端几乎不需要改动**——只需要在调用 `/me/gallery` 时传入 `content_type=manga` 即可。 + +#### 根因 4:UserDetailPage 同样不支持 manga 类型 + +`UserDetailPage.vue` 的 `kind` prop 类型(第 26 行)和路由传参同样缺少 manga 支持。即使添加了列表页路由,详情页也会因 `kind === "manga"` 时 `content` 返回固定字符串 "详情数据由现有 API 按需加载。" 而无法显示内容。 + +### 3.3 修复方案 + +#### 方案(推荐):前端 4 处修改即可新增漫画板块 + +**修改位置:** + +| 文件 | 修改内容 | +|------|----------| +| `sakura_admin_web/src/router/index.ts` | 新增 `/app/manga` 和 `/app/manga/:id` 路由 | +| `sakura_admin_web/src/views/app/AppLayout.vue` | 侧边栏新增"漫画"导航项 | +| `sakura_admin_web/src/views/app/UserListPage.vue` | `kind` 类型和标题映射增加 `manga` | +| `sakura_admin_web/src/views/app/UserDetailPage.vue` | 详情加载逻辑增加 `manga` 分支 | + +**具体做法:** + +**步骤 1:新增路由**(`router/index.ts`) + +```typescript +// 在 /app 的 children 数组中增加两条新路由 +{ path: "manga", component: UserListPage, props: { kind: "manga" } }, +{ path: "manga/:id", component: UserDetailPage, props: (route) => ({ kind: "manga", id: route.params.id }) }, +``` + +**步骤 2:侧边栏增加入口**(`AppLayout.vue`) + +```vue +漫画 + +``` + +**步骤 3:UserListPage 增加 manga 支持** + +```typescript +// 将 kind 类型扩展为: +const props = defineProps<{ kind: "gallery" | "manga" | "novels" | "alerts" }>(); + +// 标题映射增加: +const title = computed(() => ({ + gallery: "图片", + manga: "漫画", // ← 新增 + novels: "小说", + alerts: "提醒中心" +}[props.kind])); + +// API 调用逻辑修改(第 38-48 行): +async function load() { + loading.value = true; + try { + const params: Record = {}; + if (props.kind === "alerts") { + // 提醒列表保持不变 + } else { + if (search.value) params.search = search.value; + // 关键:manga 和 gallery 共用同一个后端接口,通过 content_type 区分 + if (props.kind === "manga") params.content_type = "manga"; + else if (props.kind === "gallery") params.content_type = "illust"; + } + const apiPath = props.kind === "manga" ? "/me/gallery" : `/me/${props.kind}`; + const response = await http.get(apiPath, { params }); + const data = apiData(response); + items.value = Array.isArray(data) ? data : data.items || []; + } finally { + loading.value = false; + } +} +``` + +**步骤 4:UserDetailPage 增加 manga 详情加载**(与方案二中详情页修复合并实施) + +在 `loadDetail()` 逻辑中增加: + +```typescript +if (props.kind === "manga") { + const response = await http.get(`/gallery/${props.id}`); + item.value = apiData(response); +} +``` + +> **设计说明:** 漫画和图片在后端共用 `gallery` API(同一张 `artworks` 表),只是 `content_type` 字段值不同(`"illust"` vs `"manga"`)。因此前端通过传入 `content_type=manga` 参数来筛选漫画类型,无需新增后端接口。这与移动端 APP 的处理方式一致。 + +### 3.4 预期效果 + +- Web 端侧边栏新增"漫画"入口,与"图片""小说"并列 +- 点击进入后展示所有 `content_type=manga` 的作品 +- 支持搜索漫画标题/作者/标签 +- 漫画详情页正常展示作品信息 +- "图片"板块默认展示 `content_type=illust` 的内容,与漫画分离 +- 如果以后需要更多内容类型(如 `ugoira` 动图),按相同模式扩展即可 + +--- + +## 四、Web 端列表加载性能问题 + +### 4.1 问题现象 + +Web 管理前端在浏览图片列表时,用户感知加载较慢: + +- 打开"图片"或"小说"列表页时,页面需要等待数秒才显示内容 +- 搜索时每次输入都触发后端请求,响应不够即时 +- 列表没有分页控件,所有数据一次性加载,数据量大时尤其慢 +- 缩略图使用后端 `/api/v1/files/{id}/thumbnail` 接口,未使用 CDN 或缓存策略 + +### 4.2 根因分析 + +#### 根因 1:列表页没有前端分页(🔴 最直接影响) + +**文件位置:** `sakura_admin_web/src/views/app/UserListPage.vue` 第 38-48 行 + +```typescript +async function load() { + loading.value = true; + try { + const params = props.kind === "alerts" ? {} : { search: search.value || undefined }; + const response = await http.get(`/me/${props.kind}`, { params }); + const data = apiData(response); + items.value = Array.isArray(data) ? data : data.items || []; + } finally { + loading.value = false; + } +} +``` + +**问题:** +- 请求**未传入 `page` 和 `limit` 参数** +- 后端 `/api/v1/me/gallery` 的默认 limit 是 30(`gallery.py` 第 31 行),但如果后端默认值被修改或数据量大,前端仍会慢 +- 更关键的是:**后端每次都会执行 `COUNT(*)` 查询**(`gallery.py` 第 100 行),数据量大时 `COUNT` 本身就是慢查询 +- UI 层面**没有分页控件**(上一页/下一页按钮),用户无法翻页浏览更多内容 +- 搜索框在每次 `@keyup.enter` 时触发请求,但没有防抖(debounce)机制 + +#### 根因 2:后端 COUNT 查询每次都是全表扫描(🔴 核心性能瓶颈) + +**文件位置:** `sakura_cloud/api/v1/endpoints/gallery.py` 第 43-100 行 + +```python +count_query = select(func.count()).select_from(Artwork).where(Artwork.user_id == current_user.id) +# ... 应用相同的 filters ... +total = int(db.scalar(count_query) or 0) +``` + +**问题:** +- 每次列表请求都执行 `COUNT(*)` 查询 +- `COUNT(*)` 在有大量数据时需要进行全表或索引扫描 +- 虽然 `gallery.py` 第 46-47 行使用了 `ILIKE`(`ilike(f"%{search_text}%")`),但带通配符的 `ILIKE` 无法利用 B-tree 索引,需要全表遍历每一行进行模式匹配 +- 搜索 + COUNT 组合是 O(n) 复杂度,数据量上万条后耗时明显增加 +- 即使是不搜索的普通列表请求,COUNT 也需要扫描用户的所有作品 + +#### 根因 3:列表和搜索没有防抖(🟡) + +**文件位置:** `sakura_admin_web/src/views/app/UserListPage.vue` 第 8 行 + +```vue + +``` + +**问题:** +- 用户按 Enter 才触发搜索,看似没问题 +- 但如果用户快速输入后多次按 Enter,或配合其他筛选条件多次修改,会发起多次后端请求 +- 没有防抖/节流机制,连续快速操作会产生冗余请求 + +#### 根因 4:缩略图加载策略未优化(🟡) + +**文件位置:** `sakura_admin_web/src/views/app/UserListPage.vue` — 模板中完全没有图片 + +当前列表页只显示作品的 `title` 和 `author` 文本,没有缩略图展示,所以缩略图加载不是 Web 端的瓶颈。但如果未来改进详情页展示缩略图(当前详情页是占位组件),需要注意: + +- 后端缩略图接口 `/api/v1/files/{id}/thumbnail` 是**同步读取 MinIO + 实时返回**,没有 CDN 缓存 +- 如果列表页展示 30 张缩略图,就是 30 次后端文件读取请求 + +#### 根因 5:Axios 超时 15 秒可能不够(🟢) + +**文件位置:** `sakura_admin_web/src/api/http.ts` 第 8 行 + +```typescript +export const http = axios.create({ + baseURL: API_BASE, + timeout: 15000, + withCredentials: false +}); +``` + +15 秒超时对于慢速服务器场景来说偏紧张,但影响相对较小。 + +### 4.3 修复方案 + +#### 方案 A:前端增加分页控件(🔴 最重要,直接解决加载慢) + +**修改位置:** + +| 文件 | 修改内容 | +|------|----------| +| `sakura_admin_web/src/views/app/UserListPage.vue` | 增加分页状态、分页 UI 控件、分页加载逻辑 | + +**具体做法:** + +1. 在 ` +``` + +--- + +## 14. 认证流程总结 + +### 14.1 Pixiv 认证(桌面端) + +``` +┌──────────────┐ ┌─────────────────┐ ┌──────────────────┐ +│ Cookie 导入 │────▶│ validate_cookie │────▶│ 访问设置页验证 │ +│ (cookies.txt) │ │ (AuthClient) │ │ 是否被重定向 │ +└──────────────┘ └─────────────────┘ └──────────────────┘ + │ │ + ┌───────┴───────┐ ┌──────┴──────┐ + │ 有效 → 保存 │ │ 失效 → 提示 │ + │ SessionStore │ │ 重新导入 │ + └───────────────┘ └─────────────┘ + +┌──────────────┐ ┌─────────────────┐ ┌──────────────────┐ +│ 密码登录 │────▶│ 提取 CSRF Token │────▶│ POST 登录 AJAX │ +│ │ │ (_extract_tt) │ │ + 提取 Cookie │ +└──────────────┘ └─────────────────┘ └──────────────────┘ +``` + +### 14.2 Sakura Cloud JWT 认证(全平台) + +``` +┌─────────────────────────────────────────────────────────────────┐ +│ Token 生命周期 │ +├─────────────────────────────────────────────────────────────────┤ +│ │ +│ 注册 登录 使用 │ +│ POST /register ──▶ POST /login ──▶ 返回 TokenPair │ +│ token_version++ access_token (24h) │ +│ refresh_token (30d) │ +│ │ │ +│ ┌───────────────────────────┘ │ +│ ▼ │ +│ 每次 API 请求 │ +│ Authorization: Bearer │ +│ │ │ +│ ▼ │ +│ get_current_user() │ +│ 1. decode_token() — 验证签名/过期 │ +│ 2. type == "access" — 验证 Token 类型 │ +│ 3. user exists — 验证用户存在 │ +│ 4. user.is_active — 验证账号启用 │ +│ 5. tv == user.token_version — 验证未吊销 │ +│ │ │ +│ ┌────────┴────────┐ │ +│ ▼ ▼ │ +│ 通过 401/过期 │ +│ │ │ +│ ▼ │ +│ POST /refresh │ +│ token_version++ │ +│ 返回新 TokenPair │ +│ │ +│ 登出 / 改密码 / 管理员操作 → token_version++ → 所有旧 Token 失效 │ +│ │ +└─────────────────────────────────────────────────────────────────┘ +``` + +### 14.3 桌面端 Cloud Token 管理流程 + +``` +GUI 启动 + │ + ▼ +load_saved_session() ← 从 pixiv_app_session.json 读取 + │ + ▼ +CloudSession().configure(...) ← 同步到全局单例 + │ + ▼ +CloudSession().add_listener(_on_cloud_token_refreshed) + │ + ▼ +每次创建 SakuraCloudClient 时: + │ + ├─ CloudSession.get_config() + │ └─ access_token 临近过期 (< 5min)? + │ └─ YES → try_refresh() + │ + ├─ API 请求返回 401? + │ └─ YES → SakuraCloudClient._try_refresh_token() + │ └─ 委托给 CloudSession.try_refresh() + │ ├─ 成功 → 更新内存 + 写盘 + 通知 GUI + │ └─ 失败 → 清空 token + 通知 GUI "已过期" +``` + +### 14.4 各平台 Token 存储方式 + +| 平台 | 存储方式 | 存储内容 | +|------|---------|---------| +| 桌面端 (Python) | `pixiv_app_session.json` | Pixiv Cookie + Cloud JWT Token Pair + 登录模式 | +| 云端 (FastAPI) | 无状态(JWT 自包含) | Token 内嵌 user_id, token_version, exp | +| 手机端 (Flutter) | `SharedPreferences` | `sakura.accessToken` + `sakura.username` | +| 管理后台 (Vue) | `sessionStorage` | `sakura_access_token` + `sakura_refresh_token` | + +### 14.5 Token 刷新机制对比 + +| 平台 | 刷新方式 | 触发条件 | +|------|---------|---------| +| 桌面端 CloudSession | 主动检查 JWT exp | 即将过期(< 5min)或 API 返回 401 | +| 桌面端 SakuraCloudClient | 被动响应 401 | API 返回 401 | +| 手机端 (Flutter) | **无自动刷新** | 启动时验证,失败则跳转登录页 | +| 管理后台 (Vue) | Axios 响应拦截器 | API 返回 401 + 有 refreshToken | +| 云端 API | POST /auth/refresh | 客户端主动调用 | diff --git a/docs/三项核心改进-分页规则_搜索健康分_书架保存解耦.md b/docs/三项核心改进-分页规则_搜索健康分_书架保存解耦.md new file mode 100644 index 0000000..4366981 --- /dev/null +++ b/docs/三项核心改进-分页规则_搜索健康分_书架保存解耦.md @@ -0,0 +1,656 @@ +# 两项核心改进 — TOC 分页规则 · 搜索健康分 + +> 日期:2026-06-22 +> 范围:仅移动端 Dart 侧(`sakura_mobile/lib/src/core/`) +> 涉及文件: +> +> - `legado_models.dart` — 数据模型 +> - `legado_rule_parser.dart` — 规则解析器 +> - `legado_engine.dart` — 搜索引擎入口 +> 原则:不改 Python 插件,不改 UI 层,只动 core 层。 + +--- + +## 一、改进 1:TOC 章节目录分页规则完整支持 + +### 1.1 问题 + +`fetchToc()` 只拉第一页目录。很多书源第一页只给 50~100 章,剩下的靠 **分页目录规则** 指向下一页。不支持分页规则 → 用户看到的目录永远不全。 + +### 1.2 兼容的字段名 + +书源 JSON 中,下一页 URL 的字段名可能是以下任意一种,按优先级遍历取第一个非空值: + +```dart +// legado_models.dart — BookSource / TocRule 中新增字段解析 + +static const List _tocNextPageCandidates = [ + 'tocNextPageRule', // Legado 标准名 + 'nextTocUrl', // 常见别名 + 'nextUrl', // 通用别名 + 'nextPage', // 页码式 + 'tocNextPage', // 变体 +]; + +String? get tocNextPageRule { + for (final name in _tocNextPageCandidates) { + final v = rawJson[name]; + if (v is String && v.trim().isNotEmpty) return v.trim(); + } + return null; +} +``` + +### 1.3 分页规则语法全集 + +| 规则类型 | 语法示例 | 说明 | +| --------- | ----------------------------------- | ------------------ | +| JS 表达式 | `nextUrl` | 执行 JS 获取下一页 URL | +| JS 缩写 | `@js:nextUrl` | 同上,简写形式 | +| 模板拼接 | `https://host/list?page={{page}}` | `{{page}}` 从 1 自增 | +| 偏移模板 | `{{page+1}}` / `{{page-1}}` | 页号偏移 | +| CSS 选择器 | `text.下一页@href` | 从当前页 HTML 提取下一页链接 | +| class 选择器 | `class.next@href` | 同上 | +| id 选择器 | `id.pagenext@href` | 同上 | +| 属性选择器 | `@href` | 取当前元素 href | +| XPath | `//a[contains(text(),'下一页')]/@href` | XPath 提取 | +| 字符串切片 | `!0:-1` | 对当前 URL 切片(去掉末尾字符) | +| offset 翻页 | `offset=50` | 按 offset 递增翻页 | +| 纯文本 URL | `https://...` | 直接跳到指定 URL | +| 相对路径 | `/list/2.html` | 基于当前 URL 拼接 | + +### 1.4 实现 — `TocPaginationResolver` + +```dart +// 放在 legado_engine.dart 或新建 legado_toc_pagination.dart + +class TocPaginationResolver { + final LegadoRuleExecutor _ruleExecutor; + final HttpClient _client; + + TocPaginationResolver(this._ruleExecutor, this._client); + + /// 拉取全部分页章节,合并去重。 + Future> fetchAllChapters({ + required String firstPageUrl, + required String? tocNextPageRule, + required String tocRule, + required Map? headers, + int maxPages = 20, + }) async { + final all = []; + final seenPageUrls = {}; + final seenChapterUrls = {}; + + String? url = firstPageUrl; + int page = 0; + + while (url != null && page < maxPages) { + if (!seenPageUrls.add(url)) break; // 死循环保护 + + final html = await _client.get(url, headers: headers); + final batch = _ruleExecutor.selectList(html, url, rule: tocRule); + + int added = 0; + for (final ch in batch) { + if (seenChapterUrls.add(ch.url)) { + all.add(ch); + added++; + } + } + + if (added == 0) break; // 本页无新章节,终止翻页 + + url = _resolveNextPage( + currentUrl: url, + currentHtml: html, + rule: tocNextPageRule, + pageIndex: page, + ); + page++; + } + + return all; + } + + // ---------- 下一页 URL 解析 ---------- + + String? _resolveNextPage({ + required String currentUrl, + required String currentHtml, + required String? rule, + required int pageIndex, + }) { + if (rule == null || rule.trim().isEmpty) return null; + final r = rule.trim(); + + // 1) JS: ... 或 @js:... + if (r.startsWith('') || r.startsWith('@js:')) { + final code = r + .replaceFirst(RegExp(r'^'), '') + .replaceFirst(RegExp(r'$'), '') + .replaceFirst(RegExp(r'^@js:'), ''); + return _ruleExecutor.evalJs(code, context: { + 'result': currentHtml, + 'baseUrl': currentUrl, + 'page': pageIndex, + }); + } + + // 2) 切片: !start:end + if (r.startsWith('!')) { + return _applySlice(currentUrl, r.substring(1)); + } + + // 3) CSS 选择器 / XPath + if (_isSelector(r)) { + return _ruleExecutor.select(currentHtml, currentUrl, rule: r); + } + + // 4) 模板替换 {{page}} / {{page+1}} / {{page-1}} + if (r.contains('{{page')) { + return _renderPageTemplate(r, pageIndex); + } + + // 5) offset / start 参数 + if (RegExp(r'[?&](offset|start)=').hasMatch(r)) { + return _renderOffsetTemplate(r, pageIndex); + } + + // 6) 绝对 URL + if (r.startsWith('http://') || r.startsWith('https://')) { + return _renderPageTemplate(r, pageIndex); + } + + // 7) 相对路径 + return Uri.parse(currentUrl).resolve(r).toString(); + } + + // ---------- 规则判定 ---------- + + bool _isSelector(String s) { + return s.startsWith('text.') || + s.startsWith('class.') || + s.startsWith('id.') || + s.startsWith('@') || + s.startsWith('//'); // XPath + } + + // ---------- 模板渲染 ---------- + + String _renderPageTemplate(String tpl, int page) { + return tpl + .replaceAll('{{page+1}}', '${page + 1}') + .replaceAll('{{page-1}}', '${page - 1}') + .replaceAll('{{page}}', '$page'); + } + + String _renderOffsetTemplate(String tpl, int page) { + return tpl + .replaceAllMapped( + RegExp(r'offset=(\d+)'), + (m) => 'offset=${int.parse(m.group(1)!) * page}', + ) + .replaceAllMapped( + RegExp(r'start=(\d+)'), + (m) => 'start=${int.parse(m.group(1)!) * page}', + ); + } + + String? _applySlice(String url, String expr) { + final parts = expr.split(':'); + if (parts.length != 2) return null; + final a = int.tryParse(parts[0]) ?? 0; + final b = int.tryParse(parts[1]); + final start = a < 0 ? url.length + a : a; + if (b != null) { + final end = b < 0 ? url.length + b : b; + return url.substring(start, end); + } + return url.substring(start); + } +} +``` + +### 1.5 集成到 `LegadoEngine.fetchToc` + +```dart +// legado_engine.dart — 修改 fetchToc + +Future> fetchToc(BookSource source, String tocUrl) async { + final rule = source.tocRule; + if (rule == null || rule.isEmpty) return []; + + final resolver = TocPaginationResolver(_ruleExecutor, _client); + return resolver.fetchAllChapters( + firstPageUrl: tocUrl, + tocNextPageRule: source.tocNextPageRule, // ← 新增 + tocRule: rule, + headers: _buildHeaders(source), + maxPages: 20, + ); +} +``` + +### 1.6 死循环防护(三层) + +1. **`maxPages = 20`** — 单书最多翻 20 页目录 +2. **`seenPageUrls`** — 同一 URL 不重复请求 +3. **`added == 0` 终止** — 当前页没有新增章节则停止 + +--- + +## 二、改进 2:搜索源健康分 + 短超时 + 分阶段返回 + +### 2.1 问题 + +当前 `search()` 对所有启用源并发请求,每个源 timeout 20~30 秒。10 个源一批,坏源多时整批拖到 20+ 秒才出结果。 + +### 2.2 源健康分模型 + +```dart +// legado_models.dart — 新增 SourceHealth + +class SourceHealth { + final String sourceId; + + int successCount = 0; + int failCount = 0; + int timeoutCount = 0; + int totalLatencyMs = 0; + DateTime? lastSuccessAt; + DateTime? lastFailAt; + int _consecutiveFails = 0; + + SourceHealth({required this.sourceId}); + + // ---- 判定 ---- + + /// 连续失败 ≥ 5 次 → 临时禁用 + bool get isUsable { + if (_consecutiveFails >= 5) return false; + if (failCount >= 5 && successCount == 0) return false; + return true; + } + + int get consecutiveFails => _consecutiveFails; + + /// 健康分 0~100 + double get score { + final total = successCount + failCount + timeoutCount; + if (total == 0) return 50.0; // 新源默认 50 + + double s = 0.0; + + // 成功率 60% + s += 60.0 * (successCount / total); + + // 平均延迟 25% + if (successCount > 0 && totalLatencyMs > 0) { + final avg = totalLatencyMs / successCount; + if (avg < 500) { + s += 25.0; + } else if (avg < 2000) { + s += 25.0 * (1.0 - (avg - 500) / 1500); + } + } + + // 新鲜度 15%(24h 内有成功记录) + if (lastSuccessAt != null) { + final h = DateTime.now().difference(lastSuccessAt!).inHours; + if (h < 1) { + s += 15.0; + } else if (h < 24) { + s += 15.0 * (1.0 - h / 24); + } + } + + return s.clamp(0.0, 100.0); + } + + // ---- 记录 ---- + + void recordSuccess(int latencyMs) { + successCount++; + totalLatencyMs += latencyMs; + lastSuccessAt = DateTime.now(); + _consecutiveFails = 0; + } + + void recordFailure() { + failCount++; + lastFailAt = DateTime.now(); + _consecutiveFails++; + } + + void recordTimeout() { + timeoutCount++; + lastFailAt = DateTime.now(); + _consecutiveFails++; + } + + void resetConsecutiveFails() => _consecutiveFails = 0; + + // ---- 序列化(持久化到 SharedPreferences)---- + + Map toJson() => { + 'sourceId': sourceId, + 'successCount': successCount, + 'failCount': failCount, + 'timeoutCount': timeoutCount, + 'totalLatencyMs': totalLatencyMs, + 'lastSuccessAt': lastSuccessAt?.toIso8601String(), + 'lastFailAt': lastFailAt?.toIso8601String(), + 'consecutiveFails': _consecutiveFails, + }; + + factory SourceHealth.fromJson(Map json) { + final h = SourceHealth(sourceId: json['sourceId']); + h.successCount = json['successCount'] ?? 0; + h.failCount = json['failCount'] ?? 0; + h.timeoutCount = json['timeoutCount'] ?? 0; + h.totalLatencyMs = json['totalLatencyMs'] ?? 0; + h.lastSuccessAt = json['lastSuccessAt'] != null + ? DateTime.tryParse(json['lastSuccessAt']) + : null; + h.lastFailAt = json['lastFailAt'] != null + ? DateTime.tryParse(json['lastFailAt']) + : null; + h._consecutiveFails = json['consecutiveFails'] ?? 0; + return h; + } +} +``` + +### 2.3 健康分持久化 + +```dart +// legado_engine.dart — SourceHealthStore + +class SourceHealthStore { + static const _key = 'source_health_v1'; + final Map _data = {}; + + Future load() async { + final prefs = await SharedPreferences.getInstance(); + final raw = prefs.getStringList(_key); + if (raw == null) return; + for (final s in raw) { + try { + final h = SourceHealth.fromJson(jsonDecode(s)); + _data[h.sourceId] = h; + } catch (_) {} + } + } + + Future save() async { + final prefs = await SharedPreferences.getInstance(); + final raw = _data.values.map((h) => jsonEncode(h.toJson())).toList(); + await prefs.setStringList(_key, raw); + } + + bool isUsable(String id) => _data[id]?.isUsable ?? true; + double score(String id) => _data[id]?.score ?? 50.0; + + void _ensure(String id) { + _data.putIfAbsent(id, () => SourceHealth(sourceId: id)); + } + + void recordSuccess(String id, int latencyMs) { + _ensure(id); + _data[id]!.recordSuccess(latencyMs); + } + + void recordFailure(String id) { + _ensure(id); + _data[id]!.recordFailure(); + } + + void recordTimeout(String id) { + _ensure(id); + _data[id]!.recordTimeout(); + } + + /// 用户手动重置所有源健康状态 + void resetAll() { + for (final h in _data.values) { + h.resetConsecutiveFails(); + } + } +} +``` + +### 2.4 分阶段搜索调度器 + +```dart +// legado_engine.dart — SearchScheduler + +class SearchScheduler { + static const fastTimeout = Duration(seconds: 5); + static const slowTimeout = Duration(seconds: 8); + static const maxConcurrent = 6; + + final SourceHealthStore _health; + final LegadoEngine _engine; + + SearchScheduler(this._health, this._engine); + + /// 返回 Stream,UI 层逐阶段渲染结果 + Stream search(String keyword, List allSources) async* { + // 过滤 + 按健康分排序 + final usable = allSources + .where((s) => _health.isUsable(s.id)) + .toList() + ..sort((a, b) => _health.score(b.id).compareTo(_health.score(a.id))); + + if (usable.isEmpty) { + yield SearchPhase(name: 'error', message: '没有可用的搜索源'); + return; + } + + // 分三档 + final fast = usable.where((s) => _health.score(s.id) >= 70).toList(); + final normal = usable.where((s) { + final sc = _health.score(s.id); + return sc >= 40 && sc < 70; + }).toList(); + final slow = usable.where((s) => _health.score(s.id) < 40).toList(); + + final results = []; + final doneIds = {}; + final seen = {}; + + // ---- 阶段 1:快速源,超时 5s ---- + yield SearchPhase(name: 'searching', message: '正在搜索 ${usable.length} 个源...'); + final r1 = await _batch(fast, keyword, timeout: fastTimeout, onDone: doneIds.add); + _merge(results, r1, seen); + yield SearchPhase( + name: 'partial', + items: List.of(results), + doneCount: doneIds.length, + totalCount: usable.length, + ); + + // ---- 阶段 2:普通源,超时 8s ---- + final r2 = await _batch(normal, keyword, timeout: slowTimeout, onDone: doneIds.add); + _merge(results, r2, seen); + yield SearchPhase( + name: 'partial', + items: List.of(results), + doneCount: doneIds.length, + totalCount: usable.length, + ); + + // ---- 阶段 3:慢源后台继续,不阻塞 ---- + if (slow.isNotEmpty) { + // 放入后台队列,不 await + _batch(slow, keyword, timeout: slowTimeout, onDone: doneIds.add) + .then((r3) { + _merge(results, r3, seen); + // TODO: EventBus 通知 UI 追加 + }); + yield SearchPhase( + name: 'complete_with_pending', + items: List.of(results), + doneCount: doneIds.length, + totalCount: usable.length, + message: '${slow.length} 个慢源将在后台继续搜索', + ); + } else { + yield SearchPhase( + name: 'complete', + items: List.of(results), + doneCount: doneIds.length, + totalCount: usable.length, + ); + } + } + + // ---- 内部分批并发 ---- + + Future> _batch( + List sources, + String keyword, { + required Duration timeout, + required void Function(String) onDone, + }) async { + final out = []; + for (var i = 0; i < sources.length; i += maxConcurrent) { + final chunk = sources.skip(i).take(maxConcurrent).toList(); + final futures = chunk.map((s) => _searchOne(s, keyword, timeout)); + final results = await Future.wait(futures); + for (var j = 0; j < results.length; j++) { + if (results[j] != null) out.addAll(results[j]!); + onDone(chunk[j].id); + } + } + return out; + } + + Future?> _searchOne( + BookSource source, String keyword, Duration timeout, + ) async { + final sw = Stopwatch()..start(); + try { + final items = await _engine.search(source, keyword).timeout(timeout); + sw.stop(); + _health.recordSuccess(source.id, sw.elapsedMilliseconds); + return items; + } on TimeoutException { + _health.recordTimeout(source.id); + return null; + } catch (_) { + _health.recordFailure(source.id); + return null; + } + } + + void _merge(List all, List incoming, Set seen) { + for (final item in incoming) { + if (seen.add(item.id)) all.add(item); + } + } +} + +// ---- 阶段结果 ---- + +class SearchPhase { + final String name; // searching | partial | complete | complete_with_pending | error + final List items; + final int doneCount; + final int totalCount; + final String? message; + + SearchPhase({ + required this.name, + this.items = const [], + this.doneCount = 0, + this.totalCount = 0, + this.message, + }); +} +``` + +### 2.5 UI 增量渲染时序 + +``` +t=0s → 显示 "正在搜索 N 个源..." +t=0~5s → 第 1 批结果到达 → 首屏渲染(最快的 5~10 条) +t=5~8s → 第 2 批结果到达 → 列表追加 +t=8s → 停止等待 → 提示"慢源已跳过,后台补充中" +t=8~15s → 第 3 批结果到达 → 静默追加到列表末尾 +``` + +### 2.6 健康分自动衰减 + +每次 `save()` 时做一次衰减: + +```dart +// SourceHealthStore.save() 追加衰减逻辑 +Future save() async { + // 超过 7 天无任何记录 → 衰减 successCount 和 failCount 各减半 + for (final h in _data.values) { + final latest = h.lastSuccessAt ?? h.lastFailAt; + if (latest != null && + DateTime.now().difference(latest).inDays >= 7) { + h.successCount = (h.successCount * 0.5).round(); + h.failCount = (h.failCount * 0.5).round(); + h.timeoutCount = (h.timeoutCount * 0.5).round(); + } + } + // ... 写入 SharedPreferences +} +``` + +--- + +## 三、两改进的协作关系 + +``` +用户搜索一本书 + │ + ├─ SearchScheduler.search(keyword) + │ ├─ 按健康分排序源 + │ ├─ 快速源先搜(5s 超时) + │ ├─ 普通源补搜(8s 超时) + │ └─ 返回结果列表 + │ + ├─ 用户点击某条结果 → fetchBookInfo → fetchToc + │ └─ TocPaginationResolver 拉全章节(支持分页规则) + │ + └─ 用户阅读某章 → fetchChapterContent(按需加载) +``` + +改进 1 保证"搜到书后章节不断尾",改进 2 保证"搜索本身不卡"。 + +--- + +## 四、实施顺序 + +``` +第 1 步:改进 2 的 SourceHealth + SourceHealthStore + → 只加数据模型 + 持久化,不影响现有搜索流程 + → 可以先跑几天积累健康数据 + +第 2 步:改进 2 的 SearchScheduler + 短超时 + → 替换现有 search() 的并发逻辑 + → UI 层适配 Stream + +第 3 步:改进 1 的 TocPaginationResolver + → 依赖 RuleParser 链式语法的基础支持 + → 见《Legado 规则引擎重构方案》第 1~3 步 + +第 4 步:联调 + 回归 +``` + +--- + +## 五、风险控制 + +| 风险 | 应对 | +| -------------------- | --------------------------------------------- | +| TOC 分页死循环 | `maxPages=20` + `seenUrls` 去重 + `added==0` 终止 | +| 健康分误杀快源 | 只降权不禁用;连续失败 ≥5 才标记不可用 + 手动可重置 | +| 短超时漏结果 | 慢源后台继续,event bus 异步追加,不丢数据 | +| 新源全部 50 分 | 第一轮搜索后就有真实分,冷启动无影响 | +| SharedPreferences 过大 | 只存 String list,每个源约 200 字节,100 个源 ≈ 20KB | diff --git a/docs/书源在线搜索-问题分析与改进方案.md b/docs/书源在线搜索-问题分析与改进方案.md new file mode 100644 index 0000000..8ca69f8 --- /dev/null +++ b/docs/书源在线搜索-问题分析与改进方案.md @@ -0,0 +1,1224 @@ +# 书源在线搜索 — 问题分析与改进方案 + +> 分析日期:2026-06-18 +> 涉及代码: +> - `plugins/biquge/plugin.py` — 桌面端笔趣阁插件(Python) +> - `pixiv_app/gui/app.py` — GUI 调用层(Python) +> - `sakura_mobile/lib/src/core/legado_engine.dart` — 移动端搜索引擎(Dart) +> - `sakura_mobile/lib/src/core/legado_rule_parser.dart` — 规则解析器(Dart) +> - `sakura_mobile/lib/src/core/legado_models.dart` — 数据模型(Dart) + +--- + +## 一、架构总览 + +``` +用户输入 -> GUI(app.py) -> PluginManager -> BiqugePlugin.resolve_target() + │ + ┌───────────────────────┼───────────────────────┐ + ▼ ▼ ▼ + _search_books_api() _reader_sort_books() _resolve_book_id() + (apibi.cc API 搜索) (榜单/分类列表) (书籍详情+章节) + │ │ + ▼ ▼ + 搜索结果列表 ──> BookSelectionDialog ──> 下载所有章节 + (书源在线搜索) (GUI 弹窗选书) +``` + +--- + +## 二、桌面端(Python)问题分析 + +### 问题 1:API 搜索静默失败(最严重) + +**位置**:`plugins/biquge/plugin.py:387-395` + +```python +def _search_books_api(self, keyword: str, *, limit: int) -> list[BookCandidate]: + try: + payload = self._api_get_json( + "/api/search", + params={"q": keyword}, + referer=f"{READER_BASE_URL}/#/search?q={quote(keyword)}", + ) + except Exception: + return [] # ← 静默吞掉所有异常,无法定位问题 + ... +``` + +**原因**:`except Exception: return []` 吞掉了所有异常,包括: +- 网络不通/DNS 解析失败 +- API 返回非 JSON 内容 +- HTTP 错误状态码 +- Token 加密失败(间接影响) + +**后果**:搜索默默返回空列表,用户看到"没有搜索到相关书籍",但完全不知道是网络问题还是 API 挂了还是参数不对。 + +--- + +### 问题 2:API Token 加密密钥硬编码 + +**位置**:`plugins/biquge/plugin.py:61` + +```python +TOKEN_SECRET = os.environ.get("SAKURA_BIQUGE_TOKEN_SECRET", "book@token.html") +TOKEN_MD5 = hashlib.md5(TOKEN_SECRET.encode("utf-8")).hexdigest() +TOKEN_IV = TOKEN_MD5[:16].encode("utf-8") +TOKEN_KEY = TOKEN_MD5[16:].encode("utf-8") +``` + +**原因**:`_encode_token()` 使用 AES-128-CBC 加密请求参数,密钥/IV 派生自硬编码字符串。如果 `apibi.cc` 服务端使用了不同的 secret,所有需要 token 的 API(book 详情、booklist 目录、chapter 章节内容)都会失败。 + +**影响范围**: +- `/api/search` — 不需要 token,不受影响 +- `/api/book` — 需要 token,会失败 +- `/api/booklist` — 需要 token,会失败 +- `/api/chapter` — 需要 token,会失败 + +--- + +### 问题 3:章节 ID 错位问题(关键 Bug) + +**位置**:`plugins/biquge/plugin.py:599-603` + +```python +def _chapter_resources_from_book_data(self, book_data): + chapter_list = list(book_data.get("_chapter_list") or []) + for order, raw_title in enumerate(chapter_list, start=1): + chapter_id = str(order) # ← 使用 1,2,3... 而不是 API 返回的真实 ID + ... +``` + +**原因**:API `/api/booklist` 返回的 `list` 可能是一个包含对象数组(每个对象有 `id` 和 `name`),而不是纯字符串数组。当前代码将其视为字符串列表,并用序号 `1,2,3...` 作为 `chapter_id` 去请求章节内容。 + +如果 API 的章节 ID 不是从 1 开始的连续数字(例如有些书源的章节 ID 是随机数或哈希值),那么 `_chapter_data_from_api(book_id, "1")` 会请求到错误的章节或失败。 + +**原始 API 返回可能是**: +```json +{ + "list": [ + {"id": 58291, "name": "第一章 楔子"}, + {"id": 58292, "name": "第二章 测试"} + ] +} +``` + +**但代码把它当成**: +```json +{ + "list": ["第一章 楔子", "第二章 测试"] +} +``` + +--- + +### 问题 4:Legacy HTML 回退可能失效 + +**位置**:`plugins/biquge/plugin.py:368-385` + +```python +def _search_books(self, keyword: str, *, limit: int) -> list[BookCandidate]: + books = self._search_books_api(keyword, limit=limit) + if books: + return books + # Legacy HTML fallback + attempts = [ + {"url": f"{LEGACY_BASE_URL}/s", "params": {"q": keyword}}, + {"url": f"{LEGACY_BASE_URL}/search", "params": {"q": keyword}}, + ] +``` + +**原因**: +- `LEGACY_BASE_URL = "https://www.bqgui.cc"` 这个域名可能已经失效或改版 +- HTML 结构可能已变化,导致 `_BOOK_LINK_RE` 正则无法匹配 +- 没有处理 GBK/GB2312 编码(很多中文小说站使用) + +--- + +### 问题 5:缺少 HTTP 重试机制 + +**位置**:`plugins/biquge/plugin.py:1096-1117` + +```python +def _api_get_json(self, endpoint, *, params=None, payload=None, ...): + url = host.rstrip("/") + endpoint + response = self.session.get(url, params=request_params, timeout=25, ...) + response.raise_for_status() + return response.json() +``` + +**原因**:API 调用没有重试逻辑。网络波动、临时限流(429)、服务器短暂不可用(502/503)都会直接导致失败。 + +--- + +### 问题 6:`requests.Session` 线程不安全 + +**位置**:`plugins/biquge/plugin.py:136-137` + +```python +def __init__(self) -> None: + self.session = requests.Session() +``` + +**原因**:`requests.Session` 不是线程安全的。当 GUI 多线程下载章节时(`app.py` 中的 `run_download_biquge_resources_thread` 虽然是单线程循环下载,但 `sync_plugin_paths` 里可能涉及其他线程操作),共享的 session 可能导致连接池混乱或 Cookie 串扰。 + +--- + +### 问题 7:缺少编码检测 + +**位置**:`plugins/biquge/plugin.py:361-366` + +```python +def _fetch_html(self, url, *, params=None): + response = self.session.get(url, params=params, timeout=25) + response.raise_for_status() + if not response.encoding or response.encoding.lower() in {"iso-8859-1", "ascii"}: + response.encoding = response.apparent_encoding or "utf-8" + return response.text +``` + +**原因**:只处理了 `iso-8859-1` 和 `ascii`,没有显式处理 `gbk`/`gb2312`/`gb18030`。`apparent_encoding` 的检测也可能不准确。 + +--- + +### 问题 8:`resolve_target` 路由歧义 + +**位置**:`plugins/biquge/plugin.py:159-218` + +当用户输入一个纯数字(如 `"12345"`)时: +1. `_normalize_target("12345")` → 返回 `""`(不是 URL) +2. `raw.isdigit()` → `True` +3. 直接调用 `_resolve_book_id("12345")` 将其当作书籍 ID + +但如果用户想搜索书名为 "12345" 的书,这个逻辑就错了——它直接跳到书籍详情而不是搜索。 + +--- + +## 三、移动端(Dart/Flutter)问题分析 + +### 问题 9:并发搜索无去重 + +**位置**:`sakura_mobile/lib/src/core/legado_engine.dart:91-111` + +```dart +Future> searchAll( + List enabledSources, + String keyword, {int page = 1,} +) async { + final batch = enabledSources.take(maxConcurrency).toList(); + final responses = await Future.wait( + batch.map((source) => search(source, keyword, page: page)...), + ); + return responses.expand((items) => items).toList(); +} +``` + +**原因**:如果 10 个不同书源(如多个笔趣阁镜像站)返回了同一本书,结果中会出现 10 条重复。没有基于书名+作者的跨源去重逻辑。 + +--- + +### 问题 10:GBK 编码依赖平台通道 + +**位置**:`sakura_mobile/lib/src/core/legado_engine.dart:329-361` + +```dart +Future _encodeKeyword(LegadoSource source, String keyword) async { + ... + if (encoding == 'gbk' || encoding == 'gb2312' || ...) { + final bytes = gbk.encode(keyword); // ← 纯 Dart 实现,OK + return bytes.map(...).join(); + } + // 其他编码需要平台通道 + final encoded = await _encodingChannel.invokeMethod( + 'encodeQuery', {'text': keyword, 'charset': encoding}, + ); + ... +} +``` + +**原因**:GBK 有 `gbk_codec` 包支持,但其他非 UTF-8 编码(如 Big5、EUC-JP、Shift-JIS)需要 Android/iOS 原生平台通道。在桌面端(Windows/macOS/Linux)会直接抛出 `MissingPluginException`。 + +--- + +### 问题 11:JSON 解析不够健壮 + +**位置**:`sakura_mobile/lib/src/core/legado_rule_parser.dart:85-93` + +```dart +Object? _jsonBody() { + if (_decodedJson != null) return _decodedJson; + try { + _decodedJson = jsonDecode(body); + } on FormatException { + _decodedJson = null; + } + return _decodedJson; +} +``` + +**原因**:只检查了 `FormatException`,但有些 API 返回的是带 BOM 头的 JSON、JSONP 回调包裹的数据、或 HTML 中内嵌的 JSON。这些情况都无法正确解析。 + +--- + +### 问题 12:回退搜索质量差 + +**位置**:`sakura_mobile/lib/src/core/legado_engine.dart:236-276` + +```dart +List _fallbackSearchResults(...) { + ... + for (final match in links) { + if (normalizedKeyword.isNotEmpty && + !title.toLowerCase().contains(normalizedKeyword)) { + continue; // ← 要求标题包含搜索词,太严格 + } + ... + } + return results; +} +``` + +**原因**:回退逻辑要求 HTML 中每个 `` 标签的文字必须包含搜索关键词。如果小说站点用表格或列表展示结果(没有用 `` 标签包裹标题),就会漏掉结果。 + +--- + +## 四、改进方案 + +### 方案 A:桌面端 Python —— 增强健壮性和可调试性 + +#### A1. 添加结构化日志,不再静默吞异常 + +```python +import logging + +logger = logging.getLogger(__name__) + +def _search_books_api(self, keyword: str, *, limit: int) -> list[BookCandidate]: + try: + payload = self._api_get_json( + "/api/search", + params={"q": keyword}, + referer=f"{READER_BASE_URL}/#/search?q={quote(keyword)}", + ) + except requests.ConnectionError as exc: + logger.warning("API search connection failed for '%s': %s", keyword, exc) + return [] + except requests.Timeout as exc: + logger.warning("API search timeout for '%s': %s", keyword, exc) + return [] + except requests.HTTPError as exc: + logger.warning( + "API search HTTP %s for '%s': %s", + exc.response.status_code if exc.response else "?", + keyword, + exc, + ) + return [] + except Exception as exc: + logger.error("API search unexpected error for '%s': %s", keyword, exc, exc_info=True) + return [] + ... +``` + +#### A2. 支持 API Token Secret 可配置 + +```python +TOKEN_SECRET = os.environ.get("SAKURA_BIQUGE_TOKEN_SECRET", "book@token.html") + +# 在 BiqugePlugin 中增加配置项,允许从 GUI/配置文件传入 +def get_config_schema(self) -> dict[str, Any]: + return { + "type": "object", + "properties": { + "token_secret": { + "type": "string", + "title": "API Token 密钥", + "description": "apibi.cc 的 AES 加密密钥,默认使用内置值", + }, + "api_base_url": { + "type": "string", + "title": "API 基础地址", + "description": "apibi.cc 或自定义 API 地址", + }, + }, + } + +def load_config(self) -> dict[str, Any]: + return { + "token_secret": os.environ.get("SAKURA_BIQUGE_TOKEN_SECRET", "book@token.html"), + "api_base_url": API_BASE_URL, + } +``` + +#### A3. 修复章节 ID 错位 + +从 API 返回中提取真实的章节 ID: + +```python +def _chapter_resources_from_book_data(self, book_data: dict[str, Any]) -> list[Resource]: + book_id = str(book_data.get("id") or book_data.get("dirid") or "").strip() + if not book_id: + raise PluginParseError("书籍目录缺少书籍 ID。") + + book_title = _normalize_space(str(book_data.get("title") or "")) + author = _normalize_space(str(book_data.get("author") or "")) + description = _normalize_space(str(book_data.get("intro") or "")) + chapter_list = list(book_data.get("_chapter_list") or []) + resources: list[Resource] = [] + + for order, raw_item in enumerate(chapter_list, start=1): + # === 改进:支持对象和字符串两种格式 === + if isinstance(raw_item, dict): + chapter_title = _normalize_space(str(raw_item.get("name") or raw_item.get("title") or "")) + chapter_id = str(raw_item.get("id") or raw_item.get("chapterid") or order) + else: + chapter_title = _normalize_space(str(raw_item or "")) + chapter_id = str(order) + + if not chapter_title: + continue + + source_id = f"{book_id}:{chapter_id}" + resources.append( + Resource( + id=source_id, + url=self._reader_chapter_url(book_id, chapter_id), + title=chapter_title, + author=author, + author_id="", + files=[], + metadata={ + "kind": "novel_chapter", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "biquge", + "source_id": source_id, + "book_id": book_id, + "book_title": book_title, + "series_id": book_id, + "series_title": book_title, + "chapter_title": chapter_title, + "chapter_order": self._extract_maybe_numeric_order(chapter_title, fallback=order), + "description": description, + "last_update": str(book_data.get("lastupdate") or ""), + "book_status": str(book_data.get("full") or ""), + }, + ) + ) + + return resources +``` + +#### A4. 添加 API 重试机制 + +```python +def _api_get_json( + self, + endpoint: str, + *, + params: dict[str, Any] | None = None, + payload: dict[str, Any] | None = None, + host: str = API_BASE_URL, + referer: str = "", + max_retries: int = 3, +) -> Any: + request_params = dict(params or {}) + if payload is not None: + request_params["token"] = self._encode_token(payload) + + url = host.rstrip("/") + endpoint + last_exc: Exception | None = None + + for attempt in range(1, max_retries + 1): + try: + response = self.session.get( + url, + params=request_params, + timeout=25, + headers=self._api_headers(referer), + ) + response.raise_for_status() + return response.json() + except requests.HTTPError as exc: + status = exc.response.status_code if exc.response else 0 + if status == 429 and attempt < max_retries: + retry_after = int(exc.response.headers.get("Retry-After", min(3 * attempt, 15))) + logger.warning("API rate limited, retrying in %ds (attempt %d/%d)", retry_after, attempt, max_retries) + time.sleep(retry_after) + continue + if status in (502, 503, 504) and attempt < max_retries: + logger.warning("API server error %d, retrying (attempt %d/%d)", status, attempt, max_retries) + time.sleep(min(2 ** attempt, 10)) + continue + last_exc = exc + break + except (requests.ConnectionError, requests.Timeout) as exc: + if attempt < max_retries: + logger.warning("API network error, retrying (attempt %d/%d): %s", attempt, max_retries, exc) + time.sleep(min(2 ** attempt, 10)) + continue + last_exc = exc + break + + raise PluginParseError(f"API 请求失败 ({url}): {last_exc}") from last_exc +``` + +#### A5. 增强 GBK 编码支持 + +```python +def _fetch_html(self, url: str, *, params: dict[str, str] | None = None) -> str: + response = self.session.get(url, params=params, timeout=25) + response.raise_for_status() + + # === 改进:支持 GBK/GB2312/GB18030 === + if not response.encoding or response.encoding.lower() in {"iso-8859-1", "ascii"}: + # 先尝试从 HTML meta 标签检测 + meta_match = re.search( + r']+charset=["\']?([a-zA-Z0-9_-]+)', + response.text[:2048] if response.text else "", + re.I, + ) + if meta_match: + response.encoding = meta_match.group(1) + else: + response.encoding = response.apparent_encoding or "utf-8" + + # 如果还是乱码,尝试 GBK 解码 + try: + text = response.text + if len(text) > 0 and '�' in text[:min(500, len(text))]: + # 检测到 Unicode 替换字符,说明编码错了 + response.encoding = "gb18030" + return response.text + except Exception: + pass + + return response.text +``` + +#### A6. 线程安全的 Session 管理 + +```python +import threading + +class BiqugePlugin(BasePlugin): + def __init__(self) -> None: + # 使用线程局部存储,每个线程独立的 Session + self._local = threading.local() + ... + + @property + def session(self) -> requests.Session: + if not hasattr(self._local, "session"): + self._local.session = requests.Session() + self._local.session.headers.update(self.get_headers()) + return self._local.session +``` + +#### A7. 改进 `resolve_target` 路由歧义 + +```python +def resolve_target(self, target: str, *, limit: int = 20) -> dict[str, Any]: + raw = target.strip() + if not raw: + raise PluginParseError("请输入书名、作者、书籍链接或章节链接。") + + normalized = self._normalize_target(raw) + if normalized: + # ... URL 路由逻辑不变 + ... + + # === 改进:纯数字优先搜索而不是直接当 book_id === + if raw.isdigit(): + # 先尝试搜索,如果搜索无结果再当做 book_id + books = self._search_books(raw, limit=limit) + if books: + return { + "mode": "books", + "title": f"搜索结果:{raw}", + "books": [candidate.__dict__ for candidate in books], + } + # 搜索无结果,尝试作为 book_id + try: + return self._resolve_book_id(raw) + except PluginParseError: + raise PluginParseError(f"没有搜索到与“{raw}”相关的书籍,也无法作为书籍 ID 解析。") + + books = self._search_books(raw, limit=limit) + if not books: + raise PluginParseError(f"没有搜索到与“{raw}”相关的书籍。") + return { + "mode": "books", + "title": f"搜索结果:{raw}", + "books": [candidate.__dict__ for candidate in books], + } +``` + +--- + +### 方案 B:移动端 Dart —— 增强兼容性 + +#### B1. 搜索结果跨源去重 + +```dart +Future> searchAll( + List enabledSources, + String keyword, {int page = 1,} +) async { + final batch = enabledSources.take(maxConcurrency).toList(); + final responses = await Future.wait( + batch.map((source) => search(source, keyword, page: page) + .timeout(perSourceTimeout) + .catchError((error) => [ + UnifiedSearchResult.error( + sourceId: source.id, + sourceName: source.name, + errorMessage: _shortError(error), + ), + ]), + ), + ); + + // === 改进:按书名+作者去重 === + final seen = {}; + final deduped = []; + for (final item in responses.expand((items) => items)) { + if (item.isError) { + deduped.add(item); + continue; + } + final fingerprint = '${item.title}||${item.author ?? ''}'.toLowerCase(); + if (seen.add(fingerprint)) { + deduped.add(item); + } + } + return deduped; +} +``` + +#### B2. 增强 JSON 兼容性(BOM / JSONP) + +```dart +Object? _jsonBody() { + if (_decodedJson != null) return _decodedJson; + try { + // 去掉 BOM 头 + var text = body; + if (text.codeUnitAt(0) == 0xFEFF) { + text = text.substring(1); + } + // 尝试 JSONP 格式: callback({...}) + final jsonp = RegExp(r'^[a-zA-Z_$][\w$]*\((.*)\)[;]?\s*$', dotAll: true) + .firstMatch(text.trim()); + if (jsonp != null) { + text = jsonp.group(1)!; + } + _decodedJson = jsonDecode(text); + } on FormatException { + _decodedJson = null; + } + return _decodedJson; +} +``` + +#### B3. GBK 回退方案(桌面端兼容) + +```dart +Future _encodeKeyword(LegadoSource source, String keyword) async { + final encoding = + (source.urlEncoding ?? source.charset ?? 'utf-8').trim().toLowerCase(); + if (encoding.isEmpty || encoding == 'utf-8' || encoding == 'utf8' || encoding == 'unicode') { + return Uri.encodeQueryComponent(keyword); + } + if (encoding == 'gbk' || encoding == 'gb2312' || encoding == 'gb18030' || encoding == 'gbk-0') { + final bytes = gbk.encode(keyword); + return bytes.map((b) => '%${b.toRadixString(16).padLeft(2, '0').toUpperCase()}').join(); + } + try { + final encoded = await _encodingChannel.invokeMethod( + 'encodeQuery', {'text': keyword, 'charset': encoding}, + ); + if (encoded != null && encoded.isNotEmpty) return encoded; + } on MissingPluginException { + // === 改进:桌面端回退到 UTF-8 编码并提示 === + logger.warning('编码 $encoding 在桌面端不可用,已回退到 UTF-8'); + return Uri.encodeQueryComponent(keyword); + } on PlatformException catch (error) { + logger.warning('编码 $encoding 失败: ${error.message},已回退到 UTF-8'); + return Uri.encodeQueryComponent(keyword); + } + return Uri.encodeQueryComponent(keyword); +} +``` + +#### B4. 改进回退搜索 + +```dart +List _fallbackSearchResults( + String body, LegadoSource source, String baseUrl, String keyword, +) { + final normalizedKeyword = keyword.trim().toLowerCase(); + final seen = {}; + final results = []; + + // === 改进 1:先尝试匹配所有链接(不要求标题包含关键词) === + final allLinks = RegExp( + "]*\\bhref=[\"']([^\"']+)[\"'][^>]*>(.*?)", + caseSensitive: false, dotAll: true, + ).allMatches(body); + + for (final match in allLinks) { + if (results.length >= 30) break; + final href = match.group(1)?.trim() ?? ''; + if (href.isEmpty || href.startsWith('#') || + href.toLowerCase().startsWith('javascript:')) continue; + final title = _htmlToText(match.group(2) ?? ''); + if (title.isEmpty) continue; + final detailUrl = _resolveUrl(baseUrl, href); + if (detailUrl.isEmpty || !seen.add(detailUrl)) continue; + + // 放宽匹配:有部分关键词匹配即可(而不是要求完全包含) + if (normalizedKeyword.isNotEmpty) { + final titleLower = title.toLowerCase(); + final chars = normalizedKeyword.split(''); + final matchCount = chars.where((c) => titleLower.contains(c)).length; + if (matchCount < chars.length * 0.5) continue; // 至少 50% 字符匹配 + } + + results.add(UnifiedSearchResult( + sourceId: source.id, + sourceName: source.name, + title: title, + detailUrl: detailUrl, + )); + } + + // === 改进 2:也尝试从非链接文本中提取 === + if (results.isEmpty) { + final textBlocks = RegExp(r']*>(.*?)', caseSensitive: false, dotAll: true) + .allMatches(body); + for (final match in textBlocks) { + if (results.length >= 10) break; + final text = _htmlToText(match.group(1) ?? ''); + if (text.length > 2 && text.length < 100 && text.contains(normalizedKeyword)) { + results.add(UnifiedSearchResult( + sourceId: source.id, + sourceName: source.name, + title: text, + detailUrl: baseUrl, + )); + } + } + } + + return results; +} +``` + +--- + +## 五、完整改进后的核心代码段 + +### 5.1 改进后的 `biquge/plugin.py` 关键方法(完整版) + +```python +# ============================================================ +# 以下为改进后的关键代码段,可直接替换原文件对应部分 +# ============================================================ + +import logging +import threading +import time + +logger = logging.getLogger(__name__) + +# ==================== 改进 1:可配置的 Token Secret ==================== + +class BiqugePlugin(BasePlugin): + name = "笔趣阁" + domain = "www.bqg551.cc" + version = "3.1.0" + + def __init__(self) -> None: + # 线程安全的 Session 管理 + self._local = threading.local() + self._playwright = None + self._browser = None + self._page = None + self._browser_lock = threading.Lock() + # 可配置项 + self._config_token_secret: str = "" + self._config_api_base: str = "" + atexit.register(self._close_browser) + + @property + def session(self) -> requests.Session: + """线程局部 Session,保证线程安全""" + if not hasattr(self._local, "session"): + self._local.session = requests.Session() + self._local.session.headers.update(self.get_headers()) + return self._local.session + + # ==================== 改进 2:可配置的 API 参数 ==================== + + @property + def api_base_url(self) -> str: + return self._config_api_base or API_BASE_URL + + def _resolve_token_secret(self) -> str: + """解析当前生效的 Token Secret""" + if self._config_token_secret: + return self._config_token_secret + return os.environ.get("SAKURA_BIQUGE_TOKEN_SECRET", "book@token.html") + + def _encode_token(self, payload: dict[str, Any]) -> str: + if not CRYPTOGRAPHY_AVAILABLE or padding is None or Cipher is None: + raise PluginParseError("当前环境缺少 cryptography,无法生成 apibi 访问 token。") + + secret = self._resolve_token_secret() + token_md5 = hashlib.md5(secret.encode("utf-8")).hexdigest() + token_iv = token_md5[:16].encode("utf-8") + token_key = token_md5[16:].encode("utf-8") + + raw = json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8") + padder = padding.PKCS7(128).padder() + padded = padder.update(raw) + padder.finalize() + encryptor = Cipher(algorithms.AES(token_key), modes.CBC(token_iv)).encryptor() + encrypted = encryptor.update(padded) + encryptor.finalize() + return base64.b64encode(encrypted).decode("ascii") + + # ==================== 改进 3:可配置的配置项 ==================== + + def get_config_schema(self) -> dict[str, Any]: + return { + "type": "object", + "properties": { + "token_secret": { + "type": "string", + "title": "API Token 密钥", + "description": "apibi.cc 的 AES 加密密钥(可选,默认使用内置值)", + }, + "api_base_url": { + "type": "string", + "title": "API 基础地址", + "description": f"默认 {API_BASE_URL},可改为自定义 API 地址", + }, + }, + } + + def load_config(self) -> dict[str, Any]: + return { + "token_secret": self._config_token_secret, + "api_base_url": self._config_api_base, + } + + def save_config(self, data: dict[str, Any]) -> None: + self._config_token_secret = str(data.get("token_secret") or "").strip() + self._config_api_base = str(data.get("api_base_url") or "").strip() + + # ==================== 改进 4:带重试和日志的 API 请求 ==================== + + def _api_get_json( + self, + endpoint: str, + *, + params: dict[str, Any] | None = None, + payload: dict[str, Any] | None = None, + host: str | None = None, + referer: str = "", + max_retries: int = 3, + ) -> Any: + """带重试机制的 API JSON 请求""" + request_params = dict(params or {}) + if payload is not None: + request_params["token"] = self._encode_token(payload) + + resolved_host = host or self.api_base_url + url = resolved_host.rstrip("/") + endpoint + last_exc: Exception | None = None + + for attempt in range(1, max_retries + 1): + try: + response = self.session.get( + url, + params=request_params, + timeout=25, + headers=self._api_headers(referer), + ) + response.raise_for_status() + return response.json() + except requests.HTTPError as exc: + status = exc.response.status_code if exc.response else 0 + if status == 429 and attempt < max_retries: + retry_after = int( + exc.response.headers.get("Retry-After", min(3 * attempt, 15)) + ) if exc.response else min(3 * attempt, 15) + logger.warning( + "API %s rate limited, retrying in %ds (attempt %d/%d)", + endpoint, retry_after, attempt, max_retries, + ) + time.sleep(retry_after) + continue + if status in (502, 503, 504) and attempt < max_retries: + wait = min(2 ** attempt, 10) + logger.warning( + "API %s server error %d, retrying in %ds (attempt %d/%d)", + endpoint, status, wait, attempt, max_retries, + ) + time.sleep(wait) + continue + last_exc = exc + break + except (requests.ConnectionError, requests.Timeout) as exc: + if attempt < max_retries: + wait = min(2 ** attempt, 10) + logger.warning( + "API %s network error, retrying in %ds (attempt %d/%d): %s", + endpoint, wait, attempt, max_retries, exc, + ) + time.sleep(wait) + continue + last_exc = exc + break + + raise PluginParseError(f"API 请求失败 ({url}): {last_exc}") from last_exc + + # ==================== 改进 5:带日志的搜索 ==================== + + def _search_books_api(self, keyword: str, *, limit: int) -> list[BookCandidate]: + """搜索书籍(API 方式),失败时记录日志""" + try: + payload = self._api_get_json( + "/api/search", + params={"q": keyword}, + referer=f"{READER_BASE_URL}/#/search?q={quote(keyword)}", + ) + except requests.ConnectionError as exc: + logger.warning("API search connection failed for '%s': %s", keyword, exc) + return [] + except requests.Timeout as exc: + logger.warning("API search timeout for '%s': %s", keyword, exc) + return [] + except requests.HTTPError as exc: + status = exc.response.status_code if exc.response else "?" + logger.warning("API search HTTP %s for '%s'", status, keyword) + return [] + except PluginParseError as exc: + logger.warning("API search failed for '%s': %s", keyword, exc) + return [] + except Exception as exc: + logger.error( + "API search unexpected error for '%s': %s", keyword, exc, exc_info=True, + ) + return [] + + rows = list(payload.get("data") or []) if isinstance(payload, dict) else [] + ranked: list[tuple[int, BookCandidate]] = [] + seen: set[str] = set() + keyword_parts = self._keyword_parts(keyword) + normalized_keyword = _normalize_space(keyword).lower() + + for row in rows: + if not isinstance(row, dict): + continue + book_id = str(row.get("id") or row.get("dirid") or "").strip() + title = _normalize_space(str(row.get("title") or "")) + if not book_id or not title: + continue + href = self._reader_book_url(book_id) + if href in seen: + continue + seen.add(href) + author = _normalize_space(str(row.get("author") or "")) + latest = _normalize_space(str(row.get("lastchapter") or "")) + candidate = BookCandidate( + title=title, url=href, author=author, latest_chapter=latest, + ) + score = self._candidate_score(candidate, keyword_parts) + title_lower = title.lower() + author_lower = author.lower() + if normalized_keyword and title_lower == normalized_keyword: + score += 1000 + elif normalized_keyword and title_lower.startswith(normalized_keyword): + score += 200 + elif normalized_keyword and normalized_keyword in title_lower: + score += 120 + elif normalized_keyword and normalized_keyword in author_lower: + score += 60 + ranked.append((score, candidate)) + + ranked.sort(key=lambda item: (-item[0], len(item[1].title), item[1].title)) + result = [item[1] for item in ranked[:limit]] + logger.info("API search '%s' returned %d results", keyword, len(result)) + return result + + # ==================== 改进 6:修复章节 ID 错位 ==================== + + def _chapter_resources_from_book_data( + self, book_data: dict[str, Any], + ) -> list[Resource]: + book_id = str(book_data.get("id") or book_data.get("dirid") or "").strip() + if not book_id: + raise PluginParseError("书籍目录缺少书籍 ID。") + + book_title = _normalize_space(str(book_data.get("title") or "")) + author = _normalize_space(str(book_data.get("author") or "")) + description = _normalize_space(str(book_data.get("intro") or "")) + chapter_list = list(book_data.get("_chapter_list") or []) + resources: list[Resource] = [] + + for order, raw_item in enumerate(chapter_list, start=1): + # === 改进:支持 dict 和 str 两种格式 === + if isinstance(raw_item, dict): + chapter_title = _normalize_space( + str(raw_item.get("name") or raw_item.get("title") or "") + ) + chapter_id = str( + raw_item.get("id") or raw_item.get("chapterid") or order + ) + else: + chapter_title = _normalize_space(str(raw_item or "")) + chapter_id = str(order) + + if not chapter_title: + continue + + source_id = f"{book_id}:{chapter_id}" + resources.append( + Resource( + id=source_id, + url=self._reader_chapter_url(book_id, chapter_id), + title=chapter_title, + author=author, + author_id="", + files=[], + metadata={ + "kind": "novel_chapter", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "biquge", + "source_id": source_id, + "book_id": book_id, + "book_title": book_title, + "series_id": book_id, + "series_title": book_title, + "chapter_title": chapter_title, + "chapter_order": self._extract_maybe_numeric_order( + chapter_title, fallback=order, + ), + "description": description, + "last_update": str(book_data.get("lastupdate") or ""), + "book_status": str(book_data.get("full") or ""), + }, + ) + ) + + return resources + + # ==================== 改进 7:增强编码检测 ==================== + + def _fetch_html( + self, url: str, *, params: dict[str, str] | None = None, + ) -> str: + response = self.session.get(url, params=params, timeout=25) + response.raise_for_status() + + if not response.encoding or response.encoding.lower() in {"iso-8859-1", "ascii"}: + # 先尝试从 HTML meta 标签检测 + try: + preview = response.content[:4096] + meta_match = re.search( + rb']+charset=["\']?([a-zA-Z0-9_-]+)', + preview, re.I, + ) + if meta_match: + detected = meta_match.group(1).decode("ascii", errors="replace") + response.encoding = detected + return response.text + except Exception: + pass + # 使用 apparent_encoding 兜底 + response.encoding = response.apparent_encoding or "utf-8" + + # 检测是否有大量乱码(Unicode 替换字符) + try: + text = response.text + sample = text[:min(1000, len(text))] + if sample.count('�') > len(sample) * 0.05: + # 超过 5% 是乱码,尝试 GB18030 + response.encoding = "gb18030" + return response.text + except Exception: + pass + + return response.text + + # ==================== 改进 8:resolve_target 优化 ==================== + + def resolve_target(self, target: str, *, limit: int = 20) -> dict[str, Any]: + raw = target.strip() + if not raw: + raise PluginParseError("请输入书名、作者、书籍链接或章节链接。") + + normalized = self._normalize_target(raw) + if normalized: + route = self._parse_reader_route(normalized) + if route["kind"] == "search": + keyword = route["keyword"] + books = self._search_books(keyword, limit=limit) + if not books: + raise PluginParseError(f"没有搜索到与"{keyword}"相关的书籍。") + return { + "mode": "books", + "title": f"搜索结果:{keyword}", + "books": [candidate.__dict__ for candidate in books], + } + if route["kind"] == "sort": + books = self._reader_sort_books(str(route["slug"]), limit=limit) + if not books: + raise PluginParseError("该列表页没有解析到书籍。") + return { + "mode": "books", + "title": f"列表结果:{route['slug']}", + "books": [candidate.__dict__ for candidate in books], + } + if route["kind"] == "index": + books = self._reader_index_books(limit=limit) + if not books: + raise PluginParseError("首页推荐列表为空。") + return { + "mode": "books", + "title": "首页推荐", + "books": [candidate.__dict__ for candidate in books], + } + + ids = self._extract_ids_from_url(normalized) + if ids.book_id and ids.chapter_id: + chapter = self._chapter_resource_from_ids( + ids.book_id, ids.chapter_id, source_url=normalized, + ) + return { + "mode": "chapters", + "book_title": str(chapter.metadata.get("book_title") or ""), + "resources": [chapter], + } + if ids.book_id: + return self._resolve_book_id(ids.book_id) + return self._resolve_listing_url(normalized, limit=limit) + + # === 改进:纯数字也先搜索再回退 === + if raw.isdigit(): + books = self._search_books(raw, limit=limit) + if books: + return { + "mode": "books", + "title": f"搜索结果:{raw}", + "books": [candidate.__dict__ for candidate in books], + } + try: + return self._resolve_book_id(raw) + except PluginParseError: + raise PluginParseError( + f"没有搜索到与"{raw}"相关的书籍,也无法作为书籍 ID 解析。" + ) + + books = self._search_books(raw, limit=limit) + if not books: + raise PluginParseError(f"没有搜索到与"{raw}"相关的书籍。") + return { + "mode": "books", + "title": f"搜索结果:{raw}", + "books": [candidate.__dict__ for candidate in books], + } +``` + +### 5.2 改进后的移动端关键代码 + +#### `legado_engine.dart` — searchAll 去重 + +```dart +Future> searchAll( + List enabledSources, + String keyword, {int page = 1,} +) async { + final batch = enabledSources.take(maxConcurrency).toList(); + final responses = await Future.wait( + batch.map((source) => search(source, keyword, page: page) + .timeout(perSourceTimeout) + .catchError((error) => [ + UnifiedSearchResult.error( + sourceId: source.id, + sourceName: source.name, + errorMessage: _shortError(error), + ), + ]), + ), + ); + + // 跨源去重:按书名+作者指纹 + final seen = {}; + final deduped = []; + for (final item in responses.expand((items) => items)) { + if (item.isError) { + deduped.add(item); + continue; + } + final fingerprint = '${item.title}|${item.author ?? ''}'.toLowerCase(); + if (seen.add(fingerprint)) { + deduped.add(item); + } + } + return deduped; +} +``` + +#### `legado_rule_parser.dart` — BOM/JSONP 兼容 + +```dart +Object? _jsonBody() { + if (_decodedJson != null) return _decodedJson; + try { + var text = body; + // 去掉 UTF-8 BOM + if (text.isNotEmpty && text.codeUnitAt(0) == 0xFEFF) { + text = text.substring(1); + } + // 尝试 JSONP 格式 + final jsonp = RegExp( + r'^[a-zA-Z_$][\w$]*\s*\(\s*(.*)\s*\)\s*;?\s*$', + dotAll: true, + ).firstMatch(text.trim()); + if (jsonp != null) { + text = jsonp.group(1)!; + } + _decodedJson = jsonDecode(text); + } on FormatException { + _decodedJson = null; + } + return _decodedJson; +} +``` + +--- + +## 六、总结 + +| # | 问题 | 严重度 | 修复方式 | +|---|------|--------|---------| +| 1 | API 搜索静默吞异常 | 🔴 高 | 分类捕获异常 + 日志记录 | +| 2 | Token Secret 硬编码 | 🔴 高 | 环境变量 + 配置文件 | +| 3 | 章节 ID 错位 | 🔴 高 | 支持 dict 格式提取真实 ID | +| 4 | Legacy HTML 失效 | 🟡 中 | 增加更多回退域名 + GBK | +| 5 | 缺少重试机制 | 🟡 中 | 指数退避重试 | +| 6 | Session 线程不安全 | 🟡 中 | `threading.local()` | +| 7 | 编码检测不完善 | 🟡 中 | HTML meta 检测 + 乱码检测 | +| 8 | 纯数字路由歧义 | 🟢 低 | 先搜索再回退 book_id | +| 9 | 跨源结果重复 | 🟡 中 | 书名+作者指纹去重 | +| 10 | GBK 桌面端不支持 | 🟡 中 | 非致命回退到 UTF-8 | +| 11 | JSONP/BOM 不兼容 | 🟢 低 | 预处理 body 文本 | +| 12 | 回退搜索太严格 | 🟢 低 | 放宽关键词匹配阈值 | + +**最关键的三项修复**(建议优先实施): +1. **章节 ID 错位**(问题 3)— 直接导致下载失败,必须立即修复 +2. **API 搜索静默失败**(问题 1)— 加了日志就能快速定位根因 +3. **API 重试机制**(问题 5)— 解决大部分"偶发失败"问题 diff --git a/docs/产品文档-樱云下载器.md b/docs/产品文档-樱云下载器.md new file mode 100644 index 0000000..d2d5614 --- /dev/null +++ b/docs/产品文档-樱云下载器.md @@ -0,0 +1,1187 @@ +# Sakura Downloader(樱云下载器)产品文档 + +> 版本:2.0.0 | 更新日期:2026-06-23 +> 作者:kunkunxiaomao | 博客:http://blog.kunkunxiaomao.top + +--- + +## 目录 + +1. [产品介绍](#1-产品介绍) +2. [系统架构总览](#2-系统架构总览) +3. [桌面端功能详解](#3-桌面端功能详解) +4. [Sakura Cloud 云端平台](#4-sakura-cloud-云端平台) +5. [Android 移动端功能详解](#5-android-移动端功能详解) +6. [插件系统](#6-插件系统) +7. [本地图库与缩略图墙](#7-本地图库与缩略图墙) +8. [云同步机制](#8-云同步机制) +9. [每日提醒功能](#9-每日提醒功能) +10. [代理与性能设置](#10-代理与性能设置) +11. [安装与运行](#11-安装与运行) +12. [打包与发布](#12-打包与发布) +13. [完整使用指南](#13-完整使用指南) +14. [常见问题与排错](#14-常见问题与排错) +15. [安全与风险控制](#15-安全与风险控制) +16. [开发指南](#16-开发指南) +17. [更新日志](#17-更新日志) + +--- + +## 1. 产品介绍 + +### 1.1 产品概述 + +**Sakura Downloader(樱云下载器)** 是一套面向个人的**多平台媒体采集、本地图库管理与跨设备云同步系统**。它由三个核心端组成,提供从内容发现、采集下载、本地整理到跨设备阅读的完整工作流。 + +### 1.2 产品定位 + +| 端 | 角色 | 运行环境 | 技术栈 | +|----|------|----------|--------| +| **Windows 桌面端** | 采集与整理中枢 | Windows 7+ (x64) | Python 3.8+ / CustomTkinter / SQLite / Playwright | +| **Sakura Cloud** | 数据与账号中心 | Linux 服务器 | FastAPI / PostgreSQL / JWT / Redis | +| **Android 客户端** | 跨设备阅读与提醒 | Android 8.0+ | Flutter / Riverpod / Dio | + +**一句话总结:** +- 桌面端 = **采集和整理中枢** +- 云端 = **数据和账号中心** +- 安卓端 = **个人阅读器** + +### 1.3 核心能力一览 + +| 能力 | 说明 | +|------|------| +| 🎨 **多平台采集** | 支持 Pixiv、X(Twitter)、小红书、笔趣阁小说、长佩文学、AO3 等平台的图片、漫画、小说、视频下载 | +| 📚 **本地图库管理** | SQLite 数据库管理作品元数据、作者、标签、收藏、关注作者 | +| 🖼️ **缩略图墙** | 本地 WebUI,浏览器端浏览图库、搜索标签、筛选作者、查看收藏 | +| ☁️ **云同步** | 下载内容自动上传至 Sakura Cloud,手机端实时访问 | +| 📱 **跨设备阅读** | Android 端浏览图片、漫画、小说,支持阅读进度记录 | +| 🔌 **插件系统** | 开放 Python 插件接口,支持自定义站点扩展 | +| 🔔 **每日提醒** | 云端汇总关注作者更新,手机端「发现」页查看 | +| 🔍 **OCR 文字识别** | 基于 PaddleOCR 的本地图片文字提取 | +| 🌐 **代理池** | 支持手动代理列表、Quake 网络空间搜索引擎获取代理 | +| 📡 **分布式爬虫** | 基于 Redis + aiohttp 的 Master-Worker 分布式架构(可选) | + +### 1.4 支持的平台与模式 + +#### Pixiv +| 模式 | 说明 | +|------|------| +| 作者全部作品 | 输入作者 ID 或作品链接反查作者并批量下载 | +| 单个作品 | 下载指定作品(含多页)的原图 | +| 漫画 | 完整下载漫画作品的所有页面 | +| 文章/小说 | 下载小说内容并保存为 TXT 文件 | +| 关键词搜索 | 按关键词搜索作品并批量下载 | + +#### X(Twitter) +| 模式 | 说明 | +|------|------| +| 作者媒体 | 输入用户名,抓取 /media 页的图片和视频 | +| 单个推文 | 解析单条推文中的媒体文件 | +| 搜索词媒体 | 搜索并抓取媒体内容 | + +#### 小红书 +| 模式 | 说明 | +|------|------| +| 搜索关键词 | 搜索笔记并下载图片 | +| 单篇笔记 | 解析单篇笔记的图片 | +| 链接解析 | 支持 xiaohongshu.com 和 xhslink.com 分享链接 | + +#### 小说平台 +| 平台 | 说明 | +|------|------| +| 笔趣阁小说 | 书名/作者/链接搜索,支持榜单页和列表页解析 | +| 长佩文学 | 书名/作者/ID/链接搜索,全章批量下载 | + +#### 插件自动识别 +输入链接、ID 或关键词,程序自动匹配已导入插件处理。 + +--- + +## 2. 系统架构总览 + +### 2.1 整体架构图 + +``` +┌──────────────────────────────────────────────────────────┐ +│ Sakura Downloader 系统 │ +├───────────────────────┬──────────────────────────────────┤ +│ Windows 桌面端 │ Sakura Cloud 云端 │ +│ ┌─────────────────┐ │ ┌────────────────────────────┐ │ +│ │ CustomTkinter │ │ │ FastAPI REST API │ │ +│ │ GUI 界面 │ │ │ /api/v1/auth/* │ │ +│ ├─────────────────┤ │ │ /api/v1/gallery/* │ │ +│ │ 插件管理器 │ │ │ /api/v1/upload/* │ │ +│ │ (PluginManager) │ │ │ /api/v1/novels/* │ │ +│ ├─────────────────┤ │ │ /api/v1/follows/* │ │ +│ │ 下载引擎 │ │ │ /api/v1/sync/* │ │ +│ │ (Downloader) │ │ │ /api/v1/push/* │ │ +│ ├─────────────────┤ │ ├────────────────────────────┤ │ +│ │ 本地图库 (SQLite) │ │ │ PostgreSQL 数据库 │ │ +│ ├─────────────────┤ │ │ ├── users │ │ +│ │ 云同步客户端 │──┼──│ ├── artworks │ │ +│ │ (CloudSync) │ │ │ ├── novels │ │ +│ ├─────────────────┤ │ │ ├── files │ │ +│ │ 缩略图墙 WebUI │ │ │ ├── follows │ │ +│ │ (GalleryAPI) │ │ │ ├── reminders │ │ +│ ├─────────────────┤ │ │ └── push_devices │ │ +│ │ 代理池 │ │ └────────────────────────────┘ │ +│ │ (ProxyPool) │ │ │ +│ └─────────────────┘ │ │ +├───────────────────────┼────────────────────────────────────┤ +│ Android 移动端 │ +│ ┌─────────────────────────────────────────────────────────┐ │ +│ │ Flutter App │ │ +│ │ ├── 发现页 (Feed) ├── 书架页 (书架) │ │ +│ │ ├── 图片页 (Gallery) ├── 漫画页 (漫画) │ │ +│ │ ├── 我的页 (Profile) ├── 提醒中心 │ │ +│ │ └── 小说阅读器 (Reader) │ │ +│ └─────────────────────────────────────────────────────────┘ │ +└──────────────────────────────────────────────────────────────┘ +``` + +### 2.2 目录结构 + +```text +Pixiv/ +├── main.py # 桌面端入口 +├── requirements.txt # Python 依赖 +├── pixiv_app/ # 桌面端核心模块 +│ ├── gui/ +│ │ ├── app.py # 主 GUI 窗口 (SakuraDownloaderGUI) +│ │ ├── plugin_panel.py # 插件管理面板 +│ │ └── proxy_dialog.py # 代理设置对话框 +│ ├── core/ +│ │ ├── auth.py # Pixiv 认证 (Cookie/密码登录) +│ │ ├── downloader.py # 下载引擎核心 +│ │ ├── library.py # 本地 SQLite 图库 +│ │ ├── cloud_sync.py # 云同步客户端 +│ │ ├── gallery_service.py # 缩略图墙 Web 服务 +│ │ ├── orchestrator.py # 任务编排器 +│ │ ├── proxy_pool.py # 代理池管理 +│ │ ├── ocr.py # OCR 文字识别 +│ │ ├── fingerprint.py # 浏览器指纹模拟 +│ │ ├── cookie_import.py # Cookie 导入与解析 +│ │ ├── x_media_service.py # X 媒体同步服务 +│ │ ├── origin_follow_sync.py # 原始关注同步 +│ │ ├── paths.py # 路径管理 +│ │ └── plugin/ # 插件基础设施 +│ │ ├── base.py # 插件基类 (BasePlugin) +│ │ ├── manager.py # 插件管理器 +│ │ ├── generator.py # 插件模板生成器 +│ │ └── market.py # 插件市场 +│ ├── runtime/ # 任务运行时 +│ │ ├── executor.py # 任务执行器 +│ │ ├── worker_loop.py # 工作循环 +│ │ └── enqueue.py # 任务队列 +│ ├── services/ # 服务层 +│ │ ├── gallery_api.py # 图库 API 服务 +│ │ ├── run_gallery_api.py # 图库服务启动器 +│ │ ├── x_login_flow.py # X 登录流程 +│ │ └── x_auth_manager.py # X 认证管理 +│ └── tasks/ # 任务系统 +│ ├── models.py # 任务模型 +│ ├── parser.py # 批量解析器 +│ ├── queue.py # 任务队列 +│ └── watermark.py # 水印/增量同步 +├── plugins/ # 内置站点插件 +│ ├── pixiv/plugin.py # Pixiv 插件 +│ ├── x/ # X (Twitter) 插件 +│ │ ├── plugin.py # X 主插件 +│ │ ├── playwright_client.py # Playwright 客户端 +│ │ ├── x_urls.py # URL 解析 +│ │ ├── x_login.py # X 登录 +│ │ ├── selectors.py # 页面选择器 +│ │ ├── browser_opts.py # 浏览器选项 +│ │ └── setup_plugin.py # 安装脚本 +│ ├── xiaohongshu/plugin.py # 小红书插件 +│ ├── ao3/plugin.py # AO3 插件 +│ ├── biquge/plugin.py # 笔趣阁插件 +│ └── changpei/plugin.py # 长佩文学插件 +├── sakura_cloud/ # 云端 API +│ ├── main.py # FastAPI 入口 +│ ├── api/ +│ │ ├── deps.py # 依赖注入 (JWT 验证) +│ │ ├── middleware.py # 中间件 +│ │ └── v1/ +│ │ ├── router.py # 路由注册 +│ │ └── endpoints/ # API 端点 +│ │ ├── auth.py # 认证 (登录/注册/刷新Token) +│ │ ├── gallery.py # 图库查询 +│ │ ├── upload.py # 文件上传 +│ │ ├── novels.py # 小说管理 +│ │ ├── follows.py # 关注管理 +│ │ ├── sync.py # 数据同步 +│ │ ├── push.py # 推送服务 +│ │ ├── users.py # 用户管理 +│ │ ├── me.py # 当前用户信息 +│ │ ├── admin.py # 管理后台 +│ │ └── health.py # 健康检查 +│ ├── models/ # SQLAlchemy 模型 +│ │ ├── user.py # 用户模型 +│ │ ├── artwork.py # 作品模型 +│ │ ├── novel.py # 小说模型 +│ │ ├── file.py # 文件模型 +│ │ ├── follow.py # 关注模型 +│ │ ├── push.py # 推送模型 +│ │ ├── ocr.py # OCR 模型 +│ │ ├── admin.py # 管理模型 +│ │ └── feed_cache.py # Feed 缓存 +│ ├── schemas/ # Pydantic 数据模型 +│ ├── services/ # 业务服务层 +│ │ ├── storage.py # 存储服务 +│ │ ├── reminders.py # 提醒服务 +│ │ ├── push.py # 推送服务 +│ │ ├── ocr.py # OCR 服务 +│ │ ├── audit.py # 审计服务 +│ │ └── thumbnails.py # 缩略图服务 +│ ├── core/ +│ │ ├── config.py # 配置管理 +│ │ ├── security.py # 安全模块 (JWT/密码哈希) +│ │ ├── safety.py # 内容安全 +│ │ └── logging.py # 日志配置 +│ ├── db/ # 数据库连接 +│ ├── migrations/ # Alembic 数据库迁移 +│ ├── scripts/ # 运维脚本 +│ └── docker-compose.yml # Docker 部署配置 +├── sakura_mobile/ # Flutter 安卓端 +│ └── lib/ +│ └── src/ +│ ├── app.dart # 应用入口 +│ ├── main.dart # 主函数 +│ ├── pages/ # 页面 +│ │ ├── login_page.dart # 登录页 +│ │ ├── home_page.dart # 主页 +│ │ ├── explore_page.dart # 探索页 +│ │ ├── novel_detail_page.dart # 小说详情 +│ │ ├── novel_reader_page.dart # 本地小说阅读器 +│ │ ├── online_novel_reader_page.dart # 在线小说阅读器 +│ │ ├── online_manga_reader_page.dart # 在线漫画阅读器 +│ │ ├── artwork_detail_page.dart # 插画详情 +│ │ ├── favorites_page.dart # 收藏页 +│ │ ├── reminder_center_page.dart # 提醒中心 +│ │ ├── source_manager_page.dart # 书源管理 +│ │ └── tabs/ # 底部标签页 +│ │ ├── feed_tab.dart # 发现 +│ │ ├── gallery_tab.dart # 图片 +│ │ ├── novels_tab.dart # 书架 +│ │ └── profile_tab.dart # 我的 +│ ├── core/ # 核心逻辑 +│ │ ├── api_client.dart # API 客户端 +│ │ ├── session.dart # 会话管理 +│ │ ├── providers.dart # Riverpod 状态 +│ │ ├── online_shelf.dart # 在线书架 +│ │ ├── online_cache.dart # 在线缓存 +│ │ ├── online_reading_history.dart # 阅读历史 +│ │ ├── manga_collections.dart # 漫画分组 +│ │ ├── push_notifications.dart # 推送通知 +│ │ ├── source_manager.dart # 书源管理器 +│ │ ├── legado_engine.dart # Legado 规则引擎 +│ │ ├── legado_rule_executor.dart # 规则执行器 +│ │ ├── legado_rule_parser.dart # 规则解析器 +│ │ ├── legado_models.dart # 规则数据模型 +│ │ ├── unified_search.dart # 统一搜索 +│ │ └── search_scheduler.dart # 搜索调度 +│ ├── models/ # 数据模型 +│ └── widgets/ # UI 组件 +├── distributed_crawler/ # 分布式爬虫 (可选) +├── docs/ # 项目文档 +├── scripts/ # 构建与运维脚本 +│ ├── build_desktop_exe.bat # 打包桌面 EXE +│ ├── build_mobile_android.bat # 打包 Android APK +│ ├── copy_release_artifacts.bat # 复制成品 +│ └── build_all.bat # 一键构建 +├── releases/ # 可分发成品 +│ ├── sakuradownloader.exe # Windows 桌面端 (~68 MB) +│ └── app-release.apk # Android 客户端 (~54 MB) +└── tools/ # 辅助工具 +``` + +--- + +## 3. 桌面端功能详解 + +### 3.1 GUI 界面总览 + +桌面端采用 **CustomTkinter** 构建,主窗口 "Sakura 下载器" 尺寸 1160×860,薄荷蓝配色,分为以下功能区域: + +- **登录方式卡片**:管理 Pixiv 认证状态 +- **抓取目标卡片**:选择平台、模式和目标输入 +- **性能设置卡片**:线程、延迟、代理配置 +- **云同步卡片**:云端账号管理与同步开关 +- **使用说明卡片**:操作提示 +- **任务概览卡片**:统计数据和缩略图墙入口 +- **实时进度卡片**:进度条和阶段文本 +- **抓取日志区域**:实时日志输出 +- **底部操作栏**:开始/停止按钮 + +### 3.2 登录认证 + +#### Cookie 登录(推荐) + +支持导入 `Get cookies.txt` 等浏览器插件导出的 Cookie 文件: +1. 浏览器登录 Pixiv → 用插件导出 txt 或 json 文件 +2. 桌面端点击 「导入 cookies.txt / json」 +3. 点击 「检测会话」 验证有效性 +4. 点击 「保存会话」 持久化 + +Cookie 导入支持自动识别多站点 Cookie: +- Pixiv Cookie → 用于 Pixiv 下载 +- X/Twitter Cookie → 写入 `runtime/x_cookies.json` +- 小红书 Cookie → 写入 `runtime/xiaohongshu_cookies.json` +- 长佩 Cookie → 写入 `runtime/changpei_cookies.json` + +#### 账号密码登录 + +输入邮箱/Pixiv ID 和密码后点「尝试登录」,成功后自动生成 Cookie。注意:可能被 Pixiv 风控/二步验证拦截,稳定性不如 Cookie 登录。 + +#### 会话管理 + +- 「自动载入」:启动时自动读取上次保存的会话 +- 「保存会话」:将 Cookie、云同步 Token 一并持久化到本地文件 + +### 3.3 抓取功能 + +#### 平台选择 + +主界面通过下拉菜单切换平台,模式列表随之更新: + +| 平台 | 可选模式 | +|------|---------| +| Pixiv | 作者全部作品、单个作品、漫画、文章/小说、关键词搜索 | +| X / Twitter | 作者媒体、单个推文、搜索词媒体 | +| 小红书 | 搜索关键词、单篇笔记、链接解析 | +| 笔趣阁小说 | 搜索/书籍链接 | +| 长佩文学 | 搜索/书籍链接 | +| 插件自动识别 | 自动匹配 | + +#### 目标输入格式 + +| 模式 | 输入示例 | +|------|---------| +| Pixiv 作者 | `12345678` 或 `https://www.pixiv.net/users/12345678` | +| Pixiv 作品 | `https://www.pixiv.net/artworks/12345678` | +| Pixiv 小说 | `https://www.pixiv.net/novel/show.php?id=12345678` | +| Pixiv 关键词 | `初音未来` 或 `猫耳` | +| X 作者 | `nasa` 或 `@nasa` | +| X 推文 | `https://x.com/user/status/123456...` | +| X 搜索 | `anime art filter:media` | +| 小红书笔记 | `https://www.xiaohongshu.com/explore/...` | +| 笔趣阁 | `斗破苍穹` 或书籍/章节链接 | +| 长佩 | `书名` 或 `novel-{id}.html` 链接 | + +#### 批量输入 + +开启「统一任务队列」后支持: +- 多行输入(每行一个目标) +- 逗号分隔 +- `#标签` 形式混合编排 +- 作者模式可配合水印增量同步(只抓新图) + +### 3.4 下载流程 + +1. 选择平台和模式 +2. 填写目标内容 +3. 点击「开始抓取」 +4. 程序自动: + - 解析目标(支持 ID 反查、链接解析、关键词搜索) + - 批量下载图片/小说 + - 写入本地 SQLite 图库 + - 如需云同步则自动上传 +5. 实时显示进度:作品进度、成功数、新下载页数、跳过数 + +### 3.5 性能设置 + +#### 参数配置 + +| 参数 | 范围 | 默认值 | 说明 | +|------|------|--------|------| +| 下载线程 | 2 - 16 | 8 | 并发下载线程数 | +| 请求间隔 | 0 - 1.5s | 0.1s | 请求间延迟 | + +#### 代理池 + +支持两种代理来源: +1. **手动代理列表**:粘贴 `protocol://ip:port` 格式的代理 +2. **Quake 代理**:通过 Quake 网络空间搜索引擎获取海外代理 + - 可选国家过滤:`US, JP, KR, SG, HK, TW, DE, FR, GB, CA, AU` +3. 支持代理预校验:自动检测可用性并回填 + +代理模式下线程数自动限制在 3-6 之间,降低触发限流概率。 + +--- + +## 4. Sakura Cloud 云端平台 + +### 4.1 概述 + +Sakura Cloud 是整个系统的数据中心,负责: + +- **用户认证**:JWT 登录/注册/Token 刷新 +- **内容存储**:图片、漫画、小说文件存储与分发 +- **元数据管理**:作品信息、作者、标签、收藏、关注 +- **数据同步**:桌面端上传 ↔ 手机端下载 +- **关注管理**:关注作者、更新检测 +- **每日提醒**:定时生成关注作者更新摘要 +- **推送服务**:设备 Token 管理 + +### 4.2 API 端点 + +| 分组 | 端点 | 方法 | 说明 | +|------|------|------|------| +| 认证 | `/api/v1/auth/register` | POST | 用户注册 | +| 认证 | `/api/v1/auth/login` | POST | 用户登录 | +| 认证 | `/api/v1/auth/refresh` | POST | 刷新 Token | +| 用户 | `/api/v1/users/me` | GET | 获取当前用户信息 | +| 图库 | `/api/v1/gallery/` | GET | 查询图库内容 | +| 图库 | `/api/v1/gallery/artworks` | GET | 插画列表(分页/筛选) | +| 图库 | `/api/v1/gallery/artworks/{id}` | GET | 插画详情 | +| 小说 | `/api/v1/novels/` | GET | 小说列表 | +| 小说 | `/api/v1/novels/{id}` | GET | 小说详情 | +| 文件 | `/api/v1/files/` | POST | 上传文件 | +| 文件 | `/api/v1/files/{id}` | GET | 下载文件 | +| 关注 | `/api/v1/follows/` | GET/POST | 关注列表 / 添加关注 | +| 关注 | `/api/v1/follows/sync` | POST | 同步关注 | +| 同步 | `/api/v1/sync/` | POST | 数据同步 | +| 推送 | `/api/v1/push/` | POST | 推送管理 | +| 提醒 | `/api/v1/push/reminders` | GET | 获取提醒 | +| 管理 | `/api/v1/admin/` | GET | 管理后台(需管理员权限) | +| 健康 | `/api/v1/health` | GET | 健康检查 | + +### 4.3 认证机制 + +- 使用 **JWT (JSON Web Token)** 认证 +- Access Token 短期有效,Refresh Token 用于续期 +- 桌面端和移动端登录同一账号即可共享数据 +- Token 在桌面端保存后,云同步和缩略图墙自动使用 + +### 4.4 定时任务 + +| 任务 | 频率 | 说明 | +|------|------|------| +| 提醒摘要检查 | 每 5 分钟 | 检查已开启提醒的用户,生成每日摘要 | +| 关注更新检测 | 启动时 + 每 5 分钟 | 检测关注作者是否有新内容 | +| 推送清理 | 定期 | 清理过期推送记录 | + +### 4.5 部署方式 + +```bash +# Docker Compose 部署 +cd sakura_cloud +docker-compose up -d + +# 或直接运行 +pip install -r sakura_cloud/requirements.txt +uvicorn sakura_cloud.main:app --host 0.0.0.0 --port 8000 +``` + +默认云端地址(项目中硬编码):`http://api.kunkunxiaomao.top:8000` + +--- + +## 5. Android 移动端功能详解 + +### 5.1 底部导航 + +| 标签 | 功能 | +|------|------| +| 📍 **发现** | 首页动态:最近浏览、最近阅读、更新提醒 | +| 🖼️ **图片** | 云端同步的插画浏览、详情查看、收藏、下载 | +| 📖 **漫画** | 漫画按组展示,连续浏览,支持整组下载 | +| 📚 **书架** | 三列彩色方块,按书名归档,章节选择,阅读进度排序 | +| 👤 **我的** | 用户资料、云端空间、收藏管理、每日提醒设置 | + +### 5.2 小说阅读器 + +功能强大的跨设备小说阅读器: + +| 功能 | 说明 | +|------|------| +| 自动分页 | 根据屏幕尺寸和字体大小自动分页 | +| 阅读进度 | 自动记住阅读位置,最近阅读优先排序 | +| 收藏管理 | 收藏/取消收藏 | +| 翻译功能 | 当前页翻译、原文/译文切换 | +| 阅读设置 | 字体大小、行高、背景色 可调 | +| 章节切换 | 系列小说底部面板直接列出章节,点击切换 | +| 删除管理 | 删除小说 | + +### 5.3 书源系统 + +移动端集成了 **Legado 规则引擎**,支持导入第三方书源规则: + +- 书源管理器(source_manager_page):添加/管理书源 +- 统一搜索(unified_search):跨书源搜索小说 +- 在线书架(online_shelf):管理在线书籍 +- 阅读历史(online_reading_history):记录阅读进度 +- 源健康检查(source_health):检测书源可用性 + +### 5.4 我的页功能 + +| 功能 | 说明 | +|------|------| +| 用户资料 | 查看和编辑个人信息 | +| 云端空间 | 查看存储使用情况 | +| 我的收藏 | 收藏的图片和小说管理 | +| 每日更新提醒 | 开关 + 时间设置 + 链接到提醒中心 | +| 退出登录 | 清除会话 | + +--- + +## 6. 插件系统 + +### 6.1 架构设计 + +插件系统基于 **动态 Python 模块加载** 实现。每个插件是一个包含 `plugin.py` 的目录,目录中可附带 `manifest.json`(可选)和 `__init__.py`。 + +### 6.2 核心接口 + +所有插件必须继承 `BasePlugin` 抽象基类: + +```python +class BasePlugin(ABC): + """每个站点插件必须实现的接口""" + + @property + def domain(self) -> str: # 主域名,如 "pixiv.net" + ... + + @property + def name(self) -> str: # 人类可读名称,如 "Pixiv" + ... + + @property + def version(self) -> str: # 语义版本号 + ... + + def can_handle(self, url: str) -> bool: + """判断是否可以处理该 URL""" + ... + + def parse(self, url: str) -> list[Resource]: + """解析 URL 为一个或多个可下载资源""" + ... + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + """下载资源文件到指定路径""" + ... + + def get_headers(self) -> dict[str, str]: + """HTTP 请求头(Cookie 等)""" + ... +``` + +### 6.3 资源模型 + +```python +@dataclass +class Resource: + id: str # 资源 ID + url: str # 原始 URL + title: str # 标题 + author: str # 作者名 + author_id: str # 作者 ID + files: list[dict] # 文件列表 [{url, page}, ...] + metadata: dict # 元数据 + thumbnail: str # 缩略图 URL + created_at: str # 创建时间 +``` + +### 6.4 插件清单 (manifest.json) + +```json +{ + "name": "MyPlugin", + "version": "1.0.0", + "domain": "example.com", + "author": "Your Name", + "description": "站点描述", + "requires_auth": true, + "min_platform_version": "1.0.0", + "tags": ["illust", "manga"], + "dependencies": [] +} +``` + +### 6.5 内置插件 + +| 插件名 | 域名 | 类型 | 技术特点 | +|--------|------|------|----------| +| **Pixiv** | pixiv.net | 图片/小说 | HTTP requests,支持 Cookie/代理,反查作者 | +| **X** | twitter.com | 图片/视频 | Playwright 浏览器渲染,速率限制,防封策略 | +| **小红书** | xiaohongshu.com | 图片/笔记 | Playwright,Cookie 持久化 | +| **AO3** | archiveofourown.org | 小说 | HTTP requests | +| **笔趣阁** | 多源 | 小说 | 支持搜索、榜单解析、章节批量下载 | +| **长佩文学** | gongzicp.com | 小说 | 支持搜索、书籍选择、全章抓取 | + +### 6.6 插件管理面板 + +桌面端提供完整的插件管理功能: + +| 操作 | 说明 | +|------|------| +| **导入 .py** | 导入第三方 Python 插件文件 | +| **生成模板** | 填写名称/域名/作者/描述,自动生成插件骨架代码 | +| **重新加载** | 从磁盘重载所有插件 | +| **重载** | 重载单个插件 | +| **从内存卸载** | 卸载内置插件 | +| **删除文件** | 删除用户导入的插件目录 | +| **状态显示** | 就绪/需配置 | +| **来源标识** | 内置插件 / 用户插件 | + +### 6.7 编写自定义插件 + +#### 最小示例 + +```python +from pathlib import Path +from pixiv_app.core.plugin.base import BasePlugin, Resource + + +class MySitePlugin(BasePlugin): + domain = "example.com" + name = "ExampleSite" + version = "1.0.0" + + def can_handle(self, url: str) -> bool: + return "example.com" in url + + def parse(self, url: str) -> list[Resource]: + # 实现解析逻辑 + return [ + Resource( + id="123", + url=url, + title="示例标题", + author="作者名", + author_id="456", + files=[{"url": "https://example.com/img/1.jpg", "page": 0}], + metadata={"kind": "illust"}, + ) + ] + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + # 实现下载逻辑 + import requests + save_path.mkdir(parents=True, exist_ok=True) + downloaded = [] + for item in resource.files: + resp = requests.get(item["url"], stream=True) + fname = item["url"].rsplit("/", 1)[-1] + dest = save_path / fname + dest.write_bytes(resp.content) + downloaded.append(dest) + return downloaded + + def get_headers(self) -> dict[str, str]: + return {"User-Agent": "Mozilla/5.0 ..."} + + +plugin_class = MySitePlugin # 必须导出 +``` + +#### 插件事件钩子 + +```python +from pixiv_app.core.plugin.manager import PluginManager, PluginEvent + +manager = PluginManager(["plugins/"]) +manager.on_event(PluginEvent.BEFORE_LOAD, lambda path: print(f"加载: {path}")) +manager.on_event(PluginEvent.AFTER_LOAD, lambda plugin: print(f"已加载: {plugin.name}")) +manager.on_event(PluginEvent.ERROR, lambda path, exc: print(f"错误: {path} {exc}")) +``` + +### 6.8 插件生命周期 + +1. **发现**:扫描 `plugins/` 和用户插件目录 +2. **加载**:动态 import,实例化 `plugin_class` +3. **验证**:调用 `validate()` 检查配置 +4. **使用**:URL 匹配 → `parse()` → `download()` +5. **重载**:从 sys.modules 移除后重新加载 +6. **卸载**:从内存移除,内置插件仅卸载不删除文件 + +--- + +## 7. 本地图库与缩略图墙 + +### 7.1 本地图库 (SQLite) + +所有下载内容写入本地 SQLite 数据库,存储: + +| 数据 | 字段 | +|------|------| +| 作品 | id, title, author, author_id, tags, description, created_at, downloaded_at | +| 文件 | path, size, sha256, mime_type, width, height | +| 收藏 | artwork_id, collected_at | +| 关注 | author_id, platform, followed_at | +| 标签 | name, category | +| OCR 文本 | artwork_id, text, confidence | + +数据库路径:`%LOCALAPPDATA%\PixivDownloader\library.db`(打包版)/ `data/library.db`(源码版) + +### 7.2 缩略图墙 WebUI + +内嵌本地 Web 服务,默认端口 8765(自动降级 8766/8767),浏览器访问。 + +#### 功能 + +| 功能 | 说明 | +|------|------| +| 缩略图浏览 | 网格展示所有图片和漫画 | +| 搜索 | 标题、作者、简介全文搜索 | +| 标签筛选 | 按标签分类浏览 | +| 作者视图 | 按作者聚合查看作品 | +| 作品详情 | 大图查看、信息展示 | +| 收藏管理 | 收藏/取消收藏作品 | +| 关注同步 | 添加/管理关注作者 | +| X 关注 | 管理 X 平台关注作者 | + +#### 技术实现 + +- HTTP 服务:纯 Python `http.server` 实现 +- API 端点:`/api/artworks`, `/api/artworks/{id}`, `/api/tags`, `/api/authors` +- 前端页面:Jinja2 模板渲染 + +--- + +## 8. 云同步机制 + +### 8.1 同步流程 + +``` +桌面端下载完成 + │ + ├──→ 写入本地 SQLite 图库 + │ + └──→ [云同步开启] → 上传至 Sakura Cloud + │ + ├──→ 上传文件 (图片/漫画页) + │ + ├──→ 上传元数据 (作品信息/标签/作者) + │ + ├──→ 上传关注信息 (follow) + │ + └──→ 上传更新提醒 (alert) + +手机端 + │ + └──→ [登录同一账号] → 从 Sakura Cloud 拉取数据 + │ + ├──→ 图片页: 浏览插画 + ├──→ 漫画页: 浏览漫画 + ├──→ 书架页: 阅读小说 + └──→ 发现页: 查看更新提醒 +``` + +### 8.2 配置方法 + +1. 勾选「启用 Sakura Cloud 同步」 +2. 点击「账号管理」→ 登录或注册 +3. 登录成功后 Token 自动保存 +4. 可点击「测试连接」验证 + +### 8.3 Token 管理 + +- 基于 `CloudSession` 单例全局共享 +- 自动检测 JWT 过期时间 +- Token 即将过期时自动使用 Refresh Token 续期 +- 刷新后的 Token 同步写回桌面会话文件 + +--- + +## 9. 每日提醒功能 + +### 9.1 工作机制 + +每日提醒 **不是手机本地闹钟**,而是**云端摘要提醒**: + +1. 桌面端关注作者 → 新内容下载 → 云同步上传 +2. 云端每 5 分钟检查已开启提醒的用户 +3. 有未读 alert 时生成「每日摘要提醒」 +4. 手机端「发现」页的 `更新提醒` 区域显示 +5. 同一天内不会重复生成第二条摘要 + +### 9.2 设置方法 + +**桌面端:** +1. 登录 Pixiv(Cookie 方式) +2. 启用云同步并登录云端账号 +3. 打开缩略图墙 → 作品详情 → 「关注作者」(或在「关注同步」页手动添加) +4. 对于 X 作者,在「X 关注」区域添加用户名后点「同步 X 关注」 + +**手机端:** +1. 登录同一云端账号 +2. 进入「我的」页 +3. 打开「每日更新提醒」开关 +4. 设置提醒时间(HH:MM) +5. 到「发现」页查看更新提醒 + +### 9.3 摘要格式 + +- 标题:`每日 09:00 更新提醒` +- 内容:关注作者新增动态数量、来源平台、最近活跃作者名 + +--- + +## 10. 代理与性能设置 + +### 10.1 代理配置 + +桌面端「代理设置」对话框提供: + +| 配置项 | 说明 | +|--------|------| +| 启用代理池 | 开关 | +| 手动代理 | 文本区粘贴代理列表(protocol://ip:port 格式) | +| 使用 Quake | 启用网络空间搜索引擎获取代理 | +| Quake API Key | Quake 账号的 API 密钥 | +| Quake Cookie | Quake 登录 Cookie | +| Quake 模式 | api_v3 等 | +| 国家过滤 | 逗号分隔的国家代码 | +| 预校验 | 测试代理可用性并自动回填 | + +### 10.2 性能建议 + +| 场景 | 推荐线程数 | 推荐延迟 | +|------|-----------|---------| +| 日常少量下载 | 2-4 | 0.5s | +| 批量下载 | 6-8 | 0.2s | +| 使用代理 | 3-6(自动限制) | 0.3s | +| 高风险平台 | 1-2 | 1.0s+ | + +--- + +## 11. 安装与运行 + +### 11.1 成品文件(推荐普通用户) + +直接从 `releases/` 目录获取: + +| 文件 | 说明 | 大小 | +|------|------|------| +| `releases/sakuradownloader.exe` | Windows 桌面端,双击即用 | ~68 MB | +| `releases/app-release.apk` | Android 客户端 | ~54 MB | + +### 11.2 源码运行(开发者) + +#### 环境要求 + +| 组件 | 版本要求 | +|------|---------| +| Python | 3.8+ | +| Flutter | 3.x(仅 Android 端开发) | +| JDK | 17+(仅 Android 打包) | +| PostgreSQL | 14+(仅云端部署) | + +#### 桌面端 + +```powershell +# 创建虚拟环境 +python -m venv .venv +.\.venv\Scripts\activate + +# 安装依赖 +pip install -r requirements.txt + +# 运行 +python main.py +``` + +Pixiv 需要额外安装 Playwright 浏览器: +```bash +playwright install chromium +``` + +#### 云端 + +```powershell +pip install -r sakura_cloud/requirements.txt +uvicorn sakura_cloud.main:app --reload +``` + +#### 移动端 + +```powershell +cd sakura_mobile +flutter pub get +flutter run +``` + +--- + +## 12. 打包与发布 + +### 12.1 一键打包 + +```powershell +# 仅桌面 EXE +scripts\build_desktop_exe.bat + +# 仅 Android APK +scripts\build_mobile_android.bat + +# 复制到 releases/ +scripts\copy_release_artifacts.bat + +# 一键全部 +scripts\build_all.bat +``` + +### 12.2 构建产物 + +| 产物 | 路径 | 工具 | +|------|------|------| +| Windows EXE | `dist/sakuradownloader.exe` | PyInstaller | +| Android APK | `sakura_mobile/build/app/outputs/flutter-apk/app-release.apk` | Flutter build | + +### 12.3 注意事项 + +- APK 当前为**调试签名**,仅供个人测试,不宜直接上架分发 +- 正式分发建议重新生成正式 keystore 签名 +- EXE 打包前需确认 Playwright Chromium 已安装 +- 内置插件会自动打包进 EXE + +--- + +## 13. 完整使用指南 + +### 13.1 桌面端首次使用 + +1. **启动**:双击 `sakuradownloader.exe` +2. **阅读须知**:查看启动弹窗的用户须知(5 页) +3. **导入 Cookie**: + - 浏览器登录 Pixiv → 用 `Get cookies.txt` 插件导出 + - 回到桌面端 → 「导入 cookies.txt / json」 + - 「检测会话」 → 「保存会话」 +4. **首次下载**: + - 选择平台 = `Pixiv` + - 选择模式 = `作者全部作品` + - 目标输入一个作者 ID 或作品链接 + - 点击「开始抓取」 +5. **浏览图库**:下载完成后点击「打开缩略图墙」 + +### 13.2 配置云同步 + +1. 桌面端勾选「启用 Sakura Cloud 同步」 +2. 点击「账号管理」→ 切换到「注册」→ 注册账号 +3. 切换到「登录」→ 登录 +4. 提示「登录成功,云同步凭证已自动保存」 +5. 点击「测试连接」验证 + +### 13.3 手机端首次使用 + +1. 安装 `app-release.apk` +2. 打开应用 → 使用与桌面端相同的云端账号登录 +3. 等待数据同步完成 +4. 浏览各标签页内容 + +### 13.4 使用关注与提醒 + +1. 桌面端:打开缩略图墙 → 作品详情点「关注作者」 +2. 或在「关注同步」页手动添加作者 ID +3. 手机端:我的 → 打开「每日更新提醒」→ 设置时间 +4. 到点后去「发现」页查看 + +### 13.5 导入第三方插件 + +1. 桌面端点击「插件管理」 +2. 点击「导入 .py」选择插件文件 +3. 验证状态显示「就绪」 +4. 回到主界面,选择「插件自动识别」平台 +5. 输入目标 URL/ID,程序自动匹配插件 + +--- + +## 14. 常见问题与排错 + +### 14.1 登录相关 + +| 问题 | 解决方案 | +|------|---------| +| Cookie 登录失效 | 重新从浏览器导出最新 Cookie 并导入 | +| 账号密码登录失败 | 很可能被 Pixiv 风控拦截,改用 Cookie 登录 | +| 检测会话失败 | Cookie 可能已过期,重新导出导入 | +| X 登录失败 | 确认 Cookie 已同步到 runtime/x_cookies.json | + +### 14.2 下载相关 + +| 问题 | 解决方案 | +|------|---------| +| 下载速度为 0 | 检查网络连接,尝试启用代理 | +| 某些作品下载失败 | 可能是作品被删除或设为私密,跳过继续 | +| 关键词搜索无结果 | 尝试不同关键词或减少数量限制 | +| 代理模式下全部失败 | 代理可能不可用,先用「预校验」测试 | + +### 14.3 云同步相关 + +| 问题 | 解决方案 | +|------|---------| +| 连接失败 | 检查网络,确认 API 地址可访问 | +| Token 无效 | 重新登录云端账号 | +| 手机端没内容 | 确认桌面端云同步已开启,使用同一账号 | +| 上传失败 | 检查云端存储空间 | + +### 14.4 移动端相关 + +| 问题 | 解决方案 | +|------|---------| +| APK 安装被拦截 | 允许「未知来源应用安装」 | +| 登录后没数据 | 确认桌面端已开启云同步并上传了内容 | +| 提醒没出现 | 确认已关注作者、新内容已同步、已过提醒时间 | + +### 14.5 提醒相关 + +| 问题 | 解决方案 | +|------|---------| +| 开了提醒没内容 | 需要桌面端先关注作者 + 有新内容 + 已同步到云端 | +| 没收到推送通知 | 这是应用内提醒,不是系统推送,去「发现」页查看 | +| 提醒时间不对 | 检查时区设置(默认 Asia/Shanghai) | + +--- + +## 15. 安全与风险控制 + +### 15.1 使用安全 + +| 措施 | 说明 | +|------|------| +| Cookie 本地存储 | Cookie 仅保存在本地文件,不会上传到第三方 | +| 密码不写入源码 | 账号密码仅在内存中使用,不会持久化到代码 | +| Token 加密存储 | JWT Token 保存到本地会话文件 | +| HTTPS 传输 | 云端 API 使用 HTTPS/TLS 加密 | + +### 15.2 风险控制建议 + +| 建议 | 说明 | +|------|------| +| 合理频率 | 避免短时间内高频大批量下载 | +| 定期更新 Cookie | Cookie 失效时及时重新导入 | +| 使用代理 | 大批量下载时启用代理池分散 IP | +| 控制线程数 | 不要将线程数设置过高 | +| 间隔延迟 | 适当增加请求间隔 | +| 研究学习用途 | 本工具仅供个人研究学习使用 | + +### 15.3 已知限制 + +| 限制 | 说明 | +|------|------| +| Pixiv 风控 | 高频访问可能触发验证码或临时封禁 | +| X 速率限制 | X API 有频率限制,插件已内置防封策略 | +| 小红书反爬 | 可能返回 404,作者持续优化中 | +| APK 调试签名 | 不适合正式分发,仅适合个人测试 | +| 提醒非系统推送 | 需要在应用内查看,不会弹出系统通知 | + +--- + +## 16. 开发指南 + +### 16.1 技术栈 + +| 层 | 技术 | +|----|------| +| 桌面 GUI | Python / CustomTkinter 5.2+ | +| HTTP 客户端 | requests, httpx | +| 浏览器自动化 | Playwright 1.41+ | +| 本地数据库 | SQLite | +| OCR | PaddleOCR 2.7+ | +| 模板引擎 | Jinja2 3.1+ | +| 加密 | cryptography 45.0+ | +| 打包 | PyInstaller | +| 云端框架 | FastAPI | +| 云端 ORM | SQLAlchemy (async) | +| 云端数据库 | PostgreSQL | +| 云端迁移 | Alembic | +| 移动端框架 | Flutter 3.x | +| 移动端状态 | Riverpod | +| 移动端网络 | Dio | + +### 16.2 项目依赖 + +``` +# requirements.txt +customtkinter>=5.2.0 +requests>=2.31.0 +Pillow>=10.0.0 +Jinja2>=3.1.0 +playwright>=1.41.0 +httpx>=0.27.0 +cryptography>=45.0.0 +firebase-admin>=6.5.0 +paddleocr>=2.7.0 +``` + +### 16.3 代码规范 + +- Python 代码使用 `from __future__ import annotations` 延迟注解求值 +- 所有插件必须继承 `BasePlugin` 并导出 `plugin_class` +- 桌面端使用线程模型(非 asyncio)与 requests 配合 +- 云端使用 async/await 全异步 +- 移动端使用 Riverpod 进行状态管理 + +### 16.4 运行测试 + +```powershell +cd tests +python -m pytest +``` + +### 16.5 贡献指南 + +1. Fork 项目仓库 +2. 创建功能分支 +3. 如果添加新站点支持,建议编写为独立插件 +4. 提交 PR 前运行现有测试 +5. 更新相关文档 + +--- + +## 17. 更新日志 + +### v2.0.0 (2026-06) + +- ✨ 新增:笔趣阁小说插件 +- ✨ 新增:长佩文学插件 +- ✨ 新增:小红书链接解析模式 +- ✨ 新增:插件模板生成器 +- ✨ 新增:X 关注同步功能 +- ✨ 新增:云端每日提醒系统 +- ✨ 新增:移动端 Legado 规则引擎 +- ✨ 新增:移动端漫画分组浏览 +- ✨ 新增:移动端在线书架与统一搜索 +- 🔧 优化:Cookie 导入支持多站点自动分流 +- 🔧 优化:代理池预校验与自动回填 +- 🔧 优化:云同步 Token 自动刷新机制 +- 🔧 优化:任务队列支持批量 #标签输入 +- 🔧 优化:移动端阅读器设置面板 +- 🐛 修复:多项稳定性与 UI 问题 + +--- + +## 附录 + +### A. 联系方式 + +- 作者 QQ:2811043066 +- 博客:http://blog.kunkunxiaomao.top +- 项目仓库:请查看 README.md 中的链接 + +### B. 相关文档 + +| 文档 | 说明 | +|------|------| +| [使用说明-桌面版与安卓版.md](../使用说明-桌面版与安卓版.md) | 终端用户快速上手 | +| [README.md](../README.md) | 项目概览与快速开始 | +| [项目文件/BUILD.md](../项目文件/BUILD.md) | 打包细节与排错 | +| [docs/Sakura_Downloader_完整文档.md](Sakura_Downloader_完整文档.md) | 完整技术文档 | +| [docs/login-and-auth.md](login-and-auth.md) | 登录认证详解 | + +### C. 许可说明 + +- 本工具仅供**个人研究学习**使用 +- 请勿用于商业用途或非法搬运他人作品 +- APK 为调试签名,仅适合个人测试 +- 使用 X 插件前需安装 Playwright Chromium +- 尊重各平台的服务条款和版权规定 + +--- + +> 📝 最后更新:2026-06-23 +> 💡 如有问题或建议,欢迎联系作者或提交 Issue diff --git a/docs/代码审计报告.md b/docs/代码审计报告.md new file mode 100644 index 0000000..99370dc --- /dev/null +++ b/docs/代码审计报告.md @@ -0,0 +1,410 @@ +# 代码审计报告 + +> 审计日期:2026-06-16 +> 审计范围:全项目(Flutter 移动端 30 个文件 + Python 桌面端 16 个文件 + Cloud 后端 10 个文件 + 5 个插件) +> 发现总数:111 项(Critical 9 / High 20 / Medium 46 / Low 36) + +--- + +## 目录 + +1. [Critical(紧急修复)](#一critical紧急修复) +2. [High(高危)](#二high高危) +3. [Medium(中危)](#三medium中危) +4. [Low(低危)](#四low低危) +5. [跨模块问题汇总](#五跨模块问题汇总) +6. [修复优先级建议](#六修复优先级建议) + +--- + +## 一、Critical(紧急修复) + +### C1. Cloud 后端:JWT 密钥使用已知默认值 + +- **文件:** `sakura_cloud/core/config.py:29` +- **代码:** `jwt_secret_key: str = Field(default="change-this-secret-in-production", min_length=16)` +- **问题:** 默认 JWT 密钥是公开的占位字符串。如果运维人员忘记设置 `JWT_SECRET_KEY` 环境变量,任何知道此默认值的人都可以伪造任意用户的 JWT token,获得完全未授权访问。 +- **修复:** 生产环境启动时检测是否为默认值,若是则拒绝启动并输出明确错误信息。不使用带默认值的 `Field`,改用 `Field(default=None)` 并在启动时校验。 + +### C2. 笔趣阁插件:硬编码 AES 密钥和 IV + +- **文件:** `plugins/biquge/plugin.py:60-67` +- **代码:** + ```python + TOKEN_SECRET = "book@token.html" + TOKEN_MD5 = hashlib.md5(TOKEN_SECRET.encode("utf-8")).hexdigest() + TOKEN_IV = TOKEN_MD5[:16].encode("utf-8") + TOKEN_KEY = TOKEN_MD5[16:].encode("utf-8") + ``` +- **问题:** AES 密钥和 IV 从硬编码字符串经 MD5 派生,任何人看到源代码即可解密和伪造 API token。固定 IV 下的 CBC 模式使相同明文产生相同密文,可被已知明文攻击。这不是加密,是混淆。 +- **修复:** 每次加密使用随机 IV(`os.urandom(16)`),密钥从用户配置或安全随机源派生。 + +### C3. 小红书插件:`--no-sandbox` 禁用 Chromium 安全沙箱 + +- **文件:** `plugins/xiaohongshu/plugin.py:361` +- **代码:** `"args": ["--disable-blink-features=AutomationControlled", "--no-sandbox"]` +- **问题:** `--no-sandbox` 完全禁用 Chromium 沙箱,浏览器进程以完整 OS 权限运行。小红书的页面是不受信任的外部内容——如果渲染引擎存在漏洞被利用,攻击者获得主机系统的无限制访问权。 +- **修复:** 移除 `--no-sandbox`。如果必须在 Docker/root 环境运行,使用 `--cap-add=SYS_ADMIN` 或配置 seccomp profile,而非全局禁用沙箱。 + +### C4. Cloud 后端:数据库默认凭据 `sakura:sakura` + +- **文件:** `sakura_cloud/core/config.py:17` +- **代码:** `database_url: str = "postgresql+psycopg://sakura:sakura@localhost:5432/sakura_cloud"` +- **问题:** 默认数据库用户名和密码都是 `sakura`。如果部署时未覆盖 `DATABASE_URL`,数据库对任何能访问 `localhost:5432` 的人开放。 +- **修复:** 同 C1——生产环境启动时检测默认值并拒绝启动。 + +### C5. Cloud 后端:MinIO 默认凭据 `minioadmin:minioadmin` + +- **文件:** `sakura_cloud/core/config.py:24-25` +- **代码:** `minio_access_key: str = "minioadmin"` / `minio_secret_key: str = "minioadmin"` +- **问题:** 这是 MinIO 官方默认凭据。如果 MinIO 端点暴露在网络上,任何人可以访问所有用户存储的文件。 +- **修复:** 同上——启动时检测是否为默认值,若是则拒绝启动。 + +### C6. 桌面端:云同步 API 使用明文 HTTP + +- **文件:** `pixiv_app/gui/app.py:54` +- **代码:** `DEFAULT_CLOUD_API = "http://api.kunkunxiaomao.top:8000"` +- **问题:** 使用 HTTP 而非 HTTPS。用户凭据(邮箱、密码、access token)和上传文件内容以明文在网络上传输。同一网络上的攻击者可通过被动嗅探截获所有认证凭据和文件数据。 +- **修复:** 改为 `http://api.kunkunxiaomao.top`(如果服务器支持 HTTPS)。最差情况下至少做出显式警告。 + +### C7. Flutter:`_initialized` 标志在 Firebase 初始化成功前设置 + +- **文件:** `sakura_mobile/lib/src/core/push_notifications.dart:27-29` +- **代码:** `_initialized = true` 在 `await Firebase.initializeApp()` **之前**设置 +- **问题:** 如果 `Firebase.initializeApp()` 抛出异常,`_initialized` 已经是 `true`,导致后续 `initialize()` 调用全部命中 `if (_initialized) return` 的 early return,Firebase 永远无法重试初始化。推送功能永久失效,无任何提示。 +- **修复:** 将 `_initialized = true` 移到 `await Firebase.initializeApp()` 成功之后。 + +### C8. Flutter:`recentUpdatesOnly` 是死开关(dead UI switch) + +- **文件:** `sakura_mobile/lib/src/pages/tabs/gallery_tab.dart:32,120` +- **问题:** `recentUpdatesOnly` 状态变量控制哪个 Chip 显示"已选中",但 `_load()` 方法从未将其作为查询参数传给 API。用户看到的"最近更新"筛选切换实际上没有任何效果。 +- **修复:** 在 `_load()` 中将 `recentUpdatesOnly` 转换为对应的 API 参数,或者移除该 Chip。 + +### C9. 桌面端:ProxyPool 的多线程数据竞争 + +- **文件:** `pixiv_app/core/proxy_pool.py:193,272-273,278-279` +- **问题:** + 1. `ProxyPool.current_index` 的读取(line 272)和递增(line 273)之间无锁保护,`ThreadPoolExecutor` 的多个 worker 同时调用 `get_proxy()` 时产生重复或跳跃的索引 + 2. `ProxyPool.working_proxies` 被 `mark_failed()` 的 `.remove()` 修改,而其他线程可能正在迭代它——可抛出 `ValueError` 或不一致迭代 + 3. `ProxyInfo.fail_count` 和 `success_count` 的非原子递增在 `ThreadPoolExecutor` 16 个 worker 并行调用 `verify_proxy()` 时静默丢失计数 +- **修复:** 使用 `threading.Lock` 保护所有对 `working_proxies`、`current_index` 的写操作,`fail_count`/`success_count` 改用 `threading` 安全计数或原子操作。 + +--- + +## 二、High(高危) + +### H1. Cloud 后端:用户登录无限流 → 可暴力破解 + +- **文件:** `sakura_cloud/api/v1/endpoints/auth.py:56-76` +- **问题:** `/login` 端点无限流(`admin_login_rate_limit_per_minute` 只用于 admin 端点)。攻击者可无限制尝试密码组合进行暴力破解。 +- **修复:** 对 `/login`、`/register`、`/token/refresh` 增加速率限制(如每 IP 每分钟 5 次)。 + +### H2. Cloud 后端:用户注册无限流 + 信息泄露 + +- **文件:** `sakura_cloud/api/v1/endpoints/auth.py:24-53` +- **问题:** `/register` 端点无限流。409 响应明确区分"用户名已存在"和"邮箱已存在",可用于枚举已注册用户。 +- **修复:** 加速率限制。错误消息统一为"注册失败,请检查输入"。 + +### H3. Cloud 后端:存储配额未强制执行 + +- **文件:** `sakura_cloud/api/v1/endpoints/gallery.py:223`、`sync.py:86-141` +- **问题:** `default_storage_limit_bytes` 在 config 中定义了(line 108-109),但在创建 artwork 和同步 metadata 时**从未检查** `current_user.storage_used` 是否超过配额。用户可以上传无限制的数据。 +- **修复:** 在 `gallery.py` 和 `sync.py` 的写入路径中增加 `if current_user.storage_used + new_size > limit: raise HTTPException(413)`。 + +### H4. Cloud 后端:Logout 不撤销 token + +- **文件:** `sakura_cloud/api/v1/endpoints/auth.py:79-81` +- **问题:** `/logout` 只返回 `{"ok": True}`,不做任何 token 撤销操作。access token 在其自然过期(最长 24 小时)前持续有效。用户登出后无法阻止 token 被盗用。 +- **修复:** Logout 时递增 `user.token_version`,使当前所有 token 立即失效。 + +### H5. Cloud 后端:Refresh token 轮转不撤销旧 token + +- **文件:** `sakura_cloud/api/v1/endpoints/auth.py:84-104` +- **问题:** 交换 refresh token 时签发新的 access + refresh token,但旧的 refresh token 未失效。如果 refresh token 被盗,攻击者和合法用户都可以持续刷新——直到某人重新登录(才会 bump `token_version`)。 +- **修复:** 实现 refresh token 轮转——每次使用时废弃旧 token 并签发新的。 + +### H6. 桌面端 & Flutter:敏感凭据多处明文存储 + +|| 位置 | 凭据类型 | 存储方式 | +||------|---------|---------| +|| `pixiv_app/core/auth.py:36-41` | Pixiv cookie | JSON 文件明文 | +|| `plugins/pixiv/plugin.py:42-43` | Pixiv cookie | 内存明文 | +|| `plugins/x/plugin.py:76` | X/Twitter cookies | `x_cookies.json` 明文 | +|| `plugins/ao3/plugin.py:687-688` | AO3 cookie | 内存明文 | +|| `plugins/xiaohongshu/plugin.py:386-404` | 小红书 cookies | `xiaohongshu_cookies.json` 明文 | +|| `pixiv_app/core/proxy_pool.py:67-68` | Quake API key | `app_session_file()` JSON 明文 | +|| `sakura_mobile/lib/src/core/session.dart:44-47` | Access token | SharedPreferences 明文 | + +- **修复:** 生产环境使用操作系统密钥链(Windows DPAPI / macOS Keychain / Android Keystore)。最低限度应对配置文件设置受限的文件权限。 + +### H7. Flutter:小说阅读页面翻页不节流 → API 洪泛 + +- **文件:** `sakura_mobile/lib/src/pages/novel_reader_page.dart:787-790` +- **问题:** `onPageChanged` 对**每次翻页**同步调用 `updateNovelProgress(currentNovel.id, page)`。快速翻页时数十上百个 API 请求瞬间发出,压垮服务器并浪费电量/带宽。 +- **修复:** 增加 500ms 防抖,只发送最终位置。 + +### H8. Flutter:探索页从"插画"tab 点搜索 → 打开"小说"模式 + +- **文件:** `sakura_mobile/lib/src/pages/tabs/gallery_tab.dart:398-399` +- **问题:** 用户在"图片"(illust)tab 点击在线搜索时,代码 `widget.fixedContentType != 'manga'` 为真,传入 `UnifiedSearchMode.novel`。从图片页搜索却打开了小说搜索模式。 +- **修复:** 将 explore mode 与 gallery content type 解耦。默认为 manga 模式(漫画源更多)。或增加一个独立的选择器。 + +### H9. Flutter:Session 构造器中异步工作不可 await + +- **文件:** `sakura_mobile/lib/src/core/session.dart:44-47` +- **问题:** `SessionController()` 构造器中调用异步 `_load()`,但构造器不能 `async`。Provider 观察者先看到默认 loading 态再被 `_load()` 完成后的 `notifyListeners()` 更新。Provider 初始化异常无法被外部捕获。 +- **修复:** 使用 Riverpod 的 `FutureProvider` 或 `AsyncNotifier`,或将初始化方法显式暴露给 widget tree 调用。 + +### H10. Flutter:`_validateStoredSession` 静默吞所有异常 + +- **文件:** `sakura_mobile/lib/src/core/session.dart:159` +- **问题:** `catch (_)` 捕获所有异常返回 `null`,不记录日志。网络故障或服务器宕机时用户被静默登出,无任何提示。 +- **修复:** 最低限度 `debugPrint(e)`。更好:区分网络错误(可恢复)和认证错误(需重新登录),并显示对应 UI。 + +### H11. Cloud 后端:JWT 缺少 `aud`/`iss` 声明验证 + +- **文件:** `sakura_cloud/core/security.py:23-32,53-62` +- **问题:** Token 创建时不设置 `aud`(受众)和 `iss`(签发者),`decode_token` 也不验证。如果同一 JWT 密钥被意外共享,一个服务的 token 可在另一个服务中复用。 +- **修复:** 创建 token 时设置 `aud="sakura-cloud"`,解码时验证。 + +### H12. 小红书插件:代理凭据暴露在 URL 字符串中 + +- **文件:** `plugins/xiaohongshu/plugin.py:107-110` +- **问题:** `ProxyConfig.url` 属性将明文用户名密码嵌入 URL:`"https://user:pass@host:port"`。如果这个 URL 字符串被记录到 Playwright 调试日志、异常堆栈或错误消息中,凭据泄露。 +- **修复:** 通过 Playwright 的 `proxy` context 选项分别传入 `username`/`password`(lines 368-371 已部分正确),`.url` 属性不包含凭据。 + +### H13. Flutter:默认 API URL 用 HTTP 明文 + +- **文件:** `sakura_mobile/lib/src/core/api_client.dart:9` +- **代码:** `const defaultCloudApi = 'http://api.kunkunxiaomao.top:8000'` +- **问题:** 与桌面端 C6 相同——明文 HTTP 传输所有凭据和内容。 +- **修复:** 改为 HTTPS。 + +### H14. Flutter:Push notification token refresh 监听器持有过期凭据 + +- **文件:** `sakura_mobile/lib/src/core/push_notifications.dart:66-83` +- **问题:** `onTokenRefresh.listen` 捕获注册时的 `baseUrl` 和 `accessToken`。如果用户后续登录其他服务器或换号,监听器仍用旧凭据,导致 push 注册静默失败(line 79 catch 吞错)。 +- **修复:** 每次 token 刷新时从当前 provider 读取最新凭据。 + +### H15. 桌面端:SQLite 跨线程共享缺少显式锁 + +- **文件:** `pixiv_app/core/library.py:29` +- **代码:** `check_same_thread=False` 允许跨线程共享连接,"当有锁保护时"——但锁的保护由调用者负责且不一致。 +- **问题:** `TaskQueue`(`enqueue.py:232`)将连接传给多个 `ThreadPoolExecutor` worker,没有外部锁。WAL 模式下 SQLite 通常能处理,但高并发写入时仍可能 `SQLITE_BUSY`。 +- **修复:** 改用连接池(如 `sqlite3` 的 URI 模式 `file:path?mode=rw`),或每个 worker 使用独立连接。 + +### H16. 桌面端:画廊同步对每个文件行新开 DB 连接(O(N) 连接泛滥) + +- **文件:** `pixiv_app/gui/app.py:2175-2184` +- **问题:** `_sync_pixiv_rows` 中**每个文件行**都调用 `sqlite3.connect(DEFAULT_LIBRARY_DB)` → 查询 → `close()`。100 个文件同步 = 100 次 DB 打开/关闭循环。性能低下且在高负载下可能触及 OS 文件描述符上限。 +- **修复:** 复用单个连接或将查询批量化。 + +### H17. Cloud 后端:用户登录无限流(跨所有端点) + +- **问题范围:** `/login`(auth.py:56)、`/register`(auth.py:24)、`/token/refresh`(auth.py:84)、所有 gallery 端点、所有 novel 端点、所有 sync 端点——除 admin 外**无一有限流**。 +- **修复:** 全局应用 rate limiting 中间件(如 `slowapi` 或自定义中间件)。 + +### H18. Cloud 后端:`--no-sandbox` 同样存在于 X 插件 + +- **文件:** `plugins/x/plugin.py`(Playwright 启动参数) +- **问题:** 与 C3 相同风险。X/Twitter 页面同样是不受信任的外部内容。 +- **修复:** 移除或至少评估必要性。 + +### H19. 桌面端:N+1 查询模式在关注作者同步中 + +- **文件:** `pixiv_app/core/gallery_service.py:547-561` +- **问题:** `sync_followed_artists` 对每个关注的作者创建 → 查询 → 关闭独立的 `PixivLibrary` 实例。50 个作者 = 100+ 次 DB 打开/关闭。 +- **修复:** 使用池化或缓存的连接。 + +### H20. Flutter:多个页面存在静默吞错 → 空列表 + +|| 文件 | 位置 | 模式 | +||------|------|------| +|| `novels_tab.dart` | 63-67 | `.catchError((_) => [])` | +|| `api_client.dart` | 532 | `_readCache` catch 返回 null | +|| `providers.dart` | 17-21 | `initializeIfFirstRun()` 的 future 被丢弃 | + +- **影响:** API 失败时用户看到空书架/空画廊/空收藏,无法区分"真的没数据"和"加载失败了"。 +- **修复:** 区分 error 状态并在 UI 中显示错误提示和重试按钮。 + +--- + +## 三、Medium(中危) + +### 后端 + +| # | 文件 | 行号 | 问题 | +|---|------|------|------| +| M1 | `sakura_cloud/main.py` | 130-131 | asyncio background task 无引用监控,异常静默死亡 | +| M2 | `sakura_cloud/api/middleware.py` | 77 | `img-src https: http:` 完全取消 CSP 图片保护 | +| M3 | `sakura_cloud/api/middleware.py` | 79 | `style-src 'unsafe-inline'` 可被 XSS 利用 | +| M4 | `sakura_cloud/core/config.py` | 31 | access token 有效期 24h 过长,token 被盗后窗口大 | +| M5 | `sakura_cloud/core/config.py` | 56-58 | FCM 默认启用但无有效凭据,运行时报错 | +| M6 | `sakura_cloud/core/config.py` | 18 | Redis 默认 URL 无密码认证 | +| M7 | `sakura_cloud/api/v1/endpoints/gallery.py` | 324-332 | 删除 artwork 时文件不清理 → 磁盘孤儿文件累积 | +| M8 | `sakura_cloud/api/v1/endpoints/novels.py` | 53-73 | GET 请求触发 DB 写操作(`_resolve_novel_storage_path`),违反 REST 幂等性 | +| M9 | `sakura_cloud/api/v1/endpoints/files.py` | 39 | 缩略图生成同步阻塞 FastAPI 事件循环 | +| M10 | `sakura_cloud/api/v1/endpoints/sync.py` | 38,100,138 | metadata dict 无大小限制 → 数据库 bloat 攻击 | +| M11 | `sakura_cloud/api/v1/endpoints/sync.py` | 37,99,138 | tags 列表无长度/元素限制 → 数据库 bloat 攻击 | +| M12 | `sakura_cloud/api/v1/endpoints/auth.py` | 9 | 密码复杂度仅 `min_length=8`,弱密码可过 | +| M13 | `pixiv_app/core/downloader.py` | 46-48 | 文件名未做长度限制,Windows MAX_PATH 260 字符溢出导致 OSError | +| M14 | `pixiv_app/core/downloader.py` | 476,502,537 | 大量 `except Exception: continue` 静默丢错,生产调试不可能 | +| M15 | `pixiv_app/core/downloader.py` | 690,901-945 | 每个作品创建新 `requests.Session()`(无 keep-alive),100 作品 = 100 次 TLS 握手 | +| M16 | `pixiv_app/core/gallery_service.py` | 399-414 | `_create_sync_manager` 在 3 个文件中重复实现 | +| M17 | `pixiv_app/core/origin_follow_sync.py` | 103 | `asyncio.run()` 在可能已有 event loop 的同步上下文中调用 | +| M18 | `pixiv_app/core/proxy_pool.py` | 213 | 代理测试时 `verify=False`,代理自身可 MITM 注入虚假成功结果 | +| M19 | `pixiv_app/core/auth.py` | 36-41 | `SessionStore.load()` 用裸 `except Exception` 吞所有异常(JSON 损坏、权限错误等) | +| M20 | `pixiv_app/core/gallery_service.py` | 79-81 | tag 筛选用 `JOIN`(非 `LEFT JOIN`),孤儿 `artwork_tags` 行导致作品静默遗漏 | +| M21 | `pixiv_app/core/downloader.py` | 698 | `int(detail.get("pageCount") or 0)` 遇非数字字符串抛 `ValueError` | + +### 插件 + +| # | 文件 | 行号 | 问题 | +|---|------|------|------| +| M22 | `plugins/pixiv/plugin.py` | 243-244 | 文件名从 URL 提取,URL 编码的 `%2F` 可绕过 `rsplit` 做路径穿越 | +| M23 | `plugins/x/plugin.py` | 217-218,386 | 每次操作启动新 Playwright browser context,无复用 → 资源耗尽 | +| M24 | `plugins/biquge/plugin.py` | 845-867 | Playwright page 实例缓存跨操作复用,一次失败污染后续 | +| M25 | `plugins/ao3/plugin.py` | 503-506 | EPUB 解压无 zip bomb 防护,压缩炸弹可 OOM | +| M26 | `plugins/ao3/plugin.py` | 92-96 | `Retry-After` 头无上限,`Retry-After: 999999999` 可使插件休眠 ~31 年 | +| M27 | `plugins/xiaohongshu/plugin.py` | 377 | `headless=False` 在服务器/无 GUI 环境运行失败 | +| M28 | `plugins/xiaohongshu/plugin.py` | 480-588 | `page.evaluate()` 执行不受信任页面的 JS,结果未经域名验证 | +| M29 | `plugins/xiaohongshu/plugin.py` | 630-648 | 下载图片时转发页面所有 cookies 给图片 CDN(含第三方追踪 cookie) | + +### Flutter + +| # | 文件 | 行号 | 问题 | +|---|------|------|------| +| M30 | `api_client.dart` | 440-502 | `translateText()` 每次调用创建并销毁新的 Dio 实例 | +| M31 | `api_client.dart` | 518-535 | `_readCache` 泛型类型不安全——声称返回任何 T 但 SharedPreferences 只存 JSON 基本类型 | +| M32 | `providers.dart` | 17-21 | `sourceManager.initializeIfFirstRun()` 是 async 但返回值被丢弃 | +| M33 | `app.dart` | 24 | 硬编码 Windows 字体 `Microsoft YaHei UI`,iOS/macOS/Linux 回退不一致 | +| M34 | `gallery_tab.dart` | 269-278 | 收藏/取消收藏触发全量列表重载(`_refresh()`),非单项更新 | +| M35 | `gallery_tab.dart` | 336-367 | 批量下载用 `for` + 顺序 `await`,无并发无进度 | +| M36 | `login_page.dart` | 141 | TabBarView 高度硬编码 430px,不同屏幕尺寸下可能裁剪或留白 | +| M37 | `novel_reader_page.dart` | 17-32 | 14 个可变实例字段——翻页设置(字号/行高/主题)在每次页面跳转时丢失 | +| M38 | `novel_detail_page.dart` | 67 | 硬编码 `430`px 滚动偏移到目录区,不随内容高度自适应 | +| M39 | `reminder_center_page.dart` | 22-35 | `results[0]! as Map` 等不安全类型强转,API 返回异常形状时抛 `TypeError` | +| M40 | `profile_tab.dart` | 403 | `logout()` 是 async 但 button onPressed 丢弃返回的 Future | +| M41 | `profile_tab.dart` + `reminder_center_page.dart` | 44-97 | 提醒时间设置逻辑完全重复两份 | +| M42 | `favorites_page.dart` | 51-54 | 每次点击 artwork 都重新执行 O(n) 的 `buildMangaCollections` | +| M43 | `novels_tab.dart` | 461 | epoch 0 作为时间戳兜底 → 显示"53 年前",无意义 | +| M44 | `manga_collections.dart` | 59 | 不同 null-author 作者的同名作品被错误合并为同一合集 | + +### Cloud 后端(补充) + +| # | 文件 | 行号 | 问题 | +|---|------|------|------| +| M45 | `sakura_cloud/main.py` | 123-126 | 启动时 digest 发送失败仅打日志,无告警 | +| M46 | `sakura_cloud/core/security.py` | 54-59 | `decode_token` 冗余手动检查 `exp`,且未设 clock skew 容差 | + +--- + +## 四、Low(低危) + +### 后端 + +| # | 文件 | 行号 | 问题 | +|---|------|------|------| +| L1 | `sakura_cloud/main.py` | 113-131 | startup 事件阻塞事件循环,无就绪探针/超时 | +| L2 | `sakura_cloud/api/middleware.py` | 32-42 | debug 模式 CSP `connect-src` 开放 `localhost:*`(所有本地端口) | +| L3 | `sakura_cloud/api/deps.py` | 29 | `str(user_id)` 对 None 产生字面字符串 `"None"` | +| L4 | `sakura_cloud/api/deps.py` | 34 | `token_version` 比较中,双方都为 0/None 时通过,旧 token 缺少 `tv` 声明不被拒绝 | +| L5 | `sakura_cloud/core/config.py` | 89-103 | `json.loads()` 在 field validator 中内联 import,不标准 | +| L6 | `sakura_cloud/api/v1/endpoints/auth.py` | 79-81 | logout 不递增 `token_version` | +| L7 | `sakura_cloud/api/v1/endpoints/gallery.py` | 51-97 | `ILIKE` + `plainto_tsquery` 用户输入无长度限制 → 可能触发慢查询 | +| L8 | `sakura_cloud/api/v1/endpoints/files.py` | 32 | `original_name` 用户可控 → 下载附件名可用于社工 | +| L9 | `sakura_cloud/api/v1/endpoints/sync.py` | 25 | SHA256 字段只验证长度不验证 hex 字符 | +| L10 | `sakura_cloud/api/v1/endpoints/sync.py` | 144-157 | 设备注册无每用户数量上限 | +| L11 | `pixiv_app/core/downloader.py` | 40 | `PixivRequestError` 继承 `RuntimeError` 而非 `Exception`(语义不当) | +| L12 | `pixiv_app/core/downloader.py` | 233 | `str(user_id).isdigit()` 中 `user_id` 可能为 None → `str(None)` = `"None"` | +| L13 | `pixiv_app/core/downloader.py` | 700,709,771 | 同一 artwork 三次调用 `ensure_artwork_stub`(`INSERT OR IGNORE`,无害但浪费) | +| L14 | `pixiv_app/core/gallery_service.py` | 588-613 | 同一 artwork 多次同步中重复创建 alert | +| L15 | `pixiv_app/core/x_media_service.py` | 522-523 | `datetime.now()` 无时区 → 跨时区对比可能偏差一天 | +| L16 | `pixiv_app/gui/app.py` | 1153-1157 | 小红书页缩进不一致(7 空格,PEP 8 违规) | +| L17 | `sakura_cloud/core/security.py` | 54-59 | `decode_token` 的 `exp` 手动检查与 `jwt.decode` 内建检查重复 | + +### 插件 + +| # | 文件 | 行号 | 问题 | +|---|------|------|------| +| L18 | `plugins/x/plugin.py` | 414 | 下载缓存 JSON 暴露本地绝对路径结构 | +| L19 | `plugins/x/plugin.py` | 463-475 | 文件类型仅凭 URL 子串匹配判断,无 MIME 验证 | +| L20 | `plugins/biquge/plugin.py` | 135,360-361 | `requests.Session()` 无连接池限制 | +| L21 | `plugins/xiaohongshu/plugin.py` | 375-376 | 浏览器 profile 跨会话共享,可能积累旧 cookies 泄漏 | + +### Flutter + +| # | 文件 | 行号 | 问题 | +|---|------|------|------| +| L22 | `app.dart` | 11-16 | `ColorScheme.fromSeed()` 在 build 中创建(实际仅一次,低影响) | +| L23 | `artwork_detail_page.dart` | 264-265 | `showDialog` 异常抛出时 TextEditingController 泄漏 | +| L24 | `online_manga_reader_page.dart` | 101-103 | 源 headers 直接传给 `Image.network`,第三方源可能捕获凭据 | +| L25 | `novel_reader_page.dart` | 78,330 | `Future.microtask` 在 widget 可能已 dispose 的上下文中执行 | +| L26 | `novels_tab.dart` | 446,461 | 时间戳兜底使用 epoch 0 → 显示"53 年前" | +| L27 | `explore_page.dart` | 186-222 | `_openItem` 从 provider 同步读取 source,可能搜完源已被删除 | +| L28 | `source_manager.dart` | 86-96 | `initializeIfFirstRun()` 的 initialized flag 在可能失败的 `load()` 之后无条件设为 true | +| L29 | `source_manager.dart` | 242-260 | 从 JSON 导入时,源的 id 由 name+URL 的 codeUnit 求和生成——不同源可能碰撞 | +| L30 | `legado_engine.dart` | 167-200 | `_encodeKeyword` 对非 UTF-8/GBK 编码的平台通道调用无超时 | + +--- + +## 五、跨模块问题汇总 + +| 问题类别 | 涉及模块 | 严重程度 | +|---------|---------|:--------:| +| **凭据明文存储** | 桌面端 + 5 个插件 + Flutter | Critical/High(7 处) | +| **HTTP 明文传输** | 桌面端 API + Flutter API | Critical/High(2 处) | +| **默认密码/密钥** | Cloud config(JWT、DB、MinIO、Redis) | Critical/High(4 处) | +| **无限流** | Cloud 全部用户端点 | High(17+ 端点) | +| **存储配额未执行** | Cloud gallery + sync | High(2 处) | +| **静默吞错 → 空列表** | Flutter + 桌面 downloader + session | High/Medium(10+ 处) | +| **线程安全** | 桌面 ProxyPool + SQLite + counter | Critical/Medium(3 处) | +| **资源泄漏** | 桌面 DB 连接 + Flutter Dio × translate + X 插件 browser | High/Medium(3 处) | +| **Token 生命周期** | Cloud auth(logout、refresh、expiry、version) | High/Medium(5 处) | +| **插件安全** | `--no-sandbox` + 硬编码密钥 + zip bomb + URL 路径穿越 | Critical/Medium(5 处) | +| **Flutter 状态管理** | FutureBuilder 模式 + async 构造器 + provider 初始化 | High/Medium(5 处) | + +--- + +## 六、修复优先级建议 + +### 第一轮(立即修复,1–3 天) + +| 顺序 | ID | 问题 | 工时 | +|:----:|:---|------|:----:| +| 1 | C1,C4,C5 | Cloud 默认凭据:JWT 密钥 + DB 密码 + MinIO 凭据 | 1h | +| 2 | C3,H18 | 移除 `--no-sandbox`(小红书 + X 插件) | 1h | +| 3 | C6,H13 | HTTP → HTTPS(桌面端 + Flutter) | 2h | +| 4 | C9 | ProxyPool 多线程数据竞争(加锁) | 2h | +| 5 | C7 | Firebase 初始化 flag 移到成功之后 | 10m | +| 6 | C2 | 笔趣阁 AES 密钥改为随机 IV + 安全随机密钥 | 2h | +| 7 | H1,H2,H17 | Cloud 端全局速率限制 | 4h | +| 8 | H3 | 存储配额强制执行 | 2h | + +### 第二轮(本周内,3–5 天) + +| 顺序 | ID | 问题 | 工时 | +|:----:|:---|------|:----:| +| 9 | H4,H5 | Token 撤销(logout + refresh rotation) | 4h | +| 10 | H16 | 云同步 O(N) DB 连接改为复用连接 | 2h | +| 11 | H15 | SQLite 跨线程连接池化 | 3h | +| 12 | H7 | 小说翻页 API 增加 500ms 防抖 | 1h | +| 13 | H8 | 探索页"插画→小说"模式修复 | 1h | +| 14 | C8 | gallery_tab `recentUpdatesOnly` 死开关修复 | 1h | +| 15 | H19 | 关注作者同步 N+1 查询优化 | 3h | +| 16 | H20 | 多处静默吞错 → 显式错误状态 UI | 4h | +| 17 | M8 | GET novel 路径解析触发的 DB 写操作改为 lazily 计算 | 2h | + +### 第三轮(下一迭代) + +| 顺序 | 类别 | 内容 | 工时 | +|:----:|------|------|:----:| +| 18 | 凭据加密 | 7 处明文凭据改为 OS 密钥链存储 | 8h | +| 19 | CSP | CSP 策略加固(nonce/hash 替代 unsafe-inline) | 4h | +| 20 | 测试 | 为 Critical/High 项编写回归测试 | 8h | +| 21 | Flutter 重构 | AsyncNotifier 替代 FutureBuilder(feed_tab, gallery_tab, novels_tab) | 8h | +| 22 | 插件审计 | 对全部 5 个插件的网络/文件访问权做沙箱化评估 | 4h | + +--- + +> **审核范围确认:** Flutter 移动端 30 个 Dart 文件 × Python 桌面端 16 个文件 × Cloud 后端 10 个端点文件 × 5 个插件 × 中间件/配置/安全模块。合计约 65 个源文件,发现 111 个问题。 diff --git a/docs/优化方案与功能规划.md b/docs/优化方案与功能规划.md new file mode 100644 index 0000000..6b3154b --- /dev/null +++ b/docs/优化方案与功能规划.md @@ -0,0 +1,507 @@ +# 樱云下载器 — 优化方案与功能规划 + +> 版本:2.1.0-draft | 日期:2026-06-24 +> 基于代码审计报告、产品文档、CHANGELOG 及全仓库源码分析制定 + +--- + +## 目录 + +1. [优先级定义](#0-优先级定义) +2. [P0 — 安全与基础设施(必须修)](#1-p0--安全与基础设施) +3. [P1 — 核心体验缺陷(尽快修)](#2-p1--核心体验缺陷) +4. [P2 — 功能完善与增强](#3-p2--功能完善与增强) +5. [P3 — 体验优化与锦上添花](#4-p3--体验优化与锦上添花) +6. [P4 — 长期架构演进](#5-p4--长期架构演进) +7. [实施路线图](#6-实施路线图) + +--- + +## 0. 优先级定义 + +| 等级 | 含义 | 建议时间 | +|------|------|---------| +| **P0** | 安全漏洞 / 数据风险 / 系统崩溃 | 1 周内 | +| **P1** | 核心功能阻断或严重缺陷 | 2–4 周 | +| **P2** | 功能缺口 / 重要增强 | 1–2 月 | +| **P3** | 体验优化 / 锦上添花 | 按需排期 | +| **P4** | 架构演进 / 平台扩展 | 中长期规划 | + +--- + +## 1. P0 — 安全与基础设施 + +> 来源:代码审计报告 Critical 项 + 其他发现 + +### 1.1 JWT 密钥强制生产配置 + +- **现状**:`sakura_cloud/core/config.py` 中 `jwt_secret_key` 默认值为 `"change-this-secret-in-production"`,任何人都可伪造 JWT +- **方案**:启动时检测是否为已知默认值,若是则拒绝启动并输出明确的错误信息,引导运维设置 `JWT_SECRET_KEY` 环境变量 +- **影响范围**:`sakura_cloud/core/config.py`、`sakura_cloud/core/security.py` + +### 1.2 数据库凭据与 MinIO 凭据硬编码 + +- **现状**: + - PostgreSQL 默认凭据 `sakura:sakura` + - MinIO 默认凭据 `minioadmin:minioadmin` +- **方案**:同 JWT 策略——启动时检测默认值,若未被覆盖则拒绝启动;为 MinIO 增加随机密码生成脚本 +- **影响范围**:`sakura_cloud/core/config.py`、部署文档 + +### 1.3 桌面端 → 云端启用 HTTPS + +- **现状**:`DEFAULT_CLOUD_API = "http://api.kunkunxiaomao.top:8000"` 使用明文 HTTP,用户凭据和文件明文传输 +- **方案**: + - 短期:支持 HTTP 但增加显式警告弹窗("当前使用非加密连接,凭据可能被窃取") + - 长期:服务器配置 SSL 证书(Let's Encrypt / 自签名),客户端默认改为 HTTPS,初次连接时做证书校验引导 +- **影响范围**:`pixiv_app/gui/app.py`、`pixiv_app/core/cloud_sync.py`、云端 nginx/Caddy 配置 + +### 1.4 登录接口限流 + +- **现状**:`/api/v1/auth/login` 无限流,可暴力破解 +- **方案**:对 `/login`、`/register`、`/token/refresh` 增加 IP 级别速率限制(如每分钟 5 次),使用 Redis 或内存计数器实现 +- **影响范围**:`sakura_cloud/api/v1/endpoints/auth.py`、新增限流中间件 + +### 1.5 笔趣阁插件 AES 密钥随机化 + +- **现状**:`plugins/biquge/plugin.py` 中 AES 密钥和 IV 从硬编码字符串经 MD5 派生,固定 IV 下的 CBC 模式可被已知明文攻击 +- **方案**: + - 每次加密使用随机 IV(`os.urandom(16)`),与密文一起传输 + - 密钥从用户配置或安全随机源派生,而非硬编码 +- **影响范围**:`plugins/biquge/plugin.py` + +### 1.6 Chrome `--no-sandbox` 移除 + +- **现状**:小红书插件使用 `--no-sandbox` 禁用 Chromium 沙箱,浏览器以完整 OS 权限运行不受信任的外部内容 +- **方案**:移除 `--no-sandbox`。若在 Docker/root 环境下必须使用,改为配置 `--cap-add=SYS_ADMIN` 或 seccomp profile +- **影响范围**:`plugins/xiaohongshu/plugin.py` + +### 1.7 Firebase 初始化竞态修复 + +- **现状**:`push_notifications.dart` 中 `_initialized = true` 在 `await Firebase.initializeApp()` **之前**设置,若初始化失败则永久不再重试 +- **方案**:将 `_initialized = true` 移到 `await Firebase.initializeApp()` 成功之后 +- **影响范围**:`sakura_mobile/lib/src/core/push_notifications.dart` + +### 1.8 ProxyPool 多线程数据竞争 + +- **现状**:`current_index` 无锁读写、`working_proxies` 并发修改、`fail_count`/`success_count` 非原子递增 +- **方案**:引入 `threading.Lock` 保护所有对共享数据的写操作;或将 `current_index` 改为 `itertools.cycle` + 原子计数器 +- **影响范围**:`pixiv_app/core/proxy_pool.py` + +### 1.9 敏感文件 `.gitignore` 完善 + +- **现状**:`.env.cloud.example` 已提供模板,但不清楚 `.env` 是否在 `.gitignore` 中;会话文件 `app_session.json` 可能被误提交 +- **方案**:在 `.gitignore` 中显式添加 `*.env`(除 `.example`)、`app_session.json`、`runtime/`、`*.db`、`*.key` +- **影响范围**:`.gitignore` + +--- + +## 2. P1 — 核心体验缺陷 + +> 来源:代码审计报告 High/Medium 项 + 已知 bug + 功能缺口 + +### 2.1 手机端 `recentUpdatesOnly` 死开关修复 + +- **现状**:`gallery_tab.dart` 中 `recentUpdatesOnly` 状态变量只控制 Chip 高亮,从未作为 API 参数传入 `_load()`,切换无实际效果 +- **方案**:在 `_load()` 中将 `recentUpdatesOnly` 转换为 `order=updated_at` 或后端新增 `recent_only` 查询参数;或如果后端暂时不支持,先移除该 Chip +- **影响范围**:`sakura_mobile/lib/src/pages/tabs/gallery_tab.dart` + +### 2.2 Legado 书源搜索引擎完善 + +- **现状**:6 个默认种子源 `searchUrl` / `searchListRule` 全为 null,CSS 选择器解析不支持 class/ID/属性选择器,GBK 编码无容错 +- **方案**(分两步): + - **2.2a 引擎层**:扩展 CSS 选择器支持 class(`.classname`)、ID(`#id`)、属性(`[attr=val]`)、后代(`A B`)选择器;GBK 解码增加容错参数 + - **2.2b 源数据层**:用真实可用的书源 JSON 替换默认种子源,或提供从 Legado 仓库一键导入的引导流程 +- **影响范围**:`legado_rule_parser.dart`、`legado_engine.dart`、`source_manager.dart` + +### 2.3 手机端发现页滚动卡死修复 + +- **现状**:发现页 Feed 向上滚动时可能卡在底部 +- **方案**:排查 `feed_tab.dart` 中的 `ScrollController` 和分页加载逻辑,确保加载更多后正确更新 `hasMore` 状态,以及 `ListView` 的 `addPostFrameCallback` 不产生死循环 +- **影响范围**:`sakura_mobile/lib/src/pages/tabs/feed_tab.dart` + +### 2.4 Web 管理后台页面完善 + +- **现状**:普通用户 Web 端详情页已从占位改为真实加载,但管理后台可能仍有死角 +- **方案**: + - 完成管理员端用户管理、内容审核、统计面板页面 + - 普通用户端确保图片/漫画/小说三种内容类型均有完整的列表+详情页 +- **影响范围**:`sakura_admin_web/` + +### 2.5 云同步上传失败队列与断点续传 + +- **现状**:`SAKURA_CLOUD_V1_STATUS.md` 明确标注"上传失败队列与断点恢复细节"尚未完成 +- **方案**: + - 桌面端新增 `sync_failures` 表记录上传失败的文件,支持自动/手动重试 + - 上传分片支持断点续传(保存已上传分片索引) + - 云端 `upload/complete` 接口增加分片校验 +- **影响范围**:`pixiv_app/core/cloud_sync.py`、`sakura_cloud/api/v1/endpoints/upload.py` + +### 2.6 Alembic 数据库迁移完善 + +- **现状**:SAKURA_CLOUD_V1_STATUS 标注 Alembic 迁移尚未完成 +- **方案**:补全所有模型的 Alembic migration 脚本,确保 `alembic upgrade head` 一键建库;新增 CI 中检查迁移一致性 +- **影响范围**:`sakura_cloud/migrations/` + +### 2.7 手机端 App 正式签名 + +- **现状**:APK 使用调试签名,不适合正式分发;README 和用户手册均强调此限制 +- **方案**: + - 生成正式 keystore + - 更新 `sakura_mobile/android/app/build.gradle.kts` 签名配置 + - 文档更新,区分 debug/release 构建流程 +- **影响范围**:`sakura_mobile/android/`、构建脚本 + +### 2.8 桌面端 Cookie 过期自动检测与提醒 + +- **现状**:Pixiv Cookie 过期后抓取静默失败,用户需手动排查 +- **方案**: + - 在每次抓取前自动检测会话有效性(已有「检测会话」按钮逻辑,改为自动调用) + - Cookie 将过期或已过期时弹窗提醒并引导重新导入 +- **影响范围**:`pixiv_app/core/auth.py`、`pixiv_app/gui/app.py` + +### 2.9 下载失败自动重试与错误分类 + +- **现状**:`runtime/executor.py` 已有基本重试与错误分类,但未区分限流(429)、临时故障(5xx)、权限错误(403/404) +- **方案**: + - 429 → 自动退避重试(指数退避 + 抖动) + - 5xx → 有限重试(最多 2 次) + - 403/404 → 不重试,直接标记失败 + - 失败原因写入 `download_tasks` 表便于排查 +- **影响范围**:`pixiv_app/runtime/executor.py`、`pixiv_app/tasks/models.py` + +### 2.10 图库搜索性能优化 + +- **现状**:图库 SQLite 查询在大数据量下(数万条作品)可能变慢,标签模糊搜索使用 `LIKE '%keyword%'` 无法走索引 +- **方案**: + - 对 `artworks.title`、`artists.name`、`tags.name` 建立索引 + - 标签搜索改为 FTS5 全文索引 + - 云端的 `gallery` 查询为 `include_total` 参数已有优化,进一步对 `artworks.created_at`、`artworks.artist_id` 建立联合索引 +- **影响范围**:`pixiv_app/core/library.py`、`sakura_cloud/models/artwork.py` + +--- + +## 3. P2 — 功能完善与增强 + +### 3.1 手机端推送通知(FCM) + +- **现状**:Firebase 已集成但推送链路不完整;当前"每日提醒"仅是应用内摘要,非系统级推送 +- **方案**: + - 完成 FCM token 注册与服务端对接 + - 云端提醒生成后通过 FCM 发送系统通知 + - 点击通知直接跳转到对应内容 + - 用户可配置"仅 Wi-Fi 下推送"、"免打扰时段" +- **影响范围**:`sakura_mobile/lib/src/core/push_notifications.dart`、`sakura_cloud/services/push.py` + +### 3.2 插件市场在线对接 + +- **现状**:`PluginMarketClient` 为占位类,仅支持本地文件系统发现插件 +- **方案**: + - 云端提供 `/api/v1/plugins/` 端点(列表、下载、版本检查) + - 桌面端「插件管理」面板增加"在线市场"标签页 + - 支持一键安装/更新/卸载 + - 插件版本兼容性检查 +- **影响范围**:`pixiv_app/core/plugin/market.py`、`pixiv_app/gui/plugin_panel.py`、`sakura_cloud/` 新增端点 + +### 3.3 视频下载与播放支持 + +- **现状**:X/Twitter 插件可抓取视频文件,但本地图库和缩略图墙未针对视频做优化;手机端无视频播放页 +- **方案**: + - 缩略图墙增加视频缩略图(自动截取首帧或使用平台提供的 thumbnail) + - 手机端新增视频播放页(基于 `video_player` 插件) + - 图库列表支持视频类型筛选 +- **影响范围**:`pixiv_app/core/gallery_service.py`、`sakura_mobile/` 新增页面 + +### 3.4 阅读进度跨设备同步 + +- **现状**:手机端小说阅读器记录阅读进度,但仅本地存储(`online_reading_history.dart`) +- **方案**: + - 云端新增 `/api/v1/reading-progress/` 端点(GET/PUT) + - 手机端在进度变更时自动上报云端 + - 桌面端缩略图墙也可展示阅读进度 + - 多设备间同账号自动同步阅读位置 +- **影响范围**:`sakura_mobile/lib/src/core/online_reading_history.dart`、`sakura_cloud/` 新增端点 + +### 3.5 内容合集 / 专辑功能 + +- **现状**:作品可以按作者、标签归类,但没有用户自定义合集的能力 +- **方案**: + - 桌面端和手机端均支持创建"合集",将任意作品(图片/漫画/小说)归入合集 + - 合集支持排序、封面设置、备注 + - 云端同步合集数据 +- **影响范围**:`pixiv_app/core/library.py`(新增表)、`sakura_cloud/models/`(新增模型)、`sakura_mobile/` 新增页面 + +### 3.6 漫画连续阅读模式增强 + +- **现状**:手机端已有漫画分组与连续阅读,但体验可以更流畅 +- **方案**: + - 左右滑动翻页(当前可能仅支持上下滚动) + - 预加载下一页/上一页(当前页 ± 2 页) + - 阅读进度条(当前页/总页数) + - 双击缩放、长按保存 +- **影响范围**:`sakura_mobile/lib/src/pages/online_manga_reader_page.dart` + +### 3.7 批量导出功能 + +- **现状**:用户可通过缩略图墙浏览,但无法批量导出原图 +- **方案**: + - 缩略图墙增加"批量选择 → 导出原图"功能 + - 支持按作者/标签/收藏筛选后全选导出 + - 导出格式:原图 ZIP 包或保持目录结构 +- **影响范围**:`pixiv_app/core/gallery_service.py`、缩略图墙前端模板 + +### 3.8 X (Twitter) 作者时间线监控 + +- **现状**:X 插件支持抓取作者媒体和单个推文,但缺少"监控作者新推文"的主动检测 +- **方案**: + - 桌面端新增"X 监控"面板,可添加监控的 X 用户名 + - 定时轮询作者时间线,检测新推文 → 自动下载 → 写入云端关注提醒 + - 与现有每日提醒系统打通 +- **影响范围**:`plugins/x/`、`pixiv_app/core/x_media_service.py` + +### 3.9 下载内容去重增强 + +- **现状**:`download_tasks` 表有 `fingerprint` 唯一约束,`artwork_files.content_hash` 字段已预留但默认空字符串 +- **方案**: + - 下载完成后计算文件 SHA256 并写入 `content_hash` + - 新下载前先检查 `content_hash` 是否已存在,跳过重复文件 + - 缩略图墙可展示"重复文件"列表,支持清理 +- **影响范围**:`pixiv_app/core/downloader.py`、`pixiv_app/core/library.py` + +### 3.10 代理池健康度评分 + +- **现状**:代理有 `fail_count` / `success_count` 但仅用于简单标记失败 +- **方案**: + - 引入代理健康度评分(成功率、响应延迟、可用性百分比) + - 代理按健康度排序,优先使用高分代理 + - 健康度过低的代理自动移出池 + - 代理池 UI 展示健康度统计 +- **影响范围**:`pixiv_app/core/proxy_pool.py` + +--- + +## 4. P3 — 体验优化与锦上添花 + +### 4.1 桌面端深色模式 + +- **现状**:GUI 锁定 `ctk.set_appearance_mode("light")` +- **方案**:增加"外观"设置项——浅色 / 深色 / 跟随系统;`customtkinter` 原生支持 `dark` 模式,改动极小 +- **影响范围**:`pixiv_app/gui/app.py` + +### 4.2 缩略图墙 UI 现代化 + +- **现状**:缩略图墙是本地 WebUI(Jinja2 模板),功能齐全但 UI 风格较为基础 +- **方案**: + - 引入轻量 CSS 框架(如 Pico.css 或 Water.css)美化 + - 支持网格/列表视图切换 + - 图片懒加载(当前可能一次性加载所有缩略图) + - 虚拟滚动(大量作品时性能优化) +- **影响范围**:`pixiv_app/core/gallery_service.py`、模板文件 + +### 4.3 手机端首页个性化配置 + +- **现状**:发现页展示"最近浏览的图片/漫画/小说"和"更新提醒" +- **方案**: + - 用户可选择发现页展示哪些模块(图片/漫画/小说/提醒) + - 支持拖拽排序模块 + - "继续阅读"模块(直接从上次阅读位置进入) +- **影响范围**:`sakura_mobile/lib/src/pages/tabs/feed_tab.dart` + +### 4.4 下载速度与流量统计 + +- **现状**:桌面端有进度条但无历史统计 +- **方案**: + - 记录每次下载的文件大小、耗时 + - 主界面增加"本次会话下载量"和"总计下载量"统计 + - 可查看每日/每周下载量趋势图(可选,使用 matplotlib) +- **影响范围**:`pixiv_app/gui/app.py`、`pixiv_app/core/library.py` + +### 4.5 国际化(i18n)支持 + +- **现状**:GUI 和手机端 UI 均为中文硬编码 +- **方案**: + - 桌面端:提取所有 UI 字符串到 JSON 语言文件,支持中文/英文/日文 + - 手机端:使用 Flutter `intl` / `easy_localization` 实现多语言 + - 优先完成英文翻译以扩大用户群 +- **影响范围**:全局 UI + +### 4.6 错误日志收集与诊断 + +- **现状**:日志分散在 `日志/` 目录和终端输出,排查问题需手动翻找 +- **方案**: + - 桌面端增加"一键导出诊断包"按钮(日志 + 配置 + 数据库状态摘要,压缩为 ZIP) + - 手机端增加"上传诊断日志"功能 + - 云端增加错误聚合面板(按错误类型/时间统计) +- **影响范围**:`pixiv_app/`、`sakura_mobile/`、`sakura_cloud/` + +### 4.7 桌面端系统托盘与后台运行 + +- **现状**:关闭 GUI 窗口即退出程序 +- **方案**: + - 最小化到系统托盘(使用 `pystray` 或 `customtkinter` 配合系统托盘库) + - 托盘菜单:显示/隐藏窗口、开始/停止抓取、退出 + - 下载完成后托盘气泡提示 +- **影响范围**:`pixiv_app/gui/app.py` + +### 4.8 自动更新检查 + +- **现状**:用户需手动关注 releases 目录获取新版本 +- **方案**: + - 桌面端启动时检查云端最新版本号 + - 有新版本时提示下载,可选"自动下载 → 下次启动替换" + - 手机端同理(检查 APK 版本号) +- **影响范围**:`pixiv_app/gui/app.py`、`sakura_mobile/lib/src/app.dart`、`sakura_cloud/` 新增版本检查端点 + +--- + +## 5. P4 — 长期架构演进 + +### 5.1 桌面端从 CustomTkinter 迁移到更现代化的 GUI 框架 + +- **理由**:CustomTkinter 功能有限,深色模式、动画、富文本等支持较弱;Windows 11 下风格略显过时 +- **候选方案**: + - **Tauri (Rust + Web 前端)**:体积小、性能好、现代 UI,但需重写全部 Python 逻辑为 Rust 或通过 sidecar 通信 + - **Electron + Python 后端**:重用现有 Python 下载引擎,Electron 负责 UI,但体积大(~150MB+) + - **PySide6 / PyQt6**:纯 Python 方案,迁移成本最低,支持 QSS 样式和丰富控件 + - **Flet (Flutter for Python)**:用 Python 写 Flutter UI,可与现有 Flutter 移动端共享设计语言 +- **建议**:优先评估 **Flet**(与 Flutter 移动端风格统一、纯 Python)或 **PySide6**(迁移成本最低)。不宜急于重写,等 P0/P1 完成后评估 + +### 5.2 iOS 客户端 + +- **理由**:当前仅支持 Android,iOS 用户无法使用 +- **方案**: + - Flutter 跨平台特性使得 iOS 适配成本较低(大部分 Dart 代码可复用) + - 需额外处理:iOS 文件系统权限、后台任务限制、APNs 推送替代 FCM + - 需要 Apple Developer Program 账号($99/年)用于签名分发 +- **影响范围**:`sakura_mobile/ios/`(生成)、`sakura_mobile/lib/`(少量平台适配) + +### 5.3 Web 端 PWA(渐进式 Web 应用) + +- **理由**:桌面端 EXE 体积大(~68MB),部分用户只希望轻量浏览 +- **方案**: + - 将缩略图墙升级为 PWA(Service Worker + 离线缓存 + 可安装到桌面) + - 或单独开发 `sakura_web` 前端项目替代当前 `sakura_admin_web` +- **影响范围**:`sakura_admin_web/` 或新建 `sakura_web/` + +### 5.4 分布式爬虫完善 + +- **现状**:`distributed_crawler/` 目录有 Master-Worker 架构但尚不完整 +- **方案**: + - 补全 Worker 节点注册、心跳、任务分配 + - Master 面板(Web UI)展示各 Worker 状态和任务进度 + - Docker Compose 一键部署 Master + N Workers +- **影响范围**:`distributed_crawler/` + +### 5.5 AI 辅助功能 + +- **理由**:PaddleOCR 已集成,可在其基础上扩展更多 AI 能力 +- **方案**: + - **AI 标签自动生成**:对下载的图片使用轻量视觉模型(如 MobileNet/EfficientNet)自动打标签 + - **翻译增强**:手机端小说翻译当前仅支持"当前页翻译",可升级为整本翻译 + 双语对照 + - **图片去重**:基于感知哈希(pHash/dHash)的相似图片检测 + - **内容安全扫描**:对上传至云端的内容进行自动安全审核(NSFW 检测) +- **影响范围**:`pixiv_app/core/ocr.py`、`sakura_cloud/services/ocr.py` + +### 5.6 多用户/家庭共享 + +- **现状**:云同步为单用户设计,无分享功能 +- **方案**: + - 云端新增"家庭组"/"共享空间"概念 + - 用户可邀请其他账号加入共享空间 + - 共享空间内的内容(图库/小说/漫画)成员均可访问 + - 权限控制:管理员/编辑/只读 +- **影响范围**:`sakura_cloud/` 新增模型与端点、手机端新增分享页面 + +--- + +## 6. 实施路线图 + +### 第一阶段:安全加固(第 1–2 周) + +``` +P0-1 JWT 密钥强制生产配置 ████████░░░░░░░░ 1d +P0-2 数据库/MinIO 凭据强制覆盖 ████████░░░░░░░░ 1d +P0-3 HTTPS 支持 + 警告 ████████████░░░░ 1.5d +P0-4 登录接口限流 ████████░░░░░░░░ 1d +P0-5 笔趣阁 AES 随机化 ████░░░░░░░░░░░░ 0.5d +P0-6 Chrome --no-sandbox 移除 ████░░░░░░░░░░░░ 0.5d +P0-7 Firebase 初始化竞态修复 ████░░░░░░░░░░░░ 0.5d +P0-8 ProxyPool 线程安全 ████████░░░░░░░░ 1d +P0-9 .gitignore 完善 ████░░░░░░░░░░░░ 0.5d +──────────────────────────────────────────────────────── + 小计约 7.5 人天 +``` + +### 第二阶段:体验修复(第 3–6 周) + +``` +P1-1 手机端死开关修复 ████░░░░░░░░░░░░ 0.5d +P1-2 Legado 搜索引擎完善 ████████████████ 4d +P1-3 发现页滚动卡死 ████████░░░░░░░░ 2d +P1-4 Web 管理后台完善 ████████████░░░░ 3d +P1-5 上传失败队列 + 断点续传 ████████████████ 4d +P1-6 Alembic 迁移 ████████░░░░░░░░ 2d +P1-7 正式签名 ████░░░░░░░░░░░░ 1d +P1-8 Cookie 过期自动检测 ████████░░░░░░░░ 2d +P1-9 下载失败重试优化 ████████░░░░░░░░ 2d +P1-10 图库搜索性能 ████████████░░░░ 3d +──────────────────────────────────────────────────────── + 小计约 23.5 人天 +``` + +### 第三阶段:功能增强(第 7–14 周) + +``` +P2-1 FCM 推送通知 ████████████████ 5d +P2-2 插件市场在线对接 ████████████████ 5d +P2-3 视频下载与播放 ████████████░░░░ 4d +P2-4 阅读进度跨设备同步 ████████████░░░░ 4d +P2-5 内容合集/专辑 ████████████████ 5d +P2-6 漫画连续阅读增强 ████████░░░░░░░░ 3d +P2-7 批量导出 ████████████░░░░ 4d +P2-8 X 作者时间线监控 ████████████░░░░ 4d +P2-9 下载去重增强 ████████░░░░░░░░ 3d +P2-10 代理池健康度评分 ████████░░░░░░░░ 3d +──────────────────────────────────────────────────────── + 小计约 40 人天 +``` + +### 第四阶段及以后(按需排期) + +``` +P3-1 深色模式 ████░░ 1d +P3-2 缩略图墙 UI 现代化 ██████ 2d +P3-3 首页个性化 ██████ 2d +P3-4 下载统计 ████░░ 1.5d +P3-5 国际化 ██████ 3d +P3-6 错误诊断包 ██████ 2d +P3-7 系统托盘 ██████ 2d +P3-8 自动更新 ██████ 2d +P4-X 长期架构演进 TBD +──────────────────────────────────────────────────────── + 小计约 15.5 人天 +``` + +### 关键里程碑 + +| 里程碑 | 时间 | 交付物 | +|--------|------|--------| +| **M1: 安全基线** | 第 2 周末 | 所有 P0 项修复完成,安全审计复测通过 | +| **M2: 体验基线** | 第 6 周末 | 所有 P1 项修复完成,核心链路无阻断 | +| **M3: 功能补全** | 第 14 周末 | P2 主要项完成,推送/插件市场/跨设备进度同步可用 | +| **M4: 体验升级** | 第 20 周末 | P3 项完成,深色模式/国际化/自动更新上线 | + +--- + +## 附录:已完成功能清单(便于对照) + +| 模块 | 已具备能力 | +|------|-----------| +| **桌面端** | 多平台抓取、Cookie/密码登录、插件系统、本地图库 SQLite、缩略图墙 WebUI、云同步、代理池、任务队列 + 增量同步、OCR | +| **云端** | FastAPI REST API、JWT 认证、图库/小说/文件 CRUD、关注管理、每日提醒摘要、分片上传、健康检查 | +| **手机端** | 登录注册、发现/图片/漫画/书架/我的 五个标签页、小说阅读器(分页/收藏/翻译/进度)、漫画分组阅读、书源管理+Legado 引擎、在线搜索、FCM(部分) | +| **插件** | Pixiv、X/Twitter、小红书、笔趣阁、长佩文学、AO3 | +| **DevOps** | 一键构建 EXE/APK、Docker Compose 部署、Alembic 迁移 | + +--- + +*本文档由代码审计报告、产品文档、CHANGELOG、SAKURA_CLOUD_V1_STATUS 及全仓库源码分析综合生成,供开发排期与需求评审参考。* diff --git a/docs/四大问题完整修复方案.md b/docs/四大问题完整修复方案.md new file mode 100644 index 0000000..3bba8ce --- /dev/null +++ b/docs/四大问题完整修复方案.md @@ -0,0 +1,745 @@ +# 四大问题完整修复方案(终版) + +> 更新日期:2026-06-16 +> 范围:Sakura Mobile (Flutter) + Sakura Cloud (FastAPI) + Sakura Admin Web (Vue.js) + Desktop GUI (Python) +> 版本:v2.0 — 调整优先级为 P0/P1 两级、修正问题二的风险边界和工期、问题一拆为两层修复、问题四拆为开发/生产两套方案、问题三补搜索防抖 + +--- + +## 目录 + +1. [优先级总览](#零优先级总览) +2. [P0-1:无法搜索本地不存在的图库(源不可用)](#一p0-1无法搜索本地不存在的图库源不可用) +3. [P0-2:网页端显示 JSON 数据而非图片/小说/漫画](#二p0-2网页端显示-json-数据而非图片小说漫画) +4. [P1-1:APP 发现页向上滚动卡在底部](#三p1-1app-发现页向上滚动卡在底部) +5. [P1-2:导入源的位置排版有问题](#四p1-2导入源的位置排版有问题) +6. [实施计划](#五实施计划) + +--- + +## 零、优先级总览 + +| 等级 | 问题 | 严重程度 | 工期 | 风险 | +|:----:|------|:--------:|:----:|:----:| +| **P0-1** | 书源搜索不可用 | 核心功能阻断 | 3–5 天 | **高** | +| **P0-2** | Web 端显示 JSON / 无法展示内容 | 入口不可用 | 1–2 天 | 低 | +| **P1-1** | 发现页 Feed 滚动卡死 | 高频场景阻断 | 1–2 天 | 低 | +| **P1-2** | 书源管理页排版和性能 | 管理体验差 | 0.5–1 天 | 低 | + +**优先级判断依据:** +- 问题二是"在线探索"的整个功能链路不通,影响范围最大。 +- 问题四是 Web 端入口整体不可用,新用户访问会直接流失。 +- 问题一如果高频复现(每次打开发现页都卡),也接近阻断级,但只影响单页面。 +- 问题三是管理后台的体验优化,不影响核心浏览链路。 + +--- + +## 一、P0-1:无法搜索本地不存在的图库(源不可用) + +### 1.1 问题现象 + +"在线探索"(ExplorePage)搜索漫画/小说时,所有已启用的书源永远不返回结果。无论是否从仓库导入源,`legado_engine.search()` 都静默返回 `[]`,UI 显示"暂无搜索结果"。 + +### 1.2 根因分析 + +**审查文件(6 个):** `source_manager.dart`、`legado_engine.dart`、`legado_models.dart`、`legado_rule_parser.dart`、`unified_search.dart`、`explore_page.dart` + +#### 根因 1(阻断级):默认种子源 URL = `example.invalid`,且 searchUrl/searchListRule 全空 + +**文件:** `source_manager.dart` 第 274–317 行 + +```dart +LegadoSource( + name: '笔趣阁', + url: 'https://example.invalid/novel/biquge', // IANA 保留测试域名 + type: 0, enabled: true, + // searchUrl、searchListRule —— 全部 null +), +``` + +`LegadoEngine.search()` 入口(第 36–39 行): + +```dart +if (searchUrl == null || listRule == null) { + return const []; // 静默短路 +} +``` + +六个默认种子源全都没有搜索规则。不论用户是否"启用",搜索入口直接返回空。这些种子源是从设计之初就存在的占位符,但从未被替换为可用源。 + +#### 根因 2(阻断级):CSS 选择器仅支持 `...` 简单正则,不支持 class/ID/属性/后代 + +**文件:** `legado_rule_parser.dart` 第 86–96 行 + +```dart +List _cssFragments(String selector) { + final tag = _selectorTag(selector); // 只取开头 a-zA-Z 部分作 tag + if (tag == null) return const []; + final pattern = RegExp('<$tag\\b[^>]*>.*?', ...); + return pattern.allMatches(body)...; +} +``` + +Legado 源仓库中 95%+ 的规则使用 class 选择器(`.book-list > .item`),当前引擎完全无法解析。 + +#### 根因 3(阻断级):GBK 编码 HTML 抛 `FormatException` + +**文件:** `legado_engine.dart` 第 216–220 行 + +```dart +if (charset == 'gbk' || charset == 'gb2312' || ...) { + return gbk.decode(bytes); // ← 无容错参数,遇非 GBK 字节直接抛异常 +} +``` + +`utf8.decode` 使用了 `allowMalformed: true`,但 `gbk.decode` **没有容错**。大量声称 charset=gbk 的页面实际混入了非 GBK 字节(emoji、广告注入的特殊符号等),导致整个 `_fetchBody` 失败。 + +#### 根因 4:`_ruleType` 忽略传入的 `kind` 参数 + +**文件:** `legado_rule_parser.dart` 第 130–134 行 + +`selectList` 和 `select` 虽然接受 `kind` 参数,但传给 `_ruleType` 的逻辑有断裂——`_ruleType` 方法签名不接受 `kind`,只根据规则文本前缀判断类型。如果 `source.ruleKind == 'xpath'` 但规则文本不以 `xpath:` 开头,会错误地按 CSS 解析。 + +#### 根因 5:仓库 URL 在国内网络不可达 + 首次启动静默失败 + +`legado.aoaostar.com`、`gcore.jsdelivr.net`、`git.nyne.dev` 均可能被 DNS 污染或不可达。`initializeIfFirstRun()` 首次启动自动拉仓库,12 秒超时后静默失败,用户无法感知问题所在。 + +#### 根因 6:搜索结果 DOM 解析在 UI 线程 + +每条结果调一次 `html_parser.parse(fragment)`,搜索结果 20 条 × 6 源 = 120 次 DOM 解析。低端机上会造成 frame drop。 + +### 1.3 解决方案 + +**⚠️ 法律与风险声明:** 问题二的本质是半个解析引擎重构 + 第三方内容源集成。涉及版权法律风险、源站反爬策略、编码兼容性和解析性能。以下方案在技术层面给出修复路径,**实际实施前需要产品侧确认法律边界和使用条款**。 + +#### 方案 A:删除占位种子源,首次启动不做任何自动导入 + +**修改文件:** `source_manager.dart` + +**策略——客户端不内置任何第三方书源仓库、书源 URL 或规则:** + +```dart +Future initializeIfFirstRun() async { + await load(); + final prefs = await SharedPreferences.getInstance(); + if (prefs.getBool(_initializedKey) == true) return; + + // 不做任何自动导入。首次启动源列表为空。 + await prefs.setBool(_initializedKey, true); + notifyListeners(); +} +``` + +删除 `_seedDefaultSources()` 中的 6 个 `example.invalid` 占位源。首次启动后源列表为空,UI 显示引导入口: + +> **当前没有可用书源。** +> 书源规则是第三方网站的内容解析配置,请从你信任的仓库导入,或手动添加你有使用权限的书源。 +> [从内置仓库导入] [从 URL 导入] + +**不内置任何仓库索引 JSON。** 仓库列表仅作为 UI 入口的快捷链接提供,不硬编码到客户端。 + +#### 方案 B:重写 CSS 选择器——引入 `html` 包 + Isolate 解析 + +**修改文件:** `legado_rule_parser.dart`、`pubspec.yaml` + +```yaml +dependencies: + html: ^0.15.5 +``` + +```dart +import 'package:html/parser.dart' as html_parser; + +List _cssFragments(String selector) { + try { + final doc = html_parser.parse(body); + final elements = doc.querySelectorAll(selector); + return elements.map((el) => el.outerHtml).where(_notEmpty).toList(); + } on Object { + return _legacyCssFragments(selector); // 正则降级 + } +} + +String _cssText(String fragment, String selector) { + final attrMatch = RegExp(r'@([a-zA-Z0-9_-]+)$').firstMatch(selector); + final clean = selector.replaceFirst(RegExp(r'@([a-zA-Z0-9_-]+)$'), '').trim(); + try { + final el = html_parser.parse(fragment).querySelector(clean); + if (el == null) return ''; + if (attrMatch != null) return el.attributes[attrMatch.group(1)] ?? ''; + return el.text.trim(); + } on Object { + return _legacyCssText(fragment, selector); + } +} +``` + +对超过 20 条的搜索结果,在 `Isolate` 中批量解析: + +```dart +import 'dart:isolate'; + +Future> _parseBatch( + List<_SearchRawResult> raw, +) async { + if (raw.length < 20) return _parseInline(raw); + return Isolate.run(() => _parseInIsolate(raw)); +} +``` + +#### 方案 C:修复 GBK/GB18030 解码容错 + +**修改文件:** `legado_engine.dart` 第 202–223 行 + +```dart +String _decodeBody(List bytes, {String? declaredCharset, ...}) { + if (bytes.isEmpty) return ''; + final charset = (declaredCharset ?? _charsetFromContentType(contentType)) + ?.trim().toLowerCase(); + + if (charset == 'gbk' || charset == 'gb2312' || charset == 'gb18030' || charset == 'gbk-0') { + try { + return gbk.decode(bytes); + } on FormatException { + return latin1.decode(bytes, allowInvalid: true); // 降级,保留可读部分 + } + } + + if (charset == 'latin1' || charset == 'iso-8859-1' || charset == 'windows-1252') { + return latin1.decode(bytes, allowInvalid: true); + } + + try { + return utf8.decode(bytes, allowMalformed: true); + } on FormatException { + return latin1.decode(bytes, allowInvalid: true); + } +} +``` + +#### 方案 D:修复 `_ruleType`——优先使用传入的 `kind` + +```dart +String _resolveKind(String rule, String? explicitKind) { + if (explicitKind != null && explicitKind.isNotEmpty) return explicitKind; + final lower = rule.toLowerCase(); + if (lower.startsWith('json:') || lower.startsWith(r'$.')) return 'json'; + if (lower.startsWith('regex:')) return 'regex'; + return 'css'; +} +``` + +调用方改为 `_resolveKind(normalized, kind)` 替代原来的 `kind ?? _ruleType(normalized)`。 + +#### 方案 E:多源异常隔离——单源失败不拖垮全局 + +**修改文件:** `legado_engine.dart` `searchAll()` 方法 + +当前实现已经是 `.catchError` 静默跳过,但错误信息被丢弃。改为: + +```dart +Future.wait(batch.map((source) => + search(source, keyword, page: page) + .timeout(perSourceTimeout) + .then((items) => items) + .catchError((error) => [ + UnifiedSearchResult.error( + sourceId: source.id, + sourceName: source.name, + errorMessage: _shortError(error), + ), + ]), +)); +``` + +错误结果显示在 UI 中(如 `"笔趣阁 - 请求超时"`),用户可以看到哪些源失败了。 + +#### 方案 F:仓库镜像轮询 + 网络探测 + +为 5 个仓库 URL 各提供 2–3 个镜像域名。`importFromRepo()` 依次尝试,全部失败则提示用户手动输入 URL。 + +--- + +## 二、P0-2:网页端显示 JSON 数据而非图片/小说/漫画 + +### 2.1 问题现象 + +浏览器访问 Sakura Cloud 后端或 Admin Web 时,页面显示原始 JSON(`{"items": [...]}`),而非渲染好的图片网格、小说内容、漫画翻页器。 + +### 2.2 根因分析 + +**审查文件:** `sakura_cloud/main.py`、`sakura_cloud/api/middleware.py`、`sakura_admin_web/`、`pixiv_app/services/gallery_api.py` + +#### 根因 1(阻断级):FastAPI 未注册 CORS 中间件 + +**文件:** `sakura_cloud/main.py` + +```python +app = FastAPI(...) +# ← 缺少 app.add_middleware(CORSMiddleware, ...) +``` + +Admin Web 开发时运行在 `localhost:5174`,后端在 `localhost:8000`。浏览器跨域请求被拦截,前端连 API 都调不通。CSP 中间件(`middleware.py`)处理的是内容安全策略,**不替代 CORS**。 + +#### 根因 2(阻断级):Cloud 后端未挂载 SPA 静态文件 + +**文件:** `sakura_cloud/main.py` 第 61–78 行 + +`/admin` 路由返回的是硬编码占位 HTML,不是 `sakura_admin_web/dist/index.html` 构建产物。任何 `/app`、`/login` 路径返回 404 或触发认证检查。 + +#### 根因 3:Admin Web 缺少内容浏览 Vue 组件 + +- `UserDashboard.vue`:仅导航卡片,无实际内容页 +- `UserDetailPage.vue`:占位组件——"详情数据由现有 API 按需加载" +- 路由没有注册 `/gallery`、`/novels` 等内容浏览路由 + +#### 根因 4:CSP `img-src 'self'` 阻止外部图片 + +API 返回的 `thumbnail_url` 指向外部域名时,浏览器 CSP 引擎直接拦截。 + +### 2.3 解决方案 + +#### ⸻ 开发环境 ⸻ + +#### 方案 A:注册 CORS 中间件 + +**修改文件:** `sakura_cloud/main.py` + +```python +from fastapi.middleware.cors import CORSMiddleware + +app.add_middleware( + CORSMiddleware, + allow_origins=[ + "http://localhost:5174", + "http://127.0.0.1:5174", + "app://.", + ], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) +``` + +#### 方案 B:修复 CSP 中间件 + +**修改文件:** `sakura_cloud/api/middleware.py` + +```python +"img-src 'self' data: blob: https: http:;" +"connect-src 'self' http://localhost:* ws://localhost:*;" +``` + +#### 方案 C:开发环境可选挂载 Vue dist + +```python +# sakura_cloud/main.py — 开发环境可选 +import os +from fastapi.staticfiles import StaticFiles + +frontend_dist = os.path.join(os.path.dirname(__file__), "..", "sakura_admin_web", "dist") +if os.path.isdir(frontend_dist) and settings.debug: + assets = os.path.join(frontend_dist, "assets") + if os.path.isdir(assets): + app.mount("/assets", StaticFiles(directory=assets), name="assets") + + @app.get("/app/{full_path:path}") + async def spa_fallback(full_path: str = ""): + index = os.path.join(frontend_dist, "index.html") + return FileResponse(index) if os.path.isfile(index) else HTMLResponse("Not built") +``` + +**注意:不添加 API 路径自动重定向的中间件。** 这会导致 APP 的 Dio/浏览器 Extension 发起的 API 请求被误重定向。API 路径就让它返回 JSON——它是 REST API 的正确行为。前端 SPA 从 `/app` 入口加载后用 JS fetch 数据。 + +#### ⸻ 生产环境 ⸻ + +#### 方案 D:Nginx 托管 SPA + 反向代理 API + +**推荐的生产结构:** + +``` +Nginx +├── / → /var/www/sakura_admin_web/dist/index.html +├── /app → SPA fallback (try_files $uri /index.html) +├── /login → SPA fallback +├── /assets/ → 静态资源,expires 1y(Vite 产物带 hash) +└── /api/ → proxy_pass http://127.0.0.1:8000 +``` + +**Nginx 配置示例:** + +```nginx +server { + listen 80; + server_name admin.your-domain.com; + root /var/www/sakura_admin_web/dist; + index index.html; + + location /api/ { + proxy_pass http://127.0.0.1:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location /assets/ { + expires 1y; + add_header Cache-Control "public, immutable"; + } + + location / { + try_files $uri $uri/ /index.html; + } +} +``` + +FastAPI **不长期托管前端静态资源**,只负责 API。 + +#### 方案 E:新增内容浏览 Vue 组件 + +**新增文件:** + +``` +sakura_admin_web/src/views/app/ +├── GalleryBrowsePage.vue # 画廊浏览:grid + 分页 + 搜索筛选 +├── NovelReadPage.vue # 小说阅读:章节列表 + 正文渲染 +└── MangaViewerPage.vue # 漫画翻页:PageView 横向翻页 +``` + +路由注册: + +```typescript +// router/index.ts +{ path: '/app/gallery', component: () => import('@/views/app/GalleryBrowsePage.vue') }, +{ path: '/app/novels', component: () => import('@/views/app/NovelReadPage.vue') }, +{ path: '/app/manga/:id', component: () => import('@/views/app/MangaViewerPage.vue') }, +``` + +--- + +## 三、P1-1:APP 发现页向上滚动卡在底部 + +### 3.1 问题现象 + +发现页从底部向上回滚时卡住,须等待数据加载完或强制下拉刷新才能恢复。 + +### 3.2 根因分析 + +**审查文件:** `feed_tab.dart`(829 行) + +| 根因 | 位置 | 严重程度 | +|------|------|:--------:| +| `_ensureFutures()` 在 `build()` 中反复调用 | 第 64–66 行 | 高 | +| `_refresh()` 用 `setState` 归零所有 future → 布局高度剧烈跳动 | 第 53–62 行 | **阻断** | +| `_StatCard` 在列表末尾加载完成后高度变化 | 第 182–186 行 | 中 | +| 内嵌横向 `ListView` 与纵向 `ListView` 手势竞争 | 第 443–507 行 | 中(部分设备) | +| `ScrollController` 未显式管理生命周期 | 缺 | 低 | + +### 3.3 解决方案——拆为两层 + +#### 第一层:必须做(P1 范围) + +**1. `AsyncNotifier` 替代 `FutureBuilder` + 状态统一管理** + +当前 `_FeedTabState` 用 5 个散落的 `Future` + `FutureBuilder` 管理异步状态,刷新时 `setState(null)` 归零所有 future 再在下一帧重建——这个模式是滚动问题的根源。 + +改为用一个统一的 `FeedState` 对象: + +```dart +class FeedState { + final bool loading; + final List todayArtworks; + final List recentArtworks; + final List recentNovels; + final List> alerts; + final Map stat; + final String? error; +} +``` + +用 Riverpod `AsyncNotifier` 管理整个页面的数据加载。`build()` 只读取 `ref.watch(feedProvider)`,不调用 `_ensureFutures()`,不在 build 中创建 future。 + +**2. 显式 `ScrollController`** + +```dart +class _FeedTabState extends ConsumerState { + final ScrollController _scrollController = ScrollController(); + + @override + void dispose() { + _scrollController.dispose(); + super.dispose(); + } +} +``` + +外层 `ListView` 绑定此 controller。切换 tab 时通过 `_scrollController.hasClients` 检查位置有效性。 + +**3. `_StatCard` 固定高度** + +```dart +SizedBox( + height: 106, + child: _StatCard(stat: stat), +), +``` + +通过 `AnimatedSwitcher` 平滑过渡内容而非跳变布局。 + +**4. Refresh 状态统一管理** + +`RefreshIndicator.onRefresh` 调用 provider 的 `ref.invalidate(feedProvider)`,由一个入口触发重载,不再散射到 5 个独立的 `setState(null)` 调用。 + +#### 第二层:如果仍有横向列表抢手势(专项修复) + +仅当第一层修复后,在部分设备上仍出现"在对角线滑动时外层垂直滚动丢失"的问题时,才实施此层: + +对横向 `_ArtworkStrip` 的 `ListView` 外套方向判定: + +```dart +SizedBox( + height: 210, + child: RawGestureDetector( + gestures: { + HorizontalDragGestureRecognizer: + GestureRecognizerFactoryWithHandlers( + () => HorizontalDragGestureRecognizer(), + (instance) {}, + ), + }, + child: ListView.separated( + scrollDirection: Axis.horizontal, + physics: const ClampingScrollPhysics(), + ... + ), + ), +), +``` + +`HorizontalDragGestureRecognizer` 在竞技场中只响应水平方向的拖拽,对角滑动时垂直分量不会被它吃掉,外层垂直 ListView 正常响应。 + +**不推荐 `PageStorageKey` 解决手势竞争。** `PageStorageKey` 的职责是页面切换后恢复滚动位置(`PageStorage` 机制),它不参与手势竞技场裁决。两项是不同层面的问题,不应混用。 + +--- + +## 四、P1-2:导入源的位置排版有问题 + +### 4.1 问题现象 + +书源管理页布局混乱:搜索栏在导入区底部(须滑过所有卡片才能搜索)、已导入 2200+ 条后 `for` 循环展开所有 Card 导致卡顿、5 个仓库全部展开占用过多空间。 + +### 4.2 根因分析 + +**审查文件:** `source_manager_page.dart`(465 行) + +| 根因 | 位置 | +|------|------| +| 搜索栏放在仓库 + 导入卡片**之后** | 第 81–85 行 | +| `for` 循环展开全部源,无懒加载 | 第 102–129 行 | +| 5 个仓库全部展开,每个 ~120px | 第 376–413 行 | +| 缺少分段标题和视觉层次 | 全页 | + +### 4.3 解决方案 + +#### 方案 A:CustomScrollView + SliverAppBar 吸顶搜索栏 + +```dart +@override +Widget build(BuildContext context) { + final manager = ref.watch(sourceManagerProvider); + final filteredSources = _filterSources(manager); + + return Scaffold( + body: CustomScrollView( + slivers: [ + // 搜索栏吸顶 + SliverAppBar( + pinned: true, + title: const Text('书源管理'), + bottom: PreferredSize( + preferredSize: const Size.fromHeight(56), + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 8), + child: SearchBar( + hintText: '搜索已导入的书源', + leading: const Icon(Icons.search), + onChanged: onSearchChanged, // 带防抖 + ), + ), + ), + ), + + // 统计 + 维护 + 仓库 + 导入 —— 各 SliverToBoxAdapter + SliverToBoxAdapter(child: _StatsCard(manager: manager)), + SliverToBoxAdapter(child: _MaintenanceCard(...)), + SliverToBoxAdapter(child: _CollapsibleRepoSection(...)), + SliverToBoxAdapter(child: _CustomImportCard(...)), + + // 分段标题 + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 8, 16, 4), + child: Text('已导入 (${filteredSources.length})', ...), + ), + ), + + // 源列表 —— 懒加载 + SliverList.builder( + itemCount: filteredSources.length, + itemBuilder: (context, index) { + final source = filteredSources[index]; + return Card( + child: SwitchListTile( + value: source.enabled, + onChanged: (value) => + ref.read(sourceManagerProvider).setEnabled(source.id, value), + title: Text(source.name), + subtitle: Text('${source.isManga ? '漫画' : '小说'} - ${source.url}'), + secondary: Icon(source.isManga + ? Icons.menu_book_rounded + : Icons.auto_stories_rounded), + ), + ); + }, + ), + + if (filteredSources.isEmpty) + SliverToBoxAdapter( + child: const Padding( + padding: EdgeInsets.all(28), + child: Center(child: Text('还没有导入书源,请先导入一个书源仓库。')), + ), + ), + + const SliverToBoxAdapter(child: SizedBox(height: 28)), + ], + ), + ); +} +``` + +**不使用 `shrinkWrap: true` ——** `SliverList.builder` 本身就是懒加载,不需要这个参数。 + +#### 方案 B:搜索防抖 + +```dart +Timer? _debounce; + +void onSearchChanged(String value) { + _debounce?.cancel(); + _debounce = Timer(const Duration(milliseconds: 180), () { + setState(() { + filter = value; + }); + }); +} + +@override +void dispose() { + _debounce?.cancel(); + customUrl.dispose(); + super.dispose(); +} +``` + +180ms 防抖确保用户快速输入时不会每按一个键就触发一次全文过滤。 + +#### 方案 C:仓库列表改用 ExpansionTile + +```dart +class _CollapsibleRepoSection extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Card( + child: ExpansionTile( + title: const Text('内置书源仓库'), + subtitle: Text('${SourceManager.builtinRepos.length} 个仓库可用'), + initiallyExpanded: false, + children: [ + for (final repo in SourceManager.builtinRepos) + ListTile( + leading: const Icon(Icons.cloud_download_outlined), + title: Text(repo.name), + subtitle: Text(repo.description), + trailing: FilledButton.tonal( + onPressed: ..., + child: Text(repo.isSubscription ? '订阅' : '导入'), + ), + ), + ], + ), + ); + } +} +``` + +--- + +## 五、实施计划 + +### 阶段 1:P0 可用性修复(3–6 天) + +| 天数 | 任务 | 涉及文件 | +|:----:|------|----------| +| 1–3 | **问题二:书源搜索链路修复** | | +| | └ 方案 A:删除占位种子源,空列表首次启动 | `source_manager.dart` | +| | └ 方案 B:`html` 包重写 CSS 选择器 + Isolate | `legado_rule_parser.dart`、`pubspec.yaml` | +| | └ 方案 C:GBK/GB18030 编码异常处理 | `legado_engine.dart` | +| | └ 方案 D:`_ruleType` 优先使用 `kind` 参数 | `legado_rule_parser.dart` | +| | └ 方案 E:多源异常隔离(错误透传 UI) | `legado_engine.dart` | +| | └ 方案 F:仓库镜像轮询 + 网络探测 | `source_manager.dart` | +| | └ **测试:** 真实书源搜索回归(≥10 个不同源站点) | | +| 4–6 | **问题四:Web 端 JSON → 可视化内容** | | +| | └ 方案 A:CORS 中间件(10min) | `sakura_cloud/main.py` | +| | └ 方案 B:CSP 修复(10min) | `sakura_cloud/api/middleware.py` | +| | └ 方案 C:开发环境可选挂载 Vue dist | `sakura_cloud/main.py` | +| | └ 方案 D:Nginx 生产部署配置 | 新建 `sakura_admin_web/nginx.conf` | +| | └ 方案 E:GalleryBrowsePage + NovelReadPage + MangaViewerPage | `sakura_admin_web/src/views/app/` | +| | └ **测试:** 浏览器 `/app` → 图片网格 → 点击作品 → 大图 → 分页 | + +### 阶段 2:P1 体验稳定性修复(2–4 天) + +| 天数 | 任务 | 涉及文件 | +|:----:|------|----------| +| 1–2 | **问题一:Feed 滚动稳定性(第一层必做)** | | +| | └ AsyncNotifier 替代 FutureBuilder + 状态统一管理 | `feed_tab.dart` | +| | └ 显式 ScrollController + dispose | `feed_tab.dart` | +| | └ StatCard 固定高度 106px | `feed_tab.dart` | +| | └ Refresh 状态统一入口(Riverpod provider) | `feed_tab.dart` + `providers.dart` | +| | └ 第二层(按需):RawGestureDetector 手势方向判定 | `feed_tab.dart` | +| | └ **测试:** Android 真机快速上下滚动 × 50 次 | | +| 3–4 | **问题三:书源管理页 Sliver 重构** | | +| | └ CustomScrollView + SliverAppBar 吸顶搜索栏 | `source_manager_page.dart` | +| | └ SliverList.builder 懒加载 | `source_manager_page.dart` | +| | └ ExpansionTile 折叠仓库列表 | `source_manager_page.dart` | +| | └ 搜索防抖 180ms + dispose 清理 | `source_manager_page.dart` | +| | └ **测试:** 导入 2200+ 源后滚动 + 搜索 | | + +### 阶段 3:回归测试(1–2 天) + +| 测试项 | 方法 | +|--------|------| +| Android 真机滚动 | feed_tab 快速上下滑 × 50 次,观察有无卡顿 | +| 2200+ 书源压力测试 | 从"精选书源仓库"导入全量 → 搜索 → 启用/禁用单个源 → 滚动 | +| Web 端 `/app` | Chrome + Firefox 访问 → 图片网格加载 → 分页翻页 | +| Web 端 `/api` | 直接访问 API 路径 → 确认返回 JSON(正常行为) | +| Web 端图片加载 | 检查 CSP 日志 → 确认无 `img-src` 拦截 | +| 无书源首次启动 | 清除 SharedPreferences → 冷启动 APP → 确认显示引导 UI 而非假源 | + +### 风险矩阵 + +| 风险 | 等级 | 缓解措施 | +|------|:----:|----------| +| 问题二中 CSS 选择器 `html` 包性能不足 | 中 | 保留正则降级路径,低端机自动切降级 | +| 问题二中真实书源测试覆盖不足 | 高 | 选择 ≥10 个已知稳定的 Legado 源作为回归集 | +| 问题一中 AsyncNotifier 迁移引入新问题 | 低 | 先写测试用例覆盖原有 5 种数据加载路径 | +| 问题四中 Nginx 配置与现有部署冲突 | 中 | 提供独立配置文件和部署检查清单 | +| 书源法律边界 | 高 | 客户端不内置任何仓库 URL 或规则,由用户自行导入 | + +--- + +> **版本记录:** +> - v1.0 (2026-06-16):初始版本 +> - v1.1 (2026-06-16):技术评审修订——修正延迟数字、shrinkWrap 陷阱、重定向误伤、补充 CORS/ScrollController/GBK 异常/Isolate +> - v2.0 (2026-06-16):终版——优先级改为 P0/P1、问题二风险升级为 3–5 天且不内置任何仓库、问题一拆为两层修复(第一层必做 + 第二层按需)、问题四拆为开发/生产两套方案、问题三补搜索防抖、三阶段实施计划 diff --git a/docs/在线搜索-书本章节导航-完整解决方案.md b/docs/在线搜索-书本章节导航-完整解决方案.md new file mode 100644 index 0000000..03d7e5c --- /dev/null +++ b/docs/在线搜索-书本章节导航-完整解决方案.md @@ -0,0 +1,1222 @@ +# 在线搜索错误修复 与 书→目录→章节 导航流程 — 完整解决方案 + +> 分析日期:2026-06-21 +> 参考项目:[legado-with-MD3](https://github.com/HapeLee/legado-with-MD3) +> 涉及范围:Sakura Mobile (Flutter) +> 说明:本文档提供完整的问题分析与解决方案设计,不包含实际代码改动。 + +--- + +## 目录 + +1. [总体结论](#一总体结论) +2. [问题诊断](#二问题诊断) +3. [P0:必须优先修改](#三p0必须优先修改) +4. [P1:第二阶段修改](#四p1第二阶段修改) +5. [P2:后续增强](#五p2后续增强) +6. [优先级总表](#六优先级总表) +7. [页面与数据流设计](#七页面与数据流设计) +8. [数据模型扩展](#八数据模型扩展) +9. [Legado 书源规则字段对照](#九legado-书源规则字段对照) +10. [附录:关键文件清单](#十附录关键文件清单) + +--- + +## 一、总体结论 + +**不要只修 UI,也不要只做错误提示兜底。** + +真正应该改成的完整流程是: + +``` +搜索页 (ExplorePage) + ↓ +在线书籍详情页 (OnlineBookDetailPage) ← 合并 BookInfo + TOC 的 UI + ├─ 书籍信息(封面、书名、作者、来源、简介) + ├─ 章节列表(内嵌加载,不跳独立 TOC 页) + │ ├─ 分页加载 + │ └─ 正序/倒序切换 + └─ 快捷操作:「从第一章开始阅读」「保存到书架」 + ↓ + 阅读器 (OnlineMangaReaderPage / OnlineNovelReaderPage) + ├─ 章节驱动(非 URL 驱动) + ├─ 上一章 / 下一章 导航 + ├─ 下一章预加载 + └─ 阅读记录 +``` + +核心理念: + +``` +页面合并,解析分层; +缓存前置,持久化后置; +目录回退要识别章节,不要盲扫链接; +阅读器必须从 URL 驱动改成章节驱动。 +``` + +--- + +## 二、问题诊断 + +### 2.1 问题一:搜索结果显示 `unknown:none` + +#### 现象 + +用户在 APP 在线搜索漫画/小说时,搜索结果中某些条目显示为 `unknown:none`。 + +#### 根因 + +**位置**:`sakura_mobile/lib/src/core/legado_engine.dart:563-569` + +```dart +var message = error + .toString() + .replaceFirst(RegExp(r'^(Exception|StateError):\s*'), ''); +if (message.trim() == '[unknown]: null' || message.trim() == 'null') { + message = 'Target site returned an empty or invalid response.'; +} +``` + +当前只匹配了 `[unknown]: null` 和 `null`,但 Dio 在不同平台、不同错误类型下会抛出多种格式的底层异常消息: + +| 平台/场景 | 可能的错误消息 | 是否处理 | +|-----------|---------------|----------| +| Android | `[unknown]: null` | ✅ 已匹配 | +| Android | `[unknown]: none` | ❌ **漏掉,原样展示** | +| iOS | `Connection failed: unknown:none` | ❌ 漏掉 | +| Windows 桌面 | `DioException [unknown]: null` | ❌ 部分匹配失败 | +| 超时 | `Connection timed out` | ❌ 漏掉 | +| DNS 失败 | `No address associated with hostname` | ❌ 漏掉 | +| 编码错误 | `FormatException: Bad UTF-8 encoding` | ❌ 走 FormatException 分支,不友好 | + +这些未处理的错误消息被原样透传到 `SearchItem.errorMessage`,最终在 UI 的 subtitle 中展示为 `源名称 - unknown:none`。 + +#### 搜索结果字段提取失败的连锁反应 + +**位置**:`legado_engine.dart:60-98` + +当书源的解析规则与目标网站 HTML 结构不匹配时: +- `title` 为空 → 该结果被 `.where()` 过滤掉 → 整个源的搜索结果为空 +- `author` 为空 → subtitle 中显示为 null +- `detailUrl` 为空 → 点击后 URL 为空 → 报错 + +--- + +### 2.2 问题二:缺少「书→目录→章节」导航流程 + +#### 现象 + +用户搜索到一本书后,点击结果**直接进入阅读器**,而不是进入书籍详情页。结果是: +1. 阅读器把书籍详情页 HTML 当正文展示 → 乱码/空白/无关图片 +2. 用户看不到章节列表,无法选择具体章节阅读 + +#### 根因一:数据模型缺少三个关键规则模块 + +**位置**:`sakura_mobile/lib/src/core/legado_models.dart:21-86` + +当前 `LegadoSource` 只包含搜索相关的规则字段: + +``` +searchUrl, searchListRule, searchNameRule, searchAuthorRule, +searchCoverRule, searchDetailRule, searchMethod, searchBody +``` + +完全缺失 Legado 标准书源中的三个关键规则模块: + +| 模块 | 作用 | 关键字段 | +|------|------|---------| +| `ruleBookInfo` | 解析书籍详情页 → 得到元数据 + 目录 URL | name, author, cover, intro, **tocUrl** | +| `ruleToc` | 解析目录页 → 得到章节列表 | chapterList, chapterName, chapterUrl | +| `ruleContent` | 解析章节页 → 得到正文/图片 | content, nextChapterUrl, prevChapterUrl | + +这三个模块是 Legado 生态的标准配置,被数千个书源规则所遵循。当前项目只实现了其中的搜索模块。 + +#### 根因二:搜索结果点击逻辑错误 + +**位置**:`sakura_mobile/lib/src/pages/explore_page.dart:174-228` (`_openItem` 方法) + +```dart +// 在线结果 → 直接打开阅读器(❌ 错误!) +if (mode == UnifiedSearchMode.manga) { + Navigator.of(context).push(MaterialPageRoute( + builder: (_) => OnlineMangaReaderPage( + source: source, title: item.title, + detailUrl: item.detailUrl ?? item.coverUrl ?? '', // ← 书籍详情页 URL! + ), + )); +} +``` + +`detailUrl` 在 Legado 规则中的语义是**书籍详情页 URL**,不是章节内容 URL。这个 URL 指向的页面包含封面、书名、作者、简介、章节目录链接——唯独没有章节正文。 + +将此 URL 直接传入阅读器,阅读器会: +- 漫画:调用 `fetchImageUrls()` → 提取详情页所有 `` → 返回封面 + 广告图 + 导航图标 +- 小说:调用 `fetchPlainText()` → 提取详情页纯文本 → 返回简介混合着页面导航文字 + +#### 根因三:阅读器缺少章节概念 + +两个在线阅读器都接收单一的 `detailUrl` 参数,没有: +- 章节列表 +- 上一章/下一章导航 +- 阅读进度记录 + +--- + +## 三、P0:必须优先修改 + +> 这些是当前问题的直接根因,需要最先实施。 + +### 1. 修复 `unknown:none` 错误展示 + +**修改位置**:`sakura_mobile/lib/src/core/legado_engine.dart` → `_shortError()` 方法 + +**目标**:将所有 Dio 底层异常消息逐一映射为用户可读的中文提示,不再有原始异常消息泄漏到 UI。 + +**错误消息映射表**: + +``` +[unknown]: null → 目标站点返回了空响应,请检查书源是否仍然有效 +[unknown]: none → 目标站点连接失败,请检查网络或更换书源 +unknown:none → 连接异常,书源可能已失效 +Connection refused → 目标站点拒绝了连接,可能需要 Cookie 或反爬处理 +Connection timed out → 连接超时,请稍后重试 +No address associated → 无法解析目标站点域名,请检查网络 +Bad UTF-8 encoding → 响应编码异常,请检查书源的 charset 配置 +null → 目标站点返回了无效响应 +``` + +**实现思路**: + +```dart +String _shortError(Object error) { + // 1. Dio 网络异常 — 按错误类型精准映射 + if (error is DioException) { + final host = error.requestOptions.uri.host; + final target = host.isEmpty ? '目标站点' : host; + switch (error.type) { + case DioExceptionType.connectionTimeout: + case DioExceptionType.receiveTimeout: + case DioExceptionType.sendTimeout: + return '连接 $target 超时,请稍后重试。'; + case DioExceptionType.connectionError: + return '无法连接 $target,请检查网络或更换书源。'; + case DioExceptionType.badResponse: + final code = error.response?.statusCode ?? 0; + if (code == 404) return '书源搜索接口已失效(404)。'; + if (code == 403 || code == 444) return '$target 拒绝了访问,可能需要 Cookie 配置。'; + return '$target 返回了异常状态(HTTP $code)。'; + default: + break; + } + } + + // 2. FormatException — 编码/格式问题 + if (error is FormatException) { + final msg = error.message.toLowerCase(); + if (msg.contains('utf') || msg.contains('encoding')) + return '响应编码异常,请检查书源的 charset 配置。'; + return '书源规则期望 JSON 格式,但站点返回了其他内容。'; + } + + // 3. 原始消息归一化 — 兜底处理所有未分类的异常文本 + var message = error + .toString() + .replaceFirst(RegExp(r'^(Exception|StateError|DioException):\s*'), ''); + final trimmed = message.trim(); + + // 按精确度从高到低匹配 + if (trimmed == '[unknown]: null' || trimmed == 'null') + return '目标站点返回了空响应,请检查书源是否仍然有效。'; + if (trimmed == '[unknown]: none' || trimmed == 'unknown:none') + return '连接异常,书源可能已失效。'; + if (trimmed.toLowerCase().contains('connection refused')) + return '目标站点拒绝了连接。'; + if (trimmed.toLowerCase().contains('connection timed out')) + return '连接超时,请稍后重试。'; + if (trimmed.toLowerCase().contains('no address associated')) + return '无法解析目标站点域名,请检查网络。'; + + // 4. 兜底截断,避免超长异常堆栈泄漏到 UI + if (message.length > 120) message = '${message.substring(0, 120)}...'; + return message.isEmpty ? '未知错误' : message; +} +``` + +**同时修改 UI 层**(`explore_page.dart::_SearchResultTile`): +- 当 `item.author` 为空时显示"未知作者"而非空白 +- 当 `item.isError` 时,subtitle 中的错误消息不应再包含原始异常类名 + +--- + +### 2. 扩展 `LegadoSource` 数据模型 + +**修改位置**:`sakura_mobile/lib/src/core/legado_models.dart` + +当前模型只支持搜索规则。需要新增三个规则模块的字段。 + +**新增字段**: + +```dart +class LegadoSource { + // ===== 现有字段(保持不变)===== + final String id; + final String name; + final String url; + final int type; // 0=小说, 2=漫画 + final bool enabled; + final String? userAgent; + final Map headers; + // ... 现有搜索规则字段 ... + + // ===== 🆕 BookInfo 规则(从 ruleBookInfo 解析)===== + final String? bookInfoUrl; // 详情页 URL 模板(可选,默认用搜索结果的 detailUrl) + final String? bookInfoNameRule; // 提取书名(CSS 选择器) + final String? bookInfoAuthorRule; // 提取作者 + final String? bookInfoCoverRule; // 提取封面 + final String? bookInfoIntroRule; // 提取简介 + final String? bookInfoTocRule; // 提取目录页 URL ⭐ 最关键字段 + + // ===== 🆕 TOC 规则(从 ruleToc 解析)===== + final String? tocUrl; // 目录页 URL 模板(可选,支持 {{page}}) + final String? tocListRule; // 提取章节条目列表 + final String? tocNameRule; // 提取章节名称 + final String? tocUrlRule; // 提取章节链接 + final String? tocNextPageRule; // 提取下一页目录的 URL(用于分页) + + // ===== 🆕 Content 规则(从 ruleContent 解析)===== + final String? contentRule; // 提取正文/图片内容 + final String? contentNextRule; // 提取下一章链接 + final String? contentPrevRule; // 提取上一章链接 +} +``` + +**`fromJson()` 中新增的解析逻辑**: + +```dart +// 从 ruleBookInfo 提取 +final bookInfo = _map(json['ruleBookInfo']); +bookInfoNameRule = _firstString([ + bookInfo['name'], bookInfo['bookName'], +]); +bookInfoAuthorRule = _firstString([ + bookInfo['author'], bookInfo['bookAuthor'], +]); +bookInfoCoverRule = _firstString([ + bookInfo['coverUrl'], bookInfo['cover'], +]); +bookInfoIntroRule = _firstString([ + bookInfo['intro'], bookInfo['introduce'], bookInfo['desc'], +]); +bookInfoTocRule = _firstString([ + bookInfo['tocUrl'], bookInfo['chapterUrl'], + bookInfo['catalogUrl'], bookInfo['directoryUrl'], +]); + +// 从 ruleToc 提取 +final toc = _map(json['ruleToc']); +tocListRule = _firstString([ + toc['chapterList'], toc['chapters'], toc['list'], +]); +tocNameRule = _firstString([ + toc['chapterName'], toc['name'], toc['title'], +]); +tocUrlRule = _firstString([ + toc['chapterUrl'], toc['url'], toc['href'], +]); +tocNextPageRule = _firstString([ + toc['nextTocUrl'], toc['nextPage'], toc['nextPageUrl'], +]); + +// 从 ruleContent 提取 +final content = _map(json['ruleContent']); +contentRule = _firstString([ + content['content'], content['text'], content['body'], +]); +contentNextRule = _firstString([ + content['nextChapterUrl'], content['next'], content['nextPage'], +]); +contentPrevRule = _firstString([ + content['prevChapterUrl'], content['prev'], content['prevPage'], +]); +``` + +**`copyWith` 也需要同步扩展**,确保 `setEnabled()` 等现有方法能正常工作。 + +--- + +### 3. 新增在线书籍详情页(合并 BookInfo + TOC UI) + +**新增文件**:`sakura_mobile/lib/src/pages/online_book_detail_page.dart` + +**关键设计决策**:不要创建独立的 `OnlineTocPage`。将 BookInfo 和章节列表放在**同一个页面**中,但数据加载保持**两次独立请求**(因为很多书源的 `detailUrl ≠ tocUrl`)。 + +``` +┌──────────────────────────────────────┐ +│ ← 返回 书籍详情 📤 │ AppBar +├──────────────────────────────────────┤ +│ │ +│ ┌──────────────┐ │ +│ │ │ │ 封面图 +│ │ 书籍封面 │ │ (CachedNetworkImage) +│ │ │ │ +│ └──────────────┘ │ +│ │ +│ 斗罗大陆 │ 书名(大标题) +│ 唐家三少 · 笔趣阁 · 连载中 │ 作者/来源/状态 一行 +│ │ +│ ┌──────────────────────────────┐ │ +│ │ 简介 │ 简介卡片 +│ │ 唐门外门弟子唐三,因偷学内门 │ (可展开/收起) +│ └──────────────────────────────┘ │ +│ │ +│ ┌──────────────────────────────┐ │ +│ │ ▶ 从第一章开始阅读 │ 快捷操作按钮 +│ └──────────────────────────────┘ │ +│ ┌──────────────────────────────┐ │ +│ │ 💾 保存到书架 │ 保存操作 +│ └──────────────────────────────┘ │ +│ │ +│ ──────── 章节目录 (共 336 章) ───── │ 分隔标题 +│ [正序 ↑] [倒序 ↓] │ 排序切换 +│ │ +│ ┌──────────────────────────────────┐│ +│ │ 📖 第一章 楔子 ││ 章节条目 +│ └──────────────────────────────────┘│ +│ ┌──────────────────────────────────┐│ +│ │ 📖 第二章 唐三的武魂 ││ +│ └──────────────────────────────────┘│ +│ ┌──────────────────────────────────┐│ +│ │ 📖 第三章 修炼之路 ││ +│ └──────────────────────────────────┘│ +│ ┌──────────────────────────────────┐│ +│ │ ... 更多章节(滚动加载) ││ 支持无限滚动 +│ │ 正在加载第2页... ││ TOC 分页指示器 +│ └──────────────────────────────────┘│ +│ │ +└──────────────────────────────────────┘ +``` + +**数据加载方式(分段异步)**: + +``` +第一步:请求 BookInfo(用 bookInfoRule 解析 detailUrl) + → 拿到 title, author, cover, intro, tocUrl + → 立即渲染页面上半部分(封面+书名+作者+简介+快捷按钮) + → 章节列表区域显示骨架屏 loading + +第二步:请求 TOC(用 chapterListRule 解析 tocUrl) + → 拿到 List + → 章节列表区域从骨架屏切换为真实数据 + → 如果有 tocNextPageRule,继续加载后续页 + +两步之间不互相阻塞:BookInfo 失败 → 整个页面显示错误;TOC 失败 → 上半部分正常展示,下半部分显示"目录加载失败,点击重试" +``` + +**重要**: + +``` +合并页面 ≠ 合并请求 + +因为很多书源: + detailUrl (书籍详情页) + ≠ + tocUrl (章节目录页,可能是独立的分页 URL) + +详情和目录必须允许两次独立的 HTTP 请求。 +``` + +--- + +### 4. 修改搜索结果点击逻辑 + +**修改位置**:`sakura_mobile/lib/src/pages/explore_page.dart` → `_openItem()` 方法 + +**当前逻辑(错误)**: + +``` +点击在线结果 → 直接打开 OnlineMangaReaderPage / OnlineNovelReaderPage + (把 detailUrl 当成章节内容 URL 传给阅读器) +``` + +**改为**: + +``` +点击在线结果 → 打开 OnlineBookDetailPage(detailUrl) + → 用户在详情页看到书籍信息 + 章节列表 + → 点击某个章节 → 打开阅读器(传入章节 URL) + → 或点击「从第一章开始阅读」→ 自动打开第一个章节 +``` + +```dart +void _openItem(SearchItem item) { + // 云库结果(不变) + if (item.artwork != null) { + Navigator.of(context).push( + MaterialPageRoute(builder: (_) => ArtworkDetailPage(artwork: item.artwork!)), + ); + return; + } + if (item.novel != null) { + Navigator.of(context).push( + MaterialPageRoute(builder: (_) => NovelReaderPage(novel: item.novel!)), + ); + return; + } + + // 在线结果 — 🆕 改为跳转到详情页 + final online = item.online; + if (item.isError) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(item.errorMessage ?? '搜索失败,请稍后再试。')), + ); + return; + } + if (online == null) return; + + final source = ref.read(sourceManagerProvider).sourceById(online.sourceId); + if (source == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('书源已不可用。')), + ); + return; + } + + // 🆕 跳转到在线书籍详情页 + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => OnlineBookDetailPage( + source: source, + initialTitle: item.title, + initialAuthor: item.author, + initialCoverUrl: item.coverUrl, + detailUrl: item.detailUrl ?? '', + mode: mode, // manga 或 novel + ), + ), + ); +} +``` + +--- + +### 5. 增加会话级内存缓存 + +**修改位置**:`sakura_mobile/lib/src/core/legado_engine.dart` 或新建独立缓存文件 + +**第一版用内存缓存即可,不要着急上 Hive/Isar。** + +```dart +/// 会话级内存缓存 +class OnlineContentCache { + // 存活时间:当次 APP 会话 + final _bookInfoCache = {}; + final _tocCache = >{}; + final _chapterCache = {}; + + // 缓存 key 规则:source.id + URL + String _key(String sourceId, String url) => '$sourceId|$url'; + + // BookInfo + OnlineBookInfo? getBookInfo(String sourceId, String url) => + _bookInfoCache[_key(sourceId, url)]; + void putBookInfo(String sourceId, String url, OnlineBookInfo info) => + _bookInfoCache[_key(sourceId, url)] = info; + + // TOC(章节列表) + List? getToc(String sourceId, String tocUrl) => + _tocCache[_key(sourceId, tocUrl)]; + void putToc(String sourceId, String tocUrl, List chapters) => + _tocCache[_key(sourceId, tocUrl)] = chapters; + + // Chapter(单章内容) + OnlineChapterContent? getChapter(String sourceId, String chapterUrl) => + _chapterCache[_key(sourceId, chapterUrl)]; + void putChapter(String sourceId, String chapterUrl, OnlineChapterContent content) => + _chapterCache[_key(sourceId, chapterUrl)] = content; + + // 预加载下一章(阅读器中调用) + void preloadNextChapter(String sourceId, String nextUrl) { + // 由阅读器在后台触发,不阻塞当前阅读 + } + + void clear() { + _bookInfoCache.clear(); + _tocCache.clear(); + _chapterCache.clear(); + } +} +``` + +**缓存策略**: +- `BookInfo`:进入详情页时先查缓存,未命中才请求 +- `TOC`:目录可能有几百章,每次进页面重新请求既慢也会增加书源站点压力——优先读缓存 +- `ChapterContent`:只缓存当前章 + 预加载下一章,不要一口气缓存全部章节正文(容易占内存) + +**为什么阶段一不用持久化缓存**: +- 在线书源的内容可能变化(网站改版、章节更新),持久化缓存容易导致过期数据 +- 先跑通主流程,确认缓存策略合理后,P2 阶段再引入 Hive/Isar +- 会话级缓存已能覆盖"用户在详情页和阅读器之间来回切换"的核心场景 + +--- + +### 6. 章节识别器回退策略(替代盲扫 `` 标签) + +**修改位置**:`sakura_mobile/lib/src/core/legado_engine.dart` → 新增 `_chapterAwareFallback()` 方法 + +**问题**:原方案中的 `_fallbackSearchResults` 和自动扫描 `` 都太激进——会把导航链接、广告链接、面包屑链接都当成章节。 + +**改进**:引入"章节识别器"——即使用正则匹配章节标题特征,而非盲扫所有链接。 + +```dart +/// 章节标题特征正则 +static final _chapterPatterns = [ + // 中文数字章节 + RegExp(r'第\s*[\d零一二三四五六七八九十百千万]+\s*[章节话卷回]'), + // 中文数字话 + RegExp(r'第\s*[\d零一二三四五六七八九十百千万]+\s*话'), + // 纯数字章节 + RegExp(r'第\s*\d+\s*[章节话卷回]'), + // 英文章节 + RegExp(r'Chapter\s*\d+', caseSensitive: false), + RegExp(r'Episode\s*\d+', caseSensitive: false), + // 卷 + RegExp(r'Vol\.?\s*\d+', caseSensitive: false), + RegExp(r'第\s*[\d零一二三四五六七八九十百千万]+\s*卷'), + // 纯数字话 + RegExp(r'^\d+\s*[话章]'), +]; + +/// 判断文本是否像一个章节标题 +bool _looksLikeChapterTitle(String text) { + final trimmed = text.trim(); + if (trimmed.length < 2 || trimmed.length > 80) return false; + for (final pattern in _chapterPatterns) { + if (pattern.hasMatch(trimmed)) return true; + } + return false; +} + +/// 回退目录解析:扫描 HTML 中的 标签,但只保留符合章节标题特征的 +List _fallbackTocParser(String body, String baseUrl) { + final seen = {}; + final chapters = []; + + final links = RegExp( + r"]*\bhref=[\"']([^\"']+)[\"'][^>]*>(.*?)", + caseSensitive: false, + dotAll: true, + ).allMatches(body); + + for (final match in links) { + final href = match.group(1)?.trim() ?? ''; + if (href.isEmpty || href.startsWith('#') || + href.toLowerCase().startsWith('javascript:')) continue; + + final title = _htmlToText(match.group(2) ?? ''); + if (!_looksLikeChapterTitle(title)) continue; // ← 关键过滤 + + final url = _resolveUrl(baseUrl, href); + if (url.isEmpty || !seen.add(url)) continue; + + chapters.add(OnlineChapter( + title: title, + url: url, + index: chapters.length + 1, + )); + } + return chapters; +} +``` + +**解析优先级**: + +``` +1. 如果 tocListRule 可用 → 使用规则解析(精确) +2. 如果 tocListRule 不可用 → 使用章节识别器扫描(降级但可控) +3. 返回结果 ≤ 2 条 → 不做二次请求,直接展示(可能书源确实只有少量章节,也可能是网站结构不支持回退解析) +``` + +--- + +## 四、P1:第二阶段修改 + +> 这些依赖于 P0 的数据模型和详情页基础设施就绪。 + +### 1. 阅读器改造成章节驱动 + +**修改位置**:`online_manga_reader_page.dart` 和 `online_novel_reader_page.dart` + +**当前**:读取器接收 `detailUrl`(其实是书籍详情页 URL),从中提取所有图片/文本。 + +**改造后**:读取器接收章节级参数。 + +```dart +class OnlineMangaReaderPage extends ConsumerStatefulWidget { + const OnlineMangaReaderPage({ + super.key, + required this.source, + required this.bookTitle, // 🆕 书名(用于 AppBar / 阅读记录) + required this.chapters, // 🆕 完整章节列表 + required this.currentChapterIndex,// 🆕 当前章节序号 + this.currentChapterUrl, // 当前章节 URL(优先于从 chapters 取) + }); + + final LegadoSource source; + final String bookTitle; + final List chapters; + final int currentChapterIndex; + final String? currentChapterUrl; +} +``` + +**进入阅读器时的数据加载**: + +``` +1. 从 chapters[currentChapterIndex].url 取章节 URL +2. 如果有 contentRule → 用 contentRule 解析 +3. 如果没有 contentRule → 回退到现有的 fetchImageUrls / fetchPlainText +4. 用 contentNextRule 提取下一章 URL(优先于 chapters 数组) +5. 用 contentPrevRule 提取上一章 URL +``` + +**阅读器底部导航**: + +``` +┌───────────┬───────────┬───────────┐ +│ ← 上一章 │ 📋 目录 │ 下一章 → │ +└───────────┴───────────┴───────────┘ +``` + +- "上一章":`currentChapterIndex > 0` 时才可用 +- "下一章":`currentChapterIndex < chapters.length - 1` 或 `contentNextRule` 解析到了 URL 时才可用 +- "目录":`Navigator.pop()` 回到详情页(章节列表已缓存,无需重新加载) + +--- + +### 2. 支持目录分页 `{{page}}` + +**修改位置**:`legado_engine.dart` → 新增 `_fetchPaginatedToc()` 方法 + +**问题**:很多书源的目录是分页的,URL 模板包含 `{{page}}` 占位符,例如: + +``` +/chapterlist/123?page={{page}} +``` + +如果不处理分页,只能拿到第一页的章节(通常几十章),后面几百章全部丢失。 + +**实现**: + +```dart +Future> _fetchPaginatedToc( + LegadoSource source, + String tocUrlTemplate, +) async { + final allChapters = []; + const maxPages = 20; // 防止死循环 + String? nextPageUrl = tocUrlTemplate; + + for (int page = 1; page <= maxPages; page++) { + // 1. 替换 {{page}} 占位符 + final url = nextPageUrl! + .replaceAll('{{page}}', page.toString()) + .replaceAll('{page}', page.toString()); + + // 2. 请求并解析当前页 + final body = await _fetchBody(source, url); + final parser = RuleParser(body, baseUrl: source.url); + + // 3. 用规则解析章节条目 + final fragments = _safeSelectList(parser, source.tocListRule); + for (final fragment in fragments) { + final name = _safeSelect(parser, fragment, source.tocNameRule); + final chapterUrl = _safeSelect(parser, fragment, source.tocUrlRule); + if (name.isNotEmpty && chapterUrl.isNotEmpty) { + allChapters.add(OnlineChapter( + title: name, + url: _resolveUrl(url, chapterUrl), + index: allChapters.length + 1, + )); + } + } + + // 4. 检查是否有下一页 + if (source.tocNextPageRule != null) { + nextPageUrl = _safeSelect(parser, body, source.tocNextPageRule); + if (nextPageUrl == null || nextPageUrl.isEmpty) break; // 没有下一页了 + } else { + // 如果没有 nextPageRule,尝试自动检测:如果当前页有 {{page}},则用 page+1 继续 + if (!tocUrlTemplate.contains('{{page}}') && + !tocUrlTemplate.contains('{page}')) { + break; // URL 模板中没有分页占位符,不继续 + } + // 如果当前页返回的章节数与上一页相同,可能到底了,也停止 + } + } + + return allChapters; +} +``` + +**自动停止条件**: +- `tocNextPageRule` 返回空 → 停止 +- 当前页返回 0 条新章节 → 停止 +- 达到 `maxPages`(20 页)→ 停止并打日志 +- 当前页与上一页完全重复 → 停止(防止环形分页) + +--- + +### 3. 章节预加载 + +**修改位置**:阅读器页面 + +**策略**:阅读当前章节时,后台预加载下一章内容。 + +``` +用户正在读第 3 章 + │ + ├─ 立即请求:第 3 章内容(用 contentRule 解析) + │ + └─ 后台请求:第 4 章内容(放入 chapterCache) + │ + └─ 用户点"下一章" → 缓存命中 → 秒切 +``` + +```dart +void _preloadNextChapter() { + final nextIdx = currentChapterIndex + 1; + if (nextIdx >= chapters.length) return; + + final nextChapter = chapters[nextIdx]; + final cacheKey = cache.key(source.id, nextChapter.url); + + if (cache.getChapter(source.id, nextChapter.url) != null) return; // 已缓存 + + // 异步预加载,不阻塞当前渲染 + _engine.fetchChapterContent(source, nextChapter.url) + .then((content) { + cache.putChapter(source.id, nextChapter.url, content); + }) + .catchError((_) { + // 预加载失败静默忽略,用户点到下一章时再重试 + }); +} +``` + +**注意**: +- 只预加载**下一章**,不要一口气预加载全部(几百章的图片会爆内存) +- 预加载失败静默忽略,点击"下一章"时再重新请求 + +--- + +### 4. 阅读记录 + +**新增数据模型**: + +```dart +class OnlineReadingRecord { + final String sourceId; + final String sourceName; + final String bookTitle; + final String detailUrl; + final String chapterUrl; + final String chapterTitle; + final int chapterIndex; + final int totalChapters; + final DateTime lastReadTime; +} +``` + +**存储方式(P1 阶段)**:SharedPreferences 存储 JSON 数组,最多保留 50 条最近记录。 + +**用途**: +- 「继续阅读」入口(在 ExplorePage 或首页) +- 章节已读标记(在详情页章节列表中显示 ✓ 或灰色) +- 返回详情页时自动定位到上次阅读的章节 + +**记录时机**: +- 进入某章阅读 → upsert 阅读记录 +- 切换章节 → 更新阅读记录 +- 返回详情页 → 不记录(只记录"阅读"行为) + +--- + +## 五、P2:后续增强 + +> 主流程稳定后再做这些。 + +### 1. 持久化缓存 + +从内存缓存升级到本地持久化: + +| 方案 | 适用场景 | +|------|---------| +| **Hive** | 轻量,适合存储 BookInfo / TOC / 阅读记录 | +| **Isar** | 功能更强,适合存储章节缓存 + 全文搜索 | +| **SQLite / sqflite** | 最灵活,适合复杂查询(按书源查、按时间查等) | + +建议:**Hive** 作为第一选择,因为数据模型简单且已广泛用于 Flutter 缓存场景。 + +持久化内容: +- `BookInfo` → 按 `sourceId + detailUrl` 存储,有效期 24 小时 +- `TOC` → 按 `sourceId + tocUrl` 存储,有效期 12 小时 +- `阅读记录` → 永久存储,最多 200 条 +- 章节内容 → 只缓存最近阅读的 20 章 + +### 2. ContentType 支持 + +初期不需要设计 `enum ContentType { text, image, mixed }`。 + +因为已有 `bookSourceType`: +- `type == 0` → 小说 → `contentRule` 提取文本 +- `type == 2` → 漫画 → `contentRule` 提取图片 + +`mixed` 类型(小说中插图片、漫画中插文字)可以后期再支持。 + +### 3. 高级 Legado 规则支持 + +后续逐步补充: +- JSONPath 规则(`$.data.chapters[*]`) +- Regex 规则(`regex:...`) +- `webJs` 规则(需要 WebView 执行 JS 后取结果) +- `replaceRegex`(正文净化,去广告/去水印) +- 图片解密(部分漫画网站对图片做了简单加密,需要 JS 解密后 base64 展示) + +这些是 Legado 生态的高级功能,不影响主体流程,可以逐步按需实现。 + +### 4. 更完整的兼容测试 + +- 覆盖至少 10 个不同类型的书源(小说/漫画、GBK/UTF-8、分页/单页目录) +- 测试老书源(只有搜索规则,没有 `ruleBookInfo` / `ruleToc`)的回退表现 +- 测试网络异常情况(超时、DNS 失败、404、403) + +--- + +## 六、优先级总表 + +``` +P0:必须优先修改 +├─ 1. unknown:none 错误归一化 +├─ 2. 扩展 LegadoSource 模型(ruleBookInfo / ruleToc / ruleContent) +├─ 3. 新增 OnlineBookDetailPage(合并 BookInfo + TOC UI) +├─ 4. BookInfo + TOC 分段异步加载 +├─ 5. 搜索结果点击进入详情页(explore_page.dart 导航逻辑) +├─ 6. 会话级内存缓存(BookInfo / TOC / Chapter) +└─ 7. 章节识别器回退策略(替代盲扫 ) + +P1:第二阶段修改 +├─ 1. 阅读器改造成章节驱动(接收 chapters + currentChapterIndex) +├─ 2. contentRule 正式接入(提取章节正文/图片) +├─ 3. 目录分页 {{page}} 支持 +├─ 4. 下一章预加载 +└─ 5. 阅读记录(继续阅读 / 已读标记) + +P2:后续增强 +├─ 1. 持久化缓存(Hive) +├─ 2. ContentType / mixed 内容 +├─ 3. 高级 Legado 规则支持(JSONPath / webJs / replaceRegex) +└─ 4. 更完整的兼容测试 +``` + +--- + +## 七、页面与数据流设计 + +### 7.1 整体架构 + +``` +┌─────────────────────────────────────────────────────────┐ +│ 在线搜索 (ExplorePage) │ +│ 输入关键词 → 并发搜索已启用的书源 → 展示结果列表 │ +└───────────────────────┬─────────────────────────────────┘ + │ 用户点击某本书 + ▼ +┌─────────────────────────────────────────────────────────┐ +│ 在线书籍详情页 (OnlineBookDetailPage) 🆕 │ +│ │ +│ ┌─── 上半部分:BookInfo ───┐ │ +│ │ 封面 · 书名 · 作者 · 来源 │ ← 第一步加载 │ +│ │ 状态 · 简介 │ │ +│ │ [开始阅读] [保存到书架] │ │ +│ └──────────────────────────┘ │ +│ │ +│ ┌─── 下半部分:TOC ────────┐ │ +│ │ 章节目录 (共 336 章) │ ← 第二步加载(独立请求) │ +│ │ [正序] [倒序] │ │ +│ │ 第一章 · 第二章 · ... │ 支持分页加载 │ +│ └──────────────────────────┘ │ +└───────────────────────┬─────────────────────────────────┘ + │ 点击某章节 + ▼ +┌─────────────────────────────────────────────────────────┐ +│ 在线阅读器 (MangaReader / NovelReader) 🔧 改造 │ +│ │ +│ AppBar: 第3话 / 共336话 │ +│ 正文内容 / 漫画图片 │ +│ [← 上一章] [📋 目录] [下一章 →] │ +│ │ +│ 接收参数: source, chapters, currentChapterIndex │ +│ 预加载: 下一章内容(后台) │ +│ 记录: 阅读进度 │ +└─────────────────────────────────────────────────────────┘ +``` + +### 7.2 数据流 + +``` +阶段 1 — 搜索: + LegadoSource.searchUrl + keyword + → HTTP GET/POST + → searchListRule 提取条目列表 + → 每个条目: {title, author, coverUrl, detailUrl} + +阶段 2 — 详情(先): + detailUrl + → HTTP GET + → bookInfoRule 提取: {name, author, cover, intro, tocUrl} + → 存入 bookInfoCache + → 渲染页面顶部 + +阶段 3 — 目录(后): + tocUrl + → HTTP GET(支持 {{page}} 分页) + → chapterListRule 提取: [{name, url}, ...] + → 存入 tocCache + → 渲染章节列表 + +阶段 4 — 阅读: + chapter.url + → HTTP GET + → contentRule 提取: 正文文本 / 漫画图片 URL 列表 + → 存入 chapterCache + → 展示内容 + → 后台预加载下一章 +``` + +### 7.3 路由变更 + +| 当前 | 改造后 | +|------|--------| +| ExplorePage → OnlineMangaReaderPage(detailUrl) ❌ | ExplorePage → **OnlineBookDetailPage(detailUrl)** → 点击章节 → OnlineMangaReaderPage(chapterUrl) ✅ | +| ExplorePage → OnlineNovelReaderPage(detailUrl) ❌ | ExplorePage → **OnlineBookDetailPage(detailUrl)** → 点击章节 → OnlineNovelReaderPage(chapterUrl) ✅ | + +--- + +## 八、数据模型扩展 + +### 8.1 LegadoSource 新增字段 + +```dart +class LegadoSource { + // ===== 现有字段(保持不变)===== + final String id; + final String name; + final String url; + final int type; // 0=小说, 2=漫画 + final bool enabled; + final String? userAgent; + final Map headers; + final String? searchUrl; + final String? searchListRule; + final String? searchNameRule; + final String? searchAuthorRule; + final String? searchCoverRule; + final String? searchDetailRule; + final String? searchMethod; + final String? searchBody; + final String? ruleKind; + final String? charset; + final String? urlEncoding; + + // ===== 🆕 BookInfo ===== + final String? bookInfoUrl; + final String? bookInfoNameRule; + final String? bookInfoAuthorRule; + final String? bookInfoCoverRule; + final String? bookInfoIntroRule; + final String? bookInfoTocRule; // ⭐ 最关键:目录页 URL + + // ===== 🆕 TOC ===== + final String? tocUrl; + final String? tocListRule; + final String? tocNameRule; + final String? tocUrlRule; + final String? tocNextPageRule; // 目录分页 + + // ===== 🆕 Content ===== + final String? contentRule; + final String? contentNextRule; + final String? contentPrevRule; +} +``` + +### 8.2 新增运行时数据模型 + +```dart +/// 在线书籍详情 +class OnlineBookInfo { + final String title; + final String? author; + final String? coverUrl; + final String? intro; + final String tocUrl; // ⭐ 目录页 URL + final String? status; // 连载中/已完结 + final String? category; + final String? updateTime; + final String sourceName; // 来源书源名称 +} + +/// 在线章节 +class OnlineChapter { + final String title; + final String url; + final int index; // 从 1 开始 + bool isRead; // 是否已读(阅读记录反填) +} + +/// 在线章节内容 +class OnlineChapterContent { + final String title; + final String? textContent; // 小说正文 + final List? imageUrls; // 漫画图片 + final String? nextChapterUrl; // 下一章 URL(contentNextRule 解析结果) + final String? prevChapterUrl; // 上一章 URL +} + +/// 阅读记录 +class OnlineReadingRecord { + final String sourceId; + final String sourceName; + final String bookTitle; + final String detailUrl; + final String chapterUrl; + final String chapterTitle; + final int chapterIndex; + final int totalChapters; + final DateTime lastReadTime; +} +``` + +### 8.3 fromJson / copyWith / toJson 变更 + +- `fromJson`:新增 12 个字段的解析逻辑(见上文 P0-2) +- `copyWith`:新增 12 个字段的传递(确保 `setEnabled()` 等不丢失数据) +- `toJson`:新增 12 个字段的序列化(确保持久化到 SharedPreferences 时保留) + +--- + +## 九、Legado 书源规则字段对照 + +### 9.1 完整的 Legado 书源 JSON 示例 + +```json +{ + "bookSourceName": "示例书源", + "bookSourceUrl": "https://www.example.com", + "bookSourceType": 0, + "bookSourceCharset": "gbk", + + "ruleSearch": { + "url": "/search?keyword={{key}}&page={{page}}", + "bookList": ".result-list .item", + "name": ".book-title", + "author": ".author", + "coverUrl": "img@src", + "detailUrl": "a@href" + }, + + "ruleBookInfo": { + "name": "h1.book-title", + "author": ".author span", + "coverUrl": ".book-cover img@src", + "intro": ".book-desc", + "tocUrl": ".catalog-btn@href" + }, + + "ruleToc": { + "chapterList": ".chapter-list li", + "chapterName": "a", + "chapterUrl": "a@href", + "nextTocUrl": ".next-page@href" + }, + + "ruleContent": { + "content": "#chapter-content", + "nextChapterUrl": ".next-chapter@href", + "prevChapterUrl": ".prev-chapter@href" + } +} +``` + +### 9.2 字段映射表 + +| Legado 字段路径 | Sakura LegadoSource 字段 | P0/P1 | +|----------------|-------------------------|-------| +| `bookSourceName` | `name` | ✅ 已有 | +| `bookSourceUrl` | `url` | ✅ 已有 | +| `bookSourceType` | `type` | ✅ 已有 | +| `bookSourceCharset` | `charset` | ✅ 已有 | +| `ruleSearch.url` | `searchUrl` | ✅ 已有 | +| `ruleSearch.bookList` | `searchListRule` | ✅ 已有 | +| `ruleSearch.name` | `searchNameRule` | ✅ 已有 | +| `ruleSearch.author` | `searchAuthorRule` | ✅ 已有 | +| `ruleSearch.coverUrl` | `searchCoverRule` | ✅ 已有 | +| `ruleSearch.detailUrl` | `searchDetailRule` | ✅ 已有 | +| `ruleBookInfo.name` | `bookInfoNameRule` | 🆕 P0 | +| `ruleBookInfo.author` | `bookInfoAuthorRule` | 🆕 P0 | +| `ruleBookInfo.coverUrl` | `bookInfoCoverRule` | 🆕 P0 | +| `ruleBookInfo.intro` | `bookInfoIntroRule` | 🆕 P0 | +| `ruleBookInfo.tocUrl` | `bookInfoTocRule` | 🆕 P0 ⭐ | +| `ruleToc.chapterList` | `tocListRule` | 🆕 P0 | +| `ruleToc.chapterName` | `tocNameRule` | 🆕 P0 | +| `ruleToc.chapterUrl` | `tocUrlRule` | 🆕 P0 | +| `ruleToc.nextTocUrl` | `tocNextPageRule` | 🆕 P1 | +| `ruleContent.content` | `contentRule` | 🆕 P1 | +| `ruleContent.nextChapterUrl` | `contentNextRule` | 🆕 P1 | +| `ruleContent.prevChapterUrl` | `contentPrevRule` | 🆕 P1 | + +--- + +## 十、附录:关键文件清单 + +### 修改的文件 + +| 文件 | 角色 | P0 | P1 | P2 | +|------|------|:--:|:--:|:--:| +| `sakura_mobile/lib/src/core/legado_models.dart` | 数据模型 | 🔧 | | | +| `sakura_mobile/lib/src/core/legado_engine.dart` | 解析引擎 | 🔧 | 🔧 | 🔧 | +| `sakura_mobile/lib/src/core/legado_rule_parser.dart` | 规则解析器 | | | | +| `sakura_mobile/lib/src/pages/explore_page.dart` | 搜索页导航 | 🔧 | | | +| `sakura_mobile/lib/src/pages/online_manga_reader_page.dart` | 漫画阅读器 | | 🔧 | | +| `sakura_mobile/lib/src/pages/online_novel_reader_page.dart` | 小说阅读器 | | 🔧 | | +| `sakura_mobile/lib/src/core/providers.dart` | Riverpod | 视需要 | | | + +### 新增的文件 + +| 文件 | 角色 | P0 | P1 | P2 | +|------|------|:--:|:--:|:--:| +| `sakura_mobile/lib/src/pages/online_book_detail_page.dart` | 在线书籍详情页(合并 UI) | 🆕 | | | +| `sakura_mobile/lib/src/core/online_cache.dart` | 会话级内存缓存 | 🆕 | | | + +--- + +## 总结 + +以「能落地开发」为最终目标,本方案的核心建议是: + +1. **先修 `_shortError()`** — 把 `unknown:none` 等所有 Dio 底层异常映射为中文提示,这是最小的改动但立刻消除最明显的 bug。 + +2. **再补模型** — `LegadoSource` 补全 `ruleBookInfo`、`ruleToc`、`ruleContent` 字段,`fromJson`/`copyWith`/`toJson` 全部对齐。不补齐模型,后面所有的页面都无从谈起。 + +3. **再做合并式详情页** — `OnlineBookDetailPage` 把 BookInfo + TOC 放在同一个页面,但数据加载分两次独立请求。不跳独立 TOC 页面,用户体验更流畅。 + +4. **接章节阅读** — 阅读器接收 `chapters` + `currentChapterIndex`,从 URL 驱动改成章节驱动。这是解决"点开书直接显示乱码"问题的终点。 + +5. **最后补分页和高级规则** — 目录分页 `{{page}}`、章节预加载、阅读记录、持久化缓存——这些让体验从"能用"到"好用"。 + +``` +页面合并,解析分层; +缓存前置,持久化后置; +目录回退要识别章节,不要盲扫链接; +阅读器必须从 URL 驱动改成章节驱动。 +``` diff --git a/docs/在线搜索与阅读-源码架构总结.md b/docs/在线搜索与阅读-源码架构总结.md new file mode 100644 index 0000000..026cd53 --- /dev/null +++ b/docs/在线搜索与阅读-源码架构总结.md @@ -0,0 +1,1506 @@ +# 在线搜索与阅读 — 源码架构总结 + +> 总结日期:2026-06-21 +> 源码范围:`sakura_mobile/lib/src/core/` + `sakura_mobile/lib/src/pages/` +> 说明:本文档梳理在线搜索→详情→目录→阅读全流程的现有源码结构,不做方案设计。 + +--- + +## 目录 + +1. [文件清单与职责](#一文件清单与职责) +2. [核心类关系图](#二核心类关系图) +3. [Provider 依赖注入](#三provider-依赖注入) +4. [数据模型详解](#四数据模型详解) +5. [搜索引擎 LegadoEngine](#五搜索引擎-legadoengine) +6. [规则解析器 RuleParser](#六规则解析器-ruleparser) +7. [书源管理 SourceManager](#七书源管理-sourcemanager) +8. [统一搜索 UnifiedSearch](#八统一搜索-unifiedsearch) +9. [页面层源码](#九页面层源码) +10. [缓存与阅读记录](#十缓存与阅读记录) +11. [完整调用链路](#十一完整调用链路) + +--- + +## 一、文件清单与职责 + +### Core 层(核心引擎 + 数据模型) + +| 文件 | 行数 | 核心职责 | +|------|------|---------| +| `legado_models.dart` | 515 | 所有数据模型:`LegadoSource`、`OnlineBookInfo`、`OnlineChapter`、`OnlineChapterContent`、`OnlineReadingRecord`、`UnifiedSearchResult`、`SourceRepo` | +| `legado_engine.dart` | 956 | 在线内容引擎:HTTP 请求、搜索、BookInfo 解析、TOC 解析、章节内容解析、编码处理、错误归一化 | +| `legado_rule_parser.dart` | 262 | HTML/JSON/Regex 规则解析器:CSS 选择器、JSONPath、Regex 三种解析方式 | +| `source_manager.dart` | 368 | 书源生命周期管理:导入、启用/禁用、订阅更新、健康检查、SharedPreferences 持久化 | +| `unified_search.dart` | 173 | 统一搜索编排:合并云库搜索 + 在线书源搜索,返回 `SearchSection` 列表 | +| `online_cache.dart` | 25 | 会话级内存缓存:BookInfo、TOC、ChapterContent 三级缓存 | +| `online_reading_history.dart` | 57 | 阅读记录持久化:SharedPreferences 存储最近 50 条记录 | +| `providers.dart` | 32 | Riverpod 依赖注入:5 个全局 Provider | +| `api_client.dart` | — | 云端 API 客户端(搜索用 `gallery()` / `novels()`) | +| `session.dart` | — | 会话管理(登录态) | + +### Pages 层(UI 页面) + +| 文件 | 行数 | 核心职责 | +|------|------|---------| +| `explore_page.dart` | 503 | 在线搜索页:输入框、漫画/小说切换、结果列表展示、点击跳转 | +| `online_book_detail_page.dart` | 338 | 在线书籍详情页(合并 BookInfo + TOC UI):封面、书名、作者、简介、章节列表、正序/倒序、继续阅读 | +| `online_manga_reader_page.dart` | 208 | 在线漫画阅读器:PageView 图片浏览、上一章/下一章/目录导航、预加载、阅读记录 | +| `online_novel_reader_page.dart` | 195 | 在线小说阅读器:文本展示、字体缩放、上一章/下一章/目录导航、预加载、阅读记录 | + +--- + +## 二、核心类关系图 + +``` +┌──────────────────────────────────────────────────────────────┐ +│ providers.dart │ +│ ┌─────────────┐ ┌──────────────┐ ┌───────────────────┐ │ +│ │ Session │ │SourceManager │ │ LegadoEngine │ │ +│ │ Controller │ │ (书源管理) │ │ (在线内容引擎) │ │ +│ └──────┬───────┘ └──────┬───────┘ └────────┬──────────┘ │ +│ │ │ │ │ +│ ┌──────▼───────┐ │ │ │ +│ │SakuraApiClient│ │ │ │ +│ │ (云端API) │ │ │ │ +│ └──────┬───────┘ │ │ │ +│ │ │ │ │ +│ ┌──────▼─────────────────▼────────────────────▼──────────┐ │ +│ │ UnifiedSearch │ │ +│ │ (编排:cloudFuture + onlineFuture → SearchSection) │ │ +│ └────────────────────────┬──────────────────────────────┘ │ +└───────────────────────────┼────────────────────────────────┘ + │ + ┌───────────────────┼───────────────────┐ + ▼ ▼ ▼ + ExplorePage OnlineBookDetailPage OnlineXxxReaderPage + (搜索UI) (详情+目录UI) (阅读UI) +``` + +### LegadoEngine 内部结构 + +``` +LegadoEngine +├── Dio _http ← HTTP 客户端(15s 连接超时,20s 接收超时) +├── OnlineContentCache cache ← 会话级缓存 +│ +├── 搜索 ─────────────────────────── +│ ├── search() ← 单源搜索(构建请求 → 获取响应 → 解析结果) +│ ├── searchAll() ← 多源并发搜索(每批 10 个,Future.wait) +│ ├── _buildSearchRequest() ← 构建搜索 URL + 编码关键词 +│ ├── _applySearchTemplate() ← 替换 {{key}} / {{page}} 占位符 +│ └── _fallbackSearchResults() ← 回退:扫描 标签 +│ +├── 详情 ─────────────────────────── +│ ├── fetchBookInfo() ← 解析书籍详情页 → OnlineBookInfo +│ +├── 目录 ─────────────────────────── +│ ├── fetchToc() ← 解析章节目录(带缓存) +│ ├── _fetchPaginatedToc() ← 分页目录加载(最多 20 页) +│ └── _fallbackChapters() ← 回退:章节识别器扫描 +│ +├── 内容 ─────────────────────────── +│ ├── fetchChapterContent() ← 解析章节内容 → OnlineChapterContent +│ ├── fetchPlainText() ← 旧 API:从 URL 提取纯文本(无规则) +│ ├── fetchImageUrls() ← 旧 API:从 URL 提取图片(无规则) +│ └── _extractImageUrls() ← 图片提取(含 data-src/data-original) +│ +├── 编解码 ───────────────────────── +│ ├── _encodeKeyword() ← URL 关键词编码(GBK/UTF-8/平台通道) +│ ├── _decodeBody() ← 响应体解码(GBK/UTF-8/Latin1) +│ └── _charsetFromContentType() ← 从 Content-Type 提取 charset +│ +├── HTTP ─────────────────────────── +│ ├── _fetchBody() ← 统一 HTTP 请求(GET/POST,字节响应 + 状态码检查) +│ ├── _debugDioException() ← Dio 异常日志 +│ └── _debugHttpResponse() ← HTTP 错误日志 +│ +├── 工具 ─────────────────────────── +│ ├── _resolveUrl() ← 相对 URL → 绝对 URL +│ ├── resolveResourceUrl() ← 公开版 URL 解析 +│ ├── _htmlToText() ← HTML → 纯文本 +│ ├── _plainTextFromJson() ← JSON → 提取文本字段 +│ ├── _imageUrlsFromJson() ← JSON → 提取图片 URL +│ ├── _safeSelect() / _safeSelectList() ← 规则解析安全包装 +│ ├── _shortError() ← 错误消息归一化(中文映射表) +│ └── parseSources() ← JSON 文本 → List +│ +└── 数据类 ───────────────────────── + └── _LegadoRequest { method, url, body } +``` + +--- + +## 三、Provider 依赖注入 + +**文件**:`sakura_mobile/lib/src/core/providers.dart` + +```dart +// 1. 会话管理(登录态)— StateNotifier +final sessionProvider = StateNotifierProvider(...); + +// 2. 云端 API 客户端 — 依赖 sessionProvider +final apiClientProvider = Provider((ref) { + ref.watch(sessionProvider); + return ref.read(sessionProvider.notifier).client(); +}); + +// 3. 书源管理器 — ChangeNotifier(UI 可监听 busy/message 状态) +final sourceManagerProvider = ChangeNotifierProvider((ref) { + final manager = SourceManager(); + manager.initializeIfFirstRun(); // 首次运行提示导入书源 + return manager; +}); + +// 4. 在线引擎 — 单例 +final legadoEngineProvider = Provider((ref) => LegadoEngine()); + +// 5. 统一搜索 — 组合 apiClient + sourceManager + engine +final unifiedSearchProvider = Provider((ref) { + return UnifiedSearch( + api: ref.watch(apiClientProvider), + sourceManager: ref.watch(sourceManagerProvider), + engine: ref.watch(legadoEngineProvider), + ); +}); +``` + +**Provider 消费方式**(在各页面中): + +```dart +// 获取一次值(不监听变化) +final engine = ref.read(legadoEngineProvider); + +// 监听变化(UI 自动刷新) +final manager = ref.watch(sourceManagerProvider); +``` + +--- + +## 四、数据模型详解 + +### 4.1 LegadoSource(书源规则) + +**文件**:`legado_models.dart:21-306` + +完整的 Legado 书源模型,包含四个规则模块。 + +**构造函数参数(共 29 个字段)**: + +```dart +class LegadoSource { + // === 基础字段 === + final String id; // 派生自 name+url 的 hash + final String name; // bookSourceName + final String url; // bookSourceUrl + final int type; // 0=小说, 2=漫画 + final bool enabled; + + // === HTTP 配置 === + final String? userAgent; + final Map headers; // 含 Cookie + final String? charset; // gbk / utf-8 + final String? urlEncoding; // 关键词 URL 编码字符集 + final String? ruleKind; // css / json / regex + + // === Search 规则(ruleSearch)=== + final String? searchUrl; // 搜索 URL 模板,含 {{key}} {{page}} + final String? searchListRule; // 结果条目列表选择器 + final String? searchNameRule; // 书名选择器 + final String? searchAuthorRule; // 作者选择器 + final String? searchCoverRule; // 封面选择器 + final String? searchDetailRule; // 详情链接选择器 → detailUrl + final String? searchMethod; // GET / POST + final String? searchBody; // POST body 模板 + + // === BookInfo 规则(ruleBookInfo)=== + final String? bookInfoUrl; // 详情页 URL 模板 + final String? bookInfoNameRule; + final String? bookInfoAuthorRule; + final String? bookInfoCoverRule; + final String? bookInfoIntroRule; + final String? bookInfoTocRule; // ⭐ 目录页 URL 选择器 + + // === TOC 规则(ruleToc)=== + final String? tocUrl; // 目录页 URL 模板(支持 {{page}}) + final String? tocListRule; // 章节条目列表选择器 + final String? tocNameRule; // 章节名称选择器 + final String? tocUrlRule; // 章节链接选择器 + final String? tocNextPageRule; // 下一页目录选择器 + + // === Content 规则(ruleContent)=== + final String? contentRule; // 正文内容选择器 + final String? contentNextRule; // 下一章链接选择器 + final String? contentPrevRule; // 上一章链接选择器 +} +``` + +**类型判断**: +- `isNovel` → `type == 0` +- `isManga` → `type == 2` + +**fromJson 解析逻辑**(`legado_models.dart:130-261`): +1. 从 `json['ruleSearch']` 提取搜索规则(13 种 key 备选) +2. 从 `json['ruleBookInfo']` 提取详情规则(4 种 key 备选) +3. 从 `json['ruleToc']` 提取目录规则(5 种 key 备选) +4. 从 `json['ruleContent']` 提取内容规则(3 种 key 备选) +5. 从 `json['header']` / `json['headers']` 提取请求头(支持 Map / JSON 字符串 / 冒号分隔行三种格式) +6. 从 `json['cookie']` 合并 Cookie 到 headers + +**copyWith 方法**(`legado_models.dart:93-128`): +- 唯一可变参数:`enabled` +- 所有规则字段原样传递(不可变对象) + +**toJson 方法**(`legado_models.dart:263-305`): +- 输出完整的 Legado 兼容 JSON +- 包含 `ruleSearch` / `ruleBookInfo` / `ruleToc` / `ruleContent` 四个顶层 key +- 空值不输出(`if (xxx != null)` 条件添加) + +**字段名对照表**(输出 JSON key → Legado 标准名): + +| 内部字段 | JSON key | +|---------|----------| +| `name` | `bookSourceName` | +| `url` | `bookSourceUrl` | +| `type` | `bookSourceType` | +| `searchListRule` | `ruleSearch.bookList` | +| `bookInfoTocRule` | `ruleBookInfo.tocUrl` | +| `tocListRule` | `ruleToc.chapterList` | +| `contentRule` | `ruleContent.content` | + +--- + +### 4.2 OnlineBookInfo(运行时书籍详情) + +```dart +class OnlineBookInfo { + final String title; // 书名(来自 bookInfoNameRule 解析或搜索结果的 initialTitle) + final String? author; + final String? coverUrl; + final String? intro; // 简介 + final String tocUrl; // 目录页 URL ⭐ + final String sourceName; // 来源书源名称 +} +``` + +**来源**:`LegadoEngine.fetchBookInfo()` 解析 `detailUrl` 指向的页面。 + +--- + +### 4.3 OnlineChapter(运行时章节条目) + +```dart +class OnlineChapter { + final String title; // 章节标题 + final String url; // 章节内容 URL + final int index; // 序号(从 1 开始) +} +``` + +**来源**:`LegadoEngine.fetchToc()` 解析目录页。 + +--- + +### 4.4 OnlineChapterContent(运行时章节内容) + +```dart +class OnlineChapterContent { + final String title; // 章节标题 + final String? textContent; // 小说正文(纯文本) + final List imageUrls; // 漫画图片 URL 列表 + final String? nextChapterUrl; // 下一章 URL(contentNextRule 解析结果) + final String? prevChapterUrl; // 上一章 URL +} +``` + +**来源**:`LegadoEngine.fetchChapterContent()` 解析章节页。 + +--- + +### 4.5 OnlineReadingRecord(阅读记录) + +```dart +class OnlineReadingRecord { + final String sourceId; // 书源 ID + final String sourceName; // 书源名称 + final String bookTitle; // 书名 + final String detailUrl; // 书籍详情页 URL + final String chapterUrl; // 当前阅读的章节 URL + final String chapterTitle; // 当前阅读的章节标题 + final int chapterIndex; // 章节序号(0-based) + final int totalChapters; // 总章节数 + final DateTime lastReadTime; // 最后阅读时间 +} +``` + +**存储**:`OnlineReadingHistory` → SharedPreferences JSON 数组,最多 50 条。 + +--- + +### 4.6 UnifiedSearchResult(搜索结果条目) + +```dart +class UnifiedSearchResult { + final String sourceId; // 书源 ID + final String sourceName; // 书源名称 + final String title; // 书名 + final String? author; + final String? coverUrl; + final String? detailUrl; // 书籍详情页 URL(非章节 URL) + final String? errorMessage; // 非空表示搜索失败 + + bool get isError => errorMessage?.trim().isNotEmpty == true; +} +``` + +**两种构造方式**: +- 正常结果:`UnifiedSearchResult(sourceId:, sourceName:, title:, ...)` +- 错误结果:`UnifiedSearchResult.error(sourceId:, sourceName:, errorMessage:)` → title 固定为"搜索失败" + +--- + +### 4.7 SearchItem / SearchSection(搜索页 UI 数据) + +```dart +class SearchItem { + final String title; + final String sourceName; + final String? author; + final String? coverUrl; + final String? detailUrl; + final Artwork? artwork; // 云库漫画 + final Novel? novel; // 云库小说 + final UnifiedSearchResult? online; // 在线源结果 + final String? errorMessage; + + bool get isCloud => artwork != null || novel != null; + bool get isError => errorMessage?.trim().isNotEmpty == true; +} + +class SearchSection { + final String id; // 'cloud' | 'online' + final String title; // '我的云库' | '在线源' + final List items; +} +``` + +--- + +## 五、搜索引擎 LegadoEngine + +**文件**:`legado_engine.dart`(956 行) + +### 5.1 初始化 + +```dart +class LegadoEngine { + LegadoEngine({Dio? http}) + : _http = http ?? Dio(BaseOptions( + connectTimeout: Duration(seconds: 15), + receiveTimeout: perSourceTimeout, // 20s + headers: { + 'User-Agent': 'Mozilla/5.0 ... Chrome/120 ...', + 'Accept': 'text/html,...', + 'Accept-Language': 'zh-CN,zh;q=0.9', + }, + )); + + static const maxConcurrency = 10; // 单批并发数 + static const perSourceTimeout = Duration(seconds: 20); + final OnlineContentCache cache = OnlineContentCache(); // 会话缓存实例 +} +``` + +### 5.2 搜索方法 + +#### search() — 单源搜索 + +``` +输入: LegadoSource, keyword, page + ↓ +1. 检查 searchUrl / searchListRule 非空 +2. _buildSearchRequest(source, searchUrl, keyword, page) + → _encodeKeyword(source, keyword) // GBK/UTF-8 百分号编码 + → _applySearchTemplate(template, encodedKeyword, page) + // 替换 {{key}} {{keyword}} {key} {keyword} + // searchKey %s {{page}} {page} + → resolveResourceUrl(source.url, rawUrl) // 相对→绝对 + → 返回 _LegadoRequest(method, url, body) +3. _fetchBody(source, url) // HTTP GET/POST +4. RuleParser(body) 解析 +5. _safeSelectList(parser, searchListRule) → 提取结果条目片段 +6. 每个片段提取: title/author/coverUrl/detailUrl +7. 过滤空 title +8. 如果规则解析无结果 → _fallbackSearchResults() +``` + +#### searchAll() — 多源并发搜索 + +``` +输入: List, keyword + ↓ +循环: start=0; start < sources.length; start += 10 + 每批最多 10 个源,Future.wait 并发 + 每个源: search(source, keyword) + .timeout(20s) + .catchError() → UnifiedSearchResult.error() + 结果追加到 results 列表 +``` + +**关键**:`searchAll` 不做跨源去重。同一本书出现在 5 个书源中 → 5 条结果。 + +**错误处理**: +- 超时 → 返回带 `errorMessage` 的错误结果,不影响其他源 +- 其他异常 → 同上 + +#### _fallbackSearchResults() — 搜索回退 + +当 `searchListRule` 解析不到结果时触发: +1. 扫描 HTML 中所有 `` 标签 +2. 标题必须包含搜索关键词(完全匹配) +3. 过滤 `#` 和 `javascript:` 链接 +4. 最多返回 30 条 + +#### _applySearchTemplate() — 占位符替换 + +支持的占位符(按替换优先级): + +```dart +{{key}} → encodedKeyword {{page}} → pageText +{{keyword}} → encodedKeyword {page} → pageText +{{searchKey}} → encodedKeyword → pageText +{key} → encodedKeyword +{keyword} → encodedKeyword +{searchKey} → encodedKeyword + → encodedKeyword + → encodedKeyword + → encodedKeyword +searchKey → encodedKeyword // 无包裹的字符串直接替换 +%s → encodedKeyword // printf 风格(仅替换第一个) +``` + +### 5.3 详情方法 + +#### fetchBookInfo() — 解析书籍详情 + +``` +输入: LegadoSource, detailUrl, {initialTitle, initialAuthor, initialCoverUrl} + ↓ +1. 查缓存: cache.getBookInfo(source.id, absoluteUrl) +2. _fetchBody(source, absoluteUrl) +3. RuleParser(body) → 逐一提取: + → bookInfoNameRule → parsedTitle + → bookInfoAuthorRule → parsedAuthor + → bookInfoCoverRule → parsedCover + → bookInfoIntroRule → parsedIntro + → bookInfoTocRule → parsedToc +4. 确定 tocUrl: + - 优先: parsedToc + - 其次: source.tocUrl + - 兜底: absoluteUrl (即 detailUrl 本身) +5. title 兜底: initialTitle → '未命名作品' +6. 写入 bookInfoCache +7. 返回 OnlineBookInfo +``` + +### 5.4 目录方法 + +#### fetchToc() — 解析章节目录 + +``` +输入: LegadoSource, tocUrl + ↓ +1. 查缓存: cache.getToc(source.id, absoluteUrl) +2. 未命中 → _fetchPaginatedToc(source, absoluteUrl) +3. 写入 tocCache +4. 返回 List +``` + +#### _fetchPaginatedToc() — 分页目录加载 + +``` +输入: LegadoSource, template (含 {{page}} 的 URL) + ↓ +初始化: chapters=[], seenUrls={}, seenPages={}, nextUrl=template, page=1 +循环条件: page <= 20 && nextUrl != null + 1. 替换 {{page}} / {page} → requestedUrl + 2. seenPages 去重 → break + 3. _fetchBody → body + 4. RuleParser(body) → tocListRule → fragments + 5. 遍历 fragments: + → tocNameRule → title + → tocUrlRule → url + → seenUrls 去重 + → chapters.add(OnlineChapter(title, url, index)) + 6. 如果 fragments 为空: + → _fallbackChapters(body) // 章节识别器回退 + 7. 如果本页新增 0 条 → break + 8. 检查 tocNextPageRule → 有则更新 nextUrl + 否则如果 template 含 {{page}} → 继续 page++ + 否则 → nextUrl = null(停止) +返回 chapters +``` + +#### _fallbackChapters() — 章节识别器回退 + +当 `tocListRule` 不可用时触发: +1. 正则匹配 `` 标签 +2. 标题必须匹配章节特征正则: + ```dart + r'(第\s*[0-9一二三四五六七八九十百千万零〇两]+\s*[章节卷回话]|chapter\s*\d+|episode\s*\d+)' + ``` +3. 按出现顺序排列 + +**与搜索回退的区别**:搜索回退要求标题包含搜索关键词,目录回退要求标题匹配章节正则。后者更精准,避免把导航链接当章节。 + +### 5.5 内容方法 + +#### fetchChapterContent() — 解析章节内容 + +``` +输入: LegadoSource, OnlineChapter + ↓ +1. 查缓存: cache.getChapter(source.id, absoluteUrl) +2. _fetchBody → body +3. RuleParser(body) → contentRule → content(主内容) +4. 如果是漫画: + → contentRule 提取 fragments 列表 + → 合并 fragments 为 contentBody +5. 如果是小说: + → contentBody = content.isEmpty ? body : content +6. 提取导航: + → contentNextRule → nextChapterUrl + → contentPrevRule → prevChapterUrl +7. 构建 OnlineChapterContent: + - 小说: textContent = _plainTextFromJson(contentBody) || _htmlToText(contentBody) + - 漫画: imageUrls = _extractImageUrls(contentBody, absoluteUrl) + - nextChapterUrl / prevChapterUrl +8. 写入 chapterCache +9. 返回 OnlineChapterContent +``` + +#### fetchPlainText() — 旧 API(无规则) + +``` +输入: LegadoSource, url + ↓ +_fetchBody → body +_json 文本提取优先 → _plainTextFromJson(body) +回退 HTML → _htmlToText(body) +``` + +#### fetchImageUrls() — 旧 API(无规则) + +``` +输入: LegadoSource, url + ↓ +如果 url 本身是图片 → 直接返回 [url] +否则 _fetchBody → body + HTML img 扫描: + JSON 图片扫描: _imageUrlsFromJson(body) + 合并去重 +``` + +两者都是旧 API,不依赖 `contentRule`,直接从 HTML/JSON 中暴力提取。 + +### 5.6 HTTP 层 + +#### _fetchBody() — 统一 HTTP 请求 + +``` +输入: LegadoSource, url, {method, data} + ↓ +1. 校验 URL(非空、有 scheme) +2. 构建 headers: + - Referer: source.url + - ...source.headers(含 Cookie) + - User-Agent: source.userAgent ?? 默认 UA +3. 发起请求: + - GET: _http.get>(url, options: ResponseType.bytes, followRedirects: true, validateStatus: _ => true) + - POST: _http.post>(url, data: data, ...) +4. 捕获 DioException → _debugDioException() 打日志 → rethrow +5. 检查 statusCode: + - 404 → throw 'Source search endpoint is missing or expired.' + - 403/444 → throw 'Source refused access...' + - 其他 >= 400 → throw 'HTTP $code from $host.' +6. 解码响应体: _decodeBody(bytes, declaredCharset: source.charset, contentType: headers['content-type']) +``` + +**关键设计**: +- `ResponseType.bytes` — 始终以字节数组接收,避免 Dio 自动用错误编码解码 +- `validateStatus: (_) => true` — 不抛 DioException,自己检查状态码 +- `followRedirects: true` — 自动跟随重定向 + +### 5.7 编解码层 + +#### _encodeKeyword() — URL 关键词编码 + +``` +输入: LegadoSource, keyword + ↓ +确定编码: source.urlEncoding ?? source.charset ?? 'utf-8' + ↓ +utf-8 → Uri.encodeQueryComponent(keyword) +gbk/gb2312/gb18030 → gbk.encode(keyword) → 手动百分号编码 +其他编码 → _encodingChannel.invokeMethod('encodeQuery') → 平台原生编码 + MissingPluginException → throw +``` + +#### _decodeBody() — 响应体解码 + +``` +输入: List bytes, declaredCharset, contentType + ↓ +确定 charset: declaredCharset ?? _charsetFromContentType(contentType) + ↓ +latin1/iso-8859-1/windows-1252 → latin1.decode(bytes) +gbk/gb2312/gb18030 → gbk.decode(bytes) // FormatException → latin1 兜底 +其他/未指定 → utf8.decode(bytes) // FormatException → latin1 兜底 +``` + +#### _charsetFromContentType() — 提取 charset + +```dart +// 从 "text/html; charset=gbk" 中提取 "gbk" +RegExp(r'charset=([^;]+)') +``` + +### 5.8 错误处理 + +#### _shortError() — 错误消息归一化 + +完整的错误消息映射链(按优先级): + +``` +1. DioException 类型匹配: + connectionTimeout/receiveTimeout/sendTimeout + → '连接 {host} 超时,请稍后重试。' + connectionError + → '无法连接 {host},请检查网络或更换书源。' + badResponse + 404 + → '书源搜索接口已失效(404)。' + badResponse + 403/444 + → '{host} 拒绝了访问,可能需要 Cookie 配置。' + badResponse + 其他 + → '{host} 返回了异常状态(HTTP {code})。' + +2. FormatException: + 含 utf/encoding → '响应编码异常,请检查书源的 charset 配置。' + 其他 → '书源规则期望 JSON 格式,但站点返回了其他内容。' + +3. 原始字符串归一化: + '[unknown]: null' / 'null' → '目标站点返回了空响应...' + '[unknown]: none' / 含 'unknown:none' → '连接异常,书源可能已失效。' + 含 'connection refused' → '目标站点拒绝了连接。' + 含 'connection timed out' → '连接超时,请稍后重试。' + 含 'no address associated' → '无法解析目标站点域名,请检查网络。' + +4. 兜底: + message > 120 字符 → 截断 + 空字符串 → '未知错误' +``` + +--- + +## 六、规则解析器 RuleParser + +**文件**:`legado_rule_parser.dart`(262 行) + +### 6.1 支持的三种解析类型 + +通过 `_resolveKind()` 自动识别: + +``` +规则前缀: + "json:" 或 "$." → json 模式 + "regex:" → regex 模式 + 其他 → css 模式(默认) +``` + +### 6.2 CSS 模式 + +#### selectList() — 多元素选择 + +``` +1. 清理选择器(去掉 @attr 后缀) +2. html_parser.parse(body) → document +3. document.querySelectorAll(cleanSelector) +4. 每个元素 → outerHtml +5. 如果 querySelectorAll 抛异常(畸形选择器)→ 降级到 _legacyCssFragments() +``` + +#### select() — 单元素选择 + +``` +1. 检查 @attr 后缀(如 "img@src" → 提取 src 属性值) +2. html_parser.parse(fragment) → document +3. document.querySelector(cleanSelector) +4. 有 @attr → element.attributes[attr] +5. 无 @attr → element.text +6. 如果 querySelector 抛异常 → 降级到 _legacyCssText() +``` + +#### _legacyCssFragments() — 正则降级 + +``` +提取选择器开头的 tag 名 → 正则匹配 ... → 返回 outerHtml +``` + +#### _legacyCssText() — 正则降级 + +``` +提取 tag 名 → 正则匹配 → @attr 提取属性值 / 默认提取 innerText +``` + +### 6.3 JSON 模式(JSONPath) + +**支持的语法**: +``` +$.data → 取 data 字段 +$.data.chapters → 取 data.chapters 字段 +$.data.chapters[*] → 取 chapters 数组全部元素 +$.data.items[0] → 取 items 数组第 1 个元素 +$.data.list[2].name → 取 list[2].name +["complex-key"] → 支持引号包裹的 key(自动转为 .key) +``` + +**JsonPath 解析流程**(`_jsonPath()`): +``` +1. 去掉前缀: json: 或 $. +2. 按 . 分割 path +3. 逐段遍历: + - segment 以 [*] 结尾 → 取数组全量 + - segment 含 [n] → 取数组第 n 个元素 + - 普通 key → _child(node, key) +4. _child() 递归: + - key 为空 → 返回 node + - node is Map → node[key] + - node is List → node.map(item => _child(item, key)) +``` + +**JSON Body 预处理**(`_jsonBody()`): +``` +1. 去掉 UTF-8 BOM(0xFEFF) +2. 检测 JSONP 格式: callback({...}) + → 正则提取括号内 JSON +3. jsonDecode +``` + +### 6.4 Regex 模式 + +**selectList() — Regex 全量匹配**: +``` +去掉 "regex:" 前缀 +RegExp(rule, dotAll: true).allMatches(body) + → 有捕获组: match.group(1) + → 无捕获组: match.group(0) +过滤空值 +``` + +**select() — Regex 首次匹配**: +``` +去掉 "regex:" 前缀 +RegExp(rule, dotAll: true).firstMatch(fragment) + → match.group(1) ?? match.group(0) +``` + +### 6.5 规则多值支持 + +`_ruleAlternatives()` — 支持 `||` 分隔的多个规则备选: +```dart +// "div.title || h1.title || .book-name" → 三个规则依次尝试 +// 第一个有结果的规则立即返回 +``` + +--- + +## 七、书源管理 SourceManager + +**文件**:`source_manager.dart`(368 行) + +### 7.1 生命周期 + +``` +构造 → load() → initializeIfFirstRun() + ↓ + SharedPreferences 读取已保存书源 + ↓ + _loaded = true → notifyListeners() + ↓ + startAutoUpdateTimer() // 每 6 小时检查订阅更新 +``` + +### 7.2 书源导入 + +#### importFromRepo(SourceRepo) — 从仓库导入 + +``` +1. 如果是订阅源 → _addSubscription(url) +2. HTTP GET repo.url +3. importFromJsonText(response.body) +``` + +#### importFromUrl(String) — 从自定义 URL 导入 + +``` +HTTP GET url → importFromJsonText(response.body) +``` + +#### importFromJsonText(String) — 解析 JSON 并入库 + +``` +1. _engine.parseSources(body) → List +2. 遍历 imported: + - _sources.indexWhere(id 匹配) → 更新现有源 + - 否则 → 追加新源 +3. _save() → SharedPreferences +4. notifyListeners() +``` + +#### parseSources() — JSON → 书源列表 + +``` +1. 检测 HTML(body 以 get sources; + +// 已启用书源 +List get enabledSources; + +// 已启用的小说源(type == 0) +List get enabledNovelSources; + +// 已启用的漫画源(type == 2) +List get enabledMangaSources; + +// 按 ID 查找 +LegadoSource? sourceById(String sourceId); +``` + +### 7.4 书源状态管理 + +```dart +// 启用/禁用某个书源 +setEnabled(sourceId, enabled) → _save() → notifyListeners() + +// 健康检查(HTTP GET 搜索 URL,最多检查 10 个源) +checkEnabledSources() → Future.wait(targets.map(_checkSource)) → List +``` + +### 7.5 持久化 + +```dart +// 存储 key +_sourcesKey = 'legado_sources_v1' +_initializedKey = 'source_manager_initialized_v1' +_subscriptionsKey = 'legado_subscription_urls_v1' +_lastSubscriptionUpdateKey = 'legado_last_subscription_update_v1' + +// 书源存储格式 +jsonEncode(_sources.map(source.toJson()).toList()) +// 即完整的 Legado 兼容 JSON 数组,含 ruleSearch/ruleBookInfo/ruleToc/ruleContent +``` + +--- + +## 八、统一搜索 UnifiedSearch + +**文件**:`unified_search.dart`(173 行) + +### 8.1 搜索编排 + +```dart +Future> search({query, mode}) { + // 1. 并行发起两个搜索 + cloudFuture = mode==manga ? _searchCloudManga(query) : _searchCloudNovels(query) + onlineFuture = engine.searchAll(sources, query) + .then(items => items.map(SearchItem.online)) + + // 2. 等待两个 Future 都完成(Future.wait) + results = await Future.wait([cloudFuture, onlineFuture]) + + // 3. 组装 SearchSection + return [ + if (results[0].isNotEmpty) SearchSection(id: 'cloud', title: '我的云库', items: results[0]), + if (results[1].isNotEmpty) SearchSection(id: 'online', title: '在线源', items: results[1]), + ] +} +``` + +### 8.2 云库搜索 + +```dart +_searchCloudManga(query) → api.gallery(search: query, contentType: 'manga') + → rows.map(Artwork.fromJson).map(SearchItem.cloudArtwork) + +_searchCloudNovels(query) → api.novels(search: query) + → rows.map(Novel.fromJson).map(SearchItem.cloudNovel) +``` + +### 8.3 SearchItem 工厂方法 + +```dart +SearchItem.cloudArtwork(artwork) → title, sourcePlatform, author, coverUrl, artwork +SearchItem.cloudNovel(novel) → title, sourcePlatform, author, coverPath, novel +SearchItem.online(result) → title, sourceName, author, coverUrl, detailUrl, online, errorMessage +SearchItem.error(sourceName, msg)→ title='搜索失败', sourceName, errorMessage +``` + +--- + +## 九、页面层源码 + +### 9.1 ExplorePage — 在线搜索页 + +**文件**:`explore_page.dart`(503 行) + +**状态管理**: +```dart +final search = TextEditingController(); // 搜索输入框 +late UnifiedSearchMode mode; // manga / novel +Future>? future; // 搜索结果 Future +final savingOnline = {}; // 正在保存的 item key 集合 +``` + +**搜索触发**: +```dart +_submit() { + query = search.text.trim(); + if (query.isEmpty) return; + setState(() { + future = ref.read(unifiedSearchProvider).search(query: query, mode: mode); + }); +} +``` + +**模式切换**(漫画 ↔ 小说): +```dart +SegmentedButton → onSelectionChanged → mode = newMode; future = null; +``` + +**搜索状态展示**(FutureBuilder 四种状态): +``` +future == null → 空状态(搜索引导文案) +ConnectionState.waiting → CircularProgressIndicator +snapshot.hasError → 错误状态(cloud_off 图标 + 文案) +sections.isEmpty → 无结果状态(search_off 图标 + 文案) +有结果 → Column: section 标题 + SearchResultTile 列表 +``` + +**搜索结果点击 — _openItem()**: +``` +item.artwork != null + → ArtworkDetailPage(artwork) // 云库漫画:本地详情页 + +item.novel != null + → NovelReaderPage(novel) // 云库小说:本地阅读器 + +item.isError + → SnackBar(errorMessage) // 错误提示 + +online != null + → source = sourceManager.sourceById(online.sourceId) + → source == null → SnackBar('书源已不可用') + → OnlineBookDetailPage( // 🆕 在线结果 → 详情页 + source, initialTitle, initialAuthor, + initialCoverUrl, detailUrl, onSave + ) +``` + +**保存到书架 — _saveOnlineItem()**: +``` +1. fetchBookInfo(source, detailUrl) → info +2. fetchToc(source, info.tocUrl) → chapters +3. fetchChapterContent(source, chapters.first) → content +4. 漫画: api.importOnlineManga(...) → 跳转到 ArtworkDetailPage +5. 小说: api.importOnlineNovel(...) → 跳转到 NovelReaderPage +6. 异常: SnackBar('保存失败:$error') +``` + +**UI 组件**: +- `_SourceSummary` — 已启用书源数量卡片 +- `_SearchResultTile` — 搜索结果条目(封面缩略图 + 标题 + 作者/来源 + 保存按钮) + - subtitle 三要素:`作者(未知作者兜底) - 来源名称 - 云库/在线` + - 错误条目:粉色背景 + warning 图标 + errorMessage +- `_ResultThumb` — 封面缩略图(CachedNetworkImage,160×220,有 fallback 图标) +- `_ExploreEmptyState` — 空状态占位 + +--- + +### 9.2 OnlineBookDetailPage — 在线书籍详情页 + +**文件**:`online_book_detail_page.dart`(338 行) + +**接收参数**: +```dart +source: LegadoSource // 书源 +detailUrl: String // 书籍详情页 URL +initialTitle: String // 搜索结果中的书名(兜底) +initialAuthor: String? // 搜索结果中的作者(兜底) +initialCoverUrl: String? // 搜索结果中的封面(兜底) +onSave: Future Function()? // 保存回调 +``` + +**状态管理**: +```dart +OnlineBookInfo? info; // BookInfo 解析结果 +List chapters; // 章节列表 +Object? infoError; // BookInfo 加载错误 +Object? tocError; // TOC 加载错误 +bool loadingInfo = true; // BookInfo 加载中 +bool loadingToc = true; // TOC 加载中 +bool descending = false; // 章节排序(正序/倒序) +bool saving = false; // 保存中 +OnlineReadingRecord? readingRecord; // 阅读记录 +``` + +**加载流程 — _load()**: +``` +1. detailUrl 为空 → 显示错误 +2. engine.fetchBookInfo(source, detailUrl, ...) → info +3. setState: loadingInfo=false, loadingToc=true +4. _loadToc(info.tocUrl) +5. OnlineReadingHistory.find(sourceId, detailUrl) → readingRecord +6. 异常 → setState: infoError=error, loadingInfo=false, loadingToc=false +``` + +**分段加载 — _loadToc(url)**: +``` +1. setState: tocError=null, loadingToc=true +2. engine.fetchToc(source, url) → chapters +3. setState: loadingToc=false +4. 异常 → setState: tocError=error, loadingToc=false +``` + +**关键设计**:BookInfo 和 TOC 是两次独立的 HTTP 请求(因为 detailUrl ≠ tocUrl),但 UI 合并在同一页面。BookInfo 先渲染,TOC 区域独立 loading。 + +**章节点击 — _openChapter(index)**: +``` +source.isManga + → OnlineMangaReaderPage(source, title, chapters, currentChapterIndex: index, detailUrl) + → push + → 回来后重新查阅读记录 → 刷新已读标记 + +source.isNovel + → OnlineNovelReaderPage(同上) +``` + +**页面结构**: +``` +AppBar: '书籍详情' + ↓ +上半部分(BookInfo 渲染): + Row: + 封面 (CachedNetworkImage, 104×142) + Column: 书名(headlineSmall) + 作者 + 来源(灰色) + 章节数(粉色) + 简介 ExpansionTile(可展开/收起) + 操作按钮: + FilledButton: '从第一章开始阅读' / '继续阅读'(根据 readingRecord) + OutlinedButton: '保存到书架'(条件显示) + +下半部分(TOC 渲染): + Row: '章节目录(共 N 章)' + 正序/倒序切换按钮 + 三种状态: + loading → CircularProgressIndicator + error → _ErrorState(重试按钮) + chapters.isEmpty → 空状态文案 + 有数据 → ListView of ListTile: + - leading: menu_book 图标 + - title: 章节名称 + - trailing: 已读 ✓ 或 chevron_right + - onTap: _openChapter(chapter.index - 1) +``` + +--- + +### 9.3 OnlineMangaReaderPage — 在线漫画阅读器 + +**文件**:`online_manga_reader_page.dart`(208 行) + +**接收参数**: +```dart +source: LegadoSource +title: String // 书名 +chapters: List // 完整章节列表 +currentChapterIndex: int // 当前章节序号(0-based) +detailUrl: String // 书籍详情 URL(用于阅读记录) +``` + +**状态管理**: +```dart +late int chapterIndex = widget.currentChapterIndex; +late Future contentFuture = _load(); +final pageController = PageController(); +int pageIndex = 0; // 当前漫画页(PageView 内) +``` + +**加载流程 — _load()**: +``` +1. engine.fetchChapterContent(source, chapter) → content +2. OnlineReadingHistory.save(OnlineReadingRecord(...)) +3. 预加载下一章: + next = chapterIndex + 1 + if next < chapters.length: + engine.fetchChapterContent(source, chapters[next]) + .catchError(...) // 预加载失败静默忽略 +4. return content +``` + +**章节切换 — _move(delta)**: +``` +target = chapterIndex + delta +if target < 0 || target >= chapters.length → return +setState: + chapterIndex = target + pageIndex = 0 + contentFuture = _load() +pageController.jumpToPage(0) +``` + +**页面结构**: +``` +AppBar: '章节标题(3/336)' + ↓ +FutureBuilder: + error → _MangaState('这个章节暂时没有加载成功。', retry) + loading → CircularProgressIndicator + data.imageUrls.isEmpty → _MangaState('这一章还没有找到漫画页面...', retry) + data.imageUrls: + Stack: + PageView.builder: + InteractiveViewer → Image.network(pages[index], headers: source.headers) + Positioned(右下角): '3/18' 页数指示器 + +bottomNavigationBar: + [← 上一章] [📋 目录(pop)] [下一章 →] + - 上一章: chapterIndex > 0 可用 + - 下一章: chapterIndex < chapters.length - 1 可用 +``` + +--- + +### 9.4 OnlineNovelReaderPage — 在线小说阅读器 + +**文件**:`online_novel_reader_page.dart`(195 行) + +**接收参数**: +```dart +source: LegadoSource +title: String // 书名 +chapters: List // 完整章节列表 +currentChapterIndex: int // 当前章节序号(0-based) +detailUrl: String // 书籍详情 URL(用于阅读记录) +``` + +**状态管理**: +```dart +late int chapterIndex = widget.currentChapterIndex; +late Future contentFuture = _load(); +double fontSize = 18; // 可调字号(14-26) +``` + +**加载流程 — _load()**:与漫画阅读器完全相同的逻辑。 + +**页面结构**: +``` +AppBar: '章节标题(3/336)' + actions: [缩小字体] [放大字体] + +body → FutureBuilder: + error → _ReaderState('章节加载失败', retry) + loading → CircularProgressIndicator + data.textContent 为空 → _ReaderState('这一章还是空白', retry) + data.textContent: + ListView: + Text(章节标题, titleLarge) + Text(正文, fontSize: fontSize, height: 1.75) + +bottomNavigationBar: 与漫画阅读器完全相同的三按钮导航 +``` + +--- + +## 十、缓存与阅读记录 + +### 10.1 OnlineContentCache — 会话级内存缓存 + +**文件**:`online_cache.dart`(25 行) + +```dart +class OnlineContentCache { + final _bookInfo = {}; // key: sourceId|url + final _toc = >{}; // key: sourceId|tocUrl + final _chapters = {}; // key: sourceId|chapterUrl + + // 缓存 key 规则 + String _key(String sourceId, String url) => '$sourceId|$url'; + + // BookInfo + getBookInfo(sourceId, url) → _bookInfo[key] + putBookInfo(sourceId, url, value) → _bookInfo[key] = value + + // TOC(返回不可变列表) + getToc(sourceId, url) → _toc[key] + putToc(sourceId, url, value) → _toc[key] = List.unmodifiable(value) + + // Chapter + getChapter(sourceId, url) → _chapters[key] + putChapter(sourceId, url, value) → _chapters[key] = value +} +``` + +**缓存生命周期**:当次 APP 会话(进程存活期间)。APP 杀死后缓存消失。 + +**缓存消费者**: +- `fetchBookInfo()` → 查/写 `_bookInfo` +- `fetchToc()` → 查/写 `_toc` +- `fetchChapterContent()` → 查/写 `_chapters` + +### 10.2 OnlineReadingHistory — 阅读记录持久化 + +**文件**:`online_reading_history.dart`(57 行) + +**存储方式**:SharedPreferences,key = `online_reading_history_v1` + +**数据格式**:JSON 数组,每个元素是 `OnlineReadingRecord.toJson()` + +```dart +static Future save(OnlineReadingRecord record) { + 1. 读取现有记录 → all() + 2. 删除同一本书的旧记录(按 sourceId + detailUrl 去重) + 3. 新记录插入到数组头部(最近阅读排在前面) + 4. 超过 50 条 → 截断尾部 + 5. 写回 SharedPreferences +} + +static Future find(sourceId, detailUrl) { + 遍历 all() → 匹配 sourceId + detailUrl → 返回第一条 +} + +static Future> all() { + 从 SharedPreferences 读取 → jsonDecode → List.map(OnlineReadingRecord.fromJson) +} +``` + +**调用的地方**: +- 进入章节时:`OnlineReadingHistory.save()`(在 `_load()` 中) +- 进入详情页时:`OnlineReadingHistory.find()`(显示"继续阅读"还是"从第一章开始") +- 从阅读器返回时:`OnlineReadingHistory.find()`(刷新章节已读标记) + +--- + +## 十一、完整调用链路 + +### 11.1 搜索链路 + +``` +用户输入 "海贼王" → 按回车 + ↓ +ExplorePage._submit() + ↓ +UnifiedSearch.search(query: "海贼王", mode: manga) + ↓ 并行 + ├─ _searchCloudManga("海贼王") + │ → SakuraApiClient.gallery(search: "海贼王", contentType: 'manga') + │ → HTTP GET /api/v1/gallery?search=海贼王&content_type=manga + │ → List → SearchItem.cloudArtwork() + │ + └─ LegadoEngine.searchAll(enabledMangaSources, "海贼王") + ↓ 分批(每批 10 个) + ├─ search(源1, "海贼王") + │ ├─ _encodeKeyword(源1, "海贼王") → GBK/UTF-8 编码 + │ ├─ _applySearchTemplate(源1.searchUrl, encoded, page=1) + │ │ → "https://example.com/search?keyword=%E6%B5%B7%E8%B4%BC%E7%8E%8B" + │ ├─ _fetchBody(源1, url) → HTTP GET → 字节数组 → _decodeBody → HTML 字符串 + │ ├─ RuleParser(html).selectList(源1.searchListRule) + │ │ → fragments = ["
...", "
..."] + │ └─ 遍历 fragments: + │ ├─ select(fragment, searchNameRule) → "海贼王" + │ ├─ select(fragment, searchAuthorRule) → "尾田荣一郎" + │ ├─ select(fragment, searchDetailRule) → "/book/123" + │ └─ UnifiedSearchResult(title:"海贼王", detailUrl:"https://.../book/123") + │ + ├─ search(源2, "海贼王") → 同上... + └─ ... + ↓ +Future.wait → List: + [SearchSection(id:'cloud', items: [...]), + SearchSection(id:'online', items: [UnifiedSearchResult...])] + ↓ +ExplorePage FutureBuilder → 渲染结果列表 +``` + +### 11.2 详情链路 + +``` +用户点击搜索结果中的 "海贼王" + ↓ +ExplorePage._openItem(item) + ↓ item.online.detailUrl = "https://example.com/book/123" +Navigator.push(OnlineBookDetailPage( + source: 源1, + detailUrl: "https://example.com/book/123", + initialTitle: "海贼王", + initialAuthor: "尾田荣一郎", +)) + ↓ +OnlineBookDetailPage._load() + ↓ +LegadoEngine.fetchBookInfo(源1, "https://example.com/book/123") + ├─ cache.getBookInfo(源1.id, url) → 未命中 + ├─ _fetchBody(源1, url) → HTML 字符串 + ├─ RuleParser(html) → 提取: + │ ├─ bookInfoNameRule → "海贼王" + │ ├─ bookInfoAuthorRule → "尾田荣一郎" + │ ├─ bookInfoCoverRule → "https://example.com/img/cover.jpg" + │ ├─ bookInfoIntroRule → "拥有财富、名声、权力..." + │ └─ bookInfoTocRule → "/book/123/catalog" + ├─ tocUrl = "https://example.com/book/123/catalog" + ├─ cache.putBookInfo(...) + └─ return OnlineBookInfo(title:"海贼王", tocUrl:"https://.../catalog", ...) + ↓ +setState → loadingInfo=false, info=OnlineBookInfo, loadingToc=true + ↓ 上半部分立即渲染(封面+书名+作者+简介+按钮) + ↓ +_loadToc("https://example.com/book/123/catalog") + ↓ +LegadoEngine.fetchToc(源1, "https://example.com/book/123/catalog") + ├─ cache.getToc(源1.id, url) → 未命中 + ├─ _fetchPaginatedToc(源1, url) + │ ├─ page=1: _fetchBody → HTML + │ │ RuleParser → tocListRule → fragments + │ │ 每 fragment: tocNameRule="第1话" + tocUrlRule="/book/123/ch1" + │ │ → chapters.add(OnlineChapter("第1话", "https://.../ch1", index:1)) + │ │ ... + │ ├─ tocNextPageRule → 有 → nextUrl = "?page=2" + │ ├─ page=2: 同上... + │ └─ tocNextPageRule → 空 → 停止 + ├─ cache.putToc(...) → List(共 1130 条) + └─ return chapters + ↓ +setState → loadingToc=false, chapters=[1130条] + ↓ 下半部分渲染章节列表 + ↓ +OnlineReadingHistory.find(源1.id, detailUrl) → readingRecord 或 null +``` + +### 11.3 阅读链路 + +``` +用户在详情页点击 "第1话" + ↓ +OnlineBookDetailPage._openChapter(0) // index 从 0 开始 + ↓ +Navigator.push(OnlineMangaReaderPage( + source: 源1, + title: "海贼王", + chapters: [1130条], + currentChapterIndex: 0, + detailUrl: "https://example.com/book/123", +)) + ↓ +OnlineMangaReaderPage._load() + ↓ +chapter = chapters[0] → {title:"第1话", url:"https://.../ch1"} + ↓ +LegadoEngine.fetchChapterContent(源1, chapter) + ├─ cache.getChapter(源1.id, url) → 未命中 + ├─ _fetchBody(源1, url) → HTML + ├─ RuleParser(html) → contentRule → "#chapter-content" 的 innerHtml + ├─ _extractImageUrls(contentBody, absoluteUrl) + │ ├─ RegExp: / data-src / data-original + │ └─ JSON 图片扫描 + ├─ contentNextRule → "/book/123/ch2" → nextChapterUrl + ├─ contentPrevRule → 空 → prevChapterUrl=null + ├─ cache.putChapter(...) + └─ return OnlineChapterContent( + title: "第1话", + imageUrls: ["https://.../img01.jpg", "https://.../img02.jpg", ...], + nextChapterUrl: "https://.../ch2", + ) + ↓ +OnlineReadingHistory.save(OnlineReadingRecord( + sourceId, sourceName, bookTitle, detailUrl, + chapterUrl, chapterTitle, chapterIndex:0, totalChapters:1130, + lastReadTime: now, +)) + ↓ +预加载下一章: + engine.fetchChapterContent(源1, chapters[1]) + .catchError(...) // 后台静默,失败不影响当前阅读 + ↓ +渲染 PageView: + Image.network("https://.../img01.jpg") ← 第1页 + Image.network("https://.../img02.jpg") ← 第2页 + ... + +用户点击 "下一章" + ↓ +_move(1) → chapterIndex = 1 + ↓ +setState → contentFuture = _load() + ↓ +engine.fetchChapterContent(源1, chapters[1]) + ├─ cache 命中!(已预加载) → 秒切 + └─ _load() 内部再次 save 阅读记录 + 预加载第3章 +``` + +--- + +## 附录:关键设计要点总结 + +### 数据流方向 + +``` +LegadoSource (静态规则,SharedPreferences 持久化) + ↓ +LegadoEngine (规则驱动解析,HTTP + 缓存) + ↓ +OnlineBookInfo / OnlineChapter / OnlineChapterContent (运行时数据,内存缓存) + ↓ +UI Pages (ExplorePage / OnlineBookDetailPage / ReaderPages) +``` + +### 编码处理链 + +``` +搜索关键词: + _encodeKeyword(source, keyword) + → gbk.encode / Uri.encodeQueryComponent / 平台通道 + +响应体: + _fetchBody → ResponseType.bytes + → _decodeBody(bytes, source.charset, content-type charset) + → gbk.decode / utf8.decode / latin1.decode +``` + +### 缓存策略 + +``` +BookInfo → 进入详情页时缓存,同一书源 + 同一 detailUrl 只请求一次 +TOC → 进入详情页时缓存,同一书源 + 同一 tocUrl 只请求一次 +Chapter → 阅读时缓存当前章 + 预加载下一章 +阅读记录 → SharedPreferences 持久化,跨会话保留 +``` + +### 回退策略优先级 + +``` +规则解析优先: + 1. 用书源声明的 rule 解析(CSS/JSON/Regex) + 2. 规则为空或无结果 → 回退 + - 搜索: _fallbackSearchResults (全 扫描 + 关键词过滤) + - 目录: _fallbackChapters (章节正则过滤的 扫描) + - 内容: _htmlToText / _extractImageUrls (从 HTML 暴力提取) +``` diff --git a/docs/漫画源问题.md b/docs/漫画源问题.md new file mode 100644 index 0000000..c39e3ba --- /dev/null +++ b/docs/漫画源问题.md @@ -0,0 +1,778 @@ +下面是整合后的**最终完整方案**,包括:漫画解析、章节完整性、图片加载、调试能力、搜索性能,以及你刚刚提到的顶部搜索框 UI 问题。 +Sakura 漫画模块完整改进方案 +================= + +一、改进目标 +------ + +本轮改造解决 5 类问题: + 1. 漫画章节目录不全 + 2. 漫画图片解析失败 + 3. 图片 URL 有数量但无法显示 + 4. 在线搜索速度慢 + 5. 漫画 / 图片页顶部搜索框布局异常,在线搜索入口位置不合理 + +* * * + +二、整体优先级 +======= + + P0:顶部搜索框 UI 修复与在线搜索入口重构 + P1:图片 URL 合法性验证 + P2:PageImage + headers 拆分 + P3:图片 HTTP 探测与调试能力 + P4:TOC 分页抓全章节 + P5:章节排序支持番外 + P6:Script 图片变量提取 + P7:漫画调试页 + P8:搜索健康分与增量搜索 + +说明: + P0 先做,因为它是明显 UI 问题,改动小、收益快。 + P1~P7 解决漫画“能不能看”。 + P8 最后做,解决“搜得慢”。 + +* * * + +P0:顶部搜索框 UI 修复与在线搜索入口重构 +======================= + +问题 +-- + +截图中红框区域出现: + 顶部搜索栏被状态栏 / 右上角按钮挤压 + 漫画页和图片页都出现同类问题 + 在线搜索入口放在右上角,不符合用户预期 +目标 +-- + + 漫画页 / 图片页顶部布局稳定 + 搜索框不被状态栏遮挡 + 在线搜索入口集成到搜索框流程中 + 不再在右上角放在线搜索按钮 + +推荐交互 +---- + +页面顶部只保留一个统一搜索框: + [ 搜索图标 ] 搜索标题、作者或标签 [筛选] [刷新] + +点击搜索框进入统一搜索页: + SearchPage + ├─ 搜索输入框 + ├─ 搜索范围切换:本地 / 在线 / 全部 + └─ 结果分区: + ├─ 本地漫画 + ├─ 在线漫画源 + └─ 图片 / 插画 +代码结构 +---- + +新增统一组件: + class SakuraSearchBar extends StatelessWidget { + final String hintText; + final VoidCallback onTap; + final VoidCallback? onFilterTap; + final VoidCallback? onRefreshTap; + + const SakuraSearchBar({ + super.key, + required this.hintText, + required this.onTap, + this.onFilterTap, + this.onRefreshTap, + }); + + @override + Widget build(BuildContext context) { + return Material( + elevation: 6, + borderRadius: BorderRadius.circular(32), + color: Theme.of(context).colorScheme.surface, + child: InkWell( + borderRadius: BorderRadius.circular(32), + onTap: onTap, + child: SizedBox( + height: 56, + child: Row( + children: [ + const SizedBox(width: 18), + const Icon(Icons.search), + const SizedBox(width: 12), + Expanded( + child: Text( + hintText, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.titleMedium, + ), + ), + if (onFilterTap != null) + IconButton( + icon: const Icon(Icons.tune), + onPressed: onFilterTap, + ), + if (onRefreshTap != null) + IconButton( + icon: const Icon(Icons.refresh), + onPressed: onRefreshTap, + ), + const SizedBox(width: 8), + ], + ), + ), + ), + ); + } + } + +页面顶部必须用: + SafeArea( + bottom: false, + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 8, 16, 8), + child: SakuraSearchBar( + hintText: '搜索标题、作者或标签', + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => UnifiedSearchPage( + initialMode: SearchMode.manga, + ), + ), + ); + }, + onFilterTap: _openFilter, + onRefreshTap: _refresh, + ), + ), + ) + +禁止再用: + Stack + Positioned 顶部硬定位 + AppBar 右上角塞在线搜索入口 + 状态栏高度手动写死 + +* * * + +P1:图片 URL 合法性验证 +=============== + +目标 +-- + +不要相信 `1/17` 这种页数。它可能是: + 17 个真实图片 + 17 个空字符串 + 17 个 loading 图 + 17 个 placeholder + 17 个错误 URL + +新增模型: + class ImageCandidate { + final String raw; + final String? url; + final bool isValid; + final String? reason; + + const ImageCandidate({ + required this.raw, + required this.url, + required this.isValid, + this.reason, + }); + } + +验证逻辑: + ImageCandidate validateImageCandidate(String raw, String baseUrl) { + final value = raw.trim(); + + if (value.isEmpty) { + return ImageCandidate( + raw: raw, + url: null, + isValid: false, + reason: 'empty', + ); + } + + if (value.startsWith('data:')) { + return ImageCandidate( + raw: raw, + url: null, + isValid: false, + reason: 'data-uri', + ); + } + + final lower = value.toLowerCase(); + if (lower.contains('loading') || + lower.contains('placeholder') || + lower.contains('logo') || + lower.contains('icon')) { + return ImageCandidate( + raw: raw, + url: value, + isValid: false, + reason: 'placeholder', + ); + } + + final normalized = normalizeImageUrl(value, baseUrl); + final uri = Uri.tryParse(normalized); + + if (uri == null || !uri.hasScheme || uri.host.isEmpty) { + return ImageCandidate( + raw: raw, + url: normalized, + isValid: false, + reason: 'invalid-url', + ); + } + + return ImageCandidate( + raw: raw, + url: normalized, + isValid: true, + ); + } + +URL 归一化: + String normalizeImageUrl(String raw, String baseUrl) { + var u = raw.trim() + .replaceAll(r'\/', '/') + .replaceAll(r'\u002F', '/'); + + if (u.startsWith('//')) { + u = 'https:$u'; + } + + return Uri.parse(baseUrl).resolve(u).toString(); + } + +* * * + +P2:PageImage + headers 拆分 +========================= + +问题 +-- + +Legado 漫画源经常返回: + https://xxx.jpg,{"headers":{"Referer":"https://site.com"}} + +如果直接丢给: + Image.network(raw) + +图片必炸。 +新模型 +--- + + class PageImage { + final int index; + final String raw; + final String url; + final Map headers; + + const PageImage({ + required this.index, + required this.raw, + required this.url, + required this.headers, + }); + } + +拆分逻辑 +---- + + class ParsedImageSpec { + final String url; + final Map headers; + + ParsedImageSpec({ + required this.url, + required this.headers, + }); + } + + + ParsedImageSpec splitLegadoImageSpec(String raw) { + final idx = raw.indexOf(',{'); + if (idx < 0) { + return ParsedImageSpec(url: raw.trim(), headers: {}); + } + + final url = raw.substring(0, idx).trim(); + final jsonText = raw.substring(idx + 1).trim(); + + final headers = {}; + + try { + final decoded = jsonDecode(jsonText); + final h = decoded['headers']; + + if (h is Map) { + h.forEach((k, v) { + headers[k.toString()] = v.toString(); + }); + } else if (h is String) { + final inner = jsonDecode(h); + if (inner is Map) { + inner.forEach((k, v) { + headers[k.toString()] = v.toString(); + }); + } + } + } catch (_) {} + + return ParsedImageSpec(url: url, headers: headers); + } + +生成 PageImage: + PageImage buildPageImage({ + required String raw, + required String chapterUrl, + required LegadoSource source, + required int index, + }) { + final parsed = splitLegadoImageSpec(raw); + + final headers = { + ...source.headers, + 'User-Agent': source.userAgent ?? defaultUserAgent, + 'Referer': chapterUrl, + ...parsed.headers, + }; + + return PageImage( + index: index, + raw: raw, + url: normalizeImageUrl(parsed.url, chapterUrl), + headers: headers, + ); + } + +阅读器改成: + Image.network( + page.url, + headers: page.headers, + fit: BoxFit.contain, + errorBuilder: (_, error, stack) { + return const Icon(Icons.broken_image); + }, + ) + +* * * + +P3:图片 HTTP 探测 +============= + +目标 +-- + +判断到底是: + URL 错 + headers 错 + 反爬 + 图片损坏 + Flutter 缓存问题 + +新增: + class ImageProbeResult { + final String url; + final int? statusCode; + final String? contentType; + final int contentLength; + final String first16BytesHex; + final bool looksLikeImage; + final String? error; + + const ImageProbeResult({ + required this.url, + this.statusCode, + this.contentType, + required this.contentLength, + required this.first16BytesHex, + required this.looksLikeImage, + this.error, + }); + } + +探测: + Future probeImage(Dio dio, PageImage image) async { + try { + final resp = await dio.get>( + image.url, + options: Options( + responseType: ResponseType.bytes, + headers: image.headers, + validateStatus: (_) => true, + ), + ); + + final bytes = resp.data ?? const []; + final first16 = bytes + .take(16) + .map((b) => b.toRadixString(16).padLeft(2, '0')) + .join(); + + return ImageProbeResult( + url: image.url, + statusCode: resp.statusCode, + contentType: resp.headers.value('content-type'), + contentLength: bytes.length, + first16BytesHex: first16, + looksLikeImage: isImageMagic(bytes), + ); + } catch (e) { + return ImageProbeResult( + url: image.url, + contentLength: 0, + first16BytesHex: '', + looksLikeImage: false, + error: e.toString(), + ); + } + } + +魔数判断: + bool isImageMagic(List b) { + if (b.length < 4) return false; + + // JPG + if (b[0] == 0xFF && b[1] == 0xD8 && b[2] == 0xFF) return true; + + // PNG + if (b[0] == 0x89 && b[1] == 0x50 && b[2] == 0x4E && b[3] == 0x47) { + return true; + } + + // WEBP + if (b.length >= 12 && + b[0] == 0x52 && + b[1] == 0x49 && + b[2] == 0x46 && + b[3] == 0x46 && + b[8] == 0x57 && + b[9] == 0x45 && + b[10] == 0x42 && + b[11] == 0x50) { + return true; + } + + // GIF + if (b[0] == 0x47 && b[1] == 0x49 && b[2] == 0x46 && b[3] == 0x38) { + return true; + } + + // BMP + if (b[0] == 0x42 && b[1] == 0x4D) return true; + + return false; + } + +诊断表: + 403 / 444 + → Referer / User-Agent / Cookie 问题 + + 404 + → 图片 URL 拼错或章节页已失效 + + 200 + text/html + → 反爬页 / 登录页 / 错误页 + + 200 + image/* + contentLength < 1024 + → 可能是伪装错误图或破损图 + + 200 + image/* + 魔数失败 + → 响应不是有效图片 + + 200 + image/* + 魔数成功但 Image.network 不显示 + → headers 没传进 Image.network / 图片缓存污染 / Flutter 图片组件问题 + +* * * + +P4:TOC 分页抓全章节 +============= + +问题 +-- + +一人之下 600+ 话,但现在只显示几十章,说明目录分页没有抓全。 +支持字段 +---- + + tocNextPageRule + nextTocUrl + nextUrl + nextPage + tocNextPage + +抓取流程 +---- + + firstTocUrl + → 请求当前页 + → 解析 chapterList + → 解析 nextTocUrl + → resolve 成绝对 URL + → 合并章节并去重 + → 继续下一页 + +终止条件 +---- + + nextUrl == null + seenPageUrls 已访问 + 连续 2 页新增章节为 0 + pageCount >= 100 + chapterCount >= 3000 + +建议参数: + const maxTocPages = 100; + const maxTocChapters = 3000; + const emptyPageDelay = Duration(milliseconds: 500); + +* * * + +P5:章节排序支持番外 +=========== + +支持: + 第660话 + 第660.5回 + Ch.660 + ch 660.5 + 660话 + 660.5 番外 + +章节号提取: + double? extractChapterNo(String title) { + var m = RegExp(r'第\s*(\d+(?:\.\d+)?)\s*[回话章节节]').firstMatch(title); + if (m != null) return double.tryParse(m.group(1)!); + + m = RegExp(r'[Cc]h\.?\s*(\d+(?:\.\d+)?)').firstMatch(title); + if (m != null) return double.tryParse(m.group(1)!); + + m = RegExp(r'^\s*(\d+(?:\.\d+)?)\s*(?:话|回|章|节)?').firstMatch(title); + if (m != null) return double.tryParse(m.group(1)!); + + return null; + } + +排序: + chapters.sort((a, b) { + final na = extractChapterNo(a.title); + final nb = extractChapterNo(b.title); + + if (na != null && nb != null && na != nb) { + return na.compareTo(nb); + } + + return a.index.compareTo(b.index); + }); + +* * * + +P6:Script 图片变量提取 +================ + +当 `ruleContent.content` 失败或候选图片全无效时,从 rawHtml 的 script 中提取图片。 + +支持变量: + newImgs + imageList + page_url + chapterImages + photosr + img_data + picTree + +示例: + List extractImagesFromScripts(String html) { + final out = []; + + final patterns = [ + RegExp(r'newImgs\s*=\s*(\[[\s\S]*?\])'), + RegExp(r'imageList\s*=\s*(\[[\s\S]*?\])'), + RegExp(r'"page_url"\s*:\s*(\[[\s\S]*?\])'), + RegExp(r'chapterImages\s*=\s*(\[[\s\S]*?\])'), + RegExp(r'photosr\s*=\s*(\[[\s\S]*?\])'), + RegExp(r'img_data\s*=\s*(\[[\s\S]*?\])'), + RegExp(r'picTree\s*=\s*(\[[\s\S]*?\])'), + ]; + + for (final p in patterns) { + final m = p.firstMatch(html); + if (m == null) continue; + + final raw = m.group(1)!; + final list = tryDecodeStringList(raw); + out.addAll(list.map(sanitizeScriptUrl)); + } + + return out; + } + +清洗: + String sanitizeScriptUrl(String url) { + return url + .replaceAll(r'\/', '/') + .replaceAll(r'\u002F', '/'); + } + +* * * + +P7:漫画调试页 +======== + +入口 +-- + + 书籍详情页 → 长按章节 → 调试本章 + 阅读器右上角菜单 → 调试当前章 + +展示内容 +---- + + { + "sourceName": "包子漫画5", + "bookTitle": "一人之下", + "chapterTitle": "第652回", + "chapterUrl": "...", + "ruleContent": "...", + "rawHtmlLength": 123456, + "contentOutputLength": 2048, + "rawCandidateCount": 17, + "validCandidateCount": 0, + "pageImageCount": 0, + "firstCandidates": [ + { + "raw": "...", + "normalized": "...", + "reason": "placeholder" + } + ], + "probe": [ + { + "url": "...", + "status": 200, + "contentType": "image/jpeg", + "contentLength": 312, + "first16Bytes": "ffd8ff...", + "looksLikeImage": false + } + ] + } + +操作按钮 +---- + + 复制调试信息 + 重新检测前 5 张图 + 切换 Referer:章节页 / 详情页 / 源首页 + 清除图片缓存后重试 + 打开原始章节 URL + +* * * + +P8:搜索健康分与增量搜索 +============= + +问题 +-- + +搜索慢主要来自: + 坏源太多 + 每个源 timeout 太长 + 所有源一起等结果 +模型 +-- + + class SourceHealth { + final String sourceId; + + int successCount = 0; + int failCount = 0; + int timeoutCount = 0; + int totalLatencyMs = 0; + int consecutiveFails = 0; + + DateTime? lastSuccessAt; + DateTime? lastFailAt; + } + +策略 +-- + + 健康源优先 + 坏源降权 + 连续失败 >= 5 临时跳过 + 搜索超时 5~8 秒 + 快速源先返回 + 慢源后台补充 + +搜索阶段 +---- + + 0~5s:快速源结果 + 5~8s:普通源结果 + 8s 后:慢源后台补 + +* * * + +三、最终落地顺序 +======== + +第 1 阶段:UI 先止血 +------------- + + P0:顶部搜索框 UI 修复 + P0:在线搜索入口放进统一搜索页 + +结果: + 漫画页 / 图片页顶部不再错位 + 在线搜索不再占右上角 + 搜索体验统一 +第 2 阶段:图片链路可观测 +-------------- + + P1:图片 URL 合法性验证 + P2:PageImage + headers 拆分 + P3:图片 HTTP 探测 + P7:漫画调试页 + +结果: + 可以准确判断图片失败原因 + 不再靠猜 +第 3 阶段:章节抓全 +----------- + + P4:TOC 分页 + P5:章节排序 + +结果: + 一人之下这类 600+ 话漫画可以抓全 + 章节顺序稳定 +第 4 阶段:提升漫画源覆盖率 +--------------- + + P6:Script 图片变量提取 + +结果: + 更多漫画源能解析图片 +第 5 阶段:优化搜索速度 +------------- + + P8:搜索健康分与增量搜索 + +结果: + 坏源不拖慢搜索 + 结果更快出现 + +* * * + + +======= diff --git a/docs/视频云同步与内容合集设计方案.md b/docs/视频云同步与内容合集设计方案.md new file mode 100644 index 0000000..fc4e515 --- /dev/null +++ b/docs/视频云同步与内容合集设计方案.md @@ -0,0 +1,70 @@ +# 视频云同步与内容合集设计方案 + +## 目标 + +让 X、小红书、抖音采集到的视频和图片一样进入 Sakura Cloud,并能在手机端与 Web 端查看。云端对每个用户的视频总占用做 30GB 上限,Web 端从“后台列表”调整为适合阅读、看视频、浏览插画的内容合集。 + +## 云端设计 + +- 复用现有 `upload / sync / gallery / files` 链路,不新增数据库表,不改变 API 返回结构。 +- 视频仍作为 `Artwork` 保存,`content_type=video`,`mime_type=video/*`,文件实体复用 `FileBlob` 与 `UserFile`。 +- 上传接口允许 `resource_type=video`,视频文件进入 `uploads/videos/{user_id}/{year}/{month}`。 +- 新增配置: + - `MAX_VIDEO_UPLOAD_SIZE_MB=2048`:单个视频默认最大 2GB。 + - `VIDEO_STORAGE_LIMIT_GB=30`:单用户视频总空间上限 30GB。 +- 配额计算以当前登录用户为准,从 `UserFile -> FileBlob` 统计该用户所有 `mime_type LIKE 'video/%'` 的文件大小。命中上限时返回 `413`,不会创建上传会话。 +- 已存在文件秒传时同样检查该用户的视频总配额,防止跨用户复用文件绕过限制。 + +## 桌面端采集同步设计 + +- `SyncManager` 根据 MIME 与 `content_type` 判断上传资源类型: + - 小说:`novel` + - 视频:`video` + - 其他:`image` +- X 手动预览下载和关注作者同步共用云端同步逻辑,视频文件下载成功后自动同步 metadata。 +- WebUI 通用插件下载路径也接入云同步,因此小红书、抖音插件只要返回 `files=[{type: "video"}]`,就能自动上传云端。 +- 小红书插件补充 DOM 视频解析,支持从 `video/source` 标签提取直接视频地址;搜索结果进入详情下载时也同时处理 `images + videos`。 +- 抖音插件已有视频文件抽取与下载逻辑,本方案复用其 `Resource.files` 输出,不重写下载器。 + +## 手机端设计 + +- `Artwork` 模型补充 `mimeType`、`fileSize`、`isVideo` 与 `primaryMediaPath`。 +- 图库页新增视频筛选,视频卡片显示播放叠层,不再伪装成普通插画。 +- 视频详情页展示视频封面、标题、作者、MIME、大小,并提供: + - 播放视频 + - 下载到本地 + - 收藏 +- 为避免新增播放器依赖导致构建不稳定,当前采用“受保护下载到本地后调用系统播放器”的方式。 +- Android 通过 FileProvider 暴露 Sakura 下载目录中的临时读取 URI,不暴露任意路径。 + +## Web 端内容合集设计 + +- 普通用户首页改为内容合集: + - 顶部概览:用户、空间、视频额度、最近内容。 + - 视频区:16:9 视频卡片,播放标记,显示作者与大小。 + - 插画区:缩略图网格。 + - 漫画区:阅读列表入口。 + - 小说区:阅读列表入口。 + - 在线搜索与提醒中心作为主操作入口。 +- 页面复用现有 API: + - `/me/gallery?content_type=video` + - `/me/gallery?content_type=illust` + - `/me/gallery?content_type=manga` + - `/novels/series` +- CSS 使用现有粉白主题、8px 圆角和响应式网格,手机浏览时自动改为单列。 + +## 安全与边界 + +- 不上传第三方平台 Cookie、Token 或浏览器登录态。 +- 云端只保存视频文件与作品 metadata。 +- 视频配额按当前登录用户统计,避免水平越权。 +- 文件读取继续走现有 `/files` 和 gallery ownership 校验。 +- Android FileProvider 只给外部播放器一次性读取 URI,不暴露数据库或任意文件系统路径。 + +## 验收点 + +- X、小红书、抖音插件下载视频后,云同步开启时视频可进入 cloud gallery。 +- 超过 30GB 视频总配额时,上传初始化返回 413。 +- 手机端图库可以筛选视频,视频详情可以下载并调用系统播放器查看。 +- Web 普通用户首页能看到视频、插画、漫画、小说四类内容入口。 +- 现有图片、漫画、小说同步与阅读入口保持可用。 diff --git a/docs/项目方案书.md b/docs/项目方案书.md new file mode 100644 index 0000000..5d5a759 --- /dev/null +++ b/docs/项目方案书.md @@ -0,0 +1,322 @@ +# Sakura Downloader(樱云下载器)项目方案书 + +> 版本:v1.0 | 日期:2026-06-18 | 状态:已交付 + +--- + +## 一、项目概述 + +### 1.1 项目名称 + +**Sakura Downloader(樱云下载器)** — 个人媒体采集、本地图库管理与跨设备云同步系统。 + +### 1.2 项目定位 + +一个面向个人用户的**多媒体内容采集-整理-阅读一体化平台**,解决"从多个内容平台采集插画/漫画/小说 → 本地归档整理 → 在手机/桌面等多设备上随时浏览阅读"的完整链路。 + +### 1.3 核心价值 + +| 痛点 | 解决方案 | +|------|----------| +| 多平台内容分散(Pixiv、X、小红书、AO3、笔趣阁等) | 统一插件架构,一个入口采集所有平台 | +| 下载后文件散乱、难以检索 | 本地图库 + SQLite 索引 + 缩略图墙 + 标签/作者搜索 | +| 桌面下载了,手机上没法看 | Sakura Cloud 云端同步,手机端实时访问 | +| 关注的作者更新了不知道 | 云端每日摘要提醒,手机端查看 | +| 跨平台小说阅读体验差 | Flutter 移动端专为阅读优化:自动分页、进度记忆、翻译、字体调节 | + +--- + +## 二、系统架构 + +### 2.1 整体拓扑 + +```text +┌─────────────────────────────────────────────────────────┐ +│ Sakura Cloud (FastAPI) │ +│ PostgreSQL + JWT Auth │ +│ api.kunkunxiaomao.top:8000 │ +└──────┬──────────────────────┬───────────────────────────┘ + │ REST API │ REST API + ▼ ▼ +┌──────────────┐ ┌──────────────────┐ +│ Windows 桌面端 │ │ Android 移动端 │ +│ Python + │ │ Flutter + │ +│ CustomTkinter │ │ Riverpod + Dio │ +│ (采集/整理中枢) │ │ (阅读/提醒终端) │ +└──────┬─────────┘ └──────────────────┘ + │ 本地 SQLite │ + ▼ ▼ +┌──────────────┐ ┌──────────────┐ +│ 本地图库文件 │ │ 本地缓存/下载 │ +│ (图片/漫画/小说)│ │ (离线阅读) │ +└──────────────┘ └──────────────┘ +``` + +### 2.2 技术栈总览 + +| 层级 | 技术选型 | +|------|----------| +| **Windows 桌面端** | Python 3.8+ · CustomTkinter · SQLite · Playwright · Pillow | +| **云端 API** | FastAPI · PostgreSQL · SQLAlchemy · Alembic · JWT · Docker | +| **Android 移动端** | Flutter 3.x · Dart · Riverpod · Dio · shared_preferences | +| **管理后台 Web** | Vite · TypeScript · Vue/React · Nginx | +| **插件系统** | Python 动态加载 · manifest.json · 统一 PluginBase 接口 | +| **打包分发** | PyInstaller (EXE) · Flutter build (APK) | + +--- + +## 三、客户端种类与职责 + +### 3.1 Windows 桌面端(`pixiv_app/` + `main.py`) + +**角色:采集与整理中枢** + +| 功能模块 | 说明 | +|----------|------| +| **多平台采集** | 通过插件系统支持 Pixiv、X (Twitter)、小红书、AO3、笔趣阁等 | +| **Cookie/密码登录** | 支持 Cookie 导入(推荐)和账号密码登录 | +| **灵活抓取模式** | 作者全部作品、单作品/漫画、小说/系列、关键词搜索 | +| **本地图库** | SQLite 索引 + 文件系统存储,支持缩略图生成 | +| **缩略图墙** | 本地 Web 服务(127.0.0.1:8765),浏览器中浏览、搜索、筛选、收藏 | +| **关注同步** | Pixiv 关注作者管理 + X 关注同步 | +| **云同步** | 一键上传到 Sakura Cloud,自动同步新下载内容 | +| **代理池** | 浏览器代理池 + HTTP 代理,规避反爬 | +| **Playwright 集成** | 无头浏览器渲染 JS 页面(X 插件依赖) | +| **指纹伪装** | 浏览器指纹随机化,降低封禁风险 | + +**打包产物**:`sakuradownloader.exe` (~68 MB, PyInstaller 单文件) + +### 3.2 Sakura Cloud 云端(`sakura_cloud/`) + +**角色:数据与账号中心** + +| 功能模块 | 说明 | +|----------|------| +| **用户系统** | 注册/登录/邮箱验证/JWT Token 鉴权 | +| **媒体存储** | 图片/漫画/小说上传、存储、缩略图生成 | +| **收藏系统** | 跨设备收藏同步 | +| **关注与提醒** | 关注作者管理 + 每日更新摘要提醒 | +| **阅读进度** | 小说阅读进度云端存储,跨设备同步 | +| **管理后台** | SPA 管理控制台(可选) | +| **自动运维** | 启动自动 migrate、定时清理过期上传、每 5 分钟检查提醒摘要 | + +**部署方式**:Docker Compose(FastAPI + PostgreSQL) + +### 3.3 Android 移动端(`sakura_mobile/`) + +**角色:跨设备阅读与提醒终端** + +| 功能模块 | 说明 | +|----------|------| +| **发现页** | 最近浏览 + 更新提醒摘要 | +| **图片页** | 缩略图列表 · 详情 · 收藏 · 下载 · 编辑 · 删除 | +| **漫画页** | 按漫画分组浏览,整组连续阅读,整组下载 | +| **书架页** | 三列彩色方块风格,系列小说按书名归档,章节选择 | +| **小说阅读器** | 自动分页 · 阅读进度记忆 · 收藏 · 当前页翻译 · 原文/译文切换 · 字体/行高/背景色调节 · 章节切换 | +| **我的页** | 用户资料 · 云端空间 · 收藏管理 · 每日提醒开关/时间 · 提醒中心 | +| **搜索** | 图片/漫画/小说全局搜索 | + +**打包产物**:`app-release.apk` (~54 MB, 调试签名) + +### 3.4 管理后台 Web(`sakura_admin_web/`) + +**角色:云端管理控制台** + +- 基于 Vite + TypeScript 的 SPA 单页应用 +- 通过 Sakura Cloud 的 `/admin` 路由挂载 +- 提供可视化的数据和用户管理界面 +- 可选部署(`admin_frontend_enabled` 配置项) + +--- + +## 四、插件体系 + +### 4.1 插件架构 + +```text +plugins/ +├── pixiv/ # Pixiv 插画/漫画/小说 +│ ├── manifest.json +│ └── plugin.py +├── x/ # X (Twitter) 媒体 +│ ├── manifest.json +│ ├── plugin.py +│ ├── playwright_client.py +│ └── x_login.py +├── xiaohongshu/ # 小红书 +├── ao3/ # Archive of Our Own +└── biquge/ # 笔趣阁(小说) +``` + +### 4.2 插件接口规范 + +每个插件只需实现 `PluginBase` 抽象类,包含: + +- `platform_name` — 平台标识 +- `supports_modes` — 支持的抓取模式列表 +- `search()` — 搜索 +- `fetch_work()` — 获取作品详情 +- `fetch_author_works()` — 获取作者全部作品 +- `download()` — 下载媒体文件 + +通过 `manifest.json` 声明元数据,桌面端启动时自动发现并加载。 + +--- + +## 五、核心数据流 + +### 5.1 采集下载流程 + +```text +用户输入(ID/链接/关键词) + │ + ▼ + 任务解析器 (parser.py) + │ + ▼ + 任务队列 (queue.py) + │ + ▼ + 插件调度 → 对应平台 plugin.py + │ + ▼ + HTTP/Playwright 抓取 → 解析作品列表 + │ + ▼ + 下载器 (downloader.py) → 保存到本地图库 + │ + ▼ + SQLite 索引写入 + 缩略图生成 + │ + ▼ + (可选) 云同步上传到 Sakura Cloud +``` + +### 5.2 云同步流程 + +```text +桌面端下载完成 + │ + ▼ + 检查"启用云同步"开关 + │ + ▼ + cloud_sync.py → POST /api/v1/media/upload + │ + ▼ + Sakura Cloud 存储 + 生成缩略图 + │ + ▼ + 移动端刷新 → GET /api/v1/media → 展示 +``` + +### 5.3 每日提醒流程 + +```text +桌面端同步关注作者的新作品到云端 + │ + ▼ + 云端写入 follow + update_alert 记录 + │ + ▼ + reminder_digest_loop (每5分钟检查) + │ + ▼ + 到达用户设定的提醒时间 → 生成每日摘要 + │ + ▼ + 移动端"发现"页展示更新提醒 +``` + +--- + +## 六、用法概览 + +### 6.1 桌面端典型工作流 + +1. **登录** → 导入 Pixiv Cookie(推荐)或账号密码登录 +2. **抓取** → 选择平台 → 选择模式 → 输入目标 → 开始抓取 +3. **浏览** → 打开缩略图墙 → 搜索/筛选/收藏 +4. **关注** → 在缩略图墙中关注作者 → 后续同步关注作者更新 +5. **云同步** → 登录 Sakura Cloud → 开启云同步 → 自动上传 + +### 6.2 移动端典型工作流 + +1. **安装** → 安装 APK → 允许未知来源 +2. **登录** → 使用与桌面端相同的 Sakura Cloud 账号 +3. **浏览** → 底部 5 个页签切换(发现/图片/漫画/书架/我的) +4. **阅读小说** → 自动分页 · 调节字体 · 翻译 · 章节切换 +5. **提醒** → 我的 → 每日更新提醒 → 开启并设置时间 + +### 6.3 云端部署 + +```bash +cd sakura_cloud +docker-compose up -d +# FastAPI :8000 + PostgreSQL :5432 +``` + +### 6.4 构建打包 + +```bash +# 桌面 EXE +scripts\build_desktop_exe.bat + +# Android APK +scripts\build_mobile_android.bat + +# 复制到 releases/ +scripts\copy_release_artifacts.bat +``` + +--- + +## 七、项目交付物 + +| 交付物 | 路径 | 说明 | +|--------|------|------| +| Windows EXE | `releases/sakuradownloader.exe` | 双击即用,~68 MB | +| Android APK | `releases/app-release.apk` | 调试签名,~54 MB | +| 用户使用说明 | `使用说明-桌面版与安卓版.md` | 终端用户操作手册 | +| 项目方案书 | `docs/项目方案书.md` | 本文档 | +| 开发文档 | `项目文件/BUILD.md` | 构建与排错 | +| 构建脚本 | `scripts/` | 一键打包脚本 | +| 云端 API | `sakura_cloud/` | Docker 部署 | +| 源码 | 全部 Python/Dart/TypeScript | 可二次开发 | + +--- + +## 八、技术亮点 + +1. **插件化架构** — 新平台接入只需新增一个插件目录,不改动核心代码 +2. **多反爬策略** — Cookie 管理 · Playwright 无头浏览器 · 代理池 · 指纹伪装 · 限速退避 +3. **跨设备云同步** — 桌面采集 → 云端存储 → 手机阅读,全链路打通 +4. **沉浸式阅读体验** — 自动分页 · 进度记忆 · 即时翻译 · 个性化排版 +5. **智能提醒** — 关注作者更新云端汇总,非本地闹钟,静默不打扰 +6. **本地优先** — 所有数据本地 SQLite 存储,云端为可选项,离线也能用 + +--- + +## 九、后续演进方向 + +| 方向 | 说明 | +|------|------| +| iOS 客户端 | Flutter 已预留 iOS 平台支持,补充苹果开发者账号与签名 | +| 正式应用签名 | APK 替换调试签名为正式 keystore,支持上架分发 | +| 更多平台插件 | 持续扩充插件库(如 Fantia、Fanbox、DeviantArt 等) | +| AI 能力增强 | OCR 文字识别(代码已预留)· AI 翻译优化 · 智能标签 | +| 分布式爬取 | `distributed_crawler/` 目录已有 Master-Worker 架构原型 | +| PWA 移动端 | 缩略图墙升级为 PWA,支持手机浏览器直接访问 | + +--- + +## 十、许可与免责 + +- 本项目仅供个人学习、研究及合法使用。 +- 使用 X 插件需自行承担对应平台的使用条款风险。 +- APK 当前为调试签名,仅供个人测试,不宜正式分发。 +- 请遵守各内容平台的用户协议,合理使用爬取功能。 + +--- + +*文档生成于 2026-06-18 · Sakura Downloader Project* diff --git a/main.py b/main.py new file mode 100644 index 0000000..7c56d63 --- /dev/null +++ b/main.py @@ -0,0 +1,10 @@ +from pixiv_app.gui.app import SakuraDownloaderGUI + + +def main() -> None: + app = SakuraDownloaderGUI() + app.run() + + +if __name__ == "__main__": + main() diff --git a/pixiv_app/__init__.py b/pixiv_app/__init__.py new file mode 100644 index 0000000..220abf6 --- /dev/null +++ b/pixiv_app/__init__.py @@ -0,0 +1 @@ +"""Sakura downloader application package.""" diff --git a/pixiv_app/core/__init__.py b/pixiv_app/core/__init__.py new file mode 100644 index 0000000..440202d --- /dev/null +++ b/pixiv_app/core/__init__.py @@ -0,0 +1 @@ +"""Core business logic for Sakura downloader.""" diff --git a/pixiv_app/core/auth.py b/pixiv_app/core/auth.py new file mode 100644 index 0000000..4769413 --- /dev/null +++ b/pixiv_app/core/auth.py @@ -0,0 +1,167 @@ +from __future__ import annotations + +import html +import json +import re +from dataclasses import dataclass +from pathlib import Path +from typing import Any +from urllib.parse import urlencode + +import requests + +from pixiv_app.core.downloader import USER_AGENT, build_headers + + +LOGIN_PAGE_URL = "https://accounts.pixiv.net/login" +LOGIN_AJAX_URL = "https://accounts.pixiv.net/ajax/login?lang=zh" +VALIDATE_URL = "https://www.pixiv.net/setting_user.php" + + +@dataclass +class AuthResult: + success: bool + message: str + cookie: str = "" + requires_verification: bool = False + user_name: str = "" + user_id: str = "" + + +class SessionStore: + def __init__(self, path: str | Path = "pixiv_app_session.json") -> None: + self.path = Path(path) + + def load(self) -> dict[str, Any]: + if not self.path.exists(): + return {} + try: + return json.loads(self.path.read_text(encoding="utf-8")) + except Exception: + return {} + + def save(self, payload: dict[str, Any]) -> None: + self.path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8") + + +class PixivAuthClient: + def __init__(self) -> None: + self.timeout = 20 + + def validate_cookie(self, cookie: str) -> AuthResult: + cookie = cookie.strip() + if not cookie: + return AuthResult(False, "Cookie 为空,请先填写或登录。") + + session = requests.Session() + session.headers.update(build_headers(cookie=cookie, referer="https://www.pixiv.net/")) + try: + response = session.get(VALIDATE_URL, allow_redirects=True, timeout=self.timeout) + final_url = response.url + if "return_to=" in final_url or "accounts.pixiv.net" in final_url: + return AuthResult(False, "当前 Cookie 已失效或未登录,请更新会话。") + user_id, user_name = self._extract_user_info(response.text) + return AuthResult(True, "Cookie 有效,可直接开始抓取。", cookie=cookie, user_name=user_name, user_id=user_id) + except Exception as exc: + return AuthResult(False, f"Cookie 检测失败: {exc}") + + def login_with_password(self, login_id: str, password: str) -> AuthResult: + login_id = login_id.strip() + password = password.strip() + if not login_id or not password: + return AuthResult(False, "账号和密码都不能为空。") + + session = requests.Session() + session.headers.update( + { + "User-Agent": USER_AGENT, + "Accept": "application/json, text/plain, */*", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", + "Origin": "https://accounts.pixiv.net", + "Referer": LOGIN_PAGE_URL, + "X-Requested-With": "XMLHttpRequest", + } + ) + + try: + login_page = session.get(LOGIN_PAGE_URL, timeout=self.timeout) + login_page.raise_for_status() + tt = self._extract_tt(login_page.text) + + payload = { + "login_id": login_id, + "password": password, + "source": "accounts", + "app_ios": "0", + "ref": "", + "return_to": "https://www.pixiv.net/", + "g_recaptcha_response": "", + "recaptcha_enterprise_score_token": "", + } + headers = dict(session.headers) + if tt: + headers["X-CSRF-Token"] = tt + + response = session.post( + LOGIN_AJAX_URL, + data=urlencode(payload), + headers={**headers, "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}, + timeout=self.timeout, + ) + response.raise_for_status() + body = response.json() + + if isinstance(body, dict) and body.get("body", {}).get("success"): + cookie = self._cookies_to_header(session.cookies) + validation = self.validate_cookie(cookie) + if validation.success: + validation.message = "账号密码登录成功,会话已可用。" + return validation + return AuthResult(True, "登录接口返回成功,但会话校验未完全通过,请手动检测。", cookie=cookie) + + body_payload = body.get("body", {}) if isinstance(body, dict) else {} + errors = body_payload.get("errors", {}) if isinstance(body_payload, dict) else {} + if errors.get("recaptcha") or body_payload.get("requireExtraVerification") or body_payload.get("requireTwoFactorAuthentication"): + return AuthResult( + False, + "Pixiv 要求额外验证或二步验证,程序无法静默完成。请改用 Cookie 登录,或先在浏览器登录后导出 Cookie。", + requires_verification=True, + ) + message = self._flatten_error_message(errors) or "账号密码登录失败,请检查账号、密码或改用 Cookie。" + return AuthResult(False, message) + except Exception as exc: + return AuthResult(False, f"账号密码登录失败: {exc}") + + def _extract_tt(self, html_text: str) -> str: + match = re.search(r'"pixivAccount\.tt":"([^"]+)"', html_text) + return match.group(1) if match else "" + + def _extract_user_info(self, html_text: str) -> tuple[str, str]: + user_id = "" + user_name = "" + patterns = [ + r'"userData":\{"id":"(\d+)","pixivId":"[^"]*","name":"([^"]+)"', + r'"user_id":"(\d+)".*?"name":"([^"]+)"', + ] + for pattern in patterns: + match = re.search(pattern, html_text) + if match: + user_id = match.group(1) + user_name = html.unescape(match.group(2)) + break + return user_id, user_name + + def _flatten_error_message(self, errors: dict[str, Any]) -> str: + messages: list[str] = [] + for key, value in errors.items(): + if isinstance(value, str) and value.strip(): + messages.append(f"{key}: {value}") + elif isinstance(value, list): + text_items = [str(item).strip() for item in value if str(item).strip()] + if text_items: + messages.append(f"{key}: {'; '.join(text_items)}") + return " | ".join(messages) + + def _cookies_to_header(self, jar: requests.cookies.RequestsCookieJar) -> str: + parts = [f"{cookie.name}={cookie.value}" for cookie in jar] + return "; ".join(parts) diff --git a/pixiv_app/core/browser_proxy_pool.py b/pixiv_app/core/browser_proxy_pool.py new file mode 100644 index 0000000..efcf117 --- /dev/null +++ b/pixiv_app/core/browser_proxy_pool.py @@ -0,0 +1,353 @@ +""" +Playwright 浏览器代理池 - 支持动态换 IP +功能: +- 多代理轮换(轮询/随机/会话粘性) +- 健康检查(自动剔除失效代理) +- 延迟统计(加权选择最优代理) +- 线程安全 +""" + +import random +import threading +import time +from dataclasses import dataclass, field +from typing import Optional, List, Dict, Any +from enum import Enum + + +class RotationStrategy(Enum): + """轮换策略""" + ROUND_ROBIN = "round_robin" # 轮询 + RANDOM = "random" # 随机 + WEIGHTED = "weighted" # 加权(按成功率+延迟) + SESSION_STICKY = "session_sticky" # 会话粘性(同会话同IP) + + +@dataclass +class ProxyInfo: + """代理信息""" + server: str # http://ip:port 或 socks5://ip:port + username: str = "" + password: str = "" + + # 统计数据 + success_count: int = 0 + fail_count: int = 0 + total_latency_ms: float = 0.0 + avg_latency_ms: float = 0.0 + last_check_time: float = 0.0 + last_fail_time: float = 0.0 + + # 状态 + healthy: bool = True + cooldown_until: float = 0.0 # 冷却结束时间 + + @property + def proxy_url(self) -> str: + """获取完整的代理URL(含认证)""" + if self.username and self.password: + # http://user:pass@ip:port + protocol = self.server.split("://")[0] + rest = self.server.split("://")[1] + return f"{protocol}://{self.username}:{self.password}@{rest}" + return self.server + + def to_requests_proxies(self) -> dict[str, str]: + return {"http": self.proxy_url, "https": self.proxy_url} + + def to_playwright_proxy(self) -> dict[str, str]: + data = {"server": self.server} + if self.username: + data["username"] = self.username + data["password"] = self.password + return data + + @property + def success_rate(self) -> float: + """成功率""" + total = self.success_count + self.fail_count + if total == 0: + return 1.0 + return self.success_count / total + + @property + def weight(self) -> float: + """ + 计算代理权重(用于加权随机选择) + 考虑因素:成功率、延迟、冷却状态 + """ + if not self.healthy: + return 0.0 + + # 冷却中 + if self.cooldown_until > time.time(): + return 0.0 + + # 成功率权重 + success_weight = self.success_rate + + # 延迟权重(延迟越低权重越高) + if self.avg_latency_ms > 0: + latency_weight = max(0.1, 1000.0 / max(self.avg_latency_ms, 1)) + else: + latency_weight = 1.0 + + # 失败惩罚 + fail_penalty = 1.0 / (1.0 + self.fail_count * 0.5) + + return success_weight * latency_weight * fail_penalty + + +class ProxyPool: + """代理池 - 管理多个代理的获取和轮换""" + + def __init__( + self, + strategy: RotationStrategy = RotationStrategy.WEIGHTED, + health_check_url: str = "https://httpbin.org/ip", + health_check_timeout: int = 8, + max_failures: int = 3, + cooldown_seconds: int = 60, + auto_health_check: bool = True, + ): + """ + Args: + strategy: 轮换策略 + health_check_url: 健康检查URL + health_check_timeout: 健康检查超时(秒) + max_failures: 最大失败次数,超过后标记为不健康 + cooldown_seconds: 不健康代理的冷却时间(秒) + auto_health_check: 是否自动健康检查 + """ + self.strategy = strategy + self.health_check_url = health_check_url + self.health_check_timeout = health_check_timeout + self.max_failures = max_failures + self.cooldown_seconds = cooldown_seconds + self.auto_health_check = auto_health_check + + self._proxies: List[ProxyInfo] = [] + self._session_proxy_map: Dict[str, ProxyInfo] = {} # 会话粘性用 + self._round_robin_index = 0 + self._lock = threading.Lock() + + def add_proxy(self, server: str, username: str = "", password: str = "") -> None: + """添加单个代理""" + with self._lock: + # 去重 + existing = {p.server for p in self._proxies} + if server not in existing: + self._proxies.append(ProxyInfo( + server=server, + username=username, + password=password, + )) + + def add_proxy_url(self, value: str) -> None: + parsed = parse_proxy_url(value) + if parsed is not None: + self.add_proxy(parsed.server, parsed.username, parsed.password) + + def add_proxies(self, proxies: List[dict]) -> None: + """批量添加代理 + + Args: + proxies: [{"server": "http://1.2.3.4:8080", "username": "", "password": ""}, ...] + """ + for p in proxies: + self.add_proxy( + server=p["server"], + username=p.get("username", ""), + password=p.get("password", ""), + ) + + def add_proxy_text(self, text: str) -> int: + count = 0 + for line in text.splitlines(): + parsed = parse_proxy_url(line) + if parsed is None: + continue + self.add_proxy(parsed.server, parsed.username, parsed.password) + count += 1 + return count + + def remove_proxy(self, server: str) -> None: + """移除代理""" + with self._lock: + self._proxies = [p for p in self._proxies if p.server != server] + + def get_proxy(self, session_id: str = None) -> Optional[ProxyInfo]: + """ + 获取一个代理 + + Args: + session_id: 会话ID,用于SESSION_STICKY策略 + + Returns: + 代理信息,如果没有可用代理返回None + """ + with self._lock: + # 过滤出健康的代理 + now = time.time() + healthy = [p for p in self._proxies if p.healthy and p.cooldown_until <= now] + + if not healthy: + # 尝试从冷却中恢复 + now = time.time() + recovered = [] + for p in self._proxies: + if not p.healthy and p.cooldown_until <= now: + p.healthy = True + p.fail_count = 0 + recovered.append(p) + if recovered: + healthy = recovered + + if not healthy: + return None + + # 根据策略选择代理 + if self.strategy == RotationStrategy.RANDOM: + return random.choice(healthy) + + elif self.strategy == RotationStrategy.WEIGHTED: + # 加权随机选择 + weights = [p.weight for p in healthy] + total_weight = sum(weights) + if total_weight <= 0: + return random.choice(healthy) + r = random.uniform(0, total_weight) + cumsum = 0 + for i, w in enumerate(weights): + cumsum += w + if r <= cumsum: + return healthy[i] + return healthy[0] + + elif self.strategy == RotationStrategy.SESSION_STICKY: + if session_id and session_id in self._session_proxy_map: + proxy = self._session_proxy_map[session_id] + if proxy in healthy: + return proxy + # 新会话,分配一个代理 + proxy = self._get_round_robin(healthy) + if session_id: + self._session_proxy_map[session_id] = proxy + return proxy + + else: # ROUND_ROBIN + return self._get_round_robin(healthy) + + def _get_round_robin(self, proxies: List[ProxyInfo]) -> ProxyInfo: + """轮询获取代理""" + proxy = proxies[self._round_robin_index % len(proxies)] + self._round_robin_index += 1 + return proxy + + def mark_success(self, proxy: ProxyInfo, latency_ms: float = 0) -> None: + """标记代理请求成功""" + if proxy is None: + return + with self._lock: + proxy.success_count += 1 + proxy.fail_count = 0 + proxy.healthy = True + if latency_ms > 0: + # 更新平均延迟(指数移动平均) + if proxy.avg_latency_ms == 0: + proxy.avg_latency_ms = latency_ms + else: + proxy.avg_latency_ms = proxy.avg_latency_ms * 0.7 + latency_ms * 0.3 + proxy.last_check_time = time.time() + + def mark_failed(self, proxy: ProxyInfo) -> None: + """标记代理请求失败""" + if proxy is None: + return + with self._lock: + proxy.fail_count += 1 + if proxy.fail_count >= self.max_failures: + proxy.healthy = False + proxy.cooldown_until = time.time() + self.cooldown_seconds + + def health_check(self, proxy: ProxyInfo = None) -> bool: + """ + 健康检查单个或所有代理 + + Returns: + 如果检查通过返回True + """ + import requests + + proxies_to_check = [proxy] if proxy else self._proxies + + for p in proxies_to_check: + if p is None: + continue + try: + start = time.time() + resp = requests.get( + self.health_check_url, + proxies={"http": p.proxy_url, "https": p.proxy_url}, + timeout=self.health_check_timeout, + ) + if resp.status_code == 200: + latency_ms = (time.time() - start) * 1000 + self.mark_success(p, latency_ms) + else: + self.mark_failed(p) + except Exception: + self.mark_failed(p) + + return proxy.healthy if proxy else len(self.get_healthy_proxies()) > 0 + + def get_healthy_proxies(self) -> List[ProxyInfo]: + """获取所有健康代理""" + with self._lock: + return [p for p in self._proxies if p.healthy] + + def get_stats(self) -> dict: + """获取代理池统计信息""" + with self._lock: + healthy = [p for p in self._proxies if p.healthy] + return { + "total": len(self._proxies), + "healthy": len(healthy), + "unhealthy": len(self._proxies) - len(healthy), + "proxies": [ + { + "server": p.server, + "healthy": p.healthy, + "success_rate": p.success_rate, + "avg_latency_ms": round(p.avg_latency_ms, 2), + "fail_count": p.fail_count, + } + for p in self._proxies + ] + } + + def release_session(self, session_id: str) -> None: + """释放会话绑定的代理""" + with self._lock: + self._session_proxy_map.pop(session_id, None) + + +def parse_proxy_url(value: str) -> Optional[ProxyInfo]: + text = value.strip() + if not text or text.startswith("#"): + return None + if "://" not in text: + text = "http://" + text + import re + + match = re.match(r"^(https?|socks5)://(?:(?P[^:@/]+):(?P[^@/]+)@)?(?P[\w.\-]+):(?P\d{2,5})$", text, re.I) + if not match: + return None + scheme = text.split("://", 1)[0].lower() + host = match.group("host") + port = match.group("port") + user = match.group("user") or "" + pwd = match.group("pwd") or "" + return ProxyInfo(server=f"{scheme}://{host}:{port}", username=user, password=pwd) + + diff --git a/pixiv_app/core/cloud_sync.py b/pixiv_app/core/cloud_sync.py new file mode 100644 index 0000000..5f75580 --- /dev/null +++ b/pixiv_app/core/cloud_sync.py @@ -0,0 +1,602 @@ +from __future__ import annotations + +import base64 +import hashlib +import json +import logging +import threading +import time +from collections.abc import Callable +from dataclasses import asdict +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + +import requests + +from pixiv_app.core.paths import runtime_path + +logger = logging.getLogger(__name__) + + +def file_sha256(path: str | Path) -> str: + digest = hashlib.sha256() + with Path(path).open("rb") as fp: + for chunk in iter(lambda: fp.read(1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + + +def persist_cloud_session_tokens(access_token: str, refresh_token: str = "") -> None: + """将刷新后的 Token 写回桌面会话文件,供 GUI 与 Gallery API 共用。""" + from pixiv_app.core.auth import SessionStore + from pixiv_app.core.paths import app_session_file + + store = SessionStore(app_session_file()) + payload = store.load() + cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {} + cloud["access_token"] = access_token + if refresh_token: + cloud["refresh_token"] = refresh_token + payload["cloud_sync"] = cloud + store.save(payload) + + +def _decode_jwt_exp(token: str) -> float: + """解码 JWT 的 exp 字段,返回 Unix 时间戳;解码失败返回 0。""" + if not token or "." not in token: + return 0.0 + try: + payload = token.split(".")[1] + payload += "=" * (4 - len(payload) % 4) + decoded = json.loads(base64.urlsafe_b64decode(payload)) + return float(decoded.get("exp", 0)) + except Exception: + return 0.0 + + +class CloudSession: + """全局单例,统一管理云同步 Token 的读写、刷新与持久化。 + + 所有需要访问 Sakura Cloud API 的模块(GUI、Gallery、FollowSync、XMedia) + 都通过此单例获取配置和创建客户端,确保 token 刷新互不冲突、变更全局可见。 + """ + + _instance: CloudSession | None = None + _instance_lock = threading.Lock() + + def __new__(cls) -> CloudSession: + if cls._instance is None: + with cls._instance_lock: + if cls._instance is None: + instance = super().__new__(cls) + instance._base_url = "" + instance._access_token = "" + instance._refresh_token = "" + instance._enabled = False + instance._refresh_lock = threading.Lock() + instance._listeners: list[Callable[[str, str], None]] = [] + instance._last_refresh_time = 0.0 + cls._instance = instance + return cls._instance + + # ------------------------------------------------------------------ + # 配置入口 + # ------------------------------------------------------------------ + + def configure( + self, + *, + base_url: str = "", + access_token: str = "", + refresh_token: str = "", + enabled: bool = False, + ) -> None: + """GUI 启动 / 登录 / 保存会话时调用,同步最新 token 到全局单例。""" + self._base_url = (base_url or "").strip() + self._access_token = (access_token or "").strip() + self._refresh_token = (refresh_token or "").strip() + self._enabled = bool(enabled) + + def load_from_disk(self) -> dict[str, Any]: + """从磁盘会话文件读取云端配置并应用到单例,返回原始 payload。""" + from pixiv_app.core.auth import SessionStore + from pixiv_app.core.paths import app_session_file + + store = SessionStore(app_session_file()) + payload = store.load() + cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {} + self.configure( + base_url=str(cloud.get("base_url") or ""), + access_token=str(cloud.get("access_token") or ""), + refresh_token=str(cloud.get("refresh_token") or ""), + enabled=bool(cloud.get("enabled", False)), + ) + return payload + + @property + def enabled(self) -> bool: + return self._enabled and bool(self._base_url) and bool(self._access_token) + + # ------------------------------------------------------------------ + # Config 快照 + # ------------------------------------------------------------------ + + def get_config(self) -> CloudSyncConfig: + """返回当前 token 的 CloudSyncConfig 快照。 + + 调用时机:每次创建 SakuraCloudClient 前。 + 若 access_token 即将过期(< 5 分钟),主动触发一次刷新。 + """ + if self._access_token and self._is_expired_or_near(threshold_seconds=300): + self.try_refresh() + return CloudSyncConfig( + base_url=self._base_url, + access_token=self._access_token, + refresh_token=self._refresh_token, + ) + + # ------------------------------------------------------------------ + # 监听者管理 + # ------------------------------------------------------------------ + + def add_listener(self, callback: Callable[[str, str], None]) -> None: + """注册 token 刷新回调(如 GUI 的 _on_cloud_token_refreshed)。""" + if callback not in self._listeners: + self._listeners.append(callback) + + def remove_listener(self, callback: Callable[[str, str], None]) -> None: + try: + self._listeners.remove(callback) + except ValueError: + pass + + def notify_listeners(self, access_token: str, refresh_token: str) -> None: + """通知所有监听者 token 已更新(成功时为新的 token 对,失败时为空字符串)。""" + for callback in self._listeners: + try: + callback(access_token, refresh_token) + except Exception: + logger.exception("CloudSession listener %s failed", getattr(callback, "__name__", callback)) + + # ------------------------------------------------------------------ + # Token 刷新(核心) + # ------------------------------------------------------------------ + + def try_refresh(self) -> bool: + """统一刷新入口。使用全局锁防止并发重复刷新。 + + Returns: + True: 刷新成功,access_token 已更新 + False: 刷新失败,token 已被清空,监听者已收到空 token 通知 + """ + refresh = self._refresh_token + if not refresh: + return False + + with self._refresh_lock: + # 二次检查:可能其他线程已刷新 + refresh = self._refresh_token + if not refresh: + return False + + # 距上次刷新不足 5 秒,跳过(防抖) + elapsed = time.time() - self._last_refresh_time + if elapsed < 5.0 and self._access_token: + return True + + try: + resp = requests.post( + f"{self._base_url}/api/v1/auth/refresh", + json={"refresh_token": refresh}, + timeout=30.0, + ) + if resp.status_code != 200: + logger.warning( + "CloudSession refresh failed HTTP %s: %s", + resp.status_code, + resp.text[:200], + ) + return False + + body = resp.json() + data = body.get("data", body) if isinstance(body, dict) else {} + if not isinstance(data, dict): + self._on_refresh_failed() + return False + + new_access = (data.get("access_token") or "").strip() + new_refresh = (data.get("refresh_token") or "").strip() + if not new_access: + self._on_refresh_failed() + return False + + # 更新内存 + self._access_token = new_access + if new_refresh: + self._refresh_token = new_refresh + self._last_refresh_time = time.time() + + # 写盘 + self._persist() + + # 通知监听者 + self.notify_listeners(self._access_token, self._refresh_token) + logger.info("CloudSession token refreshed successfully") + return True + + except Exception: + logger.exception("CloudSession refresh error") + self._on_refresh_failed() + return False + + def _on_refresh_failed(self) -> None: + """刷新失败:清空 token + 写盘 + 通知监听者。""" + self._access_token = "" + self._refresh_token = "" + self._last_refresh_time = time.time() + self._persist() + self.notify_listeners("", "") + + def _persist(self) -> None: + """将当前 token 写入磁盘会话文件。异常记录日志但不抛出。""" + try: + persist_cloud_session_tokens(self._access_token, self._refresh_token) + except Exception: + logger.exception("CloudSession persist to disk failed") + + # ------------------------------------------------------------------ + # JWT 辅助 + # ------------------------------------------------------------------ + + def _is_expired_or_near(self, threshold_seconds: int = 300) -> bool: + exp = _decode_jwt_exp(self._access_token) + if exp <= 0: + return False + return (exp - threshold_seconds) <= time.time() + + +@dataclass +class CloudSyncConfig: + base_url: str + access_token: str + refresh_token: str = "" + chunk_size: int = 5 * 1024 * 1024 + timeout: float = 60.0 + + +@dataclass +class SyncMetadata: + resource_type: str = "artwork" + source_platform: str = "pixiv" + source_id: str | None = None + content_type: str | None = None + title: str = "" + author: str | None = None + author_id: str | None = None + tags: list[str] = field(default_factory=list) + metadata: dict[str, Any] = field(default_factory=dict) + page_count: int = 1 + thumbnail_url: str | None = None + description: str | None = None + word_count: int = 0 + series_id: str | None = None + series_title: str | None = None + chapter_title: str | None = None + chapter_order: int = 0 + + +class SyncFailureQueue: + def __init__(self, path: str | Path | None = None) -> None: + self.path = Path(path) if path else runtime_path("cloud_sync_failures.json") + + def load(self) -> list[dict[str, Any]]: + if not self.path.is_file(): + return [] + try: + data = json.loads(self.path.read_text(encoding="utf-8")) + return data if isinstance(data, list) else [] + except Exception: + return [] + + def save(self, items: list[dict[str, Any]]) -> None: + self.path.parent.mkdir(parents=True, exist_ok=True) + self.path.write_text(json.dumps(items, ensure_ascii=False, indent=2), encoding="utf-8") + + def add(self, file_path: str | Path, metadata: SyncMetadata, mime_type: str | None, error: str) -> None: + now = time.time() + file_path = str(Path(file_path)) + items = self.load() + key = (file_path, metadata.source_platform, metadata.source_id) + for item in items: + item_key = ( + str(item.get("file_path") or ""), + str(item.get("metadata", {}).get("source_platform") or ""), + item.get("metadata", {}).get("source_id"), + ) + if item_key == key: + attempts = int(item.get("attempts") or 0) + item.update( + { + "metadata": asdict(metadata), + "mime_type": mime_type, + "attempts": attempts, + "last_error": error, + "updated_at": now, + "next_retry_at": now + min(3600, 60 * (2 ** min(attempts, 5))), + } + ) + self.save(items) + return + items.append( + { + "file_path": file_path, + "metadata": asdict(metadata), + "mime_type": mime_type, + "attempts": 0, + "last_error": error, + "created_at": now, + "updated_at": now, + "next_retry_at": now + 60, + } + ) + self.save(items) + + +class SakuraCloudClient: + def __init__( + self, + config: CloudSyncConfig, + *, + on_token_refreshed: Callable[[str, str], None] | None = None, + cloud_session: CloudSession | None = None, + ) -> None: + self.config = config + self.base_url = config.base_url.rstrip("/") + self.session = requests.Session() + self.session.headers.update({"Authorization": f"Bearer {config.access_token}"}) + self._cloud_session = cloud_session # 全局单例引用 + # 仅在没有 cloud_session 时使用独立锁和回调(兼容旧路径) + if cloud_session is None: + self._on_token_refreshed = on_token_refreshed or persist_cloud_session_tokens + self._refresh_lock = threading.Lock() + else: + self._on_token_refreshed = None + self._refresh_lock = None + + def check_file(self, sha256: str, file_size: int) -> dict[str, Any]: + return self._post("/api/v1/sync/check", {"sha256": sha256, "file_size": file_size}) + + def init_upload( + self, + *, + sha256: str, + file_name: str, + file_size: int, + mime_type: str | None, + resource_type: str, + ) -> dict[str, Any]: + return self._post( + "/api/v1/upload/init", + { + "sha256": sha256, + "file_name": file_name, + "file_size": file_size, + "mime_type": mime_type, + "resource_type": resource_type, + }, + ) + + def upload_chunk(self, upload_id: str, chunk: bytes) -> dict[str, Any]: + response = self._request( + "POST", + "/api/v1/upload/chunk", + data={"upload_id": upload_id}, + files={"chunk": ("chunk.bin", chunk)}, + ) + return self._unwrap(response) + + def complete_upload(self, upload_id: str) -> dict[str, Any]: + return self._post("/api/v1/upload/complete", {"upload_id": upload_id}) + + def sync_metadata(self, file_id: str, metadata: SyncMetadata) -> dict[str, Any]: + payload = { + "file_id": file_id, + "resource_type": metadata.resource_type, + "source_platform": metadata.source_platform, + "source_id": metadata.source_id, + "content_type": metadata.content_type, + "title": metadata.title, + "author": metadata.author, + "author_id": metadata.author_id, + "tags": metadata.tags, + "metadata": metadata.metadata, + "page_count": metadata.page_count, + "thumbnail_url": metadata.thumbnail_url, + "description": metadata.description, + "word_count": metadata.word_count, + "series_id": metadata.series_id, + "series_title": metadata.series_title, + "chapter_title": metadata.chapter_title, + "chapter_order": metadata.chapter_order, + } + return self._post("/api/v1/sync/metadata", payload) + + def upsert_follow(self, payload: dict[str, Any]) -> dict[str, Any]: + return self._post("/api/v1/follows", payload) + + def bulk_upsert_follows(self, items: list[dict[str, Any]]) -> dict[str, Any]: + return self._post("/api/v1/follows/bulk-upsert", {"items": items}) + + def create_alert(self, payload: dict[str, Any]) -> dict[str, Any]: + return self._post("/api/v1/follows/alerts", payload) + + def _request( + self, + method: str, + path: str, + *, + json: dict[str, Any] | None = None, + data: dict[str, Any] | None = None, + files: dict[str, Any] | None = None, + _retry_on_401: bool = True, + ) -> requests.Response: + url = f"{self.base_url}{path}" + response = self.session.request( + method, + url, + json=json, + data=data, + files=files, + timeout=self.config.timeout, + ) + if response.status_code == 401 and _retry_on_401 and self._try_refresh_token(): + response = self.session.request( + method, + url, + json=json, + data=data, + files=files, + timeout=self.config.timeout, + ) + return response + + def _post(self, path: str, payload: dict[str, Any], *, _retry_on_401: bool = True) -> dict[str, Any]: + response = self._request("POST", path, json=payload, _retry_on_401=_retry_on_401) + return self._unwrap(response) + + def _try_refresh_token(self) -> bool: + # --- 新路径:委托给全局 CloudSession --- + if self._cloud_session is not None: + ok = self._cloud_session.try_refresh() + if ok: + # 刷新成功后同步更新本地 config 和 session headers + new_config = self._cloud_session.get_config() + self.config.access_token = new_config.access_token + self.config.refresh_token = new_config.refresh_token + self.session.headers.update({"Authorization": f"Bearer {new_config.access_token}"}) + return ok + + # --- 旧路径(向后兼容,无 cloud_session 时使用) --- + refresh = self.config.refresh_token.strip() + if not refresh: + return False + with self._refresh_lock: + refresh = self.config.refresh_token.strip() + if not refresh: + return False + try: + resp = requests.post( + f"{self.base_url}/api/v1/auth/refresh", + json={"refresh_token": refresh}, + timeout=self.config.timeout, + ) + if resp.status_code != 200: + return False + body = resp.json() + data = body.get("data", body) if isinstance(body, dict) else {} + if not isinstance(data, dict): + return False + new_access = (data.get("access_token") or "").strip() + new_refresh = (data.get("refresh_token") or "").strip() + if not new_access: + return False + self.config.access_token = new_access + self.session.headers.update({"Authorization": f"Bearer {new_access}"}) + if new_refresh: + self.config.refresh_token = new_refresh + try: + self._on_token_refreshed(self.config.access_token, self.config.refresh_token) + except Exception: + logger.exception("SakuraCloudClient._on_token_refreshed failed") + return True + except Exception: + logger.exception("SakuraCloudClient._try_refresh_token error") + return False + + @staticmethod + def _unwrap(response: requests.Response) -> dict[str, Any]: + response.raise_for_status() + body = response.json() + if isinstance(body, dict) and "data" in body: + return body["data"] + return body + + +class SyncManager: + def __init__(self, client: SakuraCloudClient, failure_queue: SyncFailureQueue | None = None) -> None: + self.client = client + self.failure_queue = failure_queue or SyncFailureQueue() + + def sync_file(self, file_path: str | Path, metadata: SyncMetadata, *, mime_type: str | None = None) -> dict[str, Any]: + try: + return self._sync_file_once(file_path, metadata, mime_type=mime_type) + except Exception as exc: + self.failure_queue.add(file_path, metadata, mime_type, str(exc)) + raise + + def _sync_file_once(self, file_path: str | Path, metadata: SyncMetadata, *, mime_type: str | None = None) -> dict[str, Any]: + path = Path(file_path) + sha256 = file_sha256(path) + file_size = path.stat().st_size + checked = self.client.check_file(sha256, file_size) + file_id = checked.get("file_id") if checked.get("owned_by_user") else None + + if not file_id: + init = self.client.init_upload( + sha256=sha256, + file_name=path.name, + file_size=file_size, + mime_type=mime_type, + resource_type=self._upload_resource_type(metadata, mime_type), + ) + if init.get("exists") and init.get("file_id"): + file_id = str(init["file_id"]) + else: + upload_id = str(init["upload_id"]) + with path.open("rb") as fp: + for chunk in iter(lambda: fp.read(self.client.config.chunk_size), b""): + self.client.upload_chunk(upload_id, chunk) + completed = self.client.complete_upload(upload_id) + file_id = str(completed["file_id"]) + + return self.client.sync_metadata(str(file_id), metadata) + + @staticmethod + def _upload_resource_type(metadata: SyncMetadata, mime_type: str | None) -> str: + if metadata.resource_type == "novel": + return "novel" + if (mime_type or "").lower().startswith("video/") or (metadata.content_type or "").lower() == "video": + return "video" + return "image" + + def retry_failed(self, *, limit: int = 5) -> dict[str, int]: + now = time.time() + items = self.failure_queue.load() + kept: list[dict[str, Any]] = [] + retried = 0 + succeeded = 0 + failed = 0 + for item in items: + if retried >= limit or float(item.get("next_retry_at") or 0) > now: + kept.append(item) + continue + path = Path(str(item.get("file_path") or "")) + if not path.is_file(): + failed += 1 + continue + try: + metadata = SyncMetadata(**dict(item.get("metadata") or {})) + self._sync_file_once(path, metadata, mime_type=item.get("mime_type")) + succeeded += 1 + except Exception as exc: + attempts = int(item.get("attempts") or 0) + 1 + item["attempts"] = attempts + item["last_error"] = str(exc) + item["updated_at"] = now + item["next_retry_at"] = now + min(3600, 60 * (2 ** min(attempts, 5))) + kept.append(item) + failed += 1 + retried += 1 + self.failure_queue.save(kept) + return {"retried": retried, "succeeded": succeeded, "failed": failed, "remaining": len(kept)} diff --git a/pixiv_app/core/cookie_import.py b/pixiv_app/core/cookie_import.py new file mode 100644 index 0000000..afd287b --- /dev/null +++ b/pixiv_app/core/cookie_import.py @@ -0,0 +1,175 @@ +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + + +Cookie = dict[str, Any] + + +def parse_cookie_text(text: str) -> list[Cookie]: + """Parse JSON cookie exports or Netscape cookies.txt content.""" + text = text.strip() + if not text: + raise ValueError("Cookie 文件为空。") + + try: + payload = json.loads(text) + except json.JSONDecodeError: + payload = None + + if payload is not None: + raw_items = payload.get("cookies", payload) if isinstance(payload, dict) else payload + if not isinstance(raw_items, list): + raise ValueError("Cookie JSON 必须是数组,或包含 cookies 数组。") + cookies = [_normalize_json_cookie(item) for item in raw_items if isinstance(item, dict)] + else: + cookies = _parse_netscape_cookie_text(text) + + cookies = [item for item in cookies if item.get("name") and item.get("value") is not None] + if not cookies: + raise ValueError("没有解析到有效 Cookie。") + return cookies + + +def filter_cookies_for_domains(cookies: list[Cookie], domains: tuple[str, ...]) -> list[Cookie]: + result: list[Cookie] = [] + for cookie in cookies: + domain = str(cookie.get("domain", "")).lower().lstrip(".") + if any(domain == item or domain.endswith("." + item) for item in domains): + result.append(cookie) + return result + + +def cookie_domains(cookies: list[Cookie]) -> list[str]: + domains = sorted({str(item.get("domain", "")).lower().lstrip(".") for item in cookies if item.get("domain")}) + return [item for item in domains if item] + + +def has_cookie_domain(cookies: list[Cookie], domains: tuple[str, ...]) -> bool: + return bool(filter_cookies_for_domains(cookies, domains)) + + +def cookies_to_header(cookies: list[Cookie], domains: tuple[str, ...] | None = None) -> str: + selected = filter_cookies_for_domains(cookies, domains) if domains else cookies + pairs: list[str] = [] + seen: set[str] = set() + for cookie in selected: + name = str(cookie.get("name", "")).strip() + value = str(cookie.get("value", "")) + if not name or name in seen: + continue + seen.add(name) + pairs.append(f"{name}={value}") + if not pairs: + raise ValueError("没有找到匹配站点的 Cookie。") + return "; ".join(pairs) + + +def cookies_to_playwright(cookies: list[Cookie], domains: tuple[str, ...] | None = None) -> list[Cookie]: + selected = filter_cookies_for_domains(cookies, domains) if domains else cookies + output_by_key: dict[tuple[str, str, str], Cookie] = {} + for cookie in selected: + name = str(cookie.get("name") or "").strip() + domain = str(cookie.get("domain") or "").strip() + if not name or not domain: + continue + item: Cookie = { + "name": name, + "value": str(cookie.get("value", "")), + "domain": domain, + "path": str(cookie.get("path") or "/"), + "secure": bool(cookie.get("secure", False)), + "httpOnly": bool(cookie.get("httpOnly", False)), + } + expires = cookie.get("expires") + if expires is not None: + try: + expires_value = int(float(expires)) + if expires_value > 0: + item["expires"] = expires_value + except (TypeError, ValueError): + pass + same_site = str(cookie.get("sameSite", "") or "").strip() + if same_site in {"Strict", "Lax", "None"}: + item["sameSite"] = same_site + key = (str(item["domain"]).lower(), str(item["path"]), name) + output_by_key[key] = item + output = list(output_by_key.values()) + if not output: + raise ValueError("没有找到匹配站点的 Cookie。") + return output + + +def save_playwright_cookies(cookies: list[Cookie], path: str | Path) -> Path: + target = Path(path) + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(json.dumps(cookies, ensure_ascii=False, indent=2), encoding="utf-8") + return target + + +def cookie_summary(cookies: list[Cookie]) -> str: + domains = sorted({str(item.get("domain", "")).lstrip(".") or "-" for item in cookies}) + shown = ", ".join(domains[:5]) + suffix = "..." if len(domains) > 5 else "" + return f"{len(cookies)} 个 Cookie;域名:{shown}{suffix}" + + +def _parse_netscape_cookie_text(text: str) -> list[Cookie]: + cookies: list[Cookie] = [] + for raw_line in text.splitlines(): + line = raw_line.strip() + if not line or line.startswith("#HttpOnly_"): + http_only = line.startswith("#HttpOnly_") + line = line.removeprefix("#HttpOnly_") + elif line.startswith("#"): + continue + else: + http_only = False + + parts = line.split("\t") + if len(parts) < 7: + parts = line.split() + if len(parts) < 7: + continue + + domain, _include_subdomains, path, secure, expires, name = parts[:6] + value = "\t".join(parts[6:]) + cookies.append( + { + "domain": domain, + "path": path or "/", + "secure": secure.upper() == "TRUE", + "expires": int(expires) if str(expires).isdigit() else -1, + "name": name, + "value": value, + "httpOnly": http_only, + "sameSite": "Lax", + } + ) + return cookies + + +def _normalize_json_cookie(item: Cookie) -> Cookie: + expires = item.get("expires", item.get("expirationDate", item.get("expiry"))) + same_site = _normalize_same_site(item.get("sameSite")) + return { + "domain": str(item.get("domain") or item.get("host") or ""), + "path": str(item.get("path") or "/"), + "secure": bool(item.get("secure", False)), + "expires": expires, + "name": str(item.get("name") or ""), + "value": str(item.get("value") or ""), + "httpOnly": bool(item.get("httpOnly", item.get("httponly", False))), + "sameSite": same_site, + } + + +def _normalize_same_site(value: Any) -> str: + text = str(value or "").strip().lower() + if text in {"strict", "samesitestrict"}: + return "Strict" + if text in {"none", "no_restriction", "samesitenone"}: + return "None" + return "Lax" diff --git a/pixiv_app/core/downloader.py b/pixiv_app/core/downloader.py new file mode 100644 index 0000000..2e6d87d --- /dev/null +++ b/pixiv_app/core/downloader.py @@ -0,0 +1,1007 @@ +from __future__ import annotations + +import concurrent.futures +import html +import json +import os +import re +import threading +import time +from dataclasses import dataclass +from pathlib import Path +from typing import Callable, Optional +from urllib.parse import quote, urlparse + +import requests + +from pixiv_app.core.library import DEFAULT_LIBRARY_DB, DownloadFileRecord, PixivLibrary +from pixiv_app.core.proxy_pool import ProxyPool + + +USER_AGENT = ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/123.0.0.0 Safari/537.36" +) +DEFAULT_TIMEOUT = 30 + + +@dataclass +class DownloadResult: + work_id: int + total_pages: int + downloaded_pages: int + skipped_pages: int + failed_pages: int + ok: bool + message: str = "" + + +class PixivRequestError(RuntimeError): + def __init__(self, message: str, status_code: Optional[int] = None) -> None: + super().__init__(message) + self.status_code = status_code + + +def sanitize_filename(name: str, fallback: str) -> str: + cleaned = re.sub(r'[<>:"/\\|?*\x00-\x1F]+', "_", str(name)).strip().rstrip(".") + return cleaned or fallback + + +def build_headers(cookie: str = "", referer: Optional[str] = None) -> dict[str, str]: + headers = { + "User-Agent": USER_AGENT, + "Accept": "application/json, text/plain, */*", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", + "Origin": "https://www.pixiv.net", + } + headers["Referer"] = referer or "https://www.pixiv.net/" + if cookie: + headers["Cookie"] = cookie + return headers + + +def create_session(cookie: str = "") -> requests.Session: + session = requests.Session() + session.headers.update(build_headers(cookie=cookie)) + return session + + +def ensure_success(response: requests.Response) -> None: + response.raise_for_status() + + +def parse_pixiv_target(raw_value: str) -> tuple[str, int]: + text = raw_value.strip() + if not text: + raise ValueError("请输入 Pixiv 作者 ID、作品 ID、小说 ID,或对应链接。") + + patterns = ( + (r"/users/(\d+)", "user"), + (r"/artworks/(\d+)", "illust"), + (r"[?&]illust_id=(\d+)", "illust"), + (r"/novel/series/(\d+)", "novel_series"), + (r"/novel/show\.php\?id=(\d+)", "novel"), + (r"/novel/show\.php.*?[?&]id=(\d+)", "novel"), + (r"/novel/(\d+)", "novel"), + ) + for pattern, target_kind in patterns: + match = re.search(pattern, text, flags=re.IGNORECASE) + if match: + return target_kind, int(match.group(1)) + + if text.isdigit(): + return "unknown", int(text) + + raise ValueError("无法识别输入内容,请填写 Pixiv 作者 ID、作品 ID、小说 ID,或完整链接。") + + +def extract_work_ids(payload: dict) -> list[int]: + body = payload.get("body", {}) + work_ids: set[int] = set() + for section in ("illusts", "manga"): + section_data = body.get(section, {}) + if not isinstance(section_data, dict): + continue + for key in section_data.keys(): + if str(key).isdigit(): + work_ids.add(int(key)) + return sorted(work_ids, reverse=True) + + +def _pick_proxy(proxy_pool: Optional[ProxyPool]) -> tuple[Optional[dict[str, str]], object]: + if not proxy_pool: + return None, None + proxy = proxy_pool.get_proxy(rotate=True) + if not proxy: + return None, None + return {"http": proxy.proxy_url, "https": proxy.proxy_url}, proxy + + +def request_json( + session: requests.Session, + url: str, + *, + cookie: str = "", + referer: Optional[str] = None, + proxy_pool: Optional[ProxyPool] = None, + context: str = "Pixiv 请求", +) -> dict: + last_error: Optional[Exception] = None + for _ in range(3): + proxies, proxy_obj = _pick_proxy(proxy_pool) + try: + response = session.get( + url, + headers=build_headers(cookie=cookie, referer=referer), + timeout=DEFAULT_TIMEOUT, + proxies=proxies, + ) + if response.status_code == 404: + raise PixivRequestError(f"{context}不存在,或当前账号无权访问。", status_code=404) + ensure_success(response) + payload = response.json() + if isinstance(payload, dict) and payload.get("error"): + message = str(payload.get("message") or "返回了错误响应") + raise PixivRequestError(f"{context}失败: {message}", status_code=response.status_code) + if not isinstance(payload, dict): + raise PixivRequestError(f"{context}返回格式异常。", status_code=response.status_code) + return payload + except PixivRequestError as exc: + last_error = exc + if proxy_pool and proxy_obj: + proxy_pool.mark_failed(proxy_obj) + if exc.status_code == 404: + break + time.sleep(0.5) + except Exception as exc: + last_error = exc + if proxy_pool and proxy_obj: + proxy_pool.mark_failed(proxy_obj) + time.sleep(0.5) + + if last_error: + if isinstance(last_error, PixivRequestError): + raise last_error + raise PixivRequestError(f"{context}失败: {last_error}") + raise PixivRequestError(f"{context}失败。") + + +def fetch_illust_details( + illust_id: int, + *, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> dict: + close_session = session is None + session = session or create_session(cookie) + try: + payload = request_json( + session, + f"https://www.pixiv.net/ajax/illust/{illust_id}?lang=zh", + cookie=cookie, + referer=f"https://www.pixiv.net/artworks/{illust_id}", + proxy_pool=proxy_pool, + context=f"作品 {illust_id} 信息请求", + ) + return payload.get("body", {}) if isinstance(payload.get("body"), dict) else {} + finally: + if close_session: + session.close() + + +def fetch_user_profile( + user_id: int, + *, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> dict: + close_session = session is None + session = session or create_session(cookie) + try: + payload = request_json( + session, + f"https://www.pixiv.net/ajax/user/{int(user_id)}?full=1&lang=zh", + cookie=cookie, + referer=f"https://www.pixiv.net/users/{int(user_id)}", + proxy_pool=proxy_pool, + context=f"作者 {int(user_id)} 信息请求", + ) + body = payload.get("body", {}) + return body if isinstance(body, dict) else {} + finally: + if close_session: + session.close() + + +def resolve_user_id_from_illust( + illust_id: int, + *, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> int: + body = fetch_illust_details( + illust_id, + cookie=cookie, + session=session, + proxy_pool=proxy_pool, + ) + user_id = body.get("userId") + if str(user_id).isdigit(): + return int(user_id) + raise PixivRequestError(f"作品 {illust_id} 未返回有效作者 ID。") + + +def fetch_user_work_ids( + user_id: int, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, + input_kind: str = "user", +) -> tuple[int, list[int]]: + close_session = session is None + session = session or create_session(cookie) + try: + def load_user_works(resolved_user_id: int) -> list[int]: + payload = request_json( + session, + f"https://www.pixiv.net/ajax/user/{resolved_user_id}/profile/all?sensitiveFilterMode=userSetting&lang=zh", + cookie=cookie, + referer=f"https://www.pixiv.net/users/{resolved_user_id}", + proxy_pool=proxy_pool, + context=f"作者 {resolved_user_id} 作品列表请求", + ) + return extract_work_ids(payload) + + if input_kind == "illust": + resolved_user_id = resolve_user_id_from_illust( + user_id, + cookie=cookie, + session=session, + proxy_pool=proxy_pool, + ) + return resolved_user_id, load_user_works(resolved_user_id) + + try: + return user_id, load_user_works(user_id) + except PixivRequestError as exc: + if input_kind != "unknown" or exc.status_code != 404: + raise + + try: + resolved_user_id = resolve_user_id_from_illust( + user_id, + cookie=cookie, + session=session, + proxy_pool=proxy_pool, + ) + return resolved_user_id, load_user_works(resolved_user_id) + except PixivRequestError as exc: + if exc.status_code == 404: + raise PixivRequestError("输入的 ID 既不是可访问的作者,也不是可访问的作品。", status_code=404) from exc + raise + finally: + if close_session: + session.close() + + +def fetch_illust_pages( + illust_id: int, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> list[str]: + close_session = session is None + session = session or create_session(cookie) + referer = f"https://www.pixiv.net/artworks/{illust_id}" + try: + payload = request_json( + session, + f"https://www.pixiv.net/ajax/illust/{illust_id}/pages?lang=zh", + cookie=cookie, + referer=referer, + proxy_pool=proxy_pool, + context=f"作品 {illust_id} 页面请求", + ) + body = payload.get("body", []) + if isinstance(body, list): + urls = [page.get("urls", {}).get("original") for page in body if isinstance(page, dict)] + urls = [url for url in urls if url] + if urls: + return urls + + detail = fetch_illust_details( + illust_id, + cookie=cookie, + session=session, + proxy_pool=proxy_pool, + ) + original_url = detail.get("urls", {}).get("original") if isinstance(detail.get("urls"), dict) else None + return [original_url] if original_url else [] + finally: + if close_session: + session.close() + + +def fetch_novel_details( + novel_id: int, + *, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> dict: + close_session = session is None + session = session or create_session(cookie) + try: + payload = request_json( + session, + f"https://www.pixiv.net/ajax/novel/{novel_id}?lang=zh", + cookie=cookie, + referer=f"https://www.pixiv.net/novel/show.php?id={novel_id}", + proxy_pool=proxy_pool, + context=f"小说 {novel_id} 信息请求", + ) + return payload.get("body", {}) if isinstance(payload.get("body"), dict) else {} + finally: + if close_session: + session.close() + + +def fetch_novel_text( + novel_id: int, + *, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> tuple[dict, str]: + close_session = session is None + session = session or create_session(cookie) + try: + detail = fetch_novel_details( + novel_id, + cookie=cookie, + session=session, + proxy_pool=proxy_pool, + ) + content = detail.get("content") + if isinstance(content, str) and content.strip(): + return detail, content + + referer = f"https://www.pixiv.net/novel/show.php?id={novel_id}" + content_body: Optional[dict] = None + for url in ( + f"https://www.pixiv.net/ajax/novel/text/{novel_id}?lang=zh", + f"https://www.pixiv.net/ajax/novel/{novel_id}/text?lang=zh", + ): + try: + payload = request_json( + session, + url, + cookie=cookie, + referer=referer, + proxy_pool=proxy_pool, + context=f"小说 {novel_id} 正文请求", + ) + body = payload.get("body", {}) + if isinstance(body, dict): + content_body = body + break + except PixivRequestError as exc: + if exc.status_code != 404: + raise + if content_body is None: + raise PixivRequestError(f"未获取到小说 {novel_id} 正文。") + + content = content_body.get("content") + if not isinstance(content, str) or not content.strip(): + raise PixivRequestError(f"小说 {novel_id} 正文为空。") + return detail, content + finally: + if close_session: + session.close() + + +def fetch_novel_series_ids( + series_id: int, + *, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> tuple[str | None, list[int]]: + close_session = session is None + session = session or create_session(cookie) + try: + series_title: str | None = None + deduped: list[int] = [] + seen: set[int] = set() + + def add_ids(values: list[int]) -> None: + for novel_id in values: + if novel_id == series_id or novel_id in seen: + continue + seen.add(novel_id) + deduped.append(novel_id) + + def collect_from_json(value: object) -> list[int]: + ids: list[int] = [] + if isinstance(value, dict): + for key, item in value.items(): + if key in {"novelId", "novel_id", "workId"} and str(item).isdigit(): + ids.append(int(item)) + elif ( + key == "id" + and str(item).isdigit() + and any(marker in value for marker in ("title", "textCount", "characterCount", "createDate", "url")) + ): + ids.append(int(item)) + ids.extend(collect_from_json(item)) + elif isinstance(value, list): + for item in value: + ids.extend(collect_from_json(item)) + return ids + + def pick_title(value: object) -> str | None: + if isinstance(value, dict): + for key in ("title", "seriesTitle", "name"): + item = value.get(key) + if isinstance(item, str) and item.strip(): + return item.strip() + for item in value.values(): + found = pick_title(item) + if found: + return found + elif isinstance(value, list): + for item in value: + found = pick_title(item) + if found: + return found + return None + + for url in ( + f"https://www.pixiv.net/ajax/novel/series/{series_id}?lang=zh", + f"https://www.pixiv.net/ajax/novel/series_content/{series_id}?limit=100&last_order=0&order_by=asc&lang=zh", + ): + try: + payload = request_json( + session, + url, + cookie=cookie, + referer=f"https://www.pixiv.net/novel/series/{series_id}", + proxy_pool=proxy_pool, + context=f"小说合集 {series_id} 列表请求", + ) + except Exception: + continue + body = payload.get("body", payload) + series_title = series_title or pick_title(body) + add_ids(collect_from_json(body)) + if deduped: + return series_title, deduped + + for page in range(1, 21): + before = len(deduped) + for url in ( + f"https://www.pixiv.net/ajax/novel/series/{series_id}?p={page}&lang=zh", + ( + f"https://www.pixiv.net/ajax/novel/series_content/{series_id}" + f"?limit=30&last_order={(page - 1) * 30}&order_by=asc&lang=zh" + ), + ): + try: + payload = request_json( + session, + url, + cookie=cookie, + referer=f"https://www.pixiv.net/novel/series/{series_id}", + proxy_pool=proxy_pool, + context=f"小说合集 {series_id} 第 {page} 页请求", + ) + except Exception: + continue + body = payload.get("body", payload) + series_title = series_title or pick_title(body) + add_ids(collect_from_json(body)) + if len(deduped) == before: + break + + url = f"https://www.pixiv.net/novel/series/{series_id}" + proxies, _proxy_obj = _pick_proxy(proxy_pool) + response = session.get( + url, + headers=build_headers(cookie=cookie, referer=url), + timeout=DEFAULT_TIMEOUT, + proxies=proxies, + ) + ensure_success(response) + page_html = response.text + title_match = re.search(r']*(?:content="([^"]+)"|>(.*?))', + page_html, + flags=re.IGNORECASE | re.DOTALL, + ) + for match in preload_matches: + raw_json = next((part for part in match if part), "") + if not raw_json: + continue + try: + preload = json.loads(html.unescape(raw_json)) + except Exception: + continue + series_title = series_title or pick_title(preload) + add_ids(collect_from_json(preload)) + + if not deduped: + raise PixivRequestError(f"小说合集 {series_id} 未解析到章节,请确认 Cookie 权限或稍后重试。") + return series_title, deduped + finally: + if close_session: + session.close() + + +def search_illust_ids_by_keyword( + keyword: str, + *, + cookie: str = "", + session: Optional[requests.Session] = None, + proxy_pool: Optional[ProxyPool] = None, + limit: int = 30, +) -> list[int]: + cleaned_keyword = keyword.strip() + if not cleaned_keyword: + raise ValueError("关键词不能为空。") + + close_session = session is None + session = session or create_session(cookie) + encoded = quote(cleaned_keyword, safe="") + work_ids: list[int] = [] + seen: set[int] = set() + page = 1 + try: + while len(work_ids) < limit and page <= max(3, limit): + payload = request_json( + session, + ( + f"https://www.pixiv.net/ajax/search/artworks/{encoded}" + f"?word={encoded}&order=date_d&mode=all&p={page}&s_mode=s_tag_full&type=all&lang=zh" + ), + cookie=cookie, + referer=f"https://www.pixiv.net/tags/{encoded}/artworks", + proxy_pool=proxy_pool, + context=f"关键词“{cleaned_keyword}”搜索请求", + ) + body = payload.get("body", {}) + illust_manga = body.get("illustManga", {}) if isinstance(body, dict) else {} + items = illust_manga.get("data", []) if isinstance(illust_manga, dict) else [] + if not isinstance(items, list) or not items: + break + + added_this_page = 0 + for item in items: + item_id = item.get("id") if isinstance(item, dict) else None + if str(item_id).isdigit(): + illust_id = int(item_id) + if illust_id not in seen: + seen.add(illust_id) + work_ids.append(illust_id) + added_this_page += 1 + if len(work_ids) >= limit: + break + if added_this_page == 0: + break + page += 1 + return work_ids[:limit] + finally: + if close_session: + session.close() + + +def is_file_complete(path: Path) -> bool: + return path.exists() and path.is_file() and path.stat().st_size > 1024 + + +def download_binary( + file_url: str, + save_dir: Path, + cookie: str = "", + session: Optional[requests.Session] = None, + timeout: int = DEFAULT_TIMEOUT, + retry_count: int = 3, + proxy_pool: Optional[ProxyPool] = None, +) -> tuple[bool, str]: + close_session = session is None + session = session or create_session(cookie) + save_dir.mkdir(parents=True, exist_ok=True) + + filename = os.path.basename(urlparse(file_url).path) + save_path = save_dir / filename + if is_file_complete(save_path): + return True, f"跳过已存在文件: {filename}" + temp_path = save_path.with_suffix(save_path.suffix + ".part") + + try: + for attempt in range(1, retry_count + 1): + proxies, proxy_obj = _pick_proxy(proxy_pool) + try: + response = session.get( + file_url, + headers=build_headers(cookie=cookie, referer="https://www.pixiv.net/"), + timeout=timeout, + stream=True, + proxies=proxies, + ) + ensure_success(response) + with open(temp_path, "wb") as file_handle: + for chunk in response.iter_content(chunk_size=1024 * 256): + if chunk: + file_handle.write(chunk) + if temp_path.stat().st_size <= 1024: + raise IOError("文件过小,可能不是有效图片。") + temp_path.replace(save_path) + return True, f"下载成功: {filename}" + except Exception as exc: + if proxy_pool and proxy_obj: + proxy_pool.mark_failed(proxy_obj) + if temp_path.exists(): + temp_path.unlink() + if attempt == retry_count: + return False, f"下载失败 {filename}: {exc}" + time.sleep(min(0.8 * attempt, 2.0)) + finally: + if close_session: + session.close() + + +def save_text_file(save_path: Path, content: str) -> tuple[bool, str]: + save_path.parent.mkdir(parents=True, exist_ok=True) + if save_path.exists() and save_path.stat().st_size > 32: + return True, f"跳过已存在文件: {save_path.name}" + temp_path = save_path.with_suffix(save_path.suffix + ".part") + with open(temp_path, "w", encoding="utf-8-sig") as file_handle: + file_handle.write(content) + temp_path.replace(save_path) + return True, f"保存成功: {save_path.name}" + + +def download_illust( + illust_id: int, + user_id: int, + cookie: str = "", + save_root: str | Path = "Sakura_Downloads", + save_subdir: Optional[str] = None, + request_delay: float = 0.0, + stop_event: Optional[threading.Event] = None, + proxy_pool: Optional[ProxyPool] = None, + library_db_path: str | Path | None = DEFAULT_LIBRARY_DB, +) -> DownloadResult: + if stop_event and stop_event.is_set(): + return DownloadResult(illust_id, 0, 0, 0, 0, False, "任务已停止") + if request_delay > 0: + time.sleep(request_delay) + + session = create_session(cookie) + subdir = save_subdir or str(user_id) + save_dir = Path(save_root) / subdir + library: PixivLibrary | None = PixivLibrary(library_db_path) if library_db_path else None + file_records: list[DownloadFileRecord] = [] + artwork_type = "illust" + try: + detail = fetch_illust_details(illust_id, cookie=cookie, session=session, proxy_pool=proxy_pool) + artwork_type = "manga" if int(detail.get("pageCount") or 0) > 1 or int(detail.get("illustType") or 0) == 1 else "illust" + if library is not None: + library.ensure_artwork_stub(illust_id, artwork_type=artwork_type) + library.upsert_artwork(detail, artwork_type=artwork_type) + resolved_user_id = detail.get("userId") + if user_id == 0 and str(resolved_user_id).isdigit(): + user_id = int(resolved_user_id) + page_urls = fetch_illust_pages(illust_id, cookie=cookie, session=session, proxy_pool=proxy_pool) + if not page_urls: + result = DownloadResult(illust_id, 0, 0, 0, 1, False, "未获取到原图地址") + if library is not None: + library.ensure_artwork_stub(illust_id, artwork_type=artwork_type) + library.record_download_history( + artwork_id=illust_id, + status="failed", + downloaded_pages=0, + skipped_pages=0, + failed_pages=1, + message=result.message, + ) + return result + + downloaded_pages, skipped_pages, failed_pages = 0, 0, 0 + for page_index, page_url in enumerate(page_urls): + if stop_event and stop_event.is_set(): + break + filename = os.path.basename(urlparse(page_url).path) + target_path = save_dir / filename + if is_file_complete(target_path): + skipped_pages += 1 + file_records.append( + DownloadFileRecord( + page_index=page_index, + file_path=str(target_path.resolve()), + source_url=page_url, + status="skipped", + size_bytes=target_path.stat().st_size, + ) + ) + continue + success, _ = download_binary( + page_url, + save_dir=save_dir, + cookie=cookie, + session=session, + proxy_pool=proxy_pool, + ) + if success: + downloaded_pages += 1 + file_records.append( + DownloadFileRecord( + page_index=page_index, + file_path=str(target_path.resolve()), + source_url=page_url, + status="downloaded", + size_bytes=target_path.stat().st_size if target_path.exists() else 0, + ) + ) + else: + failed_pages += 1 + file_records.append( + DownloadFileRecord( + page_index=page_index, + file_path=str(target_path.resolve()), + source_url=page_url, + status="failed", + ) + ) + + total_pages = len(page_urls) + ok = failed_pages == 0 and total_pages > 0 + message = f"作品 {illust_id}: 共 {total_pages} 页, 新下 {downloaded_pages}, 跳过 {skipped_pages}, 失败 {failed_pages}" + if library is not None: + library.ensure_artwork_stub(illust_id, artwork_type=artwork_type) + library.record_files(illust_id, file_records) + library.record_download_history( + artwork_id=illust_id, + status="success" if ok else "failed", + downloaded_pages=downloaded_pages, + skipped_pages=skipped_pages, + failed_pages=failed_pages, + message=message, + ) + return DownloadResult(illust_id, total_pages, downloaded_pages, skipped_pages, failed_pages, ok, message) + except Exception as exc: + message = f"作品 {illust_id} 处理失败: {exc}" + if library is not None: + library.ensure_artwork_stub(illust_id, artwork_type="illust") + library.record_download_history( + artwork_id=illust_id, + status="failed", + downloaded_pages=0, + skipped_pages=0, + failed_pages=1, + message=message, + ) + return DownloadResult(illust_id, 0, 0, 0, 1, False, message) + finally: + if library is not None: + library.close() + session.close() + + +def download_novel( + novel_id: int, + cookie: str = "", + save_root: str | Path = "Sakura_Downloads", + save_subdir: str = "novels", + request_delay: float = 0.0, + stop_event: Optional[threading.Event] = None, + proxy_pool: Optional[ProxyPool] = None, + library_db_path: str | Path | None = DEFAULT_LIBRARY_DB, + metadata_overrides: Optional[dict[str, object]] = None, +) -> DownloadResult: + if stop_event and stop_event.is_set(): + return DownloadResult(novel_id, 0, 0, 0, 0, False, "任务已停止") + if request_delay > 0: + time.sleep(request_delay) + + session = create_session(cookie) + library: PixivLibrary | None = PixivLibrary(library_db_path) if library_db_path else None + library_artwork_id = novel_id + try: + detail, content = fetch_novel_text( + novel_id, + cookie=cookie, + session=session, + proxy_pool=proxy_pool, + ) + if metadata_overrides: + detail.update(metadata_overrides) + detail.setdefault("id", novel_id) + detail.setdefault("novelId", novel_id) + if library is not None: + library_artwork_id = library.ensure_artwork_stub(novel_id, artwork_type="novel") + library.upsert_artwork(detail, artwork_type="novel") + title = sanitize_filename(detail.get("title", ""), f"novel_{novel_id}") + user_name = sanitize_filename(detail.get("userName", ""), "unknown_author") + user_id = detail.get("userId") + author_dir = f"{user_name}_{user_id}" if str(user_id).isdigit() else user_name + save_dir = Path(save_root) / save_subdir / sanitize_filename(author_dir, f"novel_{novel_id}") + save_path = save_dir / f"{title}_{novel_id}.txt" + text = "\n".join( + [ + f"标题: {detail.get('title', title)}", + f"作者: {detail.get('userName', '')}", + f"小说ID: {novel_id}", + "", + content, + ] + ) + _, message = save_text_file(save_path, text) + result_message = f"小说 {novel_id}: {message}" + status = "skipped" if "跳过" in message else "downloaded" + downloaded_pages = 1 if status == "downloaded" else 0 + skipped_pages = 1 if status == "skipped" else 0 + if library is not None: + library.record_files( + library_artwork_id, + [ + DownloadFileRecord( + page_index=0, + file_path=str(save_path.resolve()), + source_url=f"https://www.pixiv.net/novel/show.php?id={novel_id}", + status=status, + size_bytes=save_path.stat().st_size if save_path.exists() else 0, + ) + ], + ) + library.record_download_history( + artwork_id=library_artwork_id, + status="success", + downloaded_pages=downloaded_pages, + skipped_pages=skipped_pages, + failed_pages=0, + message=result_message, + ) + return DownloadResult(novel_id, 1, downloaded_pages, skipped_pages, 0, True, result_message) + except Exception as exc: + message = f"小说 {novel_id} 下载失败: {exc}" + if library is not None: + library_artwork_id = library.ensure_artwork_stub(novel_id, artwork_type="novel") + library.record_download_history( + artwork_id=library_artwork_id, + status="failed", + downloaded_pages=0, + skipped_pages=0, + failed_pages=1, + message=message, + ) + return DownloadResult(novel_id, 1, 0, 0, 1, False, message) + finally: + if library is not None: + library.close() + session.close() + + +def resolve_workers(user_workers: int, proxy_mode: bool) -> int: + if not proxy_mode: + return max(1, user_workers) + return max(3, min(user_workers, 6)) + + +def _download_illust_batch( + work_ids: list[int], + *, + cookie: str = "", + max_workers: int = 6, + request_delay: float = 0.0, + save_root: str | Path = "Sakura_Downloads", + save_subdir: Optional[str] = None, + stop_event: Optional[threading.Event] = None, + progress_callback: Optional[Callable[[DownloadResult, int, int], None]] = None, + proxy_pool: Optional[ProxyPool] = None, + user_id_hint: int = 0, +) -> list[DownloadResult]: + worker_count = resolve_workers(max_workers, proxy_mode=proxy_pool is not None) + results: list[DownloadResult] = [] + if not work_ids: + return results + + with concurrent.futures.ThreadPoolExecutor(max_workers=worker_count) as executor: + futures = { + executor.submit( + download_illust, + illust_id=work_id, + user_id=user_id_hint, + cookie=cookie, + save_root=save_root, + save_subdir=save_subdir, + request_delay=request_delay, + stop_event=stop_event, + proxy_pool=proxy_pool, + ): work_id + for work_id in work_ids + } + completed, total = 0, len(futures) + for future in concurrent.futures.as_completed(futures): + result = future.result() + results.append(result) + completed += 1 + if progress_callback: + progress_callback(result, completed, total) + if stop_event and stop_event.is_set(): + break + + results.sort(key=lambda item: item.work_id, reverse=True) + return results + + +def download_user_works( + user_id: int, + cookie: str = "", + max_workers: int = 6, + request_delay: float = 0.0, + save_root: str | Path = "Sakura_Downloads", + stop_event: Optional[threading.Event] = None, + progress_callback: Optional[Callable[[DownloadResult, int, int], None]] = None, + work_ids: Optional[list[int]] = None, + proxy_pool: Optional[ProxyPool] = None, +) -> tuple[list[int], list[DownloadResult]]: + if work_ids is None: + _, work_ids = fetch_user_work_ids(user_id=user_id, cookie=cookie, proxy_pool=proxy_pool) + results = _download_illust_batch( + work_ids, + cookie=cookie, + max_workers=max_workers, + request_delay=request_delay, + save_root=save_root, + save_subdir=str(user_id), + stop_event=stop_event, + progress_callback=progress_callback, + proxy_pool=proxy_pool, + user_id_hint=user_id, + ) + return work_ids, results + + +def download_keyword_works( + keyword: str, + *, + cookie: str = "", + max_workers: int = 6, + request_delay: float = 0.0, + save_root: str | Path = "Sakura_Downloads", + stop_event: Optional[threading.Event] = None, + progress_callback: Optional[Callable[[DownloadResult, int, int], None]] = None, + proxy_pool: Optional[ProxyPool] = None, + limit: int = 30, + work_ids: Optional[list[int]] = None, +) -> tuple[list[int], list[DownloadResult]]: + if work_ids is None: + work_ids = search_illust_ids_by_keyword( + keyword, + cookie=cookie, + proxy_pool=proxy_pool, + limit=limit, + ) + results = _download_illust_batch( + work_ids, + cookie=cookie, + max_workers=max_workers, + request_delay=request_delay, + save_root=save_root, + save_subdir=str(Path("keywords") / sanitize_filename(keyword, "keyword")), + stop_event=stop_event, + progress_callback=progress_callback, + proxy_pool=proxy_pool, + ) + return work_ids, results diff --git a/pixiv_app/core/fingerprint.py b/pixiv_app/core/fingerprint.py new file mode 100644 index 0000000..9d9deb6 --- /dev/null +++ b/pixiv_app/core/fingerprint.py @@ -0,0 +1,159 @@ +from __future__ import annotations + +import copy +import os +import random +import threading +from dataclasses import dataclass, field +from enum import Enum +from pathlib import Path +from typing import Any + + +class DeviceType(Enum): + WINDOWS_CHROME = "windows_chrome" + WINDOWS_EDGE = "windows_edge" + MAC_CHROME = "mac_chrome" + + +@dataclass +class BrowserProfile: + """Stable browser context settings for one logical session. + + This module intentionally avoids Canvas/WebGL spoofing or large fingerprint rotation. + The goal is consistency inside a session, not impersonating many different users. + """ + + user_agent: str + platform: str = "Win32" + viewport: dict[str, int] = field(default_factory=lambda: {"width": 1920, "height": 1080}) + locale: str = "zh-CN" + timezone_id: str = "Asia/Shanghai" + color_scheme: str = "light" + reduced_motion: str = "no-preference" + device_scale_factor: float = 1.0 + is_mobile: bool = False + has_touch: bool = False + + def to_playwright_context_options(self) -> dict[str, Any]: + return { + "user_agent": self.user_agent, + "viewport": dict(self.viewport), + "locale": self.locale, + "timezone_id": self.timezone_id, + "color_scheme": self.color_scheme, + "reduced_motion": self.reduced_motion, + "device_scale_factor": self.device_scale_factor, + "is_mobile": self.is_mobile, + "has_touch": self.has_touch, + } + + def headers(self) -> dict[str, str]: + return { + "User-Agent": self.user_agent, + "Accept-Language": f"{self.locale},zh;q=0.9,en;q=0.8", + } + + +class BrowserProfileDatabase: + @staticmethod + def windows_chrome() -> BrowserProfile: + return BrowserProfile( + user_agent=real_browser_user_agent() + or "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", + platform="Win32", + viewport={"width": 1920, "height": 1080}, + ) + + @staticmethod + def windows_edge() -> BrowserProfile: + return BrowserProfile( + user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0", + platform="Win32", + viewport={"width": 1536, "height": 864}, + device_scale_factor=1.25, + ) + + @staticmethod + def mac_chrome() -> BrowserProfile: + return BrowserProfile( + user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", + platform="MacIntel", + viewport={"width": 1512, "height": 982}, + device_scale_factor=2.0, + ) + + @staticmethod + def all_desktop() -> list[BrowserProfile]: + return [ + BrowserProfileDatabase.windows_chrome(), + BrowserProfileDatabase.windows_edge(), + BrowserProfileDatabase.mac_chrome(), + ] + + @staticmethod + def by_device_type(device_type: DeviceType) -> BrowserProfile: + mapping = { + DeviceType.WINDOWS_CHROME: BrowserProfileDatabase.windows_chrome, + DeviceType.WINDOWS_EDGE: BrowserProfileDatabase.windows_edge, + DeviceType.MAC_CHROME: BrowserProfileDatabase.mac_chrome, + } + return mapping.get(device_type, BrowserProfileDatabase.windows_chrome)() + + +class SessionFingerprintManager: + """Session-sticky browser profile manager.""" + + def __init__(self, profiles: list[BrowserProfile] | None = None, *, strategy: str = "sticky") -> None: + self.profiles = profiles or BrowserProfileDatabase.all_desktop() + self.strategy = strategy + self._session_map: dict[str, BrowserProfile] = {} + self._index = 0 + self._lock = threading.Lock() + + def get_fingerprint(self, session_id: str = "default") -> BrowserProfile: + with self._lock: + if self.strategy == "sticky" and session_id in self._session_map: + return copy.deepcopy(self._session_map[session_id]) + if self.strategy == "round_robin": + profile = self.profiles[self._index % len(self.profiles)] + self._index += 1 + elif self.strategy == "random": + profile = random.choice(self.profiles) + else: + stable_index = abs(hash(session_id)) % len(self.profiles) + profile = self.profiles[stable_index] + self._session_map[session_id] = copy.deepcopy(profile) + return copy.deepcopy(profile) + + def get_stats(self) -> dict[str, Any]: + return { + "strategy": self.strategy, + "total_profiles": len(self.profiles), + "sticky_sessions": len(self._session_map), + } + + +Fingerprint = BrowserProfile +FingerprintManager = SessionFingerprintManager +FingerprintDatabase = BrowserProfileDatabase + + +def real_browser_user_agent() -> str: + """Load a user-provided real browser UA, if configured.""" + env_value = os.environ.get("PIXIV_REAL_USER_AGENT", "").strip() + if env_value: + return env_value + path_value = os.environ.get("PIXIV_REAL_USER_AGENT_FILE", "").strip() + if path_value: + path = Path(path_value) + if path.is_file(): + return path.read_text(encoding="utf-8").strip() + return "" + + +def default_profile(session_id: str = "default") -> BrowserProfile: + return SessionFingerprintManager().get_fingerprint(session_id) diff --git a/pixiv_app/core/gallery_service.py b/pixiv_app/core/gallery_service.py new file mode 100644 index 0000000..3f0e06b --- /dev/null +++ b/pixiv_app/core/gallery_service.py @@ -0,0 +1,597 @@ +from __future__ import annotations + +import re +from pathlib import Path +from typing import Any + +from pixiv_app.core.auth import SessionStore +from pixiv_app.core.downloader import download_user_works, fetch_user_profile, fetch_user_work_ids +from pixiv_app.core.library_importer import LegacyDownloadImporter +from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary +from pixiv_app.core.paths import app_session_file +from pixiv_app.core.thumbnails import DEFAULT_THUMBNAIL_DIR, ThumbnailCache + + +class LocalGalleryService: + def __init__( + self, + *, + db_path: str | Path = DEFAULT_LIBRARY_DB, + thumbnail_dir: str | Path = DEFAULT_THUMBNAIL_DIR, + ) -> None: + self.db_path = Path(db_path) + library = PixivLibrary(self.db_path) + library.close() + self.thumbnail_cache = ThumbnailCache(db_path=self.db_path, cache_dir=thumbnail_dir) + + def list_artworks( + self, + *, + query: str = "", + search_scope: str = "all", + tag: str = "", + artist_id: int | None = None, + artwork_type: str = "", + include_restricted: bool = True, + limit: int = 60, + offset: int = 0, + ensure_thumbnails: bool = True, + ) -> dict[str, Any]: + limit = max(1, min(int(limit), 200)) + offset = max(0, int(offset)) + library = PixivLibrary(self.db_path) + try: + where: list[str] = [] + params: list[Any] = [] + joins = """ + LEFT JOIN artists ar ON ar.artist_id = a.artist_id + LEFT JOIN followed_artists fa ON fa.artist_id = a.artist_id AND fa.enabled = 1 + LEFT JOIN thumbnail_cache tc ON tc.artwork_id = a.artwork_id AND tc.page_index = 0 + LEFT JOIN artwork_files af ON af.artwork_id = a.artwork_id AND af.page_index = 0 + LEFT JOIN artwork_ocr_texts ocr ON ocr.artwork_id = a.artwork_id + """ + if query: + like = f"%{query}%" + if search_scope == "ocr": + where.append("(ocr.status = 'done' AND ocr.ocr_text LIKE ?)") + params.append(like) + elif search_scope == "title": + where.append("(a.title LIKE ? OR a.caption LIKE ? OR ar.name LIKE ?)") + params.extend([like, like, like]) + elif search_scope == "tags": + joins += """ + LEFT JOIN artwork_tags at_search ON at_search.artwork_id = a.artwork_id + LEFT JOIN tags t_search ON t_search.tag_id = at_search.tag_id + """ + where.append("t_search.name LIKE ?") + params.append(like) + else: + joins += """ + LEFT JOIN artwork_tags at_search ON at_search.artwork_id = a.artwork_id + LEFT JOIN tags t_search ON t_search.tag_id = at_search.tag_id + """ + where.append( + "(a.title LIKE ? OR a.caption LIKE ? OR ar.name LIKE ? OR t_search.name LIKE ? " + "OR (ocr.status = 'done' AND ocr.ocr_text LIKE ?))" + ) + params.extend([like, like, like, like, like]) + if tag: + joins += """ + JOIN artwork_tags at_filter ON at_filter.artwork_id = a.artwork_id + JOIN tags t_filter ON t_filter.tag_id = at_filter.tag_id + """ + where.append("t_filter.name = ?") + params.append(tag) + if artist_id is not None: + where.append("a.artist_id = ?") + params.append(int(artist_id)) + if artwork_type: + where.append("a.type = ?") + params.append(artwork_type) + else: + where.append("a.type IN ('illust', 'manga')") + if not include_restricted: + where.append("a.x_restrict = 0") + + where_sql = "WHERE " + " AND ".join(where) if where else "" + total = library.connection.execute( + f"SELECT COUNT(DISTINCT a.artwork_id) AS total FROM artworks a {joins} {where_sql}", + params, + ).fetchone()["total"] + rows = library.connection.execute( + f""" + SELECT + a.artwork_id, + a.type, + a.title, + a.artist_id, + ar.name AS artist_name, + ar.account AS artist_account, + a.page_count, + a.bookmark_count, + a.like_count, + a.view_count, + a.x_restrict, + a.ai_type, + a.thumbnail_url, + CASE WHEN fav.artwork_id IS NULL THEN 0 ELSE 1 END AS is_favorite, + CASE WHEN fa.artist_id IS NULL THEN 0 ELSE 1 END AS is_followed, + COALESCE(tc.thumbnail_path, '') AS thumbnail_path, + COALESCE(af.file_path, '') AS primary_file_path, + CASE + WHEN ocr.status = 'done' AND ocr.ocr_text LIKE ? THEN 'ocr' + ELSE '' + END AS matched_by, + a.updated_at + FROM artworks a + {joins} + LEFT JOIN favorites fav ON fav.artwork_id = a.artwork_id + {where_sql} + GROUP BY a.artwork_id + ORDER BY a.updated_at DESC, a.artwork_id DESC + LIMIT ? OFFSET ? + """, + [f"%{query}%" if query else "", *params, limit, offset], + ).fetchall() + + items = [dict(row) for row in rows] + if ensure_thumbnails: + self._ensure_list_thumbnails(items) + items = self._refresh_thumbnail_paths(library, items) + return {"total": int(total), "limit": limit, "offset": offset, "items": items} + finally: + library.close() + + def get_artwork(self, artwork_id: int, *, ensure_thumbnails: bool = True) -> dict[str, Any] | None: + if ensure_thumbnails: + self.thumbnail_cache.ensure_for_artwork(artwork_id) + library = PixivLibrary(self.db_path) + try: + row = library.connection.execute( + """ + SELECT a.*, ar.name AS artist_name, ar.account AS artist_account + , CASE WHEN fav.artwork_id IS NULL THEN 0 ELSE 1 END AS is_favorite + , CASE WHEN fa.artist_id IS NULL THEN 0 ELSE 1 END AS is_followed + FROM artworks a + LEFT JOIN artists ar ON ar.artist_id = a.artist_id + LEFT JOIN favorites fav ON fav.artwork_id = a.artwork_id + LEFT JOIN followed_artists fa ON fa.artist_id = a.artist_id AND fa.enabled = 1 + WHERE a.artwork_id = ? + """, + (artwork_id,), + ).fetchone() + if row is None: + return None + files = library.connection.execute( + """ + SELECT f.*, tc.thumbnail_path, tc.width AS thumbnail_width, tc.height AS thumbnail_height + FROM artwork_files f + LEFT JOIN thumbnail_cache tc + ON tc.artwork_id = f.artwork_id AND tc.page_index = f.page_index + WHERE f.artwork_id = ? + ORDER BY f.page_index ASC + """, + (artwork_id,), + ).fetchall() + tags = library.connection.execute( + """ + SELECT t.name, t.translated_name + FROM tags t + JOIN artwork_tags at ON at.tag_id = t.tag_id + WHERE at.artwork_id = ? + ORDER BY t.name ASC + """, + (artwork_id,), + ).fetchall() + history = library.connection.execute( + """ + SELECT status, downloaded_pages, skipped_pages, failed_pages, message, finished_at + FROM download_history + WHERE artwork_id = ? + ORDER BY finished_at DESC + LIMIT 20 + """, + (artwork_id,), + ).fetchall() + return { + "artwork": dict(row), + "files": [dict(item) for item in files], + "tags": [dict(item) for item in tags], + "history": [dict(item) for item in history], + } + finally: + library.close() + + def list_tags(self, *, query: str = "", limit: int = 100) -> list[dict[str, Any]]: + library = PixivLibrary(self.db_path) + try: + params: list[Any] = [] + where = "" + if query: + where = "WHERE t.name LIKE ? OR t.translated_name LIKE ?" + params.extend([f"%{query}%", f"%{query}%"]) + rows = library.connection.execute( + f""" + SELECT t.name, t.translated_name, COUNT(at.artwork_id) AS artwork_count + FROM tags t + LEFT JOIN artwork_tags at ON at.tag_id = t.tag_id + {where} + GROUP BY t.tag_id + ORDER BY artwork_count DESC, t.name ASC + LIMIT ? + """, + [*params, max(1, min(int(limit), 500))], + ).fetchall() + return [dict(row) for row in rows] + finally: + library.close() + + def list_artists(self, *, query: str = "", limit: int = 100) -> list[dict[str, Any]]: + library = PixivLibrary(self.db_path) + try: + params: list[Any] = [] + where = "" + if query: + where = "WHERE ar.name LIKE ? OR ar.account LIKE ?" + params.extend([f"%{query}%", f"%{query}%"]) + rows = library.connection.execute( + f""" + SELECT ar.artist_id, ar.name, ar.account, COUNT(a.artwork_id) AS artwork_count + FROM artists ar + LEFT JOIN artworks a ON a.artist_id = ar.artist_id + {where} + GROUP BY ar.artist_id + ORDER BY + CASE WHEN COALESCE(NULLIF(ar.name, ''), NULLIF(ar.account, '')) = '' THEN 1 ELSE 0 END, + COALESCE(NULLIF(ar.name, ''), NULLIF(ar.account, ''), CAST(ar.artist_id AS TEXT)) ASC, + artwork_count DESC + LIMIT ? + """, + [*params, max(1, min(int(limit), 500))], + ).fetchall() + return [dict(row) for row in rows] + finally: + library.close() + + def get_artist(self, artist_id: int, *, limit: int = 60, offset: int = 0) -> dict[str, Any] | None: + library = PixivLibrary(self.db_path) + try: + row = library.connection.execute( + """ + SELECT ar.*, COUNT(a.artwork_id) AS artwork_count, + CASE WHEN fa.enabled = 1 THEN 1 ELSE 0 END AS is_followed, + fa.last_synced_at, fa.last_error + FROM artists ar + LEFT JOIN artworks a ON a.artist_id = ar.artist_id + LEFT JOIN followed_artists fa ON fa.artist_id = ar.artist_id + WHERE ar.artist_id = ? + GROUP BY ar.artist_id + """, + (artist_id,), + ).fetchone() + if row is None: + return None + return { + "artist": dict(row), + "artworks": self.list_artworks(artist_id=artist_id, limit=limit, offset=offset), + } + finally: + library.close() + + def list_favorites(self, *, limit: int = 60, offset: int = 0) -> dict[str, Any]: + library = PixivLibrary(self.db_path) + try: + total = library.connection.execute( + """ + SELECT COUNT(*) AS total + FROM favorites fav + JOIN artworks a ON a.artwork_id = fav.artwork_id + WHERE a.type IN ('illust', 'manga') + """ + ).fetchone()["total"] + rows = library.connection.execute( + """ + SELECT + a.artwork_id, a.type, a.title, a.artist_id, ar.name AS artist_name, + ar.account AS artist_account, + a.page_count, a.bookmark_count, a.like_count, a.view_count, + a.x_restrict, a.ai_type, a.thumbnail_url, + 1 AS is_favorite, + CASE WHEN fa.artist_id IS NULL THEN 0 ELSE 1 END AS is_followed, + COALESCE(tc.thumbnail_path, '') AS thumbnail_path, + COALESCE(af.file_path, '') AS primary_file_path, + fav.created_at AS favorite_at + FROM favorites fav + JOIN artworks a ON a.artwork_id = fav.artwork_id + LEFT JOIN artists ar ON ar.artist_id = a.artist_id + LEFT JOIN followed_artists fa ON fa.artist_id = a.artist_id AND fa.enabled = 1 + LEFT JOIN thumbnail_cache tc ON tc.artwork_id = a.artwork_id AND tc.page_index = 0 + LEFT JOIN artwork_files af ON af.artwork_id = a.artwork_id AND af.page_index = 0 + WHERE a.type IN ('illust', 'manga') + ORDER BY fav.created_at DESC + LIMIT ? OFFSET ? + """, + (max(1, min(int(limit), 200)), max(0, int(offset))), + ).fetchall() + items = [dict(row) for row in rows] + self._ensure_list_thumbnails(items) + items = self._refresh_thumbnail_paths(library, items) + return {"total": int(total), "limit": limit, "offset": offset, "items": items} + finally: + library.close() + + def set_favorite(self, artwork_id: int, favorite: bool, note: str = "") -> dict[str, Any]: + library = PixivLibrary(self.db_path) + try: + library.set_favorite(artwork_id, favorite, note) + return {"artwork_id": artwork_id, "favorite": favorite} + finally: + library.close() + + def list_followed_artists(self) -> list[dict[str, Any]]: + library = PixivLibrary(self.db_path) + try: + rows = library.connection.execute( + """ + SELECT fa.artist_id, COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), '') AS name, + COALESCE(NULLIF(ar.account, ''), '') AS account, + fa.enabled, fa.last_synced_at, fa.last_error, + COUNT(a.artwork_id) AS artwork_count + FROM followed_artists fa + LEFT JOIN artists ar ON ar.artist_id = fa.artist_id + LEFT JOIN artworks a ON a.artist_id = fa.artist_id + WHERE fa.enabled = 1 + GROUP BY fa.artist_id + ORDER BY + CASE WHEN COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), NULLIF(ar.account, '')) = '' THEN 1 ELSE 0 END, + COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), NULLIF(ar.account, ''), CAST(fa.artist_id AS TEXT)) ASC, + fa.artist_id ASC + """ + ).fetchall() + return [dict(row) for row in rows] + finally: + library.close() + + def set_followed_artist(self, artist_ref: int | str, followed: bool, name: str = "") -> dict[str, Any]: + artist_id = self._resolve_artist_id(artist_ref) + clean_name = self._clean_name(name) + account = "" + if followed and not clean_name: + try: + profile = fetch_user_profile( + artist_id, + cookie=str(SessionStore(app_session_file()).load().get("cookie", "")).strip(), + ) + profile_info = profile.get("profile") if isinstance(profile.get("profile"), dict) else {} + clean_name = self._clean_name( + profile.get("name") + or profile.get("userName") + or profile.get("displayName") + or profile_info.get("name") + ) + account = self._clean_name(profile.get("account") or profile.get("userAccount")) + except Exception: + clean_name = "" + library = PixivLibrary(self.db_path) + try: + if clean_name or account: + library.upsert_artist_profile(artist_id, name=clean_name, account=account) + library.set_followed_artist(artist_id, followed, clean_name) + return { + "artist_id": artist_id, + "followed": followed, + "name": clean_name, + "account": account, + "cloud_synced": False, + } + finally: + library.close() + + def _resolve_artist_id(self, artist_ref: int | str) -> int: + text = str(artist_ref).strip() + if not text: + raise ValueError("Please enter a Pixiv artist ID, profile URL, or synced artist name.") + + if text.lower().startswith("artist_") and text[7:].isdigit(): + return int(text[7:]) + + match = re.search(r"/users/(\d+)", text) + if match: + return int(match.group(1)) + + if text.isdigit(): + return int(text) + + lookup = text.lstrip("@") + library = PixivLibrary(self.db_path) + try: + rows = [ + dict(row) + for row in library.connection.execute( + """ + SELECT artist_id, name, account + FROM artists + WHERE lower(name) = lower(?) OR lower(account) = lower(?) + ORDER BY + CASE + WHEN lower(account) = lower(?) THEN 0 + WHEN lower(name) = lower(?) THEN 1 + ELSE 2 + END, + artist_id ASC + """, + (lookup, lookup, lookup, lookup), + ).fetchall() + ] + finally: + library.close() + + if len(rows) == 1: + return int(rows[0]["artist_id"]) + if not rows: + raise ValueError("Could not resolve Pixiv artist ID from that name. Enter an ID/profile URL or sync the artist first.") + choices = ", ".join(str(int(row["artist_id"])) for row in rows[:5]) + raise ValueError(f"Artist name matched multiple Pixiv users; use an ID instead: {choices}") + + @staticmethod + def _clean_name(value: Any) -> str: + text = str(value or "").strip() + return "" if text.lower() in {"none", "null", "undefined"} else text + + def scan_legacy_downloads(self, *, root: str | Path = "Sakura_Downloads") -> dict[str, Any]: + summary = LegacyDownloadImporter(db_path=self.db_path).scan(root) + self.thumbnail_cache.build_missing(limit=500) + return summary.__dict__ + + def sync_followed_artists( + self, + *, + cookie: str = "", + max_new_per_artist: int = 20, + download: bool = True, + save_root: str | Path = "Sakura_Downloads", + ) -> dict[str, Any]: + if not cookie: + cookie = str(SessionStore(app_session_file()).load().get("cookie", "")).strip() + if not cookie: + raise ValueError("No Pixiv cookie available. Save a session in the GUI first or pass cookie explicitly.") + + library = PixivLibrary(self.db_path) + try: + followed = library.connection.execute( + """ + SELECT fa.artist_id, + COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), NULLIF(ar.account, ''), '') AS name, + COALESCE(NULLIF(ar.account, ''), '') AS account + FROM followed_artists fa + LEFT JOIN artists ar ON ar.artist_id = fa.artist_id + WHERE fa.enabled = 1 + ORDER BY fa.artist_id + """ + ).fetchall() + finally: + library.close() + + results: list[dict[str, Any]] = [] + for row in followed: + artist_id = int(row["artist_id"]) + artist_name = self._clean_name(row["name"]) + artist_account = self._clean_name(row["account"]) + library = PixivLibrary(self.db_path) + try: + existing = { + int(item["artwork_id"]) + for item in library.connection.execute( + "SELECT artwork_id FROM artworks WHERE artist_id = ? AND type IN ('illust', 'manga')", + (artist_id,), + ).fetchall() + } + finally: + library.close() + + try: + _, work_ids = fetch_user_work_ids(user_id=artist_id, cookie=cookie) + new_ids = [work_id for work_id in work_ids if work_id not in existing][: max(0, int(max_new_per_artist))] + downloaded = 0 + if download and new_ids: + _, download_results = download_user_works( + user_id=artist_id, + cookie=cookie, + max_workers=3, + request_delay=0.2, + save_root=save_root, + work_ids=new_ids, + ) + downloaded = len([item for item in download_results if item.ok]) + library = PixivLibrary(self.db_path) + try: + library.mark_artist_synced(artist_id) + finally: + library.close() + cloud_synced = False + cloud_alerts = 0 + results.append( + { + "artist_id": artist_id, + "remote_count": len(work_ids), + "new_count": len(new_ids), + "downloaded": downloaded, + "cloud_synced": cloud_synced, + "alerts": cloud_alerts, + "error": "", + } + ) + except Exception as exc: + library = PixivLibrary(self.db_path) + try: + library.mark_artist_synced(artist_id, error=str(exc)) + finally: + library.close() + results.append({"artist_id": artist_id, "remote_count": 0, "new_count": 0, "downloaded": 0, "alerts": 0, "error": str(exc)}) + return {"artists": results} + + def build_missing_thumbnails(self, *, limit: int = 200) -> list[dict[str, Any]]: + return [item.__dict__ for item in self.thumbnail_cache.build_missing(limit=limit)] + + def get_thumbnail_path(self, artwork_id: int, *, page_index: int = 0) -> Path | None: + self.thumbnail_cache.ensure_for_artwork(artwork_id) + library = PixivLibrary(self.db_path) + try: + row = library.connection.execute( + """ + SELECT thumbnail_path + FROM thumbnail_cache + WHERE artwork_id = ? AND page_index = ? + """, + (artwork_id, page_index), + ).fetchone() + if row is None: + return None + path = Path(str(row["thumbnail_path"])) + return path if path.exists() else None + finally: + library.close() + + def get_source_file_path(self, artwork_id: int, *, page_index: int = 0) -> Path | None: + library = PixivLibrary(self.db_path) + try: + row = library.connection.execute( + """ + SELECT file_path + FROM artwork_files + WHERE artwork_id = ? AND page_index = ? + """, + (artwork_id, page_index), + ).fetchone() + if row is None: + return None + path = Path(str(row["file_path"])) + return path if path.exists() else None + finally: + library.close() + + def _ensure_list_thumbnails(self, items: list[dict[str, Any]]) -> None: + for item in items: + if item.get("thumbnail_path"): + continue + if not item.get("primary_file_path"): + continue + self.thumbnail_cache.ensure_for_artwork(int(item["artwork_id"])) + + def _refresh_thumbnail_paths(self, library: PixivLibrary, items: list[dict[str, Any]]) -> list[dict[str, Any]]: + artwork_ids = [int(item["artwork_id"]) for item in items] + if not artwork_ids: + return items + placeholders = ",".join("?" for _ in artwork_ids) + rows = library.connection.execute( + f""" + SELECT artwork_id, thumbnail_path + FROM thumbnail_cache + WHERE page_index = 0 AND artwork_id IN ({placeholders}) + """, + artwork_ids, + ).fetchall() + thumbs = {int(row["artwork_id"]): str(row["thumbnail_path"]) for row in rows} + for item in items: + item["thumbnail_path"] = thumbs.get(int(item["artwork_id"]), item.get("thumbnail_path", "")) + return items diff --git a/pixiv_app/core/library.py b/pixiv_app/core/library.py new file mode 100644 index 0000000..b7de2df --- /dev/null +++ b/pixiv_app/core/library.py @@ -0,0 +1,703 @@ +from __future__ import annotations + +import json +import re +import sqlite3 +from dataclasses import dataclass +from pathlib import Path +from typing import Any + +from pixiv_app.core.paths import app_library_file + +DEFAULT_LIBRARY_DB = app_library_file() + + +@dataclass +class DownloadFileRecord: + page_index: int + file_path: str + source_url: str + status: str + size_bytes: int = 0 + content_hash: str = "" + + +class PixivLibrary: + def __init__(self, db_path: str | Path = DEFAULT_LIBRARY_DB) -> None: + self.db_path = Path(db_path) + self.db_path.parent.mkdir(parents=True, exist_ok=True) if self.db_path.parent != Path(".") else None + # Allow sharing one connection across threads when guarded by a lock (e.g. TaskQueue). + self.connection = sqlite3.connect(self.db_path, timeout=30, check_same_thread=False) + self.connection.row_factory = sqlite3.Row + self.connection.execute("PRAGMA journal_mode=WAL") + self.connection.execute("PRAGMA foreign_keys=ON") + self.setup() + + def close(self) -> None: + self.connection.close() + + def setup(self) -> None: + with self.connection: + self.connection.executescript( + """ + CREATE TABLE IF NOT EXISTS artists ( + artist_id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + account TEXT, + avatar_url TEXT, + metadata_json TEXT NOT NULL DEFAULT '{}', + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS artworks ( + artwork_id INTEGER PRIMARY KEY, + artist_id INTEGER, + type TEXT NOT NULL DEFAULT 'illust', + title TEXT NOT NULL DEFAULT '', + caption TEXT NOT NULL DEFAULT '', + page_count INTEGER NOT NULL DEFAULT 0, + bookmark_count INTEGER NOT NULL DEFAULT 0, + like_count INTEGER NOT NULL DEFAULT 0, + view_count INTEGER NOT NULL DEFAULT 0, + x_restrict INTEGER NOT NULL DEFAULT 0, + ai_type INTEGER NOT NULL DEFAULT 0, + thumbnail_url TEXT, + metadata_json TEXT NOT NULL DEFAULT '{}', + created_at TEXT, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (artist_id) REFERENCES artists(artist_id) + ); + + CREATE TABLE IF NOT EXISTS tags ( + tag_id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL UNIQUE, + translated_name TEXT + ); + + CREATE TABLE IF NOT EXISTS artwork_tags ( + artwork_id INTEGER NOT NULL, + tag_id INTEGER NOT NULL, + PRIMARY KEY (artwork_id, tag_id), + FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE, + FOREIGN KEY (tag_id) REFERENCES tags(tag_id) ON DELETE CASCADE + ); + + CREATE TABLE IF NOT EXISTS artwork_files ( + artwork_id INTEGER NOT NULL, + page_index INTEGER NOT NULL, + file_path TEXT NOT NULL, + source_url TEXT NOT NULL DEFAULT '', + status TEXT NOT NULL DEFAULT 'unknown', + size_bytes INTEGER NOT NULL DEFAULT 0, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (artwork_id, page_index), + FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE + ); + + CREATE TABLE IF NOT EXISTS download_history ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + artwork_id INTEGER NOT NULL, + status TEXT NOT NULL, + downloaded_pages INTEGER NOT NULL DEFAULT 0, + skipped_pages INTEGER NOT NULL DEFAULT 0, + failed_pages INTEGER NOT NULL DEFAULT 0, + message TEXT NOT NULL DEFAULT '', + started_at TEXT, + finished_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE + ); + + CREATE TABLE IF NOT EXISTS sync_state ( + key TEXT PRIMARY KEY, + value_json TEXT NOT NULL DEFAULT '{}', + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS thumbnail_cache ( + artwork_id INTEGER NOT NULL, + page_index INTEGER NOT NULL, + source_path TEXT NOT NULL, + thumbnail_path TEXT NOT NULL, + width INTEGER NOT NULL DEFAULT 0, + height INTEGER NOT NULL DEFAULT 0, + source_mtime REAL NOT NULL DEFAULT 0, + source_size INTEGER NOT NULL DEFAULT 0, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (artwork_id, page_index), + FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE + ); + + CREATE TABLE IF NOT EXISTS artwork_ocr_texts ( + artwork_id INTEGER PRIMARY KEY, + language TEXT NOT NULL DEFAULT 'mixed', + ocr_text TEXT NOT NULL DEFAULT '', + confidence REAL, + engine TEXT NOT NULL DEFAULT 'paddleocr', + status TEXT NOT NULL DEFAULT 'pending', + attempts INTEGER NOT NULL DEFAULT 0, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE + ); + + CREATE TABLE IF NOT EXISTS favorites ( + artwork_id INTEGER PRIMARY KEY, + note TEXT NOT NULL DEFAULT '', + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE + ); + + CREATE TABLE IF NOT EXISTS followed_artists ( + artist_id INTEGER PRIMARY KEY, + name TEXT NOT NULL DEFAULT '', + enabled INTEGER NOT NULL DEFAULT 1, + last_synced_at TEXT, + last_error TEXT, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + + CREATE INDEX IF NOT EXISTS idx_artworks_artist_id ON artworks(artist_id); + CREATE INDEX IF NOT EXISTS idx_artworks_type ON artworks(type); + CREATE INDEX IF NOT EXISTS idx_download_history_artwork_id ON download_history(artwork_id); + CREATE INDEX IF NOT EXISTS idx_artwork_files_path ON artwork_files(file_path); + CREATE INDEX IF NOT EXISTS idx_thumbnail_cache_path ON thumbnail_cache(thumbnail_path); + CREATE INDEX IF NOT EXISTS idx_artwork_ocr_texts_status ON artwork_ocr_texts(status); + CREATE INDEX IF NOT EXISTS idx_artwork_ocr_texts_text ON artwork_ocr_texts(ocr_text); + CREATE INDEX IF NOT EXISTS idx_favorites_created_at ON favorites(created_at); + CREATE INDEX IF NOT EXISTS idx_followed_artists_enabled ON followed_artists(enabled); + + CREATE TABLE IF NOT EXISTS download_tasks ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + fingerprint TEXT NOT NULL UNIQUE, + target_type TEXT NOT NULL, + target_id INTEGER, + page_index INTEGER, + task_kind TEXT NOT NULL, + priority INTEGER NOT NULL DEFAULT 2, + status TEXT NOT NULL DEFAULT 'pending', + payload_json TEXT NOT NULL DEFAULT '{}', + attempts INTEGER NOT NULL DEFAULT 0, + leased_until REAL, + worker_id TEXT, + last_error TEXT NOT NULL DEFAULT '', + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + + CREATE INDEX IF NOT EXISTS idx_download_tasks_status_pri_id + ON download_tasks(status, priority DESC, id ASC); + + CREATE TABLE IF NOT EXISTS artist_sync_watermark ( + artist_id INTEGER PRIMARY KEY, + last_synced_illust_id INTEGER NOT NULL DEFAULT 0, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + """ + ) + self._apply_schema_migrations() + + def _apply_schema_migrations(self) -> None: + """Incremental ALTER for existing databases created before new columns/tables.""" + with self.connection: + cols = {row[1] for row in self.connection.execute("PRAGMA table_info(artwork_files)")} + if "content_hash" not in cols: + self.connection.execute( + "ALTER TABLE artwork_files ADD COLUMN content_hash TEXT NOT NULL DEFAULT ''" + ) + # Drop legacy placeholder names like artist_12345 so UI can fall back cleanly. + self.connection.execute( + "UPDATE artists SET name = '' WHERE name = 'artist_' || artist_id" + ) + self.connection.execute( + "UPDATE followed_artists SET name = '' WHERE name = 'artist_' || artist_id" + ) + self.connection.execute( + "UPDATE artists SET name = '' WHERE lower(name) IN ('none', 'null', 'undefined')" + ) + self.connection.execute( + "UPDATE followed_artists SET name = '' WHERE lower(name) IN ('none', 'null', 'undefined')" + ) + rows = self.connection.execute( + "SELECT artwork_id, metadata_json FROM artworks WHERE artist_id IS NULL OR artist_id = 0" + ).fetchall() + for row in rows: + try: + metadata = json.loads(str(row["metadata_json"] or "{}")) + except Exception: + continue + artist_id = _as_int(metadata.get("userId") or metadata.get("author_id") or metadata.get("artistId")) + if artist_id is None: + continue + self.ensure_artist_stub(artist_id, name=str(metadata.get("userName") or metadata.get("artistName") or "")) + self.connection.execute( + "UPDATE artworks SET artist_id = ?, updated_at = CURRENT_TIMESTAMP WHERE artwork_id = ?", + (artist_id, int(row["artwork_id"])), + ) + + def upsert_artwork(self, detail: dict[str, Any], *, artwork_type: str = "illust") -> None: + source_id = _as_int(detail.get("illustId") or detail.get("id") or detail.get("novelId")) + if source_id is None: + return + source_platform = str(detail.get("source_platform") or detail.get("source") or "") + artwork_id = library_artwork_id(source_id, artwork_type, source_platform=source_platform) + + artist_id = _as_int(detail.get("userId")) + if artist_id is not None: + self._upsert_artist(detail, artist_id) + + tags = _extract_tags(detail) + urls = detail.get("urls") if isinstance(detail.get("urls"), dict) else {} + thumbnail_url = ( + detail.get("url") + or detail.get("coverUrl") + or urls.get("small") + or urls.get("thumb") + or urls.get("regular") + or urls.get("original") + ) + with self.connection: + self.connection.execute( + """ + INSERT INTO artworks ( + artwork_id, artist_id, type, title, caption, page_count, + bookmark_count, like_count, view_count, x_restrict, ai_type, + thumbnail_url, metadata_json, created_at, updated_at + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artwork_id) DO UPDATE SET + artist_id = excluded.artist_id, + type = excluded.type, + title = excluded.title, + caption = excluded.caption, + page_count = excluded.page_count, + bookmark_count = excluded.bookmark_count, + like_count = excluded.like_count, + view_count = excluded.view_count, + x_restrict = excluded.x_restrict, + ai_type = excluded.ai_type, + thumbnail_url = excluded.thumbnail_url, + metadata_json = excluded.metadata_json, + created_at = COALESCE(excluded.created_at, artworks.created_at), + updated_at = CURRENT_TIMESTAMP + """, + ( + artwork_id, + artist_id, + artwork_type, + str(detail.get("title") or ""), + str(detail.get("description") or detail.get("caption") or ""), + _as_int(detail.get("pageCount")) or 0, + _as_int(detail.get("bookmarkCount")) or 0, + _as_int(detail.get("likeCount")) or 0, + _as_int(detail.get("viewCount")) or 0, + _as_int(detail.get("xRestrict")) or 0, + _as_int(detail.get("aiType")) or 0, + str(thumbnail_url) if thumbnail_url else None, + json.dumps({**detail, "sourceId": source_id}, ensure_ascii=False), + str(detail.get("createDate") or detail.get("uploadDate") or "") or None, + ), + ) + self.connection.execute("DELETE FROM artwork_tags WHERE artwork_id = ?", (artwork_id,)) + for name, translated_name in tags: + tag_id = self._upsert_tag(name, translated_name) + self.connection.execute( + "INSERT OR IGNORE INTO artwork_tags (artwork_id, tag_id) VALUES (?, ?)", + (artwork_id, tag_id), + ) + + def ensure_artwork_stub(self, artwork_id: int, *, artwork_type: str = "illust", title: str = "", source_platform: str = "") -> int: + db_artwork_id = library_artwork_id(artwork_id, artwork_type, source_platform=source_platform) + with self.connection: + self.connection.execute( + """ + INSERT OR IGNORE INTO artworks (artwork_id, type, title, updated_at) + VALUES (?, ?, ?, CURRENT_TIMESTAMP) + """, + (db_artwork_id, artwork_type, title), + ) + return db_artwork_id + + def ensure_artist_stub(self, artist_id: int, *, name: str = "") -> None: + with self.connection: + self.connection.execute( + """ + INSERT OR IGNORE INTO artists (artist_id, name, updated_at) + VALUES (?, ?, CURRENT_TIMESTAMP) + """, + (artist_id, name or ""), + ) + + def upsert_artist_profile(self, artist_id: int, *, name: str = "", account: str = "", avatar_url: str = "") -> None: + clean_name = _clean_optional_text(name) + clean_account = _clean_optional_text(account) + clean_avatar = _clean_optional_text(avatar_url) + with self.connection: + self.connection.execute( + """ + INSERT INTO artists (artist_id, name, account, avatar_url, metadata_json, updated_at) + VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artist_id) DO UPDATE SET + name = COALESCE(NULLIF(excluded.name, ''), artists.name), + account = COALESCE(NULLIF(excluded.account, ''), artists.account), + avatar_url = COALESCE(NULLIF(excluded.avatar_url, ''), artists.avatar_url), + metadata_json = CASE + WHEN excluded.metadata_json != '{}' THEN excluded.metadata_json + ELSE artists.metadata_json + END, + updated_at = CURRENT_TIMESTAMP + """, + ( + int(artist_id), + clean_name, + clean_account, + clean_avatar, + json.dumps( + { + "userId": int(artist_id), + "userName": clean_name, + "userAccount": clean_account, + }, + ensure_ascii=False, + ), + ), + ) + + def set_artwork_artist(self, artwork_id: int, artist_id: int) -> None: + with self.connection: + self.ensure_artist_stub(artist_id) + self.connection.execute( + """ + UPDATE artworks + SET artist_id = ?, updated_at = CURRENT_TIMESTAMP + WHERE artwork_id = ? + """, + (artist_id, artwork_id), + ) + + def record_files(self, artwork_id: int, files: list[DownloadFileRecord]) -> None: + with self.connection: + for item in files: + self.connection.execute( + """ + INSERT INTO artwork_files ( + artwork_id, page_index, file_path, source_url, status, size_bytes, + content_hash, updated_at + ) + VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artwork_id, page_index) DO UPDATE SET + file_path = excluded.file_path, + source_url = excluded.source_url, + status = excluded.status, + size_bytes = excluded.size_bytes, + content_hash = CASE + WHEN excluded.content_hash != '' THEN excluded.content_hash + ELSE artwork_files.content_hash + END, + updated_at = CURRENT_TIMESTAMP + """, + ( + artwork_id, + item.page_index, + item.file_path, + item.source_url, + item.status, + item.size_bytes, + item.content_hash or "", + ), + ) + + def record_download_history( + self, + *, + artwork_id: int, + status: str, + downloaded_pages: int, + skipped_pages: int, + failed_pages: int, + message: str, + started_at: str | None = None, + ) -> None: + with self.connection: + self.connection.execute( + """ + INSERT INTO download_history ( + artwork_id, status, downloaded_pages, skipped_pages, failed_pages, message, started_at + ) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, + (artwork_id, status, downloaded_pages, skipped_pages, failed_pages, message, started_at), + ) + + def update_sync_state(self, key: str, value: dict[str, Any]) -> None: + with self.connection: + self.connection.execute( + """ + INSERT INTO sync_state (key, value_json, updated_at) + VALUES (?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(key) DO UPDATE SET + value_json = excluded.value_json, + updated_at = CURRENT_TIMESTAMP + """, + (key, json.dumps(value, ensure_ascii=False)), + ) + + def upsert_thumbnail( + self, + *, + artwork_id: int, + page_index: int, + source_path: str, + thumbnail_path: str, + width: int, + height: int, + source_mtime: float, + source_size: int, + ) -> None: + with self.connection: + self.connection.execute( + """ + INSERT INTO thumbnail_cache ( + artwork_id, page_index, source_path, thumbnail_path, width, height, + source_mtime, source_size, updated_at + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artwork_id, page_index) DO UPDATE SET + source_path = excluded.source_path, + thumbnail_path = excluded.thumbnail_path, + width = excluded.width, + height = excluded.height, + source_mtime = excluded.source_mtime, + source_size = excluded.source_size, + updated_at = CURRENT_TIMESTAMP + """, + (artwork_id, page_index, source_path, thumbnail_path, width, height, source_mtime, source_size), + ) + + def upsert_ocr_text( + self, + artwork_id: int, + *, + text: str, + language: str = "mixed", + confidence: float | None = None, + engine: str = "paddleocr", + status: str = "done", + attempts: int = 1, + ) -> None: + with self.connection: + self.connection.execute( + """ + INSERT INTO artwork_ocr_texts ( + artwork_id, language, ocr_text, confidence, engine, status, attempts, updated_at + ) + VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artwork_id) DO UPDATE SET + language = excluded.language, + ocr_text = excluded.ocr_text, + confidence = excluded.confidence, + engine = excluded.engine, + status = excluded.status, + attempts = excluded.attempts, + updated_at = CURRENT_TIMESTAMP + """, + (int(artwork_id), language, text, confidence, engine, status, attempts), + ) + + def get_ocr_text(self, artwork_id: int) -> sqlite3.Row | None: + return self.connection.execute( + "SELECT * FROM artwork_ocr_texts WHERE artwork_id = ?", + (int(artwork_id),), + ).fetchone() + + def set_favorite(self, artwork_id: int, favorite: bool, note: str = "") -> None: + with self.connection: + if favorite: + self.connection.execute( + """ + INSERT INTO favorites (artwork_id, note, created_at) + VALUES (?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artwork_id) DO UPDATE SET note = excluded.note + """, + (artwork_id, note), + ) + else: + self.connection.execute("DELETE FROM favorites WHERE artwork_id = ?", (artwork_id,)) + + def set_followed_artist(self, artist_id: int, followed: bool, name: str = "") -> None: + with self.connection: + self.ensure_artist_stub(artist_id, name=name) + if followed: + self.connection.execute( + """ + INSERT INTO followed_artists (artist_id, name, enabled, created_at) + VALUES (?, ?, 1, CURRENT_TIMESTAMP) + ON CONFLICT(artist_id) DO UPDATE SET + name = COALESCE(NULLIF(excluded.name, ''), followed_artists.name), + enabled = 1 + """, + (artist_id, name), + ) + else: + self.connection.execute( + "UPDATE followed_artists SET enabled = 0 WHERE artist_id = ?", + (artist_id,), + ) + + def upsert_origin_followed_artist( + self, + artist_id: int, + *, + name: str = "", + account: str = "", + avatar_url: str = "", + ) -> str: + clean_name = _clean_optional_text(name) + before = self.connection.execute( + """ + SELECT fa.enabled, fa.name AS follow_name, ar.name AS artist_name, + ar.account, ar.avatar_url + FROM followed_artists fa + LEFT JOIN artists ar ON ar.artist_id = fa.artist_id + WHERE fa.artist_id = ? + """, + (int(artist_id),), + ).fetchone() + status = "created" + if before is not None: + changed = ( + not bool(before["enabled"]) + or (clean_name and clean_name != str(before["follow_name"] or before["artist_name"] or "")) + or (account and account != str(before["account"] or "")) + or (avatar_url and avatar_url != str(before["avatar_url"] or "")) + ) + status = "updated" if changed else "skipped" + with self.connection: + self.upsert_artist_profile( + int(artist_id), + name=clean_name, + account=account, + avatar_url=avatar_url, + ) + self.connection.execute( + """ + INSERT INTO followed_artists (artist_id, name, enabled, last_synced_at, last_error, created_at) + VALUES (?, ?, 1, CURRENT_TIMESTAMP, '', CURRENT_TIMESTAMP) + ON CONFLICT(artist_id) DO UPDATE SET + name = COALESCE(NULLIF(excluded.name, ''), followed_artists.name), + enabled = 1, + last_synced_at = CURRENT_TIMESTAMP, + last_error = '' + """, + (int(artist_id), clean_name), + ) + return status + + def mark_artist_synced(self, artist_id: int, *, error: str = "") -> None: + with self.connection: + self.connection.execute( + """ + INSERT INTO followed_artists (artist_id, enabled, last_synced_at, last_error) + VALUES (?, 1, CURRENT_TIMESTAMP, ?) + ON CONFLICT(artist_id) DO UPDATE SET + last_synced_at = CURRENT_TIMESTAMP, + last_error = excluded.last_error + """, + (artist_id, error), + ) + + def _upsert_artist(self, detail: dict[str, Any], artist_id: int) -> None: + self.connection.execute( + """ + INSERT INTO artists (artist_id, name, account, avatar_url, metadata_json, updated_at) + VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artist_id) DO UPDATE SET + name = excluded.name, + account = COALESCE(excluded.account, artists.account), + avatar_url = COALESCE(excluded.avatar_url, artists.avatar_url), + metadata_json = excluded.metadata_json, + updated_at = CURRENT_TIMESTAMP + """, + ( + artist_id, + str(detail.get("userName") or detail.get("artistName") or ""), + str(detail.get("userAccount") or "") or None, + str(detail.get("profileImageUrl") or detail.get("userImage") or "") or None, + json.dumps( + { + "userId": artist_id, + "userName": detail.get("userName"), + "userAccount": detail.get("userAccount"), + }, + ensure_ascii=False, + ), + ), + ) + + def _upsert_tag(self, name: str, translated_name: str | None) -> int: + self.connection.execute( + """ + INSERT INTO tags (name, translated_name) + VALUES (?, ?) + ON CONFLICT(name) DO UPDATE SET + translated_name = COALESCE(excluded.translated_name, tags.translated_name) + """, + (name, translated_name), + ) + row = self.connection.execute("SELECT tag_id FROM tags WHERE name = ?", (name,)).fetchone() + return int(row["tag_id"]) + + +def _as_int(value: Any) -> int | None: + if value is None: + return None + try: + return int(value) + except (TypeError, ValueError): + return None + + +def _clean_optional_text(value: Any) -> str: + text = str(value or "").strip() + return "" if text.lower() in {"none", "null", "undefined"} else text + + +def library_artwork_id(source_id: int, artwork_type: str = "illust", source_platform: str = "") -> int: + """Map a source-native ID into the unified library artwork_id namespace. + + Pixiv novels use negative source_id; AO3 works use a negative offset + range to avoid collisions with Pixiv novel IDs. + """ + if artwork_type == "novel": + if source_platform == "ao3": + # AO3: use negative offset range -(200M + work_id) to stay clear + # of Pixiv novel IDs (currently -1 .. -199_999_999). + AO3_OFFSET = 200_000_000 + return -(AO3_OFFSET + abs(int(source_id))) + return -abs(int(source_id)) + return int(source_id) + + +def _extract_tags(detail: dict[str, Any]) -> list[tuple[str, str | None]]: + raw_tags = detail.get("tags") + if isinstance(raw_tags, dict): + raw_tags = raw_tags.get("tags") + if not isinstance(raw_tags, list): + return [] + + result: list[tuple[str, str | None]] = [] + for item in raw_tags: + if isinstance(item, str): + result.append((item, None)) + continue + if not isinstance(item, dict): + continue + name = item.get("tag") or item.get("name") + if not name: + continue + translation = item.get("translation") + translated_name = None + if isinstance(translation, dict): + translated_name = translation.get("en") or translation.get("zh") or translation.get("romaji") + elif isinstance(translation, str): + translated_name = translation + result.append((str(name), str(translated_name) if translated_name else None)) + return result diff --git a/pixiv_app/core/library_importer.py b/pixiv_app/core/library_importer.py new file mode 100644 index 0000000..3e08b69 --- /dev/null +++ b/pixiv_app/core/library_importer.py @@ -0,0 +1,76 @@ +from __future__ import annotations + +import re +from dataclasses import dataclass +from pathlib import Path + +from pixiv_app.core.library import DEFAULT_LIBRARY_DB, DownloadFileRecord, PixivLibrary + + +IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif"} +ILLUST_FILE_PATTERN = re.compile(r"(?P\d+)_p(?P\d+)", re.IGNORECASE) + + +@dataclass +class ImportSummary: + scanned_files: int = 0 + imported_files: int = 0 + skipped_files: int = 0 + imported_artworks: int = 0 + + +class LegacyDownloadImporter: + def __init__(self, *, db_path: str | Path = DEFAULT_LIBRARY_DB) -> None: + self.db_path = Path(db_path) + + def scan(self, root: str | Path = "Sakura_Downloads") -> ImportSummary: + root_path = Path(root) + summary = ImportSummary() + if not root_path.exists(): + return summary + + library = PixivLibrary(self.db_path) + imported_artworks: set[int] = set() + try: + for file_path in root_path.rglob("*"): + if not file_path.is_file() or file_path.suffix.lower() not in IMAGE_SUFFIXES: + continue + summary.scanned_files += 1 + match = ILLUST_FILE_PATTERN.search(file_path.stem) + if match is None: + summary.skipped_files += 1 + continue + artwork_id = int(match.group("artwork_id")) + page_index = int(match.group("page_index")) + library.ensure_artwork_stub(artwork_id, artwork_type="illust", title=str(artwork_id)) + artist_id = self._infer_artist_id(root_path, file_path) + if artist_id is not None: + library.set_artwork_artist(artwork_id, artist_id) + library.record_files( + artwork_id, + [ + DownloadFileRecord( + page_index=page_index, + file_path=str(file_path.resolve()), + source_url="", + status="imported", + size_bytes=file_path.stat().st_size, + ) + ], + ) + imported_artworks.add(artwork_id) + summary.imported_files += 1 + summary.imported_artworks = len(imported_artworks) + return summary + finally: + library.close() + + def _infer_artist_id(self, root_path: Path, file_path: Path) -> int | None: + try: + relative = file_path.relative_to(root_path) + except ValueError: + return None + if not relative.parts: + return None + first = relative.parts[0] + return int(first) if first.isdigit() else None diff --git a/pixiv_app/core/ocr.py b/pixiv_app/core/ocr.py new file mode 100644 index 0000000..dc5851b --- /dev/null +++ b/pixiv_app/core/ocr.py @@ -0,0 +1,128 @@ +from __future__ import annotations + +import logging +import os +import threading +from dataclasses import dataclass +from pathlib import Path +from typing import Callable + +from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary + + +OcrExtractor = Callable[[Path], tuple[str, float | None, str]] + + +@dataclass +class LocalOcrResult: + artwork_id: int + status: str + text_length: int = 0 + confidence: float | None = None + error: str | None = None + + +class LocalOcrIndexer: + def __init__( + self, + *, + db_path: str | Path = DEFAULT_LIBRARY_DB, + enabled: bool | None = None, + max_dimension: int | None = None, + language: str | None = None, + max_concurrency: int | None = None, + ) -> None: + self.db_path = Path(db_path) + self.enabled = enabled if enabled is not None else _env_bool("SAKURA_LOCAL_OCR_ENABLED", False) + self.max_dimension = max_dimension or int(os.getenv("SAKURA_LOCAL_OCR_MAX_DIMENSION", "1800")) + self.language = language or os.getenv("SAKURA_LOCAL_OCR_LANGUAGE", "ch") + self._semaphore = threading.BoundedSemaphore( + max_concurrency or int(os.getenv("SAKURA_LOCAL_OCR_MAX_CONCURRENCY", "1")) + ) + + def index_artwork(self, artwork_id: int, *, extractor: OcrExtractor | None = None) -> LocalOcrResult: + if not self.enabled and extractor is None: + return LocalOcrResult(artwork_id=artwork_id, status="disabled") + if not self._semaphore.acquire(blocking=False): + return LocalOcrResult(artwork_id=artwork_id, status="busy") + library = PixivLibrary(self.db_path) + try: + existing = library.get_ocr_text(artwork_id) + if existing is not None and str(existing["status"]) == "done" and str(existing["ocr_text"]).strip(): + return LocalOcrResult(artwork_id=artwork_id, status="done", text_length=len(str(existing["ocr_text"]))) + + row = library.connection.execute( + """ + SELECT file_path + FROM artwork_files + WHERE artwork_id = ? + ORDER BY page_index ASC + LIMIT 1 + """, + (int(artwork_id),), + ).fetchone() + if row is None: + return LocalOcrResult(artwork_id=artwork_id, status="failed", error="file_not_found") + path = Path(str(row["file_path"])) + if path.suffix.lower() not in {".jpg", ".jpeg", ".png", ".webp"}: + library.upsert_ocr_text(artwork_id, text="", status="failed", attempts=1) + return LocalOcrResult(artwork_id=artwork_id, status="failed", error="unsupported_file_type") + + text, confidence, language = (extractor or self._paddle_ocr_extract)(path) + library.upsert_ocr_text( + artwork_id, + text=text.strip(), + confidence=confidence, + language=language or self.language, + status="done", + attempts=1, + ) + logging.info("Local OCR done artwork=%s text_len=%s confidence=%s", artwork_id, len(text.strip()), confidence) + return LocalOcrResult(artwork_id=artwork_id, status="done", text_length=len(text.strip()), confidence=confidence) + except Exception as exc: + library.upsert_ocr_text(artwork_id, text="", status="failed", attempts=1) + logging.warning("Local OCR failed artwork=%s error=%s", artwork_id, exc.__class__.__name__) + return LocalOcrResult(artwork_id=artwork_id, status="failed", error=exc.__class__.__name__) + finally: + library.close() + self._semaphore.release() + + def _paddle_ocr_extract(self, path: Path) -> tuple[str, float | None, str]: + from PIL import Image + from paddleocr import PaddleOCR + + work_path = path + with Image.open(path) as image: + original_size = image.size + image.thumbnail((self.max_dimension, self.max_dimension)) + if image.size != original_size: + tmp = path.with_suffix(".local-ocr-resized.jpg") + image.convert("RGB").save(tmp, quality=90) + work_path = tmp + try: + ocr = PaddleOCR(use_angle_cls=True, lang=self.language, use_gpu=False) + result = ocr.ocr(str(work_path), cls=True) + texts: list[str] = [] + scores: list[float] = [] + for page in result or []: + for line in page or []: + if len(line) >= 2 and isinstance(line[1], (list, tuple)): + text = str(line[1][0]).strip() + if text: + texts.append(text) + try: + scores.append(float(line[1][1])) + except (TypeError, ValueError, IndexError): + pass + confidence = sum(scores) / len(scores) if scores else None + return "\n".join(texts), confidence, self.language + finally: + if work_path != path: + work_path.unlink(missing_ok=True) + + +def _env_bool(name: str, default: bool) -> bool: + value = os.getenv(name) + if value is None: + return default + return value.strip().lower() in {"1", "true", "yes", "on"} diff --git a/pixiv_app/core/orchestrator.py b/pixiv_app/core/orchestrator.py new file mode 100644 index 0000000..be74bc5 --- /dev/null +++ b/pixiv_app/core/orchestrator.py @@ -0,0 +1,50 @@ +""" +Bridge URL-level plugin parsing with the rest of the app (tasks/GUI logging). + +Full TaskQueue integration can map Resource → DownloadTaskSpec per site. +""" + +from __future__ import annotations + +from dataclasses import dataclass +import logging +from pathlib import Path + +from pixiv_app.core.plugin.base import BasePlugin, PluginParseError, Resource +from pixiv_app.core.plugin.manager import PluginManager + +logger = logging.getLogger(__name__) + + +@dataclass +class OrchestrationResult: + plugin_name: str + resources: list[Resource] + + +class PluginTaskOrchestrator: + """Resolve plugins and run parse/download without replacing the legacy Pixiv GUI flow.""" + + def __init__(self, plugin_manager: PluginManager) -> None: + self.plugin_manager = plugin_manager + + def get_plugin_for_url(self, url: str) -> BasePlugin | None: + return self.plugin_manager.get_plugin_for_url(url) + + def parse_url(self, url: str) -> OrchestrationResult: + plugin = self.get_plugin_for_url(url) + if plugin is None: + raise PluginParseError(f"No plugin registered for: {url}") + resources = plugin.parse(url) + return OrchestrationResult(plugin_name=plugin.name, resources=resources) + + def download_resource( + self, + plugin_name: str, + resource: Resource, + save_path: Path, + ) -> list[Path]: + plugin = self.plugin_manager.plugins.get(plugin_name) + if plugin is None: + raise PluginParseError(f"Plugin not loaded: {plugin_name}") + return plugin.download(resource, save_path) diff --git a/pixiv_app/core/origin_follow_sync.py b/pixiv_app/core/origin_follow_sync.py new file mode 100644 index 0000000..9f70a15 --- /dev/null +++ b/pixiv_app/core/origin_follow_sync.py @@ -0,0 +1,423 @@ +from __future__ import annotations + +import asyncio +import re +from dataclasses import dataclass, field +from typing import Any, Callable + +from pixiv_app.core.auth import SessionStore +from pixiv_app.core.downloader import create_session, request_json +from pixiv_app.core.gallery_service import LocalGalleryService +from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary +from pixiv_app.core.paths import app_session_file +from pixiv_app.core.x_media_service import XMediaService, _normalize_username + + +ProgressCallback = Callable[[dict[str, Any]], None] + + +@dataclass +class OriginFollow: + platform: str + external_id: str + name: str = "" + handle: str = "" + avatar_url: str = "" + profile_url: str = "" + + +@dataclass +class OriginFollowSyncResult: + platform: str + created: int = 0 + updated: int = 0 + skipped: int = 0 + failed: int = 0 + cloud_created: int = 0 + cloud_updated: int = 0 + cloud_skipped: int = 0 + pages: int = 0 + errors: list[dict[str, str]] = field(default_factory=list) + progress: list[dict[str, Any]] = field(default_factory=list) + + def to_dict(self) -> dict[str, Any]: + return { + "platform": self.platform, + "created": self.created, + "updated": self.updated, + "skipped": self.skipped, + "failed": self.failed, + "cloud_created": self.cloud_created, + "cloud_updated": self.cloud_updated, + "cloud_skipped": self.cloud_skipped, + "pages": self.pages, + "errors": self.errors, + "progress": self.progress, + } + + +class OriginFollowSyncService: + def __init__( + self, + *, + db_path: str = DEFAULT_LIBRARY_DB, + gallery_service: LocalGalleryService | None = None, + x_media_service: XMediaService | None = None, + ) -> None: + self.db_path = db_path + self.gallery_service = gallery_service or LocalGalleryService(db_path=db_path) + self.x_media_service = x_media_service or XMediaService() + + def sync(self, platform: str, *, progress: ProgressCallback | None = None) -> dict[str, Any]: + normalized = platform.strip().lower() + if normalized == "pixiv": + return self.sync_pixiv(progress=progress).to_dict() + if normalized == "x": + return self.sync_x(progress=progress).to_dict() + raise ValueError("请选择 Pixiv 或 X。") + + def sync_pixiv(self, *, progress: ProgressCallback | None = None) -> OriginFollowSyncResult: + cookie = str(SessionStore(app_session_file()).load().get("cookie", "")).strip() + if not cookie: + raise ValueError("Pixiv Cookie 不存在,请先导入或保存 Pixiv 登录态。") + + result = OriginFollowSyncResult(platform="pixiv") + session = create_session(cookie) + try: + user_id = self._fetch_pixiv_current_user_id(session, cookie) + follows = self._fetch_pixiv_following(session, cookie, user_id, result, progress=progress) + finally: + session.close() + self._upsert_pixiv_local(follows, result) + return result + + def sync_x(self, *, progress: ProgressCallback | None = None) -> OriginFollowSyncResult: + plugin = self.x_media_service._get_plugin() + if not plugin.validate(): + raise ValueError("X Playwright 不可用,请先安装 Playwright 并完成 X 登录。") + + result = OriginFollowSyncResult(platform="x") + with plugin._playwright_lock: + follows = asyncio.run(self._fetch_x_following(plugin, result, progress=progress)) + self._upsert_x_local(follows, result) + return result + + def _emit( + self, + result: OriginFollowSyncResult, + progress: ProgressCallback | None, + payload: dict[str, Any], + ) -> None: + result.progress.append(payload) + if progress is not None: + progress(payload) + + def _fetch_pixiv_current_user_id(self, session: Any, cookie: str) -> str: + for url in ( + "https://www.pixiv.net/ajax/user/self?lang=zh", + "https://www.pixiv.net/ajax/user/self", + ): + try: + payload = request_json(session, url, cookie=cookie, context="Pixiv 当前账号") + body = payload.get("body") if isinstance(payload, dict) else {} + user_id = _first_text(body, "userId", "id") + if user_id: + return user_id + except Exception: + continue + try: + response = session.get("https://www.pixiv.net/", timeout=30) + response.raise_for_status() + match = re.search(r'"userData"\s*:\s*\{[^}]*"id"\s*:\s*"?(?P\d+)"?', response.text) + if match: + return match.group("id") + except Exception: + pass + raise ValueError("Pixiv Cookie 可能已失效:无法识别当前登录账号。") + + def _fetch_pixiv_following( + self, + session: Any, + cookie: str, + user_id: str, + result: OriginFollowSyncResult, + *, + progress: ProgressCallback | None = None, + ) -> list[OriginFollow]: + follows: list[OriginFollow] = [] + seen: set[str] = set() + limit = 100 + offset = 0 + page = 1 + while True: + self._emit(result, progress, {"platform": "pixiv", "page": page, "message": f"正在同步第 {page} 页"}) + url = ( + f"https://www.pixiv.net/ajax/user/{user_id}/following" + f"?offset={offset}&limit={limit}&rest=show&tag=&acceptingRequests=0&lang=zh" + ) + try: + payload = request_json( + session, + url, + cookie=cookie, + referer=f"https://www.pixiv.net/users/{user_id}/following", + context=f"Pixiv 关注列表第 {page} 页", + ) + except Exception as exc: + if page == 1: + raise ValueError("Pixiv Cookie 可能已失效:无法读取关注列表。") from exc + result.failed += 1 + result.errors.append({"id": f"page:{page}", "reason": str(exc)}) + break + users = _extract_pixiv_users(payload) + result.pages = page + if not users: + break + for item in users: + follow = _pixiv_follow_from_payload(item) + if follow is None: + result.failed += 1 + result.errors.append({"id": "unknown", "reason": "Pixiv 作者记录缺少 userId"}) + continue + if follow.external_id in seen: + result.skipped += 1 + continue + seen.add(follow.external_id) + follows.append(follow) + if len(users) < limit: + break + offset += limit + page += 1 + return follows + + async def _fetch_x_following( + self, + plugin: Any, + result: OriginFollowSyncResult, + *, + progress: ProgressCallback | None = None, + ) -> list[OriginFollow]: + from plugins.x import selectors + from plugins.x.playwright_client import XPlaywrightClient + + client = XPlaywrightClient(plugin.config) + follows: list[OriginFollow] = [] + seen: set[str] = set() + try: + await client.start() + page = await client.new_page_goto("https://x.com/home") + try: + await client.raise_if_guest_wall(page) + profile_href = await page.locator(selectors.PROFILE_LINK).first.get_attribute("href", timeout=8000) + username = _x_username_from_href(profile_href or "") + if not username: + raise ValueError("X 未登录:请先在插件管理里完成 X 登录。") + await page.goto(f"https://x.com/{username}{selectors.FOLLOWING_URL_SUFFIX}", wait_until="domcontentloaded") + await client.raise_if_guest_wall(page) + stale_rounds = 0 + page_no = 1 + while stale_rounds < 4 and page_no <= 80: + self._emit(result, progress, {"platform": "x", "page": page_no, "message": f"正在同步第 {page_no} 页"}) + added = await _collect_x_user_cells(page, seen, follows) + result.pages = page_no + stale_rounds = stale_rounds + 1 if added == 0 else 0 + await client.human_scroll_page(page) + page_no += 1 + if not follows: + raise ValueError("X 未读取到 following 列表,请确认账号已登录且 following 页面可访问。") + finally: + await page.close() + finally: + await client.close() + return follows + + def _upsert_pixiv_local(self, follows: list[OriginFollow], result: OriginFollowSyncResult) -> None: + library = PixivLibrary(self.db_path) + try: + for follow in follows: + try: + status = library.upsert_origin_followed_artist( + int(follow.external_id), + name=follow.name, + account=follow.handle, + avatar_url=follow.avatar_url, + ) + _apply_local_status(result, status) + except Exception as exc: + result.failed += 1 + result.errors.append({"id": follow.external_id, "reason": str(exc)}) + finally: + library.close() + + def _upsert_x_local(self, follows: list[OriginFollow], result: OriginFollowSyncResult) -> None: + rows = self.x_media_service._load_follow_rows() + now = _now_text() + by_handle = {str(item.get("username", "")).lower(): item for item in rows} + for follow in follows: + handle = _normalize_username(follow.handle or follow.external_id) + if not handle: + result.failed += 1 + result.errors.append({"id": follow.external_id, "reason": "X 用户名为空"}) + continue + row = by_handle.get(handle.lower()) + status = "created" + if row is None: + row = { + "username": handle, + "creator_name": follow.name or handle, + "enabled": True, + "profile_url": follow.profile_url or f"https://x.com/{handle}", + "avatar_url": follow.avatar_url, + "latest_tweet_id": "", + "latest_published_at": "", + "last_checked_at": now, + "last_error": "", + "seen_tweet_ids": [], + "created_at": now, + "updated_at": now, + } + rows.append(row) + by_handle[handle.lower()] = row + else: + changed = ( + not bool(row.get("enabled", True)) + or (follow.name and follow.name != str(row.get("creator_name") or "")) + or (follow.avatar_url and follow.avatar_url != str(row.get("avatar_url") or "")) + ) + status = "updated" if changed else "skipped" + row["username"] = handle + row["creator_name"] = follow.name or str(row.get("creator_name") or handle) + row["enabled"] = True + row["profile_url"] = follow.profile_url or f"https://x.com/{handle}" + row["avatar_url"] = follow.avatar_url or str(row.get("avatar_url") or "") + row["last_checked_at"] = now + row["last_error"] = "" + row["updated_at"] = now + _apply_local_status(result, status) + self.x_media_service._save_follow_rows(rows) + +def _apply_local_status(result: OriginFollowSyncResult, status: str) -> None: + if status == "created": + result.created += 1 + elif status == "updated": + result.updated += 1 + else: + result.skipped += 1 + + +def _extract_pixiv_users(payload: dict[str, Any]) -> list[dict[str, Any]]: + body = payload.get("body") if isinstance(payload, dict) else payload + if isinstance(body, list): + return [item for item in body if isinstance(item, dict)] + if not isinstance(body, dict): + return [] + candidates = ( + body.get("users"), + body.get("followUsers"), + body.get("userPreviews"), + body.get("thumbnails", {}).get("user") if isinstance(body.get("thumbnails"), dict) else None, + ) + for candidate in candidates: + if isinstance(candidate, list): + return [item for item in candidate if isinstance(item, dict)] + return [] + + +def _pixiv_follow_from_payload(item: dict[str, Any]) -> OriginFollow | None: + user_id = _first_text(item, "userId", "user_id", "id") + if not user_id: + user = item.get("user") if isinstance(item.get("user"), dict) else {} + user_id = _first_text(user, "userId", "user_id", "id") + item = {**item, **user} + if not user_id: + return None + name = _first_text(item, "name", "userName", "displayName") + handle = _first_text(item, "userAccount", "account", "screenName") + avatar = _first_text(item, "imageBig", "image", "profileImageUrl", "profile_image_url") + return OriginFollow( + platform="pixiv", + external_id=str(user_id), + name=name, + handle=handle, + avatar_url=avatar, + profile_url=f"https://www.pixiv.net/users/{user_id}", + ) + + +async def _collect_x_user_cells(page: Any, seen: set[str], follows: list[OriginFollow]) -> int: + from plugins.x import selectors + + added = 0 + cells = await page.locator(selectors.USER_CELL).all() + for cell in cells: + try: + data = await cell.evaluate( + """(node) => { + const text = node.innerText || ""; + const links = Array.from(node.querySelectorAll('a[href^="/"]')).map(a => a.getAttribute("href") || ""); + const img = node.querySelector('img[src*="profile_images"]'); + return { text, links, avatar: img ? img.getAttribute("src") : "" }; + }""" + ) + handle = _x_handle_from_cell(data) + if not handle or handle.lower() in seen: + continue + seen.add(handle.lower()) + follows.append( + OriginFollow( + platform="x", + external_id=handle, + name=_x_display_name_from_cell(data, handle), + handle=handle, + avatar_url=str(data.get("avatar") or ""), + profile_url=f"https://x.com/{handle}", + ) + ) + added += 1 + except Exception: + continue + return added + + +def _x_handle_from_cell(data: dict[str, Any]) -> str: + for href in data.get("links") or []: + username = _x_username_from_href(str(href)) + if username: + return username + text = str(data.get("text") or "") + match = re.search(r"@([A-Za-z0-9_]{1,15})", text) + return match.group(1) if match else "" + + +def _x_display_name_from_cell(data: dict[str, Any], handle: str) -> str: + text = str(data.get("text") or "") + for line in [item.strip() for item in text.splitlines() if item.strip()]: + if not line.startswith("@") and line.lower() not in {"follow", "following"}: + return line + return handle + + +def _x_username_from_href(href: str) -> str: + match = re.match(r"^/?([A-Za-z0-9_]{1,15})(?:/)?$", href.strip()) + if not match: + return "" + username = match.group(1) + if username.lower() in {"home", "explore", "notifications", "messages", "settings", "i"}: + return "" + return username + + +def _first_text(payload: Any, *keys: str) -> str: + if not isinstance(payload, dict): + return "" + for key in keys: + value = payload.get(key) + if value is not None and str(value).strip(): + return str(value).strip() + return "" + + +def _now_text() -> str: + from datetime import datetime, timezone + + return datetime.now(timezone.utc).isoformat() diff --git a/pixiv_app/core/paths.py b/pixiv_app/core/paths.py new file mode 100644 index 0000000..69a3cd1 --- /dev/null +++ b/pixiv_app/core/paths.py @@ -0,0 +1,113 @@ +from __future__ import annotations + +import os +import shutil +import sys +from pathlib import Path + + +APP_NAME = "SakuraDownloader" +LEGACY_APP_NAME = "PixivDownloader" + + +def is_frozen() -> bool: + return bool(getattr(sys, "frozen", False)) + + +def resource_root() -> Path: + if is_frozen(): + return Path(getattr(sys, "_MEIPASS")).resolve() + return Path(__file__).resolve().parents[2] + + +def executable_dir() -> Path: + if is_frozen(): + return Path(sys.executable).resolve().parent + return resource_root() + + +def data_root() -> Path: + override = os.environ.get("SAKURA_APP_DATA_DIR") or os.environ.get("PIXIV_APP_DATA_DIR") + if override: + root = Path(override) + elif is_frozen(): + local_appdata = os.environ.get("LOCALAPPDATA") + root = Path(local_appdata) / APP_NAME if local_appdata else executable_dir() / "data" + legacy_root = Path(local_appdata) / LEGACY_APP_NAME if local_appdata else None + if legacy_root and legacy_root.exists() and not root.exists(): + root.mkdir(parents=True, exist_ok=True) + for name in ("pixiv_app_session.json", "runtime", "profiles", "plugins"): + src = legacy_root / name + dst = root / name + if src.exists() and not dst.exists(): + if src.is_dir(): + import shutil + + shutil.copytree(src, dst) + else: + dst.write_bytes(src.read_bytes()) + else: + root = resource_root() + root.mkdir(parents=True, exist_ok=True) + return root + + +def runtime_path(*parts: str) -> Path: + path = data_root() / "runtime" + path.mkdir(parents=True, exist_ok=True) + return path.joinpath(*parts) + + +def profiles_root() -> Path: + path = data_root() / "profiles" + path.mkdir(parents=True, exist_ok=True) + return path + + +def plugins_root() -> Path: + return resource_root() / "plugins" + + +def external_plugins_root() -> Path: + """Plugins shipped beside the executable, outside the PyInstaller bundle.""" + return executable_dir() / "plugins" + + +def user_plugins_root() -> Path: + path = data_root() / "plugins" + path.mkdir(parents=True, exist_ok=True) + return path + + +def plugin_roots() -> list[Path]: + roots = [plugins_root(), external_plugins_root(), user_plugins_root()] + result: list[Path] = [] + for root in roots: + if root not in result: + result.append(root) + return result + + +def webui_root() -> Path: + return resource_root() / "pixiv_app" / "webui" + + +def app_session_file() -> Path: + return data_root() / "pixiv_app_session.json" + + +def app_library_file() -> Path: + """Persistent local library DB, writable even when the EXE directory is read-only.""" + target = data_root() / "pixiv_app_library.db" + if is_frozen() and not target.exists(): + legacy = executable_dir() / "pixiv_app_library.db" + if legacy.is_file(): + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(legacy, target) + return target + + +def downloads_root() -> Path: + path = executable_dir() / "Sakura_Downloads" + path.mkdir(parents=True, exist_ok=True) + return path diff --git a/pixiv_app/core/playwright_runtime.py b/pixiv_app/core/playwright_runtime.py new file mode 100644 index 0000000..51c5bf8 --- /dev/null +++ b/pixiv_app/core/playwright_runtime.py @@ -0,0 +1,69 @@ +from __future__ import annotations + +import os +from pathlib import Path + + +def playwright_browsers_root() -> Path | None: + local_appdata = os.environ.get("LOCALAPPDATA", "").strip() + if not local_appdata: + return None + return Path(local_appdata) / "ms-playwright" + + +def configure_playwright_runtime_env() -> Path | None: + root = playwright_browsers_root() + if root is None: + return None + if root.exists() and "PLAYWRIGHT_BROWSERS_PATH" not in os.environ: + os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(root) + return root + + +def preferred_chromium_executable() -> Path | None: + root = configure_playwright_runtime_env() + if root is None or not root.exists(): + return None + + candidates: list[tuple[int, float, Path]] = [] + for browser_dir in root.glob("chromium-*"): + if not browser_dir.is_dir(): + continue + revision = _parse_revision(browser_dir.name) + for relative in ( + Path("chrome-win64") / "chrome.exe", + Path("chrome-win") / "chrome.exe", + ): + executable = browser_dir / relative + if executable.is_file(): + try: + mtime = executable.stat().st_mtime + except OSError: + mtime = 0.0 + candidates.append((revision, mtime, executable)) + break + + if not candidates: + return None + + candidates.sort(key=lambda item: (item[0], item[1]), reverse=True) + return candidates[0][2] + + +def explain_missing_browser(exc: Exception, *, feature_name: str) -> str: + text = str(exc) + lowered = text.lower() + if "executable doesn't exist" not in lowered and "browsertype.launch_persistent_context" not in lowered: + return text + + return ( + f"{feature_name} 需要 Playwright Chromium 浏览器,但当前系统里没有可用的浏览器文件。\n" + "请先运行同目录下的 install_playwright_chromium.bat,安装完成后重新打开程序再试。" + ) + + +def _parse_revision(name: str) -> int: + try: + return int(name.rsplit("-", 1)[-1]) + except (TypeError, ValueError): + return 0 diff --git a/pixiv_app/core/plugin/__init__.py b/pixiv_app/core/plugin/__init__.py new file mode 100644 index 0000000..467c28d --- /dev/null +++ b/pixiv_app/core/plugin/__init__.py @@ -0,0 +1,24 @@ +"""Plugin system: base types, manager, generator, optional market client.""" + +from pixiv_app.core.plugin.base import ( + BasePlugin, + PluginAuthError, + PluginDownloadError, + PluginError, + PluginManifest, + PluginParseError, + Resource, +) +from pixiv_app.core.plugin.manager import PluginEvent, PluginManager + +__all__ = [ + "BasePlugin", + "PluginAuthError", + "PluginDownloadError", + "PluginError", + "PluginEvent", + "PluginManager", + "PluginManifest", + "PluginParseError", + "Resource", +] diff --git a/pixiv_app/core/plugin/base.py b/pixiv_app/core/plugin/base.py new file mode 100644 index 0000000..2bf4687 --- /dev/null +++ b/pixiv_app/core/plugin/base.py @@ -0,0 +1,119 @@ +""" +Multi-site plugin core abstractions. + +解析与下载采用同步接口,便于与现有 requests / 线程模型集成; +若站点 SDK 仅提供 asyncio,可在插件内部使用 asyncio.run 包装。 +""" + +from __future__ import annotations + +from abc import ABC, abstractmethod +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + + +@dataclass +class Resource: + """Unified resource model returned by plugins.""" + + id: str + url: str + title: str + author: str + author_id: str + files: list[dict[str, Any]] + metadata: dict[str, Any] = field(default_factory=dict) + thumbnail: str | None = None + created_at: str | None = None + + +@dataclass +class PluginManifest: + """Optional manifest shipped as manifest.json next to plugin.py.""" + + name: str + version: str + domain: str + author: str = "" + description: str = "" + requires_auth: bool = False + min_platform_version: str = "1.0.0" + tags: list[str] = field(default_factory=list) + dependencies: list[str] = field(default_factory=list) + + +class PluginError(Exception): + """Base class for plugin failures.""" + + +class PluginAuthError(PluginError): + """Authentication or authorization failed.""" + + +class PluginParseError(PluginError): + """URL or API response could not be parsed.""" + + +class PluginDownloadError(PluginError): + """Download or filesystem error.""" + + +class BasePlugin(ABC): + """Interface implemented by every site plugin.""" + + @property + @abstractmethod + def domain(self) -> str: + """Primary domain, e.g. pixiv.net.""" + + @property + @abstractmethod + def name(self) -> str: + """Human-readable plugin name.""" + + @property + @abstractmethod + def version(self) -> str: + """Semantic version string.""" + + @abstractmethod + def can_handle(self, url: str) -> bool: + """Return True if this plugin should process the URL or raw token.""" + + @abstractmethod + def parse(self, url: str) -> list[Resource]: + """Resolve URL into one or more downloadable resources.""" + + @abstractmethod + def download(self, resource: Resource, save_path: Path) -> list[Path]: + """Download files for ``resource`` under ``save_path``.""" + + @abstractmethod + def get_headers(self) -> dict[str, str]: + """HTTP headers for requests (Cookie, Bearer token, etc.).""" + + def validate(self) -> bool: + """Optional self-check (credentials present, deps installed).""" + return True + + def get_config_schema(self) -> dict[str, Any]: + """Optional JSON-schema-like dict for dynamic GUI forms.""" + return {} + + def load_config(self) -> dict[str, Any]: + """Override to load persisted plugin settings.""" + return {} + + def save_config(self, data: dict[str, Any]) -> None: + """Override to persist plugin settings.""" + +__all__ = [ + "BasePlugin", + "PluginAuthError", + "PluginDownloadError", + "PluginError", + "PluginManifest", + "PluginParseError", + "Resource", +] diff --git a/pixiv_app/core/plugin/generator.py b/pixiv_app/core/plugin/generator.py new file mode 100644 index 0000000..08e29a3 --- /dev/null +++ b/pixiv_app/core/plugin/generator.py @@ -0,0 +1,69 @@ +from __future__ import annotations + +import re +from datetime import datetime +from pathlib import Path +from typing import Any + +from jinja2 import Environment, FileSystemLoader, select_autoescape + + +def _class_name_from_plugin_name(name: str) -> str: + parts = re.split(r"[\s_]+", name.strip()) + stem = "".join(p[:1].upper() + p[1:].lower() if p else "" for p in parts if p) + return f"{stem}Plugin" if stem else "GeneratedPlugin" + + +class PluginGenerator: + """Generate a skeleton plugin directory from Jinja2 templates.""" + + def __init__(self, template_dir: str | Path | None = None) -> None: + base = Path(__file__).resolve().parent / "templates" + self.template_dir = Path(template_dir) if template_dir else base + self.env = Environment( + loader=FileSystemLoader(str(self.template_dir)), + autoescape=select_autoescape(enabled_extensions=()), + ) + + def generate( + self, + name: str, + domain: str, + author: str, + description: str, + *, + output_root: str | Path = "plugins", + ) -> Path: + slug = name.strip().lower().replace(" ", "_") + plugin_dir = Path(output_root) / slug + plugin_dir.mkdir(parents=True, exist_ok=True) + + context: dict[str, Any] = { + "name": name.strip(), + "domain": domain.strip(), + "author": author.strip(), + "description": description.strip(), + "generated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + "class_name": _class_name_from_plugin_name(name), + } + + py_template = self.env.get_template("base_plugin.py.jinja") + (plugin_dir / "plugin.py").write_text(py_template.render(**context), encoding="utf-8") + + manifest_template = self.env.get_template("manifest.json.jinja") + (plugin_dir / "manifest.json").write_text(manifest_template.render(**context), encoding="utf-8") + + init_file = plugin_dir / "__init__.py" + if not init_file.exists(): + init_file.write_text('"""Generated plugin."""\n', encoding="utf-8") + + return plugin_dir.resolve() + + def generate_from_gui(self, form_data: dict[str, str], *, output_root: str | Path = "plugins") -> Path: + return self.generate( + name=form_data["name"], + domain=form_data["domain"], + author=form_data["author"], + description=form_data["description"], + output_root=output_root, + ) diff --git a/pixiv_app/core/plugin/manager.py b/pixiv_app/core/plugin/manager.py new file mode 100644 index 0000000..acdfe95 --- /dev/null +++ b/pixiv_app/core/plugin/manager.py @@ -0,0 +1,194 @@ +from __future__ import annotations + +import importlib.util +import json +import logging +import sys +from collections import defaultdict +from enum import Enum +from pathlib import Path +from typing import Any, Callable + +from pixiv_app.core.plugin.base import BasePlugin, PluginManifest + +logger = logging.getLogger(__name__) + + +class PluginEvent(str, Enum): + BEFORE_LOAD = "before_load" + AFTER_LOAD = "after_load" + BEFORE_UNLOAD = "before_unload" + AFTER_UNLOAD = "after_unload" + ERROR = "error" + + +class PluginManager: + """Discover and load plugins from filesystem directories.""" + + def __init__(self, plugin_dirs: list[str | Path]) -> None: + self.plugin_dirs = [Path(d).resolve() for d in plugin_dirs] + self.plugins: dict[str, BasePlugin] = {} + self.manifests: dict[str, PluginManifest] = {} + self.loaded_modules: dict[str, Any] = {} + self._paths: dict[str, Path] = {} + self.load_errors: dict[Path, str] = {} + self.event_handlers: dict[PluginEvent, list[Callable[..., None]]] = defaultdict(list) + + def on_event(self, event: PluginEvent, callback: Callable[..., None]) -> None: + self.event_handlers[event].append(callback) + + def _emit(self, event: PluginEvent, *args: Any, **kwargs: Any) -> None: + for cb in self.event_handlers.get(event, []): + try: + cb(*args, **kwargs) + except Exception as exc: + logger.exception("Plugin event handler failed: %s", exc) + + def discover(self) -> list[Path]: + """Return paths to plugin.py files.""" + found: list[Path] = [] + for root in self.plugin_dirs: + if not root.is_dir(): + continue + for subdir in sorted(root.iterdir()): + if not subdir.is_dir(): + continue + candidate = subdir / "plugin.py" + if candidate.is_file(): + found.append(candidate) + return found + + def _load_manifest(self, plugin_dir: Path) -> PluginManifest | None: + manifest_path = plugin_dir / "manifest.json" + if not manifest_path.is_file(): + return None + try: + data = json.loads(manifest_path.read_text(encoding="utf-8")) + return PluginManifest( + name=str(data.get("name", plugin_dir.name)), + version=str(data.get("version", "0.0.0")), + domain=str(data.get("domain", "")), + author=str(data.get("author", "")), + description=str(data.get("description", "")), + requires_auth=bool(data.get("requires_auth", False)), + min_platform_version=str(data.get("min_platform_version", "1.0.0")), + tags=list(data.get("tags") or []), + dependencies=list(data.get("dependencies") or []), + ) + except Exception as exc: + logger.warning("Invalid manifest %s: %s", manifest_path, exc) + return None + + def load_plugin(self, plugin_path: Path) -> BasePlugin | None: + """Load a single plugin from ``.../plugin.py``.""" + plugin_path = plugin_path.resolve() + plugin_dir = plugin_path.parent + module_name = f"sakura_dynamic_plugin_{plugin_dir.name}" + module_stem = module_name.rsplit(".", 1)[0] + for loaded_name in list(sys.modules): + if loaded_name == module_name or loaded_name.startswith(f"{module_name}."): + del sys.modules[loaded_name] + self.load_errors.pop(plugin_path, None) + + self._emit(PluginEvent.BEFORE_LOAD, plugin_path) + + spec = importlib.util.spec_from_file_location( + module_name, + plugin_path, + submodule_search_locations=[str(plugin_dir)], + ) + if spec is None or spec.loader is None: + self._emit(PluginEvent.ERROR, plugin_path, "invalid spec") + self.load_errors[plugin_path] = "invalid spec" + return None + + module = importlib.util.module_from_spec(spec) + module.__package__ = module_stem + module.__file__ = str(plugin_path) + sys.modules[module_name] = module + try: + spec.loader.exec_module(module) + except Exception as exc: + logger.exception("Failed to load %s", plugin_path) + self._emit(PluginEvent.ERROR, plugin_path, exc) + self.load_errors[plugin_path] = f"{type(exc).__name__}: {exc}" + return None + + plugin_class = getattr(module, "plugin_class", None) + if plugin_class is None: + logger.error("Module %s has no plugin_class export", plugin_path) + self._emit(PluginEvent.ERROR, plugin_path, "missing plugin_class") + self.load_errors[plugin_path] = "missing plugin_class" + return None + + try: + plugin = plugin_class() + except Exception as exc: + logger.exception("Failed to instantiate plugin from %s", plugin_path) + self._emit(PluginEvent.ERROR, plugin_path, exc) + self.load_errors[plugin_path] = f"{type(exc).__name__}: {exc}" + return None + + if not isinstance(plugin, BasePlugin): + logger.error("plugin_class is not BasePlugin: %s", plugin_path) + self.load_errors[plugin_path] = "plugin_class is not BasePlugin" + return None + + key = plugin.name + manifest = self._load_manifest(plugin_dir) + if manifest: + self.manifests[key] = manifest + + self.plugins[key] = plugin + self.loaded_modules[key] = module + self._paths[key] = plugin_path + self.load_errors.pop(plugin_path, None) + self._emit(PluginEvent.AFTER_LOAD, plugin) + return plugin + + def load_all(self) -> dict[str, BasePlugin]: + for path in self.discover(): + try: + self.load_plugin(path) + except Exception as exc: + logger.exception("Failed during load_all %s: %s", path, exc) + return self.plugins + + def reload_all_from_disk(self) -> None: + """Unload every plugin entry then scan directories again.""" + for name in list(self.plugins.keys()): + self.unload_plugin(name) + self.load_errors.clear() + self.load_all() + + def reload_plugin(self, name: str) -> bool: + path = self._paths.get(name) + if path is None: + return False + self.unload_plugin(name) + return self.load_plugin(path) is not None + + def get_plugin_path(self, name: str) -> Path | None: + return self._paths.get(name) + + def unload_plugin(self, name: str) -> None: + self._emit(PluginEvent.BEFORE_UNLOAD, name) + self.plugins.pop(name, None) + mod = self.loaded_modules.pop(name, None) + self.manifests.pop(name, None) + self._paths.pop(name, None) + if mod is not None and getattr(mod, "__name__", None) in sys.modules: + del sys.modules[mod.__name__] + self._emit(PluginEvent.AFTER_UNLOAD, name) + + def get_plugin_for_url(self, url: str) -> BasePlugin | None: + for plugin in self.plugins.values(): + if plugin.can_handle(url): + return plugin + return None + + def check_dependencies(self, manifest: PluginManifest) -> bool: + for dep in manifest.dependencies: + if dep not in self.plugins: + return False + return True diff --git a/pixiv_app/core/plugin/market.py b/pixiv_app/core/plugin/market.py new file mode 100644 index 0000000..682cbb1 --- /dev/null +++ b/pixiv_app/core/plugin/market.py @@ -0,0 +1,29 @@ +""" +Optional plugin market client — stub for future HTTP catalog integration. +""" + +from __future__ import annotations + +from typing import Any + +import requests + + +class PluginMarketClient: + def __init__(self, api_url: str = "") -> None: + self.api_url = api_url.rstrip("/") + + def list_plugins(self) -> list[dict[str, Any]]: + if not self.api_url: + return [] + response = requests.get(f"{self.api_url}/plugins", timeout=15) + response.raise_for_status() + data = response.json() + return list(data.get("plugins", [])) + + def install_plugin(self, _plugin_name: str) -> bool: + """Download zip and extract into plugins/ — not implemented.""" + return False + + def check_updates(self) -> list[dict[str, Any]]: + return [] diff --git a/pixiv_app/core/plugin/templates/base_plugin.py.jinja b/pixiv_app/core/plugin/templates/base_plugin.py.jinja new file mode 100644 index 0000000..e59814f --- /dev/null +++ b/pixiv_app/core/plugin/templates/base_plugin.py.jinja @@ -0,0 +1,31 @@ +""" +{{ name }} plugin — auto-generated at {{ generated_at }}. +""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +from pixiv_app.core.plugin.base import BasePlugin, Resource + + +class {{ class_name }}(BasePlugin): + domain = "{{ domain }}" + name = "{{ name }}" + version = "1.0.0" + + def can_handle(self, url: str) -> bool: + return "{{ domain }}" in url.lower() + + def parse(self, url: str) -> list[Resource]: + raise NotImplementedError("Implement URL parsing and build Resource list.") + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + raise NotImplementedError("Download files for resource into save_path.") + + def get_headers(self) -> dict[str, str]: + return {"User-Agent": "Mozilla/5.0", "Accept": "*/*"} + + +plugin_class = {{ class_name }} diff --git a/pixiv_app/core/plugin/templates/manifest.json.jinja b/pixiv_app/core/plugin/templates/manifest.json.jinja new file mode 100644 index 0000000..66f717a --- /dev/null +++ b/pixiv_app/core/plugin/templates/manifest.json.jinja @@ -0,0 +1,11 @@ +{ + "name": "{{ name }}", + "version": "1.0.0", + "domain": "{{ domain }}", + "author": "{{ author }}", + "description": "{{ description }}", + "requires_auth": false, + "tags": ["general"], + "min_platform_version": "1.0.0", + "dependencies": [] +} diff --git a/pixiv_app/core/proxy_pool.py b/pixiv_app/core/proxy_pool.py new file mode 100644 index 0000000..cd99ec4 --- /dev/null +++ b/pixiv_app/core/proxy_pool.py @@ -0,0 +1,292 @@ +from __future__ import annotations + +import random +import re +import threading +import time +from concurrent.futures import ThreadPoolExecutor, as_completed +from dataclasses import dataclass +from typing import Any, Optional + +import requests + + +@dataclass +class ProxyInfo: + ip: str + port: int + protocol: str = "http" + source: str = "manual" + location: str = "" + org: str = "" + last_check: float = 0.0 + success_count: int = 0 + fail_count: int = 0 + + @property + def proxy_url(self) -> str: + return f"{self.protocol}://{self.ip}:{self.port}" + + @property + def is_alive(self) -> bool: + return self.success_count > 0 or self.fail_count < 3 + + +def parse_proxy_text(text: str) -> list[ProxyInfo]: + proxies: list[ProxyInfo] = [] + seen: set[tuple[str, int, str]] = set() + for raw_line in text.splitlines(): + line = raw_line.strip() + if not line or line.startswith("#"): + continue + if "://" not in line: + line = f"http://{line}" + match = re.match(r"^(https?)://([\w\.\-]+):(\d{2,5})$", line, re.IGNORECASE) + if not match: + continue + protocol, ip, port_text = match.groups() + key = (ip, int(port_text), protocol.lower()) + if key in seen: + continue + seen.add(key) + proxies.append(ProxyInfo(ip=ip, port=int(port_text), protocol=protocol.lower(), source="manual")) + return proxies + + +class QuakeClient: + def __init__( + self, + api_key: str = "", + cookie: str = "", + mode: str = "api_v3", + request_kwargs: Optional[dict[str, Any]] = None, + ): + self.api_key = api_key.strip() + self.cookie = cookie.strip() + self.mode = mode + self.request_kwargs = dict(request_kwargs or {}) + self.base_url = "https://quake.360.net/api/v3" + self.web_assoc_url = "https://quake.360.net/api/search/field/association/quake_service" + + def _api_headers(self) -> dict[str, str]: + return { + "X-QuakeToken": self.api_key, + "Content-Type": "application/json", + } + + def _web_headers(self) -> dict[str, str]: + headers = { + "Accept": "application/json, text/plain, */*", + "Content-Type": "application/json", + "Origin": "https://quake.360.net", + "Referer": "https://quake.360.net/quake/", + "User-Agent": ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" + ), + } + if self.cookie: + headers["Cookie"] = self.cookie + return headers + + def search_ips_api(self, query: str, size: int = 100, start: int = 0) -> list[dict[str, Any]]: + if not self.api_key: + return [] + url = f"{self.base_url}/search/quake_service" + payload = {"query": query, "start": start, "size": min(size, 500)} + kwargs = dict(self.request_kwargs) + kwargs.setdefault("timeout", 30) + try: + response = requests.post(url, headers=self._api_headers(), json=payload, **kwargs) + if response.status_code != 200: + return [] + body = response.json() + return body.get("data", []) if isinstance(body, dict) else [] + except Exception: + return [] + + def search_ips_web_assoc(self, search_content: str) -> list[dict[str, Any]]: + if not self.cookie: + return [] + payload = { + "search_content": search_content, + "device": { + "device_type": "PC", + "os": "Windows", + "os_version": "10.0", + "language": "zh_CN", + "network": "4g", + "browser_info": "Chrome", + "fingerprint": "pixiv-proxy-pool", + "user_agent": self._web_headers()["User-Agent"], + "date": time.strftime("%Y/%m/%d %H:%M:%S"), + "UUID": "pixiv-proxy-association", + }, + } + kwargs = dict(self.request_kwargs) + kwargs.setdefault("timeout", 30) + try: + response = requests.post( + self.web_assoc_url, + headers=self._web_headers(), + json=payload, + **kwargs, + ) + if response.status_code != 200: + return [] + text = response.text + ips = sorted(set(re.findall(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", text))) + return [{"ip": ip, "port": 80, "organization": "", "location": {}} for ip in ips] + except Exception: + return [] + + def get_foreign_proxies( + self, + size: int = 120, + countries: Optional[list[str]] = None, + ) -> list[ProxyInfo]: + proxy_ports = [80, 443, 8080, 3128, 8888, 1080, 8118] + port_query = " OR ".join([f"port:{port}" for port in proxy_ports]) + country_codes = countries or ["US", "JP", "KR", "SG", "HK", "TW", "DE", "FR", "GB", "CA", "AU"] + country_query = " OR ".join([f'country:"{code}"' for code in country_codes]) + query = f"({port_query}) AND ({country_query}) AND response:ok" + + if self.mode == "web_assoc": + data = self.search_ips_web_assoc(query) + else: + data = self.search_ips_api(query=query, size=size) + + proxies: list[ProxyInfo] = [] + seen: set[tuple[str, int, str]] = set() + for item in data: + if not isinstance(item, dict): + continue + ip = item.get("ip") + port = item.get("port") + if not ip or not isinstance(port, int): + continue + protocol = "https" if port in (443, 8443) else "http" + key = (ip, port, protocol) + if key in seen: + continue + seen.add(key) + location_obj = item.get("location", {}) if isinstance(item.get("location"), dict) else {} + location = "/".join( + [str(location_obj.get("country", "")).strip(), str(location_obj.get("province", "")).strip()] + ).strip("/") + proxies.append( + ProxyInfo( + ip=ip, + port=port, + protocol=protocol, + source=f"quake:{self.mode}", + location=location, + org=str(item.get("organization", "") or item.get("org", "")), + ) + ) + return proxies + + +class ProxyPool: + def __init__(self, test_url: str = "https://httpbin.org/ip", test_timeout: int = 6): + self.proxies: list[ProxyInfo] = [] + self.working_proxies: list[ProxyInfo] = [] + self.current_index = 0 + self.test_url = test_url + self.test_timeout = test_timeout + self.session = requests.Session() + self.session.headers.update({"User-Agent": "SakuraDownloaderProxyPool/1.0"}) + self._lock = threading.Lock() + + def add_proxies(self, proxies: list[ProxyInfo]) -> None: + with self._lock: + existing = {(p.ip, p.port, p.protocol) for p in self.proxies} + for proxy in proxies: + key = (proxy.ip, proxy.port, proxy.protocol) + if key not in existing: + existing.add(key) + self.proxies.append(proxy) + + def verify_proxy(self, proxy: ProxyInfo) -> bool: + target_proxies = {"http": proxy.proxy_url, "https": proxy.proxy_url} + try: + response = self.session.get( + self.test_url, + proxies=target_proxies, + timeout=self.test_timeout, + verify=False, + ) + if response.status_code == 200: + with self._lock: + proxy.last_check = time.time() + proxy.success_count += 1 + proxy.fail_count = 0 + return True + except Exception: + pass + with self._lock: + proxy.fail_count += 1 + return False + + def verify_all(self, max_workers: int = 16, max_proxies: int = 80) -> list[ProxyInfo]: + with self._lock: + to_verify = list(self.proxies[:max_proxies]) + self.working_proxies = [] + if not to_verify: + return self.working_proxies + with ThreadPoolExecutor(max_workers=max_workers) as executor: + future_map = {executor.submit(self.verify_proxy, proxy): proxy for proxy in to_verify} + for future in as_completed(future_map): + proxy = future_map[future] + try: + if future.result(): + with self._lock: + self.working_proxies.append(proxy) + except Exception: + continue + with self._lock: + return list(self.working_proxies) + + def collect_until_target( + self, + quake_client: QuakeClient, + target_working: int = 5, + stable_rounds: int = 2, + fetch_batch_size: int = 60, + verify_batch_size: int = 30, + max_rounds: int = 6, + max_workers: int = 16, + countries: Optional[list[str]] = None, + ) -> list[ProxyInfo]: + stable_hits = 0 + for _ in range(max_rounds): + if stable_hits >= stable_rounds: + break + new_items = quake_client.get_foreign_proxies(size=fetch_batch_size, countries=countries) + if new_items: + self.add_proxies(new_items) + self.verify_all(max_workers=max_workers, max_proxies=verify_batch_size) + with self._lock: + working_count = len(self.working_proxies) + if working_count >= target_working: + stable_hits += 1 + else: + stable_hits = 0 + with self._lock: + return list(self.working_proxies) + + def get_proxy(self, rotate: bool = True) -> Optional[ProxyInfo]: + with self._lock: + if not self.working_proxies: + return None + if not rotate: + return random.choice(self.working_proxies) + proxy = self.working_proxies[self.current_index % len(self.working_proxies)] + self.current_index += 1 + return proxy + + def mark_failed(self, proxy: ProxyInfo) -> None: + with self._lock: + proxy.fail_count += 1 + if proxy.fail_count >= 3 and proxy in self.working_proxies: + self.working_proxies.remove(proxy) diff --git a/pixiv_app/core/thumbnails.py b/pixiv_app/core/thumbnails.py new file mode 100644 index 0000000..e914ff8 --- /dev/null +++ b/pixiv_app/core/thumbnails.py @@ -0,0 +1,145 @@ +from __future__ import annotations + +from dataclasses import dataclass +from pathlib import Path + +from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary +from pixiv_app.core.paths import runtime_path + + +DEFAULT_THUMBNAIL_DIR = runtime_path("thumbnails") +SUPPORTED_IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif"} + + +@dataclass +class ThumbnailResult: + artwork_id: int + page_index: int + source_path: str + thumbnail_path: str + width: int + height: int + generated: bool + + +class ThumbnailCache: + def __init__( + self, + *, + db_path: str | Path = DEFAULT_LIBRARY_DB, + cache_dir: str | Path = DEFAULT_THUMBNAIL_DIR, + size: tuple[int, int] = (360, 360), + quality: int = 86, + ) -> None: + self.db_path = Path(db_path) + self.cache_dir = Path(cache_dir) + self.size = size + self.quality = quality + self.cache_dir.mkdir(parents=True, exist_ok=True) + + def ensure_for_artwork(self, artwork_id: int) -> list[ThumbnailResult]: + library = PixivLibrary(self.db_path) + try: + rows = library.connection.execute( + """ + SELECT artwork_id, page_index, file_path + FROM artwork_files + WHERE artwork_id = ? + ORDER BY page_index ASC + """, + (artwork_id,), + ).fetchall() + return [result for row in rows if (result := self.ensure_for_file(library, row)) is not None] + finally: + library.close() + + def build_missing(self, *, limit: int = 200) -> list[ThumbnailResult]: + library = PixivLibrary(self.db_path) + try: + rows = library.connection.execute( + """ + SELECT f.artwork_id, f.page_index, f.file_path + FROM artwork_files f + LEFT JOIN thumbnail_cache t + ON t.artwork_id = f.artwork_id AND t.page_index = f.page_index + WHERE t.thumbnail_path IS NULL + OR t.source_path != f.file_path + ORDER BY f.updated_at DESC + LIMIT ? + """, + (limit,), + ).fetchall() + return [result for row in rows if (result := self.ensure_for_file(library, row)) is not None] + finally: + library.close() + + def ensure_for_file(self, library: PixivLibrary, row) -> ThumbnailResult | None: + source_path = Path(str(row["file_path"])) + if source_path.suffix.lower() not in SUPPORTED_IMAGE_SUFFIXES or not source_path.exists(): + return None + stat = source_path.stat() + thumbnail_path = self._thumbnail_path(int(row["artwork_id"]), int(row["page_index"]), source_path) + + cached = library.connection.execute( + """ + SELECT thumbnail_path, width, height, source_mtime, source_size + FROM thumbnail_cache + WHERE artwork_id = ? AND page_index = ? + """, + (int(row["artwork_id"]), int(row["page_index"])), + ).fetchone() + if ( + cached + and Path(str(cached["thumbnail_path"])).exists() + and float(cached["source_mtime"]) == stat.st_mtime + and int(cached["source_size"]) == stat.st_size + ): + return ThumbnailResult( + artwork_id=int(row["artwork_id"]), + page_index=int(row["page_index"]), + source_path=str(source_path), + thumbnail_path=str(cached["thumbnail_path"]), + width=int(cached["width"]), + height=int(cached["height"]), + generated=False, + ) + + width, height = self._generate(source_path, thumbnail_path) + library.upsert_thumbnail( + artwork_id=int(row["artwork_id"]), + page_index=int(row["page_index"]), + source_path=str(source_path), + thumbnail_path=str(thumbnail_path), + width=width, + height=height, + source_mtime=stat.st_mtime, + source_size=stat.st_size, + ) + return ThumbnailResult( + artwork_id=int(row["artwork_id"]), + page_index=int(row["page_index"]), + source_path=str(source_path), + thumbnail_path=str(thumbnail_path), + width=width, + height=height, + generated=True, + ) + + def _generate(self, source_path: Path, thumbnail_path: Path) -> tuple[int, int]: + try: + from PIL import Image, ImageOps + except ImportError as exc: + raise RuntimeError("Pillow is required to generate thumbnails. Install it with: pip install Pillow") from exc + + thumbnail_path.parent.mkdir(parents=True, exist_ok=True) + with Image.open(source_path) as image: + image = ImageOps.exif_transpose(image) + image.thumbnail(self.size, Image.Resampling.LANCZOS) + if image.mode not in {"RGB", "L"}: + image = image.convert("RGB") + image.save(thumbnail_path, format="JPEG", quality=self.quality, optimize=True) + return image.size + + def _thumbnail_path(self, artwork_id: int, page_index: int, source_path: Path) -> Path: + bucket = str(abs(artwork_id) % 1000).zfill(3) + return self.cache_dir / bucket / f"{artwork_id}_{page_index}_{source_path.stem}.jpg" diff --git a/pixiv_app/core/x_media_service.py b/pixiv_app/core/x_media_service.py new file mode 100644 index 0000000..b68c79b --- /dev/null +++ b/pixiv_app/core/x_media_service.py @@ -0,0 +1,405 @@ +from __future__ import annotations + +import json +import time +import uuid +from dataclasses import asdict +from datetime import datetime +from pathlib import Path +from typing import Any + +from pixiv_app.core.auth import SessionStore +from pixiv_app.core.cookie_import import ( + cookie_summary, + cookies_to_playwright, + parse_cookie_text, + save_playwright_cookies, +) +from pixiv_app.core.paths import ( + app_session_file, + downloads_root, + plugin_roots, + plugins_root as app_plugins_root, + runtime_path, +) +from pixiv_app.core.plugin.base import Resource +from pixiv_app.core.plugin.manager import PluginManager + + +def project_root() -> Path: + return app_plugins_root().parent + + +class XMediaService: + IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp", ".gif"} + VIDEO_EXTS = {".mp4", ".mov", ".webm", ".m4v"} + + def __init__(self, *, plugins_root: str | Path | None = None, save_root: str | Path | None = None) -> None: + self.plugins_root = Path(plugins_root) if plugins_root else app_plugins_root() + self.save_root = Path(save_root) if save_root else downloads_root() / "x" + self.cookie_path = runtime_path("x_cookies.json") + self.follow_store_path = runtime_path("x_followed_authors.json") + self._manager: PluginManager | None = None + self._preview_cache: dict[str, list[Resource]] = {} + + def import_cookie_text(self, text: str) -> dict[str, Any]: + cookies = parse_cookie_text(text) + x_cookies = cookies_to_playwright(cookies, ("x.com", "twitter.com")) + path = save_playwright_cookies(x_cookies, self.cookie_path) + plugin = self._get_plugin() + if hasattr(plugin, "reset_playwright_session"): + plugin.reset_playwright_session() + return {"count": len(x_cookies), "path": str(path), "summary": cookie_summary(x_cookies)} + + def preview_author_media(self, username: str, *, media_type: str = "all", max_items: int = 120) -> dict[str, Any]: + handle = _normalize_username(username) + media_type = _normalize_media_type(media_type) + max_items = max(1, min(int(max_items), 500)) + url = f"https://x.com/{handle}/media" + + plugin = self._get_plugin() + if not plugin.validate(): + raise ValueError("X plugin is unavailable. Install playwright/httpx and run playwright install chromium first.") + + resources = plugin.parse(url) + resources = _filter_resources(resources, media_type)[:max_items] + preview_id = uuid.uuid4().hex + self._preview_cache[preview_id] = resources + return { + "preview_id": preview_id, + "username": handle, + "media_type": media_type, + "count": len(resources), + "items": [_resource_to_json(item, i) for i, item in enumerate(resources)], + "created_at": int(time.time()), + } + + def download_preview(self, preview_id: str, *, selected_ids: list[str] | None = None) -> dict[str, Any]: + resources = self._preview_cache.get(preview_id) + if resources is None: + raise ValueError("Preview expired. Re-run the X media preview.") + selected = set(selected_ids or []) + if selected: + resources = [item for item in resources if item.id in selected] + if not resources: + raise ValueError("No X media items selected.") + + plugin = self._get_plugin() + self.save_root.mkdir(parents=True, exist_ok=True) + results: list[dict[str, Any]] = [] + downloaded = 0 + failed = 0 + for resource in resources: + try: + paths = plugin.download(resource, self.save_root) + downloaded += len(paths) + results.append( + { + "id": resource.id, + "ok": bool(paths), + "paths": [str(path) for path in paths], + "title": resource.title, + "synced_files": 0, + } + ) + if not paths: + failed += 1 + except Exception as exc: # Keep batch downloads moving. + failed += 1 + results.append({"id": resource.id, "ok": False, "error": str(exc), "title": resource.title}) + + return { + "requested": len(resources), + "downloaded_files": downloaded, + "synced_files": 0, + "failed_items": failed, + "items": results, + } + + def list_local_media(self, *, limit: int = 160, offset: int = 0) -> dict[str, Any]: + self.save_root.mkdir(parents=True, exist_ok=True) + limit = max(1, min(int(limit), 500)) + offset = max(0, int(offset)) + files = [ + path + for path in self.save_root.rglob("*") + if path.is_file() and path.suffix.lower() in self.IMAGE_EXTS | self.VIDEO_EXTS + ] + files.sort(key=lambda path: path.stat().st_mtime, reverse=True) + page = files[offset : offset + limit] + return { + "total": len(files), + "items": [self._local_media_to_json(path) for path in page], + } + + def delete_local_media(self, media_ids: list[str]) -> dict[str, Any]: + deleted: list[str] = [] + failed: list[dict[str, str]] = [] + for media_id in media_ids: + try: + path = self.resolve_local_media(media_id) + path.unlink() + deleted.append(media_id) + except Exception as exc: + failed.append({"id": media_id, "error": str(exc)}) + return {"deleted": deleted, "failed": failed} + + def resolve_local_media(self, media_id: str) -> Path: + rel_text = _decode_media_id(media_id) + target = (self.save_root / rel_text).resolve() + root = self.save_root.resolve() + if root != target and root not in target.parents: + raise ValueError("Illegal media path.") + if not target.is_file(): + raise ValueError("Local media file does not exist.") + return target + + def list_followed_authors(self) -> list[dict[str, Any]]: + rows = [item for item in self._load_follow_rows() if bool(item.get("enabled", True))] + rows.sort(key=lambda item: ((str(item.get("creator_name") or "") or str(item.get("username") or "")).lower())) + return rows + + def set_followed_author(self, username: str, *, followed: bool = True, creator_name: str = "") -> dict[str, Any]: + handle = _normalize_username(username) + rows = self._load_follow_rows() + row = next((item for item in rows if str(item.get("username", "")).lower() == handle.lower()), None) + now = _now_text() + if row is None: + row = { + "username": handle, + "creator_name": creator_name.strip() or handle, + "enabled": True, + "profile_url": f"https://x.com/{handle}", + "latest_tweet_id": "", + "latest_published_at": "", + "last_checked_at": "", + "last_error": "", + "seen_tweet_ids": [], + "created_at": now, + } + rows.append(row) + + row["username"] = handle + row["creator_name"] = creator_name.strip() or str(row.get("creator_name") or handle) + row["enabled"] = bool(followed) + row["profile_url"] = f"https://x.com/{handle}" + row["updated_at"] = now + if not followed: + row["last_error"] = "" + + self._save_follow_rows(rows) + return dict(row) + + def sync_followed_authors(self, *, download: bool = True, max_new_per_author: int = 20) -> dict[str, Any]: + followed = self.list_followed_authors() + if not followed: + return {"authors": []} + + plugin = self._get_plugin() + if not plugin.validate(): + raise ValueError("X plugin is unavailable. Import a valid X cookie and ensure Playwright is installed.") + + results: list[dict[str, Any]] = [] + rows = self._load_follow_rows() + + for row in rows: + if not bool(row.get("enabled", True)): + continue + + handle = str(row.get("username") or "").strip() + if not handle: + continue + + try: + parse_for_sync = getattr(plugin, "parse_for_sync", plugin.parse) + resources = parse_for_sync(f"https://x.com/{handle}") + grouped = _group_resources_by_tweet(resources) + seen_ids = [str(item) for item in row.get("seen_tweet_ids", []) if str(item).strip()] + seen_set = set(seen_ids) + new_groups = [item for item in grouped if item["tweet_id"] and item["tweet_id"] not in seen_set] + new_groups = new_groups[: max(0, int(max_new_per_author))] + + downloaded_files = 0 + synced_files = 0 + created_alerts = 0 + new_ids: list[str] = [] + + for group in new_groups: + representative = group["resource"] + new_ids.append(group["tweet_id"]) + + if download: + for resource in group["resources"]: + paths = plugin.download(resource, self.save_root) + downloaded_files += len(paths) + + latest_group = grouped[0] if grouped else None + if latest_group is not None: + representative = latest_group["resource"] + row["creator_name"] = representative.author or str(row.get("creator_name") or handle) + row["latest_tweet_id"] = latest_group["tweet_id"] + row["latest_published_at"] = representative.created_at or str(row.get("latest_published_at") or "") + + row["last_checked_at"] = _now_text() + row["last_error"] = "" + row["updated_at"] = _now_text() + row["seen_tweet_ids"] = _merge_seen_ids(new_ids, seen_ids) + + results.append( + { + "username": handle, + "creator_name": row.get("creator_name") or handle, + "new_posts": len(new_groups), + "downloaded_files": downloaded_files, + "synced_files": synced_files, + "alerts": created_alerts, + "error": "", + } + ) + except Exception as exc: + row["last_checked_at"] = _now_text() + row["last_error"] = str(exc) + row["updated_at"] = _now_text() + results.append( + { + "username": handle, + "creator_name": row.get("creator_name") or handle, + "new_posts": 0, + "downloaded_files": 0, + "synced_files": 0, + "alerts": 0, + "error": str(exc), + } + ) + + self._save_follow_rows(rows) + return {"authors": results} + + def _get_plugin(self): + if self._manager is None: + self._manager = PluginManager(plugin_roots() if self.plugins_root == app_plugins_root() else [self.plugins_root]) + self._manager.load_all() + plugin = self._manager.plugins.get("X") + if plugin is None: + raise ValueError("X plugin is missing. Confirm plugins/x exists.") + return plugin + + def _load_follow_rows(self) -> list[dict[str, Any]]: + if not self.follow_store_path.is_file(): + return [] + try: + payload = json.loads(self.follow_store_path.read_text(encoding="utf-8")) + except Exception: + return [] + if isinstance(payload, list): + return [dict(item) for item in payload if isinstance(item, dict)] + return [] + + def _save_follow_rows(self, rows: list[dict[str, Any]]) -> None: + self.follow_store_path.parent.mkdir(parents=True, exist_ok=True) + self.follow_store_path.write_text( + json.dumps(rows, ensure_ascii=False, indent=2), + encoding="utf-8", + ) + + def _session_payload(self) -> dict[str, Any]: + return SessionStore(app_session_file()).load() + + def _local_media_to_json(self, path: Path) -> dict[str, Any]: + rel = path.relative_to(self.save_root) + stat = path.stat() + kind = "video" if path.suffix.lower() in self.VIDEO_EXTS else "image" + media_id = _encode_media_id(str(rel).replace("\\", "/")) + return { + "id": media_id, + "name": path.name, + "relative_path": str(rel).replace("\\", "/"), + "kind": kind, + "size": stat.st_size, + "modified_at": int(stat.st_mtime), + "url": f"/media/x-local/{media_id}", + } + + +def _normalize_username(username: str) -> str: + value = username.strip() + if value.startswith("@"): + value = value[1:] + if "/" in value: + parts = [item for item in value.replace("https://", "").replace("http://", "").split("/") if item] + value = parts[1] if parts and parts[0].lower() in {"x.com", "twitter.com", "www.x.com", "www.twitter.com"} else parts[0] + if not value: + raise ValueError("Please enter an X username.") + if len(value) > 15 or not value.replace("_", "").isalnum(): + raise ValueError("Invalid X username format.") + return value + + +def _normalize_media_type(media_type: str) -> str: + value = (media_type or "all").strip().lower() + if value not in {"all", "image", "video"}: + raise ValueError("media_type must be all, image, or video.") + return value + + +def _filter_resources(resources: list[Resource], media_type: str) -> list[Resource]: + if media_type == "all": + return resources + return [item for item in resources if any(file.get("type", "image") == media_type for file in item.files)] + + +def _resource_to_json(resource: Resource, index: int) -> dict[str, Any]: + payload = asdict(resource) + payload["index"] = index + payload["kind"] = "video" if any(file.get("type") == "video" for file in resource.files) else "image" + payload["file_count"] = len(resource.files) + return payload + + +def _group_resources_by_tweet(resources: list[Resource]) -> list[dict[str, Any]]: + grouped: dict[str, dict[str, Any]] = {} + ordered: list[str] = [] + for resource in resources: + tweet_id = str(resource.metadata.get("tweet_id") or resource.id or "") + key = tweet_id or resource.id + if key not in grouped: + grouped[key] = { + "tweet_id": tweet_id, + "resource": resource, + "resources": [resource], + } + ordered.append(key) + else: + grouped[key]["resources"].append(resource) + return [grouped[key] for key in ordered] + + +def _merge_seen_ids(new_ids: list[str], existing_ids: list[str], *, limit: int = 200) -> list[str]: + merged: list[str] = [] + seen: set[str] = set() + for item in [*new_ids, *existing_ids]: + value = str(item).strip() + if not value or value in seen: + continue + seen.add(value) + merged.append(value) + if len(merged) >= limit: + break + return merged + + +def _encode_media_id(value: str) -> str: + from base64 import urlsafe_b64encode + + return urlsafe_b64encode(value.encode("utf-8")).decode("ascii").rstrip("=") + + +def _decode_media_id(value: str) -> str: + from base64 import urlsafe_b64decode + + padding = "=" * (-len(value) % 4) + return urlsafe_b64decode((value + padding).encode("ascii")).decode("utf-8") + + +def _now_text() -> str: + return datetime.now().isoformat(timespec="seconds") diff --git a/pixiv_app/gui/__init__.py b/pixiv_app/gui/__init__.py new file mode 100644 index 0000000..0d2d8ca --- /dev/null +++ b/pixiv_app/gui/__init__.py @@ -0,0 +1 @@ +"""GUI layer for Sakura downloader.""" diff --git a/pixiv_app/gui/app.py b/pixiv_app/gui/app.py new file mode 100644 index 0000000..8738ba3 --- /dev/null +++ b/pixiv_app/gui/app.py @@ -0,0 +1,2794 @@ +from __future__ import annotations + +import queue +import socket +import threading +import time +import json +import urllib.error +import urllib.request +import webbrowser +from pathlib import Path +from tkinter import filedialog, messagebox +from typing import Any +from urllib.parse import quote, urlparse + +import customtkinter as ctk +import requests + +from pixiv_app.core.auth import AuthResult, PixivAuthClient, SessionStore +from pixiv_app.core.cookie_import import ( + cookie_domains, + cookie_summary, + cookies_to_header, + cookies_to_playwright, + has_cookie_domain, + parse_cookie_text, + save_playwright_cookies, +) +from pixiv_app.core.cloud_sync import CloudSession +from pixiv_app.core.downloader import ( + DownloadResult, + PixivRequestError, + download_illust, + download_keyword_works, + download_novel, + download_user_works, + fetch_novel_series_ids, + fetch_user_work_ids, + parse_pixiv_target, + resolve_workers, + search_illust_ids_by_keyword, +) +from pixiv_app.core.plugin.manager import PluginManager +from pixiv_app.core.paths import app_session_file, downloads_root, plugin_roots, plugins_root, resource_root, runtime_path +from pixiv_app.core.proxy_pool import ProxyInfo, ProxyPool, QuakeClient, parse_proxy_text +from pixiv_app.gui.proxy_dialog import ProxyDialog +from pixiv_app.runtime.enqueue import collect_task_specs, enqueue_specs_to_library +from pixiv_app.runtime.worker_loop import run_queue_until_idle +from pixiv_app.services.gallery_api import GalleryApiServer +from pixiv_app.tasks.parser import parse_batch_for_mode + + +DEFAULT_CLOUD_API = "http://api.kunkunxiaomao.top:8000" + + +ctk.set_appearance_mode("light") +ctk.set_default_color_theme("blue") + + +class BookSelectionDialog(ctk.CTkToplevel): + def __init__(self, parent, books: list[dict[str, Any]], *, title: str = "选择书籍") -> None: + super().__init__(parent) + self.title(title) + self.geometry("720x560") + self.minsize(640, 420) + self.transient(parent) + self.grab_set() + + self._books = books + self._selected: dict[str, Any] | None = None + self._choice_var = ctk.StringVar(value=str(books[0].get("url") or "") if books else "") + + shell = ctk.CTkFrame(self, fg_color="#fff8fb") + shell.pack(fill="both", expand=True, padx=18, pady=18) + + ctk.CTkLabel( + shell, + text=title, + text_color="#7f3158", + font=ctk.CTkFont(size=22, weight="bold"), + ).pack(anchor="w", padx=18, pady=(18, 6)) + ctk.CTkLabel( + shell, + text="请选择要抓取的小说。确认后会自动抓取该书的全部章节。", + text_color="#a06b84", + font=ctk.CTkFont(size=13), + ).pack(anchor="w", padx=18, pady=(0, 12)) + + list_frame = ctk.CTkScrollableFrame(shell, fg_color="#fffdfd", corner_radius=16) + list_frame.pack(fill="both", expand=True, padx=14, pady=(0, 12)) + + if not books: + ctk.CTkLabel( + list_frame, + text="没有可选书籍。", + text_color="#8e6578", + font=ctk.CTkFont(size=14), + ).pack(anchor="w", padx=12, pady=12) + + for index, book in enumerate(books, start=1): + url = str(book.get("url") or "") + title_text = str(book.get("title") or f"未命名书籍 {index}") + author = str(book.get("author") or "").strip() + summary = f"作者:{author}" if author else "作者:未知" + if book.get("latest_chapter"): + summary += f" 最近章节:{book['latest_chapter']}" + card = ctk.CTkFrame(list_frame, fg_color="#ffffff", corner_radius=14, border_width=1, border_color="#f0d7e5") + card.pack(fill="x", padx=6, pady=6) + radio = ctk.CTkRadioButton( + card, + text=title_text, + variable=self._choice_var, + value=url, + text_color="#6d2d4a", + font=ctk.CTkFont(size=15, weight="bold"), + ) + radio.pack(anchor="w", padx=14, pady=(12, 4)) + ctk.CTkLabel( + card, + text=summary, + text_color="#8c6579", + font=ctk.CTkFont(size=12), + ).pack(anchor="w", padx=42, pady=(0, 4)) + ctk.CTkLabel( + card, + text=url, + text_color="#b18499", + font=ctk.CTkFont(size=11), + wraplength=620, + justify="left", + ).pack(anchor="w", padx=42, pady=(0, 12)) + + action_row = ctk.CTkFrame(shell, fg_color="transparent") + action_row.pack(fill="x", padx=14, pady=(0, 14)) + ctk.CTkButton( + action_row, + text="取消", + width=110, + fg_color="#f3dce7", + text_color="#7c4760", + hover_color="#e9c9da", + command=self._cancel, + ).pack(side="right") + ctk.CTkButton( + action_row, + text="确认抓取", + width=128, + fg_color="#f59bc2", + text_color="#ffffff", + hover_color="#eb86b3", + command=self._confirm, + ).pack(side="right", padx=(0, 10)) + + self.protocol("WM_DELETE_WINDOW", self._cancel) + + def _confirm(self) -> None: + selected_url = self._choice_var.get().strip() + for book in self._books: + if str(book.get("url") or "") == selected_url: + self._selected = book + break + self.destroy() + + def _cancel(self) -> None: + self._selected = None + self.destroy() + + def show(self) -> dict[str, Any] | None: + self.wait_window() + return self._selected + + +class SakuraDownloaderGUI: + TARGET_MODE_MAP = { + "作者全部作品": "user", + "单个作品": "illust", + "漫画": "manga", + "文章/小说": "novel", + "关键词图片": "keyword", + } + X_TARGET_MODE_MAP = { + "作者媒体": "x_user", + "单个推文": "x_status", + "搜索词媒体": "x_keyword", + } + XHS_TARGET_MODE_MAP = { + "搜索关键词": "xhs_keyword", + "单篇笔记": "xhs_note", + "链接解析": "xhs_link", + } + BIQUGE_TARGET_MODE_MAP = { + "搜索 / 书籍链接": "biquge_book", + } + CHANGPEI_TARGET_MODE_MAP = { + "搜索 / 书籍链接": "changpei_book", + } + GENERIC_PLUGIN_MODE_MAP = { + "自动解析": "plugin_auto", + } + PLACEHOLDER_MAP = { + "user": "请输入作者 ID,或作品链接自动反查作者", + "illust": "请输入作品 ID 或作品链接", + "manga": "请输入漫画作品 ID 或漫画链接,多页会自动完整下载", + "novel": "请输入小说 ID 或小说链接", + "keyword": "请输入关键词,例如 初音未来、猫耳", + } + X_PLACEHOLDER_MAP = { + "x_user": "请输入 X 作者名,例如 nasa 或 @nasa", + "x_status": "请输入 X / Twitter 单条推文链接", + "x_keyword": "请输入搜索词,例如 anime art filter:media", + } + XHS_PLACEHOLDER_MAP = { + "xhs_keyword": "请输入小红书搜索关键词,例如 穿搭、插画、摄影", + "xhs_note": "请输入小红书笔记链接,例如 https://www.xiaohongshu.com/explore/...", + "xhs_link": "请输入小红书链接或 xhslink.com 分享链接", + } + BIQUGE_PLACEHOLDER_MAP = { + "biquge_book": "请输入书名、作者、书籍链接、章节链接,或榜单/列表页链接", + } + CHANGPEI_PLACEHOLDER_MAP = { + "changpei_book": "请输入书名、作者、书籍 ID、novel-{id}.html 或 read-{书ID}-{章ID}.html 链接", + } + LOGIN_MODE_MAP = { + "Cookie 登录": "cookie", + "账号密码登录": "password", + } + + BG_COLOR = "#fff3f8" + SURFACE_COLOR = "#fff8fb" + CARD_COLOR = "#ffffff" + PRIMARY_COLOR = "#cc5f91" + PRIMARY_HOVER = "#b64f80" + SECONDARY_COLOR = "#ffe3ef" + TEXT_COLOR = "#2b1c27" + MUTED_COLOR = "#7f6876" + BORDER_COLOR = "#f0d5e2" + SUCCESS_COLOR = "#56b98a" + WARNING_COLOR = "#f2b95e" + DANGER_COLOR = "#eb7d7d" + + def __init__(self) -> None: + self.root = ctk.CTk(fg_color=self.BG_COLOR) + self.root.title("Sakura 本地媒体库") + self.root.geometry("1280x840") + self.root.minsize(1024, 720) + self.apply_window_icon() + + self.is_downloading = False + self.stop_event = threading.Event() + self.download_thread: threading.Thread | None = None + self.gallery_thread: threading.Thread | None = None + self.gallery_url = "http://127.0.0.1:8765" + self.ui_queue: queue.Queue = queue.Queue() + + self.total_works = 0 + self.completed_works = 0 + self.success_works = 0 + self.failed_works = 0 + self.downloaded_pages = 0 + self.skipped_pages = 0 + self.failed_pages = 0 + + self.proxy_config = { + "enabled": False, + "use_quake": False, + "quake_mode": "api_v3", + "quake_api_key": "", + "quake_cookie": "", + "manual_text": "", + "countries_text": "US,JP,KR,SG,HK,TW,DE,FR,GB,CA,AU", + } + self.proxy_summary_var = ctk.StringVar(value="未启用代理池") + self.use_queue_var = ctk.BooleanVar(value=True) + self.incremental_wm_var = ctk.BooleanVar(value=True) + self.cloud_sync_enabled_var = ctk.BooleanVar(value=False) + self.cloud_status_var = ctk.StringVar(value="未启用云同步") + self.cloud_base_url = DEFAULT_CLOUD_API + self.cloud_access_token = "" + self.cloud_refresh_token = "" + self.platform_var = ctk.StringVar(value="Pixiv") + self.target_mode_var = ctk.StringVar(value="作者全部作品") + self.login_mode_var = ctk.StringVar(value="Cookie 登录") + self.auth_status_var = ctk.StringVar(value="未登录,推荐导入 cookies.txt;也可尝试账号密码登录。") + self.plugin_health_var = ctk.StringVar(value="点击检测,确认本地插件是否可以正常加载。") + self.cookie_json: list[dict] = [] + self.cookie_header = "" + + self.auth_client = PixivAuthClient() + self.session_store = SessionStore(app_session_file()) + + self.setup_ui() + self.process_ui_queue() + self.update_target_mode_ui(self.target_mode_var.get()) + self.update_crawler_platform_ui() + self.update_login_mode_ui(self.login_mode_var.get()) + self.load_saved_session(silent=True) + self.root.after(500, self.show_login_warning) + + def apply_window_icon(self) -> None: + icon_path = resource_root() / "build_assets" / "app_icon.ico" + if not icon_path.exists(): + return + try: + self.root.iconbitmap(str(icon_path)) + except Exception: + pass + + def setup_ui(self) -> None: + self.create_header() + self.create_body() + self.create_footer() + + def create_header(self) -> None: + header = ctk.CTkFrame(self.root, fg_color=self.SURFACE_COLOR, corner_radius=0, border_width=0, height=86) + header.pack(fill="x") + header.pack_propagate(False) + content = ctk.CTkFrame(header, fg_color="transparent") + content.pack(fill="both", expand=True, padx=26, pady=14) + title_group = ctk.CTkFrame(content, fg_color="transparent") + title_group.pack(side="left", fill="y") + ctk.CTkLabel( + title_group, + text="Sakura", + text_color=self.TEXT_COLOR, + font=ctk.CTkFont(size=26, weight="bold"), + ).pack(anchor="w") + ctk.CTkLabel( + title_group, + text="下载、整理与浏览你的本地内容", + text_color=self.MUTED_COLOR, + font=ctk.CTkFont(size=13), + ).pack(anchor="w", pady=(2, 0)) + actions = ctk.CTkFrame(content, fg_color="transparent") + actions.pack(side="right", anchor="e", pady=6) + ctk.CTkButton( + actions, + text="打开媒体库", + width=112, + height=36, + corner_radius=8, + fg_color=self.PRIMARY_COLOR, + hover_color=self.PRIMARY_HOVER, + command=self.open_gallery_wall, + ).pack(side="left", padx=(0, 8)) + ctk.CTkButton( + actions, + text="插件管理", + width=100, + height=36, + corner_radius=8, + fg_color=self.SECONDARY_COLOR, + text_color=self.TEXT_COLOR, + hover_color="#f5ccdd", + command=self.open_plugin_panel, + ).pack(side="left") + + def create_body(self) -> None: + body = ctk.CTkTabview( + self.root, + fg_color="transparent", + segmented_button_fg_color="#fff7fb", + segmented_button_selected_color=self.PRIMARY_COLOR, + segmented_button_selected_hover_color=self.PRIMARY_HOVER, + segmented_button_unselected_color="#fff7fb", + segmented_button_unselected_hover_color="#ffe3ef", + text_color=self.TEXT_COLOR, + ) + body.pack(fill="both", expand=True, padx=18, pady=(10, 8)) + body.add("下载任务") + body.add("本地媒体库") + body.add("账号与设置") + + download_tab = body.tab("下载任务") + download_tab.grid_columnconfigure(0, weight=6) + download_tab.grid_columnconfigure(1, weight=5) + download_tab.grid_rowconfigure(0, weight=1) + download_left = ctk.CTkScrollableFrame(download_tab, fg_color="transparent", corner_radius=0) + download_left.grid(row=0, column=0, sticky="nsew", padx=(0, 7), pady=8) + download_right = ctk.CTkScrollableFrame(download_tab, fg_color="transparent", corner_radius=0) + download_right.grid(row=0, column=1, sticky="nsew", padx=(7, 0), pady=8) + self.create_input_card(download_left) + self.create_progress_card(download_left) + self.create_overview_card(download_right) + self.create_log_card(download_right) + + library_tab = body.tab("本地媒体库") + library_tab.grid_columnconfigure(0, weight=1) + library_tab.grid_columnconfigure(1, weight=1) + library_tab.grid_rowconfigure(0, weight=1) + library_left = ctk.CTkScrollableFrame(library_tab, fg_color="transparent", corner_radius=0) + library_left.grid(row=0, column=0, sticky="nsew", padx=(0, 7), pady=8) + library_right = ctk.CTkScrollableFrame(library_tab, fg_color="transparent", corner_radius=0) + library_right.grid(row=0, column=1, sticky="nsew", padx=(7, 0), pady=8) + self.create_library_card(library_left) + self.create_quick_actions_card(library_left) + self.create_plugin_health_card(library_right) + self.create_tips_card(library_right) + + settings_tab = body.tab("账号与设置") + settings_tab.grid_columnconfigure(0, weight=1) + settings_tab.grid_columnconfigure(1, weight=1) + settings_tab.grid_rowconfigure(0, weight=1) + settings_left = ctk.CTkScrollableFrame(settings_tab, fg_color="transparent", corner_radius=0) + settings_left.grid(row=0, column=0, sticky="nsew", padx=(0, 7), pady=8) + settings_right = ctk.CTkScrollableFrame(settings_tab, fg_color="transparent", corner_radius=0) + settings_right.grid(row=0, column=1, sticky="nsew", padx=(7, 0), pady=8) + self.create_auth_card(settings_left) + self.create_settings_card(settings_right) + + def make_card(self, parent, title: str, desc: str | None = None) -> ctk.CTkFrame: + card = ctk.CTkFrame(parent, fg_color=self.CARD_COLOR, corner_radius=10, border_width=1, border_color=self.BORDER_COLOR) + card.pack(fill="x", pady=(0, 12)) + ctk.CTkLabel(card, text=title, text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=18, weight="bold")).pack( + anchor="w", padx=18, pady=(16, 4) + ) + if desc: + ctk.CTkLabel( + card, + text=desc, + text_color=self.MUTED_COLOR, + font=ctk.CTkFont(size=13), + wraplength=520, + justify="left", + ).pack(anchor="w", padx=18, pady=(0, 12)) + return card + + def create_library_card(self, parent) -> None: + card = self.make_card( + parent, + "本地媒体库", + "插画以缩略图浏览,漫画、小说和视频按作品聚合;无需登录云端也可以使用。", + ) + ctk.CTkButton( + card, + text="打开本地媒体库", + command=self.open_gallery_wall, + height=44, + corner_radius=8, + fg_color=self.SUCCESS_COLOR, + hover_color="#3f9d72", + ).pack(fill="x", padx=18, pady=(2, 10)) + ctk.CTkLabel( + card, + text="媒体库在本机启动,只读取 Sakura 的本地数据库与下载目录。", + text_color=self.MUTED_COLOR, + font=ctk.CTkFont(size=12), + justify="left", + ).pack(anchor="w", padx=18, pady=(0, 16)) + + def create_plugin_health_card(self, parent) -> None: + card = self.make_card(parent, "插件状态", "检测 X、Pixiv、小红书、抖音等插件能否加载及其依赖是否齐全。") + status = ctk.CTkFrame(card, fg_color="#f7fbff", corner_radius=8) + status.pack(fill="x", padx=18, pady=(0, 10)) + ctk.CTkLabel( + status, + textvariable=self.plugin_health_var, + text_color="#365b85", + font=ctk.CTkFont(size=12), + justify="left", + wraplength=480, + ).pack(anchor="w", padx=12, pady=10) + row = ctk.CTkFrame(card, fg_color="transparent") + row.pack(fill="x", padx=18, pady=(0, 16)) + ctk.CTkButton( + row, + text="检测全部插件", + command=self.check_plugin_health, + height=38, + corner_radius=8, + ).pack(side="left") + ctk.CTkButton( + row, + text="打开插件管理", + command=self.open_plugin_panel, + height=38, + corner_radius=8, + fg_color=self.SECONDARY_COLOR, + hover_color="#f5ccdd", + text_color=self.TEXT_COLOR, + ).pack(side="left", padx=(8, 0)) + + def check_plugin_health(self) -> None: + self.plugin_health_var.set("正在检测插件...") + + def worker() -> None: + # Use isolated instances so a health check cannot interrupt an active download. + manager = PluginManager(self._plugin_roots()) + manager.load_all() + ready = [name for name, plugin in manager.plugins.items() if plugin.validate()] + unavailable = [name for name, plugin in manager.plugins.items() if not plugin.validate()] + summary = f"已加载 {len(manager.plugins)} 个,可用 {len(ready)} 个" + if unavailable: + summary += f";需配置:{', '.join(sorted(unavailable))}" + if manager.load_errors: + summary += f";加载失败 {len(manager.load_errors)} 个" + else: + summary += ";没有加载错误" + self.enqueue_ui(self.plugin_health_var.set, summary) + + threading.Thread(target=worker, daemon=True).start() + + def create_auth_card(self, parent) -> None: + card = self.make_card(parent, "登录方式", "推荐导入 Get cookies.txt 导出的 txt/json 文件;程序会解析成 JSON 保存,抓取时再交给后端请求层使用。") + + mode_row = ctk.CTkFrame(card, fg_color="transparent") + mode_row.pack(fill="x", padx=20, pady=(0, 12)) + ctk.CTkLabel(mode_row, text="方式", text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=14, weight="bold"), width=78).pack( + side="left", padx=(0, 10) + ) + self.login_mode_menu = ctk.CTkOptionMenu( + mode_row, + values=list(self.LOGIN_MODE_MAP.keys()), + variable=self.login_mode_var, + command=self.update_login_mode_ui, + width=220, + ) + self.login_mode_menu.pack(side="left") + + self.cookie_frame = ctk.CTkFrame(card, fg_color="transparent") + cookie_row = ctk.CTkFrame(self.cookie_frame, fg_color="transparent") + cookie_row.pack(fill="x", padx=20, pady=(0, 14)) + ctk.CTkLabel( + cookie_row, + text="Cookie txt", + text_color=self.TEXT_COLOR, + font=ctk.CTkFont(size=14, weight="bold"), + width=78, + ).pack(side="left", padx=(0, 10)) + self.cookie_import_button = ctk.CTkButton( + cookie_row, + text="导入 cookies.txt / json", + command=self.import_cookie_txt, + height=42, + corner_radius=14, + fg_color="#f7fbff", + border_width=2, + border_color=self.BORDER_COLOR, + text_color="#6f7f91", + hover_color="#e8f3ff", + ) + self.cookie_import_button.pack(side="left", fill="x", expand=True) + + self.password_frame = ctk.CTkFrame(card, fg_color="transparent") + self.login_id_entry = self.create_labeled_entry( + self.password_frame, + "账号", + "请输入邮箱、Pixiv ID 或登录账号", + ) + self.password_entry = self.create_labeled_entry( + self.password_frame, + "密码", + "请输入密码;不会写入源码,只用于本次尝试登录", + show="*", + ) + + action_row = ctk.CTkFrame(card, fg_color="transparent") + action_row.pack(fill="x", padx=20, pady=(4, 10)) + self.load_session_button = ctk.CTkButton(action_row, text="自动载入", width=110, command=self.load_saved_session) + self.load_session_button.pack(side="left") + self.validate_session_button = ctk.CTkButton(action_row, text="检测会话", width=110, command=self.validate_session) + self.validate_session_button.pack(side="left", padx=(10, 0)) + self.login_button = ctk.CTkButton(action_row, text="尝试登录", width=110, command=self.login_with_password) + self.login_button.pack(side="left", padx=(10, 0)) + self.save_session_button = ctk.CTkButton(action_row, text="保存会话", width=110, command=self.save_session) + self.save_session_button.pack(side="left", padx=(10, 0)) + + status_row = ctk.CTkFrame(card, fg_color="#f7fbff", corner_radius=12) + status_row.pack(fill="x", padx=20, pady=(0, 18)) + ctk.CTkLabel( + status_row, + textvariable=self.auth_status_var, + text_color="#365b85", + font=ctk.CTkFont(size=12), + justify="left", + wraplength=520, + ).pack(anchor="w", padx=12, pady=8) + + def create_input_card(self, parent) -> None: + card = self.make_card( + parent, + "抓取目标", + "先选择平台:Pixiv 使用下方模式与队列;插件自动识别会按 can_handle() 匹配已导入插件。", + ) + + plat_row = ctk.CTkFrame(card, fg_color="transparent") + plat_row.pack(fill="x", padx=20, pady=(0, 12)) + ctk.CTkLabel(plat_row, text="平台", text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=14, weight="bold"), width=78).pack( + side="left", padx=(0, 10) + ) + self.platform_menu = ctk.CTkOptionMenu( + plat_row, + values=["Pixiv", "X / Twitter", "小红书", "笔趣阁小说", "长佩文学", "插件自动识别"], + variable=self.platform_var, + command=self.update_crawler_platform_ui, + width=200, + ) + self.platform_menu.pack(side="left") + + mode_row = ctk.CTkFrame(card, fg_color="transparent") + mode_row.pack(fill="x", padx=20, pady=(0, 12)) + ctk.CTkLabel(mode_row, text="模式", text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=14, weight="bold"), width=78).pack( + side="left", padx=(0, 10) + ) + self.target_mode_menu = ctk.CTkOptionMenu( + mode_row, + values=list(self.TARGET_MODE_MAP.keys()), + variable=self.target_mode_var, + command=self.update_target_mode_ui, + width=220, + ) + self.target_mode_menu.pack(side="left") + + self.target_entry = self.create_labeled_entry(card, "目标", self.PLACEHOLDER_MAP["user"]) + self.mode_hint_label = ctk.CTkLabel(card, text="", text_color=self.MUTED_COLOR, font=ctk.CTkFont(size=12), justify="left") + self.mode_hint_label.pack(anchor="w", padx=20, pady=(0, 10)) + + keyword_row = ctk.CTkFrame(card, fg_color="transparent") + keyword_row.pack(fill="x", padx=20, pady=(0, 12)) + self.keyword_limit_label = ctk.CTkLabel(keyword_row, text="关键词数量", text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=14, weight="bold"), width=78) + self.keyword_limit_label.pack( + side="left", padx=(0, 10) + ) + self.keyword_limit_entry = ctk.CTkEntry( + keyword_row, + width=120, + height=42, + corner_radius=14, + fg_color="#f7fbff", + border_color=self.BORDER_COLOR, + ) + self.keyword_limit_entry.insert(0, "20") + self.keyword_limit_entry.pack(side="left") + self.keyword_row_frame = keyword_row + + row = ctk.CTkFrame(card, fg_color="transparent") + row.pack(fill="x", padx=20, pady=(4, 18)) + self.hero_start_button = ctk.CTkButton( + row, + text="开始抓取", + command=self.start_download, + height=46, + width=180, + corner_radius=999, + fg_color=self.PRIMARY_COLOR, + hover_color=self.PRIMARY_HOVER, + text_color="#ffffff", + font=ctk.CTkFont(size=16, weight="bold"), + ) + self.hero_start_button.pack(side="left") + + def create_settings_card(self, parent) -> None: + card = self.make_card(parent, "性能设置", "代理模式下线程数会自动限制在 3 到 6 之间,以降低触发限流的概率。") + self.thread_slider, _ = self.create_slider_row(card, "下载线程", 2, 16, 14, 8, lambda value: f"{int(value)}") + self.delay_slider, _ = self.create_slider_row(card, "请求间隔", 0.0, 1.5, 15, 0.1, lambda value: f"{value:.1f}s") + + row = ctk.CTkFrame(card, fg_color="transparent") + row.pack(fill="x", padx=20, pady=(0, 14)) + self.proxy_button = ctk.CTkButton(row, text="代理设置", command=self.open_proxy_dialog, width=120, height=38, corner_radius=12) + self.proxy_button.pack(side="left") + ctk.CTkLabel(row, textvariable=self.proxy_summary_var, text_color=self.MUTED_COLOR, font=ctk.CTkFont(size=12)).pack( + side="left", padx=10 + ) + + opt = ctk.CTkFrame(card, fg_color="transparent") + opt.pack(fill="x", padx=20, pady=(0, 14)) + self.use_queue_checkbox = ctk.CTkCheckBox( + opt, + text="统一任务队列(SQLite 状态)", + variable=self.use_queue_var, + font=ctk.CTkFont(size=13), + text_color=self.TEXT_COLOR, + ) + self.use_queue_checkbox.pack(side="left", padx=(0, 18)) + self.incremental_wm_checkbox = ctk.CTkCheckBox( + opt, + text="作者增量同步(水印)", + variable=self.incremental_wm_var, + font=ctk.CTkFont(size=13), + text_color=self.TEXT_COLOR, + ) + self.incremental_wm_checkbox.pack(side="left") + + def create_cloud_sync_card(self, parent) -> None: + card = self.make_card(parent, "云同步", "下载完成后自动上传到 Sakura Cloud,手机端可直接刷新查看。") + opt = ctk.CTkFrame(card, fg_color="transparent") + opt.pack(fill="x", padx=20, pady=(0, 12)) + self.cloud_sync_checkbox = ctk.CTkCheckBox( + opt, + text="启用 Sakura Cloud 同步", + variable=self.cloud_sync_enabled_var, + font=ctk.CTkFont(size=13), + text_color=self.TEXT_COLOR, + ) + self.cloud_sync_checkbox.pack(side="left") + self.cloud_test_btn = ctk.CTkButton( + opt, + text="测试连接", + width=88, + height=30, + corner_radius=12, + fg_color=self.SECONDARY_COLOR, + text_color=self.TEXT_COLOR, + hover_color=self.PRIMARY_HOVER, + command=self.test_cloud_connection, + ) + self.cloud_test_btn.pack(side="right", padx=(8, 0)) + self.cloud_account_btn = ctk.CTkButton( + opt, + text="账号管理", + width=100, + height=30, + corner_radius=12, + command=self.open_cloud_auth_dialog, + ) + self.cloud_account_btn.pack(side="right") + ctk.CTkLabel( + card, + text="账号与 Token 会在登录后自动保存,无需手动填写 API 或 Token。", + text_color=self.MUTED_COLOR, + font=ctk.CTkFont(size=12), + ).pack(anchor="w", padx=20, pady=(0, 10)) + status_row = ctk.CTkFrame(card, fg_color="#f7fbff", corner_radius=12) + status_row.pack(fill="x", padx=20, pady=(0, 18)) + ctk.CTkLabel( + status_row, + textvariable=self.cloud_status_var, + text_color="#365b85", + font=ctk.CTkFont(size=12), + justify="left", + wraplength=520, + ).pack(anchor="w", padx=12, pady=8) + + def test_cloud_connection(self) -> None: + """测试云服务器连接和 Token 有效性。""" + api_url = self.cloud_base_url or DEFAULT_CLOUD_API + token = self.cloud_access_token.strip() + if not token: + self.cloud_status_var.set("请先点击“账号管理”登录或注册云端账号") + return + + self.cloud_status_var.set("正在测试连接...") + + def worker() -> None: + try: + resp = requests.get( + f"{api_url}/api/v1/users/me", + headers={"Authorization": f"Bearer {token}"}, + timeout=10, + ) + if resp.status_code == 200: + user = resp.json().get("data", {}) + username = user.get("username", "未知") + self.enqueue_ui(self.cloud_status_var.set, f"连接成功!用户:{username}") + return + detail = self.extract_api_error(resp) + self.enqueue_ui( + self.cloud_status_var.set, + f"连接失败 (HTTP {resp.status_code}):{detail}" if detail else f"Token 无效或已过期 (HTTP {resp.status_code})", + ) + except requests.exceptions.ConnectionError: + self.enqueue_ui(self.cloud_status_var.set, f"无法连接到 {api_url},请检查网络和 API 地址") + except Exception as exc: + self.enqueue_ui(self.cloud_status_var.set, f"连接失败: {str(exc)[:80]}") + + threading.Thread(target=worker, daemon=True).start() + + def open_cloud_auth_dialog(self) -> None: + """打开云同步账号管理窗口。""" + dialog = ctk.CTkToplevel(self.root) + dialog.title("Sakura Cloud 账号管理") + dialog.geometry("520x620") + dialog.transient(self.root) + dialog.grab_set() + + api_frame = ctk.CTkFrame(dialog, fg_color="transparent") + api_frame.pack(fill="x", padx=20, pady=(15, 5)) + ctk.CTkLabel(api_frame, text="当前 API 地址:", font=ctk.CTkFont(size=12, weight="bold")).pack(side="left") + api_url = self.cloud_base_url or DEFAULT_CLOUD_API + ctk.CTkLabel(api_frame, text=api_url, text_color="#6f8dac", font=ctk.CTkFont(size=11)).pack(side="left", padx=5) + + tabview = ctk.CTkTabview(dialog, width=480, height=500) + tabview.pack(padx=20, pady=10) + tabview.add("登录") + tabview.add("注册") + tabview.add("获取 Token") + + login_frame = tabview.tab("登录") + ctk.CTkLabel(login_frame, text="邮箱 / 用户名", anchor="w").pack(fill="x", padx=20, pady=(15, 0)) + login_email = ctk.CTkEntry(login_frame, placeholder_text="user@example.com 或 username") + login_email.pack(fill="x", padx=20, pady=5) + ctk.CTkLabel(login_frame, text="密码", anchor="w").pack(fill="x", padx=20, pady=(10, 0)) + login_password = ctk.CTkEntry(login_frame, placeholder_text="••••••••", show="*") + login_password.pack(fill="x", padx=20, pady=5) + login_status = ctk.CTkLabel(login_frame, text="", text_color="gray") + login_status.pack(pady=10) + + def do_login() -> None: + email = login_email.get().strip() + password = login_password.get() + if not email or not password: + login_status.configure(text="请填写账号和密码", text_color="red") + return + login_status.configure(text="登录中...", text_color="orange") + + def worker() -> None: + api_addr = self.cloud_base_url or DEFAULT_CLOUD_API + try: + resp = requests.post( + f"{api_addr}/api/v1/auth/login", + json={"username_or_email": email, "password": password}, + timeout=15, + ) + if resp.status_code == 200: + data = resp.json() + token = data.get("data", {}).get("access_token") + if token: + self.cloud_access_token = str(token).strip() + refresh = (data.get("data", {}).get("refresh_token") or "") + if refresh: + self.cloud_refresh_token = str(refresh).strip() + CloudSession().configure( + base_url=self.cloud_base_url, + access_token=self.cloud_access_token, + refresh_token=self.cloud_refresh_token, + enabled=True, + ) + self.enqueue_ui(self.cloud_status_var.set, "登录成功,云同步凭证已自动保存") + self.enqueue_ui(self.cloud_sync_enabled_var.set, True) + self.enqueue_ui(self.save_session) + self.enqueue_ui(dialog.destroy) + return + detail = self.extract_api_error(resp) + self.enqueue_ui( + lambda: login_status.configure( + text=f"登录失败 (HTTP {resp.status_code}):{detail or '请检查账号密码'}", + text_color="red", + ) + ) + except requests.exceptions.ConnectionError: + self.enqueue_ui( + lambda: login_status.configure(text=f"无法连接服务器,请检查 API 地址: {api_addr}", text_color="red") + ) + except Exception as exc: + self.enqueue_ui(lambda: login_status.configure(text=f"错误: {str(exc)[:80]}", text_color="red")) + + threading.Thread(target=worker, daemon=True).start() + + ctk.CTkButton(login_frame, text="登录并保存云同步凭证", command=do_login, height=40).pack(pady=20) + + reg_frame = tabview.tab("注册") + ctk.CTkLabel(reg_frame, text="用户名", anchor="w").pack(fill="x", padx=20, pady=(15, 0)) + reg_username = ctk.CTkEntry(reg_frame, placeholder_text="3-50 个字符") + reg_username.pack(fill="x", padx=20, pady=5) + ctk.CTkLabel(reg_frame, text="邮箱", anchor="w").pack(fill="x", padx=20, pady=(10, 0)) + reg_email = ctk.CTkEntry(reg_frame, placeholder_text="user@example.com") + reg_email.pack(fill="x", padx=20, pady=5) + ctk.CTkLabel(reg_frame, text="密码", anchor="w").pack(fill="x", padx=20, pady=(10, 0)) + reg_password = ctk.CTkEntry(reg_frame, placeholder_text="至少 8 位", show="*") + reg_password.pack(fill="x", padx=20, pady=5) + reg_status = ctk.CTkLabel(reg_frame, text="", text_color="gray") + reg_status.pack(pady=10) + + def do_register() -> None: + username = reg_username.get().strip() + email = reg_email.get().strip() + password = reg_password.get() + if len(username) < 3: + reg_status.configure(text="用户名至少 3 个字符", text_color="red") + return + if "@" not in email: + reg_status.configure(text="请输入有效邮箱", text_color="red") + return + if len(password) < 8: + reg_status.configure(text="密码至少 8 个字符", text_color="red") + return + reg_status.configure(text="注册中...", text_color="orange") + + def worker() -> None: + api_addr = self.cloud_base_url or DEFAULT_CLOUD_API + try: + resp = requests.post( + f"{api_addr}/api/v1/auth/register", + json={"username": username, "email": email, "password": password}, + timeout=15, + ) + if resp.status_code == 201: + self.enqueue_ui(lambda: reg_status.configure(text="注册成功!请返回登录页登录", text_color="green")) + self.enqueue_ui(lambda: tabview.set("登录")) + elif resp.status_code == 409: + detail = self.extract_api_error(resp) or "用户名或邮箱已被注册" + self.enqueue_ui(lambda: reg_status.configure(text=detail, text_color="red")) + else: + detail = self.extract_api_error(resp) + self.enqueue_ui( + lambda: reg_status.configure( + text=f"注册失败 (HTTP {resp.status_code}):{detail or '请稍后重试'}", + text_color="red", + ) + ) + except requests.exceptions.ConnectionError: + self.enqueue_ui(lambda: reg_status.configure(text=f"无法连接服务器: {api_addr}", text_color="red")) + except Exception as exc: + self.enqueue_ui(lambda: reg_status.configure(text=f"错误: {str(exc)[:80]}", text_color="red")) + + threading.Thread(target=worker, daemon=True).start() + + ctk.CTkButton(reg_frame, text="注册新账号", command=do_register, height=40).pack(pady=20) + + help_frame = tabview.tab("获取 Token") + help_text = """获取 Access Token 的几种方式: + +方式一(推荐):使用上方「登录」页,登录后自动保存云同步凭证 + +方式二(手动): +1. 打开终端/命令行 +2. 执行以下命令(替换 API 地址和账号密码): + +curl -X POST "/api/v1/auth/login" \\ + -H "Content-Type: application/json" \\ + -d '{"username_or_email": "your_email@example.com", "password": "your_password"}' + +3. 从返回的 JSON 中复制 access_token +4. 如需手动接入,可联系开发者补充后台导入入口 + +方式三:联系管理员获取测试 Token(测试阶段可用) + +当前 API 地址:%s +""" % api_url + textbox = ctk.CTkTextbox(help_frame, height=300, wrap="word") + textbox.pack(fill="both", expand=True, padx=20, pady=15) + textbox.insert("1.0", help_text) + textbox.configure(state="disabled") + + btn_frame = ctk.CTkFrame(dialog, fg_color="transparent") + btn_frame.pack(fill="x", padx=20, pady=(0, 15)) + ctk.CTkButton(btn_frame, text="关闭", command=dialog.destroy, width=100).pack(side="right") + + + def extract_api_error(self, response: requests.Response) -> str: + """优先提取后端结构化错误信息,提升提示可读性。""" + try: + payload = response.json() + except Exception: + return response.text[:120].strip() + if isinstance(payload, dict): + data = payload.get("data") + for key in ("detail", "message", "error"): + value = payload.get(key) + if isinstance(value, str) and value.strip(): + return value.strip() + if isinstance(data, dict): + for key in ("detail", "message", "error"): + value = data.get(key) + if isinstance(value, str) and value.strip(): + return value.strip() + return response.text[:120].strip() + + def create_tips_card(self, parent) -> None: + card = self.make_card(parent, "使用说明") + for tip in [ + "Cookie 登录最稳,账号密码登录可能被 Pixiv 额外验证拦截。", + "“开始爬取并预校验”会把可用代理自动写回代理列表。", + "关键词图片会先搜索作品列表,再按数量限制批量下载。", + "开启任务队列后支持多行 / 逗号混合输入与 #标签;作者模式可配合水印只抓新图。", + ]: + ctk.CTkLabel(card, text=f"- {tip}", text_color=self.MUTED_COLOR, font=ctk.CTkFont(size=13), justify="left").pack( + anchor="w", padx=20, pady=(0, 8) + ) + ctk.CTkLabel(card, text="", height=8).pack() + + def create_overview_card(self, parent) -> None: + card = self.make_card(parent, "任务概览") + grid = ctk.CTkFrame(card, fg_color="transparent") + grid.pack(fill="x", padx=16, pady=(2, 18)) + grid.grid_columnconfigure((0, 1), weight=1) + self.works_card = self.create_stat_panel(grid, 0, 0, "作品进度", "0 / 0", self.PRIMARY_COLOR) + self.success_card = self.create_stat_panel(grid, 0, 1, "成功项目", "0", self.SUCCESS_COLOR) + self.pages_card = self.create_stat_panel(grid, 1, 0, "新下载页数", "0", self.WARNING_COLOR) + self.skip_card = self.create_stat_panel(grid, 1, 1, "已跳过页数", "0", self.SECONDARY_COLOR) + + gallery_row = ctk.CTkFrame(card, fg_color="transparent") + gallery_row.pack(fill="x", padx=20, pady=(0, 18)) + self.gallery_button = ctk.CTkButton( + gallery_row, + text="打开缩略图墙", + command=self.open_gallery_wall, + height=42, + width=150, + corner_radius=14, + fg_color=self.SUCCESS_COLOR, + hover_color="#3f9d72", + ) + self.gallery_button.pack(side="left") + ctk.CTkLabel( + gallery_row, + text="浏览本地图库、搜索标签、查看收藏", + text_color=self.MUTED_COLOR, + font=ctk.CTkFont(size=12), + ).pack(side="left", padx=10) + + def create_quick_actions_card(self, parent) -> None: + card = self.make_card(parent, "常用入口", "导入、图库和插件集中在这里。") + grid = ctk.CTkFrame(card, fg_color="transparent") + grid.pack(fill="x", padx=16, pady=(2, 18)) + grid.grid_columnconfigure((0, 1), weight=1) + actions = [ + ("导入 Cookie", "登录态", self.import_cookie_txt), + ("打开图库", "整理内容", self.open_gallery_wall), + ("插件管理", "扩展站点", self.open_plugin_panel), + ] + for index, (title, subtitle, command) in enumerate(actions): + self.create_quick_action_panel(grid, index // 2, index % 2, title, subtitle, command) + + def create_quick_action_panel(self, parent, row: int, column: int, title: str, subtitle: str, command) -> None: + panel = ctk.CTkButton( + parent, + text=f"{title}\n{subtitle}", + command=command, + height=64, + corner_radius=16, + fg_color="#fff7fb", + hover_color="#ffe3ef", + border_width=1, + border_color=self.BORDER_COLOR, + text_color=self.TEXT_COLOR, + font=ctk.CTkFont(size=12, weight="bold"), + anchor="w", + ) + panel.grid(row=row, column=column, sticky="nsew", padx=6, pady=6) + + def create_progress_card(self, parent) -> None: + card = self.make_card(parent, "实时进度") + self.progress_bar = ctk.CTkProgressBar(card, progress_color=self.PRIMARY_COLOR, fg_color=self.SECONDARY_COLOR, corner_radius=999, height=20) + self.progress_bar.pack(fill="x", padx=20, pady=(4, 12)) + self.progress_bar.set(0) + self.progress_label = ctk.CTkLabel(card, text="准备开始", text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=14, weight="bold")) + self.progress_label.pack(anchor="w", padx=20) + self.stats_label = ctk.CTkLabel(card, text="失败项目 0 | 失败页数 0", text_color=self.MUTED_COLOR, font=ctk.CTkFont(size=13)) + self.stats_label.pack(anchor="w", padx=20, pady=(4, 18)) + + def create_log_card(self, parent) -> None: + card = self.make_card(parent, "抓取日志") + card.pack_configure(fill="both", expand=True) + self.log_text = ctk.CTkTextbox( + card, + height=520, + corner_radius=16, + fg_color="#f4f9ff", + border_width=1, + border_color=self.BORDER_COLOR, + text_color=self.TEXT_COLOR, + font=ctk.CTkFont(family="Consolas", size=12), + ) + self.log_text.pack(fill="both", expand=True, padx=20, pady=(0, 20)) + + def create_footer(self) -> None: + footer = ctk.CTkFrame(self.root, fg_color=self.SURFACE_COLOR, corner_radius=0, height=68) + footer.pack(fill="x", side="bottom") + footer.pack_propagate(False) + + row = ctk.CTkFrame(footer, fg_color="transparent") + row.pack(fill="x", expand=True, padx=24, pady=10) + + ctk.CTkLabel( + row, + text="任务控制", + text_color=self.MUTED_COLOR, + font=ctk.CTkFont(size=12), + ).pack(side="left") + + self.start_button = ctk.CTkButton(row, text="开始抓取", command=self.start_download, width=160, height=42, corner_radius=8) + self.start_button.pack(side="right", padx=(8, 0)) + + self.stop_button = ctk.CTkButton( + row, + text="停止任务", + command=self.stop_download, + width=120, + height=42, + corner_radius=8, + fg_color=self.DANGER_COLOR, + state="disabled", + ) + self.stop_button.pack(side="right") + + def create_labeled_entry(self, parent, label_text: str, placeholder: str, show: str | None = None) -> ctk.CTkEntry: + row = ctk.CTkFrame(parent, fg_color="transparent") + row.pack(fill="x", padx=20, pady=(0, 14)) + ctk.CTkLabel(row, text=label_text, text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=14, weight="bold"), width=78).pack( + side="left", padx=(0, 10) + ) + entry = ctk.CTkEntry( + row, + placeholder_text=placeholder, + height=42, + corner_radius=14, + fg_color="#f7fbff", + border_color=self.BORDER_COLOR, + show=show or "", + ) + entry.pack(side="left", fill="x", expand=True) + return entry + + def create_slider_row(self, parent, name, from_, to, steps, default, formatter): + row = ctk.CTkFrame(parent, fg_color="transparent") + row.pack(fill="x", padx=20, pady=(0, 16)) + ctk.CTkLabel(row, text=name, text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=14, weight="bold"), width=78).pack( + side="left", padx=(0, 10) + ) + value_label = ctk.CTkLabel(row, text=formatter(default), text_color=self.PRIMARY_HOVER, font=ctk.CTkFont(size=13, weight="bold"), width=52) + value_label.pack(side="right") + slider = ctk.CTkSlider(row, from_=from_, to=to, number_of_steps=steps, progress_color=self.PRIMARY_COLOR) + slider.pack(side="left", fill="x", expand=True, padx=(0, 12)) + slider.set(default) + slider.configure(command=lambda value: value_label.configure(text=formatter(value))) + return slider, value_label + + def create_stat_panel(self, parent, row, column, title, value, color): + panel = ctk.CTkFrame(parent, fg_color="#f7fbff", corner_radius=18, border_width=1, border_color=self.BORDER_COLOR) + panel.grid(row=row, column=column, sticky="nsew", padx=6, pady=6) + ctk.CTkFrame(panel, width=12, height=12, corner_radius=999, fg_color=color).pack(anchor="w", padx=16, pady=(14, 8)) + ctk.CTkLabel(panel, text=title, text_color=self.MUTED_COLOR, font=ctk.CTkFont(size=13)).pack(anchor="w", padx=16) + label = ctk.CTkLabel(panel, text=value, text_color=self.TEXT_COLOR, font=ctk.CTkFont(size=24, weight="bold")) + label.pack(anchor="w", padx=16, pady=(2, 16)) + return label + + def process_ui_queue(self) -> None: + while True: + try: + callback, args = self.ui_queue.get_nowait() + callback(*args) + except queue.Empty: + break + self.root.after(100, self.process_ui_queue) + + def enqueue_ui(self, callback, *args) -> None: + self.ui_queue.put((callback, args)) + + def log_message(self, message: str) -> None: + self.log_text.insert("end", f"[{time.strftime('%H:%M:%S')}] {message}\n") + self.log_text.see("end") + + def set_inputs_state(self, enabled: bool) -> None: + state = "normal" if enabled else "disabled" + self.target_entry.configure(state=state) + self.proxy_button.configure(state=state) + self.target_mode_menu.configure(state=state) + self.login_mode_menu.configure(state=state) + if hasattr(self, "cookie_import_button"): + self.cookie_import_button.configure(state=state) + self.login_id_entry.configure(state=state) + self.password_entry.configure(state=state) + self.load_session_button.configure(state=state) + self.validate_session_button.configure(state=state) + self.login_button.configure(state=state) + self.save_session_button.configure(state=state) + self.keyword_limit_entry.configure( + state=state if self.TARGET_MODE_MAP.get(self.target_mode_var.get(), "user") == "keyword" else "disabled" + ) + if hasattr(self, "use_queue_checkbox"): + self.use_queue_checkbox.configure(state=state) + if hasattr(self, "incremental_wm_checkbox"): + self.incremental_wm_checkbox.configure(state=state) + if hasattr(self, "cloud_sync_checkbox"): + self.cloud_sync_checkbox.configure(state=state) + if hasattr(self, "cloud_account_btn"): + self.cloud_account_btn.configure(state=state) + if hasattr(self, "cloud_test_btn"): + self.cloud_test_btn.configure(state=state) + if hasattr(self, "platform_menu"): + self.platform_menu.configure(state=state) + if state == "normal": + self.update_crawler_platform_ui() + + def update_login_mode_ui(self, display_mode: str) -> None: + mode = self.LOGIN_MODE_MAP.get(display_mode, "cookie") + self.cookie_frame.pack_forget() + self.password_frame.pack_forget() + self.login_button.configure(state="normal" if mode == "password" else "disabled") + self.validate_session_button.configure(state="normal") + if mode == "cookie": + self.cookie_frame.pack(fill="x", pady=(0, 0)) + else: + self.password_frame.pack(fill="x", pady=(0, 0)) + + def update_crawler_platform_ui(self, _choice: str | None = None) -> None: + platform = self.platform_var.get() + is_pixiv = platform == "Pixiv" + if is_pixiv: + values = list(self.TARGET_MODE_MAP.keys()) + elif platform == "小红书": + values = list(self.XHS_TARGET_MODE_MAP.keys()) + elif platform == "笔趣阁小说": + values = list(self.BIQUGE_TARGET_MODE_MAP.keys()) + elif platform == "长佩文学": + values = list(self.CHANGPEI_TARGET_MODE_MAP.keys()) + elif platform == "插件自动识别": + values = list(self.GENERIC_PLUGIN_MODE_MAP.keys()) + else: + values = list(self.X_TARGET_MODE_MAP.keys()) + self.target_mode_menu.configure(values=values, state="normal") + if self.target_mode_var.get() not in values: + self.target_mode_var.set(values[0]) + self.keyword_row_frame.configure(fg_color="transparent") + if is_pixiv: + self.update_target_mode_ui(self.target_mode_var.get()) + self.use_queue_checkbox.configure(state="normal") + self.incremental_wm_checkbox.configure(state="normal") + else: + self.update_target_mode_ui(self.target_mode_var.get()) + self.use_queue_checkbox.configure(state="disabled") + self.incremental_wm_checkbox.configure(state="disabled") + + def update_target_mode_ui(self, display_mode: str) -> None: + platform = self.platform_var.get() + if platform == "插件自动识别": + self.target_entry.configure(placeholder_text="输入链接、ID 或关键词;程序会自动匹配已导入插件的 can_handle()") + self.keyword_limit_label.configure(text="抓取数量") + self.keyword_limit_entry.configure(state="normal") + self.mode_hint_label.configure( + text="自动解析:从内置插件和用户导入插件中选择第一个 can_handle() 返回 True 的插件。", + text_color=self.MUTED_COLOR, + ) + return + if platform == "笔趣阁小说": + mode = self.BIQUGE_TARGET_MODE_MAP.get(display_mode, "biquge_book") + self.target_entry.configure(placeholder_text=self.BIQUGE_PLACEHOLDER_MAP[mode]) + self.keyword_limit_label.configure(text="候选上限") + self.keyword_limit_entry.configure(state="normal") + self.mode_hint_label.configure( + text="支持直接输入书名或作者进行搜索,也支持粘贴书籍页、章节页和榜单列表页;若命中多本书,会先让你选择再抓取全部章节。", + text_color=self.MUTED_COLOR, + ) + return + if platform == "长佩文学": + mode = self.CHANGPEI_TARGET_MODE_MAP.get(display_mode, "changpei_book") + self.target_entry.configure(placeholder_text=self.CHANGPEI_PLACEHOLDER_MAP[mode]) + self.keyword_limit_label.configure(text="候选上限") + self.keyword_limit_entry.configure(state="normal") + self.mode_hint_label.configure( + text="支持书名/作者搜索,或粘贴长佩书籍页、章节页链接;若命中多本书,会先让你选择再抓取全部章节。", + text_color=self.MUTED_COLOR, + ) + return + if platform == "小红书": + mode = self.XHS_TARGET_MODE_MAP.get(display_mode, "xhs_keyword") + self.target_entry.configure(placeholder_text=self.XHS_PLACEHOLDER_MAP[mode]) + self.keyword_limit_label.configure(text="抓取数量") + self.keyword_limit_entry.configure(state="normal" if mode == "xhs_keyword" else "disabled") + hints = { + "xhs_keyword": "搜索关键词:解析搜索结果里的笔记封面,下载时会进入笔记抓取图片。", + "xhs_note": "单篇笔记:解析一条笔记里的图片,请输入完整小红书笔记链接。", + "xhs_link": "链接解析:支持 xiaohongshu.com 和 xhslink.com 分享链接。", + } + self.mode_hint_label.configure(text=hints[mode], text_color=self.MUTED_COLOR) + return + if platform != "Pixiv": + mode = self.X_TARGET_MODE_MAP.get(display_mode, "x_user") + self.target_entry.configure(placeholder_text=self.X_PLACEHOLDER_MAP[mode]) + self.keyword_limit_label.configure(text="抓取数量") + self.keyword_limit_entry.configure(state="normal" if mode in {"x_user", "x_keyword"} else "disabled") + hints = { + "x_user": "作者媒体:输入作者名会自动访问该作者 /media 页,解析图片和视频。", + "x_status": "单个推文:解析一条推文里的图片或视频,请输入完整推文链接。", + "x_keyword": "搜索词媒体:自动打开 X 搜索媒体页,抓取搜索结果里的图片或视频。", + } + self.mode_hint_label.configure(text=hints[mode], text_color=self.MUTED_COLOR) + return + mode = self.TARGET_MODE_MAP.get(display_mode, "user") + self.keyword_limit_label.configure(text="关键词数量") + self.target_entry.configure(placeholder_text=self.PLACEHOLDER_MAP[mode]) + self.keyword_limit_entry.configure(state="normal" if mode == "keyword" else "disabled") + hints = { + "user": "作者全部作品:支持作者 ID,也支持先粘贴任意作品链接自动反查作者并批量下载。", + "illust": "单个作品:只抓取这一条作品的原图,多图会全部下载。", + "manga": "漫画:抓取漫画作品或多页作品,所有页会自动完整下载。", + "novel": "文章/小说:保存为 TXT 文件,方便后续整理和导出。", + "keyword": "关键词图片:按搜索结果抓图,数量由“关键词数量”控制。", + } + self.mode_hint_label.configure(text=hints[mode]) + + def get_active_cookie(self) -> str: + payload = self.session_store.load() + cookies = self.cookie_json or payload.get("cookie_json") or [] + if cookies: + try: + return cookies_to_header(list(cookies), ("pixiv.net",)) + except Exception: + pass + if self.cookie_header: + return self.cookie_header + return str(payload.get("cookie", "")).strip() + + def save_session(self) -> None: + cookie = self.get_active_cookie() + cloud_config = self.get_cloud_sync_config() + if not cookie and not self.cookie_json and not cloud_config.get("access_token"): + messagebox.showerror("保存失败", "当前没有可保存的 Cookie 或云同步 Token。") + return + payload = { + "login_mode": self.LOGIN_MODE_MAP.get(self.login_mode_var.get(), "cookie"), + "cookie": cookie, + "cookie_json": self.cookie_json, + "login_id": self.login_id_entry.get().strip(), + "cloud_sync": cloud_config, + "saved_at": time.strftime("%Y-%m-%d %H:%M:%S"), + } + self.session_store.save(payload) + self.auth_status_var.set("会话已保存,下次启动可自动载入。") + self.log_message("已保存本地会话。") + + def get_cloud_sync_config(self) -> dict: + return { + "enabled": bool(self.cloud_sync_enabled_var.get()), + "base_url": (self.cloud_base_url or DEFAULT_CLOUD_API).strip(), + "access_token": self.cloud_access_token.strip(), + "refresh_token": self.cloud_refresh_token.strip(), + } + + def set_cookie_indicator(self, text: str) -> None: + self.cookie_import_button.configure(text=text) + + def show_login_warning(self) -> None: + if getattr(self, "_login_notice_window", None) is not None: + try: + self._login_notice_window.focus() + return + except Exception: + self._login_notice_window = None + + pages = [ + ( + "【登录功能说明】", + [ + "当前登录功能仍在优化中,建议使用 cookie 导入方式进行登录。", + "推荐使用浏览器插件(如 Get cookies.txt)导出登录态,并导入本工具使用。", + ], + ), + ( + "【小红书插件说明】", + ["当前小红书插件有些许问题,模拟爬取小红书时,会404 NOT FOUND", + "作者会持续优化,还请耐心等待....."], + ), + ( + "【使用建议】", + [ + "1. 建议在已正常登录 Pixiv / X(Twitter)的浏览器环境下获取 cookie", + "2. 保持浏览器在后台请勿关闭", + "3. cookie 仅用于维持登录状态,请定期更新(例如失效时重新导入)", + "4. 请避免在短时间内进行高频或大规模下载操作,以降低触发平台限制的风险", + ], + ), + ( + "【注意事项】", + [ + "平台可能会对异常访问行为进行限流或验证", + "不建议长时间连续运行批量任务", + "若出现无法访问或登录失效,请重新获取 cookie 后再尝试", + ], + ), + ( + "【用户须知】", + [ + "爬虫运行有风险,请合理使用", + "本工具只做研究学习使用,严谨非法搬运", + "若遇到问题,请联系作者", + "QQ:2811043066", + "使用方法请前往博客", + "博客:http://blog.kunkunxiaomao.top", + ] + ) + ] + + notice_width = 680 + notice_height = 520 + + win = ctk.CTkToplevel(self.root) + self._login_notice_window = win + win.title("用户须知") + win.geometry(f"{notice_width}x{notice_height}") + win.minsize(620, 480) + win.configure(fg_color=self.BG_COLOR) + win.transient(self.root) + win.grab_set() + + def close_notice() -> None: + self._login_notice_window = None + win.destroy() + + win.protocol("WM_DELETE_WINDOW", close_notice) + + outer = ctk.CTkFrame(win, fg_color="transparent") + outer.pack(fill="both", expand=True, padx=20, pady=20) + + card = ctk.CTkFrame( + outer, + fg_color=self.CARD_COLOR, + corner_radius=18, + border_width=1, + border_color=self.BORDER_COLOR, + ) + card.pack(fill="both", expand=True) + + top = ctk.CTkFrame(card, fg_color="transparent") + top.pack(fill="x", padx=24, pady=(20, 8)) + badge = ctk.CTkFrame(top, width=42, height=42, corner_radius=14, fg_color=self.SECONDARY_COLOR) + badge.pack(side="left", padx=(0, 12)) + badge.pack_propagate(False) + ctk.CTkLabel( + badge, + text="!", + text_color=self.TEXT_COLOR, + font=ctk.CTkFont(size=24, weight="bold"), + ).pack(expand=True) + ctk.CTkLabel( + top, + text="用户须知", + text_color=self.TEXT_COLOR, + font=ctk.CTkFont(size=23, weight="bold"), + ).pack(side="left") + + page_label = ctk.CTkLabel(card, text="", text_color=self.MUTED_COLOR, font=ctk.CTkFont(size=12)) + page_label.pack(anchor="w", padx=24, pady=(0, 8)) + + content = ctk.CTkFrame(card, fg_color="#f7fbff", corner_radius=16, border_width=1, border_color=self.BORDER_COLOR) + content.pack(fill="both", expand=True, padx=24, pady=(0, 14)) + + title_label = ctk.CTkLabel( + content, + text="", + text_color=self.TEXT_COLOR, + font=ctk.CTkFont(size=19, weight="bold"), + justify="left", + ) + title_label.pack(anchor="w", padx=20, pady=(16, 8)) + + body_frame = ctk.CTkScrollableFrame( + content, + fg_color="transparent", + scrollbar_button_color=self.SECONDARY_COLOR, + scrollbar_button_hover_color=self.PRIMARY_COLOR, + ) + body_frame.pack(fill="both", expand=True, padx=16, pady=(0, 12)) + + footer = ctk.CTkFrame(card, fg_color="transparent") + footer.pack(fill="x", padx=24, pady=(0, 20)) + + page_index = {"value": 0} + + def render_page() -> None: + title, lines = pages[page_index["value"]] + title_label.configure(text=title) + page_label.configure(text=f"{page_index['value'] + 1} / {len(pages)}") + for child in body_frame.winfo_children(): + child.destroy() + for line in lines: + ctk.CTkLabel( + body_frame, + text=line, + text_color="#365b85", + font=ctk.CTkFont(size=13), + wraplength=560, + justify="left", + ).pack(anchor="w", fill="x", padx=(0, 8), pady=(0, 8)) + prev_button.configure(state="normal" if page_index["value"] > 0 else "disabled") + next_button.configure(text="我知道了" if page_index["value"] == len(pages) - 1 else "下一页") + + def previous_page() -> None: + if page_index["value"] > 0: + page_index["value"] -= 1 + render_page() + + def next_page() -> None: + if page_index["value"] >= len(pages) - 1: + close_notice() + return + page_index["value"] += 1 + render_page() + + prev_button = ctk.CTkButton( + footer, + text="上一页", + command=previous_page, + width=110, + height=38, + corner_radius=12, + fg_color=self.SECONDARY_COLOR, + hover_color="#cce7ff", + text_color=self.TEXT_COLOR, + ) + prev_button.pack(side="left") + next_button = ctk.CTkButton( + footer, + text="下一页", + command=next_page, + width=130, + height=38, + corner_radius=12, + fg_color=self.PRIMARY_COLOR, + hover_color=self.PRIMARY_HOVER, + text_color="#ffffff", + ) + next_button.pack(side="right") + + self.root.update_idletasks() + x = self.root.winfo_x() + max((self.root.winfo_width() - notice_width) // 2, 0) + y = self.root.winfo_y() + max((self.root.winfo_height() - notice_height) // 2, 0) + win.geometry(f"{notice_width}x{notice_height}+{x}+{y}") + render_page() + + def import_cookie_txt(self) -> None: + path = filedialog.askopenfilename( + title="导入 Cookie txt", + filetypes=[ + ("Cookie files", "*.txt *.json"), + ("Text files", "*.txt"), + ("JSON files", "*.json"), + ("All files", "*.*"), + ], + ) + if not path: + return + try: + text = Path(path).read_text(encoding="utf-8-sig") + except UnicodeDecodeError: + text = Path(path).read_text(encoding="utf-8", errors="replace") + try: + cookies = parse_cookie_text(text) + except Exception as exc: + messagebox.showerror("导入失败", str(exc)) + return + + pixiv_cookies = cookies_to_playwright(cookies, ("pixiv.net",)) if has_cookie_domain(cookies, ("pixiv.net",)) else [] + x_cookies = cookies_to_playwright(cookies, ("x.com", "twitter.com")) if has_cookie_domain(cookies, ("x.com", "twitter.com")) else [] + xhs_cookies = ( + cookies_to_playwright(cookies, ("xiaohongshu.com", "xhslink.com")) + if has_cookie_domain(cookies, ("xiaohongshu.com", "xhslink.com")) + else [] + ) + changpei_cookies = cookies_to_playwright(cookies, ("gongzicp.com", "gongzicp.cn")) if has_cookie_domain(cookies, ("gongzicp.com", "gongzicp.cn")) else [] + + self.cookie_json = cookies + self.cookie_header = cookies_to_header(pixiv_cookies, ("pixiv.net",)) if pixiv_cookies else "" + self.login_mode_var.set("Cookie 登录") + self.update_login_mode_ui("Cookie 登录") + summary = cookie_summary(cookies) + if x_cookies: + save_playwright_cookies(x_cookies, runtime_path("x_cookies.json")) + if xhs_cookies: + save_playwright_cookies(xhs_cookies, runtime_path("xiaohongshu_cookies.json")) + if changpei_cookies: + save_playwright_cookies(changpei_cookies, runtime_path("changpei_cookies.json")) + self.set_cookie_indicator(f"已导入 {Path(path).name}({len(cookies)} 个 Cookie)") + self.save_session() + if pixiv_cookies: + self.auth_status_var.set(f"已从 txt 导入 Pixiv Cookie:{summary}") + elif x_cookies: + self.auth_status_var.set(f"已从 txt 导入 X Cookie:{summary};已同步到 runtime/x_cookies.json") + elif xhs_cookies: + self.auth_status_var.set(f"已从 txt 导入小红书 Cookie:{summary};已同步到 runtime/xiaohongshu_cookies.json") + elif changpei_cookies: + self.auth_status_var.set(f"已从 txt 导入长佩 Cookie:{summary};已同步到 runtime/changpei_cookies.json") + else: + self.auth_status_var.set(f"已从 txt 导入 Cookie:{summary}") + self.log_message(f"已导入 Cookie txt: {path} ({summary})") + domain_note = ", ".join(cookie_domains(cookies)[:4]) + extras = [] + if x_cookies: + extras.append("X Cookie 到 runtime/x_cookies.json") + if xhs_cookies: + extras.append("小红书 Cookie 到 runtime/xiaohongshu_cookies.json") + if changpei_cookies: + extras.append("长佩 Cookie 到 runtime/changpei_cookies.json") + extra = ",并已同步 " + ";".join(extras) if extras else "" + messagebox.showinfo("导入完成", f"已解析为 JSON 并保存 Cookie。\n{summary}{extra}\n{domain_note}") + + def load_saved_session(self, silent: bool = False) -> None: + payload = self.session_store.load() + if not payload: + if not silent: + messagebox.showinfo("提示", "本地还没有保存的会话。") + return + login_mode = payload.get("login_mode", "cookie") + display_mode = "账号密码登录" if login_mode == "password" else "Cookie 登录" + self.login_mode_var.set(display_mode) + self.update_login_mode_ui(display_mode) + self.cookie_json = list(payload.get("cookie_json") or []) + self.cookie_header = str(payload.get("cookie", "") or "") if not self.cookie_json else "" + if self.cookie_json: + self.set_cookie_indicator(f"已载入 JSON Cookie({len(self.cookie_json)} 个)") + elif payload.get("cookie"): + self.set_cookie_indicator("已载入旧版 Cookie header") + else: + self.set_cookie_indicator("导入 cookies.txt / json") + self.login_id_entry.delete(0, "end") + self.login_id_entry.insert(0, str(payload.get("login_id", ""))) + cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {} + if hasattr(self, "cloud_sync_enabled_var"): + self.cloud_sync_enabled_var.set(bool(cloud.get("enabled", False))) + self.cloud_base_url = str(cloud.get("base_url") or DEFAULT_CLOUD_API) + self.cloud_access_token = str(cloud.get("access_token") or "") + self.cloud_refresh_token = str(cloud.get("refresh_token") or "") + CloudSession().configure( + base_url=self.cloud_base_url, + access_token=self.cloud_access_token, + refresh_token=self.cloud_refresh_token, + enabled=bool(cloud.get("enabled", False)), + ) + CloudSession().add_listener(self._on_cloud_token_refreshed) + if self.cloud_access_token: + self.cloud_status_var.set("云同步账号已载入,可直接开启自动上传") + else: + self.cloud_status_var.set("未连接云端账号,请点击“账号管理”登录") + self.auth_status_var.set(f"已载入本地会话,保存时间:{payload.get('saved_at', '未知')}") + self.log_message("已自动载入本地会话。") + + def _run_auth_thread(self, worker) -> None: + threading.Thread(target=worker, daemon=True).start() + + def validate_session(self) -> None: + cookie = self.get_active_cookie() + if not cookie: + messagebox.showerror("检测失败", "当前没有 Cookie 可检测。") + return + self.auth_status_var.set("正在检测当前会话...") + + def worker() -> None: + result = self.auth_client.validate_cookie(cookie) + self.enqueue_ui(self.apply_auth_result, result, False) + + self._run_auth_thread(worker) + + def login_with_password(self) -> None: + login_id = self.login_id_entry.get().strip() + password = self.password_entry.get().strip() + if not login_id or not password: + messagebox.showerror("登录失败", "请先填写账号和密码。") + return + self.auth_status_var.set("正在尝试账号密码登录...") + + def worker() -> None: + result = self.auth_client.login_with_password(login_id, password) + self.enqueue_ui(self.apply_auth_result, result, True) + + self._run_auth_thread(worker) + + def apply_auth_result(self, result: AuthResult, from_password_login: bool) -> None: + self.auth_status_var.set(result.message) + self.log_message(result.message) + if result.success and result.cookie: + self.cookie_json = [] + self.cookie_header = result.cookie + self.set_cookie_indicator("账号密码登录已生成 Cookie header") + if from_password_login: + self.login_mode_var.set("Cookie 登录") + self.update_login_mode_ui("Cookie 登录") + self.save_session() + elif from_password_login and result.requires_verification: + messagebox.showinfo("需要额外验证", result.message) + elif not result.success and not from_password_login: + messagebox.showerror("检测失败", result.message) + + def open_plugin_panel(self) -> None: + from pixiv_app.gui.plugin_panel import PluginPanelWindow + + PluginPanelWindow(self.root, cookie=self.get_active_cookie()) + + def open_proxy_dialog(self) -> None: + ProxyDialog(self.root, self.proxy_config, self.save_proxy_config, self.handle_proxy_dialog_action) + + def open_gallery_wall(self) -> None: + try: + url = self.ensure_gallery_service() + webbrowser.open(url) + self.log_message(f"已打开缩略图墙: {url}") + except Exception as exc: + message = f"无法打开缩略图墙: {exc}" + self.log_message(message) + messagebox.showerror("打开失败", message) + + def ensure_gallery_service(self) -> str: + for port in (8765, 8766, 8767): + url = f"http://127.0.0.1:{port}" + if self.is_gallery_wall_ready(url): + self.gallery_url = url + return url + + last_error: Exception | None = None + for port in (8765, 8766, 8767): + url = f"http://127.0.0.1:{port}" + if self.is_port_occupied(url): + continue + try: + errors: list[Exception] = [] + server = GalleryApiServer(host="127.0.0.1", port=port) + + def run_server() -> None: + try: + server.serve_forever() + except Exception as exc: + errors.append(exc) + + thread = threading.Thread( + target=run_server, + daemon=True, + name=f"gallery-api-{port}", + ) + thread.start() + self.gallery_thread = thread + for _ in range(20): + if self.is_gallery_wall_ready(url): + self.gallery_url = url + return url + if errors: + last_error = errors[0] + break + time.sleep(0.1) + except Exception as exc: + last_error = exc + continue + if last_error: + raise last_error + raise RuntimeError("本地图库端口被占用,且没有可用的缩略图墙服务。") + + def is_gallery_wall_ready(self, url: str) -> bool: + try: + with urllib.request.urlopen(url + "/api/health", timeout=1.5) as response: + payload = json.loads(response.read(4096).decode("utf-8", errors="replace")) + return response.status == 200 and payload.get("status") == "ok" + except (OSError, ValueError, urllib.error.URLError): + return False + + def is_port_occupied(self, url: str) -> bool: + try: + parsed = urlparse(url) + with socket.create_connection((parsed.hostname or "127.0.0.1", parsed.port or 80), timeout=0.5): + pass + return True + except OSError: + return False + + def save_proxy_config(self, config: dict) -> None: + self.proxy_config = config + manual_count = len(parse_proxy_text(config.get("manual_text", ""))) + if not config.get("enabled"): + self.proxy_summary_var.set("未启用代理池") + return + summary = f"已启用,列表中 {manual_count} 条" + if config.get("use_quake"): + summary += f" + Quake({config.get('quake_mode', 'api_v3')})" + self.proxy_summary_var.set(summary) + + def format_proxy_text(self, proxies: list[ProxyInfo]) -> str: + lines: list[str] = [] + seen: set[str] = set() + for proxy in proxies: + if proxy.proxy_url not in seen: + seen.add(proxy.proxy_url) + lines.append(proxy.proxy_url) + return "\n".join(lines) + + def handle_proxy_dialog_action(self, action: str, config: dict) -> str | dict[str, str]: + if action != "fetch_preview": + return "未知操作" + manual_text = str(config.get("manual_text", "")).strip() + countries_text = str(config.get("countries_text", "")).strip() + countries = [item.strip().upper() for item in countries_text.split(",") if item.strip()] + pool = ProxyPool() + manual_proxies = parse_proxy_text(manual_text) + if manual_proxies: + pool.add_proxies(manual_proxies) + if config.get("use_quake"): + quake = QuakeClient( + api_key=str(config.get("quake_api_key", "")), + cookie=str(config.get("quake_cookie", "")), + mode=str(config.get("quake_mode", "api_v3")), + ) + pool.add_proxies(quake.get_foreign_proxies(size=80, countries=countries or None)) + total = len(pool.proxies) + if total == 0: + return {"status": "没有找到可校验的代理,请检查输入。", "manual_text": manual_text} + working = pool.verify_all(max_workers=12, max_proxies=min(total, 80)) + merged_text = self.format_proxy_text(working) if working else manual_text + return {"status": f"预校验完成:可用 {len(working)} / 总计 {total},已回填到代理列表。", "manual_text": merged_text} + + def reset_counters(self) -> None: + self.total_works = 0 + self.completed_works = 0 + self.success_works = 0 + self.failed_works = 0 + self.downloaded_pages = 0 + self.skipped_pages = 0 + self.failed_pages = 0 + self.refresh_overview() + + def refresh_overview(self) -> None: + self.works_card.configure(text=f"{self.completed_works} / {self.total_works}") + self.success_card.configure(text=str(self.success_works)) + self.pages_card.configure(text=str(self.downloaded_pages)) + self.skip_card.configure(text=str(self.skipped_pages)) + progress = (self.completed_works / self.total_works) if self.total_works else 0 + self.progress_bar.set(progress) + self.progress_label.configure(text=f"当前进度 {self.completed_works}/{self.total_works}") + self.stats_label.configure(text=f"失败项目 {self.failed_works} | 失败页数 {self.failed_pages}") + + def update_stage_progress(self, text: str, progress: float | None = None) -> None: + if progress is not None: + self.progress_bar.set(max(0.0, min(1.0, float(progress)))) + self.progress_label.configure(text=text) + + def collect_job_config(self) -> dict: + return { + "mode": self.TARGET_MODE_MAP.get(self.target_mode_var.get(), "user"), + "target": self.target_entry.get().strip(), + "cookie": self.get_active_cookie(), + "keyword_limit": self.keyword_limit_entry.get().strip() or "20", + "use_queue": self.use_queue_var.get(), + "incremental_wm": self.incremental_wm_var.get(), + } + + def validate_job_config(self, config: dict) -> dict: + if self.use_queue_var.get(): + return self._validate_job_config_queue(config) + return self._validate_job_config_legacy(config) + + def _validate_job_config_queue(self, config: dict) -> dict: + mode = config["mode"] + target = config["target"].strip() + if not target: + raise ValueError("请输入抓取目标。") + if not str(config["keyword_limit"]).isdigit(): + raise ValueError("关键词数量必须是正整数。") + config["keyword_limit"] = max(1, min(int(config["keyword_limit"]), 100)) + lines, _preview = parse_batch_for_mode(target, ui_mode=mode) + if mode == "keyword": + if not lines: + raise ValueError("请输入关键词(可与 # 标签形式混排)。") + return config + if not lines: + raise ValueError("未解析到有效目标,请检查链接、纯数字 ID 或 #标签。") + if mode == "user": + for line in lines: + if line.category == "novel": + raise ValueError("作者全部作品模式中出现小说链接,请改用文章/小说模式或删除该条目。") + if mode in {"illust", "manga"}: + for line in lines: + if line.category != "illust": + raise ValueError("当前模式下仅支持 Pixiv 作品 ID、链接或未知数字 ID。") + if mode == "novel": + for line in lines: + if line.category != "novel": + raise ValueError("文章/小说模式下请输入小说 ID、链接或未知数字 ID。") + return config + + def _validate_job_config_legacy(self, config: dict) -> dict: + mode = config["mode"] + target = config["target"] + if not target: + raise ValueError("请输入抓取目标。") + if mode == "keyword": + if not str(config["keyword_limit"]).isdigit(): + raise ValueError("关键词数量必须是正整数。") + config["keyword_limit"] = max(1, min(int(config["keyword_limit"]), 100)) + return config + target_kind, target_id = parse_pixiv_target(target) + config["target_kind"] = target_kind + config["target_id"] = target_id + if mode == "user" and target_kind == "novel": + raise ValueError("作者全部作品模式不接受小说链接,请输入作者 ID 或作品链接。") + if mode in {"illust", "manga"} and target_kind not in {"illust", "unknown"}: + raise ValueError("当前模式是作品/漫画,请输入作品 ID 或作品链接。") + if mode == "novel" and target_kind in {"user", "illust"}: + raise ValueError("当前模式是文章/小说,请输入小说 ID 或小说链接。") + return config + + def _plugins_root(self) -> Path: + return plugins_root() + + def _plugin_roots(self) -> list[Path]: + return plugin_roots() + + def _plugin_manager(self) -> PluginManager: + if getattr(self, "_cached_plugin_manager", None) is None: + self._cached_plugin_manager = PluginManager(self._plugin_roots()) + self._cached_plugin_manager.load_all() + return self._cached_plugin_manager + + def _validate_x_url(self) -> None: + text = self.target_entry.get().strip() + if not text: + raise ValueError("请输入 X / Twitter 抓取目标。") + mode = self.X_TARGET_MODE_MAP.get(self.target_mode_var.get(), "x_user") + normalized = self._normalize_x_target(text, mode=mode) + if normalized: + if mode == "x_status" and "/status/" not in normalized.lower(): + raise ValueError("单个推文模式请输入完整推文链接。") + return + x_plugin = self._plugin_manager().plugins.get("X") + if x_plugin is not None and not x_plugin.can_handle(text): + raise ValueError( + "目标格式不符合当前 X 模式;作者模式可填作者名,单个模式填推文链接,搜索模式填关键词。" + ) + + def _normalize_x_target(self, text: str, *, mode: str | None = None) -> str: + mode = mode or self.X_TARGET_MODE_MAP.get(self.target_mode_var.get(), "x_user") + value = text.strip() + if not value: + return "" + lower = value.lower() + if mode == "x_keyword": + return f"https://x.com/search?q={quote(value)}&src=typed_query&f=media" + if mode == "x_status": + return value if ("twitter.com/" in lower or "x.com/" in lower) and "/status/" in lower else "" + if "twitter.com/" in lower or "x.com/" in lower: + if "/status/" in lower: + return "" + if lower.rstrip("/").endswith("/media"): + return value + return value.rstrip("/") + "/media" + if "://" in value: + return value + if value.startswith("@"): + value = value[1:] + if len(value) <= 15 and value.replace("_", "").isalnum(): + return f"https://x.com/{value}/media" + return "" + + def _x_max_items(self) -> int: + value = self.keyword_limit_entry.get().strip() or "20" + if not value.isdigit(): + raise ValueError("抓取数量必须是正整数。") + return max(1, min(int(value), 120)) + + def _validate_xhs_target(self) -> str: + text = self.target_entry.get().strip() + if not text: + raise ValueError("请输入小红书抓取目标。") + mode = self.XHS_TARGET_MODE_MAP.get(self.target_mode_var.get(), "xhs_keyword") + lower = text.lower() + if mode == "xhs_keyword": + if "xiaohongshu.com" in lower or "xhslink.com" in lower: + raise ValueError("搜索关键词模式请输入关键词;小红书链接请切换到“单篇笔记”或“链接解析”。") + return text + if mode == "xhs_note": + if "xiaohongshu.com/explore/" not in lower and "xhslink.com" not in lower and "note_id" not in lower: + raise ValueError("单篇笔记模式请输入小红书笔记链接或 xhslink.com 分享链接。") + return text + if "xiaohongshu.com" not in lower and "xhslink.com" not in lower: + raise ValueError("链接解析模式请输入 xiaohongshu.com 或 xhslink.com 链接。") + return text + + def _xhs_max_items(self) -> int: + value = self.keyword_limit_entry.get().strip() or "20" + if not value.isdigit(): + raise ValueError("抓取数量必须是正整数。") + return max(1, min(int(value), 50)) + + def _validate_biquge_target(self) -> str: + text = self.target_entry.get().strip() + if not text: + raise ValueError("请输入笔趣阁小说名称、作者、书籍链接或列表页链接。") + return text + + def _biquge_max_items(self) -> int: + value = self.keyword_limit_entry.get().strip() or "20" + if not value.isdigit(): + raise ValueError("候选上限必须是正整数。") + return max(1, min(int(value), 50)) + + def _get_biquge_plugin(self): + manager = self._plugin_manager() + manager.reload_all_from_disk() + plugin = manager.plugins.get("笔趣阁") + if plugin is None: + raise ValueError("未找到内置笔趣阁插件,请确认 EXE 内已包含 plugins/biquge。") + return plugin + + def _get_changpei_plugin(self): + manager = self._plugin_manager() + manager.reload_all_from_disk() + plugin = manager.plugins.get("长佩文学") + if plugin is None: + raise ValueError("未找到内置长佩插件,请确认 EXE 内已包含 plugins/changpei。") + changpei_cookie = self._load_changpei_cookie() + if changpei_cookie and hasattr(plugin, "set_auth"): + plugin.set_auth(changpei_cookie) + return plugin + + def _load_changpei_cookie(self) -> str: + """从 runtime/changpei_cookies.json 加载长佩 Cookie 并转为 header 字符串。""" + cookie_path = runtime_path("changpei_cookies.json") + if not cookie_path.is_file(): + return "" + try: + cookies = json.loads(cookie_path.read_text(encoding="utf-8")) + if isinstance(cookies, list) and cookies: + return cookies_to_header(cookies, ("gongzicp.com", "gongzicp.cn")) + except Exception: + pass + return "" + + def _validate_changpei_target(self) -> str: + text = self.target_entry.get().strip() + if not text: + raise ValueError("请输入长佩小说名称、作者、书籍 ID 或链接。") + return text + + def _resolve_biquge_resources(self, plugin, target: str, max_items: int) -> tuple[list[Any], str] | None: + if not hasattr(plugin, "resolve_target"): + raise ValueError("当前笔趣阁插件缺少 resolve_target(),无法选择书籍。") + outcome = plugin.resolve_target(target, limit=max_items) + if not isinstance(outcome, dict): + raise ValueError("笔趣阁插件返回结果格式无效。") + if outcome.get("mode") == "books": + books = list(outcome.get("books") or []) + if not books: + raise ValueError("没有搜索到可选书籍。") + title = str(outcome.get("title") or "选择要抓取的笔趣阁小说") + chosen = BookSelectionDialog(self.root, books, title=title).show() + if chosen is None: + return None + outcome = plugin.resolve_target(str(chosen.get("url") or ""), limit=max_items) + if not isinstance(outcome, dict): + raise ValueError("笔趣阁插件解析所选书籍失败。") + if outcome.get("mode") != "chapters": + raise ValueError("未能解析出章节列表。") + resources = list(outcome.get("resources") or []) + if not resources: + raise ValueError("解析到了书籍,但没有抓取到章节。") + book_title = str(outcome.get("book_title") or resources[0].metadata.get("book_title") or resources[0].title or "").strip() + return resources, book_title + + def _plugin_max_items(self) -> int: + value = self.keyword_limit_entry.get().strip() or "20" + if not value.isdigit(): + raise ValueError("抓取数量必须是正整数。") + return max(1, min(int(value), 200)) + + def start_download(self) -> None: + if self.is_downloading: + return + if self.platform_var.get() == "笔趣阁小说": + try: + plugin = self._get_biquge_plugin() + target = self._validate_biquge_target() + max_items = self._biquge_max_items() + resolved = self._resolve_biquge_resources(plugin, target, max_items) + except ValueError as exc: + messagebox.showerror("输入错误", str(exc)) + return + except Exception as exc: + messagebox.showerror("解析失败", f"笔趣阁插件解析失败:{exc}") + return + if resolved is None: + self.log_message("已取消笔趣阁书籍选择。") + return + resources, book_title = resolved + self.reset_counters() + self.is_downloading = True + self.stop_event.clear() + self.start_button.configure(state="disabled") + self.hero_start_button.configure(state="disabled") + self.stop_button.configure(state="normal") + self.set_inputs_state(False) + self.progress_label.configure(text="正在准备笔趣阁任务...") + self.log_message( + f"准备开始:平台=笔趣阁小说,书籍={book_title or '未命名小说'},章节数={len(resources)},目标={target[:120]}" + ) + self.download_thread = threading.Thread( + target=self.run_download_novel_plugin_thread, + args=("笔趣阁", resources, book_title, "笔趣阁", "biquge"), + daemon=True, + ) + self.download_thread.start() + return + if self.platform_var.get() == "长佩文学": + try: + plugin = self._get_changpei_plugin() + target = self._validate_changpei_target() + max_items = self._biquge_max_items() + resolved = self._resolve_biquge_resources(plugin, target, max_items) + except ValueError as exc: + messagebox.showerror("输入错误", str(exc)) + return + except Exception as exc: + messagebox.showerror("解析失败", f"长佩插件解析失败:{exc}") + return + if resolved is None: + self.log_message("已取消长佩书籍选择。") + return + resources, book_title = resolved + self.reset_counters() + self.is_downloading = True + self.stop_event.clear() + self.start_button.configure(state="disabled") + self.hero_start_button.configure(state="disabled") + self.stop_button.configure(state="normal") + self.set_inputs_state(False) + self.progress_label.configure(text="正在准备长佩任务...") + self.log_message( + f"准备开始:平台=长佩文学,书籍={book_title or '未命名小说'},章节数={len(resources)},目标={target[:120]}" + ) + self.download_thread = threading.Thread( + target=self.run_download_novel_plugin_thread, + args=("长佩文学", resources, book_title, "长佩文学", "changpei"), + daemon=True, + ) + self.download_thread.start() + return + if self.platform_var.get() == "插件自动识别": + target = self.target_entry.get().strip() + if not target: + messagebox.showerror("输入错误", "请输入插件解析目标。") + return + try: + max_items = self._plugin_max_items() + except ValueError as exc: + messagebox.showerror("输入错误", str(exc)) + return + self.reset_counters() + self.is_downloading = True + self.stop_event.clear() + self.start_button.configure(state="disabled") + self.hero_start_button.configure(state="disabled") + self.stop_button.configure(state="normal") + self.set_inputs_state(False) + self.progress_label.configure(text="正在匹配插件...") + self.log_message(f"准备开始:平台=插件自动识别,目标={target[:120]}") + self.download_thread = threading.Thread( + target=self.run_download_generic_plugin_thread, + args=(target, max_items), + daemon=True, + ) + self.download_thread.start() + return + if self.platform_var.get() == "小红书": + try: + target = self._validate_xhs_target() + max_items = self._xhs_max_items() + except ValueError as exc: + messagebox.showerror("输入错误", str(exc)) + return + self.reset_counters() + self.is_downloading = True + self.stop_event.clear() + self.start_button.configure(state="disabled") + self.hero_start_button.configure(state="disabled") + self.stop_button.configure(state="normal") + self.set_inputs_state(False) + self.progress_label.configure(text="正在准备小红书任务...") + self.log_message(f"准备开始:平台=小红书,模式={self.target_mode_var.get()},目标={target[:120]}") + self.download_thread = threading.Thread( + target=self.run_download_xiaohongshu_thread, + args=(target, max_items), + daemon=True, + ) + self.download_thread.start() + return + if self.platform_var.get() != "Pixiv": + try: + self._validate_x_url() + except ValueError as exc: + messagebox.showerror("输入错误", str(exc)) + return + raw_target = self.target_entry.get().strip() + x_mode = self.X_TARGET_MODE_MAP.get(self.target_mode_var.get(), "x_user") + url = self._normalize_x_target(raw_target, mode=x_mode) or raw_target + try: + max_items = self._x_max_items() + except ValueError as exc: + messagebox.showerror("输入错误", str(exc)) + return + self.reset_counters() + self.is_downloading = True + self.stop_event.clear() + self.start_button.configure(state="disabled") + self.hero_start_button.configure(state="disabled") + self.stop_button.configure(state="normal") + self.set_inputs_state(False) + self.progress_label.configure(text="正在准备 X 任务...") + self.log_message(f"准备开始:平台=X / Twitter,模式={self.target_mode_var.get()},目标={url[:120]}") + self.download_thread = threading.Thread(target=self.run_download_x_thread, args=(url, max_items), daemon=True) + self.download_thread.start() + return + + try: + config = self.validate_job_config(self.collect_job_config()) + except ValueError as exc: + messagebox.showerror("输入错误", str(exc)) + return + self.reset_counters() + self.is_downloading = True + self.stop_event.clear() + self.start_button.configure(state="disabled") + self.hero_start_button.configure(state="disabled") + self.stop_button.configure(state="normal") + self.set_inputs_state(False) + self.progress_label.configure(text="正在准备任务...") + self.log_message(f"准备开始:模式={self.target_mode_var.get()},目标={config['target']}") + self.download_thread = threading.Thread(target=self.run_download, args=(config,), daemon=True) + self.download_thread.start() + + def stop_download(self) -> None: + if not self.is_downloading: + return + self.stop_event.set() + self.log_message("已请求停止,正在等待当前任务收尾...") + self.progress_label.configure(text="正在停止任务...") + + def finalize_ui(self) -> None: + self.is_downloading = False + self.start_button.configure(state="normal") + self.hero_start_button.configure(state="normal") + self.stop_button.configure(state="disabled") + self.set_inputs_state(True) + + def handle_result(self, result: DownloadResult, completed: int, total: int) -> None: + self.completed_works = completed + self.total_works = total + self.downloaded_pages += result.downloaded_pages + self.skipped_pages += result.skipped_pages + self.failed_pages += result.failed_pages + if result.ok: + self.success_works += 1 + else: + self.failed_works += 1 + self.refresh_overview() + self.log_message(result.message) + + def handle_finish(self, stopped: bool) -> None: + self.refresh_overview() + self.finalize_ui() + if stopped: + self.progress_label.configure(text="任务已停止") + messagebox.showinfo("任务停止", f"已完成项目 {self.completed_works}/{self.total_works}") + else: + self.progress_label.configure(text="抓取完成") + messagebox.showinfo("抓取完成", f"项目总数 {self.total_works}\n成功项目 {self.success_works}") + + def handle_error(self, message: str) -> None: + self.log_message(message) + self.progress_label.configure(text="任务异常") + self.finalize_ui() + messagebox.showerror("运行错误", message) + + def prepare_proxy_pool(self) -> ProxyPool | None: + manual_text = self.proxy_config.get("manual_text", "") + use_quake = bool(self.proxy_config.get("use_quake")) + countries_text = str(self.proxy_config.get("countries_text", "")).strip() + countries = [item.strip().upper() for item in countries_text.split(",") if item.strip()] + pool = ProxyPool() + manual_proxies = parse_proxy_text(manual_text) + if manual_proxies: + pool.add_proxies(manual_proxies) + pool.verify_all(max_workers=12, max_proxies=80) + self.enqueue_ui(self.log_message, f"手动代理校验完成: 可用 {len(pool.working_proxies)}/{len(manual_proxies)}") + if use_quake: + quake = QuakeClient( + api_key=self.proxy_config.get("quake_api_key", ""), + cookie=self.proxy_config.get("quake_cookie", ""), + mode=self.proxy_config.get("quake_mode", "api_v3"), + ) + pool.collect_until_target( + quake_client=quake, + target_working=5, + stable_rounds=2, + fetch_batch_size=60, + verify_batch_size=40, + max_rounds=6, + max_workers=12, + countries=countries or None, + ) + self.enqueue_ui(self.log_message, f"Quake 抓取并校验后可用代理: {len(pool.working_proxies)}") + if not pool.working_proxies: + self.enqueue_ui(self.log_message, "未获得可用代理,将回退到直连模式。") + return None + return pool + + def _on_cloud_token_refreshed(self, new_access: str, new_refresh: str) -> None: + """Token 刷新回调:成功时更新内存和磁盘;失败时(空 token)清除状态并提示过期。""" + if not new_access: + self.cloud_access_token = "" + self.cloud_refresh_token = "" + self.cloud_sync_enabled_var.set(False) + self.enqueue_ui(self.cloud_status_var.set, "云同步会话已过期,请重新登录云端账号") + self.enqueue_ui(self.log_message, "[云同步] Token 刷新失败,会话已过期,请点击账号管理重新登录。") + return + self.cloud_access_token = new_access + if new_refresh: + self.cloud_refresh_token = new_refresh + self.enqueue_ui(self.cloud_status_var.set, "云同步 Token 已自动续期") + try: + payload = self.session_store.load() + cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {} + cloud["access_token"] = new_access + if new_refresh: + cloud["refresh_token"] = new_refresh + payload["cloud_sync"] = cloud + self.session_store.save(payload) + except Exception: + self.enqueue_ui(self.save_session) + + def run_download(self, config: dict) -> None: + try: + if config.get("use_queue", True): + self.run_download_queue(config) + else: + self.run_download_legacy(config) + except Exception as exc: + self.enqueue_ui(self.handle_error, f"程序运行出错: {exc}") + + def run_download_x_thread(self, url: str, max_items: int = 20) -> None: + """Parse and download via bundled X plugin (Playwright); not using Pixiv SQLite task queue.""" + try: + plugin = self._plugin_manager().plugins.get("X") + if plugin is None or not plugin.validate(): + self.enqueue_ui( + self.handle_error, + "X 插件不可用:请 pip install playwright httpx && playwright install chromium," + "并确认 plugins/x 存在后重启应用或稍后在插件管理中重载。", + ) + return + + self.enqueue_ui(self.log_message, "正在解析 X 页面…") + if hasattr(plugin, "config"): + plugin.config.max_media_items = max_items + resources = plugin.parse(url) + resources = resources[:max_items] + if self.stop_event.is_set(): + self.enqueue_ui(self.handle_finish, True) + return + if not resources: + self.enqueue_ui( + self.handle_error, + "解析结果为空(无媒体、需登录,或 X 临时风控/限流)。请等待 1-3 分钟后重试," + "必要时重新导入 x.com_cookies.txt。", + ) + return + + total = len(resources) + self.enqueue_ui(self.set_total_works, total) + save_root = downloads_root() / "x" + completed = 0 + for i, res in enumerate(resources): + if self.stop_event.is_set(): + break + try: + paths = plugin.download(res, save_root) + ok = len(paths) > 0 + pages = len(res.files) + downloaded = len(paths) + failed_p = 0 if ok else max(1, pages) + msg = f"[X] {res.title[:60]} — 保存文件 {downloaded}/{pages}" + result = DownloadResult( + work_id=i, + total_pages=max(pages, 1), + downloaded_pages=downloaded if ok else 0, + skipped_pages=0, + failed_pages=failed_p if not ok else 0, + ok=ok, + message=msg, + ) + except Exception as exc: + result = DownloadResult( + work_id=i, + total_pages=1, + downloaded_pages=0, + skipped_pages=0, + failed_pages=1, + ok=False, + message=f"[X] 下载失败: {exc}", + ) + completed += 1 + self.enqueue_ui(self.handle_result, result, completed, total) + + self.enqueue_ui(self.handle_finish, self.stop_event.is_set()) + except Exception as exc: + self.enqueue_ui(self.handle_error, f"X 下载出错: {exc}") + + def run_download_xiaohongshu_thread(self, target: str, max_items: int = 20) -> None: + """Parse and download via Xiaohongshu plugin (Playwright).""" + try: + plugin = self._plugin_manager().plugins.get("小红书") + if plugin is None: + self.enqueue_ui(self.handle_error, "小红书插件不可用:未找到 plugins/xiaohongshu。") + return + if not plugin.validate(): + self.enqueue_ui( + self.handle_error, + "小红书插件依赖不可用:请 pip install playwright requests && playwright install chromium。", + ) + return + + if hasattr(plugin, "config"): + plugin.config.max_notes_per_session = max_items + + progress_state = {"base": 0.02, "span": 0.28} + + def on_xhs_progress(message: str, value: float) -> None: + progress = progress_state["base"] + max(0.0, min(1.0, float(value))) * progress_state["span"] + self.enqueue_ui(self.update_stage_progress, message, progress) + + if hasattr(plugin, "progress_callback"): + plugin.progress_callback = on_xhs_progress + + self.enqueue_ui(self.update_stage_progress, "小红书:准备解析", 0.02) + self.enqueue_ui(self.log_message, "正在解析小红书页面…") + resources = plugin.parse(target) + resources = resources[:max_items] + if self.stop_event.is_set(): + self.enqueue_ui(self.handle_finish, True) + return + if not resources: + self.enqueue_ui( + self.handle_error, + "解析结果为空(无图片、需登录,或页面暂时不可访问)。请确认已导入小红书 Cookie 后再试。", + ) + return + + total = len(resources) + self.enqueue_ui(self.set_total_works, total) + self.enqueue_ui(self.update_stage_progress, f"小红书:解析到 {total} 个资源,准备下载", 0.30) + save_root = downloads_root() + completed = 0 + for i, res in enumerate(resources): + if self.stop_event.is_set(): + break + progress_state["base"] = 0.30 + (i / total) * 0.68 + progress_state["span"] = 0.68 / total + self.enqueue_ui(self.update_stage_progress, f"小红书:下载第 {i + 1}/{total} 个资源", progress_state["base"]) + try: + paths = plugin.download(res, save_root) + ok = len(paths) > 0 + pages = len(res.files) if res.files else max(len(paths), 1) + downloaded = len(paths) + failed_p = 0 if ok else max(1, pages) + title = res.title or res.id or f"项目 {i + 1}" + result = DownloadResult( + work_id=i, + total_pages=max(pages, 1), + downloaded_pages=downloaded if ok else 0, + skipped_pages=0, + failed_pages=failed_p if not ok else 0, + ok=ok, + message=f"[小红书] {title[:60]} — 保存文件 {downloaded}/{max(pages, 1)}", + ) + except Exception as exc: + result = DownloadResult( + work_id=i, + total_pages=1, + downloaded_pages=0, + skipped_pages=0, + failed_pages=1, + ok=False, + message=f"[小红书] 下载失败: {exc}", + ) + completed += 1 + self.enqueue_ui(self.handle_result, result, completed, total) + + self.enqueue_ui(self.update_stage_progress, "小红书:任务收尾中", 0.98) + self.enqueue_ui(self.handle_finish, self.stop_event.is_set()) + except Exception as exc: + self.enqueue_ui(self.handle_error, f"小红书下载出错: {exc}") + finally: + try: + if "plugin" in locals() and hasattr(plugin, "progress_callback"): + plugin.progress_callback = None + except Exception: + pass + + def run_download_novel_plugin_thread( + self, + plugin_name: str, + resources: list[Any], + book_title: str = "", + platform_label: str = "", + sync_platform: str = "", + ) -> None: + label = platform_label or plugin_name + sync_key = sync_platform or plugin_name.lower() + try: + manager = self._plugin_manager() + plugin = manager.plugins.get(plugin_name) + if plugin is None: + self.enqueue_ui(self.handle_error, f"{label}插件不可用:未找到对应插件。") + return + total = len(resources) + if total == 0: + self.enqueue_ui(self.handle_error, f"{label}没有可下载的章节。") + return + self.enqueue_ui(self.set_total_works, total) + self.enqueue_ui( + self.update_stage_progress, + f"{label}:已选《{book_title or '未命名小说'}》,准备抓取 {total} 章", + 0.08, + ) + save_root = downloads_root() + completed = 0 + for index, res in enumerate(resources, start=1): + if self.stop_event.is_set(): + break + progress = 0.10 + ((index - 1) / total) * 0.82 + chapter_name = str(res.metadata.get("chapter_title") or res.title or f"第 {index} 章") + self.enqueue_ui( + self.update_stage_progress, + f"{label}:正在抓取 {index}/{total} - {chapter_name[:36]}", + progress, + ) + try: + paths = plugin.download(res, save_root) + ok = len(paths) > 0 + result = DownloadResult( + work_id=index, + total_pages=1, + downloaded_pages=1 if ok else 0, + skipped_pages=0 if ok else 0, + failed_pages=0 if ok else 1, + ok=ok, + message=f"[{label}] {chapter_name[:60]} - {'保存成功' if ok else '保存失败'}", + ) + except Exception as exc: + result = DownloadResult( + work_id=index, + total_pages=1, + downloaded_pages=0, + skipped_pages=0, + failed_pages=1, + ok=False, + message=f"[{label}] {chapter_name[:60]} - 下载失败: {exc}", + ) + completed += 1 + self.enqueue_ui(self.handle_result, result, completed, total) + self.enqueue_ui(self.update_stage_progress, f"{label}:任务收尾中...", 0.98) + self.enqueue_ui(self.handle_finish, self.stop_event.is_set()) + except Exception as exc: + self.enqueue_ui(self.handle_error, f"{label}下载出错: {exc}") + + def run_download_biquge_resources_thread(self, plugin_name: str, resources: list[Any], book_title: str = "") -> None: + self.run_download_novel_plugin_thread(plugin_name, resources, book_title, "笔趣阁", "biquge") + + def run_download_generic_plugin_thread(self, target: str, max_items: int = 20) -> None: + """Auto-match a plugin by can_handle(), then parse and download.""" + try: + manager = self._plugin_manager() + manager.reload_all_from_disk() + plugin = manager.get_plugin_for_url(target) + if plugin is None: + self.enqueue_ui( + self.handle_error, + "没有插件能处理该目标。请在「插件管理」导入插件,并确认插件 can_handle() 能识别该输入。", + ) + return + if not plugin.validate(): + self.enqueue_ui(self.handle_error, f"插件不可用或需配置: {plugin.name}") + return + + self.enqueue_ui(self.log_message, f"已匹配插件:{plugin.name},正在解析…") + resources = plugin.parse(target)[:max_items] + if self.stop_event.is_set(): + self.enqueue_ui(self.handle_finish, True) + return + if not resources: + self.enqueue_ui(self.handle_error, f"插件 {plugin.name} 解析结果为空。") + return + + total = len(resources) + self.enqueue_ui(self.set_total_works, total) + save_root = downloads_root() + completed = 0 + for i, res in enumerate(resources): + if self.stop_event.is_set(): + break + try: + paths = plugin.download(res, save_root) + ok = len(paths) > 0 + pages = len(res.files) if res.files else max(len(paths), 1) + downloaded = len(paths) + title = res.title or res.id or f"项目 {i + 1}" + result = DownloadResult( + work_id=i, + total_pages=max(pages, 1), + downloaded_pages=downloaded if ok else 0, + skipped_pages=0, + failed_pages=0 if ok else max(1, pages), + ok=ok, + message=f"[{plugin.name}] {title[:60]} — 保存文件 {downloaded}/{max(pages, 1)}", + ) + except Exception as exc: + result = DownloadResult( + work_id=i, + total_pages=1, + downloaded_pages=0, + skipped_pages=0, + failed_pages=1, + ok=False, + message=f"[{plugin.name}] 下载失败: {exc}", + ) + completed += 1 + self.enqueue_ui(self.handle_result, result, completed, total) + + self.enqueue_ui(self.handle_finish, self.stop_event.is_set()) + except Exception as exc: + self.enqueue_ui(self.handle_error, f"插件自动解析出错: {exc}") + + def run_download_queue(self, config: dict) -> None: + requested_workers = int(self.thread_slider.get()) + request_delay = float(self.delay_slider.get()) + proxy_pool = self.prepare_proxy_pool() if self.proxy_config.get("enabled") else None + max_workers = resolve_workers(requested_workers, proxy_mode=proxy_pool is not None) + if proxy_pool is not None: + self.enqueue_ui(self.log_message, f"代理模式启用,线程数自动调整为 {max_workers}") + + cookie = config["cookie"] + + def on_progress(result: DownloadResult, completed: int, total: int) -> None: + self.enqueue_ui(self.handle_result, result, completed, total) + + specs, preview, summary = collect_task_specs( + config=config, + cookie=cookie, + proxy_pool=proxy_pool, + incremental_watermark=config.get("incremental_wm", True), + stop_event=self.stop_event, + ) + self.enqueue_ui(self.log_message, summary) + stat_txt = ", ".join(f"{k}:{v}" for k, v in sorted(preview.by_category.items())) or "无" + self.enqueue_ui(self.log_message, f"解析预览: {stat_txt}(输入条数 {preview.total_lines})") + if not specs: + self.enqueue_ui(self.handle_error, "没有生成下载任务(列表为空或被水印过滤)。") + return + + inserted, skipped, requeued_failed, requeued_done, incomplete_left = enqueue_specs_to_library(specs) + self.enqueue_ui( + self.log_message, + f"任务队列: 新插入 {inserted},指纹跳过 {skipped},失败重排队 {requeued_failed}," + f"已完成重跑 {requeued_done},本批未完成 {incomplete_left}", + ) + if incomplete_left == 0 and inserted == 0 and requeued_failed == 0 and requeued_done == 0: + self.enqueue_ui( + self.handle_error, + "本批链接对应的任务均已完成(download_tasks 中为已完成状态)。" + "若要强制重新下载,需先在库中清理对应记录或扩展任务模型。", + ) + return + + if requeued_done > 0: + self.enqueue_ui( + self.log_message, + f"检测到 {requeued_done} 个已完成任务,已自动重新排队,可用于重新下载。", + ) + + if inserted == 0 and (incomplete_left > 0 or requeued_failed > 0 or requeued_done > 0): + self.enqueue_ui( + self.log_message, + "指纹已在队列中;将继续执行未完成 / 失败 / 已完成重跑的任务(无需重复插入)。", + ) + + self.enqueue_ui(self.set_total_works, incomplete_left) + + run_queue_until_idle( + cookie=cookie, + proxy_pool=proxy_pool, + max_workers=max_workers, + request_delay=request_delay, + stop_event=self.stop_event, + progress_callback=on_progress, + poll_ui=None, + progress_total=incomplete_left, + ) + self.enqueue_ui(self.handle_finish, self.stop_event.is_set()) + + def run_download_legacy(self, config: dict) -> None: + try: + requested_workers = int(self.thread_slider.get()) + request_delay = float(self.delay_slider.get()) + proxy_pool = self.prepare_proxy_pool() if self.proxy_config.get("enabled") else None + max_workers = resolve_workers(requested_workers, proxy_mode=proxy_pool is not None) + if proxy_pool is not None: + self.enqueue_ui(self.log_message, f"代理模式启用,线程数自动调整为 {max_workers}") + + mode = config["mode"] + cookie = config["cookie"] + + def on_progress(result: DownloadResult, completed: int, total: int) -> None: + self.enqueue_ui(self.handle_result, result, completed, total) + + if mode == "user": + target_kind = config["target_kind"] + target_id = config["target_id"] + kind_text = {"user": "作者 ID", "illust": "作品 ID", "unknown": "数字 ID", "novel": "小说 ID"} + self.enqueue_ui(self.log_message, f"已识别为{kind_text.get(target_kind, '目标')}: {target_id}") + resolved_user_id, work_ids = fetch_user_work_ids(user_id=target_id, cookie=cookie, proxy_pool=proxy_pool, input_kind=target_kind) + if resolved_user_id != target_id or target_kind == "illust": + self.enqueue_ui(self.log_message, f"已自动解析到作者 ID: {resolved_user_id}") + self.enqueue_ui(self.set_total_works, len(work_ids)) + if not work_ids: + self.enqueue_ui(self.handle_error, "没有获取到可下载的作品。请确认 ID 正确,或尝试提供可用会话。") + return + self.enqueue_ui(self.log_message, f"已获取 {len(work_ids)} 个作品,开始下载。") + download_user_works( + user_id=resolved_user_id, + cookie=cookie, + max_workers=max_workers, + request_delay=request_delay, + stop_event=self.stop_event, + progress_callback=on_progress, + work_ids=work_ids, + proxy_pool=proxy_pool, + ) + elif mode in {"illust", "manga"}: + target_id = config["target_id"] + self.enqueue_ui(self.set_total_works, 1) + result = download_illust( + illust_id=target_id, + user_id=0, + cookie=cookie, + save_subdir=str(Path("single_illust") / str(target_id)), + request_delay=request_delay, + stop_event=self.stop_event, + proxy_pool=proxy_pool, + ) + self.enqueue_ui(self.handle_result, result, 1, 1) + elif mode == "novel": + target_id = config["target_id"] + target_kind = config.get("target_kind") + if target_kind == "novel_series": + series_title, novel_ids = fetch_novel_series_ids( + target_id, + cookie=cookie, + proxy_pool=proxy_pool, + ) + self.enqueue_ui(self.set_total_works, len(novel_ids)) + for index, novel_id in enumerate(novel_ids, start=1): + if self.stop_event.is_set(): + break + result = download_novel( + novel_id=novel_id, + cookie=cookie, + save_subdir="novels", + request_delay=request_delay, + stop_event=self.stop_event, + proxy_pool=proxy_pool, + metadata_overrides={ + "seriesId": str(target_id), + "seriesTitle": series_title or "", + "chapterOrder": index, + }, + ) + self.enqueue_ui(self.handle_result, result, index, len(novel_ids)) + else: + self.enqueue_ui(self.set_total_works, 1) + result = download_novel( + novel_id=target_id, + cookie=cookie, + save_subdir="novels", + request_delay=request_delay, + stop_event=self.stop_event, + proxy_pool=proxy_pool, + ) + self.enqueue_ui(self.handle_result, result, 1, 1) + elif mode == "keyword": + keyword = config["target"] + limit = config["keyword_limit"] + preview_ids = search_illust_ids_by_keyword(keyword, cookie=cookie, proxy_pool=proxy_pool, limit=limit) + self.enqueue_ui(self.set_total_works, len(preview_ids)) + if not preview_ids: + self.enqueue_ui(self.handle_error, f"关键词“{keyword}”没有搜索到可下载作品。") + return + self.enqueue_ui(self.log_message, f"关键词“{keyword}”搜索到 {len(preview_ids)} 个作品,开始下载。") + download_keyword_works( + keyword, + cookie=cookie, + max_workers=max_workers, + request_delay=request_delay, + stop_event=self.stop_event, + progress_callback=on_progress, + proxy_pool=proxy_pool, + limit=limit, + work_ids=preview_ids, + ) + else: + raise PixivRequestError(f"不支持的模式: {mode}") + + self.enqueue_ui(self.handle_finish, self.stop_event.is_set()) + except Exception as exc: + self.enqueue_ui(self.handle_error, f"程序运行出错: {exc}") + + def set_total_works(self, total: int) -> None: + self.total_works = total + self.refresh_overview() + self.progress_label.configure(text="正在抓取...") + + def run(self) -> None: + self.root.mainloop() diff --git a/pixiv_app/gui/plugin_panel.py b/pixiv_app/gui/plugin_panel.py new file mode 100644 index 0000000..45c6920 --- /dev/null +++ b/pixiv_app/gui/plugin_panel.py @@ -0,0 +1,343 @@ +from __future__ import annotations + +import re +import shutil +from collections.abc import Callable +from datetime import datetime +from pathlib import Path +from tkinter import filedialog, messagebox + +import customtkinter as ctk + +from pixiv_app.core.paths import plugin_roots, user_plugins_root +from pixiv_app.core.plugin.base import BasePlugin +from pixiv_app.core.plugin.generator import PluginGenerator +from pixiv_app.core.plugin.manager import PluginManager + + +def _slugify_plugin_name(value: str) -> str: + text = re.sub(r"[^0-9A-Za-z_\-\u4e00-\u9fff]+", "_", value.strip()) + text = text.strip("._-") + return text or "imported_plugin" + + +class PluginPanel(ctk.CTkFrame): + """Generic plugin list, import, reload, and template tools.""" + + def __init__( + self, + parent, + plugin_manager: PluginManager, + *, + user_root: Path | None = None, + text_color: str = "#1e3a5f", + muted_color: str = "#6f8dac", + border_color: str = "#d6e8fb", + danger_color: str = "#eb7d7d", + ) -> None: + super().__init__(parent, fg_color="transparent") + self.manager = plugin_manager + self.user_root = user_root or user_plugins_root() + self._text_color = text_color + self._muted_color = muted_color + self._border_color = border_color + self._danger_color = danger_color + + title_frame = ctk.CTkFrame(self, fg_color="transparent") + title_frame.pack(fill="x", padx=12, pady=(10, 6)) + ctk.CTkLabel( + title_frame, + text="插件管理", + font=ctk.CTkFont(size=18, weight="bold"), + text_color=text_color, + ).pack(side="left") + + btn_row = ctk.CTkFrame(title_frame, fg_color="transparent") + btn_row.pack(side="right") + ctk.CTkButton(btn_row, text="导入 .py", width=88, command=self.import_python_plugin).pack(side="left", padx=4) + ctk.CTkButton(btn_row, text="生成模板", width=88, command=self.open_create_dialog).pack(side="left", padx=4) + ctk.CTkButton(btn_row, text="重新加载", width=88, command=self.reload_all).pack(side="left", padx=4) + + hint = ( + "导入一个 Python 插件文件即可扩展站点;插件需导出 plugin_class," + "并继承 pixiv_app.core.plugin.base.BasePlugin。" + ) + ctk.CTkLabel( + self, + text=hint, + text_color=muted_color, + font=ctk.CTkFont(size=12), + wraplength=760, + justify="left", + ).pack(anchor="w", padx=14, pady=(0, 8)) + + self.summary_var = ctk.StringVar(value="正在读取插件状态...") + ctk.CTkLabel( + self, + textvariable=self.summary_var, + text_color=text_color, + font=ctk.CTkFont(size=12, weight="bold"), + anchor="w", + ).pack(fill="x", padx=14, pady=(0, 8)) + + path_row = ctk.CTkFrame(self, fg_color="transparent") + path_row.pack(fill="x", padx=12, pady=(0, 8)) + ctk.CTkLabel(path_row, text="用户插件目录:", text_color=text_color, font=ctk.CTkFont(size=12, weight="bold")).pack( + side="left" + ) + ctk.CTkLabel(path_row, text=str(self.user_root), text_color=muted_color, font=ctk.CTkFont(size=11)).pack( + side="left", fill="x", expand=True + ) + + self.container = ctk.CTkScrollableFrame(self, fg_color="transparent") + self.container.pack(fill="both", expand=True, padx=8, pady=(0, 8)) + + self.error_container = ctk.CTkFrame(self, fg_color="transparent") + self.error_container.pack(fill="x", padx=8, pady=(0, 8)) + + self.log_text = ctk.CTkTextbox(self, height=120, font=ctk.CTkFont(family="Consolas", size=11)) + self.log_text.pack(fill="x", padx=12, pady=(0, 12)) + + self.refresh() + + def log(self, message: str) -> None: + stamp = datetime.now().strftime("%H:%M:%S") + self.log_text.insert("end", f"[{stamp}] {message}\n") + self.log_text.see("end") + + def refresh(self) -> None: + for child in self.container.winfo_children(): + child.destroy() + for child in self.error_container.winfo_children(): + child.destroy() + if not self.manager.plugins: + ctk.CTkLabel( + self.container, + text="没有发现插件。请点击「导入 .py」或「生成模板」。", + text_color=self._muted_color, + justify="left", + ).pack(anchor="w", pady=8) + for name, plugin in sorted(self.manager.plugins.items(), key=lambda x: x[0].lower()): + self._add_plugin_card(name, plugin) + ready_count = sum(1 for plugin in self.manager.plugins.values() if plugin.validate()) + self.summary_var.set( + f"已加载 {len(self.manager.plugins)} 个插件 · 可用 {ready_count} 个 · " + f"加载失败 {len(self.manager.load_errors)} 个" + ) + self._add_error_cards() + + def _add_error_cards(self) -> None: + if not self.manager.load_errors: + return + title = ctk.CTkLabel( + self.error_container, + text="加载失败的插件", + font=ctk.CTkFont(size=14, weight="bold"), + text_color=self._danger_color, + ) + title.pack(anchor="w", padx=4, pady=(8, 4)) + for path, error in sorted(self.manager.load_errors.items(), key=lambda x: str(x[0]).lower()): + card = ctk.CTkFrame(self.error_container, corner_radius=10, border_width=1, border_color=self._danger_color) + card.pack(fill="x", pady=4) + ctk.CTkLabel( + card, + text=str(path), + font=ctk.CTkFont(size=11), + text_color=self._text_color, + wraplength=760, + justify="left", + ).pack(anchor="w", padx=10, pady=(8, 2)) + ctk.CTkLabel( + card, + text=error, + font=ctk.CTkFont(size=11), + text_color=self._danger_color, + wraplength=760, + justify="left", + ).pack(anchor="w", padx=10, pady=(0, 8)) + + def _add_plugin_card(self, name: str, plugin: BasePlugin) -> None: + card = ctk.CTkFrame(self.container, corner_radius=12, border_width=1, border_color=self._border_color) + card.pack(fill="x", pady=6) + + top = ctk.CTkFrame(card, fg_color="transparent") + top.pack(fill="x", padx=12, pady=(10, 4)) + ctk.CTkLabel(top, text=plugin.name, font=ctk.CTkFont(size=15, weight="bold"), text_color=self._text_color).pack( + side="left" + ) + ctk.CTkLabel(top, text=f"v{plugin.version}", font=ctk.CTkFont(size=12), text_color=self._muted_color).pack( + side="left", padx=10 + ) + is_ready = plugin.validate() + st = "可用" if is_ready else "需配置" + ctk.CTkLabel( + top, + text=st, + font=ctk.CTkFont(size=12, weight="bold"), + text_color="#3f9b72" if is_ready else self._danger_color, + ).pack(side="right") + + path = self.manager.get_plugin_path(name) + source = "用户插件" if path and self.user_root in path.parents else "内置插件" + ctk.CTkLabel( + card, + text=f"域名 {plugin.domain} 来源 {source}", + font=ctk.CTkFont(size=12), + text_color=self._muted_color, + ).pack(anchor="w", padx=12, pady=(0, 4)) + if path: + ctk.CTkLabel( + card, + text=str(path), + font=ctk.CTkFont(size=11), + text_color=self._muted_color, + wraplength=760, + justify="left", + ).pack(anchor="w", padx=12, pady=(0, 8)) + + actions = ctk.CTkFrame(card, fg_color="transparent") + actions.pack(fill="x", padx=12, pady=(0, 10)) + ctk.CTkButton(actions, text="重载", width=72, command=lambda n=name: self.reload_one(n)).pack(side="left", padx=(0, 6)) + if path and self.user_root in path.parents: + ctk.CTkButton( + actions, + text="删除文件", + width=86, + fg_color=self._danger_color, + command=lambda n=name: self.delete_user_plugin(n), + ).pack(side="left") + else: + ctk.CTkButton(actions, text="从内存卸载", width=92, command=lambda n=name: self.unload_one(n)).pack(side="left") + + def import_python_plugin(self) -> None: + path = filedialog.askopenfilename( + title="导入 Python 插件", + filetypes=[("Python plugin", "*.py"), ("All files", "*.*")], + ) + if not path: + return + src = Path(path) + if not src.is_file(): + messagebox.showerror("导入失败", "文件不存在。") + return + + plugin_dir = self.user_root / _slugify_plugin_name(src.stem) + index = 2 + while plugin_dir.exists() and (plugin_dir / "plugin.py").resolve() != src.resolve(): + plugin_dir = self.user_root / f"{_slugify_plugin_name(src.stem)}_{index}" + index += 1 + plugin_dir.mkdir(parents=True, exist_ok=True) + dst = plugin_dir / "plugin.py" + + try: + shutil.copy2(src, dst) + plugin = self.manager.load_plugin(dst) + except Exception as exc: + messagebox.showerror("导入失败", str(exc)) + return + if plugin is None: + messagebox.showerror("导入失败", "插件未能加载。请确认文件中导出了 plugin_class。") + return + + self.refresh() + self.log(f"已导入插件: {plugin.name} -> {dst}") + messagebox.showinfo("导入完成", f"已导入插件:{plugin.name}\n\n{dst}") + + def reload_all(self) -> None: + self.manager.reload_all_from_disk() + self.refresh() + self.log("已重新加载全部插件。") + + def reload_one(self, name: str) -> None: + if self.manager.reload_plugin(name): + self.refresh() + self.log(f"已重载插件: {name}") + else: + self.log(f"重载失败: {name}") + + def unload_one(self, name: str) -> None: + self.manager.unload_plugin(name) + self.refresh() + self.log(f"已从内存卸载: {name}") + + def delete_user_plugin(self, name: str) -> None: + path = self.manager.get_plugin_path(name) + if path is None or self.user_root not in path.parents: + messagebox.showinfo("提示", "只能删除用户导入的插件文件。") + return + if not messagebox.askyesno("确认删除", f"将删除插件目录:\n{path.parent}\n\n是否继续?"): + return + self.manager.unload_plugin(name) + shutil.rmtree(path.parent, ignore_errors=True) + self.manager.reload_all_from_disk() + self.refresh() + self.log(f"已删除用户插件: {name}") + + def open_create_dialog(self) -> None: + CreatePluginDialog(self, self.user_root, on_created=lambda: (self.reload_all(), self.refresh())) + + +class CreatePluginDialog(ctk.CTkToplevel): + def __init__(self, parent, plugins_root: Path, *, on_created: Callable[[], None] | None = None) -> None: + super().__init__(parent) + self.title("生成插件模板") + self.geometry("520x420") + self.plugins_root = plugins_root + self._on_created = on_created + self.generator = PluginGenerator() + + pad = {"padx": 16, "pady": 8} + ctk.CTkLabel(self, text="名称").pack(anchor="w", **pad) + self.name_entry = ctk.CTkEntry(self, placeholder_text="例如 MySite") + self.name_entry.pack(fill="x", **pad) + + ctk.CTkLabel(self, text="域名").pack(anchor="w", **pad) + self.domain_entry = ctk.CTkEntry(self, placeholder_text="例如 example.com") + self.domain_entry.pack(fill="x", **pad) + + ctk.CTkLabel(self, text="作者").pack(anchor="w", **pad) + self.author_entry = ctk.CTkEntry(self) + self.author_entry.pack(fill="x", **pad) + + ctk.CTkLabel(self, text="描述").pack(anchor="w", **pad) + self.desc_text = ctk.CTkTextbox(self, height=100) + self.desc_text.pack(fill="x", **pad) + + ctk.CTkButton(self, text="生成到用户插件目录", command=self._generate).pack(pady=16) + + def _generate(self) -> None: + data = { + "name": self.name_entry.get().strip(), + "domain": self.domain_entry.get().strip(), + "author": self.author_entry.get().strip(), + "description": self.desc_text.get("1.0", "end").strip(), + } + if not data["name"] or not data["domain"]: + messagebox.showerror("校验失败", "请填写名称与域名。") + return + try: + path = self.generator.generate_from_gui(data, output_root=self.plugins_root) + except Exception as exc: + messagebox.showerror("失败", str(exc)) + return + messagebox.showinfo("完成", f"已生成: {path}") + self.destroy() + if self._on_created: + self._on_created() + + +class PluginPanelWindow(ctk.CTkToplevel): + """Standalone window hosting PluginPanel.""" + + def __init__(self, parent, *, cookie: str = "") -> None: + super().__init__(parent) + self.title("插件管理") + self.geometry("760x620") + user_root = user_plugins_root() + self.manager = PluginManager(plugin_roots()) + self.manager.load_all() + pixiv = self.manager.plugins.get("Pixiv") + if pixiv is not None and hasattr(pixiv, "set_auth"): + pixiv.set_auth(cookie) + panel = PluginPanel(self, self.manager, user_root=user_root) + panel.pack(fill="both", expand=True) diff --git a/pixiv_app/gui/proxy_dialog.py b/pixiv_app/gui/proxy_dialog.py new file mode 100644 index 0000000..e749621 --- /dev/null +++ b/pixiv_app/gui/proxy_dialog.py @@ -0,0 +1,160 @@ +from __future__ import annotations + +import threading +from typing import Any, Callable, Optional + +import customtkinter as ctk + + +class ProxyDialog(ctk.CTkToplevel): + def __init__( + self, + master, + initial_config: dict, + on_save: Callable[[dict], None], + on_action: Optional[Callable[[str, dict], str | dict[str, Any]]] = None, + ): + super().__init__(master) + self.title("代理池设置") + self.geometry("760x700") + self.minsize(700, 640) + self.on_save = on_save + self.on_action = on_action + self.initial_config = initial_config + + self.use_proxy_var = ctk.BooleanVar(value=bool(initial_config.get("enabled", False))) + self.use_quake_var = ctk.BooleanVar(value=bool(initial_config.get("use_quake", False))) + self.quake_mode_var = ctk.StringVar(value=str(initial_config.get("quake_mode", "api_v3"))) + self.status_var = ctk.StringVar(value="准备就绪") + + self._build_ui() + self.transient(master) + self.grab_set() + + def _build_ui(self) -> None: + container = ctk.CTkFrame(self, fg_color="transparent") + container.pack(fill="both", expand=True, padx=16, pady=16) + + ctk.CTkLabel(container, text="代理池设置", font=ctk.CTkFont(size=24, weight="bold")).pack(anchor="w", pady=(0, 6)) + ctk.CTkLabel( + container, + text="预校验通过的代理会自动回填到下面的代理列表里,保存后下次可直接使用。", + text_color="#6f8dac", + font=ctk.CTkFont(size=12), + ).pack(anchor="w", pady=(0, 12)) + + toggle_row = ctk.CTkFrame(container, fg_color="#f7fbff", corner_radius=12) + toggle_row.pack(fill="x", pady=(0, 10), padx=2) + ctk.CTkSwitch(toggle_row, text="启用代理池", variable=self.use_proxy_var).pack(anchor="w", padx=12, pady=(8, 6)) + ctk.CTkSwitch(toggle_row, text="启用 Quake 自动爬取", variable=self.use_quake_var).pack(anchor="w", padx=12, pady=(0, 10)) + + mode_row = ctk.CTkFrame(container, fg_color="transparent") + mode_row.pack(fill="x", pady=(0, 8)) + ctk.CTkLabel(mode_row, text="Quake 模式:", width=120).pack(side="left") + ctk.CTkOptionMenu(mode_row, values=["api_v3", "web_assoc"], variable=self.quake_mode_var, width=220).pack(side="left") + + key_row = ctk.CTkFrame(container, fg_color="transparent") + key_row.pack(fill="x", pady=(0, 8)) + ctk.CTkLabel(key_row, text="Quake API Key:", width=120).pack(side="left") + self.api_key_entry = ctk.CTkEntry(key_row, placeholder_text="输入 Quake API Key") + self.api_key_entry.pack(side="left", fill="x", expand=True) + self.api_key_entry.insert(0, str(self.initial_config.get("quake_api_key", ""))) + + cookie_row = ctk.CTkFrame(container, fg_color="transparent") + cookie_row.pack(fill="x", pady=(0, 8)) + ctk.CTkLabel(cookie_row, text="Quake Cookie:", width=120).pack(side="left") + self.cookie_entry = ctk.CTkEntry(cookie_row, placeholder_text="输入 Quake Cookie(web_assoc 模式需要)") + self.cookie_entry.pack(side="left", fill="x", expand=True) + self.cookie_entry.insert(0, str(self.initial_config.get("quake_cookie", ""))) + + country_row = ctk.CTkFrame(container, fg_color="transparent") + country_row.pack(fill="x", pady=(0, 8)) + ctk.CTkLabel(country_row, text="海外国家代码:", width=120).pack(side="left") + self.country_entry = ctk.CTkEntry(country_row, placeholder_text="US,JP,KR,SG,HK,TW,DE,FR,GB,CA,AU") + self.country_entry.pack(side="left", fill="x", expand=True) + self.country_entry.insert(0, str(self.initial_config.get("countries_text", "US,JP,KR,SG,HK,TW,DE,FR,GB,CA,AU"))) + + tool_row = ctk.CTkFrame(container, fg_color="transparent") + tool_row.pack(fill="x", pady=(8, 6)) + self.manual_input = ctk.CTkEntry(tool_row, placeholder_text="快速添加单个代理,如 1.2.3.4:8080") + self.manual_input.pack(side="left", fill="x", expand=True) + ctk.CTkButton(tool_row, text="手动添加", width=110, command=self._add_manual_proxy).pack(side="left", padx=(8, 0)) + self.fetch_button = ctk.CTkButton(tool_row, text="开始爬取并预校验", width=170, command=self._trigger_fetch) + self.fetch_button.pack(side="left", padx=(8, 0)) + + ctk.CTkLabel( + container, + text="代理列表(每行一个,支持 ip:port / http://ip:port / https://ip:port)", + anchor="w", + ).pack(fill="x", pady=(8, 0)) + self.proxy_text = ctk.CTkTextbox(container, height=360) + self.proxy_text.pack(fill="both", expand=True, pady=(6, 12)) + self.proxy_text.insert("1.0", str(self.initial_config.get("manual_text", ""))) + + status_row = ctk.CTkFrame(container, fg_color="#f7fbff", corner_radius=12) + status_row.pack(fill="x", pady=(0, 10)) + ctk.CTkLabel(status_row, textvariable=self.status_var, text_color="#365b85", font=ctk.CTkFont(size=12)).pack( + anchor="w", padx=12, pady=8 + ) + + button_row = ctk.CTkFrame(container, fg_color="transparent") + button_row.pack(fill="x") + ctk.CTkButton(button_row, text="取消", command=self.destroy, width=120).pack(side="right", padx=(8, 0)) + ctk.CTkButton(button_row, text="保存", command=self._save, width=120).pack(side="right") + + def _collect_config(self) -> dict: + return { + "enabled": bool(self.use_proxy_var.get()), + "use_quake": bool(self.use_quake_var.get()), + "quake_mode": self.quake_mode_var.get().strip() or "api_v3", + "quake_api_key": self.api_key_entry.get().strip(), + "quake_cookie": self.cookie_entry.get().strip(), + "manual_text": self.proxy_text.get("1.0", "end").strip(), + "countries_text": self.country_entry.get().strip(), + } + + def _add_manual_proxy(self) -> None: + value = self.manual_input.get().strip() + if not value: + self.status_var.set("请输入代理地址后再添加。") + return + old_text = self.proxy_text.get("1.0", "end").strip() + new_text = f"{old_text}\n{value}".strip() + self.proxy_text.delete("1.0", "end") + self.proxy_text.insert("1.0", new_text) + self.manual_input.delete(0, "end") + self.status_var.set("已加入手动代理列表。") + + def _trigger_fetch(self) -> None: + if not self.on_action: + self.status_var.set("当前版本未启用预校验动作。") + return + self.fetch_button.configure(state="disabled", text="处理中...") + self.status_var.set("正在爬取并校验代理,请稍候...") + + config = self._collect_config() + + def worker() -> None: + try: + result = self.on_action("fetch_preview", config) + except Exception as exc: + result = {"status": f"预校验失败: {exc}"} + self.after(0, lambda: self._finish_fetch(result)) + + threading.Thread(target=worker, daemon=True).start() + + def _finish_fetch(self, result: str | dict[str, Any]) -> None: + self.fetch_button.configure(state="normal", text="开始爬取并预校验") + if isinstance(result, dict): + manual_text = result.get("manual_text") + if isinstance(manual_text, str): + self.proxy_text.delete("1.0", "end") + self.proxy_text.insert("1.0", manual_text) + status = str(result.get("status", "预校验完成")) + self.status_var.set(status) + return + self.status_var.set(str(result)) + + def _save(self) -> None: + self.on_save(self._collect_config()) + self.destroy() diff --git a/pixiv_app/services/__init__.py b/pixiv_app/services/__init__.py new file mode 100644 index 0000000..65f8975 --- /dev/null +++ b/pixiv_app/services/__init__.py @@ -0,0 +1 @@ +"""Local service entry points for Sakura downloader.""" diff --git a/pixiv_app/services/gallery_api.py b/pixiv_app/services/gallery_api.py new file mode 100644 index 0000000..1c8a0bb --- /dev/null +++ b/pixiv_app/services/gallery_api.py @@ -0,0 +1,500 @@ +from __future__ import annotations + +import json +import mimetypes +import time +import uuid +from dataclasses import asdict +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from urllib.parse import parse_qs, urlparse + +from pixiv_app.core.auth import SessionStore +from pixiv_app.core.cookie_import import cookie_summary, cookies_to_header, cookies_to_playwright, parse_cookie_text +from pixiv_app.core.gallery_service import LocalGalleryService +from pixiv_app.core.library import DEFAULT_LIBRARY_DB +from pixiv_app.core.origin_follow_sync import OriginFollowSyncService +from pixiv_app.core.paths import app_session_file, downloads_root, plugin_roots, webui_root +from pixiv_app.core.plugin.manager import PluginManager +from pixiv_app.core.thumbnails import DEFAULT_THUMBNAIL_DIR +from pixiv_app.core.x_media_service import XMediaService + + +class GalleryApiServer: + def __init__( + self, + *, + host: str = "127.0.0.1", + port: int = 8765, + db_path: str | Path = DEFAULT_LIBRARY_DB, + thumbnail_dir: str | Path = DEFAULT_THUMBNAIL_DIR, + web_root: str | Path | None = None, + ) -> None: + self.host = host + self.port = port + self.service = LocalGalleryService(db_path=db_path, thumbnail_dir=thumbnail_dir) + self.x_media_service = XMediaService() + self.origin_follow_sync_service = OriginFollowSyncService( + db_path=str(db_path), + gallery_service=self.service, + x_media_service=self.x_media_service, + ) + self.plugin_manager = PluginManager(plugin_roots()) + self.plugin_manager.load_all() + self.plugin_preview_cache: dict[str, tuple[str, list]] = {} + self.web_root = Path(web_root) if web_root else webui_root() + + def serve_forever(self) -> None: + service = self.service + x_media_service = self.x_media_service + origin_follow_sync_service = self.origin_follow_sync_service + plugin_manager = self.plugin_manager + plugin_preview_cache = self.plugin_preview_cache + web_root = self.web_root + + Handler = type( + "GalleryApiHandlerForServer", + (GalleryApiHandler,), + { + "gallery_service": service, + "x_media_service": x_media_service, + "origin_follow_sync_service": origin_follow_sync_service, + "plugin_manager": plugin_manager, + "plugin_preview_cache": plugin_preview_cache, + "web_root": web_root, + }, + ) + + server = ThreadingHTTPServer((self.host, self.port), Handler) + try: + server.serve_forever() + finally: + server.server_close() + + +class GalleryApiHandler(BaseHTTPRequestHandler): + gallery_service: LocalGalleryService + x_media_service: XMediaService + origin_follow_sync_service: OriginFollowSyncService + plugin_manager: PluginManager + plugin_preview_cache: dict[str, tuple[str, list]] + web_root: Path + + def do_GET(self) -> None: + parsed = urlparse(self.path) + query = parse_qs(parsed.query) + try: + if parsed.path in {"/", "/index.html"}: + self._send_static_file(self.web_root / "index.html") + elif parsed.path.startswith("/assets/"): + self._send_static_file(self.web_root / parsed.path.removeprefix("/assets/")) + elif parsed.path == "/api/health": + self._send_json({"status": "ok"}) + elif parsed.path == "/api/session/status": + self._send_json(self._handle_session_status()) + elif parsed.path == "/api/plugins/pages": + self._send_json(self._handle_plugin_pages()) + elif parsed.path == "/api/artworks": + self._send_json(self._handle_artworks(query)) + elif parsed.path == "/api/favorites": + self._send_json(self.gallery_service.list_favorites(limit=_int(query, "limit", 60), offset=_int(query, "offset", 0))) + elif parsed.path.startswith("/api/artworks/"): + self._send_json(self._handle_artwork_detail(parsed.path)) + elif parsed.path == "/api/tags": + self._send_json(self.gallery_service.list_tags(query=_first(query, "q"), limit=_int(query, "limit", 100))) + elif parsed.path == "/api/artists": + self._send_json(self.gallery_service.list_artists(query=_first(query, "q"), limit=_int(query, "limit", 100))) + elif parsed.path.startswith("/api/artists/"): + self._send_json(self._handle_artist_detail(parsed.path, query)) + elif parsed.path == "/api/followed-artists": + self._send_json(self.gallery_service.list_followed_artists()) + elif parsed.path == "/api/thumbnails/build": + self._send_json({"items": self.gallery_service.build_missing_thumbnails(limit=_int(query, "limit", 200))}) + elif parsed.path == "/api/import/legacy": + self._send_json(self.gallery_service.scan_legacy_downloads(root=_first(query, "root", "Sakura_Downloads"))) + elif parsed.path == "/api/sync/followed": + self._send_json( + self.gallery_service.sync_followed_artists( + max_new_per_artist=_int(query, "max_new", 20), + download=_bool(query, "download", True), + ) + ) + elif parsed.path == "/api/x/local-media": + self._send_json( + self.x_media_service.list_local_media( + limit=_int(query, "limit", 160), + offset=_int(query, "offset", 0), + ) + ) + elif parsed.path == "/api/x/follows": + self._send_json(self.x_media_service.list_followed_authors()) + elif parsed.path.startswith("/media/thumbnail/"): + self._send_file(self._resolve_media_path(parsed.path, thumbnail=True)) + elif parsed.path.startswith("/media/source/"): + self._send_file(self._resolve_media_path(parsed.path, thumbnail=False)) + elif parsed.path.startswith("/media/x-local/"): + self._send_file(self.x_media_service.resolve_local_media(parsed.path.rsplit("/", 1)[-1])) + else: + self._send_json({"error": "not found"}, status=404) + except ValueError as exc: + self._send_json({"error": str(exc)}, status=400) + except Exception as exc: + self._send_json({"error": str(exc)}, status=500) + + def do_POST(self) -> None: + parsed = urlparse(self.path) + try: + payload = self._read_json_body() + if parsed.path == "/api/favorites": + self._send_json( + self.gallery_service.set_favorite( + int(payload["artwork_id"]), + bool(payload.get("favorite", True)), + str(payload.get("note", "")), + ) + ) + elif parsed.path == "/api/followed-artists": + self._send_json( + self.gallery_service.set_followed_artist( + payload.get("artist_id", ""), + bool(payload.get("followed", True)), + str(payload.get("name", "")), + ) + ) + elif parsed.path == "/api/import/legacy": + self._send_json(self.gallery_service.scan_legacy_downloads(root=str(payload.get("root", "Sakura_Downloads")))) + elif parsed.path == "/api/sync/followed": + self._send_json( + self.gallery_service.sync_followed_artists( + cookie=str(payload.get("cookie", "")), + max_new_per_artist=int(payload.get("max_new", 20)), + download=bool(payload.get("download", True)), + ) + ) + elif parsed.path == "/api/cookies/import": + self._send_json(self._handle_cookie_import(payload)) + elif parsed.path == "/api/origin-follows/sync": + self._send_json( + self.origin_follow_sync_service.sync( + str(payload.get("platform", "")), + ) + ) + elif parsed.path == "/api/x/media/preview": + self._send_json( + self.x_media_service.preview_author_media( + str(payload.get("username", "")), + media_type=str(payload.get("media_type", "all")), + max_items=int(payload.get("max_items", 120)), + ) + ) + elif parsed.path == "/api/x/media/download": + selected_ids = payload.get("selected_ids") + if selected_ids is not None and not isinstance(selected_ids, list): + raise ValueError("selected_ids 必须是数组。") + self._send_json( + self.x_media_service.download_preview( + str(payload.get("preview_id", "")), + selected_ids=[str(item) for item in selected_ids] if selected_ids is not None else None, + ) + ) + elif parsed.path == "/api/x/local-media/delete": + media_ids = payload.get("ids", []) + if not isinstance(media_ids, list): + raise ValueError("ids 必须是数组。") + self._send_json(self.x_media_service.delete_local_media([str(item) for item in media_ids])) + elif parsed.path == "/api/x/follows": + self._send_json( + self.x_media_service.set_followed_author( + str(payload.get("username", "")), + followed=bool(payload.get("followed", True)), + creator_name=str(payload.get("name", "")), + ) + ) + elif parsed.path == "/api/x/follows/sync": + self._send_json( + self.x_media_service.sync_followed_authors( + download=bool(payload.get("download", True)), + max_new_per_author=int(payload.get("max_new", 20)), + ) + ) + elif parsed.path == "/api/xiaohongshu/preview": + self._send_json(self._handle_plugin_preview("小红书", payload)) + elif parsed.path == "/api/xiaohongshu/download": + self._send_json(self._handle_plugin_download(payload)) + else: + self._send_json({"error": "not found"}, status=404) + except ValueError as exc: + self._send_json({"error": str(exc)}, status=400) + except Exception as exc: + self._send_json({"error": str(exc)}, status=500) + + def do_OPTIONS(self) -> None: + self.send_response(204) + self._send_cors_headers() + self.end_headers() + + def log_message(self, format: str, *args) -> None: + return None + + def _handle_artworks(self, query: dict[str, list[str]]) -> dict: + artist_id = _first(query, "artist_id") + return self.gallery_service.list_artworks( + query=_first(query, "q"), + search_scope=_first(query, "search_scope") or "all", + tag=_first(query, "tag"), + artist_id=int(artist_id) if artist_id else None, + artwork_type=_first(query, "type"), + include_restricted=_bool(query, "include_restricted", True), + limit=_int(query, "limit", 60), + offset=_int(query, "offset", 0), + ensure_thumbnails=_bool(query, "ensure_thumbnails", True), + ) + + def _handle_plugin_pages(self) -> dict: + self.plugin_manager.reload_all_from_disk() + items = [] + if "X" in self.plugin_manager.plugins: + items.append( + { + "id": "x-media", + "view": "xMedia", + "title": "X 作者媒体", + "description": "按作者名称解析图片或视频并选择下载", + } + ) + if "小红书" in self.plugin_manager.plugins: + plugin = self.plugin_manager.plugins["小红书"] + items.append( + { + "id": "xiaohongshu", + "view": "xiaohongshu", + "title": "小红书", + "description": "搜索关键词或解析笔记链接,预览后下载", + "available": bool(plugin.validate()), + } + ) + return {"items": items} + + def _handle_plugin_preview(self, plugin_name: str, payload: dict) -> dict: + plugin = self.plugin_manager.plugins.get(plugin_name) + if plugin is None: + raise ValueError(f"未安装插件: {plugin_name}") + if not plugin.validate(): + raise ValueError(f"{plugin_name} 插件依赖不可用,请先安装 Playwright。") + target = str(payload.get("target", "")).strip() + if not target: + raise ValueError("请输入解析目标。") + limit = max(1, min(int(payload.get("limit", 20)), 50)) + resources = plugin.parse(target)[:limit] + preview_id = uuid.uuid4().hex + self.plugin_preview_cache[preview_id] = (plugin_name, resources) + return { + "preview_id": preview_id, + "count": len(resources), + "items": [_resource_to_json(item, i) for i, item in enumerate(resources)], + } + + def _handle_plugin_download(self, payload: dict) -> dict: + preview_id = str(payload.get("preview_id", "")) + cached = self.plugin_preview_cache.get(preview_id) + if cached is None: + raise ValueError("预览结果已过期,请重新解析。") + plugin_name, resources = cached + plugin = self.plugin_manager.plugins.get(plugin_name) + if plugin is None: + raise ValueError(f"插件已卸载: {plugin_name}") + selected_ids = payload.get("selected_ids") + selected = set(str(item) for item in selected_ids) if isinstance(selected_ids, list) else set() + if selected: + resources = [item for item in resources if item.id in selected] + save_root = downloads_root() + results = [] + downloaded = 0 + failed = 0 + for resource in resources: + try: + paths = plugin.download(resource, save_root) + downloaded += len(paths) + results.append({ + "id": resource.id, + "ok": bool(paths), + "paths": [str(path) for path in paths], + "synced_files": 0, + }) + except Exception as exc: + failed += 1 + results.append({"id": resource.id, "ok": False, "error": str(exc)}) + return { + "requested": len(resources), + "downloaded_files": downloaded, + "synced_files": 0, + "failed_items": failed, + "items": results, + } + + def _handle_artwork_detail(self, path: str) -> dict: + artwork_id = int(path.rsplit("/", 1)[-1]) + item = self.gallery_service.get_artwork(artwork_id) + if item is None: + raise ValueError(f"Artwork not found: {artwork_id}") + return item + + def _handle_artist_detail(self, path: str, query: dict[str, list[str]]) -> dict: + artist_id = int(path.rsplit("/", 1)[-1]) + item = self.gallery_service.get_artist( + artist_id, + limit=_int(query, "limit", 60), + offset=_int(query, "offset", 0), + ) + if item is None: + raise ValueError(f"Artist not found: {artist_id}") + return item + + def _resolve_media_path(self, path: str, *, thumbnail: bool) -> Path: + parts = path.strip("/").split("/") + if len(parts) != 4: + raise ValueError("Expected /media/{thumbnail|source}/{artwork_id}/{page_index}") + artwork_id = int(parts[2]) + page_index = int(parts[3]) + media_path = ( + self.gallery_service.get_thumbnail_path(artwork_id, page_index=page_index) + if thumbnail + else self.gallery_service.get_source_file_path(artwork_id, page_index=page_index) + ) + if media_path is None: + raise ValueError("Media file not found") + return media_path + + def _handle_cookie_import(self, payload: dict) -> dict: + platform = str(payload.get("platform", "")).strip().lower() + text = str(payload.get("text", "")) + if platform in {"pixiv", "px"}: + cookies = parse_cookie_text(text) + pixiv_cookies = cookies_to_playwright(cookies, ("pixiv.net",)) + header = cookies_to_header(pixiv_cookies, ("pixiv.net",)) + store = SessionStore(app_session_file()) + session_payload = store.load() + session_payload.update( + { + "login_mode": "cookie", + "cookie": header, + "cookie_json": pixiv_cookies, + "login_id": "", + "saved_at": time.strftime("%Y-%m-%d %H:%M:%S"), + } + ) + store.save(session_payload) + return {"platform": "pixiv", "count": len(pixiv_cookies), "summary": cookie_summary(pixiv_cookies)} + if platform in {"x", "twitter"}: + result = self.x_media_service.import_cookie_text(text) + result["platform"] = "x" + return result + raise ValueError("platform 必须是 pixiv 或 x。") + + def _handle_session_status(self) -> dict: + payload = SessionStore(app_session_file()).load() + pixiv_cookie = str(payload.get("cookie") or "").strip() + cookie_json = payload.get("cookie_json") + cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {} + return { + "pixiv": { + "has_cookie": bool(pixiv_cookie or cookie_json), + "saved_at": str(payload.get("saved_at") or ""), + "login_mode": str(payload.get("login_mode") or ""), + }, + "cloud": { + "enabled": bool(cloud.get("enabled")), + "has_token": bool(str(cloud.get("access_token") or "").strip()), + "base_url": str(cloud.get("base_url") or "").strip(), + }, + } + + def _send_json(self, payload: dict | list, *, status: int = 200) -> None: + body = json.dumps(payload, ensure_ascii=False).encode("utf-8") + self.send_response(status) + self.send_header("Content-Type", "application/json; charset=utf-8") + self._send_cors_headers() + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def _send_file(self, path: Path) -> None: + content_type = mimetypes.guess_type(path.name)[0] or "application/octet-stream" + body = path.read_bytes() + self.send_response(200) + self.send_header("Content-Type", content_type) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def _send_static_file(self, path: Path) -> None: + if not path.exists() or not path.is_file(): + self._send_json({"error": "not found"}, status=404) + return + self._send_file(path) + + def _send_cors_headers(self) -> None: + self.send_header("Access-Control-Allow-Origin", "*") + self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") + self.send_header("Access-Control-Allow-Headers", "Content-Type") + + def _read_json_body(self) -> dict: + length = int(self.headers.get("Content-Length", "0") or "0") + if length == 0: + return {} + body = self.rfile.read(length) + payload = json.loads(body.decode("utf-8")) + if not isinstance(payload, dict): + raise ValueError("JSON body must be an object") + return payload + + +def _first(query: dict[str, list[str]], key: str, default: str = "") -> str: + values = query.get(key) + if not values: + return default + return values[0] + + +def _int(query: dict[str, list[str]], key: str, default: int) -> int: + value = _first(query, key) + if not value: + return default + return int(value) + + +def _bool(query: dict[str, list[str]], key: str, default: bool) -> bool: + value = _first(query, key) + if not value: + return default + return value.lower() in {"1", "true", "yes", "on"} + + +def _resource_to_json(resource, index: int) -> dict: + payload = asdict(resource) + payload["index"] = index + payload["kind"] = "video" if any(file.get("type") == "video" for file in resource.files) else "image" + payload["file_count"] = len(resource.files) + return payload + + +def _plugin_platform_slug(plugin_name: str) -> str: + lowered = plugin_name.lower() + if "douyin" in lowered or "抖音" in plugin_name: + return "douyin" + if "xiaohongshu" in lowered or "小红书" in plugin_name: + return "xiaohongshu" + if lowered == "x": + return "x" + return lowered.replace(" ", "_")[:20] or "plugin" + + +def _resource_file_type(resource, index: int, path: Path) -> str: + if index < len(resource.files): + file_type = str(resource.files[index].get("type") or "").lower() + if file_type == "video": + return "video" + mime_type = mimetypes.guess_type(str(path))[0] or "" + return "video" if mime_type.startswith("video/") else "illust" + + diff --git a/pixiv_app/services/run_gallery_api.py b/pixiv_app/services/run_gallery_api.py new file mode 100644 index 0000000..f1bc4a1 --- /dev/null +++ b/pixiv_app/services/run_gallery_api.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +import argparse +import logging + +from pixiv_app.services.gallery_api import GalleryApiServer + + +def main() -> None: + parser = argparse.ArgumentParser(description="Run local Sakura gallery API") + parser.add_argument("--host", default="127.0.0.1") + parser.add_argument("--port", type=int, default=8765) + parser.add_argument("--db", default="pixiv_app_library.db") + parser.add_argument("--thumbnail-dir", default="runtime/thumbnails") + parser.add_argument("--web-root", default="") + args = parser.parse_args() + + logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") + logging.info("Local gallery API listening on http://%s:%s", args.host, args.port) + GalleryApiServer( + host=args.host, + port=args.port, + db_path=args.db, + thumbnail_dir=args.thumbnail_dir, + web_root=args.web_root or None, + ).serve_forever() + + +if __name__ == "__main__": + main() diff --git a/pixiv_app/services/x_auth_manager.py b/pixiv_app/services/x_auth_manager.py new file mode 100644 index 0000000..5786a90 --- /dev/null +++ b/pixiv_app/services/x_auth_manager.py @@ -0,0 +1,356 @@ +""" +X (Twitter) authentication helper — Playwright **persistent context** only. + +- First-time login is always manual (including email verification); no credential stuffing. +- Session persistence is Chromium's user_data_dir (cookies + localStorage live inside it). +- Optional JSON cookie export into ``profiles/x//cookies/`` for backup only. + +Directory layout (created automatically):: + + profiles/ + x/ + / + chromium_profile/ # user_data_dir → persistent session (real storage) + cookies/ # optional Playwright cookie exports (JSON) + localStorage/ # reserved / notes (Chromium keeps LS inside chromium_profile) + +Usage:: + + auth = XAuthManager(profile_key="default") + await auth.start_session() + page = await auth.get_page() + await page.goto("https://x.com/i/flow/login") + # ... user completes login manually ... + if await auth.is_logged_in(): + ... + await auth.close() +""" + +from __future__ import annotations + +import asyncio +import json +import logging +from pathlib import Path +from typing import Any + +from playwright.async_api import BrowserContext, Page, Playwright, async_playwright + +from pixiv_app.core.paths import profiles_root +from pixiv_app.core.playwright_runtime import configure_playwright_runtime_env, preferred_chromium_executable + +logger = logging.getLogger(__name__) + + +def _default_profiles_root() -> Path: + return profiles_root() + + +class XAuthManager: + """ + Manage a long-lived Playwright Chromium **persistent** profile for x.com. + + Not thread-safe: use one instance per logical browser session, or guard externally. + """ + + HOME_URL = "https://x.com/home" + LOGIN_FLOW_HINT = "https://x.com/i/flow/login" + + def __init__( + self, + *, + profile_key: str = "default", + profiles_root: Path | None = None, + headless: bool = True, + auto_reconnect: bool = True, + max_health_retries: int = 2, + ) -> None: + """ + :param profile_key: Subdirectory under ``profiles/x/`` for multi-account switching. + :param profiles_root: Defaults to ``/profiles``. + :param headless: Headless for automated checks; use ``False`` for first manual login. + :param auto_reconnect: If ``start_session`` finds a dead context, restart Playwright. + :param max_health_retries: Retries when validating session after transient failures. + """ + self._profile_key = profile_key + root = profiles_root or _default_profiles_root() + self._profile_home = root / "x" / profile_key + self._user_data_dir = self._profile_home / "chromium_profile" + self._cookies_dir = self._profile_home / "cookies" + self._localstorage_dir = self._profile_home / "localStorage" + + self._headless = headless + self._auto_reconnect = auto_reconnect + self._max_health_retries = max(1, max_health_retries) + + self._pw: Playwright | None = None + self._context: BrowserContext | None = None + self._primary_page: Page | None = None + + self._ensure_storage_layout() + + def _ensure_storage_layout(self) -> None: + """Create profile dirs. Real cookie/localStorage persistence is under ``chromium_profile``.""" + self._user_data_dir.mkdir(parents=True, exist_ok=True) + self._cookies_dir.mkdir(parents=True, exist_ok=True) + self._localstorage_dir.mkdir(parents=True, exist_ok=True) + + @property + def user_data_dir(self) -> Path: + """Playwright ``user_data_dir`` passed to ``launch_persistent_context``.""" + return self._user_data_dir + + def _context_alive(self) -> bool: + if self._context is None: + return False + try: + _ = self._context.pages + return True + except Exception: + return False + + async def start_session(self, *, force_new: bool = False) -> BrowserContext: + """ + Launch or reuse persistent Chromium. Session is restored from disk automatically. + + :param force_new: Close existing context and open a fresh one (same profile dir). + """ + if self._context_alive() and not force_new: + return self._context # type: ignore[return-value] + + if self._context is not None: + await self.close() + + self._pw = await async_playwright().start() + try: + self._context = await self._launch_persistent(self._pw) + except Exception: + await self._pw.stop() + self._pw = None + raise + + self._primary_page = None + logger.info("XAuthManager: persistent context ready (%s)", self._user_data_dir) + return self._context + + async def _launch_persistent(self, pw: Playwright) -> BrowserContext: + """Single place for launch_persistent_context configuration.""" + self._user_data_dir.mkdir(parents=True, exist_ok=True) + configure_playwright_runtime_env() + + base_kw: dict[str, Any] = { + "user_data_dir": str(self._user_data_dir), + "headless": self._headless, + "viewport": {"width": 1280, "height": 800}, + "locale": "zh-CN", + "ignore_default_args": ["--enable-automation"], + "args": ["--disable-blink-features=AutomationControlled"], + "user_agent": ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" + ), + } + + import os + + env_ch = os.environ.get("X_PLAYWRIGHT_CHANNEL", "").strip() + local_executable = preferred_chromium_executable() + attempts: list[dict[str, Any]] = [] + seen: set[str] = set() + for extra in ( + {"channel": env_ch} if env_ch else {}, + {"executable_path": str(local_executable)} if local_executable else {}, + {"channel": "chrome"}, + {}, + ): + key = repr(extra) + if key in seen: + continue + seen.add(key) + attempts.append(extra) + + last_err: Exception | None = None + for extra in attempts: + kw = dict(base_kw) + kw.update(extra) + try: + ctx = await pw.chromium.launch_persistent_context(**kw) + await ctx.add_init_script( + """ + try { + Object.defineProperty(navigator, 'webdriver', { get: () => undefined }); + } catch (e) {} + """ + ) + return ctx + except Exception as exc: + last_err = exc + logger.warning("launch_persistent_context failed (%s): %s", extra, exc) + continue + + assert last_err is not None + raise last_err + + async def get_context(self) -> BrowserContext: + if not self._context_alive(): + if self._auto_reconnect: + return await self.start_session() + raise RuntimeError("No browser session; call start_session() first.") + return self._context # type: ignore[return-value] + + async def get_page(self, *, new: bool = False) -> Page: + """ + Return a page for navigation. Default reuses one tab; set ``new=True`` for a fresh tab. + """ + ctx = await self.get_context() + if new: + return await ctx.new_page() + + if self._primary_page is not None: + try: + _ = self._primary_page.url + return self._primary_page + except Exception: + self._primary_page = None + + if ctx.pages: + self._primary_page = ctx.pages[0] + return self._primary_page + + self._primary_page = await ctx.new_page() + return self._primary_page + + async def is_logged_in(self, *, page: Page | None = None, navigate: bool = True) -> bool: + """ + Heuristic login detection (DOM + URL). Not guaranteed if X changes UI. + + - ``False`` if still on login / signup flow URLs or login UI is visible. + - ``True`` if home timeline / primary column or account UI is present. + """ + p = page if page is not None else await self.get_page() + + if navigate: + await p.goto(self.HOME_URL, wait_until="domcontentloaded", timeout=45_000) + await asyncio.sleep(0.6) + + url = (p.url or "").lower() + if any( + x in url + for x in ( + "/i/flow/login", + "/i/flow/signup", + "/login", + ) + ): + return False + + # Logged-in indicators (best-effort; X changes selectors periodically) + logged_in_selectors = ( + '[data-testid="SideNav_AccountSwitcher_Button"]', + '[data-testid="AppTabBar_Home_Link"]', + '[data-testid="primaryColumn"]', + '[data-testid="tweet"]', + ) + for sel in logged_in_selectors: + try: + el = await p.wait_for_selector(sel, timeout=2500, state="visible") + if el: + return True + except Exception: + continue + + # Login / gate modal text (guest wall) + try: + guest = p.get_by_text( + "Sign in to X", + exact=False, + ) + if await guest.count() and await guest.first.is_visible(timeout=800): + return False + except Exception: + pass + + return False + + async def session_healthy(self) -> bool: + """True if browser context responds and ``is_logged_in`` succeeds within retries.""" + for attempt in range(self._max_health_retries): + try: + if not self._context_alive(): + return False + return await self.is_logged_in(navigate=True) + except Exception as exc: + logger.debug("session_healthy attempt %s: %s", attempt + 1, exc) + await asyncio.sleep(1.0) + return False + + async def export_playwright_cookies(self, filename: str = "playwright_cookies.json") -> Path: + """Optional backup: write context cookies to ``profiles/x//cookies/``.""" + ctx = await self.get_context() + cookies = await ctx.cookies() + path = self._cookies_dir / filename + path.write_text(json.dumps(cookies, indent=2), encoding="utf-8") + return path + + async def close(self) -> None: + """Close Playwright; persistent data remains on disk for next run.""" + if self._primary_page is not None: + try: + await self._primary_page.close() + except Exception: + pass + self._primary_page = None + + if self._context is not None: + try: + await self._context.close() + except Exception: + pass + self._context = None + + if self._pw is not None: + try: + await self._pw.stop() + except Exception: + pass + self._pw = None + + logger.info("XAuthManager: closed (%s)", self._profile_key) + + # --- sync helpers for Tkinter / scripts without asyncio --- + + def start_session_sync(self, *, force_new: bool = False) -> BrowserContext: + return asyncio.run(self.start_session(force_new=force_new)) + + def get_page_sync(self, *, new: bool = False) -> Page: + return asyncio.run(self.get_page(new=new)) + + def is_logged_in_sync(self, *, navigate: bool = True) -> bool: + return asyncio.run(self.is_logged_in(navigate=navigate)) + + def close_sync(self) -> None: + asyncio.run(self.close()) + + +async def _demo() -> None: + logging.basicConfig(level=logging.INFO) + auth = XAuthManager(profile_key="default", headless=False) + try: + await auth.start_session() + page = await auth.get_page() + await page.goto(XAuthManager.LOGIN_FLOW_HINT, wait_until="domcontentloaded") + print( + "浏览器已打开登录流程。请在窗口中手动完成登录(含邮箱验证),完成后回到终端按 Enter…", + ) + await asyncio.to_thread(input) + ok = await auth.is_logged_in() + print("is_logged_in:", ok) + if ok: + path = await auth.export_playwright_cookies() + print("Cookies 备份:", path) + finally: + await auth.close() + + +if __name__ == "__main__": + asyncio.run(_demo()) diff --git a/pixiv_app/services/x_login_flow.py b/pixiv_app/services/x_login_flow.py new file mode 100644 index 0000000..387c5bc --- /dev/null +++ b/pixiv_app/services/x_login_flow.py @@ -0,0 +1,156 @@ +""" +Helpers for X (Twitter) web login flow in Playwright. + +X often opens a combined sheet titled 「注册 X」 even from /i/flow/login; users must switch to +the actual sign-in path or they loop after entering email. These helpers only automate UI clicks +(no credential stuffing, no bypass). +""" + +from __future__ import annotations + +import asyncio +import re + +from playwright.async_api import Page + +LOGIN_URL_X = "https://x.com/i/flow/login" +LOGIN_URL_TWITTER = "https://twitter.com/i/flow/login" + +# When X shows this, login inside Playwright often cannot proceed (rate limit / bot heuristics). +X_FLOW_ERROR_RECOVERY_HINT_CN = ( + "这是 X 端返回的通用错误,常见于自动化浏览器或风控,并不代表本程序损坏。\n\n" + "可行做法(任选):\n" + "1)点击弹窗「重试」后等待几分钟再试,或更换网络(如手机热点);\n" + "2)在插件管理中「清除本 Profile 浏览器数据」后重新「手动登录」;\n" + "3)用本机普通 Chrome 登录 x.com,导出 Cookie 为 JSON,放到 runtime/x_cookies.json,再点「同步到抓取」。\n\n" + "我们无法绕过 X/Google 的风控策略。" +) + + +async def detect_x_generic_flow_error(page: Page) -> bool: + """True if the grey modal 「出错了 / Something went wrong」 is likely visible.""" + try: + dlg = page.locator('[role="dialog"]') + if await dlg.count() > 0: + text = (await dlg.first.inner_text())[:1200] + low = text.lower() + if ("出错了" in text or "something went wrong" in low) and ( + "重试" in text or "reload" in low or "retry" in low or "重新加载" in text + ): + return True + except Exception: + pass + + return False + + +async def prefer_login_over_signup(page: Page, *, max_attempts: int = 5) -> None: + """ + If the modal looks like sign-up (注册 X / Sign up for X), try to switch to login. + + Safe to call multiple times; no-op when login sheet is already shown. + """ + for _ in range(max_attempts): + await asyncio.sleep(0.35) + + # Already looks like login-oriented sheet (best-effort) + try: + login_heading = page.get_by_role("heading", name=re.compile(r"登录.*X|Sign in to X|登录或注册", re.I)) + if await login_heading.count() > 0 and await login_heading.first.is_visible(timeout=400): + return + except Exception: + pass + + signup_visible = False + try: + su = page.get_by_text(re.compile(r"注册 X|Sign up for X"), exact=False) + if await su.count() > 0: + signup_visible = await su.first.is_visible(timeout=500) + except Exception: + pass + + if not signup_visible: + try: + dlg = page.locator('[role="dialog"]') + if await dlg.count() > 0: + t = await dlg.first.inner_text() + if "注册 X" in t or "Sign up for X" in t: + signup_visible = True + except Exception: + pass + + if not signup_visible: + return + + clicked = await _try_click_login_switch(page) + if not clicked: + break + + +async def _try_click_login_switch(page: Page) -> bool: + """Return True if a qualifying control was clicked.""" + + # Known Twitter/X test ids (may change) + for tid in ("signupSheetSwitchToLogin", "switch_to_login"): + try: + loc = page.locator(f'[data-testid="{tid}"]') + if await loc.count() > 0 and await loc.first.is_visible(timeout=600): + await loc.first.click(timeout=3000) + await asyncio.sleep(0.8) + return True + except Exception: + continue + + # Footer / inverse of 「还没有账号?注册」 → often 「登录」 link nearby + try: + inv = page.get_by_text(re.compile(r"已有账号|已有帐户|Already have an account", re.I)) + if await inv.count() > 0: + await inv.first.click(timeout=3500) + await asyncio.sleep(0.85) + return True + except Exception: + pass + + try: + plain = page.get_by_role("link", name=re.compile(r"^(登录|Sign in)$", re.I)) + if await plain.count() > 0: + await plain.first.click(timeout=3500) + await asyncio.sleep(0.85) + return True + except Exception: + pass + + try: + href_login = page.locator('a[href*="login"]').filter(has_not_text=re.compile(r"Google|Apple", re.I)) + if await href_login.count() > 0 and await href_login.first.is_visible(timeout=600): + await href_login.first.click(timeout=3500) + await asyncio.sleep(0.85) + return True + except Exception: + pass + + try: + link = page.locator('a[href="/login"]').first + if await link.is_visible(timeout=600): + await link.click(timeout=3500) + await asyncio.sleep(0.85) + return True + except Exception: + pass + + return False + + +async def goto_x_login_entry(page: Page, *, timeout: float = 90_000) -> None: + """Navigate to official login flow and try to leave sign-up shell.""" + await page.goto(LOGIN_URL_X, wait_until="domcontentloaded", timeout=timeout) + await prefer_login_over_signup(page) + + # Some locales keep showing 「注册 X」 on x.com; twitter.com entry occasionally differs. + try: + su = page.get_by_text(re.compile(r"注册 X|Sign up for X"), exact=False) + if await su.count() > 0 and await su.first.is_visible(timeout=700): + await page.goto(LOGIN_URL_TWITTER, wait_until="domcontentloaded", timeout=timeout) + await prefer_login_over_signup(page) + except Exception: + pass diff --git a/pixiv_app/tasks/__init__.py b/pixiv_app/tasks/__init__.py new file mode 100644 index 0000000..55ed42f --- /dev/null +++ b/pixiv_app/tasks/__init__.py @@ -0,0 +1,19 @@ +"""Task models, SQLite-backed queue, batch parser, and watermark helpers.""" + +from pixiv_app.tasks.models import DownloadTaskSpec, ParsePreview, ParsedLine, TaskPriority +from pixiv_app.tasks.parser import parse_batch_for_mode, split_input_blob +from pixiv_app.tasks.queue import TaskQueue +from pixiv_app.tasks.watermark import advance_watermark, filter_newer_than_watermark, get_last_synced_illust_id + +__all__ = [ + "DownloadTaskSpec", + "ParsePreview", + "ParsedLine", + "TaskPriority", + "TaskQueue", + "advance_watermark", + "filter_newer_than_watermark", + "get_last_synced_illust_id", + "parse_batch_for_mode", + "split_input_blob", +] diff --git a/pixiv_app/tasks/models.py b/pixiv_app/tasks/models.py new file mode 100644 index 0000000..b4ce084 --- /dev/null +++ b/pixiv_app/tasks/models.py @@ -0,0 +1,50 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from enum import IntEnum +from typing import Any, Literal + + +class TaskPriority(IntEnum): + LOW = 1 + MEDIUM = 2 + HIGH = 3 + + +TaskStatus = Literal["pending", "running", "done", "failed"] + + +@dataclass(frozen=True) +class DownloadTaskSpec: + """Serializable unit stored in SQLite via TaskQueue (payload omits secrets).""" + + target_type: Literal["illust", "novel"] + target_id: int + task_kind: str + priority: TaskPriority + page_index: int | None = None + payload: dict[str, Any] = field(default_factory=dict) + + def fingerprint(self) -> str: + page_part = self.page_index if self.page_index is not None else -1 + return f"{self.task_kind}:{self.target_type}:{self.target_id}:{page_part}" + + +@dataclass +class ParsedLine: + """One logical input token after splitting (before expansion to illust ids).""" + + category: Literal["user", "illust", "novel", "tag", "keyword"] + id_value: int + tag: str = "" + keyword_text: str = "" + raw_token: str = "" + pixiv_kind: str = "user" + + +@dataclass +class ParsePreview: + """Statistics for GUI preview.""" + + total_lines: int + by_category: dict[str, int] diff --git a/pixiv_app/tasks/parser.py b/pixiv_app/tasks/parser.py new file mode 100644 index 0000000..378f60b --- /dev/null +++ b/pixiv_app/tasks/parser.py @@ -0,0 +1,89 @@ +from __future__ import annotations + +import re +from typing import Literal + +from pixiv_app.core.downloader import parse_pixiv_target +from pixiv_app.tasks.models import ParsePreview, ParsedLine + + +def split_input_blob(blob: str) -> list[str]: + parts = re.split(r"[\s,,;;\n\r]+", blob.strip()) + return [p for p in parts if p] + + +def parse_batch_for_mode( + raw: str, + *, + ui_mode: Literal["user", "illust", "manga", "novel", "keyword"], +) -> tuple[list[ParsedLine], ParsePreview]: + """ + Parse multi-line / comma / space separated input into structured intents. + + ui_mode matches GUI combo selection so validation stays aligned with existing rules. + """ + tokens = split_input_blob(raw) + lines: list[ParsedLine] = [] + stats: dict[str, int] = {} + + def bump(key: str) -> None: + stats[key] = stats.get(key, 0) + 1 + + if ui_mode == "keyword": + kw = " ".join(tokens) if tokens else raw.strip() + if kw.startswith("#"): + kw = kw[1:].strip() + if kw: + lines.append(ParsedLine(category="keyword", id_value=0, keyword_text=kw, raw_token=raw.strip())) + bump("keyword") + return lines, ParsePreview(total_lines=len(lines), by_category=stats) + + for token in tokens: + t = token.strip() + if not t: + continue + if t.startswith("#"): + tag = t[1:].strip() + if tag: + lines.append(ParsedLine(category="tag", id_value=0, tag=tag, raw_token=t)) + bump("tag") + continue + + try: + kind, num_id = parse_pixiv_target(t) + except ValueError: + continue + + if ui_mode == "novel": + if kind == "novel": + lines.append(ParsedLine(category="novel", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("novel") + elif kind == "novel_series": + lines.append(ParsedLine(category="novel", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("novel") + elif kind == "unknown": + lines.append(ParsedLine(category="novel", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("novel") + continue + + if ui_mode in {"illust", "manga"}: + if kind == "illust": + lines.append(ParsedLine(category="illust", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("illust") + elif kind == "unknown": + lines.append(ParsedLine(category="illust", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("illust") + continue + + # ui_mode == "user" + if kind == "user": + lines.append(ParsedLine(category="user", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("user") + elif kind == "illust": + lines.append(ParsedLine(category="user", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("user") + elif kind == "unknown": + lines.append(ParsedLine(category="user", id_value=num_id, raw_token=t, pixiv_kind=kind)) + bump("user") + + return lines, ParsePreview(total_lines=len(lines), by_category=stats) diff --git a/pixiv_app/tasks/queue.py b/pixiv_app/tasks/queue.py new file mode 100644 index 0000000..8007f7c --- /dev/null +++ b/pixiv_app/tasks/queue.py @@ -0,0 +1,238 @@ +from __future__ import annotations + +import json +import sqlite3 +import threading +import time +from pixiv_app.tasks.models import DownloadTaskSpec + + +class TaskQueue: + """SQLite-backed queue with lease/reclaim; thread-safe for concurrent workers.""" + + def __init__(self, conn: sqlite3.Connection) -> None: + self._conn = conn + self._lock = threading.Lock() + + def enqueue(self, tasks: list[DownloadTaskSpec]) -> tuple[int, int]: + inserted = 0 + with self._lock: + with self._conn: + for t in tasks: + fp = t.fingerprint() + cur = self._conn.execute( + """ + INSERT OR IGNORE INTO download_tasks ( + fingerprint, target_type, target_id, page_index, task_kind, + priority, status, payload_json + ) + VALUES (?, ?, ?, ?, ?, ?, 'pending', ?) + """, + ( + fp, + t.target_type, + t.target_id, + t.page_index, + t.task_kind, + int(t.priority), + json.dumps(t.payload, ensure_ascii=False), + ), + ) + inserted += cur.rowcount + skipped = len(tasks) - inserted + return inserted, skipped + + def requeue_failed_by_fingerprints(self, fingerprints: list[str]) -> int: + """Reset `failed` rows to `pending` so a retry can pick them up.""" + if not fingerprints: + return 0 + placeholders = ",".join("?" * len(fingerprints)) + with self._lock: + with self._conn: + cur = self._conn.execute( + f""" + UPDATE download_tasks + SET status = 'pending', + leased_until = NULL, + worker_id = NULL, + last_error = '', + updated_at = CURRENT_TIMESTAMP + WHERE fingerprint IN ({placeholders}) AND status = 'failed' + """, + fingerprints, + ) + return int(cur.rowcount or 0) + + def requeue_done_by_fingerprints(self, fingerprints: list[str]) -> int: + """Reset completed rows to `pending` so the user can rerun the same task batch.""" + if not fingerprints: + return 0 + placeholders = ",".join("?" * len(fingerprints)) + with self._lock: + with self._conn: + cur = self._conn.execute( + f""" + UPDATE download_tasks + SET status = 'pending', + attempts = 0, + leased_until = NULL, + worker_id = NULL, + last_error = '', + updated_at = CURRENT_TIMESTAMP + WHERE fingerprint IN ({placeholders}) AND status = 'done' + """, + fingerprints, + ) + return int(cur.rowcount or 0) + + def count_incomplete_for_fingerprints(self, fingerprints: list[str]) -> int: + """Rows for these fingerprints that are not successfully finished (`done`).""" + if not fingerprints: + return 0 + placeholders = ",".join("?" * len(fingerprints)) + with self._lock: + row = self._conn.execute( + f""" + SELECT COUNT(*) FROM download_tasks + WHERE fingerprint IN ({placeholders}) AND status != 'done' + """, + fingerprints, + ).fetchone() + return int(row[0]) if row else 0 + + def lease( + self, + worker_id: str, + *, + limit: int = 4, + lease_seconds: float = 180.0, + ) -> list[int]: + """Grab up to `limit` tasks; reclaims expired leases.""" + now = time.time() + deadline = now + lease_seconds + ids: list[int] = [] + with self._lock: + with self._conn: + for _ in range(limit): + cur = self._conn.execute( + """ + WITH picked AS ( + SELECT id FROM download_tasks + WHERE status = 'pending' + OR (status = 'running' AND leased_until IS NOT NULL AND leased_until < ?) + ORDER BY priority DESC, id ASC + LIMIT 1 + ) + UPDATE download_tasks + SET status = 'running', + worker_id = ?, + leased_until = ?, + updated_at = CURRENT_TIMESTAMP + WHERE id IN (SELECT id FROM picked) + RETURNING id + """, + (now, worker_id, deadline), + ) + row = cur.fetchone() + if row is None: + break + ids.append(int(row[0])) + return ids + + def load_task(self, task_id: int) -> sqlite3.Row | None: + with self._lock: + row = self._conn.execute( + "SELECT * FROM download_tasks WHERE id = ?", + (task_id,), + ).fetchone() + return row + + def mark_done(self, task_id: int) -> None: + with self._lock: + with self._conn: + self._conn.execute( + """ + UPDATE download_tasks + SET status = 'done', + leased_until = NULL, + worker_id = NULL, + last_error = '', + updated_at = CURRENT_TIMESTAMP + WHERE id = ? + """, + (task_id,), + ) + + def mark_failed_final(self, task_id: int, message: str) -> None: + with self._lock: + with self._conn: + self._conn.execute( + """ + UPDATE download_tasks + SET status = 'failed', + leased_until = NULL, + worker_id = NULL, + last_error = ?, + updated_at = CURRENT_TIMESTAMP + WHERE id = ? + """, + (message[:2000], task_id), + ) + + def requeue_retry(self, task_id: int, message: str, max_attempts: int = 4) -> bool: + """Return True if scheduled for another attempt.""" + with self._lock: + with self._conn: + row = self._conn.execute( + "SELECT attempts FROM download_tasks WHERE id = ?", + (task_id,), + ).fetchone() + if row is None: + return False + attempts = int(row[0]) + 1 + if attempts >= max_attempts: + self._conn.execute( + """ + UPDATE download_tasks + SET status = 'failed', + attempts = ?, + leased_until = NULL, + worker_id = NULL, + last_error = ?, + updated_at = CURRENT_TIMESTAMP + WHERE id = ? + """, + (attempts, message[:2000], task_id), + ) + return False + self._conn.execute( + """ + UPDATE download_tasks + SET status = 'pending', + attempts = ?, + leased_until = NULL, + worker_id = NULL, + last_error = ?, + updated_at = CURRENT_TIMESTAMP + WHERE id = ? + """, + (attempts, message[:2000], task_id), + ) + return True + + def count_by_status(self, status: str) -> int: + with self._lock: + row = self._conn.execute( + "SELECT COUNT(*) FROM download_tasks WHERE status = ?", + (status,), + ).fetchone() + return int(row[0]) if row else 0 + + def snapshot_counts(self) -> dict[str, int]: + with self._lock: + rows = self._conn.execute( + """ + SELECT status, COUNT(*) FROM download_tasks GROUP BY status + """ + ).fetchall() + return {str(r[0]): int(r[1]) for r in rows} diff --git a/pixiv_app/tasks/watermark.py b/pixiv_app/tasks/watermark.py new file mode 100644 index 0000000..2825fb9 --- /dev/null +++ b/pixiv_app/tasks/watermark.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +import sqlite3 + + +def get_last_synced_illust_id(conn: sqlite3.Connection, artist_id: int) -> int: + row = conn.execute( + "SELECT last_synced_illust_id FROM artist_sync_watermark WHERE artist_id = ?", + (artist_id,), + ).fetchone() + return int(row[0]) if row else 0 + + +def advance_watermark(conn: sqlite3.Connection, artist_id: int, illust_id: int) -> None: + """Raise watermark to at least illust_id for incremental author sync.""" + with conn: + conn.execute( + """ + INSERT INTO artist_sync_watermark (artist_id, last_synced_illust_id, updated_at) + VALUES (?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(artist_id) DO UPDATE SET + last_synced_illust_id = MAX( + artist_sync_watermark.last_synced_illust_id, + excluded.last_synced_illust_id + ), + updated_at = CURRENT_TIMESTAMP + """, + (artist_id, illust_id), + ) + + +def filter_newer_than_watermark(work_ids: list[int], watermark: int) -> list[int]: + """Keep illust ids strictly greater than last synced watermark (Pixiv ids grow over time).""" + return [wid for wid in work_ids if wid > watermark] diff --git a/pixiv_app/webui/app.js b/pixiv_app/webui/app.js new file mode 100644 index 0000000..7006546 --- /dev/null +++ b/pixiv_app/webui/app.js @@ -0,0 +1,947 @@ +const state = { + view: "home", + query: "", + tag: "", + type: "", + artistId: "", + offset: 0, + limit: 48, + total: 0, + xPreviewId: "", + xPreviewUsername: "", + xPreviewAuthor: "", + xhsPreviewId: "", +}; + +const els = { + search: document.querySelector("#searchInput"), + type: document.querySelector("#typeFilter"), + status: document.querySelector("#statusLine"), + grid: document.querySelector("#artworkGrid"), + tags: document.querySelector("#tagList"), + artists: document.querySelector("#artistList"), + followed: document.querySelector("#followedList"), + xFollowed: document.querySelector("#xFollowedList"), + title: document.querySelector("#viewTitle"), + subtitle: document.querySelector("#viewSubtitle"), + pageInfo: document.querySelector("#pageInfo"), + dialog: document.querySelector("#detailDialog"), + detail: document.querySelector("#detailContent"), + pixivCookieFile: document.querySelector("#pixivCookieFile"), + pixivSessionStatus: document.querySelector("#pixivSessionStatus"), + refreshSessionButton: document.querySelector("#refreshSessionButton"), + xCookieFile: document.querySelector("#xCookieFile"), + xUsername: document.querySelector("#xUsernameInput"), + xMediaType: document.querySelector("#xMediaType"), + xMaxItems: document.querySelector("#xMaxItems"), + xMediaGrid: document.querySelector("#xMediaGrid"), + xMediaSummary: document.querySelector("#xMediaSummary"), + xSelectAll: document.querySelector("#xSelectAll"), + xFollowUsername: document.querySelector("#xFollowUsername"), + xFollowName: document.querySelector("#xFollowName"), + xLocalGrid: document.querySelector("#xLocalGrid"), + xLocalSummary: document.querySelector("#xLocalSummary"), + xLocalSelectAll: document.querySelector("#xLocalSelectAll"), + xhsNavButton: document.querySelector("#xiaohongshuNavButton"), + xhsTarget: document.querySelector("#xhsTargetInput"), + xhsLimit: document.querySelector("#xhsLimitInput"), + xhsGrid: document.querySelector("#xhsGrid"), + xhsSummary: document.querySelector("#xhsSummary"), + xhsSelectAll: document.querySelector("#xhsSelectAll"), + originFollowSyncDialog: document.querySelector("#originFollowSyncDialog"), + originFollowSyncResult: document.querySelector("#originFollowSyncResult"), +}; + +function setStatus(text) { + els.status.textContent = text; +} + +function isPlaceholderPixivName(name, artistId) { + const value = String(name || "").trim(); + if (!value) { + return true; + } + const idText = String(artistId || "").trim(); + return Boolean(idText) && value.toLowerCase() === `artist_${idText}`.toLowerCase(); +} + +function pixivArtistStoredName(item) { + const artistId = item?.artist_id ?? item?.artistId ?? item?.creator_id ?? ""; + const name = String(item?.name || item?.artist_name || item?.creator_name || "").trim(); + return isPlaceholderPixivName(name, artistId) ? "" : name; +} + +function pixivArtistDisplayName(item) { + const artistId = item?.artist_id ?? item?.artistId ?? item?.creator_id ?? ""; + const name = pixivArtistStoredName(item); + if (name) { + return name; + } + const account = String(item?.account || item?.artist_account || item?.creator_handle || "").trim().replace(/^@/, ""); + if (account) { + return `@${account}`; + } + return artistId ? `Pixiv ID ${artistId}` : "Pixiv 作者"; +} + +window.addEventListener("unhandledrejection", (event) => { + setStatus(event.reason?.message || "操作失败,请查看后端日志。"); +}); + +async function api(path, options = {}) { + const response = await fetch(path, { + headers: { "Content-Type": "application/json" }, + ...options, + }); + const payload = await response.json(); + if (!response.ok || payload.error) { + throw new Error(payload.error || `HTTP ${response.status}`); + } + return payload; +} + +async function loadSessionStatus() { + if (!els.pixivSessionStatus) { + return null; + } + try { + const payload = await api("/api/session/status"); + const pixiv = payload.pixiv || {}; + const cookieText = pixiv.has_cookie + ? `已共享桌面端 Pixiv 会话${pixiv.saved_at ? `(${pixiv.saved_at})` : ""}` + : "未检测到桌面端 Pixiv 会话,请先在 EXE 登录或导入 Cookie。"; + els.pixivSessionStatus.textContent = cookieText; + return payload; + } catch (error) { + els.pixivSessionStatus.textContent = `会话状态读取失败:${error.message || error}`; + return null; + } +} + +function artworkUrl(item, pageIndex = 0, kind = "thumbnail") { + return `/media/${kind}/${item.artwork_id}/${pageIndex}`; +} + +function renderArtworkGrid(payload) { + state.total = payload.total || 0; + els.grid.innerHTML = ""; + if (!payload.items.length) { + els.grid.innerHTML = `
还没有作品。可以先下载作品,或点击“扫描旧目录”导入已有文件。
`; + } else { + els.grid.append(...payload.items.map(renderArtworkCard)); + } + const currentPage = Math.floor(state.offset / state.limit) + 1; + const totalPages = Math.max(1, Math.ceil(state.total / state.limit)); + els.pageInfo.textContent = `${currentPage} / ${totalPages}`; +} + +function renderArtworkCard(item) { + const card = document.createElement("article"); + card.className = "artwork-card"; + const thumb = item.thumbnail_path + ? `${escapeHtml(item.title || String(item.artwork_id))}` + : `
${item.type === "novel" ? "小说" : "暂无缩略图"}
`; + card.innerHTML = ` + +
+
${escapeHtml(item.title || String(item.artwork_id))}
+
${escapeHtml(item.artist_name || "未知作者")} · ${escapeHtml(item.type || "")}
+
+ + ${item.artist_id ? `` : ""} + ${item.artist_id ? `` : ""} +
+
+ `; + const artworkSub = card.querySelector(".artwork-sub"); + if (artworkSub) { + artworkSub.textContent = `${pixivArtistDisplayName(item)} · ${item.type || ""}`; + } + const artworkFollow = card.querySelector("[data-follow]"); + if (artworkFollow) { + artworkFollow.dataset.name = pixivArtistStoredName(item); + } + return card; +} + +async function loadArtworks() { + const params = new URLSearchParams({ + limit: state.limit, + offset: state.offset, + ensure_thumbnails: "true", + }); + if (state.query) params.set("q", state.query); + if (state.tag) params.set("tag", state.tag); + if (state.type) params.set("type", state.type); + if (state.artistId) params.set("artist_id", state.artistId); + setStatus("正在加载图库..."); + const payload = await api(`/api/artworks?${params}`); + renderArtworkGrid(payload); + setStatus(`已加载 ${payload.items.length} / ${payload.total} 个作品`); +} + +async function loadFavorites() { + setStatus("正在加载收藏..."); + const params = new URLSearchParams({ limit: state.limit, offset: state.offset }); + const payload = await api(`/api/favorites?${params}`); + renderArtworkGrid(payload); + setStatus(`收藏 ${payload.total} 个作品`); +} + +async function loadTags() { + const tags = await api("/api/tags?limit=80"); + els.tags.innerHTML = ""; + if (!tags.length) { + els.tags.innerHTML = `暂无标签`; + return; + } + for (const tag of tags) { + const button = document.createElement("button"); + button.className = `tag-chip ${state.tag === tag.name ? "active" : ""}`; + button.textContent = `${tag.name} ${tag.artwork_count}`; + button.dataset.tag = tag.name; + els.tags.append(button); + } +} + +async function loadArtists() { + setStatus("正在加载作者..."); + const artists = await api("/api/artists?limit=200"); + els.artists.innerHTML = ""; + if (!artists.length) { + els.artists.innerHTML = `
暂无作者数据。
`; + } else { + els.artists.append(...artists.map(renderArtistRow)); + } + setStatus(`已加载 ${artists.length} 位作者`); +} + +async function loadFollowed() { + const followed = await api("/api/followed-artists"); + els.followed.innerHTML = ""; + if (!followed.length) { + els.followed.innerHTML = `
还没有关注作者。
`; + } else { + els.followed.append(...followed.map(renderArtistRow)); + } +} + +async function loadXFollowed() { + const followed = await api("/api/x/follows"); + els.xFollowed.innerHTML = ""; + if (!followed.length) { + els.xFollowed.innerHTML = `
还没有 X 关注作者。
`; + } else { + els.xFollowed.append(...followed.map(renderXFollowRow)); + } +} + +function renderArtistRow(artist) { + const row = document.createElement("div"); + row.className = "artist-row"; + const isFollowed = Number(artist.enabled ?? artist.is_followed ?? 0); + row.innerHTML = ` +
+ ${escapeHtml(artist.name || `artist_${artist.artist_id}`)} + ID ${artist.artist_id} · ${artist.artwork_count || 0} 个作品${artist.last_synced_at ? ` · 上次同步 ${artist.last_synced_at}` : ""} +
+
+ + +
+ `; + const artistTitle = row.querySelector("strong"); + if (artistTitle) { + artistTitle.textContent = pixivArtistDisplayName(artist); + } + const artistFollow = row.querySelector("[data-follow]"); + if (artistFollow) { + artistFollow.dataset.name = pixivArtistStoredName(artist); + } + return row; +} + +function renderXFollowRow(artist) { + const row = document.createElement("div"); + row.className = "artist-row"; + const handle = artist.username || artist.creator_handle || ""; + const isFollowed = Number(artist.enabled ?? 1); + const latest = artist.latest_published_at ? ` · 最新 ${artist.latest_published_at}` : ""; + const checked = artist.last_checked_at ? ` · 检查 ${artist.last_checked_at}` : ""; + const error = artist.last_error ? ` · ${escapeHtml(artist.last_error)}` : ""; + row.innerHTML = ` +
+ ${escapeHtml(artist.creator_name || handle || "x_author")} + @${escapeHtml(handle)}${latest}${checked}${error} +
+
+ + +
+ `; + return row; +} + +async function openArtwork(artworkId) { + const payload = await api(`/api/artworks/${artworkId}`); + const artwork = payload.artwork; + const files = payload.files || []; + const tags = payload.tags || []; + const firstFile = files[0]; + const image = firstFile + ? `${escapeHtml(artwork.title)}` + : `
没有本地文件
`; + els.detail.innerHTML = ` +
+
${image}
+
+

${escapeHtml(artwork.title || String(artwork.artwork_id))}

+

${escapeHtml(artwork.artist_name || "未知作者")} · ${escapeHtml(artwork.type)}

+
+ + ${artwork.artist_id ? `` : ""} +
+
+ ${tags.map((tag) => ``).join("")} +
+

${escapeHtml(stripHtml(artwork.caption || ""))}

+

页数 ${files.length || artwork.page_count || 0} · 收藏 ${artwork.bookmark_count || 0} · 浏览 ${artwork.view_count || 0}

+
+
+ `; + const detailArtist = els.detail.querySelector(".detail-info p"); + if (detailArtist) { + detailArtist.textContent = `${pixivArtistDisplayName(artwork)} · ${artwork.type}`; + } + const detailFollow = els.detail.querySelector("[data-follow]"); + if (detailFollow) { + detailFollow.dataset.name = pixivArtistStoredName(artwork); + } + els.dialog.showModal(); +} + +async function showArtist(artistId) { + setActiveView("gallery"); + state.artistId = String(artistId); + state.tag = ""; + state.offset = 0; + const detail = await api(`/api/artists/${artistId}?limit=1`); + els.title.textContent = detail.artist.name || `作者 ${artistId}`; + els.subtitle.textContent = `作者 ID ${artistId} · ${detail.artist.artwork_count || 0} 个本地作品`; + await loadArtworks(); + els.title.textContent = pixivArtistDisplayName(detail.artist); +} + +async function toggleFavorite(artworkId, currentOn) { + await api("/api/favorites", { + method: "POST", + body: JSON.stringify({ artwork_id: Number(artworkId), favorite: !Number(currentOn) }), + }); + await reloadCurrent(); +} + +async function followArtist(artistId, name = "") { + await setPixivFollow(artistId, name, true); +} + +async function setPixivFollow(artistId, name = "", followed = true) { + await api("/api/followed-artists", { + method: "POST", + body: JSON.stringify({ artist_id: String(artistId).trim(), name, followed }), + }); + setStatus(followed ? `已关注作者 ${artistId}` : `已取消关注作者 ${artistId}`); + await Promise.all([loadFollowed(), reloadCurrent()]); +} + +async function followXAuthor(username, name = "") { + await setXFollow(username, name, true); +} + +async function setXFollow(username, name = "", followed = true) { + const handle = username.trim().replace(/^@/, ""); + if (!handle) { + return; + } + await api("/api/x/follows", { + method: "POST", + body: JSON.stringify({ username: handle, name, followed }), + }); + setStatus(followed ? `已关注 X 作者 @${handle}` : `已取消关注 X 作者 @${handle}`); + await loadXFollowed(); +} + +async function scanLegacy() { + setStatus("正在扫描旧下载目录..."); + const result = await api("/api/import/legacy", { method: "POST", body: JSON.stringify({ root: "Sakura_Downloads" }) }); + setStatus(`扫描完成:导入 ${result.imported_files} 个文件,${result.imported_artworks} 个作品`); + await Promise.all([loadTags(), reloadCurrent()]); +} + +async function syncFollowed() { + setStatus("正在同步关注作者..."); + try { + const result = await api("/api/sync/followed", { + method: "POST", + body: JSON.stringify({ download: true, max_new: 20 }), + }); + const downloaded = result.artists.reduce((sum, item) => sum + (item.downloaded || 0), 0); + setStatus(`同步完成:下载 ${downloaded} 个新作品`); + await Promise.all([loadFollowed(), loadTags(), reloadCurrent()]); + } catch (error) { + setStatus(error.message || "同步关注作者失败"); + } +} + +async function syncXFollowed() { + setStatus("正在同步 X 关注..."); + const result = await api("/api/x/follows/sync", { + method: "POST", + body: JSON.stringify({ download: true, max_new: 20 }), + }); + const total = (result.authors || []).reduce((sum, item) => sum + (item.new_posts || 0), 0); + setStatus(`X 同步完成:发现 ${total} 个新动态`); + await loadXFollowed(); +} + +function openOriginFollowSyncDialog() { + els.originFollowSyncResult.textContent = "请选择要同步的平台。"; + els.originFollowSyncDialog.showModal(); +} + +async function syncOriginFollows(platform) { + const label = platform === "pixiv" ? "Pixiv" : "X"; + const button = platform === "pixiv" ? document.querySelector("#syncOriginPixivButton") : document.querySelector("#syncOriginXButton"); + if (button?.disabled) { + return; + } + if (button) { + button.disabled = true; + } + setStatus(`正在同步 ${label} 原站关注...`); + els.originFollowSyncResult.textContent = `正在同步 ${label} 原站关注,请稍候...`; + try { + if (platform === "pixiv") { + const session = await loadSessionStatus(); + if (session && !session.pixiv?.has_cookie) { + throw new Error("未检测到桌面端 Pixiv 会话,请先在 EXE 登录或导入 Cookie。"); + } + } + const result = await api("/api/origin-follows/sync", { + method: "POST", + body: JSON.stringify({ platform }), + }); + const lines = [ + `${label} 原站关注同步完成`, + `同步页数:${result.pages || 0}`, + `新增:${result.created || 0} 位`, + `更新:${result.updated || 0} 位`, + `跳过:${result.skipped || 0} 位`, + `失败:${result.failed || 0} 位`, + ]; + if (Array.isArray(result.progress) && result.progress.length) { + lines.push("", "进度:"); + for (const item of result.progress.slice(-20)) { + lines.push(item.message || `正在同步第 ${item.page || "?"} 页`); + } + } + if (Array.isArray(result.errors) && result.errors.length) { + lines.push("", "失败原因:"); + for (const item of result.errors.slice(0, 10)) { + lines.push(`${item.id || "-"}:${item.reason || ""}`); + } + } + els.originFollowSyncResult.textContent = lines.join("\n"); + setStatus(`${label} 原站关注同步完成:新增 ${result.created || 0},更新 ${result.updated || 0},跳过 ${result.skipped || 0},失败 ${result.failed || 0}`); + if (platform === "pixiv") { + await loadFollowed(); + } else { + await loadXFollowed(); + } + } catch (error) { + const message = error.message || `${label} 原站关注同步失败`; + els.originFollowSyncResult.textContent = message; + setStatus(message); + } finally { + if (button) { + button.disabled = false; + } + } +} + +async function followCurrentX() { + const username = els.xUsername.value.trim(); + if (!username) { + setStatus("请先输入 X 作者名。"); + return; + } + const handle = username.replace(/^@/, ""); + const creatorName = state.xPreviewUsername === handle ? state.xPreviewAuthor : ""; + await followXAuthor(username, creatorName || handle); +} + +async function buildThumbnails() { + setStatus("正在生成缩略图..."); + const result = await api("/api/thumbnails/build?limit=500"); + setStatus(`缩略图完成:处理 ${result.items.length} 个文件`); + await reloadCurrent(); +} + +async function loadPluginPages() { + const payload = await api("/api/plugins/pages"); + const pages = payload.items || []; + const hasXhs = pages.some((item) => item.view === "xiaohongshu"); + els.xhsNavButton.classList.toggle("hidden", !hasXhs); +} + +async function importCookieFile(platform, fileInput) { + const file = fileInput.files?.[0]; + if (!file) { + setStatus("请先选择 cookies.txt 或 json 文件。"); + return; + } + setStatus(`正在导入 ${platform} Cookie...`); + const text = await file.text(); + const result = await api("/api/cookies/import", { + method: "POST", + body: JSON.stringify({ platform, text }), + }); + setStatus(`Cookie 导入完成:${result.summary || `${result.count || 0} 个 Cookie`}`); +} + +async function previewXMedia(event) { + event.preventDefault(); + const username = els.xUsername.value.trim(); + if (!username) { + setStatus("请输入 X 作者名称。"); + return; + } + els.xMediaGrid.innerHTML = `
正在解析 X 媒体页,首次启动 Playwright 可能需要一些时间。
`; + els.xMediaSummary.textContent = "解析中..."; + const submitButton = event.submitter || document.querySelector("#xMediaForm button[type='submit']"); + if (submitButton) { + submitButton.disabled = true; + } + setStatus(`正在解析 X 作者 @${username.replace(/^@/, "")}...`); + try { + const payload = await api("/api/x/media/preview", { + method: "POST", + body: JSON.stringify({ + username, + media_type: els.xMediaType.value, + max_items: Number(els.xMaxItems.value || 120), + }), + }); + state.xPreviewId = payload.preview_id; + renderXMedia(payload); + setStatus(`X 解析完成:${payload.count} 个媒体项`); + } catch (error) { + state.xPreviewId = ""; + els.xMediaSummary.textContent = "解析失败"; + els.xMediaGrid.innerHTML = `
${escapeHtml(error.message || "X 解析失败")}
`; + setStatus(error.message || "X 解析失败"); + } finally { + if (submitButton) { + submitButton.disabled = false; + } + } +} + +function renderXMedia(payload) { + els.xMediaGrid.innerHTML = ""; + els.xMediaSummary.textContent = `${payload.username} · ${payload.count} 个媒体项`; + if (!payload.items.length) { + els.xMediaGrid.innerHTML = `
没有解析到媒体。可能需要先导入 X Cookie,或该作者媒体页为空。
`; + return; + } + els.xMediaGrid.append(...payload.items.map(renderXMediaCard)); + els.xSelectAll.checked = true; +} + +function renderXMediaCard(item) { + const card = document.createElement("article"); + card.className = "x-media-card"; + const preview = item.thumbnail || item.files?.[0]?.url || ""; + const media = item.kind === "video" + ? `
VIDEO
` + : `${escapeHtml(item.title || item.id)}`; + card.innerHTML = ` + +
${media}
+
+
${escapeHtml(item.title || item.id)}
+
@${escapeHtml(item.author_id || "")} · ${item.file_count || 1} 个文件
+
+ `; + return card; +} + +async function downloadXSelected() { + if (!state.xPreviewId) { + setStatus("请先解析 X 作者媒体。"); + return; + } + const selected = [...document.querySelectorAll("[data-x-id]:checked")].map((item) => item.dataset.xId); + if (!selected.length) { + setStatus("没有选中的媒体。"); + return; + } + setStatus(`正在下载 ${selected.length} 个 X 媒体项...`); + const result = await api("/api/x/media/download", { + method: "POST", + body: JSON.stringify({ preview_id: state.xPreviewId, selected_ids: selected }), + }); + setStatus(`X 下载完成:保存 ${result.downloaded_files} 个文件,失败 ${result.failed_items} 项`); + await loadXLocalMedia(); +} + +async function loadXLocalMedia() { + const payload = await api("/api/x/local-media?limit=160"); + els.xLocalGrid.innerHTML = ""; + els.xLocalSummary.textContent = `本地 ${payload.total || 0} 个文件`; + if (!payload.items.length) { + els.xLocalGrid.innerHTML = `
还没有本地 X 图片或视频。
`; + return; + } + els.xLocalGrid.append(...payload.items.map(renderXLocalCard)); +} + +function renderXLocalCard(item) { + const card = document.createElement("article"); + card.className = "x-media-card"; + const media = item.kind === "video" + ? `` + : `${escapeHtml(item.name)}`; + card.innerHTML = ` + +
${media}
+
+
${escapeHtml(item.name)}
+
${escapeHtml(item.relative_path)} · ${formatBytes(item.size)}
+
+ `; + return card; +} + +async function deleteXLocalSelected() { + const selected = [...document.querySelectorAll("[data-x-local-id]:checked")].map((item) => item.dataset.xLocalId); + if (!selected.length) { + setStatus("没有选中的本地 X 文件。"); + return; + } + setStatus(`正在删除 ${selected.length} 个本地 X 文件...`); + const result = await api("/api/x/local-media/delete", { + method: "POST", + body: JSON.stringify({ ids: selected }), + }); + setStatus(`删除完成:${result.deleted.length} 个,失败 ${result.failed.length} 个`); + els.xLocalSelectAll.checked = false; + await loadXLocalMedia(); +} + +async function previewXhs(event) { + event.preventDefault(); + const target = els.xhsTarget.value.trim(); + if (!target) { + setStatus("请输入小红书关键词或笔记链接。"); + return; + } + els.xhsGrid.innerHTML = `
正在解析小红书内容...
`; + els.xhsSummary.textContent = "解析中..."; + const payload = await api("/api/xiaohongshu/preview", { + method: "POST", + body: JSON.stringify({ target, limit: Number(els.xhsLimit.value || 20) }), + }); + state.xhsPreviewId = payload.preview_id; + renderXhs(payload); + setStatus(`小红书解析完成:${payload.count} 个项目`); +} + +function renderXhs(payload) { + els.xhsGrid.innerHTML = ""; + els.xhsSummary.textContent = `${payload.count} 个项目`; + if (!payload.items.length) { + els.xhsGrid.innerHTML = `
没有解析到内容,可能需要先登录或换一个关键词。
`; + return; + } + els.xhsGrid.append(...payload.items.map(renderXhsCard)); + els.xhsSelectAll.checked = true; +} + +function renderXhsCard(item) { + const card = document.createElement("article"); + card.className = "x-media-card"; + const preview = item.thumbnail || item.metadata?.cover_url || ""; + const media = preview + ? `${escapeHtml(item.title || item.id)}` + : `
暂无预览
`; + card.innerHTML = ` + +
${media}
+
+
${escapeHtml(item.title || item.id)}
+
${escapeHtml(item.author || "")} · ${item.file_count || 0} 个文件
+
+ `; + return card; +} + +async function downloadXhsSelected() { + if (!state.xhsPreviewId) { + setStatus("请先解析小红书内容。"); + return; + } + const selected = [...document.querySelectorAll("[data-xhs-id]:checked")].map((item) => item.dataset.xhsId); + if (!selected.length) { + setStatus("没有选中的小红书项目。"); + return; + } + setStatus(`正在下载 ${selected.length} 个小红书项目...`); + const result = await api("/api/xiaohongshu/download", { + method: "POST", + body: JSON.stringify({ preview_id: state.xhsPreviewId, selected_ids: selected }), + }); + setStatus(`小红书下载完成:保存 ${result.downloaded_files} 个文件,失败 ${result.failed_items} 项`); +} + +function setActiveView(view) { + state.view = view; + document.querySelectorAll(".nav-button").forEach((button) => { + button.classList.toggle("active", button.dataset.view === view); + }); + document.querySelectorAll(".view").forEach((section) => section.classList.remove("active")); + if (view === "home") { + document.querySelector("#homeView").classList.add("active"); + } else if (view === "artists") { + document.querySelector("#artistsView").classList.add("active"); + } else if (view === "sync") { + document.querySelector("#syncView").classList.add("active"); + } else if (view === "xMedia") { + document.querySelector("#xMediaView").classList.add("active"); + } else if (view === "xiaohongshu") { + document.querySelector("#xiaohongshuView").classList.add("active"); + } else { + document.querySelector("#galleryView").classList.add("active"); + } +} + +async function reloadCurrent() { + if (state.view === "home") { + setStatus("首页已就绪。选择一个入口开始整理或同步内容。"); + } else if (state.view === "favorites") { + els.title.textContent = "本地收藏"; + els.subtitle.textContent = "你标记收藏的作品"; + await loadFavorites(); + } else if (state.view === "artists") { + await loadArtists(); + } else if (state.view === "sync") { + await Promise.all([loadFollowed(), loadXFollowed()]); + } else if (state.view === "xMedia") { + await loadXLocalMedia(); + setStatus("X 作者媒体页已就绪。"); + } else if (state.view === "xiaohongshu") { + setStatus("小红书插件页已就绪。"); + } else { + if (!state.artistId) { + els.title.textContent = state.tag ? `标签:${state.tag}` : "全部作品"; + els.subtitle.textContent = state.query ? `搜索:${state.query}` : "本地已入库作品"; + } + await loadArtworks(); + } +} + +function clearFilters() { + state.query = ""; + state.tag = ""; + state.type = ""; + state.artistId = ""; + state.offset = 0; + els.search.value = ""; + els.type.value = ""; +} + +function escapeHtml(value) { + return String(value ?? "").replace(/[&<>"']/g, (char) => ({ + "&": "&", + "<": "<", + ">": ">", + '"': """, + "'": "'", + })[char]); +} + +function stripHtml(value) { + const tmp = document.createElement("div"); + tmp.innerHTML = value; + return tmp.textContent || tmp.innerText || ""; +} + +function formatBytes(value) { + const size = Number(value || 0); + if (size < 1024) return `${size} B`; + if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KB`; + return `${(size / 1024 / 1024).toFixed(1)} MB`; +} + +document.querySelectorAll(".nav-button").forEach((button) => { + button.addEventListener("click", async () => { + setActiveView(button.dataset.view); + state.offset = 0; + if (state.view !== "gallery") { + state.artistId = ""; + } + await reloadCurrent(); + }); +}); + +els.search.addEventListener("input", debounce(async () => { + state.query = els.search.value.trim(); + state.offset = 0; + setActiveView("gallery"); + await reloadCurrent(); +}, 280)); + +els.type.addEventListener("change", async () => { + state.type = els.type.value; + state.offset = 0; + setActiveView("gallery"); + await reloadCurrent(); +}); + +document.addEventListener("click", async (event) => { + const target = event.target.closest("button"); + if (!target) return; + if (target.dataset.viewAction) { + setActiveView(target.dataset.viewAction); + state.offset = 0; + await reloadCurrent(); + return; + } + if (target.dataset.open) await openArtwork(target.dataset.open); + if (target.dataset.fav) await toggleFavorite(target.dataset.fav, target.dataset.on); + if (target.dataset.artist) await showArtist(target.dataset.artist); + if (target.dataset.tag) { + state.tag = target.dataset.tag; + state.artistId = ""; + state.offset = 0; + setActiveView("gallery"); + await loadTags(); + await reloadCurrent(); + } + if (target.dataset.follow) { + const nextFollowed = target.dataset.followed === "1" ? false : true; + await setPixivFollow(target.dataset.follow, target.dataset.name || "", nextFollowed); + } + if (target.dataset.followX) { + const nextFollowed = target.dataset.followedX === "1" ? false : true; + await setXFollow(target.dataset.followX, target.dataset.name || "", nextFollowed); + } + if (target.dataset.openX) { + els.xUsername.value = `@${target.dataset.openX}`; + setActiveView("xMedia"); + setStatus(`已填入 @${target.dataset.openX},可以直接解析最新媒体。`); + } +}); + +document.querySelector("#clearFiltersButton").addEventListener("click", async () => { + clearFilters(); + await loadTags(); + await reloadCurrent(); +}); + +document.querySelector("#prevPageButton").addEventListener("click", async () => { + state.offset = Math.max(0, state.offset - state.limit); + await reloadCurrent(); +}); + +document.querySelector("#nextPageButton").addEventListener("click", async () => { + if (state.offset + state.limit < state.total) { + state.offset += state.limit; + await reloadCurrent(); + } +}); + +document.querySelector("#scanButton").addEventListener("click", scanLegacy); +document.querySelector("#homeScanButton")?.addEventListener("click", scanLegacy); +document.querySelector("#originFollowSyncButton").addEventListener("click", openOriginFollowSyncDialog); +document.querySelector("#closeOriginFollowSyncDialogButton").addEventListener("click", () => els.originFollowSyncDialog.close()); +document.querySelector("#syncOriginPixivButton").addEventListener("click", () => syncOriginFollows("pixiv")); +document.querySelector("#syncOriginXButton").addEventListener("click", () => syncOriginFollows("x")); +document.querySelector("#syncButton").addEventListener("click", syncFollowed); +document.querySelector("#syncXButton").addEventListener("click", syncXFollowed); +document.querySelector("#buildThumbsButton").addEventListener("click", buildThumbnails); +document.querySelector("#homeThumbButton")?.addEventListener("click", buildThumbnails); +document.querySelector("#closeDialogButton").addEventListener("click", () => els.dialog.close()); +document.querySelector("#importPixivCookieButton")?.addEventListener("click", () => importCookieFile("pixiv", els.pixivCookieFile)); +els.refreshSessionButton?.addEventListener("click", loadSessionStatus); +document.querySelector("#importXCookieButton").addEventListener("click", () => importCookieFile("x", els.xCookieFile)); +document.querySelector("#xMediaForm").addEventListener("submit", previewXMedia); +document.querySelector("#followCurrentXButton").addEventListener("click", followCurrentX); +document.querySelector("#downloadXSelectedButton").addEventListener("click", downloadXSelected); +document.querySelector("#refreshXLocalButton").addEventListener("click", loadXLocalMedia); +document.querySelector("#deleteXLocalButton").addEventListener("click", deleteXLocalSelected); +document.querySelector("#xhsForm").addEventListener("submit", previewXhs); +document.querySelector("#downloadXhsSelectedButton").addEventListener("click", downloadXhsSelected); +els.xSelectAll.addEventListener("change", () => { + document.querySelectorAll("[data-x-id]").forEach((item) => { + item.checked = els.xSelectAll.checked; + }); +}); +els.xLocalSelectAll.addEventListener("change", () => { + document.querySelectorAll("[data-x-local-id]").forEach((item) => { + item.checked = els.xLocalSelectAll.checked; + }); +}); +els.xhsSelectAll.addEventListener("change", () => { + document.querySelectorAll("[data-xhs-id]").forEach((item) => { + item.checked = els.xhsSelectAll.checked; + }); +}); + +document.querySelector("#followForm").addEventListener("submit", async (event) => { + event.preventDefault(); + const id = document.querySelector("#followArtistId").value.trim(); + const name = document.querySelector("#followArtistName").value.trim(); + if (!id) return; + await followArtist(id, name); + event.target.reset(); +}); + +document.querySelector("#xFollowForm").addEventListener("submit", async (event) => { + event.preventDefault(); + const username = els.xFollowUsername.value.trim(); + const name = els.xFollowName.value.trim(); + if (!username) return; + await followXAuthor(username, name); + event.target.reset(); +}); + +function debounce(fn, delay) { + let timer = 0; + return (...args) => { + clearTimeout(timer); + timer = setTimeout(() => fn(...args), delay); + }; +} + +async function boot() { + try { + await Promise.all([loadPluginPages(), loadSessionStatus(), loadTags(), loadArtworks(), loadFollowed(), loadXFollowed()]); + } catch (error) { + setStatus(error.message); + } +} + +boot(); diff --git a/pixiv_app/webui/index.html b/pixiv_app/webui/index.html new file mode 100644 index 0000000..2ca1a7e --- /dev/null +++ b/pixiv_app/webui/index.html @@ -0,0 +1,284 @@ + + + + + + Sakura 本地媒体库 + + + + + +
+
+
+ + +
+
+ + +
+
+ +
准备就绪
+ +
+
+
+

本地优先

+

今天想看什么?

+

插画、漫画、小说和视频都保存在本机。你可以直接浏览、搜索和收藏。

+
+ +
+ +
+ + + + +
+ +
+
+ 1 +
+ 在 EXE 主界面导入 Cookie + Pixiv、X、小红书等登录态会保存到本地运行目录。 +
+
+
+ 2 +
+ 选择平台和目标后开始抓取 + 作者、单作品、关键词、插件链接都可以从主界面发起。 +
+
+
+ 3 +
+ 回到图库整理内容 + 收藏、关注作者、生成缩略图和查看详情都在这里完成。 +
+
+
+
+ +
+
+
+

全部作品

+

本地已入库作品

+
+ +
+
+
+ + 0 / 0 + +
+
+ +
+
+
+

作者

+

按本地作品数量排序

+
+
+
+
+ +
+
+
+ 同步原站关注 + 读取当前 Pixiv / X 登录账号的关注列表,保存到本地。 +
+ +
+
+
+

关注同步

+

使用已保存 Cookie 同步关注作者的新作品

+
+ +
+
+
+ Pixiv 登录态 + 正在读取桌面端已保存的 Pixiv 会话... +
+ +
+ +
+
+
+

X 关注

+

按时间线检查 X 作者的最新媒体推文,并保存到本地媒体库。

+
+ +
+ +
+
+ +
+
+
+

X 作者媒体

+

输入作者名称,解析媒体页中的图片或视频,勾选后下载到本地。

+
+
+ +
+
+ X Cookie + 导入 Get cookies.txt 导出的 txt/json,会写入 runtime/x_cookies.json。 +
+ + +
+ +
+ + + + +
+ +
+ + + + 尚未解析 +
+ +
+ +
+
+

本地 X 媒体

+

查看已下载的 X 图片和视频,可删除本地文件。

+
+
+ + +
+
+
+ + 等待加载 +
+
+
+ +
+
+
+

小红书

+

输入关键词或笔记链接,先预览再勾选下载。

+
+
+
+ + + +
+
+ + + 等待解析 +
+
+
+
+ + +
+
+ 同步原站关注 + 选择平台后,会读取当前登录账号的关注列表并保存到本地。 +
+ +
+
+ + +
+

+    
+ + + +
+
+ + + + diff --git a/pixiv_app/webui/styles.css b/pixiv_app/webui/styles.css new file mode 100644 index 0000000..c0aa561 --- /dev/null +++ b/pixiv_app/webui/styles.css @@ -0,0 +1,756 @@ +:root { + color-scheme: light; + --bg: #f6f7f9; + --surface: #ffffff; + --surface-soft: #faf5f7; + --line: #e5dce1; + --text: #242229; + --muted: #706b73; + --primary: #cc5f91; + --primary-strong: #af477a; + --accent: #277d78; + --ok: #3f9b72; + --shadow: 0 10px 28px rgba(48, 39, 44, 0.09); +} + +* { + box-sizing: border-box; +} + +body { + margin: 0; + min-height: 100vh; + display: grid; + grid-template-columns: 248px minmax(0, 1fr); + background: var(--bg); + color: var(--text); + font-family: "Segoe UI", "Microsoft YaHei", Arial, sans-serif; + letter-spacing: 0; +} + +button, +input, +select { + font: inherit; +} + +button { + cursor: pointer; +} + +.sidebar { + min-height: 100vh; + border-right: 1px solid var(--line); + background: #ffffff; + padding: 22px 18px; + position: sticky; + top: 0; + overflow-y: auto; +} + +.brand { + display: flex; + align-items: center; + gap: 12px; + margin-bottom: 24px; +} + +.brand-mark { + width: 46px; + height: 46px; + display: grid; + place-items: center; + background: var(--primary); + color: white; + border-radius: 8px; + font-weight: 700; +} + +.brand h1, +.brand p, +.panel h2, +.section-head h2, +.section-head p { + margin: 0; +} + +.brand h1 { + font-size: 19px; + font-weight: 750; +} + +.brand p, +.section-head p { + color: var(--muted); + font-size: 13px; + margin-top: 3px; +} + +.nav { + display: grid; + gap: 8px; + margin-bottom: 22px; +} + +.nav-button, +.ghost-button, +.primary-button { + border: 1px solid var(--line); + border-radius: 10px; + min-height: 40px; + padding: 0 14px; + background: var(--surface); + color: var(--text); +} + +.nav-button { + text-align: left; + font-weight: 650; +} + +.nav-button.active, +.primary-button { + background: var(--primary); + border-color: var(--primary); + color: white; + box-shadow: 0 8px 18px rgba(204, 95, 145, 0.2); +} + +.primary-button:hover { + background: var(--primary-strong); +} + +.ghost-button:hover, +.nav-button:hover { + border-color: var(--primary); + color: var(--primary); +} + +.danger-button:hover { + border-color: var(--accent); + color: var(--accent); +} + +.panel { + border-top: 1px solid var(--line); + padding-top: 18px; +} + +.plugin-panel { + margin-bottom: 18px; +} + +.panel h2 { + font-size: 14px; + margin-bottom: 12px; +} + +.compact-nav { + margin-bottom: 0; +} + +.tag-list { + display: flex; + flex-wrap: wrap; + gap: 8px; +} + +.tag-chip { + border: 1px solid var(--line); + background: #fff7fb; + border-radius: 999px; + padding: 6px 10px; + color: var(--muted); + max-width: 100%; +} + +.tag-chip.active { + color: white; + background: var(--accent); + border-color: var(--accent); +} + +.main { + min-width: 0; + padding: 24px 30px 40px; +} + +.welcome-card { + border: 1px solid var(--line); + border-radius: 8px; + background: #ffffff; + box-shadow: var(--shadow); + padding: 28px 30px; + display: flex; + align-items: center; + justify-content: space-between; + gap: 20px; + margin-bottom: 18px; +} + +.welcome-card h2 { + margin: 0 0 8px; + font-size: 28px; + line-height: 1.22; +} + +.welcome-card p { + margin: 0; + color: var(--muted); + max-width: 660px; + line-height: 1.7; +} + +.eyebrow { + color: var(--primary) !important; + font-size: 13px; + font-weight: 750; + margin-bottom: 6px !important; +} + +.quick-grid { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 14px; + margin-bottom: 18px; +} + +.quick-card { + border: 1px solid var(--line); + border-radius: 8px; + background: var(--surface); + min-height: 132px; + padding: 18px; + text-align: left; + box-shadow: 0 8px 22px rgba(130, 66, 98, 0.08); +} + +.quick-card:hover { + border-color: var(--primary); + transform: translateY(-1px); +} + +.quick-card strong, +.quick-card span { + display: block; +} + +.quick-card strong { + font-size: 17px; + margin-bottom: 8px; +} + +.quick-card span { + color: var(--muted); + font-size: 13px; + line-height: 1.55; +} + +.help-steps { + display: grid; + gap: 12px; +} + +.help-steps article { + border: 1px solid var(--line); + border-radius: 8px; + background: rgba(255, 255, 255, 0.86); + padding: 16px; + display: flex; + gap: 14px; + align-items: flex-start; +} + +.help-steps b { + width: 30px; + height: 30px; + border-radius: 999px; + background: #ffe3ef; + color: var(--primary); + display: grid; + place-items: center; +} + +.help-steps strong, +.help-steps span { + display: block; +} + +.help-steps span { + color: var(--muted); + margin-top: 4px; +} + +.toolbar { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + margin-bottom: 14px; +} + +.search-wrap { + display: grid; + grid-template-columns: minmax(220px, 420px) 150px; + gap: 10px; + width: min(100%, 610px); +} + +.search-wrap input, +.search-wrap select, +.follow-form input, +.x-media-form input, +.x-media-form select { + height: 42px; + border: 1px solid var(--line); + border-radius: 8px; + background: var(--surface); + color: var(--text); + padding: 0 12px; + min-width: 0; +} + +.toolbar-actions { + display: flex; + gap: 10px; +} + +.status-line { + min-height: 32px; + color: var(--muted); + font-size: 13px; + background: rgba(255, 255, 255, 0.72); + border: 1px solid rgba(240, 213, 226, 0.8); + border-radius: 8px; + padding: 8px 12px; + margin-bottom: 14px; +} + +.view { + display: none; +} + +.view.active { + display: block; +} + +.hidden { + display: none !important; +} + +.section-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + margin-bottom: 16px; +} + +.section-head h2 { + font-size: 24px; +} + +.artwork-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); + gap: 16px; +} + +.artwork-card { + border: 1px solid var(--line); + border-radius: 12px; + background: var(--surface); + overflow: hidden; + box-shadow: 0 8px 22px rgba(130, 66, 98, 0.08); +} + +.thumb-button { + border: 0; + padding: 0; + width: 100%; + aspect-ratio: 1 / 1; + background: #dde5ec; + display: block; +} + +.thumb-button img { + width: 100%; + height: 100%; + object-fit: cover; + display: block; +} + +.thumb-placeholder { + height: 100%; + display: grid; + place-items: center; + color: var(--muted); + padding: 16px; + text-align: center; +} + +.artwork-meta { + padding: 10px; + display: grid; + gap: 7px; +} + +.artwork-title { + font-weight: 650; + line-height: 1.35; + min-height: 38px; +} + +.artwork-sub { + color: var(--muted); + font-size: 12px; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.card-actions { + display: flex; + gap: 8px; +} + +.small-button { + border: 1px solid var(--line); + border-radius: 7px; + background: var(--surface-soft); + min-height: 30px; + padding: 0 9px; + color: var(--text); +} + +.small-button.active { + background: var(--accent); + border-color: var(--accent); + color: white; +} + +.pager { + display: flex; + align-items: center; + justify-content: center; + gap: 12px; + margin: 22px 0 0; +} + +.artist-list { + display: grid; + gap: 10px; +} + +.artist-row { + border: 1px solid var(--line); + border-radius: 12px; + background: var(--surface); + min-height: 66px; + padding: 12px 14px; + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; +} + +.artist-row strong { + display: block; +} + +.artist-row span { + color: var(--muted); + font-size: 13px; +} + +.follow-form { + display: grid; + grid-template-columns: 160px minmax(180px, 320px) 110px; + gap: 10px; + margin-bottom: 16px; +} + +.import-strip { + border: 1px solid var(--line); + border-radius: 12px; + background: var(--surface); + padding: 12px; + display: grid; + grid-template-columns: minmax(220px, 1fr) auto auto; + gap: 10px; + align-items: center; + margin-bottom: 16px; +} + +.import-strip strong, +.import-strip span { + display: block; +} + +.import-strip span, +.x-actions { + color: var(--muted); + font-size: 13px; +} + +.file-button { + border: 1px solid var(--line); + border-radius: 8px; + min-height: 38px; + padding: 0 14px; + display: inline-grid; + place-items: center; + background: var(--surface-soft); + color: var(--text); + cursor: pointer; +} + +.file-button input { + display: none; +} + +.x-media-form { + display: grid; + grid-template-columns: minmax(220px, 1fr) 150px 120px; + gap: 10px; + margin-bottom: 14px; +} + +#xMediaForm { + grid-template-columns: minmax(220px, 1fr) 150px 110px 120px; +} + +.x-actions { + min-height: 38px; + display: flex; + align-items: center; + gap: 14px; + margin-bottom: 14px; +} + +.x-actions label { + display: flex; + align-items: center; + gap: 6px; +} + +.x-media-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); + gap: 14px; +} + +.x-media-card { + border: 1px solid var(--line); + border-radius: 8px; + background: var(--surface); + overflow: hidden; + position: relative; +} + +.x-local-head { + border-top: 1px solid var(--line); + padding-top: 22px; + margin-top: 28px; +} + +.x-check { + position: absolute; + z-index: 2; + left: 8px; + top: 8px; + min-height: 30px; + display: flex; + align-items: center; + gap: 6px; + border-radius: 8px; + background: rgba(255, 255, 255, 0.92); + padding: 4px 8px; + font-size: 12px; +} + +.x-thumb { + width: 100%; + aspect-ratio: 1 / 1; + background: #101820; + display: grid; + place-items: center; +} + +.x-thumb img { + width: 100%; + height: 100%; + object-fit: cover; + display: block; +} + +.x-thumb video { + width: 100%; + height: 100%; + object-fit: contain; + background: #101820; + display: block; +} + +.video-tile { + width: 100%; + height: 100%; + display: grid; + place-items: center; + color: white; + background: #223140; + font-weight: 700; +} + +.detail-dialog { + width: min(980px, calc(100vw - 36px)); + max-height: calc(100vh - 36px); + border: 0; + border-radius: 14px; + padding: 0; + box-shadow: var(--shadow); +} + +.detail-dialog::backdrop { + background: rgba(9, 18, 28, 0.5); +} + +.dialog-head { + display: flex; + gap: 16px; + align-items: flex-start; + justify-content: space-between; + padding: 20px 20px 0; +} + +.dialog-head strong, +.dialog-head span { + display: block; +} + +.dialog-head span { + color: var(--muted); + margin-top: 4px; +} + +.dialog-actions { + display: flex; + flex-wrap: wrap; + gap: 10px; + padding: 18px 20px 0; +} + +.sync-result { + background: #fff7fa; + border: 1px solid var(--line); + border-radius: 8px; + color: var(--text); + margin: 18px 20px 20px; + max-height: 260px; + overflow: auto; + padding: 12px; + white-space: pre-wrap; +} + +.close-button { + position: absolute; + right: 12px; + top: 12px; + width: 34px; + height: 34px; + border: 1px solid var(--line); + border-radius: 50%; + background: var(--surface); + z-index: 2; +} + +.detail-layout { + display: grid; + grid-template-columns: minmax(260px, 52%) minmax(260px, 1fr); + min-height: 520px; +} + +.detail-media { + background: #101820; + display: grid; + place-items: center; + padding: 20px; +} + +.detail-media img { + max-width: 100%; + max-height: 72vh; + object-fit: contain; +} + +.detail-info { + padding: 28px; + overflow: auto; +} + +.detail-info h2 { + margin: 0 0 8px; + font-size: 26px; +} + +.detail-info p { + color: var(--muted); +} + +.detail-tags { + display: flex; + flex-wrap: wrap; + gap: 8px; + margin: 16px 0; +} + +.empty { + border: 1px dashed var(--line); + border-radius: 14px; + padding: 34px; + background: var(--surface); + color: var(--muted); + text-align: center; +} + +@media (max-width: 860px) { + body { + grid-template-columns: 1fr; + } + + .sidebar { + position: static; + min-height: auto; + } + + .quick-grid { + grid-template-columns: 1fr; + } + + .welcome-card { + align-items: stretch; + flex-direction: column; + } + + .toolbar, + .section-head, + .artist-row { + align-items: stretch; + flex-direction: column; + } + + .search-wrap, + .follow-form, + .import-strip, + .x-media-form { + grid-template-columns: 1fr; + } + + .toolbar-actions { + flex-wrap: wrap; + } + + .detail-layout { + grid-template-columns: 1fr; + } +} diff --git a/plugins/ao3/__init__.py b/plugins/ao3/__init__.py new file mode 100644 index 0000000..49488d0 --- /dev/null +++ b/plugins/ao3/__init__.py @@ -0,0 +1 @@ +"""Bundled AO3 (Archive of Our Own) plugin package.""" diff --git a/plugins/ao3/manifest.json b/plugins/ao3/manifest.json new file mode 100644 index 0000000..1dee9ca --- /dev/null +++ b/plugins/ao3/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "AO3", + "version": "1.0.0", + "domain": "archiveofourown.org", + "author": "SakuraHub", + "description": "AO3 fanfiction archive novel plugin. Supports work URL parsing, metadata extraction, EPUB/HTML download, cookie-based auth, and update checking.", + "requires_auth": false, + "min_platform_version": "1.0.0", + "tags": ["novel", "fanfiction", "ao3"], + "dependencies": [] +} diff --git a/plugins/ao3/plugin.py b/plugins/ao3/plugin.py new file mode 100644 index 0000000..77a6c90 --- /dev/null +++ b/plugins/ao3/plugin.py @@ -0,0 +1,1028 @@ +""" +AO3 (Archive of Our Own) novel plugin. + +Parses AO3 work pages, extracts metadata, downloads works via EPUB or HTML, +and integrates with the existing library / cloud-sync / update-check pipelines. + +Reuses: +- pixiv_app.core.library.PixivLibrary for local storage +- pixiv_app.core.cloud_sync.SyncMetadata / SyncManager for cloud sync +- pixiv_app.core.cookie_import utilities for cookie management +- pixiv_app.core.downloader helpers (sanitize_filename, save_text_file) +""" + +from __future__ import annotations + +import logging +import re +import time +from dataclasses import dataclass, field +from datetime import datetime, timezone +from html import unescape as html_unescape +from pathlib import Path +from typing import Any +from urllib.parse import urlparse + +import requests + +from pixiv_app.core.downloader import ( + USER_AGENT, + DEFAULT_TIMEOUT, + DownloadFileRecord, + sanitize_filename, + save_text_file, +) +from pixiv_app.core.library import PixivLibrary +from pixiv_app.core.plugin.base import ( + BasePlugin, + PluginAuthError, + PluginDownloadError, + PluginError, + PluginParseError, + Resource, +) + +logger = logging.getLogger(__name__) + +# --------------------------------------------------------------------------- +# Constants +# --------------------------------------------------------------------------- + +AO3_BASE = "https://archiveofourown.org" +AO3_DOMAINS = ("archiveofourown.org", "ao3.org") + +# AO3 URL patterns +_WORK_URL_RE = re.compile( + r"/works/(?P\d+)(?:/chapters/(?P\d+))?", + re.IGNORECASE, +) + +# --------------------------------------------------------------------------- +# AO3-specific request helpers +# --------------------------------------------------------------------------- + + +def _ao3_request( + url: str, + *, + cookie: str = "", + timeout: int = DEFAULT_TIMEOUT, + retries: int = 3, +) -> requests.Response: + """GET an AO3 page, handling rate-limit (Retry-After) and transient errors.""" + headers = { + "User-Agent": USER_AGENT, + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", + } + if cookie: + headers["Cookie"] = cookie + + last_exc: Exception | None = None + for attempt in range(1, retries + 1): + try: + response = requests.get(url, headers=headers, timeout=timeout, allow_redirects=True) + except requests.RequestException as exc: + last_exc = exc + if attempt < retries: + time.sleep(min(1.0 * attempt, 3.0)) + continue + + if response.status_code == 429: + retry_after = response.headers.get("Retry-After") + try: + wait = int(retry_after) if retry_after else 5 * attempt + except (TypeError, ValueError): + wait = 5 * attempt + logger.warning("AO3 rate-limited, waiting %ds", wait) + time.sleep(wait) + last_exc = PluginError("AO3 rate limited the request; please retry later.") + continue + + if response.status_code == 404: + raise PluginParseError("AO3 work was not found (404).") + + if response.status_code == 401: + raise PluginAuthError("AO3 requires login for this work. Import a valid AO3 Cookie.") + + # AO3 adult content confirmation page + if "This work could have adult content" in response.text or "adult check" in response.url.lower(): + raise PluginAuthError( + "This AO3 work requires adult-content confirmation. Import a valid AO3 Cookie." + ) + + if not response.ok: + last_exc = PluginError(f"AO3 request failed HTTP {response.status_code}") + if attempt < retries: + time.sleep(min(1.0 * attempt, 3.0)) + continue + + # Check for login wall (page says you need to log in but it's not 401) + if "Please log in" in response.text and "adult" not in response.text.lower(): + if not cookie: + raise PluginAuthError("This AO3 work is visible only to logged-in users. Import an AO3 Cookie.") + raise PluginAuthError("AO3 Cookie may be expired. Import it again.") + + return response + + if last_exc: + raise PluginError(f"AO3 request failed: {last_exc}") from last_exc + raise PluginError("AO3 request failed after maximum retries.") + + +def _ao3_download_file( + url: str, + save_path: Path, + *, + cookie: str = "", + timeout: int = 60, +) -> Path: + """Download a binary file (EPUB) from AO3.""" + headers = { + "User-Agent": USER_AGENT, + "Accept": "*/*", + } + if cookie: + headers["Cookie"] = cookie + + if save_path.exists() and save_path.stat().st_size > 1024: + return save_path # already downloaded + + temp_path = save_path.with_suffix(save_path.suffix + ".part") + response = requests.get(url, headers=headers, timeout=timeout, stream=True) + response.raise_for_status() + + with open(temp_path, "wb") as fh: + for chunk in response.iter_content(chunk_size=256 * 1024): + if chunk: + fh.write(chunk) + + if temp_path.stat().st_size <= 1024: + temp_path.unlink(missing_ok=True) + raise PluginDownloadError("Downloaded file is too small to be a valid EPUB.") + + temp_path.replace(save_path) + return save_path + + +# --------------------------------------------------------------------------- +# AO3 metadata extraction (pure HTML scraping) +# --------------------------------------------------------------------------- + + +@dataclass +class AO3WorkMeta: + """Parsed AO3 work metadata.""" + + work_id: str = "" + title: str = "" + author: str = "" + author_id: str = "" # AO3 username + summary: str = "" + language: str = "" + rating: str = "" + warnings: list[str] = field(default_factory=list) + fandoms: list[str] = field(default_factory=list) + relationships: list[str] = field(default_factory=list) + characters: list[str] = field(default_factory=list) + tags: list[str] = field(default_factory=list) + categories: list[str] = field(default_factory=list) + status: str = "" # "Completed" or "In Progress" + chapter_count: int = 1 + word_count: int = 0 + published_at: str = "" + updated_at: str = "" + original_url: str = "" + cover: str = "" + + def to_dict(self) -> dict[str, Any]: + return { + "source": "ao3", + "work_id": self.work_id, + "title": self.title, + "author": self.author, + "author_id": self.author_id, + "summary": self.summary, + "language": self.language, + "rating": self.rating, + "warnings": self.warnings, + "fandoms": self.fandoms, + "relationships": self.relationships, + "characters": self.characters, + "tags": self.tags, + "categories": self.categories, + "status": self.status, + "chapter_count": self.chapter_count, + "word_count": self.word_count, + "published_at": self.published_at, + "updated_at": self.updated_at, + "cover": self.cover, + "original_url": self.original_url, + } + + +def _extract_work_id_from_url(url: str) -> str: + """Extract the work_id from an AO3 URL.""" + match = _WORK_URL_RE.search(url) + if match: + return match.group("work_id") + raise PluginParseError(f"Invalid AO3 work URL: {url}") + + + +def extract_ao3_ids(url: str) -> tuple[str, str | None]: + """Extract ``(work_id, chapter_id)`` from an AO3 work/chapter URL.""" + match = _WORK_URL_RE.search(url.strip()) + if not match: + raise PluginParseError(f"Invalid AO3 work URL: {url}") + return match.group("work_id"), match.group("chapter_id") + + +def _is_ao3_url(url: str) -> bool: + parsed = urlparse(url.strip()) + host = parsed.netloc.lower().split("@")[-1].split(":")[0] + return host in AO3_DOMAINS and bool(_WORK_URL_RE.search(parsed.path)) + +def _meta_tag(html: str, property_name: str) -> str | None: + """Extract a tag content by property or name.""" + for attr in ("property", "name"): + pattern = re.compile( + rf' str: + """Return inner text of the first element matching a regex.""" + pattern = re.compile(selector_re, re.IGNORECASE | re.DOTALL) + match = pattern.search(html) + if not match: + return "" + inner = match.group(1) + # Strip inner HTML tags + inner = re.sub(r"<[^>]+>", " ", inner) + inner = html_unescape(inner) + return re.sub(r"\s+", " ", inner).strip() + + +def _dd_texts(html: str, dt_label: str) -> list[str]: + """Extract tag texts from
that follows a
with given label. + + AO3 meta uses two patterns: + 1. Tag block:
Rating:
+ 2. Stats block:
Language:
English
+ """ + expected = _normalize_ao3_label(dt_label) + match = None + for candidate in re.finditer( + r"]*>(.*?)\s*]*>(.*?)", + html, + re.IGNORECASE | re.DOTALL, + ): + label_text = _normalize_ao3_label(candidate.group(1)) + if label_text == expected: + match = candidate + break + if match is None: + return [] + dd_html = match.group(2) + # Extract tag text from elements + tags = re.findall( + r']*class="[^"]*tag[^"]*"[^>]*>(.*?)', + dd_html, + re.IGNORECASE | re.DOTALL, + ) + if tags: + return [ + html_unescape(re.sub(r"<[^>]+>", "", t)).strip() + for t in tags if t.strip() + ] + # Fallback: plain text in
+ inner = re.sub(r"<[^>]+>", "", dd_html) + inner = html_unescape(inner).strip() + return [inner] if inner else [] + + +def _normalize_ao3_label(value: str) -> str: + text = re.sub(r"<[^>]+>", "", value) + text = html_unescape(text) + text = text.replace("*", "") + text = re.sub(r"\s+", " ", text).strip().rstrip(":").lower() + return text + + +def _dd_text_single(html: str, dt_label: str) -> str: + """Extract a single text value from
after
.""" + texts = _dd_texts(html, dt_label) + return texts[0] if texts else "" + + +def _stats_value(html: str, label: str) -> str: + """Extract a value from the stats block:
Words:
12345
.""" + return _dd_text_single(html, label) + + +def parse_work_meta(html: str, url: str = "") -> AO3WorkMeta: + """Parse an AO3 work page HTML into structured metadata.""" + meta = AO3WorkMeta() + + # Work ID from URL or meta + meta.original_url = url + try: + meta.work_id = _extract_work_id_from_url(url) + except PluginParseError: + meta.work_id = "" + + # Title + meta.title = _text_of_first(html, r']*>(.*?)') + if not meta.title: + meta.title = _meta_tag(html, "og:title") or "" + + # Author + meta.author = _text_of_first(html, r']*rel="author"[^>]*>(.*?)') + if not meta.author: + meta.author = _meta_tag(html, "og:site_name") or "" + # Author ID from href + author_href_match = re.search(r']*rel="author"[^>]*href="([^"]+)"', html, re.IGNORECASE) + if author_href_match: + author_path = author_href_match.group(1) + author_id_match = re.search(r"/users/([^/]+)", author_path) + if author_id_match: + meta.author_id = author_id_match.group(1) + else: + # Pseud path: /users/username/pseuds/name + pseud_match = re.search(r"/users/([^/]+)/pseuds", author_path) + if pseud_match: + meta.author_id = pseud_match.group(1) + + # Summary + meta.summary = _text_of_first(html, r']*>(.*?)') + if not meta.summary: + meta.summary = _meta_tag(html, "og:description") or "" + + # Language + meta.language = _dd_text_single(html, "Language:") + + # Rating + meta.rating = _dd_text_single(html, "Rating:") + + # Warnings + meta.warnings = _dd_texts(html, "Archive Warnings*:") + + # Fandoms + meta.fandoms = _dd_texts(html, "Fandoms*:") + + # Relationships + meta.relationships = _dd_texts(html, "Relationships:") + + # Characters + meta.characters = _dd_texts(html, "Characters:") + + # Tags (freeform) + meta.tags = _dd_texts(html, "Additional Tags:") + + # Categories + meta.categories = _dd_texts(html, "Categories:") + + # Stats + raw_chapters = _stats_value(html, "Chapters:") + if "/" in raw_chapters: + parts = raw_chapters.split("/") + meta.chapter_count = int(parts[0].strip() or "1") + total = parts[1].strip() + if total == "?": + meta.status = "In Progress" + elif total.isdigit() and int(total) == meta.chapter_count: + meta.status = "Completed" + else: + meta.status = "In Progress" + elif raw_chapters.isdigit(): + meta.chapter_count = int(raw_chapters) + else: + meta.chapter_count = 1 + + raw_words = _stats_value(html, "Words:") + meta.word_count = int(raw_words.replace(",", "") or "0") + + # Check completion status explicitly + if not meta.status: + completed = _dd_texts(html, "Completion Status*:") + if completed: + meta.status = completed[0] + + if not meta.status: + meta.status = "Completed" if meta.chapter_count == 1 else "In Progress" + + # Published date + meta.published_at = _stats_value(html, "Published:") + # Normalize timezone + if meta.published_at: + try: + # AO3 dates are often like "2024-01-15" + dt = datetime.strptime(meta.published_at.strip(), "%Y-%m-%d") + meta.published_at = dt.replace(tzinfo=timezone.utc).isoformat() + except ValueError: + pass + + # Updated date + meta.updated_at = _stats_value(html, "Updated:") + if meta.updated_at: + try: + dt = datetime.strptime(meta.updated_at.strip(), "%Y-%m-%d") + meta.updated_at = dt.replace(tzinfo=timezone.utc).isoformat() + except ValueError: + pass + + # Cover: AO3 does not have covers, leave empty. + meta.cover = "" + + return meta + + +# --------------------------------------------------------------------------- +# EPUB text extraction (minimal: extract paragraphs from EPUB) +# --------------------------------------------------------------------------- + +from zipfile import ZipFile + + +def _extract_epub_text(epub_path: Path) -> str: + """Extract readable text from an EPUB file (XHTML chapters). + + Returns joined text with chapter separators. + Paragraphs within a chapter are separated by blank lines. + Inline markup (, , etc.) is preserved as plain text + without introducing spurious paragraph breaks. + """ + from html.parser import HTMLParser + + # Tags whose closing triggers a paragraph break. + _BLOCK_TAGS = {"p", "div", "br", "h1", "h2", "h3", "h4", "h5", "h6", "li", "blockquote", "section", "article", "hr"} + + class AO3TextExtractor(HTMLParser): + def __init__(self): + super().__init__() + self.paragraphs: list[str] = [] + self._buf: list[str] = [] # accumulating text for the current paragraph + self._skip = 0 # nesting depth for skip regions (script/style/head) + + def handle_starttag(self, tag, attrs): + if tag in ("script", "style", "head"): + self._skip += 1 + + def handle_endtag(self, tag): + if tag in ("script", "style", "head"): + self._skip = max(0, self._skip - 1) + return + if tag in _BLOCK_TAGS: + # flush the current in-progress paragraph + text = " ".join(self._buf).strip() + if text: + self.paragraphs.append(text) + self._buf.clear() + + def handle_data(self, data): + if self._skip: + return + text = data.strip() + if text: + self._buf.append(text) + + def finish(self): + """Flush any remaining buffered text.""" + text = " ".join(self._buf).strip() + if text: + self.paragraphs.append(text) + self._buf.clear() + + chapters: list[str] = [] + with ZipFile(epub_path, "r") as zf: + xhtml_names = sorted( + [n for n in zf.namelist() if n.endswith((".xhtml", ".html", ".htm"))], + ) + for name in xhtml_names: + try: + content = zf.read(name).decode("utf-8", errors="replace") + except Exception: + continue + + # Extract title from tag + title_match = re.search(r"<title>(.*?)", content, re.IGNORECASE) + chapter_title = html_unescape(title_match.group(1).strip()) if title_match else "" + + extractor = AO3TextExtractor() + try: + extractor.feed(content) + extractor.finish() + except Exception: + continue + + if not extractor.paragraphs: + continue + + chapter_text = "\n\n".join(extractor.paragraphs) + if chapter_title and chapter_title.lower() != "chapters": + chapters.append(f"\n\n=== {chapter_title} ===\n\n") + chapters.append(chapter_text) + + return "\n".join(chapters) if chapters else "" + + +def _extract_html_work_text(html: str) -> str: + """Extract readable AO3 work text from a work HTML page. + + AO3 wraps chapter content in
+ (or
). We split on these opening + markers, then extract

for the chapter title + and all

elements for the body. + """ + # Prefer the actual AO3 chapter article block. Summary/preface blocks also + # use ``userstuff``, so the role="article" guard is important. + ARTICLE_START_RE = re.compile( + r']*class="[^"]*userstuff[^"]*"[^>]*role="article"[^>]*>', + re.IGNORECASE, + ) + segments = ARTICLE_START_RE.split(html)[1:] # drop content before first block + if not segments: + # Fallback 1: try the older AO3 "chapter" div class + segments = re.findall( + r']*class="[^"]*chapter[^"]*"[^>]*>(.*?)

\s*(?=]*class="[^"]*chapter|\Z)', + html, + re.IGNORECASE | re.DOTALL, + ) + if not segments: + # Fallback 2: try
wrapper + segments = re.findall( + r']*id="chapters"[^>]*>(.*?)
\s*(?=]*id="afterword"|\Z)', + html, + re.IGNORECASE | re.DOTALL, + ) + + chapters: list[str] = [] + for segment in segments: + # Trim everything after the *next* userstuff article start or common + # trailing markers so we stay within this chapter's block. + end_match = ARTICLE_START_RE.search(segment) + if end_match: + segment = segment[: end_match.start()] + # Also trim at AO3 main-content/comment/footer markers. Chapter pages + # commonly end the body with before feedback/actions. + for trailer_marker in ( + r"", + r"", + r']*id="feedback"', + r']*id="afterword"', + r']*class="[^"]*afterword[^"]*"', + r']*class="[^"]*notes[^"]*module[^"]*"', + ): + trailer_match = re.search(trailer_marker, segment, re.IGNORECASE) + if trailer_match: + segment = segment[: trailer_match.start()] + + # Chapter heading:

Chapter Text

+ title_match = re.search( + r']*class="[^"]*landmark[^"]*heading[^"]*"[^>]*>(.*?)', + segment, + re.IGNORECASE | re.DOTALL, + ) + chapter_title = ( + html_unescape(re.sub(r"<[^>]+>", "", title_match.group(1))).strip() + if title_match + else "" + ) + if chapter_title.lower() == "chapter text": + chapter_title = "" + + # Extract all

text + raw_paragraphs = re.findall( + r"]*>(.*?)

", segment, re.IGNORECASE | re.DOTALL, + ) + paragraphs: list[str] = [] + for raw in raw_paragraphs: + cleaned = html_unescape(re.sub(r"<[^>]+>", " ", raw)) + cleaned = re.sub(r"\s+", " ", cleaned).strip() + if cleaned: + paragraphs.append(cleaned) + + if chapter_title: + chapters.append(f"=== {chapter_title} ===") + if paragraphs: + chapters.append("\n\n".join(paragraphs)) + + if chapters: + return "\n\n".join(chapters).strip() + + # Last resort: extract from
+ text = _text_of_first( + html, + r']*>(.*?)
', + ) + return text.strip() + + +def _resolve_ao3_epub_url(work_id: str, *, cookie: str = "") -> str | None: + """Resolve the actual AO3 EPUB download link for a work. + + AO3's download page at ``/works/{id}/download`` lists format links like + ``EPUB``. We fetch that page + and extract the EPUB href. Returns the absolute URL, or *None* when + the download page is unreachable or does not offer EPUB. + """ + try: + download_page_url = f"{AO3_BASE}/works/{work_id}/download" + response = _ao3_request(download_page_url, cookie=cookie, retries=2) + except PluginError: + return None + + # Look for an EPUB link: EPUB + match = re.search( + r''']*href\s*=\s*["'](?P/downloads/[^"']+\.epub)["'][^>]*>\s*EPUB\s*''', + response.text, + re.IGNORECASE, + ) + if match: + return AO3_BASE + match.group("href") + + # Fallback: any whose text is "EPUB" + match = re.search( + r''']*href\s*=\s*["'](?P[^"']+\.epub)["'][^>]*>\s*EPUB\s*''', + response.text, + re.IGNORECASE, + ) + if match: + href = match.group("href") + return href if href.startswith("http") else AO3_BASE + href + + return None + + +def _format_ao3_text_header(resource: Resource, work_id: str) -> str: + return "\n".join( + [ + f"Title: {resource.title}", + f"Author: {resource.author}", + f"AO3 Work ID: {work_id}", + f"Source: {resource.url}", + f"Summary: {resource.metadata.get('summary', '')}", + "", + "", + ] + ) + + +# --------------------------------------------------------------------------- +# AO3 Plugin +# --------------------------------------------------------------------------- + + +class AO3Plugin(BasePlugin): + domain = "archiveofourown.org" + name = "AO3" + version = "1.0.0" + + def __init__(self, cookie: str = "") -> None: + self.cookie = cookie + self._session = requests.Session() + + # ---- auth ---- + + def set_auth(self, cookie: str) -> None: + self.cookie = cookie or "" + + def get_headers(self) -> dict[str, str]: + headers = { + "User-Agent": USER_AGENT, + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", + } + if self.cookie: + headers["Cookie"] = self.cookie + return headers + + # ---- URL detection ---- + + def can_handle(self, url: str) -> bool: + return _is_ao3_url(url) + + # ---- parse ---- + + def parse(self, url: str) -> list[Resource]: + url = url.strip() + if not _is_ao3_url(url): + raise PluginParseError(f"Invalid AO3 work URL: {url}") + work_id, _chapter_id = extract_ao3_ids(url) + meta = self._fetch_meta(work_id) + + # Build tags as a flat list for the cloud sync / library + all_tags = ( + meta.fandoms + + meta.warnings + + meta.relationships + + meta.characters + + meta.tags + + meta.categories + ) + + metadata = meta.to_dict() + metadata.update({ + "kind": "novel", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "ao3", + "source_id": work_id, + "series_id": work_id, # AO3 work is the series (multi-chapter) + "series_title": meta.title, + "chapter_title": meta.title, + "chapter_order": 1, + "tags_flat": all_tags, + }) + + return [ + Resource( + id=work_id, + url=meta.original_url or f"{AO3_BASE}/works/{work_id}", + title=meta.title, + author=meta.author, + author_id=meta.author_id, + files=[], + metadata=metadata, + thumbnail=meta.cover or None, + created_at=meta.published_at or None, + ) + ] + + # ---- download ---- + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + work_id = resource.metadata.get("work_id") or resource.id + title = sanitize_filename(resource.title, f"ao3_{work_id}") + author = sanitize_filename(resource.author, "unknown_author") + + save_path.mkdir(parents=True, exist_ok=True) + novel_dir = save_path / "novels" / sanitize_filename(author, f"ao3_{work_id}") + novel_dir.mkdir(parents=True, exist_ok=True) + + downloaded_paths: list[Path] = [] + error_messages: list[str] = [] + body_text = "" + + text_path = novel_dir / f"{title}_{work_id}.txt" + + # ---- Primary: download full-work HTML page (most reliable) ---- + try: + html_url = ( + f"{AO3_BASE}/works/{work_id}" + f"?view_adult=true&view_full_work=true" + ) + response = _ao3_request(html_url, cookie=self.cookie) + body_text = _extract_html_work_text(response.text) + if body_text.strip(): + full_text = _format_ao3_text_header(resource, work_id) + body_text + ok, _msg = save_text_file(text_path, full_text) + if ok and text_path.exists(): + downloaded_paths.append(text_path) + logger.info( + "AO3: HTML full-work text saved for work %s (%d chars)", + work_id, len(body_text), + ) + else: + error_messages.append("HTML page contained no extractable text.") + except Exception as exc: + error_messages.append(f"HTML download failed: {exc}") + logger.warning( + "AO3: HTML full-work download failed for work %s: %s", + work_id, exc, + ) + + # ---- Secondary: try EPUB for archival preservation ---- + epub_url = _resolve_ao3_epub_url(work_id, cookie=self.cookie) + if epub_url: + epub_path = novel_dir / f"{title}_{work_id}.epub" + try: + _ao3_download_file(epub_url, epub_path, cookie=self.cookie) + downloaded_paths.append(epub_path) + logger.info("AO3: EPUB saved for work %s", work_id) + except Exception as exc: + logger.warning( + "AO3: EPUB download skipped for work %s: %s", + work_id, exc, + ) + + if not downloaded_paths: + raise PluginDownloadError( + f"AO3 work {work_id} download failed: {'; '.join(error_messages)}" + ) + + # Update resource metadata + resource.metadata["word_count"] = len(body_text) + + return downloaded_paths + + # ---- update checking ---- + + def check_update(self, work_id: str) -> dict[str, Any] | None: + """Check if the work has been updated since last import. + + Returns a dict with update info, or None if no update. + """ + try: + url = f"{AO3_BASE}/works/{work_id}" + response = _ao3_request(url, cookie=self.cookie) + meta = parse_work_meta(response.text, url) + + return { + "work_id": meta.work_id, + "title": meta.title, + "updated_at": meta.updated_at, + "chapter_count": meta.chapter_count, + "word_count": meta.word_count, + "status": meta.status, + "original_url": meta.original_url, + "has_update": True, # caller should determine this + } + except PluginError: + return None + except Exception: + return None + + # ---- internal helpers ---- + + def _fetch_meta(self, work_id: str) -> AO3WorkMeta: + """Fetch and parse metadata for a work.""" + url = f"{AO3_BASE}/works/{work_id}?view_adult=true" + response = _ao3_request(url, cookie=self.cookie) + return parse_work_meta(response.text, response.url) + + def validate(self) -> bool: + return True + + # ---- cookie config (matches Pixiv pattern) ---- + + def get_config_schema(self) -> dict[str, Any]: + return { + "type": "object", + "properties": { + "cookie": { + "type": "string", + "title": "AO3 Cookie", + "description": "Paste AO3 Cookie text.", + }, + }, + } + + def load_config(self) -> dict[str, Any]: + return {"cookie": self.cookie} + + def save_config(self, data: dict[str, Any]) -> None: + self.cookie = str(data.get("cookie") or "") + + +# --------------------------------------------------------------------------- +# Library integration helper +# --------------------------------------------------------------------------- + +def ao3_to_library_artwork_id(work_id: str) -> int: + """Map an AO3 work_id to the library artwork_id namespace. + + Uses negative integers beyond the Pixiv novel range to avoid collisions. + AO3 work IDs can be up to 8+ digits (e.g. 12345678), we offset them: + - AO3 prefix: -200000000 (200 million) + - artwork_id = -(200000000 + int(work_id)) + + This keeps AO3 works clearly distinct from Pixiv novels (-1 to -199999999). + """ + AO3_OFFSET = 200_000_000 + return -(AO3_OFFSET + int(work_id)) + + +def upsert_ao3_novel_to_library( + library: PixivLibrary, + meta: AO3WorkMeta, + *, + file_records: list[DownloadFileRecord] | None = None, +) -> int: + """Insert or update an AO3 work in the local library. + + Returns the library artwork_id. + """ + from pixiv_app.core.library import library_artwork_id + + work_id = int(meta.work_id) + lib_id = library_artwork_id(work_id, artwork_type="novel", source_platform="ao3") + + # Build detail dict compatible with upsert_artwork + # Include source / source_platform so upsert_artwork computes the correct + # AO3-offset artwork_id via library_artwork_id(id, "novel", "ao3"). + detail: dict[str, Any] = { + "id": work_id, + "novelId": work_id, + "title": meta.title, + "userName": meta.author, + "userId": _ao3_author_id_to_int(meta.author_id), + "description": meta.summary, + "pageCount": meta.chapter_count, + "likeCount": 0, + "bookmarkCount": 0, + "viewCount": 0, + "xRestrict": 1 if meta.rating in ("Explicit", "Not Rated") else 0, + "aiType": 0, + "createDate": meta.published_at, + "source_platform": "ao3", + "source": "ao3", + "tags": [ + {"tag": tag, "translation": None} + for tag in ( + meta.fandoms + + meta.characters + + meta.tags + + meta.relationships + + meta.warnings + + meta.categories + ) + ], + } + + # Upsert artist stub + artist_id = _ao3_author_id_to_int(meta.author_id) + library.ensure_artist_stub(artist_id, name=meta.author) + library.upsert_artist_profile(artist_id, name=meta.author) + + # Upsert artwork via the standard method (now AO3-ID-aware) + library.ensure_artwork_stub(work_id, artwork_type="novel", title=meta.title, source_platform="ao3") + library.upsert_artwork(detail, artwork_type="novel") + + # Record files if provided + if file_records: + library.record_files(lib_id, file_records) + + return lib_id + + +def _ao3_author_id_to_int(author_id: str) -> int: + """Convert an AO3 username string to a stable int for the library. + + Uses a simple DJB2 hash constrained to positive 32-bit range. + """ + if not author_id: + return 0 + h = 5381 + for ch in author_id: + h = ((h << 5) + h) + ord(ch) + return h & 0x7FFFFFFF + + +# --------------------------------------------------------------------------- +# Cloud sync metadata builder +# --------------------------------------------------------------------------- + +def build_ao3_sync_metadata( + meta: AO3WorkMeta, + file_path: Path, + *, + chapter_title: str = "", + chapter_order: int = 1, +) -> dict[str, Any]: + """Build a SyncMetadata-like dict for cloud sync. + + Compatible with pixiv_app.core.cloud_sync.SyncMetadata. + """ + from pixiv_app.core.cloud_sync import SyncMetadata + + all_tags = ( + meta.fandoms + + meta.warnings + + meta.relationships + + meta.characters + + meta.tags + + meta.categories + ) + + return SyncMetadata( + resource_type="novel", + source_platform="ao3", + source_id=meta.work_id, + content_type="novel", + title=meta.title, + author=meta.author, + author_id=meta.author_id, + tags=all_tags, + metadata=meta.to_dict(), + page_count=meta.chapter_count, + description=meta.summary, + word_count=meta.word_count, + series_id=meta.work_id, # work = series for AO3 + series_title=meta.title, + chapter_title=chapter_title or meta.title, + chapter_order=chapter_order, + ) + + +# --------------------------------------------------------------------------- +# Plugin class export +# --------------------------------------------------------------------------- + +plugin_class = AO3Plugin diff --git a/plugins/biquge/manifest.json b/plugins/biquge/manifest.json new file mode 100644 index 0000000..0362655 --- /dev/null +++ b/plugins/biquge/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "笔趣阁", + "version": "3.0.0", + "domain": "www.bqg551.cc", + "author": "Sakura Downloader", + "description": "基于 bqg551 阅读站公开 API 的小说搜索、目录解析、章节抓取与 TXT 导出。", + "requires_auth": false, + "tags": ["novel", "bqg551", "biquge", "bookshelf"], + "min_platform_version": "1.0.0", + "dependencies": [] +} diff --git a/plugins/biquge/plugin.py b/plugins/biquge/plugin.py new file mode 100644 index 0000000..1b189ad --- /dev/null +++ b/plugins/biquge/plugin.py @@ -0,0 +1,1133 @@ +""" +Biquge novel plugin backed by the reader site's public API. + +Primary flow: +- search books via https://apibi.cc/api/search +- resolve books via https://apibi.cc/api/book + /api/booklist +- fetch chapter text via https://apibi.cc/api/chapter + +The plugin still keeps limited HTML / Playwright fallbacks for legacy pages. +""" + +from __future__ import annotations + +import atexit +import base64 +import hashlib +import json +import os +import re +import threading +from dataclasses import dataclass +from html import unescape +from pathlib import Path +from typing import Any +from urllib.parse import parse_qs, quote, urljoin, urlparse + +import requests + +try: + from cryptography.hazmat.primitives import padding + from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes + + CRYPTOGRAPHY_AVAILABLE = True +except Exception: + padding = None + Cipher = None + algorithms = None + modes = None + CRYPTOGRAPHY_AVAILABLE = False + +from pixiv_app.core.downloader import sanitize_filename, save_text_file +from pixiv_app.core.playwright_runtime import ( + configure_playwright_runtime_env, + explain_missing_browser, + preferred_chromium_executable, +) +from pixiv_app.core.plugin.base import BasePlugin, PluginParseError, Resource + +try: + from playwright.sync_api import sync_playwright + + PLAYWRIGHT_AVAILABLE = True +except Exception: + sync_playwright = None + PLAYWRIGHT_AVAILABLE = False + + +LEGACY_BASE_URL = "https://www.bqgui.cc" +READER_BASE_URL = "https://www.bqg551.cc" +API_BASE_URL = "https://apibi.cc" +TOKEN_SECRET = os.environ.get("SAKURA_BIQUGE_TOKEN_SECRET", "book@token.html") +USER_AGENT = ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" +) +TOKEN_MD5 = hashlib.md5(TOKEN_SECRET.encode("utf-8")).hexdigest() +TOKEN_IV = TOKEN_MD5[:16].encode("utf-8") +TOKEN_KEY = TOKEN_MD5[16:].encode("utf-8") +SORT_SLUGS = { + "xuanhuan", + "wuxia", + "dushi", + "lishi", + "wangyou", + "kehuan", + "mm", + "top", + "finish", +} + +_BOOK_PATH_RE = re.compile(r"/book/(?P\d+)/?$", re.I) +_CHAPTER_PATH_RE = re.compile( + r"/book/(?P\d+)/(?P\d+)(?:_\d+)?\.html(?:[?#].*)?$", + re.I, +) +_HASH_BOOK_RE = re.compile(r"^book/(?P\d+)/?$", re.I) +_HASH_CHAPTER_RE = re.compile( + r"^book/(?P\d+)/(?P\d+)(?:_\d+)?\.html$", + re.I, +) +_BOOK_LINK_RE = re.compile( + r"""]+href\s*=\s*["'](?P/book/\d+/)["'][^>]*>(?P.*?)</a>""", + re.I | re.S, +) +_CHAPTER_LINK_RE = re.compile( + r"""<a[^>]+href\s*=\s*["'](?P<href>/book/\d+/\d+\.html)["'][^>]*>(?P<title>.*?)</a>""", + re.I | re.S, +) + + +@dataclass +class BookCandidate: + title: str + url: str + author: str = "" + latest_chapter: str = "" + + +@dataclass +class TargetIds: + book_id: str = "" + chapter_id: str = "" + + +def _clean_html_text(raw: str) -> str: + text = re.sub(r"<br\s*/?>", "\n", raw, flags=re.I) + text = re.sub(r"</p\s*>", "\n", text, flags=re.I) + text = re.sub(r"<[^>]+>", "", text) + text = unescape(text) + text = text.replace("\r", "") + text = text.replace("\u3000", " ") + lines = [line.strip().strip('"').strip("'") for line in text.splitlines()] + return "\n".join(line for line in lines if line) + + +def _normalize_space(value: str) -> str: + return re.sub(r"\s+", " ", _clean_html_text(value)).strip() + + +class BiqugePlugin(BasePlugin): + name = "笔趣阁" + domain = "www.bqg551.cc" + version = "3.0.0" + + def __init__(self) -> None: + self.session = requests.Session() + self.session.headers.update(self.get_headers()) + self._playwright = None + self._browser = None + self._page = None + self._browser_lock = threading.Lock() + atexit.register(self._close_browser) + + def validate(self) -> bool: + return True + + def can_handle(self, url: str) -> bool: + lowered = url.strip().lower() + return any(host in lowered for host in ("bqgui.cc", "bqg551.cc", "apibi.cc")) + + def get_headers(self) -> dict[str, str]: + return { + "User-Agent": USER_AGENT, + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Language": "zh-CN,zh;q=0.9", + "Referer": READER_BASE_URL + "/", + } + + def resolve_target(self, target: str, *, limit: int = 20) -> dict[str, Any]: + raw = target.strip() + if not raw: + raise PluginParseError("请输入书名、作者、书籍链接或章节链接。") + + normalized = self._normalize_target(raw) + if normalized: + route = self._parse_reader_route(normalized) + if route["kind"] == "search": + keyword = route["keyword"] + books = self._search_books(keyword, limit=limit) + if not books: + raise PluginParseError(f"没有搜索到与“{keyword}”相关的书籍。") + return { + "mode": "books", + "title": f"搜索结果:{keyword}", + "books": [candidate.__dict__ for candidate in books], + } + if route["kind"] == "sort": + books = self._reader_sort_books(str(route["slug"]), limit=limit) + if not books: + raise PluginParseError("该列表页没有解析到书籍。") + return { + "mode": "books", + "title": f"列表结果:{route['slug']}", + "books": [candidate.__dict__ for candidate in books], + } + if route["kind"] == "index": + books = self._reader_index_books(limit=limit) + if not books: + raise PluginParseError("首页推荐列表为空。") + return { + "mode": "books", + "title": "首页推荐", + "books": [candidate.__dict__ for candidate in books], + } + + ids = self._extract_ids_from_url(normalized) + if ids.book_id and ids.chapter_id: + chapter = self._chapter_resource_from_ids(ids.book_id, ids.chapter_id, source_url=normalized) + return { + "mode": "chapters", + "book_title": str(chapter.metadata.get("book_title") or ""), + "resources": [chapter], + } + if ids.book_id: + return self._resolve_book_id(ids.book_id) + return self._resolve_listing_url(normalized, limit=limit) + + if raw.isdigit(): + return self._resolve_book_id(raw) + + books = self._search_books(raw, limit=limit) + if not books: + raise PluginParseError(f"没有搜索到与“{raw}”相关的书籍。") + return { + "mode": "books", + "title": f"搜索结果:{raw}", + "books": [candidate.__dict__ for candidate in books], + } + + def parse(self, url: str) -> list[Resource]: + outcome = self.resolve_target(url, limit=20) + if outcome.get("mode") == "chapters": + return list(outcome.get("resources") or []) + books = list(outcome.get("books") or []) + if not books: + raise PluginParseError("未解析到可下载内容。") + selected = self.resolve_target(str(books[0].get("url") or ""), limit=20) + resources = list(selected.get("resources") or []) + if not resources: + raise PluginParseError("选中的书籍没有章节内容。") + return resources + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + if str(resource.metadata.get("kind") or "") != "novel_chapter": + raise PluginParseError("不支持的笔趣阁资源类型。") + + content = str(resource.metadata.get("content") or "") + if not content.strip(): + chapter_data = self._parse_chapter_page(resource.url) + content = chapter_data["content"] + resource.metadata.update(chapter_data) + + book_title = str(resource.metadata.get("book_title") or "未命名小说") + chapter_title = str(resource.metadata.get("chapter_title") or resource.title or "未命名章节") + chapter_order = int(resource.metadata.get("chapter_order") or 0) + + folder = save_path / "novels" / sanitize_filename(book_title, "biquge_book") + filename = ( + f"{chapter_order:04d}_{sanitize_filename(chapter_title, 'chapter')}.txt" + if chapter_order + else f"{sanitize_filename(chapter_title, 'chapter')}.txt" + ) + final_path = folder / filename + text = "\n".join( + [ + f"书名: {book_title}", + f"章节: {chapter_title}", + f"作者: {resource.author or '未知'}", + f"链接: {resource.url}", + "", + content.strip(), + ] + ).strip() + save_text_file(final_path, text) + resource.metadata["word_count"] = len(content) + return [final_path] + + def _normalize_target(self, target: str) -> str: + value = target.strip() + lowered = value.lower() + if value.startswith("//"): + return "https:" + value + if value.startswith("#/"): + return READER_BASE_URL + "/" + value + if value.startswith("/#/"): + return READER_BASE_URL + value + if value.startswith("/book/"): + return urljoin(LEGACY_BASE_URL, value) + if value.startswith("/"): + return urljoin(LEGACY_BASE_URL, value) + if lowered.startswith(("http://", "https://")): + return value + if lowered.startswith(("www.bqgui.cc", "m.bqgui.cc", "www.bqg551.cc", "bqg551.cc", "apibi.cc")): + return "https://" + value + return "" + + def _parse_reader_route(self, url: str) -> dict[str, str]: + parsed = urlparse(url) + fragment = parsed.fragment.lstrip("/") + if not fragment: + return {"kind": "none"} + + fragment_path, _, fragment_query = fragment.partition("?") + cleaned_path = fragment_path.strip("/") + if not cleaned_path: + return {"kind": "index"} + + if cleaned_path == "search": + keyword = parse_qs(fragment_query).get("q", [""])[0].strip() + return {"kind": "search", "keyword": keyword} + + if cleaned_path in SORT_SLUGS: + return {"kind": "sort", "slug": cleaned_path} + + return {"kind": "none"} + + def _extract_ids_from_url(self, url: str) -> TargetIds: + parsed = urlparse(url) + + for source in (parsed.path, parsed.fragment.partition("?")[0]): + if not source: + continue + normalized = source if source.startswith("/") else f"/{source}" + match = _CHAPTER_PATH_RE.search(normalized) + if match: + return TargetIds(book_id=match.group("book_id"), chapter_id=match.group("chapter_id")) + match = _BOOK_PATH_RE.search(normalized) + if match: + return TargetIds(book_id=match.group("book_id")) + + cleaned = source.strip("/") + match = _HASH_CHAPTER_RE.match(cleaned) + if match: + return TargetIds(book_id=match.group("book_id"), chapter_id=match.group("chapter_id")) + match = _HASH_BOOK_RE.match(cleaned) + if match: + return TargetIds(book_id=match.group("book_id")) + + return TargetIds() + + def _resolve_book_id(self, book_id: str) -> dict[str, Any]: + book_data = self._book_data_from_api(book_id) + chapters = self._chapter_resources_from_book_data(book_data) + if not chapters: + raise PluginParseError(f"书籍 {book_id} 没有解析到章节目录。") + return { + "mode": "chapters", + "book_title": str(chapters[0].metadata.get("book_title") or ""), + "resources": chapters, + } + + def _resolve_listing_url(self, url: str, *, limit: int) -> dict[str, Any]: + html = self._fetch_html(url) + chapters = self._extract_chapter_resources(html, url) + if chapters: + return { + "mode": "chapters", + "book_title": str(chapters[0].metadata.get("book_title") or ""), + "resources": chapters, + } + + books = self._extract_book_candidates(html, url, limit=limit) + if books: + return { + "mode": "books", + "title": "请选择要抓取的书籍", + "books": [candidate.__dict__ for candidate in books], + } + raise PluginParseError("该页面没有解析到书籍或章节列表。") + + def _fetch_html(self, url: str, *, params: dict[str, str] | None = None) -> str: + response = self.session.get(url, params=params, timeout=25) + response.raise_for_status() + if not response.encoding or response.encoding.lower() in {"iso-8859-1", "ascii"}: + response.encoding = response.apparent_encoding or "utf-8" + return response.text + + def _search_books(self, keyword: str, *, limit: int) -> list[BookCandidate]: + books = self._search_books_api(keyword, limit=limit) + if books: + return books + + attempts = [ + {"url": f"{LEGACY_BASE_URL}/s", "params": {"q": keyword}}, + {"url": f"{LEGACY_BASE_URL}/search", "params": {"q": keyword}}, + ] + for attempt in attempts: + try: + html = self._fetch_html(str(attempt["url"]), params=attempt.get("params")) + except Exception: + continue + books = self._extract_book_candidates(html, str(attempt["url"]), limit=limit, keyword=keyword) + if books: + return books + return [] + + def _search_books_api(self, keyword: str, *, limit: int) -> list[BookCandidate]: + try: + payload = self._api_get_json( + "/api/search", + params={"q": keyword}, + referer=f"{READER_BASE_URL}/#/search?q={quote(keyword)}", + ) + except Exception: + return [] + + rows = list(payload.get("data") or []) if isinstance(payload, dict) else [] + ranked: list[tuple[int, BookCandidate]] = [] + seen: set[str] = set() + keyword_parts = self._keyword_parts(keyword) + normalized_keyword = _normalize_space(keyword).lower() + + for row in rows: + if not isinstance(row, dict): + continue + book_id = str(row.get("id") or row.get("dirid") or "").strip() + title = _normalize_space(str(row.get("title") or "")) + if not book_id or not title: + continue + href = self._reader_book_url(book_id) + if href in seen: + continue + seen.add(href) + + author = _normalize_space(str(row.get("author") or "")) + latest = _normalize_space(str(row.get("lastchapter") or "")) + candidate = BookCandidate(title=title, url=href, author=author, latest_chapter=latest) + score = self._candidate_score(candidate, keyword_parts) + title_lower = title.lower() + author_lower = author.lower() + if normalized_keyword and title_lower == normalized_keyword: + score += 1000 + elif normalized_keyword and title_lower.startswith(normalized_keyword): + score += 200 + elif normalized_keyword and normalized_keyword in title_lower: + score += 120 + elif normalized_keyword and normalized_keyword in author_lower: + score += 60 + ranked.append((score, candidate)) + + ranked.sort(key=lambda item: (-item[0], len(item[1].title), item[1].title)) + return [item[1] for item in ranked[:limit]] + + def _reader_index_books(self, *, limit: int) -> list[BookCandidate]: + payload = self._api_get_json("/api/index", params={"sort": "index"}, referer=READER_BASE_URL + "/#/") + if not isinstance(payload, dict): + return [] + rows: list[dict[str, Any]] = [] + for value in payload.values(): + if isinstance(value, list): + rows.extend(item for item in value if isinstance(item, dict)) + return self._book_candidates_from_api_rows(rows, limit=limit) + + def _reader_sort_books(self, slug: str, *, limit: int) -> list[BookCandidate]: + payload = self._api_get_json( + "/api/sort", + params={"sort": slug}, + referer=f"{READER_BASE_URL}/#/{slug}", + ) + rows = list(payload.get("data") or []) if isinstance(payload, dict) else [] + return self._book_candidates_from_api_rows(rows, limit=limit) + + def _book_candidates_from_api_rows(self, rows: list[dict[str, Any]], *, limit: int) -> list[BookCandidate]: + candidates: list[BookCandidate] = [] + seen: set[str] = set() + for row in rows: + book_id = str(row.get("id") or row.get("dirid") or "").strip() + title = _normalize_space(str(row.get("title") or "")) + if not book_id or not title: + continue + href = self._reader_book_url(book_id) + if href in seen: + continue + seen.add(href) + candidates.append( + BookCandidate( + title=title, + url=href, + author=_normalize_space(str(row.get("author") or "")), + latest_chapter=_normalize_space(str(row.get("lastchapter") or "")), + ) + ) + if len(candidates) >= limit: + break + return candidates + + def _keyword_parts(self, keyword: str) -> list[str]: + normalized = _normalize_space(keyword).lower() + if not normalized: + return [] + parts = [part for part in re.split(r"[\s/|,,、]+", normalized) if part] + return sorted(set(parts), key=len, reverse=True) + + def _candidate_score(self, candidate: BookCandidate, keyword_parts: list[str]) -> int: + if not keyword_parts: + return 0 + title = candidate.title.lower() + author = candidate.author.lower() + score = -1 + for part in keyword_parts: + if title == part: + score = max(score, 200) + elif part in title: + score = max(score, 120 + min(40, len(part) * 4)) + if author == part: + score = max(score, 100) + elif part in author: + score = max(score, 70 + min(20, len(part) * 2)) + return score + + def _extract_book_candidates( + self, + html: str, + base_url: str, + *, + limit: int, + keyword: str = "", + ) -> list[BookCandidate]: + ranked: list[tuple[int, BookCandidate]] = [] + seen: set[str] = set() + search_mode = "/s" in base_url.lower() or "search" in base_url.lower() + keyword_parts = self._keyword_parts(keyword) + + for match in _BOOK_LINK_RE.finditer(html): + href = urljoin(base_url, match.group("href")) + if href in seen: + continue + seen.add(href) + title = _normalize_space(match.group("title")) + if not title or len(title) > 120: + continue + author = self._extract_author_near_anchor(html, match.start()) + latest = self._extract_latest_chapter_near_anchor(html, match.start()) + candidate = BookCandidate(title=title, url=href, author=author, latest_chapter=latest) + score = self._candidate_score(candidate, keyword_parts) + if search_mode and keyword_parts and score < 0: + continue + ranked.append((score, candidate)) + + ranked.sort(key=lambda item: (-item[0], len(item[1].title), item[1].title)) + return [item[1] for item in ranked[:limit]] + + def _extract_author_near_anchor(self, html: str, index: int) -> str: + snippet = html[max(0, index - 260) : min(len(html), index + 260)] + patterns = ( + r"作者[::]\s*</[^>]+>\s*([^<]{1,40})", + r"作者[::]\s*([^<\s]{1,40})", + r"</a>\s*([^<]{1,40})\s*</", + ) + for pattern in patterns: + match = re.search(pattern, snippet, re.I) + if match: + author = _normalize_space(match.group(1)) + if author and author != "最新章节": + return author + return "" + + def _extract_latest_chapter_near_anchor(self, html: str, index: int) -> str: + snippet = html[max(0, index - 260) : min(len(html), index + 360)] + match = re.search(r"最新章(?:节|節)?[::]?\s*([^<]{1,80})", snippet, re.I) + return _normalize_space(match.group(1)) if match else "" + + def _book_data_from_api(self, book_id: str) -> dict[str, Any]: + referer = self._reader_book_url(book_id) + try: + book_data = self._api_get_json("/api/book", payload={"id": int(book_id)}, referer=referer) + except Exception: + book_data = self._api_get_json( + "/api/book", + params={"id": book_id}, + host=READER_BASE_URL, + referer=referer, + ) + + book_list_id = str( + (book_data or {}).get("dirid") + or (book_data or {}).get("id") + or book_id + ).strip() + + try: + book_list = self._api_get_json("/api/booklist", payload={"id": int(book_list_id)}, referer=referer) + except Exception: + book_list = self._api_get_json( + "/api/booklist", + params={"id": book_list_id}, + host=READER_BASE_URL, + referer=referer, + ) + + if not isinstance(book_data, dict): + raise PluginParseError(f"书籍 {book_id} 信息返回异常。") + if not isinstance(book_list, dict): + raise PluginParseError(f"书籍 {book_id} 目录返回异常。") + book_data["_chapter_list"] = list(book_list.get("list") or []) + return book_data + + def _chapter_resources_from_book_data(self, book_data: dict[str, Any]) -> list[Resource]: + book_id = str(book_data.get("id") or book_data.get("dirid") or "").strip() + if not book_id: + raise PluginParseError("书籍目录缺少书籍 ID。") + + book_title = _normalize_space(str(book_data.get("title") or "")) + author = _normalize_space(str(book_data.get("author") or "")) + description = _normalize_space(str(book_data.get("intro") or "")) + chapter_list = list(book_data.get("_chapter_list") or []) + resources: list[Resource] = [] + + for order, raw_title in enumerate(chapter_list, start=1): + chapter_title = _normalize_space(str(raw_title or "")) + if not chapter_title: + continue + chapter_id = str(order) + source_id = f"{book_id}:{chapter_id}" + resources.append( + Resource( + id=source_id, + url=self._reader_chapter_url(book_id, chapter_id), + title=chapter_title, + author=author, + author_id="", + files=[], + metadata={ + "kind": "novel_chapter", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "biquge", + "source_id": source_id, + "book_id": book_id, + "book_title": book_title, + "series_id": book_id, + "series_title": book_title, + "chapter_title": chapter_title, + "chapter_order": self._extract_maybe_numeric_order(chapter_title, fallback=order), + "description": description, + "last_update": str(book_data.get("lastupdate") or ""), + "book_status": str(book_data.get("full") or ""), + }, + ) + ) + + return resources + + def _chapter_resource_from_ids(self, book_id: str, chapter_id: str, *, source_url: str = "") -> Resource: + data = self._chapter_data_from_api(book_id, chapter_id, source_url=source_url) + return Resource( + id=str(data["source_id"]), + url=source_url or self._reader_chapter_url(book_id, chapter_id), + title=str(data["chapter_title"]), + author=str(data.get("author") or ""), + author_id="", + files=[], + metadata={ + "kind": "novel_chapter", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "biquge", + "source_id": str(data["source_id"]), + "book_id": str(data.get("book_id") or ""), + "book_title": str(data.get("book_title") or ""), + "series_id": str(data.get("book_id") or ""), + "series_title": str(data.get("book_title") or ""), + "chapter_title": str(data["chapter_title"]), + "chapter_order": int(data.get("chapter_order") or 1), + "description": str(data.get("description") or ""), + "content": str(data.get("content") or ""), + "word_count": int(data.get("word_count") or 0), + }, + ) + + def _chapter_data_from_api(self, book_id: str, chapter_id: str, *, source_url: str = "") -> dict[str, Any]: + referer = source_url or self._reader_chapter_url(book_id, chapter_id) + payload = {"id": int(book_id), "chapterid": int(chapter_id)} + data = self._api_get_json("/api/chapter", payload=payload, referer=referer) + if not isinstance(data, dict): + raise PluginParseError(f"章节 {book_id}/{chapter_id} 返回异常。") + + chapter_title = _normalize_space(str(data.get("chaptername") or f"第 {chapter_id} 章")) + book_title = _normalize_space(str(data.get("title") or "")) + author = _normalize_space(str(data.get("author") or "")) + content = self._clean_api_content(str(data.get("txt") or "")) + if not content: + raise PluginParseError(f"章节《{chapter_title or chapter_id}》正文为空。") + + order_value = str(data.get("chapterid") or chapter_id) + chapter_order = self._extract_maybe_numeric_order( + chapter_title, + fallback=int(order_value) if order_value.isdigit() else 1, + ) + source_id = f"{book_id}:{chapter_id}" + return { + "source_id": source_id, + "book_id": str(book_id), + "book_title": book_title, + "chapter_title": chapter_title, + "chapter_order": chapter_order, + "author": author, + "description": "", + "content": content, + "word_count": len(content), + } + + def _clean_api_content(self, raw: str) -> str: + text = raw.replace("\r", "") + lines: list[str] = [] + for raw_line in text.splitlines(): + line = raw_line.strip() + if not line: + continue + if "请收藏本站" in line: + break + lines.append(line) + return "\n\n".join(lines).strip() + + def _parse_chapter_page(self, url: str) -> dict[str, Any]: + ids = self._extract_ids_from_url(url) + if ids.book_id and ids.chapter_id: + try: + return self._chapter_data_from_api(ids.book_id, ids.chapter_id, source_url=url) + except Exception: + pass + + html = self._fetch_html(url) + if self._looks_like_verify_page(html): + return self._parse_chapter_page_with_playwright(url) + + chapter_title = self._extract_h1(html) or self._extract_title_tag(html) or "未命名章节" + book_title = self._extract_book_title(html) + author = self._extract_author(html) + description = self._extract_description(html) + book_id = ids.book_id or self._extract_book_id(url, html) + chapter_id = ids.chapter_id or "1" + chapter_order = self._extract_maybe_numeric_order( + chapter_title, + fallback=int(chapter_id) if chapter_id.isdigit() else 1, + ) + content = self._extract_chapter_content(html) + if ( + not content + or chapter_title.strip() == "加载中……" + or book_title.strip() == "加载中……" + or "function getCookie" in content + or "/userverify" in content + ): + return self._parse_chapter_page_with_playwright( + url, + book_title=book_title, + author=author, + description=description, + book_id=book_id, + chapter_id=chapter_id, + chapter_title=chapter_title, + chapter_order=chapter_order, + ) + + source_id = f"{book_id}:{chapter_id}" if book_id else chapter_id + return { + "source_id": source_id, + "book_id": book_id, + "book_title": book_title, + "chapter_title": chapter_title, + "chapter_order": chapter_order, + "author": author, + "description": description, + "content": content, + "word_count": len(content), + } + + def _looks_like_verify_page(self, html: str) -> bool: + lowered = html.lower() + return ( + "/userverify" in lowered + or "/user/verify.html" in lowered + or "<title>加载中" in html + or "function getcookie" in lowered + or "location.href = \"/userverify" in lowered + ) + + def _parse_chapter_page_with_playwright( + self, + url: str, + *, + book_title: str = "", + author: str = "", + description: str = "", + book_id: str = "", + chapter_id: str = "", + chapter_title: str = "", + chapter_order: int = 1, + ) -> dict[str, Any]: + if not PLAYWRIGHT_AVAILABLE or sync_playwright is None: + raise PluginParseError("当前章节触发了站点校验,请安装 Playwright Chromium 后再抓取。") + + try: + page = self._ensure_browser_page() + page.goto(url, wait_until="domcontentloaded", timeout=120000) + page.wait_for_selector("#title", timeout=30000) + page.wait_for_selector("#chaptercontent", timeout=30000) + page.wait_for_function( + """ + () => { + const title = document.querySelector('#title')?.innerText?.trim() || ''; + const text = document.querySelector('#chaptercontent')?.innerText?.trim() || ''; + return title.length > 0 && text.length > 40 && !text.includes('加载中'); + } + """, + timeout=45000, + ) + page.wait_for_timeout(800) + except Exception as exc: + raise PluginParseError(f"Playwright 解析章节失败:{exc}") from exc + + resolved_chapter_title = page.locator("#title").inner_text().strip() or chapter_title or "未命名章节" + resolved_content = self._clean_browser_content(page.locator("#chaptercontent").inner_text()) + if not resolved_content: + raise PluginParseError(f"章节《{resolved_chapter_title}》正文为空。") + + resolved_book_title = book_title + try: + if page.locator("#pathname a").count() > 0: + resolved_book_title = page.locator("#pathname a").first.inner_text().strip() or resolved_book_title + except Exception: + pass + if not resolved_book_title: + resolved_book_title = self._extract_book_title(page.content()) + + resolved_author = author or self._extract_author(page.content()) + resolved_ids = self._extract_ids_from_url(page.url) + resolved_book_id = resolved_ids.book_id or book_id + resolved_chapter_id = resolved_ids.chapter_id or chapter_id + + resolved_chapter_order = chapter_order + if not resolved_chapter_order or resolved_chapter_order <= 0: + resolved_chapter_order = self._extract_maybe_numeric_order( + resolved_chapter_title, + fallback=int(resolved_chapter_id) if resolved_chapter_id.isdigit() else 1, + ) + + source_id = ( + f"{resolved_book_id}:{resolved_chapter_id}" + if resolved_book_id and resolved_chapter_id + else (resolved_chapter_id or resolved_book_id or chapter_id or "1") + ) + return { + "source_id": source_id, + "book_id": resolved_book_id, + "book_title": resolved_book_title, + "chapter_title": resolved_chapter_title, + "chapter_order": resolved_chapter_order, + "author": resolved_author, + "description": description, + "content": resolved_content, + "word_count": len(resolved_content), + } + + def _ensure_browser_page(self): + with self._browser_lock: + if self._page is not None and not self._page.is_closed(): + return self._page + + if self._browser is None: + try: + configure_playwright_runtime_env() + self._playwright = sync_playwright().start() + launch_kw: dict[str, Any] = {"headless": True} + executable = preferred_chromium_executable() + if executable is not None: + launch_kw["executable_path"] = str(executable) + self._browser = self._playwright.chromium.launch(**launch_kw) + except Exception as exc: + self._close_browser() + raise PluginParseError( + explain_missing_browser(exc, feature_name="笔趣阁章节解析") + ) from exc + + self._page = self._browser.new_page() + self._page.set_extra_http_headers({"User-Agent": USER_AGENT}) + return self._page + + def _close_browser(self) -> None: + with self._browser_lock: + page = self._page + browser = self._browser + playwright = self._playwright + self._page = None + self._browser = None + self._playwright = None + + try: + if page is not None and not page.is_closed(): + page.close() + except Exception: + pass + try: + if browser is not None: + browser.close() + except Exception: + pass + try: + if playwright is not None: + playwright.stop() + except Exception: + pass + + def _clean_browser_content(self, raw: str) -> str: + text = raw.replace("\r", "") + lines: list[str] = [] + for raw_line in text.splitlines(): + line = raw_line.strip() + if not line: + continue + if "请收藏本站" in line: + break + if any(marker in line for marker in ("点此报错", "加入书签", "上一章", "下一章", "目录")): + continue + lines.append(line) + return "\n\n".join(lines).strip() + + def _extract_chapter_content(self, html: str) -> str: + area_match = re.search( + r'<div[^>]+id="chaptercontent"[^>]*>(?P<body>.*?)</div>', + html, + re.I | re.S, + ) + if area_match is None: + area_match = re.search( + r'<div[^>]+(?:id|class)="[^"]*(?:chaptercontent|content|readcontent|article|txt)[^"]*"[^>]*>(?P<body>.*?)</div>', + html, + re.I | re.S, + ) + area = area_match.group("body") if area_match else html + text = _clean_html_text(area) + lines: list[str] = [] + for raw_line in text.splitlines(): + line = raw_line.strip() + if not line: + continue + if any(marker in line for marker in ("最新网址", "上一章", "下一章", "加入书签", "返回目录")): + continue + lines.append(line) + return "\n\n".join(lines).strip() + + def _extract_chapter_resources(self, html: str, page_url: str) -> list[Resource]: + area = html + marker = re.search(r"(章节目录|章节列表|目录)", html, re.I) + if marker: + area = html[marker.start() :] + + book_title = self._extract_book_title(html) + author = self._extract_author(html) + description = self._extract_description(html) + book_id = self._extract_book_id(page_url, html) + seen: set[str] = set() + resources: list[Resource] = [] + + for order, match in enumerate(_CHAPTER_LINK_RE.finditer(area), start=1): + href = urljoin(page_url, match.group("href")) + if href in seen: + continue + seen.add(href) + chapter_title = _normalize_space(match.group("title")) + if not chapter_title or chapter_title in {"上一章", "下一章", "返回目录"}: + continue + chapter_match = _CHAPTER_PATH_RE.search(urlparse(href).path) + chapter_id = chapter_match.group("chapter_id") if chapter_match else str(order) + source_id = f"{book_id}:{chapter_id}" if book_id else chapter_id + resources.append( + Resource( + id=source_id, + url=href, + title=chapter_title, + author=author, + author_id="", + files=[], + metadata={ + "kind": "novel_chapter", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "biquge", + "source_id": source_id, + "book_id": book_id, + "book_title": book_title, + "series_id": book_id, + "series_title": book_title, + "chapter_title": chapter_title, + "chapter_order": self._extract_maybe_numeric_order(chapter_title, fallback=order), + "description": description, + }, + ) + ) + + return resources + + def _extract_book_title(self, html: str) -> str: + patterns = ( + r'<meta[^>]+property="og:novel:book_name"[^>]+content="([^"]+)"', + r'<meta[^>]+property="og:title"[^>]+content="([^"]+)"', + r'<h1[^>]*>(.*?)</h1>', + r'<title>(.*?)', + ) + for pattern in patterns: + match = re.search(pattern, html, re.I | re.S) + if match: + text = _normalize_space(match.group(1)) + if text: + return re.sub(r"\s*最新章节.*$", "", text, flags=re.I).strip(" -") + match = re.search(r']+href="/book/\d+/"[^>]*>(.*?)', html, re.I | re.S) + if match: + return _normalize_space(match.group(1)) + return "" + + def _extract_author(self, html: str) -> str: + patterns = ( + r"作者[::]\s*\s*([^<]{1,40})", + r"作者[::]\s*([^<\s]{1,40})", + r']+property="og:novel:author"[^>]+content="([^"]+)"', + ) + for pattern in patterns: + match = re.search(pattern, html, re.I) + if match: + return _normalize_space(match.group(1)) + return "" + + def _extract_description(self, html: str) -> str: + patterns = ( + r']+name="description"[^>]+content="([^"]+)"', + r']+property="og:description"[^>]+content="([^"]+)"', + r']+class="intro"[^>]*>(.*?)
', + ) + for pattern in patterns: + match = re.search(pattern, html, re.I | re.S) + if match: + return _normalize_space(match.group(1)) + return "" + + def _extract_h1(self, html: str) -> str: + match = re.search(r"]*>(.*?)", html, re.I | re.S) + return _normalize_space(match.group(1)) if match else "" + + def _extract_title_tag(self, html: str) -> str: + match = re.search(r"(.*?)", html, re.I | re.S) + return _normalize_space(match.group(1)) if match else "" + + def _extract_book_id(self, url: str, html: str) -> str: + ids = self._extract_ids_from_url(url) + if ids.book_id: + return ids.book_id + match = _BOOK_PATH_RE.search(html) + return match.group("book_id") if match else "" + + def _extract_maybe_numeric_order(self, chapter_title: str, *, fallback: int) -> int: + match = re.search(r"第\s*([0-9]+|[零一二三四五六七八九十百千两]+)\s*(?:章|话|卷|回|节|篇)", chapter_title) + if match: + value = self._cn_number_to_int(match.group(1)) + if value > 0: + return value + return fallback + + def _cn_number_to_int(self, text: str) -> int: + if text.isdigit(): + return int(text) + + digits = { + "零": 0, + "一": 1, + "二": 2, + "两": 2, + "三": 3, + "四": 4, + "五": 5, + "六": 6, + "七": 7, + "八": 8, + "九": 9, + } + units = {"十": 10, "百": 100, "千": 1000} + total = 0 + current = 0 + + for char in text: + if char in digits: + current = digits[char] + elif char in units: + if current == 0: + current = 1 + total += current * units[char] + current = 0 + + return total + current + + def _reader_book_url(self, book_id: str) -> str: + return f"{READER_BASE_URL}/#/book/{book_id}/" + + def _reader_chapter_url(self, book_id: str, chapter_id: str) -> str: + return f"{READER_BASE_URL}/#/book/{book_id}/{chapter_id}.html" + + def _api_headers(self, referer: str = "") -> dict[str, str]: + return { + "User-Agent": USER_AGENT, + "Accept": "application/json, text/javascript, */*; q=0.01", + "Accept-Language": "zh-CN,zh;q=0.9", + "X-Requested-With": "XMLHttpRequest", + "Referer": referer or (READER_BASE_URL + "/"), + } + + def _api_get_json( + self, + endpoint: str, + *, + params: dict[str, Any] | None = None, + payload: dict[str, Any] | None = None, + host: str = API_BASE_URL, + referer: str = "", + ) -> Any: + request_params = dict(params or {}) + if payload is not None: + request_params["token"] = self._encode_token(payload) + + url = host.rstrip("/") + endpoint + response = self.session.get( + url, + params=request_params, + timeout=25, + headers=self._api_headers(referer), + ) + response.raise_for_status() + return response.json() + + def _encode_token(self, payload: dict[str, Any]) -> str: + if not CRYPTOGRAPHY_AVAILABLE or padding is None or Cipher is None or algorithms is None or modes is None: + raise PluginParseError( + "当前环境缺少 cryptography,无法生成 apibi 访问 token。" + ) + + raw = json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8") + padder = padding.PKCS7(128).padder() + padded = padder.update(raw) + padder.finalize() + encryptor = Cipher(algorithms.AES(TOKEN_KEY), modes.CBC(TOKEN_IV)).encryptor() + encrypted = encryptor.update(padded) + encryptor.finalize() + return base64.b64encode(encrypted).decode("ascii") + + +plugin_class = BiqugePlugin diff --git a/plugins/changpei/manifest.json b/plugins/changpei/manifest.json new file mode 100644 index 0000000..e2ee6b9 --- /dev/null +++ b/plugins/changpei/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "长佩文学", + "version": "1.0.0", + "domain": "www.gongzicp.com", + "author": "Sakura Downloader", + "description": "长佩文学(gongzicp.com)小说搜索、目录解析、章节抓取与 TXT 导出。", + "requires_auth": true, + "tags": ["novel", "gongzicp", "changpei", "bookshelf"], + "min_platform_version": "1.0.0", + "dependencies": [] +} diff --git a/plugins/changpei/plugin.py b/plugins/changpei/plugin.py new file mode 100644 index 0000000..75f2d10 --- /dev/null +++ b/plugins/changpei/plugin.py @@ -0,0 +1,514 @@ +""" +Changpei (长佩文学 / gongzicp.com) novel plugin. + +Uses the public webapi endpoints on https://www.gongzicp.com/webapi/. +Chapter bodies are AES-CBC encrypted; decryption matches the site's CryptoJS helper. +""" + +from __future__ import annotations + +import base64 +import re +from dataclasses import dataclass +from html import unescape +from pathlib import Path +from typing import Any +from urllib.parse import urljoin, urlparse + +import requests + +try: + from cryptography.hazmat.primitives import padding as crypto_padding + from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes + + CRYPTOGRAPHY_AVAILABLE = True +except Exception: + crypto_padding = None + Cipher = None + algorithms = None + modes = None + CRYPTOGRAPHY_AVAILABLE = False + +from pixiv_app.core.downloader import sanitize_filename, save_text_file +from pixiv_app.core.plugin.base import BasePlugin, PluginParseError, Resource + +BASE_URL = "https://www.gongzicp.com" +WEBAPI_BASE = f"{BASE_URL}/webapi" +USER_AGENT = ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" +) + +_NOVEL_PATH_RE = re.compile(r"/novel-(?P\d+)\.html", re.I) +_READ_PATH_RE = re.compile(r"/read-(?P\d+)-(?P\d+)\.html", re.I) +_SINGLE_READ_RE = re.compile(r"/read-(?P\d+)\.html", re.I) + + +@dataclass +class BookCandidate: + title: str + url: str + author: str = "" + latest_chapter: str = "" + + +@dataclass +class TargetIds: + novel_id: str = "" + chapter_id: str = "" + + +def _js_to_string(value: int, base: int) -> str: + digits = "0123456789abcdefghijklmnopqrstuvwxyz" + chars: list[str] = [] + while value: + chars.append(digits[value % base]) + value //= base + return "".join(reversed(chars)) or "0" + + +def _clean_html_text(raw: str) -> str: + text = re.sub(r"", "\n", raw, flags=re.I) + text = re.sub(r"", "\n", text, flags=re.I) + text = re.sub(r"<[^>]+>", "", text) + text = unescape(text) + text = text.replace("\r", "") + lines = [line.strip() for line in text.splitlines()] + return "\n".join(line for line in lines if line) + + +def _normalize_space(value: str) -> str: + return re.sub(r"\s+", " ", _clean_html_text(value)).strip() + + +class _ChapterDecryptor: + """AES-CBC decryptor matching index.eef719ce.js class XE.""" + + def __init__(self) -> None: + if not CRYPTOGRAPHY_AVAILABLE: + raise PluginParseError("缺少 cryptography 依赖,无法解密长佩章节正文。") + suffix = _js_to_string(int("165455", 14), 32) + key_seed = "iGzsYn" + suffix + iv_str = f"$h$b3!{key_seed}" + decoded = base64.b64decode("dTBMUnJidSRFbg==").decode("utf-8") + key_str = decoded + _js_to_string(int("4d5a6c8", 14), 36) + "A" + self._key = key_str.encode("utf-8") + self._iv = iv_str.encode("utf-8") + + def decrypt(self, encrypted_b64: str) -> str: + if not encrypted_b64: + return "" + raw = base64.b64decode(encrypted_b64) + cipher = Cipher(algorithms.AES(self._key), modes.CBC(self._iv)) + decryptor = cipher.decryptor() + padded = decryptor.update(raw) + decryptor.finalize() + unpadder = crypto_padding.PKCS7(128).unpadder() + plain = unpadder.update(padded) + unpadder.finalize() + return plain.decode("utf-8") + + +class ChangpeiPlugin(BasePlugin): + name = "长佩文学" + domain = "www.gongzicp.com" + version = "1.0.0" + + def __init__(self, cookie: str = "") -> None: + self.cookie = cookie + self.session = requests.Session() + self.session.headers.update(self.get_headers()) + self._chapter_decryptor: _ChapterDecryptor | None = None + + def set_auth(self, cookie: str) -> None: + """设置登录 Cookie,用于访问需订阅/登录的章节。""" + self.cookie = cookie or "" + self.session.headers.update(self.get_headers()) + + def validate(self) -> bool: + return CRYPTOGRAPHY_AVAILABLE + + def can_handle(self, url: str) -> bool: + lowered = url.strip().lower() + if any(host in lowered for host in ("gongzicp.com", "gongzicp.cn")): + return True + if lowered.isdigit(): + return True + return False + + def get_headers(self) -> dict[str, str]: + headers = { + "User-Agent": USER_AGENT, + "Accept": "application/json, text/plain, */*", + "Accept-Language": "zh-CN,zh;q=0.9", + "Referer": BASE_URL + "/", + } + if self.cookie: + headers["Cookie"] = self.cookie + return headers + + def resolve_target(self, target: str, *, limit: int = 20) -> dict[str, Any]: + raw = target.strip() + if not raw: + raise PluginParseError("请输入书名、作者、书籍链接或章节链接。") + + normalized = self._normalize_target(raw) + if normalized: + ids = self._extract_ids_from_url(normalized) + if ids.novel_id and ids.chapter_id: + chapter = self._chapter_resource_from_ids(ids.novel_id, ids.chapter_id, source_url=normalized) + return { + "mode": "chapters", + "book_title": str(chapter.metadata.get("book_title") or ""), + "resources": [chapter], + } + if ids.novel_id: + return self._resolve_novel_id(ids.novel_id) + if ids.chapter_id and not ids.novel_id: + raise PluginParseError("章节链接缺少书籍 ID,请使用 read-{书籍ID}-{章节ID}.html 格式。") + + if raw.isdigit(): + return self._resolve_novel_id(raw) + + books = self._search_books(raw, limit=limit) + if not books: + raise PluginParseError(f"没有搜索到与“{raw}”相关的书籍。") + return { + "mode": "books", + "title": f"搜索结果:{raw}", + "books": [candidate.__dict__ for candidate in books], + } + + def parse(self, url: str) -> list[Resource]: + outcome = self.resolve_target(url, limit=20) + if outcome.get("mode") == "chapters": + return list(outcome.get("resources") or []) + books = list(outcome.get("books") or []) + if not books: + raise PluginParseError("未解析到可下载内容。") + selected = self.resolve_target(str(books[0].get("url") or ""), limit=20) + resources = list(selected.get("resources") or []) + if not resources: + raise PluginParseError("选中的书籍没有章节内容。") + return resources + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + if str(resource.metadata.get("kind") or "") != "novel_chapter": + raise PluginParseError("不支持的长佩资源类型。") + + content = str(resource.metadata.get("content") or "") + if not content.strip(): + nid = str(resource.metadata.get("novel_id") or resource.metadata.get("book_id") or "") + cid = str(resource.metadata.get("chapter_id") or "") + if nid and cid: + preserved_title = str(resource.metadata.get("book_title") or "") + preserved_author = str(resource.author or resource.metadata.get("author") or "") + chapter_data = self._chapter_data_from_api(nid, cid) + content = str(chapter_data.get("content") or "") + resource.metadata.update(chapter_data) + if preserved_title and not str(resource.metadata.get("book_title") or "").strip(): + resource.metadata["book_title"] = preserved_title + if preserved_author and not str(resource.author or "").strip(): + resource.author = preserved_author + + book_title = str(resource.metadata.get("book_title") or "未命名小说") + chapter_title = str(resource.metadata.get("chapter_title") or resource.title or "未命名章节") + chapter_order = int(resource.metadata.get("chapter_order") or 0) + + folder = save_path / "novels" / sanitize_filename(book_title, "changpei_book") + filename = ( + f"{chapter_order:04d}_{sanitize_filename(chapter_title, 'chapter')}.txt" + if chapter_order + else f"{sanitize_filename(chapter_title, 'chapter')}.txt" + ) + final_path = folder / filename + text = "\n".join( + [ + f"书名: {book_title}", + f"章节: {chapter_title}", + f"作者: {resource.author or '未知'}", + f"链接: {resource.url}", + "", + content.strip(), + ] + ).strip() + save_text_file(final_path, text) + resource.metadata["word_count"] = len(content) + return [final_path] + + def _get_decryptor(self) -> _ChapterDecryptor: + if self._chapter_decryptor is None: + self._chapter_decryptor = _ChapterDecryptor() + return self._chapter_decryptor + + def _normalize_target(self, target: str) -> str: + value = target.strip() + lowered = value.lower() + if lowered.startswith(("http://", "https://", "//")): + if value.startswith("//"): + return "https:" + value + return value + if lowered.startswith(("www.gongzicp.com", "gongzicp.com")): + return "https://" + value.lstrip("/") + if _NOVEL_PATH_RE.search(value) or _READ_PATH_RE.search(value) or _SINGLE_READ_RE.search(value): + return urljoin(BASE_URL, value.lstrip("/")) + return "" + + def _extract_ids_from_url(self, url: str) -> TargetIds: + parsed = urlparse(url) + path = parsed.path or "" + match = _READ_PATH_RE.search(path) + if match: + return TargetIds(novel_id=match.group("nid"), chapter_id=match.group("cid")) + match = _NOVEL_PATH_RE.search(path) + if match: + return TargetIds(novel_id=match.group("nid")) + match = _SINGLE_READ_RE.search(path) + if match: + return TargetIds(chapter_id=match.group("cid")) + return TargetIds() + + def _webapi_get(self, path: str, *, params: dict[str, Any] | None = None, referer: str = "") -> dict[str, Any]: + headers = dict(self.get_headers()) + if referer: + headers["Referer"] = referer + response = self.session.get( + f"{WEBAPI_BASE}{path}", + params=params or {}, + headers=headers, + timeout=30, + ) + response.raise_for_status() + payload = response.json() + code = payload.get("code") + if code not in (200, "200"): + message = str(payload.get("msg") or "请求失败") + raise PluginParseError(f"长佩接口错误:{message}") + data = payload.get("data") + if isinstance(data, dict): + return data + if data is None: + return {} + return {"value": data} + + def _novel_url(self, novel_id: str) -> str: + return f"{BASE_URL}/novel-{novel_id}.html" + + def _chapter_url(self, novel_id: str, chapter_id: str) -> str: + return f"{BASE_URL}/read-{novel_id}-{chapter_id}.html" + + def _search_books(self, keyword: str, *, limit: int) -> list[BookCandidate]: + page = 1 + books: list[BookCandidate] = [] + while len(books) < limit: + data = self._webapi_get( + "/search/novels", + params={"k": keyword, "page": page}, + referer=f"{BASE_URL}/novel/searchNovel", + ) + items = list(data.get("list") or []) + if not items: + break + for item in items: + novel_id = str(item.get("novel_id") or "").strip() + title = _normalize_space(str(item.get("novel_name") or "")) + if not novel_id or not title: + continue + author = _normalize_space(str(item.get("novel_author") or item.get("author_nickname") or "")) + books.append( + BookCandidate( + title=title, + url=self._novel_url(novel_id), + author=author, + latest_chapter=_normalize_space(str(item.get("novel_uptime") or "")), + ) + ) + if len(books) >= limit: + break + if len(items) < 10: + break + page += 1 + return books + + def _novel_info(self, novel_id: str) -> dict[str, Any]: + referer = self._novel_url(novel_id) + return self._webapi_get("/novel/novelInfo", params={"id": novel_id}, referer=referer) + + def _chapter_list(self, novel_id: str) -> list[dict[str, Any]]: + referer = self._novel_url(novel_id) + data = self._webapi_get("/novel/chapterGetList", params={"nid": novel_id}, referer=referer) + return list(data.get("list") or []) + + def _decrypt_chapter_content(self, encrypted: str) -> str: + try: + return self._get_decryptor().decrypt(encrypted) + except Exception: + return "" + + def _chapter_data_from_api(self, novel_id: str, chapter_id: str) -> dict[str, Any]: + referer = self._chapter_url(novel_id, chapter_id) + payload = self._webapi_get( + "/novel/chapterGetInfo", + params={"nid": novel_id, "cid": chapter_id}, + referer=referer, + ) + chapter_info = dict(payload.get("chapterInfo") or {}) + novel_info = dict(payload.get("novelInfo") or {}) + + locked = bool(chapter_info.get("lock")) or bool(chapter_info.get("chapter_ispay")) + status = int(chapter_info.get("status") or 0) + encrypted = str(chapter_info.get("content") or "") + content = "" + if not locked and status == 1 and encrypted: + content = self._decrypt_chapter_content(encrypted) + + chapter_title = _normalize_space(str(chapter_info.get("name") or f"章节 {chapter_id}")) + book_title = _normalize_space(str(novel_info.get("name") or novel_info.get("novel_name") or "")) + author = _normalize_space(str(novel_info.get("author") or novel_info.get("author_nickname") or "")) + order = int(chapter_info.get("order") or 0) + source_id = f"{novel_id}:{chapter_id}" + + if locked: + content = "[章节需订阅或已锁定,无法抓取正文]" + + return { + "source_id": source_id, + "novel_id": novel_id, + "book_id": novel_id, + "book_title": book_title, + "chapter_id": chapter_id, + "chapter_title": chapter_title, + "chapter_order": order, + "author": author, + "content": content, + "word_count": len(content), + "locked": locked, + "description": _clean_html_text(str(novel_info.get("novel_info") or novel_info.get("novel_desc") or "")), + } + + def _flatten_chapter_items(self, items: list[dict[str, Any]]) -> list[dict[str, Any]]: + result: list[dict[str, Any]] = [] + + for item in items: + if not isinstance(item, dict): + continue + + if str(item.get("type") or "") == "item": + result.append(item) + + for key in ("children", "list", "chapterList", "chapters"): + child_items = item.get(key) + if isinstance(child_items, list): + result.extend(self._flatten_chapter_items(child_items)) + + return result + + def _chapter_resources_from_novel(self, novel_id: str, novel_info: dict[str, Any]) -> list[Resource]: + book_title = _normalize_space(str(novel_info.get("novel_name") or "")) + author = _normalize_space(str(novel_info.get("author_nickname") or "")) + description = _clean_html_text(str(novel_info.get("novel_info") or novel_info.get("novel_desc") or "")) + chapters = self._flatten_chapter_items(self._chapter_list(novel_id)) + resources: list[Resource] = [] + + for index, item in enumerate(chapters, start=1): + chapter_id = str(item.get("id") or "").strip() + chapter_title = _normalize_space(str(item.get("name") or "")) + if not chapter_id or not chapter_title: + continue + locked = bool(item.get("lock")) or bool(item.get("pay")) + order = int(item.get("order") or index) + source_id = f"{novel_id}:{chapter_id}" + resources.append( + Resource( + id=source_id, + url=self._chapter_url(novel_id, chapter_id), + title=chapter_title, + author=author, + author_id=str(novel_info.get("author_id") or ""), + files=[], + metadata={ + "kind": "novel_chapter", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "changpei", + "source_id": source_id, + "novel_id": novel_id, + "book_id": novel_id, + "book_title": book_title, + "series_id": novel_id, + "series_title": book_title, + "chapter_id": chapter_id, + "chapter_title": chapter_title, + "chapter_order": order, + "description": description, + "locked": locked, + "last_update": str(novel_info.get("novel_uptime") or ""), + "book_status": str(novel_info.get("novel_process") or ""), + }, + ) + ) + return resources + + def _chapter_resource_from_ids(self, novel_id: str, chapter_id: str, *, source_url: str = "") -> Resource: + data = self._chapter_data_from_api(novel_id, chapter_id) + return Resource( + id=str(data["source_id"]), + url=source_url or self._chapter_url(novel_id, chapter_id), + title=str(data["chapter_title"]), + author=str(data.get("author") or ""), + author_id="", + files=[], + metadata={ + "kind": "novel_chapter", + "resource_type": "novel", + "content_type": "novel", + "source_platform": "changpei", + "source_id": str(data["source_id"]), + "novel_id": novel_id, + "book_id": novel_id, + "book_title": str(data.get("book_title") or ""), + "series_id": novel_id, + "series_title": str(data.get("book_title") or ""), + "chapter_id": chapter_id, + "chapter_title": str(data["chapter_title"]), + "chapter_order": int(data.get("chapter_order") or 1), + "description": str(data.get("description") or ""), + "content": str(data.get("content") or ""), + "word_count": int(data.get("word_count") or 0), + "locked": bool(data.get("locked")), + }, + ) + + def _resolve_novel_id(self, novel_id: str) -> dict[str, Any]: + novel_info = self._novel_info(novel_id) + resources = self._chapter_resources_from_novel(novel_id, novel_info) + if not resources: + raise PluginParseError(f"书籍 {novel_id} 没有解析到章节目录。") + book_title = str(resources[0].metadata.get("book_title") or "") + return { + "mode": "chapters", + "book_title": book_title, + "resources": resources, + } + + # ---- cookie config (matches Pixiv / AO3 pattern) ---- + + def get_config_schema(self) -> dict[str, Any]: + return { + "type": "object", + "properties": { + "cookie": { + "type": "string", + "title": "长佩 Cookie", + "description": "粘贴长佩 (gongzicp.com) 登录后的 Cookie 文本,用于访问已购章节。", + }, + }, + } + + def load_config(self) -> dict[str, Any]: + return {"cookie": self.cookie} + + def save_config(self, data: dict[str, Any]) -> None: + self.cookie = str(data.get("cookie") or "") + self.session.headers.update(self.get_headers()) + + +plugin_class = ChangpeiPlugin \ No newline at end of file diff --git a/plugins/douyin/__init__.py b/plugins/douyin/__init__.py new file mode 100644 index 0000000..042a16c --- /dev/null +++ b/plugins/douyin/__init__.py @@ -0,0 +1 @@ +"""Douyin plugin package.""" diff --git a/plugins/douyin/manifest.json b/plugins/douyin/manifest.json new file mode 100644 index 0000000..3566d0a --- /dev/null +++ b/plugins/douyin/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "Douyin", + "version": "1.0.0", + "domain": "douyin.com", + "author": "Sakura Downloader", + "description": "Douyin video and image-note parser/downloader for public share URLs.", + "requires_auth": false, + "tags": ["social", "video", "image"], + "min_platform_version": "1.0.0", + "dependencies": [] +} diff --git a/plugins/douyin/plugin.py b/plugins/douyin/plugin.py new file mode 100644 index 0000000..39c5282 --- /dev/null +++ b/plugins/douyin/plugin.py @@ -0,0 +1,387 @@ +"""Douyin plugin. + +Supports public Douyin share, video, note, and modal links. Cookie support is +optional and reads from runtime/douyin_cookies.json when present. +""" + +from __future__ import annotations + +import html +import json +import re +from dataclasses import dataclass +from pathlib import Path +from typing import Any +from urllib.parse import parse_qs, unquote, urlparse + +import httpx + +from pixiv_app.core.cookie_import import cookies_to_header, parse_cookie_text +from pixiv_app.core.paths import runtime_path +from pixiv_app.core.plugin.base import BasePlugin, PluginDownloadError, PluginParseError, Resource + + +_AWEME_ID_RE = re.compile( + r"(?:aweme_id=|modal_id=|/video/|/note/|/share/video/|/share/note/)(\d{6,})", + re.IGNORECASE, +) +_JSON_SCRIPT_RE = re.compile( + r']+id=["\'](?PRENDER_DATA|__UNIVERSAL_DATA_FOR_REHYDRATION__)["\'][^>]*>' + r"(?P.*?)", + re.IGNORECASE | re.DOTALL, +) +_JSON_URL_RE = re.compile(r"https?:\\?/\\?/[^\"'<>\\]+") + + +@dataclass +class DouyinPluginConfig: + timeout: float = 30.0 + max_redirects: int = 8 + cookie_file: str = "" + + +class DouyinPlugin(BasePlugin): + domain = "douyin.com" + name = "Douyin" + version = "1.0.0" + + def __init__(self) -> None: + self.config = DouyinPluginConfig(cookie_file=str(runtime_path("douyin_cookies.json"))) + + def can_handle(self, url: str) -> bool: + lower = (url or "").strip().lower() + return any( + token in lower + for token in ( + "douyin.com", + "v.douyin.com", + "iesdouyin.com", + "amemv.com", + ) + ) + + def get_headers(self) -> dict[str, str]: + headers = { + "User-Agent": ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" + ), + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", + "Referer": "https://www.douyin.com/", + } + cookie_header = self._cookie_header() + if cookie_header: + headers["Cookie"] = cookie_header + return headers + + def parse(self, url: str) -> list[Resource]: + target = _extract_first_douyin_url(url) + if not target: + raise PluginParseError("No valid Douyin URL found.") + + final_url, body = self._fetch_page(target) + aweme_id = _extract_aweme_id(final_url) or _extract_aweme_id(body) or _extract_aweme_id(target) + payloads = _extract_json_payloads(body) + focus_nodes = _find_nodes_for_aweme(payloads, aweme_id) if aweme_id else [] + media = _extract_media(focus_nodes or payloads) + if not media: + media = _extract_media_from_raw_html(body) + + files = _dedupe_files(media) + if not files: + raise PluginParseError( + "No Douyin video or image media was found. Login, captcha, or page changes may be required." + ) + + title = _first_text(focus_nodes or payloads, ("desc", "title", "caption")) or "Douyin" + author = _first_text(focus_nodes or payloads, ("nickname", "author_name", "unique_id")) or "" + author_id = _first_text(focus_nodes or payloads, ("uid", "sec_uid", "short_id", "unique_id")) or "" + created_at = _first_text(focus_nodes or payloads, ("create_time", "createTime")) + + return [ + Resource( + id=aweme_id or str(abs(hash(final_url))), + url=final_url, + title=title, + author=author, + author_id=author_id, + files=files, + metadata={ + "source_platform": "douyin", + "aweme_id": aweme_id or "", + "kind": "douyin", + "original_url": target, + }, + thumbnail=next( + (item.get("thumbnail") or item.get("url") for item in files if item.get("type") == "image"), + None, + ), + created_at=str(created_at) if created_at else None, + ) + ] + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + save_path.mkdir(parents=True, exist_ok=True) + downloaded: list[Path] = [] + with httpx.Client(timeout=max(30.0, self.config.timeout), follow_redirects=True, headers=self.get_headers()) as client: + for index, item in enumerate(resource.files, start=1): + media_url = str(item.get("url") or "").strip() + if not media_url: + continue + suffix = _guess_suffix(media_url, str(item.get("type") or "")) + target = save_path / f"{_safe_filename(resource.id)}_{index:02d}{suffix}" + if target.exists() and target.stat().st_size > 512: + downloaded.append(target) + continue + try: + response = client.get(media_url, headers={**self.get_headers(), "Referer": resource.url}) + response.raise_for_status() + except Exception as exc: + raise PluginDownloadError(f"Douyin media download failed: {exc}") from exc + content_type = response.headers.get("content-type", "").lower() + if "text/html" in content_type: + raise PluginDownloadError("Douyin media download returned HTML. Login or captcha may be required.") + target.write_bytes(response.content) + if target.stat().st_size <= 512: + target.unlink(missing_ok=True) + raise PluginDownloadError("Douyin media download was too small and likely failed.") + downloaded.append(target) + return downloaded + + def _fetch_page(self, url: str) -> tuple[str, str]: + try: + with httpx.Client( + timeout=self.config.timeout, + follow_redirects=True, + max_redirects=self.config.max_redirects, + headers=self.get_headers(), + ) as client: + response = client.get(url) + response.raise_for_status() + return str(response.url), response.text + except Exception as exc: + raise PluginParseError(f"Douyin page load failed: {exc}") from exc + + def _cookie_header(self) -> str: + cookie_file = Path(self.config.cookie_file or "") + if not cookie_file.is_file(): + return "" + try: + cookies = parse_cookie_text(cookie_file.read_text(encoding="utf-8")) + return cookies_to_header(cookies, ("douyin.com", "iesdouyin.com", "amemv.com")) + except Exception: + return "" + + +def _extract_first_douyin_url(text: str) -> str: + value = (text or "").strip() + match = re.search(r"https?://[^\s\"'<>]*?(?:douyin|iesdouyin|amemv)\.com/[^\s\"'<>]*", value, re.I) + if match: + return match.group(0).rstrip(",.;)") + if "douyin.com" in value.lower() and not value.startswith(("http://", "https://")): + return "https://" + value.lstrip("/") + return value if value.startswith(("http://", "https://")) else "" + + +def _extract_aweme_id(text: str) -> str: + match = _AWEME_ID_RE.search(text or "") + if match: + return match.group(1) + try: + query = parse_qs(urlparse(text).query) + except Exception: + return "" + for key in ("aweme_id", "modal_id", "item_id"): + values = query.get(key) + if values and str(values[0]).isdigit(): + return str(values[0]) + return "" + + +def _extract_json_payloads(page: str) -> list[Any]: + payloads: list[Any] = [] + for match in _JSON_SCRIPT_RE.finditer(page or ""): + raw = html.unescape(match.group("body") or "").strip() + if match.group("id") == "RENDER_DATA": + raw = unquote(raw) + try: + payloads.append(json.loads(raw)) + except Exception: + continue + + for pattern in ( + r"window\.__INITIAL_STATE__\s*=\s*({.*?})\s*", + r"window\._ROUTER_DATA\s*=\s*({.*?})\s*", + ): + for match in re.finditer(pattern, page or "", re.DOTALL): + try: + payloads.append(json.loads(match.group(1))) + except Exception: + continue + return payloads + + +def _find_nodes_for_aweme(payloads: list[Any], aweme_id: str) -> list[Any]: + if not aweme_id: + return [] + matches: list[Any] = [] + + def walk(node: Any) -> None: + if isinstance(node, dict): + values = { + str(node.get(key) or "") + for key in ("aweme_id", "awemeId", "id", "item_id", "itemId", "modal_id", "modalId") + } + if aweme_id in values: + matches.append(node) + for value in node.values(): + walk(value) + elif isinstance(node, list): + for item in node: + walk(item) + + for payload in payloads: + walk(payload) + return matches + + +def _extract_media(payloads: list[Any]) -> list[dict[str, str]]: + files: list[dict[str, str]] = [] + + def add_video(node: dict[str, Any]) -> None: + for key in ("play_addr", "download_addr", "playApi", "video", "videoModel"): + urls = _urls_from_node(node.get(key)) + if urls: + files.append({"type": "video", "url": _normalize_url(urls[0])}) + return + + def add_images(node: dict[str, Any]) -> None: + for key in ("images", "image_post_info", "image", "imageAlbum"): + for image_url in _urls_from_node(node.get(key)): + files.append({"type": "image", "url": _normalize_url(image_url)}) + + def walk(node: Any) -> None: + if isinstance(node, dict): + add_images(node) + if any(key in node for key in ("play_addr", "download_addr", "video", "playApi", "videoModel")): + add_video(node) + for value in node.values(): + walk(value) + elif isinstance(node, list): + for item in node: + walk(item) + + for payload in payloads: + walk(payload) + return files + + +def _extract_media_from_raw_html(page: str) -> list[dict[str, str]]: + files: list[dict[str, str]] = [] + for raw in _JSON_URL_RE.findall(page or ""): + url = _normalize_url(raw) + low = url.lower() + if any(token in low for token in ("douyin", "byteimg", "pstatp", "douyinvod", "amemv", "douyinpic")): + if any(token in low for token in (".mp4", "playwm", "play/?", "video_id=")): + files.append({"type": "video", "url": url}) + elif any(token in low for token in (".jpeg", ".jpg", ".png", ".webp", "image")): + files.append({"type": "image", "url": url}) + return files + + +def _urls_from_node(node: Any) -> list[str]: + if node is None: + return [] + if isinstance(node, str): + value = _normalize_url(node) + return [value] if value.startswith(("http://", "https://", "//")) else [] + if isinstance(node, list): + urls: list[str] = [] + for item in node: + urls.extend(_urls_from_node(item)) + return urls + if isinstance(node, dict): + urls: list[str] = [] + for key in ( + "url_list", + "urlList", + "urls", + "uri", + "url", + "src", + "download_url", + "downloadUrl", + "play_addr", + "playAddr", + "origin_cover", + "cover", + ): + urls.extend(_urls_from_node(node.get(key))) + return urls + return [] + + +def _dedupe_files(files: list[dict[str, str]]) -> list[dict[str, str]]: + video: dict[str, str] | None = None + images: list[dict[str, str]] = [] + seen: set[str] = set() + for item in files: + url = _normalize_url(item.get("url", "")) + if not url or url in seen: + continue + seen.add(url) + media_type = item.get("type", "image") + row = {"type": media_type, "url": url} + if media_type == "video": + if video is None: + video = row + continue + images.append(row) + return ([video] if video else []) + images + + +def _first_text(payloads: list[Any], keys: tuple[str, ...]) -> str: + result = "" + + def walk(node: Any) -> None: + nonlocal result + if result: + return + if isinstance(node, dict): + for key in keys: + value = node.get(key) + if isinstance(value, (str, int)) and str(value).strip(): + result = str(value).strip() + return + for value in node.values(): + walk(value) + elif isinstance(node, list): + for item in node: + walk(item) + + for payload in payloads: + walk(payload) + return result + + +def _normalize_url(url: str) -> str: + value = html.unescape(str(url or "")).strip().replace("\\/", "/") + if value.startswith("//"): + value = "https:" + value + return value + + +def _guess_suffix(url: str, media_type: str) -> str: + path = urlparse(url).path.lower() + suffix = Path(path).suffix + if suffix in {".jpg", ".jpeg", ".png", ".webp", ".gif", ".mp4", ".mov", ".m4v"}: + return suffix + return ".mp4" if media_type == "video" else ".jpg" + + +def _safe_filename(value: str) -> str: + return re.sub(r"[^A-Za-z0-9_.-]+", "_", value or "douyin").strip("._") or "douyin" + + +plugin_class = DouyinPlugin diff --git a/plugins/pixiv/__init__.py b/plugins/pixiv/__init__.py new file mode 100644 index 0000000..7e6b5c4 --- /dev/null +++ b/plugins/pixiv/__init__.py @@ -0,0 +1 @@ +"""Bundled Pixiv plugin package.""" diff --git a/plugins/pixiv/manifest.json b/plugins/pixiv/manifest.json new file mode 100644 index 0000000..1005019 --- /dev/null +++ b/plugins/pixiv/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "Pixiv", + "version": "2.0.0", + "domain": "pixiv.net", + "author": "Sakura Downloader", + "description": "Pixiv illustrations, novels, user batches and tag search via existing core modules.", + "requires_auth": true, + "tags": ["pixiv", "illustration"], + "min_platform_version": "1.0.0", + "dependencies": [] +} diff --git a/plugins/pixiv/plugin.py b/plugins/pixiv/plugin.py new file mode 100644 index 0000000..3683bf8 --- /dev/null +++ b/plugins/pixiv/plugin.py @@ -0,0 +1,253 @@ +""" +Pixiv site plugin — wraps existing pixiv_app downloader helpers (no rewrite). +""" + +from __future__ import annotations + +import re +from pathlib import Path +from typing import Any +from urllib.parse import unquote, urlsplit + +from pixiv_app.core.downloader import ( + download_binary, + download_novel, + fetch_illust_details, + fetch_illust_pages, + fetch_novel_series_ids, + fetch_user_work_ids, + parse_pixiv_target, + search_illust_ids_by_keyword, +) +from pixiv_app.core.plugin.base import ( + BasePlugin, + PluginParseError, + Resource, +) + +try: + from pixiv_app.core.fingerprint import default_profile +except Exception: # pragma: no cover + default_profile = None + + +_TAG_PATH_RE = re.compile(r"/tags/([^/]+)/?", re.IGNORECASE) + + +class PixivPlugin(BasePlugin): + domain = "pixiv.net" + name = "Pixiv" + version = "2.0.0" + + def __init__(self, cookie: str = "", proxy_pool: Any = None) -> None: + self.cookie = cookie + self.proxy_pool = proxy_pool + + def set_auth(self, cookie: str) -> None: + self.cookie = cookie or "" + + def set_proxy_pool(self, proxy_pool: Any) -> None: + self.proxy_pool = proxy_pool + + def can_handle(self, url: str) -> bool: + text = url.strip() + if not text: + return False + if text.isdigit(): + return True + candidate = text if "://" in text else f"https://{text.lstrip('/')}" + try: + hostname = (urlsplit(candidate).hostname or "").lower().rstrip(".") + except ValueError: + return False + return any(hostname == domain or hostname.endswith(f".{domain}") for domain in ("pixiv.net", "pximg.net")) + + def get_headers(self) -> dict[str, str]: + from pixiv_app.core.downloader import build_headers + + headers = build_headers(cookie=self.cookie) + if default_profile: + headers.update(default_profile("pixiv").headers()) + return headers + + def parse(self, url: str) -> list[Resource]: + raw = url.strip() + if not raw: + raise PluginParseError("Empty URL") + + if _TAG_PATH_RE.search(raw): + m = _TAG_PATH_RE.search(raw) + assert m is not None + tag = unquote(m.group(1)) + return self._parse_tag(tag) + + try: + kind, num_id = parse_pixiv_target(raw) + except ValueError as exc: + raise PluginParseError(str(exc)) from exc + + if kind == "novel": + return [self._resource_novel_stub(num_id)] + + if kind == "novel_series": + return self._parse_novel_series(num_id) + + if kind == "user": + return self._parse_user(num_id, input_kind="user") + + if kind == "illust": + return [self._parse_illust(num_id)] + + if kind == "unknown": + return [self._parse_illust(num_id)] + + raise PluginParseError(f"Unsupported Pixiv target kind: {kind}") + + def _parse_illust(self, illust_id: int) -> Resource: + detail = fetch_illust_details(illust_id, cookie=self.cookie, proxy_pool=self.proxy_pool) + page_urls = fetch_illust_pages(illust_id, cookie=self.cookie, proxy_pool=self.proxy_pool) + files = [{"url": u, "page": i} for i, u in enumerate(page_urls) if u] + thumb = page_urls[0] if page_urls else None + return Resource( + id=str(illust_id), + url=f"https://www.pixiv.net/artworks/{illust_id}", + title=str(detail.get("title") or ""), + author=str(detail.get("userName") or ""), + author_id=str(detail.get("userId") or ""), + files=files, + metadata={**detail, "kind": "illust", "illust_id": illust_id}, + thumbnail=thumb, + created_at=str(detail.get("createDate") or detail.get("uploadDate") or "") or None, + ) + + def _parse_user(self, seed_id: int, *, input_kind: str) -> list[Resource]: + resolved_uid, work_ids = fetch_user_work_ids( + seed_id, + cookie=self.cookie, + proxy_pool=self.proxy_pool, + input_kind=input_kind, + ) + out: list[Resource] = [] + for wid in work_ids: + out.append( + Resource( + id=str(wid), + url=f"https://www.pixiv.net/artworks/{wid}", + title="", + author="", + author_id=str(resolved_uid), + files=[], + metadata={ + "kind": "illust_lazy", + "illust_id": wid, + "resolved_user_id": resolved_uid, + }, + ) + ) + return out + + def _parse_tag(self, tag: str) -> list[Resource]: + ids = search_illust_ids_by_keyword( + tag, + cookie=self.cookie, + proxy_pool=self.proxy_pool, + limit=50, + ) + resources: list[Resource] = [] + for iid in ids: + resources.append( + Resource( + id=str(iid), + url=f"https://www.pixiv.net/artworks/{iid}", + title="", + author="", + author_id="", + files=[], + metadata={"kind": "illust_lazy", "illust_id": iid, "tag": tag}, + ) + ) + return resources + + def _parse_novel_series(self, series_id: int) -> list[Resource]: + series_title, novel_ids = fetch_novel_series_ids( + series_id, + cookie=self.cookie, + proxy_pool=self.proxy_pool, + ) + resources: list[Resource] = [] + for chapter_order, novel_id in enumerate(novel_ids, start=1): + resource = self._resource_novel_stub(novel_id) + resource.metadata.update( + { + "series_id": str(series_id), + "series_title": series_title, + "chapter_order": chapter_order, + } + ) + resources.append(resource) + return resources + + def _resource_novel_stub(self, novel_id: int) -> Resource: + return Resource( + id=str(novel_id), + url=f"https://www.pixiv.net/novel/show.php?id={novel_id}", + title="", + author="", + author_id="", + files=[], + metadata={"kind": "novel", "novel_id": novel_id}, + ) + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + save_path.mkdir(parents=True, exist_ok=True) + kind = resource.metadata.get("kind") + + if kind == "novel": + novel_id = int(resource.metadata.get("novel_id") or resource.id) + download_novel( + novel_id, + cookie=self.cookie, + save_root=save_path, + save_subdir="novels", + proxy_pool=self.proxy_pool, + library_db_path=None, + metadata_overrides={ + "seriesId": resource.metadata.get("series_id"), + "seriesTitle": resource.metadata.get("series_title"), + "chapterOrder": resource.metadata.get("chapter_order"), + }, + ) + matches = list(save_path.rglob(f"*{novel_id}.txt")) + return matches + + illust_id = int(resource.metadata.get("illust_id") or resource.id) + + if kind == "illust_lazy" or not resource.files: + detail = fetch_illust_details(illust_id, cookie=self.cookie, proxy_pool=self.proxy_pool) + page_urls = fetch_illust_pages(illust_id, cookie=self.cookie, proxy_pool=self.proxy_pool) + resource.files = [{"url": u, "page": i} for i, u in enumerate(page_urls) if u] + resource.metadata.update(detail) + + downloaded: list[Path] = [] + subdir = resource.metadata.get("save_subdir") + target_dir = save_path / subdir if subdir else save_path + target_dir.mkdir(parents=True, exist_ok=True) + + for item in resource.files: + file_url = item.get("url") + if not file_url: + continue + ok, _msg = download_binary( + file_url, + save_dir=target_dir, + cookie=self.cookie, + proxy_pool=self.proxy_pool, + ) + if ok: + fname = file_url.rsplit("/", 1)[-1].split("?")[0] + downloaded.append(target_dir / fname) + + return downloaded + + +plugin_class = PixivPlugin diff --git a/plugins/taobao/manifest.json b/plugins/taobao/manifest.json new file mode 100644 index 0000000..5164f02 --- /dev/null +++ b/plugins/taobao/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "Taobao", + "version": "1.0.0", + "domain": "taobao.com", + "author": "Sakura Downloader", + "description": "Taobao and Tmall product image/video parser with optional local Cookie support.", + "requires_auth": false, + "tags": ["shopping", "image", "video"], + "min_platform_version": "1.0.0", + "dependencies": [] +} diff --git a/plugins/taobao/plugin.py b/plugins/taobao/plugin.py new file mode 100644 index 0000000..73b0d32 --- /dev/null +++ b/plugins/taobao/plugin.py @@ -0,0 +1,400 @@ +"""Taobao and Tmall product parser/downloader. + +The plugin parses public product pages and optionally reuses a locally saved +Taobao cookie. Tests mock every HTTP request; no live site is contacted there. +""" + +from __future__ import annotations + +import html +import json +import re +from dataclasses import dataclass +from pathlib import Path +from typing import Any +from urllib.parse import parse_qs, urlparse + +import httpx + +from pixiv_app.core.cookie_import import cookies_to_header, parse_cookie_text +from pixiv_app.core.paths import runtime_path +from pixiv_app.core.plugin.base import BasePlugin, PluginDownloadError, PluginParseError, Resource + + +_ALLOWED_DOMAINS = ("taobao.com", "tmall.com", "tb.cn") +_TITLE_KEYS = {"title", "itemtitle", "item_title", "subject", "goodsname", "goods_name"} +_AUTHOR_KEYS = {"shopname", "shop_name", "sellername", "seller_name", "nick", "nickname"} +_ID_KEYS = {"itemid", "item_id", "nid", "auctionid", "auction_id", "goodsid", "goods_id"} +_PRICE_KEYS = {"price", "promotionprice", "promotion_price", "saleprice", "sale_price"} +_IMAGE_KEYS = { + "images", + "image", + "pics", + "picurl", + "pic_url", + "imageurl", + "image_url", + "itemimages", + "item_images", + "detailimages", + "detail_images", + "mainimage", + "main_image", +} +_VIDEO_KEYS = {"video", "videos", "videourl", "video_url", "videoid", "video_id"} +_URL_RE = re.compile(r"(?:https?:)?(?:\\?/){2}[^\s\"'<>]+", re.IGNORECASE) +_JSON_SCRIPT_RE = re.compile( + r"]*(?:type=[\"']application/(?:ld\+)?json[\"']|id=[\"']__NEXT_DATA__[\"'])[^>]*>" + r"(?P.*?)", + re.IGNORECASE | re.DOTALL, +) +_META_RE = re.compile( + r"]+(?:property|name)=[\"'](?Pog:title|og:image|description|og:description)[\"']" + r"[^>]+content=[\"'](?P.*?)[\"'][^>]*>", + re.IGNORECASE | re.DOTALL, +) +_IMG_RE = re.compile( + r"]+(?:data-src|data-original|data-ks-lazyload|src)=[\"'](?P[^\"']+)[\"']", + re.IGNORECASE, +) + + +@dataclass +class TaobaoPluginConfig: + timeout: float = 25.0 + max_redirects: int = 8 + cookie_file: str = "" + max_media: int = 100 + + +class TaobaoPlugin(BasePlugin): + domain = "taobao.com" + name = "Taobao" + version = "1.0.0" + + def __init__(self) -> None: + self.config = TaobaoPluginConfig(cookie_file=str(runtime_path("taobao_cookies.txt"))) + + def can_handle(self, url: str) -> bool: + target = _extract_first_product_url(url) + if not target: + return False + try: + host = (urlparse(target).hostname or "").lower() + except ValueError: + return False + return _allowed_host(host) + + def get_headers(self) -> dict[str, str]: + headers = { + "User-Agent": ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" + ), + "Accept": "text/html,application/xhtml+xml,application/json;q=0.9,image/avif,image/webp,*/*;q=0.8", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.7", + "Referer": "https://www.taobao.com/", + } + cookie = self._cookie_header() + if cookie: + headers["Cookie"] = cookie + return headers + + def parse(self, url: str) -> list[Resource]: + target = _extract_first_product_url(url) + if not target or not self.can_handle(target): + raise PluginParseError("未识别到有效的淘宝或天猫商品链接。") + + final_url, page = self._fetch_page(target) + if not self.can_handle(final_url): + raise PluginParseError("淘宝短链接跳转到了不受支持的站点。") + if _looks_like_challenge(page, final_url): + raise PluginParseError("淘宝要求登录或安全验证,请导入有效 Cookie 后重试。") + + payloads = _extract_json_payloads(page) + extracted = _extract_structured_fields(payloads) + meta = _extract_meta(page) + product_id = _extract_item_id(final_url) or extracted["id"] or _extract_item_id(target) + title = extracted["title"] or meta.get("og:title", "") + title = _clean_title(title) or f"淘宝商品 {product_id or ''}".strip() + author = extracted["author"] + description = meta.get("og:description") or meta.get("description") or "" + + files = list(extracted["files"]) + if meta.get("og:image"): + files.insert(0, {"type": "image", "url": _normalize_url(meta["og:image"])}) + for match in _IMG_RE.finditer(page): + files.append({"type": "image", "url": _normalize_url(match.group("url"))}) + files.extend(_extract_media_from_html(page)) + files = _dedupe_media(files, limit=self.config.max_media) + if not files: + raise PluginParseError("商品页面已打开,但没有解析到商品图片或视频,页面结构可能已更新。") + + thumbnail = next((item["url"] for item in files if item["type"] == "image"), None) + return [ + Resource( + id=product_id or str(abs(hash(final_url))), + url=final_url, + title=title, + author=author, + author_id="", + files=files, + metadata={ + "source_platform": "taobao", + "kind": "product", + "product_id": product_id, + "price": extracted["price"], + "description": description, + "original_url": target, + }, + thumbnail=thumbnail, + ) + ] + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + save_path.mkdir(parents=True, exist_ok=True) + downloaded: list[Path] = [] + headers = self.get_headers() + with httpx.Client(timeout=max(30.0, self.config.timeout), follow_redirects=True, headers=headers) as client: + for index, item in enumerate(resource.files, start=1): + media_url = _normalize_url(str(item.get("url") or "")) + if not media_url: + continue + media_type = str(item.get("type") or "image") + suffix = _guess_suffix(media_url, media_type) + target = save_path / f"{_safe_filename(resource.id)}_{index:03d}{suffix}" + if target.is_file() and target.stat().st_size > 256: + downloaded.append(target) + continue + try: + response = client.get(media_url, headers={**headers, "Referer": resource.url}) + response.raise_for_status() + except Exception as exc: + raise PluginDownloadError(f"淘宝媒体下载失败: {exc}") from exc + content_type = response.headers.get("content-type", "").lower() + if "text/html" in content_type or "application/json" in content_type: + raise PluginDownloadError("淘宝媒体地址返回了验证页面,请更新 Cookie 后重试。") + target.write_bytes(response.content) + if target.stat().st_size <= 256: + target.unlink(missing_ok=True) + raise PluginDownloadError("下载内容过小,可能是失效链接或占位图。") + downloaded.append(target) + return downloaded + + def _fetch_page(self, url: str) -> tuple[str, str]: + try: + with httpx.Client( + timeout=self.config.timeout, + follow_redirects=True, + max_redirects=self.config.max_redirects, + headers=self.get_headers(), + ) as client: + response = client.get(url) + response.raise_for_status() + return str(response.url), response.text + except Exception as exc: + raise PluginParseError(f"淘宝商品页面加载失败: {exc}") from exc + + def _cookie_header(self) -> str: + path = Path(self.config.cookie_file or "") + if not path.is_file(): + return "" + try: + cookies = parse_cookie_text(path.read_text(encoding="utf-8")) + return cookies_to_header(cookies, _ALLOWED_DOMAINS) + except Exception: + return "" + + +def _allowed_host(host: str) -> bool: + return any(host == domain or host.endswith(f".{domain}") for domain in _ALLOWED_DOMAINS) + + +def _extract_first_product_url(text: str) -> str: + value = html.unescape((text or "").strip()) + match = re.search(r"https?://[^\s\"'<>]+", value, re.IGNORECASE) + if match: + return match.group(0).rstrip(",.;)") + if any(domain in value.lower() for domain in _ALLOWED_DOMAINS): + return "https://" + value.lstrip("/") + return "" + + +def _extract_item_id(url: str) -> str: + try: + parsed = urlparse(url) + query = parse_qs(parsed.query) + except ValueError: + return "" + for key in ("id", "itemId", "item_id", "auctionId"): + value = (query.get(key) or [""])[0] + match = re.search(r"\d{5,}", str(value)) + if match: + return match.group(0) + for pattern in (r"/item/(\d{5,})", r"/i(\d{5,})\.htm", r"item_id[=/](\d{5,})"): + match = re.search(pattern, parsed.path, re.IGNORECASE) + if match: + return match.group(1) + return "" + + +def _extract_json_payloads(page: str) -> list[Any]: + payloads: list[Any] = [] + for match in _JSON_SCRIPT_RE.finditer(page or ""): + raw = html.unescape(match.group("body") or "").strip() + try: + payloads.append(json.loads(raw)) + except (TypeError, ValueError): + continue + for pattern in ( + r"window\.__INITIAL_STATE__\s*=\s*({.*?})\s*;?\s*", + r"window\.__INIT_DATA__\s*=\s*({.*?})\s*;?\s*", + ): + for match in re.finditer(pattern, page or "", re.IGNORECASE | re.DOTALL): + try: + payloads.append(json.loads(match.group(1))) + except (TypeError, ValueError): + continue + return payloads + + +def _extract_structured_fields(payloads: list[Any]) -> dict[str, Any]: + titles: list[str] = [] + authors: list[str] = [] + ids: list[str] = [] + prices: list[str] = [] + files: list[dict[str, str]] = [] + + def walk(node: Any) -> None: + if isinstance(node, dict): + for key, value in node.items(): + normalized = str(key).replace("-", "_").lower() + compact = normalized.replace("_", "") + values = _text_values(value) + if normalized in _TITLE_KEYS or compact in _TITLE_KEYS: + titles.extend(values) + if normalized in _AUTHOR_KEYS or compact in _AUTHOR_KEYS: + authors.extend(values) + if normalized in _ID_KEYS or compact in _ID_KEYS: + ids.extend(values) + if normalized in _PRICE_KEYS or compact in _PRICE_KEYS: + prices.extend(values) + if normalized in _IMAGE_KEYS or compact in _IMAGE_KEYS: + files.extend({"type": "image", "url": url} for url in _urls_from_node(value)) + if normalized in _VIDEO_KEYS or compact in _VIDEO_KEYS: + files.extend({"type": "video", "url": url} for url in _urls_from_node(value)) + walk(value) + elif isinstance(node, list): + for item in node: + walk(item) + + for payload in payloads: + walk(payload) + product_id = next((match.group(0) for value in ids if (match := re.search(r"\d{5,}", value))), "") + return { + "title": _best_text(titles), + "author": _best_text(authors), + "id": product_id, + "price": next((value for value in prices if value.strip()), ""), + "files": files, + } + + +def _extract_meta(page: str) -> dict[str, str]: + values: dict[str, str] = {} + for match in _META_RE.finditer(page or ""): + values[match.group("name").lower()] = html.unescape(match.group("content")).strip() + return values + + +def _extract_media_from_html(page: str) -> list[dict[str, str]]: + files: list[dict[str, str]] = [] + for raw in _URL_RE.findall(page or ""): + url = _normalize_url(raw) + lower = url.lower().split("?", 1)[0] + if any(lower.endswith(ext) for ext in (".jpg", ".jpeg", ".png", ".webp", ".avif")): + files.append({"type": "image", "url": url}) + elif any(lower.endswith(ext) for ext in (".mp4", ".m3u8", ".webm")): + files.append({"type": "video", "url": url}) + return files + + +def _urls_from_node(node: Any) -> list[str]: + if isinstance(node, str): + value = _normalize_url(node) + return [value] if value.startswith(("http://", "https://")) else [] + if isinstance(node, list): + return [url for item in node for url in _urls_from_node(item)] + if isinstance(node, dict): + urls: list[str] = [] + for key, value in node.items(): + if str(key).lower() in {"url", "src", "urls", "url_list", "url_list_v2", "image", "video"}: + urls.extend(_urls_from_node(value)) + elif isinstance(value, (dict, list)): + urls.extend(_urls_from_node(value)) + return urls + return [] + + +def _text_values(node: Any) -> list[str]: + if isinstance(node, (str, int, float)): + return [str(node).strip()] + if isinstance(node, dict): + return [str(value).strip() for key, value in node.items() if str(key).lower() in {"text", "value", "name"}] + return [] + + +def _best_text(values: list[str]) -> str: + cleaned = [html.unescape(value).strip() for value in values if value and len(value.strip()) > 1] + return max(cleaned, key=len, default="") + + +def _clean_title(value: str) -> str: + title = re.sub(r"\s*[-_–—]\s*(淘宝网|天猫|tmall|taobao).*$", "", value or "", flags=re.IGNORECASE) + return re.sub(r"\s+", " ", title).strip() + + +def _normalize_url(value: str) -> str: + url = html.unescape((value or "").strip()).replace("\\u002F", "/").replace("\\/", "/") + if url.startswith("//"): + return "https:" + url + return url + + +def _dedupe_media(files: list[dict[str, str]], *, limit: int) -> list[dict[str, str]]: + result: list[dict[str, str]] = [] + seen: set[str] = set() + for item in files: + url = _normalize_url(str(item.get("url") or "")) + if not url.startswith(("http://", "https://")) or url in seen: + continue + lower = url.lower() + if any(token in lower for token in ("sprite", "icon", "logo", "avatar", "qrcode")): + continue + seen.add(url) + result.append({"type": "video" if item.get("type") == "video" else "image", "url": url}) + if len(result) >= max(1, limit): + break + return result + + +def _looks_like_challenge(page: str, url: str) -> bool: + sample = f"{url}\n{page[:20000]}".lower() + markers = ("login.taobao.com", "punish", "captcha", "滑动验证", "安全验证", "请登录") + return any(marker in sample for marker in markers) + + +def _guess_suffix(url: str, media_type: str) -> str: + suffix = Path(urlparse(url).path).suffix.lower() + allowed = {".jpg", ".jpeg", ".png", ".webp", ".avif", ".gif", ".mp4", ".webm", ".m3u8"} + if suffix in allowed: + return suffix + return ".mp4" if media_type == "video" else ".jpg" + + +def _safe_filename(value: str) -> str: + return re.sub(r"[^0-9A-Za-z._-]+", "_", value or "taobao")[:80] or "taobao" + + +plugin_class = TaobaoPlugin + diff --git a/plugins/x/__init__.py b/plugins/x/__init__.py new file mode 100644 index 0000000..8e9ffaa --- /dev/null +++ b/plugins/x/__init__.py @@ -0,0 +1 @@ +"""X (Twitter) Playwright plugin.""" diff --git a/plugins/x/browser_opts.py b/plugins/x/browser_opts.py new file mode 100644 index 0000000..c8f497a --- /dev/null +++ b/plugins/x/browser_opts.py @@ -0,0 +1,113 @@ +"""Shared Playwright launch options for X — reduce automation fingerprint; prefer system Chrome.""" + +from __future__ import annotations + +import os +from pathlib import Path + +from playwright.async_api import BrowserContext, Playwright + +from pixiv_app.core.playwright_runtime import configure_playwright_runtime_env, preferred_chromium_executable + +try: + from pixiv_app.core.fingerprint import default_profile +except Exception: # pragma: no cover - plugin can run standalone + default_profile = None + +try: + from pixiv_app.core.browser_proxy_pool import parse_proxy_url +except Exception: # pragma: no cover + parse_proxy_url = None + + +async def launch_x_persistent_context( + playwright: Playwright, + profile_dir: Path, + *, + headless: bool, +) -> BrowserContext: + """ + Launch persistent Chromium with flags that reduce obvious automation signals. + + Order: ``X_PLAYWRIGHT_CHANNEL`` env (if set), then system Chrome, then bundled Chromium. + Google OAuth often still blocks automation browsers; users should use X email/phone login + or import cookies from a normal browser session. + """ + configure_playwright_runtime_env() + profile_dir.mkdir(parents=True, exist_ok=True) + browser_profile = default_profile("x") if default_profile else None + + base_kw: dict = { + "user_data_dir": str(profile_dir), + "headless": headless, + "viewport": browser_profile.viewport if browser_profile else {"width": 1280, "height": 800}, + "locale": browser_profile.locale if browser_profile else "zh-CN", + "timezone_id": browser_profile.timezone_id if browser_profile else "Asia/Shanghai", + "screen": browser_profile.viewport if browser_profile else {"width": 1280, "height": 800}, + "extra_http_headers": { + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", + }, + "ignore_default_args": ["--enable-automation"], + "args": [ + "--disable-blink-features=AutomationControlled", + "--window-size=1280,800", + ], + "user_agent": browser_profile.user_agent + if browser_profile + else ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" + ), + } + if browser_profile: + base_kw["device_scale_factor"] = browser_profile.device_scale_factor + base_kw["is_mobile"] = browser_profile.is_mobile + base_kw["has_touch"] = browser_profile.has_touch + + proxy_value = os.environ.get("X_PROXY_URL", os.environ.get("PIXIV_BROWSER_PROXY", "")).strip() + if proxy_value and parse_proxy_url: + parsed_proxy = parse_proxy_url(proxy_value) + if parsed_proxy is not None: + base_kw["proxy"] = parsed_proxy.to_playwright_proxy() + + env_ch = os.environ.get("X_PLAYWRIGHT_CHANNEL", "").strip() + local_executable = preferred_chromium_executable() + seen: set[str] = set() + attempts: list[tuple[str, dict]] = [] + for label, extra in ( + ("env_channel", {"channel": env_ch} if env_ch else {}), + ("installed_chromium", {"executable_path": str(local_executable)} if local_executable else {}), + ("system_chrome", {"channel": "chrome"}), + ("default", {}), + ): + key = f"{label}:{extra!r}" + if key in seen: + continue + seen.add(key) + attempts.append((label, extra)) + + last_err: Exception | None = None + for _, extra in attempts: + kw = dict(base_kw) + kw.update(extra) + try: + ctx = await playwright.chromium.launch_persistent_context(**kw) + await _apply_stealth_scripts(ctx) + return ctx + except Exception as exc: + last_err = exc + continue + + assert last_err is not None + raise last_err + + +async def _apply_stealth_scripts(ctx: BrowserContext) -> None: + """Best-effort scripts; cannot guarantee OAuth providers will accept the browser.""" + await ctx.add_init_script( + """ + try { + Object.defineProperty(navigator, 'webdriver', { get: () => undefined }); + } catch (e) {} + """ + ) diff --git a/plugins/x/manifest.json b/plugins/x/manifest.json new file mode 100644 index 0000000..ca29ac3 --- /dev/null +++ b/plugins/x/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "X", + "version": "1.0.0", + "domain": "twitter.com", + "author": "Sakura Downloader", + "description": "X (Twitter) media via Playwright + httpx. Session in runtime/x_profile.", + "requires_auth": true, + "tags": ["social", "media", "playwright"], + "min_platform_version": "1.0.0", + "dependencies": [] +} diff --git a/plugins/x/playwright_client.py b/plugins/x/playwright_client.py new file mode 100644 index 0000000..af8c869 --- /dev/null +++ b/plugins/x/playwright_client.py @@ -0,0 +1,946 @@ +"""Playwright helpers for the X plugin.""" + +from __future__ import annotations + +import asyncio +import json +import random +import re +from pathlib import Path +from typing import TYPE_CHECKING, Any +from urllib.parse import urljoin, urlparse + +import httpx +from playwright.async_api import BrowserContext, Page, TimeoutError as PlaywrightTimeoutError, async_playwright + +from .browser_opts import launch_x_persistent_context + +from pixiv_app.core.plugin.base import PluginParseError + +try: + from pixiv_app.core.fingerprint import default_profile +except Exception: # pragma: no cover + default_profile = None + +_GUEST_WALL_HINT = ( + "X 需要先登录。\n" + "请在“插件管理”里点击“X 登录”完成登录,或把 Cookie 导出为 JSON 保存到 " + "runtime/x_cookies.json 后重试。" +) + +if TYPE_CHECKING: + pass + + +class XPlaywrightClient: + def __init__(self, config: Any) -> None: + self.config = config + self._pw: Any = None + self.context: BrowserContext | None = None + self._response_store: dict[int, list[dict[str, Any]]] = {} + self._response_tasks: dict[int, set[asyncio.Task[Any]]] = {} + + async def start(self) -> None: + if self.context is not None: + return + self._pw = await async_playwright().start() + profile = Path(self.config.profile_dir) + profile.mkdir(parents=True, exist_ok=True) + + self.context = await launch_x_persistent_context( + self._pw, + profile, + headless=self.config.headless, + ) + + cookies = self._prepare_cookie_items(self._load_cookie_items()) + if cookies: + loaded = await self._add_cookies_tolerant(cookies) + if loaded <= 0: + raise PluginParseError( + "X Cookie 注入失败:请重新导出 x.com cookies.txt,确认包含 auth_token 和 ct0 后再导入。" + ) + + async def close(self) -> None: + if self.context is not None: + await self.context.close() + self.context = None + self._response_tasks.clear() + self._response_store.clear() + if self._pw is not None: + await self._pw.stop() + self._pw = None + + async def new_page_goto(self, url: str, *, wait_until: str = "domcontentloaded") -> Page: + assert self.context is not None + page = await self.context.new_page() + self._attach_response_collector(page) + timeout = getattr(self.config, "wait_timeout", 30000) + try: + await page.goto(url, wait_until=wait_until, timeout=timeout) + except PlaywrightTimeoutError as exc: + if page.url and page.url != "about:blank": + await asyncio.sleep(2.0) + return page + try: + await page.goto(url, wait_until="commit", timeout=min(timeout, 15000)) + except PlaywrightTimeoutError as retry_exc: + raise PluginParseError( + "X 页面加载超时,可能是网络较慢、访问过快触发风控,或登录会话已失效。" + ) from retry_exc + except Exception as retry_exc: + raise PluginParseError(f"X 页面加载失败: {retry_exc}") from retry_exc + if page.url == "about:blank": + raise PluginParseError("X 页面没有成功打开,请稍后重试。") from exc + await self.human_pause(2.0, 4.5) + await self.move_mouse_softly(page) + return page + + async def human_pause(self, low: float | None = None, high: float | None = None) -> None: + low = float(low if low is not None else getattr(self.config, "min_action_delay", 3.0)) + high = float(high if high is not None else getattr(self.config, "max_action_delay", 8.0)) + if high < low: + high = low + await asyncio.sleep(random.uniform(low, high)) + + async def move_mouse_softly(self, page: Page) -> None: + try: + viewport = page.viewport_size or {"width": 1280, "height": 800} + width = int(viewport.get("width", 1280)) + height = int(viewport.get("height", 800)) + for _ in range(random.randint(1, 3)): + x = random.randint(max(12, width // 8), max(20, width - 24)) + y = random.randint(max(12, height // 8), max(20, height - 24)) + await page.mouse.move(x, y, steps=random.randint(8, 18)) + await asyncio.sleep(random.uniform(0.2, 0.7)) + except Exception: + pass + + async def human_scroll_page(self, page: Page) -> float: + await self.move_mouse_softly(page) + viewport = page.viewport_size or {"height": 800} + height = int(viewport.get("height", 800)) + delta = random.randint(max(260, height // 2), max(420, int(height * 1.2))) + if random.random() < 0.12: + delta = -random.randint(120, 280) + await page.mouse.wheel(0, delta) + if random.random() < 0.07: + await asyncio.sleep(random.uniform(30.0, 90.0)) + await self.human_pause() + return float(await page.evaluate("document.body.scrollHeight")) + + async def raise_if_guest_wall(self, page: Page) -> None: + await asyncio.sleep(0.45) + try: + url = page.url or "" + if "/i/flow/login" in url or "/i/flow/signup" in url: + raise PluginParseError(_GUEST_WALL_HINT) + except PluginParseError: + raise + except Exception: + pass + + try: + wall = page.get_by_text( + re.compile( + r"注册 X|Sign up for X|登录|Sign in|Phone, email, or username", + re.I, + ) + ).first + if await wall.is_visible(timeout=2200): + raise PluginParseError(_GUEST_WALL_HINT) + except PluginParseError: + raise + except Exception: + pass + + try: + dialog = page.locator('[role="dialog"]').first + if await dialog.is_visible(timeout=600): + inner = (await dialog.inner_text())[:800] + if ("注册" in inner or "Sign up" in inner) and ("Google" in inner or "Apple" in inner or "Next" in inner): + raise PluginParseError(_GUEST_WALL_HINT) + except PluginParseError: + raise + except Exception: + pass + + async def wait_for_main_content(self, page: Page) -> None: + timeout = min(15000, getattr(self.config, "wait_timeout", 30000)) + for sel in ('article[data-testid="tweet"]', "article", '[data-testid="tweetPhoto"]'): + try: + await page.wait_for_selector(sel, timeout=timeout) + return + except Exception: + continue + + async def wait_for_media(self, page: Page) -> bool: + timeout = 10000 + for sel in ( + '[data-testid="tweetPhoto"]', + 'img[src*="pbs.twimg.com/media"]', + "video", + "video source", + ): + try: + await page.wait_for_selector(sel, timeout=timeout) + return True + except Exception: + continue + return False + + async def extract_media_urls(self, page: Page) -> list[str]: + urls: list[str] = [] + seen: set[str] = set() + + imgs = await page.query_selector_all('img[src*="pbs.twimg.com/media"]') + for img in imgs: + src = await img.get_attribute("src") + if src and "profile_images" not in src: + orig = self._original_tw_image_url(src) + if orig and orig not in seen: + seen.add(orig) + urls.append(orig) + + for sel in ('[data-testid="tweetPhoto"] img', "article img"): + elements = await page.query_selector_all(sel) + for el in elements: + src = await el.get_attribute("src") + if src and "pbs.twimg.com/media" in src: + orig = self._original_tw_image_url(src) + if orig and orig not in seen: + seen.add(orig) + urls.append(orig) + + try: + raw_video_urls = await page.evaluate( + """ + () => { + const values = []; + for (const el of document.querySelectorAll('video, video source')) { + const candidates = [ + el.currentSrc, + el.src, + el.getAttribute?.('src'), + el.getAttribute?.('data-src'), + el.getAttribute?.('poster'), + ]; + for (const value of candidates) { + if (typeof value === 'string' && value.trim()) { + values.push(value.trim()); + } + } + } + return values; + } + """ + ) + except Exception: + raw_video_urls = [] + + for src in raw_video_urls: + normalized = self._normalize_media_url(str(src)) + if normalized and self._is_video_url(normalized) and normalized not in seen: + seen.add(normalized) + urls.append(normalized) + + try: + html = await page.content() + except Exception: + html = "" + + for image_url in self._extract_image_urls_from_html(html): + if image_url not in seen: + seen.add(image_url) + urls.append(image_url) + + for pattern in ( + r"https?://video\.twimg\.com/[^\s\"'<>\\]+", + r"https:\\/\\/video\.twimg\.com\\/[^\s\"'<>]+", + ): + for match in re.findall(pattern, html): + normalized = self._normalize_media_url(match) + if normalized and self._is_video_url(normalized) and normalized not in seen: + seen.add(normalized) + urls.append(normalized) + + return urls + + async def extract_media_urls_from_api_responses(self, page: Page) -> list[str]: + await self.wait_for_response_capture(page) + payloads = self._response_store.get(id(page), []) + if not payloads: + return [] + + urls: list[str] = [] + seen: set[str] = set() + + def add(url: str | None) -> None: + if not url: + return + normalized = self._normalize_media_url(str(url)) + if not normalized: + return + if "pbs.twimg.com/media" in normalized: + normalized = self._original_tw_image_url(normalized) + if normalized not in seen: + seen.add(normalized) + urls.append(normalized) + + for payload in payloads: + data = payload.get("json") + if data is None: + continue + + for media_item in self._find_media_entities(data): + media_url = media_item.get("media_url_https") or media_item.get("media_url") + media_type = str(media_item.get("type") or "").lower() + if media_url and media_type in {"photo", "animated_gif", "video", ""}: + add(str(media_url)) + + video_info = media_item.get("video_info") + if isinstance(video_info, dict): + best_video = self._pick_best_video_variant(video_info) + add(best_video) + + return urls + + async def extract_media_records_from_api_responses(self, page: Page) -> list[dict[str, Any]]: + await self.wait_for_response_capture(page) + payloads = self._response_store.get(id(page), []) + if not payloads: + return [] + + records: list[dict[str, Any]] = [] + seen: set[tuple[str, str]] = set() + + def add_record(kind: str, url: str | None, *, thumbnail: str | None = None) -> None: + if not url: + return + normalized = self._normalize_media_url(str(url)) + if not normalized: + return + key = (kind, normalized) + if key in seen: + return + seen.add(key) + records.append( + { + "type": kind, + "url": normalized, + "thumbnail": self._normalize_media_url(thumbnail) if thumbnail else None, + } + ) + + for payload in payloads: + data = payload.get("json") + if data is None: + continue + + for media_item in self._find_media_entities(data): + media_type = str(media_item.get("type") or "").lower() + thumb_url = media_item.get("media_url_https") or media_item.get("media_url") + if media_type == "photo": + add_record("image", thumb_url) + continue + + video_info = media_item.get("video_info") + if isinstance(video_info, dict): + best_video = self._pick_best_video_variant(video_info) + if best_video: + add_record("video", best_video, thumbnail=thumb_url) + continue + + return records + + async def wait_for_response_capture(self, page: Page, timeout: float = 2.0) -> None: + page_id = id(page) + tasks = self._response_tasks.get(page_id) + if not tasks: + return + + deadline = asyncio.get_running_loop().time() + timeout + while tasks and asyncio.get_running_loop().time() < deadline: + pending = [task for task in list(tasks) if not task.done()] + if not pending: + break + _, pending = await asyncio.wait(pending, timeout=0.2) + tasks = self._response_tasks.get(page_id) + if not tasks: + break + + async def scroll_and_collect_media(self, page: Page, max_scrolls: int = 12, max_items: int = 40) -> list[str]: + collected: list[str] = [] + seen: set[str] = set() + last_height = 0.0 + for _ in range(max_scrolls): + batch: list[str] = [] + for item in await self.extract_media_records_from_api_responses(page): + url = item.get("url") + if isinstance(url, str) and url: + batch.append(url) + batch.extend(await self.extract_media_urls(page)) + for url in batch: + if url not in seen: + seen.add(url) + collected.append(url) + if len(collected) >= max_items: + return collected + try: + height = await self.human_scroll_page(page) + except Exception: + break + if height == last_height: + break + last_height = float(height) + return collected + + async def scroll_and_collect_tweet_urls(self, page: Page, limit: int = 40) -> list[str]: + found: list[str] = [] + seen: set[str] = set() + stale_rounds = 0 + max_rounds = 25 + + for _ in range(max_rounds): + if len(found) >= limit: + break + before = len(found) + links = await page.query_selector_all('a[href*="/status/"]') + for link in links: + href = await link.get_attribute("href") + if not href or "/status/" not in href: + continue + if href.startswith("/"): + full = f"https://x.com{href}" + elif href.startswith("http"): + full = href + else: + continue + full = re.sub(r"\?.*$", "", full.split("#")[0]) + if full not in seen: + seen.add(full) + found.append(full) + if len(found) >= limit: + break + + if len(found) == before: + stale_rounds += 1 + if stale_rounds >= 5: + break + else: + stale_rounds = 0 + + try: + await self.human_scroll_page(page) + except Exception: + break + + return found[:limit] + + async def get_author_info(self, page: Page) -> dict[str, str]: + info = {"name": "", "username": ""} + try: + block = await page.query_selector('[data-testid="User-Name"]') + if block: + text = await block.inner_text() + lines = [line.strip() for line in text.split("\n") if line.strip()] + if lines: + info["name"] = lines[0] + for line in lines: + if line.startswith("@"): + info["username"] = line.lstrip("@") + break + except Exception: + pass + return info + + async def get_user_display_name(self, page: Page, fallback: str) -> dict[str, str]: + info = {"name": fallback, "username": fallback} + try: + block = await page.query_selector('[data-testid="UserName"]') + if block: + text = await block.inner_text() + lines = [line.strip() for line in text.split("\n") if line.strip()] + if lines: + info["name"] = lines[0] + except Exception: + pass + return info + + async def get_tweet_text(self, page: Page) -> str: + try: + el = await page.query_selector('[data-testid="tweetText"]') + if el: + return (await el.inner_text()).strip() + except Exception: + pass + return "" + + async def get_tweet_date(self, page: Page) -> str: + try: + time_el = await page.query_selector("article time") + if time_el: + return (await time_el.get_attribute("datetime")) or "" + except Exception: + pass + return "" + + async def download_image(self, url: str, save_path: Path, *, referer: str | None = None) -> Path | None: + save_path.mkdir(parents=True, exist_ok=True) + name = url.split("/")[-1].split("?")[0] + if not any(name.lower().endswith(ext) for ext in (".jpg", ".jpeg", ".png", ".webp")): + name += ".jpg" + target = save_path / name + if target.exists() and target.stat().st_size > 256: + return target + + async with httpx.AsyncClient(timeout=60.0, follow_redirects=True) as client: + response = await client.get(url, headers=self._media_request_headers(url, referer=referer)) + content_type = response.headers.get("content-type", "").lower() + if response.status_code == 200 and "text/html" not in content_type: + target.write_bytes(response.content) + if target.stat().st_size > 256: + return target + target.unlink(missing_ok=True) + return None + + async def download_video(self, url: str, save_path: Path, *, referer: str | None = None) -> Path | None: + save_path.mkdir(parents=True, exist_ok=True) + async with httpx.AsyncClient(timeout=120.0, follow_redirects=True) as client: + response = await client.get(url, headers=self._media_request_headers(url, referer=referer)) + if response.status_code != 200: + return None + + final_url = str(response.url) + content_type = response.headers.get("content-type", "").lower() + body_text = response.text if self._looks_like_hls(final_url, content_type, "") or "text" in content_type else "" + if "text/html" in content_type and not self._looks_like_hls(final_url, content_type, body_text): + return None + + if self._looks_like_hls(final_url, content_type, body_text): + return await self._download_hls_playlist( + client, + playlist_url=final_url, + playlist_text=body_text, + save_path=save_path, + referer=referer, + ) + + target = self._video_target_path(final_url, save_path) + if target.exists() and target.stat().st_size > 1024: + return target + + target.write_bytes(response.content) + if target.stat().st_size <= 1024: + target.unlink(missing_ok=True) + return None + return target + + async def _download_hls_playlist( + self, + client: httpx.AsyncClient, + *, + playlist_url: str, + playlist_text: str, + save_path: Path, + referer: str | None, + ) -> Path | None: + selected_url, selected_text = await self._resolve_hls_playlist( + client, + playlist_url=playlist_url, + playlist_text=playlist_text, + referer=referer, + ) + if not selected_text: + return None + + segment_urls, init_url = self._parse_hls_segments(selected_url, selected_text) + if not segment_urls: + return None + + target = self._video_target_path(selected_url, save_path, segment_urls=segment_urls, init_url=init_url) + if target.exists() and target.stat().st_size > 1024: + return target + + with target.open("wb") as fh: + if init_url: + init_resp = await client.get(init_url, headers=self._media_request_headers(init_url, referer=referer)) + if init_resp.status_code != 200: + target.unlink(missing_ok=True) + return None + fh.write(init_resp.content) + + for segment_url in segment_urls: + seg_resp = await client.get( + segment_url, + headers=self._media_request_headers(segment_url, referer=referer), + ) + if seg_resp.status_code != 200: + target.unlink(missing_ok=True) + return None + fh.write(seg_resp.content) + + if target.stat().st_size <= 1024: + target.unlink(missing_ok=True) + return None + return target + + async def _resolve_hls_playlist( + self, + client: httpx.AsyncClient, + *, + playlist_url: str, + playlist_text: str, + referer: str | None, + ) -> tuple[str, str]: + if "#EXT-X-STREAM-INF" not in playlist_text: + return playlist_url, playlist_text + + variants = self._parse_hls_variants(playlist_url, playlist_text) + for variant_url in variants: + resp = await client.get(variant_url, headers=self._media_request_headers(variant_url, referer=referer)) + if resp.status_code == 200: + return str(resp.url), resp.text + return playlist_url, playlist_text + + def _parse_hls_variants(self, playlist_url: str, playlist_text: str) -> list[str]: + variants: list[tuple[int, str]] = [] + pending_bandwidth = 0 + + for raw_line in playlist_text.splitlines(): + line = raw_line.strip() + if not line: + continue + if line.startswith("#EXT-X-STREAM-INF"): + match = re.search(r"BANDWIDTH=(\d+)", line) + pending_bandwidth = int(match.group(1)) if match else 0 + continue + if line.startswith("#"): + continue + variants.append((pending_bandwidth, urljoin(playlist_url, line))) + pending_bandwidth = 0 + + variants.sort(key=lambda item: item[0], reverse=True) + return [url for _, url in variants] + + def _parse_hls_segments(self, playlist_url: str, playlist_text: str) -> tuple[list[str], str | None]: + segments: list[str] = [] + init_url: str | None = None + + for raw_line in playlist_text.splitlines(): + line = raw_line.strip() + if not line: + continue + if line.startswith("#EXT-X-MAP:"): + match = re.search(r'URI="([^"]+)"', line) + if match: + init_url = urljoin(playlist_url, match.group(1)) + continue + if line.startswith("#"): + continue + segments.append(urljoin(playlist_url, line)) + + return segments, init_url + + def _media_request_headers(self, url: str, *, referer: str | None = None) -> dict[str, str]: + headers = { + "User-Agent": self._ua(), + "Accept": "*/*", + "Referer": referer or "https://x.com/", + "Origin": "https://x.com", + } + cookie_header = self._cookie_header(url) + if cookie_header: + headers["Cookie"] = cookie_header + csrf_token = self._cookie_value("ct0") + if csrf_token: + headers["x-csrf-token"] = csrf_token + return headers + + def _cookie_header(self, url: str) -> str: + pairs: list[str] = [] + for item in self._load_cookie_items(): + name = str(item.get("name") or "").strip() + value = str(item.get("value") or "") + if name: + pairs.append(f"{name}={value}") + return "; ".join(pairs) + + def _cookie_value(self, name: str) -> str: + for item in self._load_cookie_items(): + if str(item.get("name") or "") == name: + return str(item.get("value") or "") + return "" + + def _load_cookie_items(self) -> list[dict[str, Any]]: + cookie_path = Path(getattr(self.config, "cookie_file", "") or "") + if not cookie_path.is_file(): + return [] + try: + payload = json.loads(cookie_path.read_text(encoding="utf-8")) + except (json.JSONDecodeError, OSError): + return [] + return payload if isinstance(payload, list) else [] + + async def _add_cookies_tolerant(self, cookies: list[dict[str, Any]]) -> int: + assert self.context is not None + try: + await self.context.add_cookies(cookies) + return len(cookies) + except Exception: + pass + + loaded = 0 + for cookie in cookies: + try: + await self.context.add_cookies([cookie]) + loaded += 1 + except Exception: + continue + return loaded + + @staticmethod + def _prepare_cookie_items(cookies: list[dict[str, Any]]) -> list[dict[str, Any]]: + prepared_by_key: dict[tuple[str, str, str], dict[str, Any]] = {} + for cookie in cookies: + name = str(cookie.get("name") or "").strip() + domain = str(cookie.get("domain") or "").strip() + if not name or not domain: + continue + + item: dict[str, Any] = { + "name": name, + "value": str(cookie.get("value") or ""), + "domain": domain, + "path": str(cookie.get("path") or "/"), + "secure": bool(cookie.get("secure", False)), + "httpOnly": bool(cookie.get("httpOnly", False)), + } + + expires = cookie.get("expires") + if expires is not None: + try: + expires_value = int(float(expires)) + if expires_value > 0: + item["expires"] = expires_value + except (TypeError, ValueError): + pass + + same_site = str(cookie.get("sameSite") or "").strip() + if same_site in {"Strict", "Lax", "None"}: + if same_site == "None" and not item["secure"]: + item["sameSite"] = "Lax" + else: + item["sameSite"] = same_site + + key = (domain.lower(), str(item["path"]), name) + prepared_by_key[key] = item + return list(prepared_by_key.values()) + + def _video_target_path( + self, + url: str, + save_path: Path, + *, + segment_urls: list[str] | None = None, + init_url: str | None = None, + ) -> Path: + parsed = urlparse(url) + name = Path(parsed.path).name or "x_video" + + if name.endswith(".m3u8") or not Path(name).suffix: + if segment_urls: + seg_name = Path(urlparse(segment_urls[0]).path).name + if seg_name: + name = seg_name + if init_url: + init_name = Path(urlparse(init_url).path).name + if init_name: + name = init_name + + suffix = Path(name).suffix.lower() + if suffix in {".m3u8", ".m4s", ".mpd", ""}: + name = f"{Path(name).stem or 'x_video'}.mp4" + return save_path / name + + def _looks_like_hls(self, url: str, content_type: str, body: str) -> bool: + low_url = url.lower() + if ".m3u8" in low_url: + return True + if "mpegurl" in content_type or "vnd.apple.mpegurl" in content_type: + return True + return "#EXTM3U" in body[:256] + + def _attach_response_collector(self, page: Page) -> None: + page_id = id(page) + bucket: list[dict[str, Any]] = [] + self._response_store[page_id] = bucket + self._response_tasks[page_id] = set() + + def handle_response(response: Any) -> None: + task = asyncio.create_task(self._capture_response(page, response)) + task_bucket = self._response_tasks.setdefault(page_id, set()) + task_bucket.add(task) + + def cleanup(done_task: asyncio.Task[Any]) -> None: + current_bucket = self._response_tasks.get(page_id) + if current_bucket is not None: + current_bucket.discard(done_task) + + task.add_done_callback(cleanup) + + page.on("response", handle_response) + + async def _capture_response(self, page: Page, response: Any) -> None: + url = str(getattr(response, "url", "") or "") + if not self._should_capture_response(url): + return + + try: + headers = await response.all_headers() + except Exception: + headers = {} + content_type = str(headers.get("content-type", "")).lower() + if "json" not in content_type and "javascript" not in content_type and "text/plain" not in content_type: + return + + try: + text = await response.text() + except Exception: + return + if not text or len(text) < 2: + return + + parsed_json: Any = None + try: + parsed_json = json.loads(text) + except Exception: + return + + self._response_store.setdefault(id(page), []).append( + { + "url": url, + "status": getattr(response, "status", None), + "json": parsed_json, + } + ) + + def _should_capture_response(self, url: str) -> bool: + low = url.lower() + return any( + token in low + for token in ( + "/i/api/graphql/", + "/graphql/", + "tweetdetail", + "tweetresultbyrestid", + "conversation", + "adaptive.json", + ) + ) + + def _find_media_entities(self, payload: Any) -> list[dict[str, Any]]: + media_entities: list[dict[str, Any]] = [] + + def walk(node: Any) -> None: + if isinstance(node, dict): + if "media_url_https" in node or "video_info" in node: + media_entities.append(node) + for value in node.values(): + walk(value) + return + if isinstance(node, list): + for item in node: + walk(item) + + walk(payload) + return media_entities + + def _pick_best_video_variant(self, video_info: dict[str, Any]) -> str | None: + variants = video_info.get("variants") + if not isinstance(variants, list): + return None + + best_mp4: tuple[int, str] | None = None + best_hls: str | None = None + for item in variants: + if not isinstance(item, dict): + continue + url = item.get("url") + if not isinstance(url, str) or not url: + continue + low_url = url.lower() + content_type = str(item.get("content_type") or "").lower() + try: + bitrate = int(item.get("bitrate") or 0) + except (TypeError, ValueError): + bitrate = 0 + if "mp4" in content_type or ".mp4" in low_url: + if best_mp4 is None or bitrate > best_mp4[0]: + best_mp4 = (bitrate, url) + elif "mpegurl" in content_type or ".m3u8" in low_url: + best_hls = url + + if best_mp4 is not None: + return best_mp4[1] + return best_hls + + def _extract_image_urls_from_html(self, html: str) -> list[str]: + if not html: + return [] + seen: set[str] = set() + urls: list[str] = [] + patterns = ( + r"https?://pbs\.twimg\.com/media/[^\s\"'<>\\]+", + r"https:\\/\\/pbs\.twimg\.com\\/media\\/[^\s\"'<>]+", + ) + for pattern in patterns: + for match in re.findall(pattern, html): + normalized = self._normalize_media_url(match) + if "pbs.twimg.com/media" not in normalized: + continue + orig = self._original_tw_image_url(normalized) + if orig not in seen: + seen.add(orig) + urls.append(orig) + return urls + + def _ua(self) -> str: + if default_profile: + return default_profile("x").user_agent + return ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" + ) + + @staticmethod + def _original_tw_image_url(url: str) -> str: + if "pbs.twimg.com/media" not in url: + return url + base = url.split("?")[0] + return f"{base}?format=jpg&name=orig" if "format=" not in url else re.sub( + r"name=\w+", + "name=orig", + url, + ) + + @staticmethod + def _normalize_media_url(url: str) -> str: + value = url.strip().replace("\\/", "/") + if value.startswith("//"): + value = f"https:{value}" + return value + + @staticmethod + def _is_video_url(url: str) -> bool: + low = url.lower() + return any( + token in low + for token in ( + ".mp4", + ".m3u8", + "video.twimg.com", + "/amplify_video/", + "/ext_tw_video/", + ) + ) diff --git a/plugins/x/plugin.py b/plugins/x/plugin.py new file mode 100644 index 0000000..6f7dcb3 --- /dev/null +++ b/plugins/x/plugin.py @@ -0,0 +1,504 @@ +""" +X (Twitter) plugin. + +Uses Playwright for rendering and httpx for media downloads. +""" + +from __future__ import annotations + +import asyncio +import json +import queue +import random +import re +import threading +import time +from dataclasses import dataclass +from pathlib import Path +from typing import Any +from urllib.parse import urlsplit + +from .playwright_client import XPlaywrightClient +from .x_login import run_interactive_login as x_login_flow +from .x_urls import ( + extract_search_query, + extract_tweet_id, + extract_username_and_kind, + is_search_url, + is_status_url, + normalize_url, +) + +from pixiv_app.core.paths import runtime_path +from pixiv_app.core.plugin.base import BasePlugin, PluginParseError, Resource + + +@dataclass +class XPluginConfig: + cookie_file: str = "" + profile_dir: str = "" + headless: bool = False + safety_mode: bool = True + wait_timeout: int = 45000 + timeline_tweet_limit: int = 8 + media_scroll_rounds: int = 5 + max_media_items: int = 40 + parse_cooldown_seconds: float = 60.0 + min_action_delay: float = 4.0 + max_action_delay: float = 11.0 + download_delay_min: float = 2.0 + download_delay_max: float = 5.0 + batch_pause_min: float = 12.0 + batch_pause_max: float = 28.0 + batch_pause_after_min: int = 2 + batch_pause_after_max: int = 4 + max_parse_per_hour: int = 24 + max_download_files_per_hour: int = 60 + max_consecutive_failures: int = 3 + failure_cooldown_seconds: float = 1800.0 + sync_parse_timeout_seconds: float = 120.0 + + +class XPlugin(BasePlugin): + name = "X" + domain = "twitter.com" + version = "1.0.0" + + _HANDLE_PATTERNS = ( + re.compile(r"(?:twitter|x)\.com/\w+/status/\d+", re.I), + re.compile(r"(?:twitter|x)\.com/[^/]+/media\b", re.I), + re.compile(r"(?:twitter|x)\.com/[A-Za-z0-9_]{1,15}/?$", re.I), + re.compile(r"(?:twitter|x)\.com/[A-Za-z0-9_]{1,15}/media/?$", re.I), + re.compile(r"(?:twitter|x)\.com/search\?", re.I), + ) + + def __init__(self) -> None: + self.config = XPluginConfig( + cookie_file=str(runtime_path("x_cookies.json")), + profile_dir=str(runtime_path("x_profile")), + headless=False, + ) + self._cache_file = runtime_path("x_download_cache.json") + self._playwright_lock = threading.Lock() + self._last_parse_at = 0.0 + self._parse_timestamps: list[float] = [] + self._download_timestamps: list[float] = [] + self._consecutive_failures = 0 + self._suspended_until = 0.0 + + def can_handle(self, url: str) -> bool: + text = url.strip() + if not text: + return False + candidate = text if "://" in text else f"https://{text.lstrip('/')}" + try: + hostname = (urlsplit(candidate).hostname or "").lower().rstrip(".") + except ValueError: + return False + if hostname not in {"x.com", "www.x.com", "twitter.com", "www.twitter.com", "mobile.twitter.com"}: + return False + return any(pattern.search(text) for pattern in self._HANDLE_PATTERNS) + + def validate(self) -> bool: + try: + import playwright # noqa: F401 + except ImportError: + return False + return True + + def get_headers(self) -> dict[str, str]: + return { + "User-Agent": ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" + ) + } + + def parse(self, url: str) -> list[Resource]: + self._enforce_account_safety("parse", units=1) + try: + with self._playwright_lock: + self._respect_parse_cooldown() + try: + result = asyncio.run(self._parse_async(normalize_url(url))) + self._record_success() + self._record_usage(self._parse_timestamps, 1) + return result + finally: + self._last_parse_at = time.monotonic() + except PluginParseError: + self._record_failure() + raise + except Exception as exc: + self._record_failure() + raise PluginParseError(f"X 解析失败: {exc}") from exc + + def parse_for_sync(self, url: str) -> list[Resource]: + self._enforce_account_safety("parse", units=1) + timeout = max(30.0, float(self.config.sync_parse_timeout_seconds)) + try: + with self._playwright_lock: + result = asyncio.run(asyncio.wait_for(self._parse_async(normalize_url(url)), timeout=timeout)) + self._record_success() + self._record_usage(self._parse_timestamps, 1) + self._last_parse_at = time.monotonic() + return result + except asyncio.TimeoutError as exc: + self._record_failure() + raise PluginParseError(f"X 同步解析超时({int(timeout)} 秒),请稍后重试或检查登录状态。") from exc + except PluginParseError: + self._record_failure() + raise + except Exception as exc: + self._record_failure() + raise PluginParseError(f"X 同步解析失败: {exc}") from exc + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + requested = max(1, len(resource.files)) + self._enforce_account_safety("download", units=requested) + try: + with self._playwright_lock: + result = asyncio.run(self._download_async(resource, save_path)) + self._record_success() + self._record_usage(self._download_timestamps, max(1, len(result) or requested)) + return result + except Exception as exc: + self._record_failure() + raise PluginParseError(f"X 下载失败: {exc}") from exc + + def reset_playwright_session(self) -> None: + return None + + def _respect_parse_cooldown(self) -> None: + elapsed = time.monotonic() - self._last_parse_at + remain = float(self.config.parse_cooldown_seconds) - elapsed + if 0 < remain < self.config.parse_cooldown_seconds: + time.sleep(remain) + + def _enforce_account_safety(self, action: str, *, units: int) -> None: + if not self.config.safety_mode: + return + + now = time.monotonic() + if self._suspended_until > now: + remaining = int(self._suspended_until - now) + minutes = max(1, remaining // 60) + raise PluginParseError(f"X 账号安全模式已暂停操作,请约 {minutes} 分钟后再试。") + + self._trim_usage(now) + if action == "parse": + if len(self._parse_timestamps) + units > self.config.max_parse_per_hour: + raise PluginParseError("X 账号安全模式已触发每小时解析上限,请稍后再试。") + return + + if action == "download": + if len(self._download_timestamps) + units > self.config.max_download_files_per_hour: + raise PluginParseError("X 账号安全模式已触发每小时下载上限,请稍后再试。") + + def _trim_usage(self, now: float | None = None) -> None: + current = now if now is not None else time.monotonic() + window = 3600.0 + self._parse_timestamps = [item for item in self._parse_timestamps if current - item < window] + self._download_timestamps = [item for item in self._download_timestamps if current - item < window] + + def _record_usage(self, bucket: list[float], units: int) -> None: + now = time.monotonic() + self._trim_usage(now) + for _ in range(max(1, units)): + bucket.append(now) + + def _record_success(self) -> None: + self._consecutive_failures = 0 + + def _record_failure(self) -> None: + if not self.config.safety_mode: + return + self._consecutive_failures += 1 + if self._consecutive_failures >= self.config.max_consecutive_failures: + self._suspended_until = time.monotonic() + float(self.config.failure_cooldown_seconds) + self._consecutive_failures = 0 + + async def _parse_async(self, url: str) -> list[Resource]: + if not self.validate(): + raise PluginParseError("未安装 Playwright。请先执行: pip install playwright && playwright install chromium") + + client = XPlaywrightClient(self.config) + await client.start() + try: + if is_status_url(url): + return await self._parse_single_tweet(client, url) + if is_search_url(url): + return await self._parse_search_media(client, url) + + user, kind = extract_username_and_kind(url) + if user and kind == "media": + return await self._parse_user_media(client, url, user) + if user and kind == "profile": + return await self._parse_user_timeline(client, url, user) + + raise PluginParseError(f"无法识别的 X 链接: {url}") + finally: + await client.close() + + async def _parse_single_tweet(self, client: XPlaywrightClient, url: str) -> list[Resource]: + page = await client.new_page_goto(url) + try: + await client.raise_if_guest_wall(page) + await client.wait_for_main_content(page) + await client.wait_for_media(page) + try: + await page.wait_for_load_state("networkidle", timeout=5000) + except Exception: + pass + + tweet_id = extract_tweet_id(url) or "" + author = await client.get_author_info(page) + media_records = await client.extract_media_records_from_api_responses(page) + media_urls = await client.extract_media_urls(page) + if not media_urls: + try: + await page.mouse.wheel(0, 500) + await asyncio.sleep(1.2) + except Exception: + pass + media_urls = await client.extract_media_urls(page) + if not media_records and not media_urls: + media_records = await client.extract_media_records_from_api_responses(page) + if not media_urls: + media_urls = [item["url"] for item in media_records] or await client.extract_media_urls_from_api_responses(page) + + content = await client.get_tweet_text(page) + created = await client.get_tweet_date(page) + finally: + await page.close() + + if not media_records and not media_urls: + raise PluginParseError("未在该推文解析到媒体,可能需要重新登录,或该页面结构已变化。") + + resources: list[Resource] = [] + if media_records: + for index, item in enumerate(media_records): + file_url = item["url"] + thumb = item.get("thumbnail") or file_url + resources.append( + Resource( + id=f"{tweet_id}_{index}", + url=url, + title=(content[:120] if content else f"tweet_{tweet_id}") or f"tweet_{tweet_id}", + author=author.get("name", ""), + author_id=author.get("username", ""), + files=[{"url": file_url, "type": item.get("type", self._media_type(file_url)), "referer": url}], + metadata={"tweet_id": tweet_id, "full_text": content, "author": author}, + thumbnail=thumb, + created_at=created or None, + ) + ) + return resources + + for index, media_url in enumerate(media_urls): + resources.append( + Resource( + id=f"{tweet_id}_{index}", + url=url, + title=(content[:120] if content else f"tweet_{tweet_id}") or f"tweet_{tweet_id}", + author=author.get("name", ""), + author_id=author.get("username", ""), + files=[{"url": media_url, "type": self._media_type(media_url), "referer": url}], + metadata={"tweet_id": tweet_id, "full_text": content, "author": author}, + thumbnail=media_urls[0] if index == 0 else None, + created_at=created or None, + ) + ) + return resources + + async def _parse_search_media(self, client: XPlaywrightClient, url: str) -> list[Resource]: + page = await client.new_page_goto(url) + try: + await client.raise_if_guest_wall(page) + media_urls = await client.scroll_and_collect_media( + page, + max_scrolls=self.config.media_scroll_rounds, + max_items=self.config.max_media_items, + ) + finally: + await page.close() + + query = extract_search_query(url) or "search" + resources: list[Resource] = [] + for index, media_url in enumerate(media_urls): + resources.append( + Resource( + id=f"search_{abs(hash(query))}_{index}", + url=url, + title=f"X search - {query}", + author=query, + author_id="search", + files=[{"url": media_url, "type": self._media_type(media_url), "referer": url}], + metadata={"source": "search_media", "query": query}, + thumbnail=media_url, + created_at=None, + ) + ) + return resources + + async def _parse_user_media(self, client: XPlaywrightClient, url: str, username: str) -> list[Resource]: + page = await client.new_page_goto(url) + try: + await client.raise_if_guest_wall(page) + info = await client.get_user_display_name(page, username) + media_urls = await client.scroll_and_collect_media( + page, + max_scrolls=self.config.media_scroll_rounds, + max_items=self.config.max_media_items, + ) + finally: + await page.close() + + resources: list[Resource] = [] + for index, media_url in enumerate(media_urls): + resources.append( + Resource( + id=f"{username}_media_{index}", + url=url, + title=f"{info.get('name', username)} - media", + author=info.get("name", username), + author_id=username, + files=[{"url": media_url, "type": self._media_type(media_url), "referer": url}], + metadata={"source": "media_tab", "username": username}, + thumbnail=media_url, + created_at=None, + ) + ) + return resources + + async def _parse_user_timeline(self, client: XPlaywrightClient, url: str, username: str) -> list[Resource]: + page = await client.new_page_goto(url) + try: + await client.raise_if_guest_wall(page) + tweet_urls = await client.scroll_and_collect_tweet_urls( + page, + limit=self.config.timeline_tweet_limit, + ) + finally: + await page.close() + + all_results: list[Resource] = [] + for tweet_url in tweet_urls: + try: + all_results.extend(await self._parse_single_tweet(client, tweet_url)) + except Exception: + continue + return all_results + + async def _download_async(self, resource: Resource, save_path: Path) -> list[Path]: + client = XPlaywrightClient(self.config) + results: list[Path] = [] + subdir = resource.metadata.get("save_subdir") + target_base = save_path / subdir if subdir else save_path + cache = self._load_download_cache() + batch_pause_after = random.randint(self.config.batch_pause_after_min, self.config.batch_pause_after_max) + + try: + for index, item in enumerate(resource.files, start=1): + file_url = item.get("url") + if not file_url: + continue + + cached = cache.get(file_url) + if cached and Path(cached).is_file(): + results.append(Path(cached)) + continue + + kind = item.get("type", "image") + referer = item.get("referer") or resource.url + if kind == "video": + saved = await client.download_video(file_url, target_base, referer=referer) + else: + saved = await client.download_image(file_url, target_base, referer=referer) + + if saved is None: + continue + + cache[file_url] = str(saved) + results.append(saved) + await asyncio.sleep( + random.uniform(self.config.download_delay_min, self.config.download_delay_max) + * random.uniform(0.8, 1.5) + ) + + if index % batch_pause_after == 0: + await asyncio.sleep(random.uniform(self.config.batch_pause_min, self.config.batch_pause_max)) + batch_pause_after = random.randint( + self.config.batch_pause_after_min, + self.config.batch_pause_after_max, + ) + + self._save_download_cache(cache) + return results + finally: + await client.close() + + def _load_download_cache(self) -> dict[str, str]: + try: + if self._cache_file.is_file(): + payload = json.loads(self._cache_file.read_text(encoding="utf-8")) + if isinstance(payload, dict): + return {str(key): str(value) for key, value in payload.items()} + except Exception: + pass + return {} + + def _save_download_cache(self, payload: dict[str, str]) -> None: + self._cache_file.parent.mkdir(parents=True, exist_ok=True) + self._cache_file.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8") + + def run_interactive_login( + self, + *, + proceed: threading.Event | None = None, + ready_signal: queue.Queue[bool] | None = None, + ) -> bool: + path = Path(self.config.profile_dir) + try: + with self._playwright_lock: + return asyncio.run(x_login_flow(path, proceed=proceed, ready_signal=ready_signal)) + except Exception: + return False + + def set_headless(self, headless: bool) -> None: + self.config.headless = headless + + @staticmethod + def _media_type(url: str) -> str: + low = url.lower() + if ( + "video" in low + or ".mp4" in low + or ".m3u8" in low + or "video.twimg.com" in low + or "/amplify_video/" in low + or "/ext_tw_video/" in low + ): + return "video" + return "image" + + def get_config_schema(self) -> dict[str, Any]: + return { + "headless": {"type": "bool", "label": "无头模式", "default": False}, + "safety_mode": {"type": "bool", "label": "账号安全模式", "default": True}, + "timeline_tweet_limit": {"type": "int", "label": "时间线采样条数", "default": 8, "min": 1, "max": 30}, + "media_scroll_rounds": {"type": "int", "label": "媒体页滚动轮数", "default": 5, "min": 1, "max": 12}, + "max_media_items": {"type": "int", "label": "单次最多媒体数", "default": 40, "min": 1, "max": 120}, + "parse_cooldown_seconds": {"type": "float", "label": "解析冷却秒数", "default": 60.0, "min": 10, "max": 600}, + "min_action_delay": {"type": "float", "label": "最小操作延迟秒数", "default": 4.0, "min": 1, "max": 60}, + "max_action_delay": {"type": "float", "label": "最大操作延迟秒数", "default": 11.0, "min": 2, "max": 90}, + "download_delay_min": {"type": "float", "label": "下载后最小停顿秒数", "default": 2.0, "min": 0, "max": 60}, + "download_delay_max": {"type": "float", "label": "下载后最大停顿秒数", "default": 5.0, "min": 0, "max": 120}, + "max_parse_per_hour": {"type": "int", "label": "每小时最大解析次数", "default": 24, "min": 1, "max": 120}, + "max_download_files_per_hour": {"type": "int", "label": "每小时最大下载文件数", "default": 60, "min": 1, "max": 300}, + "max_consecutive_failures": {"type": "int", "label": "连续失败暂停阈值", "default": 3, "min": 1, "max": 10}, + "failure_cooldown_seconds": {"type": "float", "label": "连续失败暂停秒数", "default": 1800.0, "min": 60, "max": 86400}, + } + + +plugin_class = XPlugin diff --git a/plugins/x/selectors.py b/plugins/x/selectors.py new file mode 100644 index 0000000..7218676 --- /dev/null +++ b/plugins/x/selectors.py @@ -0,0 +1,8 @@ +"""DOM selectors for X pages used by Sakura desktop sync.""" + +PROFILE_LINK = 'a[data-testid="AppTabBar_Profile_Link"]' +USER_CELL = '[data-testid="UserCell"]' +USER_NAME = '[data-testid="UserName"]' +USER_AVATAR = 'img[src*="profile_images"]' +USER_LINK = 'a[href^="/"][role="link"]' +FOLLOWING_URL_SUFFIX = "/following" diff --git a/plugins/x/setup_plugin.py b/plugins/x/setup_plugin.py new file mode 100644 index 0000000..ea10d8a --- /dev/null +++ b/plugins/x/setup_plugin.py @@ -0,0 +1,18 @@ +"""Install Python deps and Chromium for the X plugin.""" + +from __future__ import annotations + +import subprocess +import sys + + +def main() -> None: + deps = ["playwright", "httpx"] + for dep in deps: + subprocess.check_call([sys.executable, "-m", "pip", "install", dep]) + subprocess.check_call([sys.executable, "-m", "playwright", "install", "chromium"]) + print("Done. Restart the app and reload plugins.") + + +if __name__ == "__main__": + main() diff --git a/plugins/x/x_login.py b/plugins/x/x_login.py new file mode 100644 index 0000000..82eb167 --- /dev/null +++ b/plugins/x/x_login.py @@ -0,0 +1,59 @@ +"""Interactive X login via persistent Chromium profile.""" + +from __future__ import annotations + +import asyncio +import queue +import shutil +import threading +from pathlib import Path + +from playwright.async_api import async_playwright + +from .browser_opts import launch_x_persistent_context +from pixiv_app.services.x_login_flow import goto_x_login_entry + + +async def run_interactive_login( + profile_dir: Path, + *, + proceed: threading.Event | None = None, + ready_signal: queue.Queue[bool] | None = None, +) -> bool: + """ + Open visible Chromium with persistent ``profile_dir``. + + Optionally notify GUI via ``ready_signal`` after the login page loads, then block until + ``proceed`` is set (e.g. user clicked OK after logging in). If ``proceed`` is None, + falls back to terminal ``input()``. + """ + profile_dir.mkdir(parents=True, exist_ok=True) + playwright = await async_playwright().start() + context = None + loop = asyncio.get_running_loop() + try: + context = await launch_x_persistent_context(playwright, profile_dir, headless=False) + page = context.pages[0] if context.pages else await context.new_page() + await goto_x_login_entry(page, timeout=60_000) + + if ready_signal is not None: + ready_signal.put(True) + + if proceed is not None: + await loop.run_in_executor(None, proceed.wait) + else: + await loop.run_in_executor( + None, + lambda: input("\n在浏览器中完成 X 登录后,回到控制台按 Enter...\n"), + ) + return True + finally: + if context is not None: + await context.close() + await playwright.stop() + + +def clear_profile(profile_dir: Path) -> None: + if profile_dir.exists(): + shutil.rmtree(profile_dir, ignore_errors=True) + profile_dir.mkdir(parents=True, exist_ok=True) diff --git a/plugins/x/x_urls.py b/plugins/x/x_urls.py new file mode 100644 index 0000000..18742ef --- /dev/null +++ b/plugins/x/x_urls.py @@ -0,0 +1,57 @@ +"""URL helpers for X / Twitter.""" + +from __future__ import annotations + +import re +from urllib.parse import parse_qs, urlparse + +STATUS_RE = re.compile( + r"(?:https?://)?(?:www\.)?(?:twitter|x)\.com/[^/]+/status/(\d+)", + re.IGNORECASE, +) +PROFILE_OR_MEDIA_RE = re.compile( + r"(?:https?://)?(?:www\.)?(?:twitter|x)\.com/([A-Za-z0-9_]{1,15})(?:/(media))?/?$", + re.IGNORECASE, +) + + +def normalize_url(url: str) -> str: + u = url.strip() + if not u.startswith("http"): + u = "https://" + u + return u + + +def extract_tweet_id(url: str) -> str | None: + m = STATUS_RE.search(url) + return m.group(1) if m else None + + +def extract_username_and_kind(url: str) -> tuple[str | None, str | None]: + u = normalize_url(url) + m = PROFILE_OR_MEDIA_RE.match(u) + if not m: + return None, None + user = m.group(1) + media_flag = m.group(2) + if user.lower() in {"home", "explore", "settings", "messages", "notifications", "i", "intent", "search"}: + return None, None + if media_flag: + return user, "media" + return user, "profile" + + +def is_status_url(url: str) -> bool: + return extract_tweet_id(url) is not None + + +def is_search_url(url: str) -> bool: + parsed = urlparse(normalize_url(url)) + host = parsed.netloc.lower() + return host in {"x.com", "www.x.com", "twitter.com", "www.twitter.com"} and parsed.path.rstrip("/") == "/search" + + +def extract_search_query(url: str) -> str: + parsed = urlparse(normalize_url(url)) + values = parse_qs(parsed.query).get("q") or [] + return values[0] if values else "" diff --git a/plugins/xiaohongshu/__init__.py b/plugins/xiaohongshu/__init__.py new file mode 100644 index 0000000..660438e --- /dev/null +++ b/plugins/xiaohongshu/__init__.py @@ -0,0 +1,2 @@ +# plugins/xiaohongshu/__init__.py +"""小红书 (Xiaohongshu) 爬虫插件 - 换IP + 指纹模拟版本""" \ No newline at end of file diff --git a/plugins/xiaohongshu/plugin.py b/plugins/xiaohongshu/plugin.py new file mode 100644 index 0000000..fab6ca3 --- /dev/null +++ b/plugins/xiaohongshu/plugin.py @@ -0,0 +1,1021 @@ +# plugins/xiaohongshu/plugin.py +""" +小红书爬虫插件 - 核心功能 +- Playwright 浏览器自动化 +- 动态代理池轮换 +- 浏览器指纹伪装 +- 行为节奏模拟 +""" + +from __future__ import annotations + +import asyncio +import json +import random +import re +import threading +import time +from dataclasses import dataclass, field +from datetime import datetime +from pathlib import Path +from typing import Any, Callable, Optional +from urllib.parse import parse_qs, quote, unquote, urlparse + +# 动态加载 playwright +try: + from playwright.async_api import BrowserContext, Page, Playwright, async_playwright + + PLAYWRIGHT_AVAILABLE = True +except ImportError: + PLAYWRIGHT_AVAILABLE = False + +from pixiv_app.core.plugin.base import BasePlugin, PluginParseError, Resource +from pixiv_app.core.paths import runtime_path +from pixiv_app.core.playwright_runtime import ( + configure_playwright_runtime_env, + explain_missing_browser, + preferred_chromium_executable, +) + +try: + from pixiv_app.core.fingerprint import BrowserProfile, SessionFingerprintManager +except Exception: # pragma: no cover + BrowserProfile = None + SessionFingerprintManager = None + + +def _extract_note_id_from_text(text: str) -> str: + patterns = [ + r"xiaohongshu\.com/explore/([a-zA-Z0-9]+)", + r"/explore/([a-zA-Z0-9]+)", + r"note_id[=:]([a-zA-Z0-9]+)", + ] + for pattern in patterns: + match = re.search(pattern, text or "", re.IGNORECASE) + if match: + return match.group(1) + return "" + + +def _extract_redirect_path(url: str) -> str: + try: + query = parse_qs(urlparse(url).query) + value = (query.get("redirectPath") or [""])[0] + except Exception: + value = "" + value = unquote(value or "").strip() + if not value: + return "" + if value.startswith("//"): + return "https:" + value + if value.startswith("/"): + return "https://www.xiaohongshu.com" + value + if "xiaohongshu.com" in value.lower() or "xhslink.com" in value.lower(): + return value if "://" in value else "https://" + value.lstrip("/") + return "" + + +def _normalize_xhs_url(url: str) -> str: + value = (url or "").strip() + if not value: + return "" + if value.startswith("//"): + return "https:" + value + if value.startswith("/"): + return "https://www.xiaohongshu.com" + value + lower = value.lower() + if ("xiaohongshu.com" in lower or "xhslink.com" in lower) and "://" not in value: + return "https://" + value.lstrip("/") + return value + + +# ==================== 配置定义 ==================== + +@dataclass +class ProxyConfig: + """代理配置""" + server: str # http://ip:port 或 socks5://ip:port + username: str = "" + password: str = "" + failures: int = 0 + successes: int = 0 + latency_ms: float = 0.0 + healthy: bool = True + + @property + def url(self) -> str: + return self.server + + @property + def score(self) -> float: + """计算代理评分(用于加权选择)""" + if not self.healthy: + return 0.0 + total = self.successes + self.failures + if total == 0: + return 1.0 + success_rate = self.successes / total + latency_penalty = max(0.1, min(1.0, 1000.0 / max(self.latency_ms, 1.0))) + return success_rate * latency_penalty + + +@dataclass +class FingerprintConfig: + """浏览器指纹配置""" + user_agent: str + viewport: dict[str, int] + locale: str = "zh-CN" + timezone_id: str = "Asia/Shanghai" + color_scheme: str = "light" + reduced_motion: str = "no-preference" + device_scale_factor: float = 1.0 + is_mobile: bool = False + has_touch: bool = False + + @classmethod + def from_browser_profile(cls, profile: Any) -> "FingerprintConfig": + return cls( + user_agent=profile.user_agent, + viewport=dict(profile.viewport), + locale=profile.locale, + timezone_id=profile.timezone_id, + color_scheme=profile.color_scheme, + reduced_motion=profile.reduced_motion, + device_scale_factor=profile.device_scale_factor, + is_mobile=profile.is_mobile, + has_touch=profile.has_touch, + ) + + @classmethod + def desktop_chrome(cls) -> "FingerprintConfig": + return cls( + user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", + viewport={"width": 1920, "height": 1080}, + device_scale_factor=1.0, + ) + + @classmethod + def desktop_edge(cls) -> "FingerprintConfig": + return cls( + user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0", + viewport={"width": 1536, "height": 864}, + device_scale_factor=1.25, + ) + + @classmethod + def mac_chrome(cls) -> "FingerprintConfig": + return cls( + user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", + viewport={"width": 1512, "height": 982}, + device_scale_factor=2.0, + ) + + @classmethod + def mobile_ios(cls) -> "FingerprintConfig": + return cls( + user_agent="Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1", + viewport={"width": 390, "height": 844}, + device_scale_factor=3.0, + is_mobile=True, + has_touch=True, + ) + + +@dataclass +class XiaohongshuConfig: + """小红书插件配置""" + # 代理配置 + proxy_enabled: bool = False + proxy_list: list[ProxyConfig] = field(default_factory=list) + proxy_rotation: str = "round_robin" # round_robin, random, session_sticky + + # 指纹配置 + fingerprint_list: list[FingerprintConfig] = field(default_factory=lambda: [ + FingerprintConfig.desktop_chrome(), + FingerprintConfig.desktop_edge(), + FingerprintConfig.mac_chrome(), + ]) + + # 行为控制 + request_delay_min: float = 3.0 # 最小延迟(秒) + request_delay_max: float = 8.0 # 最大延迟(秒) + scroll_delay_min: float = 0.5 # 滚动延迟(秒) + scroll_delay_max: float = 1.5 + batch_pause_min: float = 5.0 # 批次暂停(秒) + batch_pause_max: float = 12.0 + max_scrolls: int = 15 # 最大滚动次数 + + # 采集限制 + max_notes_per_session: int = 50 # 单会话最大笔记数 + max_concurrent: int = 2 # 最大并发数 + + # 会话持久化 + profile_dir: str = "runtime/xiaohongshu_profile" + cookie_file: str = "runtime/xiaohongshu_cookies.json" + + +# ==================== 代理池管理器 ==================== + +class ProxyPool: + """代理池管理器 - 支持轮换和健康检查""" + + def __init__(self, config: XiaohongshuConfig): + self.config = config + self._proxies: list[ProxyConfig] = list(config.proxy_list) + self._current_index = 0 + self._lock = threading.Lock() + + def add_proxy(self, proxy: ProxyConfig) -> None: + """添加代理""" + with self._lock: + # 去重 + existing = {p.server for p in self._proxies} + if proxy.server not in existing: + self._proxies.append(proxy) + + def get_proxy(self) -> Optional[ProxyConfig]: + """获取一个代理(按策略)""" + if not self.config.proxy_enabled or not self._proxies: + return None + + with self._lock: + healthy = [p for p in self._proxies if p.healthy] + if not healthy: + return None + + if self.config.proxy_rotation == "random": + return random.choice(healthy) + + # round_robin + proxy = healthy[self._current_index % len(healthy)] + self._current_index += 1 + return proxy + + def mark_success(self, proxy: Optional[ProxyConfig], latency_ms: float = 0) -> None: + """标记代理成功""" + if proxy is None: + return + with self._lock: + proxy.successes += 1 + proxy.failures = 0 + proxy.healthy = True + if latency_ms > 0: + proxy.latency_ms = latency_ms + + def mark_failed(self, proxy: Optional[ProxyConfig]) -> None: + """标记代理失败""" + if proxy is None: + return + with self._lock: + proxy.failures += 1 + if proxy.failures >= 3: + proxy.healthy = False + + def healthy_count(self) -> int: + """可用代理数量""" + with self._lock: + return len([p for p in self._proxies if p.healthy]) + + def total_count(self) -> int: + return len(self._proxies) + + +# ==================== 指纹管理器 ==================== + +class FingerprintManager: + """浏览器指纹管理器""" + + def __init__(self, config: XiaohongshuConfig): + self.config = config + self._fingerprints = list(config.fingerprint_list) + self._current_index = 0 + self._shared = SessionFingerprintManager(strategy="sticky") if SessionFingerprintManager else None + + def get_fingerprint(self, session_id: str = "default") -> FingerprintConfig: + """获取一个指纹""" + if self._shared is not None: + return FingerprintConfig.from_browser_profile(self._shared.get_fingerprint(f"xiaohongshu:{session_id}")) + if not self._fingerprints: + return FingerprintConfig.desktop_chrome() + index = abs(hash(session_id)) % len(self._fingerprints) + return self._fingerprints[index] + + def apply_to_context(self, context: BrowserContext, fingerprint: FingerprintConfig) -> None: + """将指纹应用到浏览器上下文""" + # 注意:viewport 在创建 context 时设置,这里只做额外配置 + pass + + +# ==================== 浏览器会话管理器 ==================== + +class XiaohongshuSession: + """小红书浏览器会话 - 管理单个 IP+指纹 的会话""" + + def __init__( + self, + session_id: str, + proxy_pool: ProxyPool, + fingerprint_manager: FingerprintManager, + config: XiaohongshuConfig, + ): + self.session_id = session_id + self.proxy_pool = proxy_pool + self.fingerprint_manager = fingerprint_manager + self.config = config + + self._pw: Optional[Playwright] = None + self._context: Optional[BrowserContext] = None + self._page: Optional[Page] = None + self._current_proxy: Optional[ProxyConfig] = None + self._fingerprint: Optional[FingerprintConfig] = None + + self.request_count = 0 + self.created_at = datetime.now() + + async def start(self) -> None: + """启动会话(创建独立的浏览器上下文)""" + configure_playwright_runtime_env() + self._pw = await async_playwright().start() + + # 获取代理和指纹 + self._current_proxy = self.proxy_pool.get_proxy() + self._fingerprint = self.fingerprint_manager.get_fingerprint(self.session_id) + + # 构建 context 参数 + context_options = { + "viewport": self._fingerprint.viewport, + "user_agent": self._fingerprint.user_agent, + "locale": self._fingerprint.locale, + "timezone_id": self._fingerprint.timezone_id, + "color_scheme": self._fingerprint.color_scheme, + "reduced_motion": self._fingerprint.reduced_motion, + "device_scale_factor": self._fingerprint.device_scale_factor, + "is_mobile": self._fingerprint.is_mobile, + "has_touch": self._fingerprint.has_touch, + "ignore_default_args": ["--enable-automation"], + "args": [ + "--disable-blink-features=AutomationControlled", + ], + } + + # 添加代理配置 + if self._current_proxy: + context_options["proxy"] = { + "server": self._current_proxy.server, + } + if self._current_proxy.username: + context_options["proxy"]["username"] = self._current_proxy.username + context_options["proxy"]["password"] = self._current_proxy.password + + # 创建上下文 + self._context = await self._pw.chromium.launch_persistent_context( + user_data_dir=str(Path(self.config.profile_dir) / self.session_id), + headless=False, # 小红书能检测无头模式,必须 False + **context_options + ) + + # 加载保存的 Cookie + await self._load_cookies() + + self._page = self._context.pages[0] if self._context.pages else await self._context.new_page() + + async def _load_cookies(self) -> None: + """加载保存的 Cookie""" + cookie_path = Path(self.config.cookie_file) + if cookie_path.exists(): + try: + cookies = json.loads(cookie_path.read_text(encoding="utf-8")) + if isinstance(cookies, list): + await self._context.add_cookies(cookies) + except Exception: + pass + + async def _save_cookies(self) -> None: + """保存 Cookie 到文件""" + if self._context: + cookies = await self._context.cookies() + Path(self.config.cookie_file).write_text( + json.dumps(cookies, indent=2, ensure_ascii=False), + encoding="utf-8" + ) + + async def get_page(self) -> Page: + """获取页面对象""" + if self._page is None: + await self.start() + return self._page + + async def random_delay(self) -> None: + """随机延迟(模拟真人节奏)""" + delay = random.uniform(self.config.request_delay_min, self.config.request_delay_max) + await asyncio.sleep(delay) + self.request_count += 1 + + async def random_scroll(self) -> None: + """随机滚动页面""" + scroll_y = random.randint(220, 620) + try: + await self._page.mouse.move(random.randint(80, 900), random.randint(120, 700), steps=random.randint(8, 18)) + await self._page.mouse.wheel(0, scroll_y) + except Exception: + await self._page.evaluate(f"window.scrollBy(0, {scroll_y})") + await asyncio.sleep(random.uniform(self.config.scroll_delay_min, self.config.scroll_delay_max)) + + async def simulate_browsing(self, duration: float = 2.0) -> None: + """模拟浏览行为(停留、思考)""" + await asyncio.sleep(duration * random.uniform(0.7, 1.3)) + + async def close(self) -> None: + """关闭会话""" + if self._context: + await self._save_cookies() + await self._context.close() + if self._pw: + await self._pw.stop() + self._page = None + self._context = None + self._pw = None + + @property + def proxy_info(self) -> str: + return self._current_proxy.server if self._current_proxy else "直连" + + +# ==================== 小红书 API 封装 ==================== + +class XiaohongshuAPI: + """小红书 API 封装(基于 Playwright)""" + + BASE_URL = "https://www.xiaohongshu.com" + + def __init__(self, session: XiaohongshuSession): + self.session = session + + async def search(self, keyword: str, limit: int = 20) -> list[dict]: + """搜索笔记""" + page = await self.session.get_page() + + # 访问搜索页面 + search_url = f"{self.BASE_URL}/search_result?keyword={quote(keyword)}" + await page.goto(search_url, wait_until="domcontentloaded", timeout=45000) + await self.session.random_delay() + + # 滚动加载 + notes = [] + last_count = 0 + scrolls_without_new = 0 + + for scroll_idx in range(self.session.config.max_scrolls): + # 等待笔记容器加载 + try: + await page.wait_for_selector(".note-item, .feeds-container .note", timeout=5000) + except: + pass + + # 提取当前页面的笔记 + current_notes = await page.evaluate(""" + () => { + const items = document.querySelectorAll('.note-item, .feeds-container .note, section, a[href*="/explore/"]'); + return Array.from(items).map(el => { + const anchor = el.matches?.('a') ? el : ( + el.querySelector('a[href*="/explore/"], a[href*="xhslink.com"], a') + ); + const href = anchor?.href || anchor?.getAttribute('href') || ''; + const img = el.querySelector('img'); + const id = el.getAttribute('data-note-id') || + href.match(/\\/explore\\/([a-zA-Z0-9]+)/)?.[1] || + href.match(/note_id[=:]([a-zA-Z0-9]+)/)?.[1] || + ''; + return { + id, + title: el.querySelector('.title, .note-title, [class*="title"]')?.innerText?.trim() || '', + author: el.querySelector('.author .name, .nickname, [class*="nickname"]')?.innerText?.trim() || '', + likes: el.querySelector('.like-count, .likes, [class*="like"]')?.innerText?.trim() || '0', + coverUrl: img?.currentSrc || img?.src || img?.getAttribute('data-src') || '', + url: href + }; + }); + } + """) + + # 去重合并 + existing_ids = {n.get("id") for n in notes} + for note in current_notes: + note_url = _normalize_xhs_url(str(note.get("url") or "")) + note_id = str(note.get("id") or _extract_note_id_from_text(note_url)) + if note_id and note_id not in existing_ids: + note["id"] = note_id + note["url"] = note_url + existing_ids.add(note_id) + notes.append(note) + if len(notes) >= limit: + break + + if len(notes) >= limit: + break + + # 滚动 + if len(current_notes) == last_count: + scrolls_without_new += 1 + if scrolls_without_new >= 3: + break + else: + scrolls_without_new = 0 + last_count = len(current_notes) + + await self.session.random_scroll() + await self.session.simulate_browsing(1.0) + + return notes[:limit] + + async def get_note_detail(self, note_id: str = "", note_url: str | None = None) -> dict: + """获取笔记详情""" + page = await self.session.get_page() + + url = note_url or f"{self.BASE_URL}/explore/{note_id}" + url = _normalize_xhs_url(url) + await page.goto(url, wait_until="domcontentloaded", timeout=45000) + await self.session.random_delay() + + current_url = page.url + if "/404" in urlparse(current_url).path: + redirect_url = _extract_redirect_path(current_url) + if redirect_url and redirect_url != current_url and redirect_url != url: + await page.goto(redirect_url, wait_until="domcontentloaded", timeout=45000) + await self.session.random_delay() + current_url = page.url + + final_note_id = note_id or _extract_note_id_from_text(current_url) + + try: + await page.wait_for_selector("img", timeout=10000) + except Exception: + pass + + # 提取详情 + detail = await page.evaluate(""" + () => { + // 标题 + const title = document.querySelector('.title, .note-title')?.innerText?.trim() || ''; + // 作者 + const author = document.querySelector('.author .name, .nickname')?.innerText?.trim() || ''; + // 内容描述 + const desc = document.querySelector('.desc, .note-content')?.innerText?.trim() || ''; + // 点赞数 + const likes = document.querySelector('.like-count, .likes')?.innerText?.trim() || '0'; + // 收藏数 + const collects = document.querySelector('.collect-count')?.innerText?.trim() || '0'; + // 评论数 + const comments = document.querySelector('.comment-count')?.innerText?.trim() || '0'; + // 图片列表 + const seen = new Set(); + const images = Array.from(document.querySelectorAll('img')) + .map(img => img.currentSrc || img.src || img.getAttribute('data-src') || '') + .filter(src => /^https?:\\/\\//.test(src)) + .filter(src => /xhscdn|sns-webpic|xiaohongshu/.test(src)) + .filter(src => { + if (seen.has(src)) return false; + seen.add(src); + return true; + }); + + const seenVideos = new Set(); + const videos = Array.from(document.querySelectorAll('video, video source, source')) + .map(el => el.currentSrc || el.src || el.getAttribute('src') || '') + .filter(src => /^https?:\\/\\//.test(src)) + .filter(src => /xhscdn|sns-video|xiaohongshu|xhs/.test(src)) + .filter(src => { + if (seenVideos.has(src)) return false; + seenVideos.add(src); + return true; + }); + + return { title, author, desc, likes, collects, comments, images, videos }; + } + """) + + if "/404" in urlparse(current_url).path and not detail.get("images") and not detail.get("videos"): + raise PluginParseError( + "小红书页面跳转到 404。请粘贴浏览器地址栏里的完整笔记链接," + "尤其要保留 ?xsec_token=...&xsec_source=... 参数。" + ) + + if not detail.get("images") and not detail.get("videos"): + raise PluginParseError( + "页面已打开,但没有解析到图片。请确认链接是图文笔记," + "并优先粘贴带 xsec_token 的完整浏览器地址栏链接。" + ) + + detail["id"] = final_note_id + detail["url"] = current_url + + return detail + + async def get_note_images(self, note_id: str, note_url: str | None = None) -> list[str]: + """获取笔记中的图片 URL""" + detail = await self.get_note_detail(note_id, note_url=note_url) + return detail.get("images", []) + + async def get_note_media(self, note_id: str, note_url: str | None = None) -> dict: + detail = await self.get_note_detail(note_id, note_url=note_url) + return {"images": detail.get("images", []), "videos": detail.get("videos", [])} + + async def download_image(self, url: str, save_path: Path) -> Optional[Path]: + """下载图片""" + return await self.download_media(url, save_path, media_type="image") + + async def download_media(self, url: str, save_path: Path, *, media_type: str = "image") -> Optional[Path]: + import httpx + + page = await self.session.get_page() + + # 构造文件名 + filename = url.split("/")[-1].split("?")[0] + if media_type == "video" and not filename.endswith((".mp4", ".mov", ".webm", ".m4v")): + filename = f"{filename}.mp4" + elif media_type != "video" and not filename.endswith((".jpg", ".jpeg", ".png", ".webp")): + filename = f"{filename}.jpg" + + target = save_path / filename + + # 检查是否已存在 + if target.exists() and target.stat().st_size > 1024: + return target + + # 使用浏览器上下文下载(保持会话) + try: + async with httpx.AsyncClient(timeout=60.0, follow_redirects=True) as client: + # 获取当前页面的 cookies + cookies = await page.context.cookies() + cookie_dict = {c["name"]: c["value"] for c in cookies} + + response = await client.get( + url, + headers={ + "User-Agent": self.session._fingerprint.user_agent, + "Referer": page.url if page and page.url != "about:blank" else self.BASE_URL, + }, + cookies=cookie_dict, + ) + + if response.status_code == 200: + target.write_bytes(response.content) + return target + except Exception: + pass + + return None + + +# ==================== 插件主类 ==================== + +class XiaohongshuPlugin(BasePlugin): + """小红书爬虫插件""" + + name = "小红书" + domain = "xiaohongshu.com" + version = "1.0.0" + + def __init__(self): + self.config = XiaohongshuConfig() + self.config.profile_dir = str(runtime_path("xiaohongshu_profile")) + self.config.cookie_file = str(runtime_path("xiaohongshu_cookies.json")) + self.progress_callback: Optional[Callable[[str, float], None]] = None + self._proxy_pool: Optional[ProxyPool] = None + self._fingerprint_manager: Optional[FingerprintManager] = None + self._sessions: dict[str, XiaohongshuSession] = {} + self._session_lock = threading.Lock() + + def _progress(self, message: str, value: float) -> None: + if self.progress_callback is None: + return + try: + self.progress_callback(message, value) + except Exception: + pass + + def can_handle(self, url: str) -> bool: + """判断是否能处理该 URL""" + text = url.strip().lower() + return "xiaohongshu.com" in text or "xhslink.com" in text or text.isdigit() + + def validate(self) -> bool: + """检查依赖是否安装""" + return PLAYWRIGHT_AVAILABLE + + def get_headers(self) -> dict[str, str]: + """获取请求头""" + return { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", + "Accept": "application/json, text/plain, */*", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", + } + + def set_proxies(self, proxy_list: list[dict]) -> None: + """ + 设置代理列表 + proxy_list: [{"server": "http://ip:port", "username": "", "password": ""}, ...] + """ + self.config.proxy_enabled = True + self.config.proxy_list = [ + ProxyConfig( + server=p.get("server"), + username=p.get("username", ""), + password=p.get("password", ""), + ) + for p in proxy_list + ] + self._proxy_pool = ProxyPool(self.config) + + def set_fingerprints(self, fingerprints: list[dict]) -> None: + """设置指纹列表""" + self.config.fingerprint_list = [ + FingerprintConfig( + user_agent=f.get("user_agent"), + viewport=f.get("viewport", {"width": 1920, "height": 1080}), + locale=f.get("locale", "zh-CN"), + timezone_id=f.get("timezone_id", "Asia/Shanghai"), + ) + for f in fingerprints + ] + + def _get_session(self, session_id: str = "default") -> XiaohongshuSession: + """获取或创建会话(每个会话独立 IP+指纹)""" + with self._session_lock: + if session_id not in self._sessions: + if self._proxy_pool is None: + self._proxy_pool = ProxyPool(self.config) + if self._fingerprint_manager is None: + self._fingerprint_manager = FingerprintManager(self.config) + + session = XiaohongshuSession( + session_id=session_id, + proxy_pool=self._proxy_pool, + fingerprint_manager=self._fingerprint_manager, + config=self.config, + ) + self._sessions[session_id] = session + + return self._sessions[session_id] + + def _close_session(self, session_id: str) -> None: + """关闭会话""" + with self._session_lock: + if session_id in self._sessions: + # 异步关闭需要在 async 环境中处理,这里触发但不等待 + asyncio.create_task(self._sessions[session_id].close()) + del self._sessions[session_id] + + def parse(self, url: str) -> list[Resource]: + """解析 URL 为资源列表""" + if not self.validate(): + raise PluginParseError("请先安装 Playwright: pip install playwright && playwright install chromium") + + try: + return asyncio.run(self._parse_async(url)) + except Exception as exc: + raise PluginParseError(f"小红书解析失败: {exc}") from exc + + async def _parse_async(self, url: str) -> list[Resource]: + """异步解析 URL""" + self._progress("小红书:准备解析目标", 0.05) + # 提取关键词或笔记 ID + note_id = self._extract_note_id(url) + note_url = self._extract_note_url(url, note_id=note_id) + keyword = self._extract_keyword(url) + + session = self._get_session(f"parse_{int(time.time())}") + + try: + self._progress("小红书:启动浏览器会话", 0.10) + await session.start() + api = XiaohongshuAPI(session) + + resources = [] + + if note_id or note_url: + # 单篇笔记 + self._progress("小红书:打开笔记页面", 0.22) + detail = await api.get_note_detail(note_id or "", note_url=note_url) + self._progress("小红书:提取笔记图片", 0.55) + note_id = detail.get("id") or note_id or _extract_note_id_from_text(detail.get("url", "")) + if not note_id: + note_id = f"note_{int(time.time())}" + images = detail.get("images", []) + videos = detail.get("videos", []) + + resource = Resource( + id=note_id, + url=detail.get("url") or note_url or f"https://www.xiaohongshu.com/explore/{note_id}", + title=detail.get("title", "")[:200], + author=detail.get("author", ""), + author_id="", + files=[{"url": video, "type": "video"} for video in videos] + + [{"url": img, "type": "image"} for img in images], + metadata=detail, + thumbnail=images[0] if images else None, + created_at="", + ) + resources.append(resource) + + elif keyword: + # 关键词搜索 + self._progress("小红书:打开搜索结果页", 0.18) + notes = await api.search(keyword, limit=min(self.config.max_notes_per_session, 30)) + self._progress(f"小红书:整理搜索结果({len(notes)} 条)", 0.62) + + for note in notes: + note_url = _normalize_xhs_url(note.get("url", "")) + note_id = note.get("id") or _extract_note_id_from_text(note_url) + if not note_id: + continue + # 只返回元数据,实际图片在 download 时下载 + resource = Resource( + id=note_id, + url=note_url, + title=note.get("title", "")[:200], + author=note.get("author", ""), + author_id="", + files=[], # 延迟下载 + metadata={ + "kind": "search_result", + "keyword": keyword, + "note_id": note_id, + "note_url": note_url, + "likes": note.get("likes"), + "cover_url": note.get("coverUrl"), + }, + thumbnail=note.get("coverUrl"), + created_at="", + ) + resources.append(resource) + + self._progress(f"小红书:解析完成({len(resources)} 个资源)", 0.72) + return resources + + finally: + await session.close() + + def download(self, resource: Resource, save_path: Path) -> list[Path]: + """下载资源""" + try: + return asyncio.run(self._download_async(resource, save_path)) + except Exception as exc: + raise PluginParseError(f"小红书下载失败: {exc}") from exc + + async def _download_async(self, resource: Resource, save_path: Path) -> list[Path]: + """异步下载""" + self._progress(f"小红书:准备下载 {resource.id}", 0.0) + session = self._get_session(f"download_{resource.id}_{int(time.time())}") + + try: + self._progress(f"小红书:启动下载会话 {resource.id}", 0.10) + await session.start() + api = XiaohongshuAPI(session) + + # 创建保存目录 + subdir = resource.metadata.get("keyword", resource.id) + target_dir = save_path / "xiaohongshu" / subdir + target_dir.mkdir(parents=True, exist_ok=True) + + downloaded = [] + + # 如果有直接的文件列表 + if resource.files: + if resource.url: + try: + self._progress(f"小红书:打开笔记页 {resource.id}", 0.20) + page = await session.get_page() + await page.goto(_normalize_xhs_url(resource.url), wait_until="domcontentloaded", timeout=45000) + await session.random_delay() + except Exception: + pass + total_files = max(len(resource.files), 1) + for item in resource.files: + file_url = item.get("url") + if file_url: + self._progress(f"小红书:下载图片 {len(downloaded) + 1}/{total_files}", 0.35 + (len(downloaded) / total_files) * 0.55) + media_type = "video" if item.get("type") == "video" else "image" + path = await api.download_media(file_url, target_dir, media_type=media_type) + if path: + downloaded.append(path) + await session.random_delay() + + # 如果是搜索结果,需要先获取详情再下载 + elif resource.metadata.get("kind") == "search_result": + note_id = resource.metadata.get("note_id") + note_url = _normalize_xhs_url(str(resource.metadata.get("note_url") or resource.url or "")) + if note_id or note_url: + self._progress(f"小红书:进入笔记详情 {resource.id}", 0.20) + media = await api.get_note_media(note_id, note_url=note_url) + media_items = [{"url": video, "type": "video"} for video in media.get("videos", [])] + [ + {"url": image, "type": "image"} for image in media.get("images", []) + ] + total_files = max(len(media_items), 1) + for media_item in media_items: + img_url = media_item["url"] + self._progress(f"小红书:下载图片 {len(downloaded) + 1}/{total_files}", 0.35 + (len(downloaded) / total_files) * 0.55) + path = await api.download_media(img_url, target_dir, media_type=media_item["type"]) + if path: + downloaded.append(path) + await session.random_delay() + + self._progress(f"小红书:资源下载完成 {resource.id}", 0.96) + return downloaded + + finally: + await session.close() + + def _extract_note_id(self, url: str) -> Optional[str]: + """提取笔记 ID""" + # 标准链接: xiaohongshu.com/explore/xxxxx + patterns = [ + r"xiaohongshu\.com/explore/([a-zA-Z0-9]+)", + r"note_id[=:]([a-zA-Z0-9]+)", + ] + for pattern in patterns: + match = re.search(pattern, url, re.IGNORECASE) + if match: + return match.group(1) + + # 纯数字 ID + if url.strip().isdigit(): + return url.strip() + + return None + + def _extract_note_url(self, url: str, *, note_id: str | None = None) -> Optional[str]: + """提取或还原单篇笔记访问 URL,尽量保留 xsec_token 等安全参数。""" + text = url.strip() + if not text: + return None + lower = text.lower() + if "xiaohongshu.com/404" in lower: + redirect_url = _extract_redirect_path(text) + if redirect_url: + return redirect_url + if "xiaohongshu.com" in lower or "xhslink.com" in lower: + if text.startswith("//"): + return "https:" + text + if "://" not in text: + return "https://" + text.lstrip("/") + return text + if note_id: + return f"https://www.xiaohongshu.com/explore/{note_id}" + return None + + def _extract_keyword(self, url: str) -> Optional[str]: + """提取搜索关键词""" + # 搜索链接: xiaohongshu.com/search_result?keyword=xxx + match = re.search(r"keyword=([^&]+)", url) + if match: + return unquote(match.group(1)) + if url.strip() and "xiaohongshu.com" not in url.lower() and "xhslink.com" not in url.lower(): + return url.strip() + return None + + def get_proxy_stats(self) -> dict: + """获取代理池统计""" + if self._proxy_pool: + return { + "total": self._proxy_pool.total_count(), + "healthy": self._proxy_pool.healthy_count(), + } + return {"total": 0, "healthy": 0} + + def rotate_session(self, session_id: str = "default") -> None: + """强制轮换会话(换 IP + 换指纹)""" + self._close_session(session_id) + + def run_interactive_login(self, proceed: Optional[threading.Event] = None) -> bool: + """交互式登录""" + if not self.validate(): + return False + + async def _login(): + session = XiaohongshuSession( + session_id="login", + proxy_pool=self._proxy_pool or ProxyPool(self.config), + fingerprint_manager=self._fingerprint_manager or FingerprintManager(self.config), + config=self.config, + ) + try: + await session.start() + page = await session.get_page() + await page.goto("https://www.xiaohongshu.com", wait_until="networkidle") + + if proceed: + proceed.wait() + else: + input("\n在浏览器中完成小红书登录后,按 Enter 继续...\n") + + await session._save_cookies() + return True + finally: + await session.close() + + return asyncio.run(_login()) + + +# 导出插件类 +plugin_class = XiaohongshuPlugin diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2670e79 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +customtkinter>=5.2.0 +requests>=2.31.0 +Pillow>=10.0.0 +Jinja2>=3.1.0 +playwright>=1.41.0 +httpx>=0.27.0 +cryptography>=45.0.0 +firebase-admin>=6.5.0 +paddleocr>=2.7.0 diff --git a/sakura_admin_web/index.html b/sakura_admin_web/index.html new file mode 100644 index 0000000..fc77b52 --- /dev/null +++ b/sakura_admin_web/index.html @@ -0,0 +1,13 @@ + + + + + + + Sakura Cloud + + +
+ + + diff --git a/sakura_admin_web/nginx.conf b/sakura_admin_web/nginx.conf new file mode 100644 index 0000000..dab0a0d --- /dev/null +++ b/sakura_admin_web/nginx.conf @@ -0,0 +1,25 @@ +server { + listen 80; + server_name _; + + root /var/www/sakura_admin_web/dist; + index index.html; + + location /api/ { + proxy_pass http://127.0.0.1:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location /assets/ { + expires 1y; + add_header Cache-Control "public, immutable"; + try_files $uri =404; + } + + location / { + try_files $uri $uri/ /index.html; + } +} diff --git a/sakura_admin_web/package-lock.json b/sakura_admin_web/package-lock.json new file mode 100644 index 0000000..bc7d626 --- /dev/null +++ b/sakura_admin_web/package-lock.json @@ -0,0 +1,1833 @@ +{ + "name": "sakura-admin-web", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "sakura-admin-web", + "version": "0.1.0", + "dependencies": { + "@vitejs/plugin-vue": "^5.2.4", + "axios": "^1.7.9", + "vite": "^6.0.7", + "vue": "^3.5.13", + "vue-router": "^4.5.0" + }, + "devDependencies": { + "typescript": "^5.7.2", + "vue-tsc": "^2.2.0" + } + }, + "node_modules/@babel/helper-string-parser": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.29.7.tgz", + "integrity": "sha512-Pb5ijPrZ89GDH8223L4UP8i6QApWxs04RbPQJTeWDV0/keR2E36MeKnyr6LYmUUvqRRI+Iv87SuF1W6ErINzYw==", + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/helper-validator-identifier": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.29.7.tgz", + "integrity": "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==", + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/parser": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.29.7.tgz", + "integrity": "sha512-hnORnjP/1P/zFEndoeX+n+t1RwWRJiJpM/jO7FW32Kn9r5+sJB2JWOdYo4L6k78j15eCwY3Gm/7364B1EMwtNg==", + "license": "MIT", + "dependencies": { + "@babel/types": "^7.29.7" + }, + "bin": { + "parser": "bin/babel-parser.js" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@babel/types": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.29.7.tgz", + "integrity": "sha512-4zBIxpPzowiZpusoFkyGVwakdRJUyuH5PxQ/PrqghfdFWWasvnCdPfQXHrenDai+gyLARulZjZowCOj6fjT4pA==", + "license": "MIT", + "dependencies": { + "@babel/helper-string-parser": "^7.29.7", + "@babel/helper-validator-identifier": "^7.29.7" + }, + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.12.tgz", + "integrity": "sha512-Hhmwd6CInZ3dwpuGTF8fJG6yoWmsToE+vYgD4nytZVxcu1ulHpUQRAB1UJ8+N1Am3Mz4+xOByoQoSZf4D+CpkA==", + "cpu": [ + "ppc64" + ], + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.25.12.tgz", + "integrity": "sha512-VJ+sKvNA/GE7Ccacc9Cha7bpS8nyzVv0jdVgwNDaR4gDMC/2TTRc33Ip8qrNYUcpkOHUT5OZ0bUcNNVZQ9RLlg==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.25.12.tgz", + "integrity": "sha512-6AAmLG7zwD1Z159jCKPvAxZd4y/VTO0VkprYy+3N2FtJ8+BQWFXU+OxARIwA46c5tdD9SsKGZ/1ocqBS/gAKHg==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.25.12.tgz", + "integrity": "sha512-5jbb+2hhDHx5phYR2By8GTWEzn6I9UqR11Kwf22iKbNpYrsmRB18aX/9ivc5cabcUiAT/wM+YIZ6SG9QO6a8kg==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.25.12.tgz", + "integrity": "sha512-N3zl+lxHCifgIlcMUP5016ESkeQjLj/959RxxNYIthIg+CQHInujFuXeWbWMgnTo4cp5XVHqFPmpyu9J65C1Yg==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.25.12.tgz", + "integrity": "sha512-HQ9ka4Kx21qHXwtlTUVbKJOAnmG1ipXhdWTmNXiPzPfWKpXqASVcWdnf2bnL73wgjNrFXAa3yYvBSd9pzfEIpA==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.25.12.tgz", + "integrity": "sha512-gA0Bx759+7Jve03K1S0vkOu5Lg/85dou3EseOGUes8flVOGxbhDDh/iZaoek11Y8mtyKPGF3vP8XhnkDEAmzeg==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.25.12.tgz", + "integrity": "sha512-TGbO26Yw2xsHzxtbVFGEXBFH0FRAP7gtcPE7P5yP7wGy7cXK2oO7RyOhL5NLiqTlBh47XhmIUXuGciXEqYFfBQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.25.12.tgz", + "integrity": "sha512-lPDGyC1JPDou8kGcywY0YILzWlhhnRjdof3UlcoqYmS9El818LLfJJc3PXXgZHrHCAKs/Z2SeZtDJr5MrkxtOw==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.25.12.tgz", + "integrity": "sha512-8bwX7a8FghIgrupcxb4aUmYDLp8pX06rGh5HqDT7bB+8Rdells6mHvrFHHW2JAOPZUbnjUpKTLg6ECyzvas2AQ==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.25.12.tgz", + "integrity": "sha512-0y9KrdVnbMM2/vG8KfU0byhUN+EFCny9+8g202gYqSSVMonbsCfLjUO+rCci7pM0WBEtz+oK/PIwHkzxkyharA==", + "cpu": [ + "ia32" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.25.12.tgz", + "integrity": "sha512-h///Lr5a9rib/v1GGqXVGzjL4TMvVTv+s1DPoxQdz7l/AYv6LDSxdIwzxkrPW438oUXiDtwM10o9PmwS/6Z0Ng==", + "cpu": [ + "loong64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.25.12.tgz", + "integrity": "sha512-iyRrM1Pzy9GFMDLsXn1iHUm18nhKnNMWscjmp4+hpafcZjrr2WbT//d20xaGljXDBYHqRcl8HnxbX6uaA/eGVw==", + "cpu": [ + "mips64el" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.25.12.tgz", + "integrity": "sha512-9meM/lRXxMi5PSUqEXRCtVjEZBGwB7P/D4yT8UG/mwIdze2aV4Vo6U5gD3+RsoHXKkHCfSxZKzmDssVlRj1QQA==", + "cpu": [ + "ppc64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.25.12.tgz", + "integrity": "sha512-Zr7KR4hgKUpWAwb1f3o5ygT04MzqVrGEGXGLnj15YQDJErYu/BGg+wmFlIDOdJp0PmB0lLvxFIOXZgFRrdjR0w==", + "cpu": [ + "riscv64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.25.12.tgz", + "integrity": "sha512-MsKncOcgTNvdtiISc/jZs/Zf8d0cl/t3gYWX8J9ubBnVOwlk65UIEEvgBORTiljloIWnBzLs4qhzPkJcitIzIg==", + "cpu": [ + "s390x" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.25.12.tgz", + "integrity": "sha512-uqZMTLr/zR/ed4jIGnwSLkaHmPjOjJvnm6TVVitAa08SLS9Z0VM8wIRx7gWbJB5/J54YuIMInDquWyYvQLZkgw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.25.12.tgz", + "integrity": "sha512-xXwcTq4GhRM7J9A8Gv5boanHhRa/Q9KLVmcyXHCTaM4wKfIpWkdXiMog/KsnxzJ0A1+nD+zoecuzqPmCRyBGjg==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.25.12.tgz", + "integrity": "sha512-Ld5pTlzPy3YwGec4OuHh1aCVCRvOXdH8DgRjfDy/oumVovmuSzWfnSJg+VtakB9Cm0gxNO9BzWkj6mtO1FMXkQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.25.12.tgz", + "integrity": "sha512-fF96T6KsBo/pkQI950FARU9apGNTSlZGsv1jZBAlcLL1MLjLNIWPBkj5NlSz8aAzYKg+eNqknrUJ24QBybeR5A==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.25.12.tgz", + "integrity": "sha512-MZyXUkZHjQxUvzK7rN8DJ3SRmrVrke8ZyRusHlP+kuwqTcfWLyqMOE3sScPPyeIXN/mDJIfGXvcMqCgYKekoQw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.25.12.tgz", + "integrity": "sha512-rm0YWsqUSRrjncSXGA7Zv78Nbnw4XL6/dzr20cyrQf7ZmRcsovpcRBdhD43Nuk3y7XIoW2OxMVvwuRvk9XdASg==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.25.12.tgz", + "integrity": "sha512-3wGSCDyuTHQUzt0nV7bocDy72r2lI33QL3gkDNGkod22EsYl04sMf0qLb8luNKTOmgF/eDEDP5BFNwoBKH441w==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.25.12.tgz", + "integrity": "sha512-rMmLrur64A7+DKlnSuwqUdRKyd3UE7oPJZmnljqEptesKM8wx9J8gx5u0+9Pq0fQQW8vqeKebwNXdfOyP+8Bsg==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.25.12.tgz", + "integrity": "sha512-HkqnmmBoCbCwxUKKNPBixiWDGCpQGVsrQfJoVGYLPT41XWF8lHuE5N6WhVia2n4o5QK5M4tYr21827fNhi4byQ==", + "cpu": [ + "ia32" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.25.12.tgz", + "integrity": "sha512-alJC0uCZpTFrSL0CCDjcgleBXPnCrEAhTBILpeAp7M/OFgoqtAetfBzX0xM00MUsVVPpVjlPuMbREqnZCXaTnA==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz", + "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==", + "license": "MIT" + }, + "node_modules/@rollup/rollup-android-arm-eabi": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.61.1.tgz", + "integrity": "sha512-JnBB8MdXj45cajvTuO5FmPlvFVJRQgvrz1uSEl3NwqFnReAPGwb8EanbGi4z2nRaqLzjJSv5/JmycoTKlRZxHA==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-android-arm64": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.61.1.tgz", + "integrity": "sha512-Jx2g7iSjw4AOT0HDPHM9RV3GNjRXwybWtSFZiZAYUTjUwjVrYIwq3kBf+LnhqJlzXFAqTAh2F7IGI+O568exPw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-darwin-arm64": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.61.1.tgz", + "integrity": "sha512-0F1L/Z3Eqv8mT2n3dCpeO8GcTvHvVqkP5/t6DMsn0KzhYVcg+s7Ncl5DS8qjKYEeio6Az0Gt6nyBORay5qIlCA==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-darwin-x64": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.61.1.tgz", + "integrity": "sha512-qLttcH871ujY4YcVfUSShhOw+CsoTatYz8gRbHO7Bb92QH059/P0y5do1KMs41fY0BpD2x4AJH/gID0zFiqVKQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-freebsd-arm64": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.61.1.tgz", + "integrity": "sha512-fUI4RapGE0Oh3mb8mgfvC1O2nU1RpDZUKnDQm3xB1Ipg7C2wTs5Kstz7G2uWK99a8S2yTMq8/P4uycwNa0nJyw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-freebsd-x64": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.61.1.tgz", + "integrity": "sha512-H5YrdvJaDtI/U9/emrD4b++xkvp3y/JvOe4rizHbxvkyMfRS/CiRYdji+Pl8D0brEaNFWUh1drQxgAGIl6Xudw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-linux-arm-gnueabihf": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.61.1.tgz", + "integrity": "sha512-Q8CBCCQtDFrYtXoeUXSrnFXKOnyUhx6bz+SkL6A0E7V8kAiCJ5pamq1WtbfpVGhR5TSpXY6ak3avmDc5fHTyJA==", + "cpu": [ + "arm" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm-musleabihf": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.61.1.tgz", + "integrity": "sha512-nwnhk1581l0FBVellGcVCAT0Oi06onEA3WB53sf01VO3I0UPBkMH9sXONYME2K0ovXcNayJfNtHfm6mpJElatQ==", + "cpu": [ + "arm" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-gnu": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.61.1.tgz", + "integrity": "sha512-x5Xr49hwt3hdW75UOZm3395YwwzPyauktslv29KpWL/T+vVAzoT3azLcTWv0eMciBNrx+DYjH4paehHoLpPvpg==", + "cpu": [ + "arm64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-musl": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.61.1.tgz", + "integrity": "sha512-unMS3H73DpaoPyyEVPjGKleM/s0mkmsauTENpw4INQY8y4+IuLNjkueQ5QCtC0D3N38Y38yhAU8OoZ20S2Tm6w==", + "cpu": [ + "arm64" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-loong64-gnu": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-gnu/-/rollup-linux-loong64-gnu-4.61.1.tgz", + "integrity": "sha512-zNZzGRnAhwjFEYmvphJRV5XaQGjs62cCmeYYHUT//NbvEnHauw+I85nGG+SiVg5ld4GX8D1IbKIX+ozITQnhMQ==", + "cpu": [ + "loong64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-loong64-musl": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-musl/-/rollup-linux-loong64-musl-4.61.1.tgz", + "integrity": "sha512-LdpWGL8X209B2SIvWjqlc8VZgM6PKfontSerGepuldQmHYrAOtnMCXeJkxXGbC+PPZVOuu5czJo7fNV6aeW8rQ==", + "cpu": [ + "loong64" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-ppc64-gnu": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-gnu/-/rollup-linux-ppc64-gnu-4.61.1.tgz", + "integrity": "sha512-EC5kTtNaNGOmbMGqar8dvJy6y/hg99GAwjfBz++pxZhQATXGcRjd6c5en5wcbru0vkRmiMGsQKdMJOOf6sza4g==", + "cpu": [ + "ppc64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-ppc64-musl": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-musl/-/rollup-linux-ppc64-musl-4.61.1.tgz", + "integrity": "sha512-8hiwp6D4acEcNK78I4rP0/XtS1sknWIAMJBPdR4l6zUtyTm5KiTDr5bXmWt4foY7nAN7AThDHgkLIEZOWKbzWw==", + "cpu": [ + "ppc64" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-riscv64-gnu": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.61.1.tgz", + "integrity": "sha512-10dh/h/BqA7DuMPWSxkR8uks18FRwnwOEqr5zOTEl+NOwP/OMzKX8OFR/Of9xxDA7D5qef1Nzar5WDD2kCCr1g==", + "cpu": [ + "riscv64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-riscv64-musl": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-musl/-/rollup-linux-riscv64-musl-4.61.1.tgz", + "integrity": "sha512-YKJ5lg35DP17gcAOggnihe+APw9HLyj1Xn7gsmGumBJAUDa6NGXNixJzmkWLhcK9TOuuyQjdamzvJefkO7qHZQ==", + "cpu": [ + "riscv64" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-s390x-gnu": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.61.1.tgz", + "integrity": "sha512-Mlil5G2Jj6a7B3LWGctg+XPL9vdXYuzCtNXfxOQ0nPjc2m6ueUktocPGH9bnAM0bNRKb/bAWTujUU7IJQdQA+g==", + "cpu": [ + "s390x" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-gnu": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.61.1.tgz", + "integrity": "sha512-bVWIOIk6pV01p4CdUbPP7CJ/434z+OooYjDuFcR+44N35YvKUC66G8MGnvcWx5mWKW3g61J+t74l3Kj15Kwn2Q==", + "cpu": [ + "x64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-musl": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.61.1.tgz", + "integrity": "sha512-qy5pBvZbqNFheBz61R1rzsezjm0J7O2oNGoWtGoY89SZYLUfxAJTBAqDChqAIdB4rCiIbi9nF7yZ83GnNiLwSw==", + "cpu": [ + "x64" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-openbsd-x64": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-openbsd-x64/-/rollup-openbsd-x64-4.61.1.tgz", + "integrity": "sha512-E83TXjI4zm0+5f2qO+UOudaCYIhYwpJ5jq6YCZNIZ+6CbfhKrkAGezeiASBL9ElxAxFsRS9ZhESv8mfnj6TKeg==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ] + }, + "node_modules/@rollup/rollup-openharmony-arm64": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-openharmony-arm64/-/rollup-openharmony-arm64-4.61.1.tgz", + "integrity": "sha512-fbWnKqVkjrJN38vNe3ahkbk6iejS/3b0Nt7EEtPpE6RBacZcGXNKbzfHN3GUUlXOPghUg0j6XUGrtjX9z1sIvA==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ] + }, + "node_modules/@rollup/rollup-win32-arm64-msvc": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.61.1.tgz", + "integrity": "sha512-ArMl38iVAbk0New1ogihQNY6iphLi4ZaRsa037gUzv5yeKPY8TD3Dmy4x2RNC1VztU/uqm+G+/RwFrSka3Oy2g==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-ia32-msvc": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.61.1.tgz", + "integrity": "sha512-0mYtjHS9ucAbcATycCNK9IGBk/cCe/ma7EmSLGZdsxnOA8cjRIyU04wDpVAD9NiOfLUR9KTxdiO53uOkherqjQ==", + "cpu": [ + "ia32" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-x64-gnu": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-gnu/-/rollup-win32-x64-gnu-4.61.1.tgz", + "integrity": "sha512-gK1iCEPfpoSG9wfBihXxvBMi8ZfcWffYkEsC/Eih+iFENTaewvNcrEQ69lIOWYO5pePHKLHHO7nq5AILGO/HQQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-x64-msvc": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.61.1.tgz", + "integrity": "sha512-X+zaP2x+j4RXGfbp/seSoRHWnPxzApilDszisZxbYH5C/jTxFhCtDNdPGZb9lJyYPs24wGxruPF7Y+sIXt9Gzw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@types/estree": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz", + "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==", + "license": "MIT" + }, + "node_modules/@vitejs/plugin-vue": { + "version": "5.2.4", + "resolved": "https://registry.npmjs.org/@vitejs/plugin-vue/-/plugin-vue-5.2.4.tgz", + "integrity": "sha512-7Yx/SXSOcQq5HiiV3orevHUFn+pmMB4cgbEkDYgnkUWb0WfeQ/wa2yFv6D5ICiCQOVpjA7vYDXrC7AGO8yjDHA==", + "license": "MIT", + "engines": { + "node": "^18.0.0 || >=20.0.0" + }, + "peerDependencies": { + "vite": "^5.0.0 || ^6.0.0", + "vue": "^3.2.25" + } + }, + "node_modules/@volar/language-core": { + "version": "2.4.15", + "resolved": "https://registry.npmjs.org/@volar/language-core/-/language-core-2.4.15.tgz", + "integrity": "sha512-3VHw+QZU0ZG9IuQmzT68IyN4hZNd9GchGPhbD9+pa8CVv7rnoOZwo7T8weIbrRmihqy3ATpdfXFnqRrfPVK6CA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@volar/source-map": "2.4.15" + } + }, + "node_modules/@volar/source-map": { + "version": "2.4.15", + "resolved": "https://registry.npmjs.org/@volar/source-map/-/source-map-2.4.15.tgz", + "integrity": "sha512-CPbMWlUN6hVZJYGcU/GSoHu4EnCHiLaXI9n8c9la6RaI9W5JHX+NqG+GSQcB0JdC2FIBLdZJwGsfKyBB71VlTg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@volar/typescript": { + "version": "2.4.15", + "resolved": "https://registry.npmjs.org/@volar/typescript/-/typescript-2.4.15.tgz", + "integrity": "sha512-2aZ8i0cqPGjXb4BhkMsPYDkkuc2ZQ6yOpqwAuNwUoncELqoy5fRgOQtLR9gB0g902iS0NAkvpIzs27geVyVdPg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@volar/language-core": "2.4.15", + "path-browserify": "^1.0.1", + "vscode-uri": "^3.0.8" + } + }, + "node_modules/@vue/compiler-core": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/compiler-core/-/compiler-core-3.5.35.tgz", + "integrity": "sha512-BUmHaR1J+O+CKZ9uJucdVTEr1LHsdyvv7vG3eNRhK3CczEHeMd/LtsHAuD7PbrxvI2envCY2v7HI1vC1aBRzKw==", + "license": "MIT", + "dependencies": { + "@babel/parser": "^7.29.3", + "@vue/shared": "3.5.35", + "entities": "^7.0.1", + "estree-walker": "^2.0.2", + "source-map-js": "^1.2.1" + } + }, + "node_modules/@vue/compiler-dom": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/compiler-dom/-/compiler-dom-3.5.35.tgz", + "integrity": "sha512-k+bprkXxuqhVajgTx5mUHuir7TwQzUKOWR40ng1ncAqQRPnrLngGGgqVEEhOnTMlc8btHYVKmrP8s5Qyg0hvYA==", + "license": "MIT", + "dependencies": { + "@vue/compiler-core": "3.5.35", + "@vue/shared": "3.5.35" + } + }, + "node_modules/@vue/compiler-sfc": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/compiler-sfc/-/compiler-sfc-3.5.35.tgz", + "integrity": "sha512-G5VPMcXTSywXBgtFOZOnHKBxKSrwXUcvY1iaF5/hRcy7t0J6CH/d8ha9F4nzi00Fax1eLV0QHM7v4mQu68jydw==", + "license": "MIT", + "dependencies": { + "@babel/parser": "^7.29.3", + "@vue/compiler-core": "3.5.35", + "@vue/compiler-dom": "3.5.35", + "@vue/compiler-ssr": "3.5.35", + "@vue/shared": "3.5.35", + "estree-walker": "^2.0.2", + "magic-string": "^0.30.21", + "postcss": "^8.5.15", + "source-map-js": "^1.2.1" + } + }, + "node_modules/@vue/compiler-ssr": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/compiler-ssr/-/compiler-ssr-3.5.35.tgz", + "integrity": "sha512-rGhAeXgdM7/ffTJGXT69rCCdTmjDewnFuUZfBQQHTdcEBeWdT5HCGY60y2ytLJr9/Dsu7IntUi5z/w0h6Rjnzw==", + "license": "MIT", + "dependencies": { + "@vue/compiler-dom": "3.5.35", + "@vue/shared": "3.5.35" + } + }, + "node_modules/@vue/compiler-vue2": { + "version": "2.7.16", + "resolved": "https://registry.npmjs.org/@vue/compiler-vue2/-/compiler-vue2-2.7.16.tgz", + "integrity": "sha512-qYC3Psj9S/mfu9uVi5WvNZIzq+xnXMhOwbTFKKDD7b1lhpnn71jXSFdTQ+WsIEk0ONCd7VV2IMm7ONl6tbQ86A==", + "dev": true, + "license": "MIT", + "dependencies": { + "de-indent": "^1.0.2", + "he": "^1.2.0" + } + }, + "node_modules/@vue/devtools-api": { + "version": "6.6.4", + "resolved": "https://registry.npmjs.org/@vue/devtools-api/-/devtools-api-6.6.4.tgz", + "integrity": "sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g==", + "license": "MIT" + }, + "node_modules/@vue/language-core": { + "version": "2.2.12", + "resolved": "https://registry.npmjs.org/@vue/language-core/-/language-core-2.2.12.tgz", + "integrity": "sha512-IsGljWbKGU1MZpBPN+BvPAdr55YPkj2nB/TBNGNC32Vy2qLG25DYu/NBN2vNtZqdRbTRjaoYrahLrToim2NanA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@volar/language-core": "2.4.15", + "@vue/compiler-dom": "^3.5.0", + "@vue/compiler-vue2": "^2.7.16", + "@vue/shared": "^3.5.0", + "alien-signals": "^1.0.3", + "minimatch": "^9.0.3", + "muggle-string": "^0.4.1", + "path-browserify": "^1.0.1" + }, + "peerDependencies": { + "typescript": "*" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@vue/reactivity": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/reactivity/-/reactivity-3.5.35.tgz", + "integrity": "sha512-tVc+SsHConvh/Lz64qq1pP3rYArBmK42xonovEcxY74SQtvctZodG/zhq54P5dr38cVuw25d27cPNRdlMidpGQ==", + "license": "MIT", + "dependencies": { + "@vue/shared": "3.5.35" + } + }, + "node_modules/@vue/runtime-core": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/runtime-core/-/runtime-core-3.5.35.tgz", + "integrity": "sha512-A/xFNX9loIcWDygeQuNCfKuh0CoYBzxhqEMNah5TSFg9Z53DrFYEN2qi5CU9necjM1OWYegYREUTHmXTmhfXtg==", + "license": "MIT", + "dependencies": { + "@vue/reactivity": "3.5.35", + "@vue/shared": "3.5.35" + } + }, + "node_modules/@vue/runtime-dom": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/runtime-dom/-/runtime-dom-3.5.35.tgz", + "integrity": "sha512-odrJ1C391dbGnyDRh8U+rnP7J2amIEzfmRk5vXy7xi3aZhEXofTvpi0T4HJb6jlNqQZTNPR5MPHSB3RHNkIORA==", + "license": "MIT", + "dependencies": { + "@vue/reactivity": "3.5.35", + "@vue/runtime-core": "3.5.35", + "@vue/shared": "3.5.35", + "csstype": "^3.2.3" + } + }, + "node_modules/@vue/server-renderer": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/server-renderer/-/server-renderer-3.5.35.tgz", + "integrity": "sha512-NkebSOYdB97wi8OQcO3HqzZSlymJi/aWsN/7h74OSVhRTm6qGs3Jp3e0rCXynmWwSlKeRrnlIug+ilYoHBmQDA==", + "license": "MIT", + "dependencies": { + "@vue/compiler-ssr": "3.5.35", + "@vue/shared": "3.5.35" + }, + "peerDependencies": { + "vue": "3.5.35" + } + }, + "node_modules/@vue/shared": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/@vue/shared/-/shared-3.5.35.tgz", + "integrity": "sha512-zSbjL7gRXwks2ZQLRGCajBtBXEOXW9Ddhn/HvSdrGkE2dqGnumzW8XtusRrxrE9LvqtiqDXQ+A60Hp6mvdYxfA==", + "license": "MIT" + }, + "node_modules/agent-base": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", + "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", + "license": "MIT", + "dependencies": { + "debug": "4" + }, + "engines": { + "node": ">= 6.0.0" + } + }, + "node_modules/alien-signals": { + "version": "1.0.13", + "resolved": "https://registry.npmjs.org/alien-signals/-/alien-signals-1.0.13.tgz", + "integrity": "sha512-OGj9yyTnJEttvzhTUWuscOvtqxq5vrhF7vL9oS0xJ2mK0ItPYP1/y+vCFebfxoEyAz0++1AIwJ5CMr+Fk3nDmg==", + "dev": true, + "license": "MIT" + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" + }, + "node_modules/axios": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.17.0.tgz", + "integrity": "sha512-J8SwNxprqqpbfenehxWYXE7CW+wM1BB4w3+N+g+/Wx40xM4rsLrfPmHHxSWIxJLYDgSY/HqlFPIYb2/S3rxafw==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.16.0", + "form-data": "^4.0.5", + "https-proxy-agent": "^5.0.1", + "proxy-from-env": "^2.1.0" + } + }, + "node_modules/balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true, + "license": "MIT" + }, + "node_modules/brace-expansion": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.1.1.tgz", + "integrity": "sha512-WR1cURNjuvBLMZBMbqM0UoE+WAfdUcEV1ccD8PVBVOI+Z3ND4+SZbN8RsfT2bMuG1qwz5RFvPukSZm5fF2D5eA==", + "dev": true, + "license": "MIT", + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/csstype": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz", + "integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==", + "license": "MIT" + }, + "node_modules/de-indent": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/de-indent/-/de-indent-1.0.2.tgz", + "integrity": "sha512-e/1zu3xH5MQryN2zdVaF0OrdNLUbvWxzMbi+iNA6Bky7l1RoP8a2fIbRocyHclXt/arDrrR6lL3TqFD9pMQTsg==", + "dev": true, + "license": "MIT" + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/entities": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz", + "integrity": "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz", + "integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-set-tostringtag": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", + "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.6", + "has-tostringtag": "^1.0.2", + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/esbuild": { + "version": "0.25.12", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.12.tgz", + "integrity": "sha512-bbPBYYrtZbkt6Os6FiTLCTFxvq4tt3JKall1vRwshA3fdVztsLAatFaZobhkBC8/BrPetoa0oksYoKXoG4ryJg==", + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.25.12", + "@esbuild/android-arm": "0.25.12", + "@esbuild/android-arm64": "0.25.12", + "@esbuild/android-x64": "0.25.12", + "@esbuild/darwin-arm64": "0.25.12", + "@esbuild/darwin-x64": "0.25.12", + "@esbuild/freebsd-arm64": "0.25.12", + "@esbuild/freebsd-x64": "0.25.12", + "@esbuild/linux-arm": "0.25.12", + "@esbuild/linux-arm64": "0.25.12", + "@esbuild/linux-ia32": "0.25.12", + "@esbuild/linux-loong64": "0.25.12", + "@esbuild/linux-mips64el": "0.25.12", + "@esbuild/linux-ppc64": "0.25.12", + "@esbuild/linux-riscv64": "0.25.12", + "@esbuild/linux-s390x": "0.25.12", + "@esbuild/linux-x64": "0.25.12", + "@esbuild/netbsd-arm64": "0.25.12", + "@esbuild/netbsd-x64": "0.25.12", + "@esbuild/openbsd-arm64": "0.25.12", + "@esbuild/openbsd-x64": "0.25.12", + "@esbuild/openharmony-arm64": "0.25.12", + "@esbuild/sunos-x64": "0.25.12", + "@esbuild/win32-arm64": "0.25.12", + "@esbuild/win32-ia32": "0.25.12", + "@esbuild/win32-x64": "0.25.12" + } + }, + "node_modules/estree-walker": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", + "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==", + "license": "MIT" + }, + "node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/follow-redirects": { + "version": "1.16.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.16.0.tgz", + "integrity": "sha512-y5rN/uOsadFT/JfYwhxRS5R7Qce+g3zG97+JrtFZlC9klX/W5hD7iiLzScI4nZqUS7DNUdhPgw4xI8W2LuXlUw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz", + "integrity": "sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.2", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-tostringtag": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", + "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", + "license": "MIT", + "dependencies": { + "has-symbols": "^1.0.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz", + "integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/he": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", + "dev": true, + "license": "MIT", + "bin": { + "he": "bin/he" + } + }, + "node_modules/https-proxy-agent": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz", + "integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==", + "license": "MIT", + "dependencies": { + "agent-base": "6", + "debug": "4" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/magic-string": { + "version": "0.30.21", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", + "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==", + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.5" + } + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/minimatch": { + "version": "9.0.9", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.9.tgz", + "integrity": "sha512-OBwBN9AL4dqmETlpS2zasx+vTeWclWzkblfZk7KTA5j3jeOONz/tRCnZomUyvNg83wL5Zv9Ss6HMJXAgL8R2Yg==", + "dev": true, + "license": "ISC", + "dependencies": { + "brace-expansion": "^2.0.2" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/muggle-string": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/muggle-string/-/muggle-string-0.4.1.tgz", + "integrity": "sha512-VNTrAak/KhO2i8dqqnqnAHOa3cYBwXEZe9h+D5h/1ZqFSTEFHdM65lR7RoIqq3tBBYavsOXV84NoHXZ0AkPyqQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/nanoid": { + "version": "3.3.12", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.12.tgz", + "integrity": "sha512-ZB9RH/39qpq5Vu6Y+NmUaFhQR6pp+M2Xt76XBnEwDaGcVAqhlvxrl3B2bKS5D3NH3QR76v3aSrKaF/Kiy7lEtQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/path-browserify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", + "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==", + "dev": true, + "license": "MIT" + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", + "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/postcss": { + "version": "8.5.15", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.15.tgz", + "integrity": "sha512-FfR8sjd4em2T6fb3I2MwAJU7HWVMr9zba+enmQeeWFfCbm+UOC/0X4DS8XtpUTMwWMGbjKYP7xjfNekzyGmB3A==", + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.12", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.1" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/proxy-from-env": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-2.1.0.tgz", + "integrity": "sha512-cJ+oHTW1VAEa8cJslgmUZrc+sjRKgAKl3Zyse6+PV38hZe/V6Z14TbCuXcan9F9ghlz4QrFr2c92TNF82UkYHA==", + "license": "MIT", + "engines": { + "node": ">=10" + } + }, + "node_modules/rollup": { + "version": "4.61.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.61.1.tgz", + "integrity": "sha512-I4KW6iuRpuu2uHBLraZ1wNZe0DP7lnRha+VJ9tNaYVaVgKhW0aI3h4RYnoRPeql0flHm/Co55b7snEDcOfOJrA==", + "license": "MIT", + "dependencies": { + "@types/estree": "1.0.9" + }, + "bin": { + "rollup": "dist/bin/rollup" + }, + "engines": { + "node": ">=18.0.0", + "npm": ">=8.0.0" + }, + "optionalDependencies": { + "@rollup/rollup-android-arm-eabi": "4.61.1", + "@rollup/rollup-android-arm64": "4.61.1", + "@rollup/rollup-darwin-arm64": "4.61.1", + "@rollup/rollup-darwin-x64": "4.61.1", + "@rollup/rollup-freebsd-arm64": "4.61.1", + "@rollup/rollup-freebsd-x64": "4.61.1", + "@rollup/rollup-linux-arm-gnueabihf": "4.61.1", + "@rollup/rollup-linux-arm-musleabihf": "4.61.1", + "@rollup/rollup-linux-arm64-gnu": "4.61.1", + "@rollup/rollup-linux-arm64-musl": "4.61.1", + "@rollup/rollup-linux-loong64-gnu": "4.61.1", + "@rollup/rollup-linux-loong64-musl": "4.61.1", + "@rollup/rollup-linux-ppc64-gnu": "4.61.1", + "@rollup/rollup-linux-ppc64-musl": "4.61.1", + "@rollup/rollup-linux-riscv64-gnu": "4.61.1", + "@rollup/rollup-linux-riscv64-musl": "4.61.1", + "@rollup/rollup-linux-s390x-gnu": "4.61.1", + "@rollup/rollup-linux-x64-gnu": "4.61.1", + "@rollup/rollup-linux-x64-musl": "4.61.1", + "@rollup/rollup-openbsd-x64": "4.61.1", + "@rollup/rollup-openharmony-arm64": "4.61.1", + "@rollup/rollup-win32-arm64-msvc": "4.61.1", + "@rollup/rollup-win32-ia32-msvc": "4.61.1", + "@rollup/rollup-win32-x64-gnu": "4.61.1", + "@rollup/rollup-win32-x64-msvc": "4.61.1", + "fsevents": "~2.3.2" + } + }, + "node_modules/source-map-js": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", + "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==", + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/tinyglobby": { + "version": "0.2.17", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", + "integrity": "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==", + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.4" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "devOptional": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/vite": { + "version": "6.4.3", + "resolved": "https://registry.npmjs.org/vite/-/vite-6.4.3.tgz", + "integrity": "sha512-NTKlcQjlAK7MlQoyb6LgaqHc8sso/pVyUJYWMws3jg21uTJw/LddqIFPcPqP6PzpgbIcZyKI85sFE4HBrQDA8A==", + "license": "MIT", + "dependencies": { + "esbuild": "^0.25.0", + "fdir": "^6.4.4", + "picomatch": "^4.0.2", + "postcss": "^8.5.3", + "rollup": "^4.34.9", + "tinyglobby": "^0.2.13" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", + "jiti": ">=1.21.0", + "less": "*", + "lightningcss": "^1.21.0", + "sass": "*", + "sass-embedded": "*", + "stylus": "*", + "sugarss": "*", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "lightningcss": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vscode-uri": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/vscode-uri/-/vscode-uri-3.1.0.tgz", + "integrity": "sha512-/BpdSx+yCQGnCvecbyXdxHDkuk55/G3xwnC0GqY4gmQ3j+A+g8kzzgB4Nk/SINjqn6+waqw3EgbVF2QKExkRxQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/vue": { + "version": "3.5.35", + "resolved": "https://registry.npmjs.org/vue/-/vue-3.5.35.tgz", + "integrity": "sha512-cx89fnr+0kVGHiNFG6y6s0bdjypJRFNZn6x3WPstNdQR1bi1mbB7h4v5IBGTsPJU3nK1+0Iqj3Zf+hZWMieR4Q==", + "license": "MIT", + "dependencies": { + "@vue/compiler-dom": "3.5.35", + "@vue/compiler-sfc": "3.5.35", + "@vue/runtime-dom": "3.5.35", + "@vue/server-renderer": "3.5.35", + "@vue/shared": "3.5.35" + }, + "peerDependencies": { + "typescript": "*" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/vue-router": { + "version": "4.6.4", + "resolved": "https://registry.npmjs.org/vue-router/-/vue-router-4.6.4.tgz", + "integrity": "sha512-Hz9q5sa33Yhduglwz6g9skT8OBPii+4bFn88w6J+J4MfEo4KRRpmiNG/hHHkdbRFlLBOqxN8y8gf2Fb0MTUgVg==", + "license": "MIT", + "dependencies": { + "@vue/devtools-api": "^6.6.4" + }, + "funding": { + "url": "https://github.com/sponsors/posva" + }, + "peerDependencies": { + "vue": "^3.5.0" + } + }, + "node_modules/vue-tsc": { + "version": "2.2.12", + "resolved": "https://registry.npmjs.org/vue-tsc/-/vue-tsc-2.2.12.tgz", + "integrity": "sha512-P7OP77b2h/Pmk+lZdJ0YWs+5tJ6J2+uOQPo7tlBnY44QqQSPYvS0qVT4wqDJgwrZaLe47etJLLQRFia71GYITw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@volar/typescript": "2.4.15", + "@vue/language-core": "2.2.12" + }, + "bin": { + "vue-tsc": "bin/vue-tsc.js" + }, + "peerDependencies": { + "typescript": ">=5.0.0" + } + } + } +} diff --git a/sakura_admin_web/package.json b/sakura_admin_web/package.json new file mode 100644 index 0000000..f08c47f --- /dev/null +++ b/sakura_admin_web/package.json @@ -0,0 +1,22 @@ +{ + "name": "sakura-admin-web", + "version": "0.1.0", + "private": true, + "type": "module", + "scripts": { + "dev": "vite --host 0.0.0.0", + "build": "vue-tsc --noEmit && vite build", + "preview": "vite preview --host 0.0.0.0" + }, + "dependencies": { + "@vitejs/plugin-vue": "^5.2.4", + "axios": "^1.7.9", + "vite": "^6.0.7", + "vue": "^3.5.13", + "vue-router": "^4.5.0" + }, + "devDependencies": { + "typescript": "^5.7.2", + "vue-tsc": "^2.2.0" + } +} diff --git a/sakura_admin_web/src/App.vue b/sakura_admin_web/src/App.vue new file mode 100644 index 0000000..7c2aa3f --- /dev/null +++ b/sakura_admin_web/src/App.vue @@ -0,0 +1,3 @@ + diff --git a/sakura_admin_web/src/api/http.ts b/sakura_admin_web/src/api/http.ts new file mode 100644 index 0000000..5d91429 --- /dev/null +++ b/sakura_admin_web/src/api/http.ts @@ -0,0 +1,53 @@ +import axios, { type AxiosError } from "axios"; +import { authState, clearSession, saveSession } from "../stores/session"; + +const API_BASE = import.meta.env.VITE_API_BASE_URL || "/api/v1"; + +export const http = axios.create({ + baseURL: API_BASE, + timeout: 15000, + withCredentials: false +}); + +let refreshing: Promise | null = null; + +http.interceptors.request.use((config) => { + if (authState.accessToken) { + config.headers.Authorization = `Bearer ${authState.accessToken}`; + } + return config; +}); + +http.interceptors.response.use( + (response) => response, + async (error: AxiosError) => { + const original = error.config; + if (!original || error.response?.status !== 401 || !authState.refreshToken || original.headers?.["X-Retry"]) { + throw error; + } + refreshing ||= http + .post("/auth/refresh", { refresh_token: authState.refreshToken }, { headers: { "X-Retry": "1" } }) + .then((response) => { + const tokens = response.data?.data; + saveSession(tokens.access_token, tokens.refresh_token); + }) + .catch((refreshError) => { + clearSession(); + throw refreshError; + }) + .finally(() => { + refreshing = null; + }); + await refreshing; + original.headers = { + ...(original.headers as Record), + Authorization: `Bearer ${authState.accessToken}`, + "X-Retry": "1" + } as any; + return http(original); + } +); + +export function apiData(response: { data: { data: T } }): T { + return response.data.data; +} diff --git a/sakura_admin_web/src/main.ts b/sakura_admin_web/src/main.ts new file mode 100644 index 0000000..4ab9f8f --- /dev/null +++ b/sakura_admin_web/src/main.ts @@ -0,0 +1,6 @@ +import { createApp } from "vue"; +import App from "./App.vue"; +import { router } from "./router"; +import "./styles.css"; + +createApp(App).use(router).mount("#app"); diff --git a/sakura_admin_web/src/router/index.ts b/sakura_admin_web/src/router/index.ts new file mode 100644 index 0000000..25be953 --- /dev/null +++ b/sakura_admin_web/src/router/index.ts @@ -0,0 +1,95 @@ +import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router"; +import { authState, isAdmin, loadMe } from "../stores/auth"; +import LoginPage from "../views/public/LoginPage.vue"; +import RegisterPage from "../views/public/RegisterPage.vue"; +import ForgotPasswordPage from "../views/public/ForgotPasswordPage.vue"; +import ForbiddenPage from "../views/public/ForbiddenPage.vue"; +import NotFoundPage from "../views/public/NotFoundPage.vue"; +import AppLayout from "../views/app/AppLayout.vue"; +import UserDashboard from "../views/app/UserDashboard.vue"; +import UserListPage from "../views/app/UserListPage.vue"; +import UserDetailPage from "../views/app/UserDetailPage.vue"; +import UserChapterListPage from "../views/app/UserChapterListPage.vue"; +import UserOnlineSearchPage from "../views/app/UserOnlineSearchPage.vue"; +import AdminLayout from "../views/admin/AdminLayout.vue"; +import AdminDashboard from "../views/admin/AdminDashboard.vue"; +import AdminTablePage from "../views/admin/AdminTablePage.vue"; +import AdminUserDetail from "../views/admin/AdminUserDetail.vue"; + +const routes: RouteRecordRaw[] = [ + { path: "/", redirect: "/app" }, + { path: "/login", component: LoginPage }, + { path: "/register", component: RegisterPage }, + { path: "/forgot-password", component: ForgotPasswordPage }, + { path: "/403", component: ForbiddenPage }, + { + path: "/app", + component: AppLayout, + meta: { requiresAuth: true }, + children: [ + { path: "", component: UserDashboard }, + { path: "gallery", component: UserListPage, props: { kind: "gallery" } }, + { path: "gallery/:id", component: UserDetailPage, props: (route) => ({ kind: "gallery", id: route.params.id }) }, + // 漫画三级结构 + { path: "manga", component: UserListPage, props: { kind: "manga" } }, + { path: "manga/series/:seriesKey", component: UserChapterListPage, props: (route) => ({ kind: "manga", seriesKey: route.params.seriesKey }) }, + { path: "manga/read/:id", component: UserDetailPage, props: (route) => ({ kind: "manga", id: route.params.id }) }, + // 小说三级结构 + { path: "novels", component: UserListPage, props: { kind: "novels" } }, + { path: "novels/series/:seriesId", component: UserChapterListPage, props: (route) => ({ kind: "novels", seriesId: route.params.seriesId }) }, + { path: "novels/read/:id", component: UserDetailPage, props: (route) => ({ kind: "novels", id: route.params.id }) }, + // 旧路由重定向(兼容书签/旧链接) + { path: "manga/:id", redirect: (to) => `/app/manga/read/${to.params.id}` }, + { path: "novels/:id", redirect: (to) => `/app/novels/read/${to.params.id}` }, + { path: "search", component: UserOnlineSearchPage }, + { path: "alerts", component: UserListPage, props: { kind: "alerts" } }, + { path: "profile", component: UserDetailPage, props: { kind: "profile" } } + ] + }, + { + path: "/admin", + component: AdminLayout, + meta: { requiresAuth: true, requiresAdmin: true }, + children: [ + { path: "", component: AdminDashboard }, + { path: "users", component: AdminTablePage, props: { resource: "users" } }, + { path: "users/:id", component: AdminUserDetail }, + { path: "files", component: AdminTablePage, props: { resource: "files" } }, + { path: "gallery", component: AdminTablePage, props: { resource: "gallery" } }, + { path: "novels", component: AdminTablePage, props: { resource: "novels" } }, + { path: "ocr", component: AdminTablePage, props: { resource: "ocr/tasks" } }, + { path: "push", component: AdminTablePage, props: { resource: "push/devices" } }, + { path: "alerts", component: AdminTablePage, props: { resource: "alerts" } }, + { path: "audit-logs", component: AdminTablePage, props: { resource: "audit-logs" } }, + { path: "system", component: AdminTablePage, props: { resource: "system/health" } } + ] + }, + { path: "/404", component: NotFoundPage }, + { path: "/:pathMatch(.*)*", redirect: "/404" } +]; + +export const router = createRouter({ + history: createWebHistory(), + routes +}); + +const PUBLIC_PATHS = new Set(["/login", "/register", "/forgot-password", "/403", "/404"]); + +function isPublicPath(path: string) { + return PUBLIC_PATHS.has(path); +} + +router.beforeEach(async (to) => { + if (isPublicPath(to.path)) { + if ((to.path === "/login" || to.path === "/register") && authState.accessToken) { + const user = await loadMe().catch(() => null); + if (user) return isAdmin(user.role) ? "/admin" : "/app"; + } + return true; + } + if (!to.meta.requiresAuth) return true; + const user = await loadMe().catch(() => null); + if (!user) return { path: "/login", query: { redirect: to.fullPath } }; + if (to.meta.requiresAdmin && !isAdmin(authState.user?.role)) return "/403"; + return true; +}); diff --git a/sakura_admin_web/src/stores/auth.ts b/sakura_admin_web/src/stores/auth.ts new file mode 100644 index 0000000..54ba13a --- /dev/null +++ b/sakura_admin_web/src/stores/auth.ts @@ -0,0 +1,42 @@ +import { apiData, http } from "../api/http"; +import { authState, clearSession, saveSession, type CurrentUser, type Role } from "./session"; + +export { authState, clearSession, saveSession }; + +export async function login(usernameOrEmail: string, password: string, admin = false) { + const path = admin ? "/admin/login" : "/auth/login"; + const response = await http.post(path, { username_or_email: usernameOrEmail, password }); + const tokens = apiData<{ access_token: string; refresh_token: string }>(response); + saveSession(tokens.access_token, tokens.refresh_token); + await loadMe(); +} + +export async function register(username: string, email: string, password: string) { + await http.post("/auth/register", { username, email, password }); + await login(username, password); +} + +export async function loadMe(force = false) { + if (authState.user && !force) return authState.user; + if (!authState.accessToken) return null; + authState.loading = true; + try { + const response = await http.get("/me"); + authState.user = apiData(response); + return authState.user; + } finally { + authState.loading = false; + } +} + +export async function logout() { + try { + await http.post("/auth/logout"); + } finally { + clearSession(); + } +} + +export function isAdmin(role?: Role | null) { + return role === "admin" || role === "super_admin"; +} diff --git a/sakura_admin_web/src/stores/seriesCache.ts b/sakura_admin_web/src/stores/seriesCache.ts new file mode 100644 index 0000000..b4c5534 --- /dev/null +++ b/sakura_admin_web/src/stores/seriesCache.ts @@ -0,0 +1,87 @@ +import { reactive } from "vue"; + +export interface MangaChapterCacheEntry { + id: string; + title: string; + order: number; + page_count: number; + thumbnail_url: string; + updated_at: string; +} + +export interface MangaSeriesCacheEntry { + seriesKey: string; + seriesTitle: string; + coverUrl: string; + author: string; + chapters: MangaChapterCacheEntry[]; +} + +type MangaRow = Record; + +export const mangaSeriesCache = reactive>({}); + +function cleanText(value: unknown) { + return String(value || "").trim(); +} + +function resolveDisplayImage(value: unknown) { + const text = cleanText(value); + return text; +} + +export function mangaSeriesKey(item: MangaRow) { + const metadata = item.artwork_metadata || {}; + const seriesId = cleanText(metadata.series_id); + if (seriesId) return `manga-${seriesId}`; + const fallbackTitle = cleanText(metadata.series_title) || cleanText(item.title) || cleanText(item.source_id) || cleanText(item.id); + return `manga-title-${fallbackTitle}`; +} + +export function normalizeMangaChapter(item: MangaRow): MangaChapterCacheEntry { + const metadata = item.artwork_metadata || {}; + return { + id: String(item.id), + title: cleanText(item.title) || cleanText(metadata.chapter_title) || "未命名章节", + order: Number(metadata.chapter_order || 0), + page_count: Number(item.page_count || 1), + thumbnail_url: cleanText(item.thumbnail_url), + updated_at: cleanText(item.updated_at), + }; +} + +export function buildMangaSeriesGroups(rows: MangaRow[]) { + const groups = new Map(); + for (const row of rows) { + const key = mangaSeriesKey(row); + if (!groups.has(key)) groups.set(key, []); + groups.get(key)!.push(row); + } + + return Array.from(groups.entries()).map(([seriesKey, pages]) => { + const sorted = [...pages].sort((a, b) => normalizeMangaChapter(a).order - normalizeMangaChapter(b).order); + const latest = [...pages].sort((a, b) => Date.parse(String(b.updated_at || "")) - Date.parse(String(a.updated_at || "")))[0] || pages[0]; + const first = sorted[0] || latest; + const metadata = first?.artwork_metadata || {}; + const chapters = sorted.map(normalizeMangaChapter); + const entry: MangaSeriesCacheEntry = { + seriesKey, + seriesTitle: cleanText(metadata.series_title) || cleanText(first?.title) || "未命名漫画", + coverUrl: resolveDisplayImage(first?.thumbnail_url || first?.image_url || first?.thumbnail_path || first?.file_path), + author: cleanText(first?.author), + chapters, + }; + mangaSeriesCache[seriesKey] = entry; + return { + entry, + first, + latest, + pageTotal: pages.reduce((total, item) => total + Number(item.page_count || 1), 0), + tags: Array.isArray(first?.tags) ? first.tags : [], + }; + }); +} + +export function clearMangaCache() { + Object.keys(mangaSeriesCache).forEach((key) => delete mangaSeriesCache[key]); +} diff --git a/sakura_admin_web/src/stores/session.ts b/sakura_admin_web/src/stores/session.ts new file mode 100644 index 0000000..428887f --- /dev/null +++ b/sakura_admin_web/src/stores/session.ts @@ -0,0 +1,38 @@ +import { reactive } from "vue"; + +export type Role = "user" | "admin" | "super_admin"; + +export interface CurrentUser { + id: string; + username: string; + email: string; + role: Role; + is_active: boolean; + storage_used: number; + storage_limit: number; +} + +const ACCESS_KEY = "sakura_access_token"; +const REFRESH_KEY = "sakura_refresh_token"; + +export const authState = reactive({ + accessToken: sessionStorage.getItem(ACCESS_KEY) || "", + refreshToken: sessionStorage.getItem(REFRESH_KEY) || "", + user: null as CurrentUser | null, + loading: false +}); + +export function saveSession(accessToken: string, refreshToken: string) { + authState.accessToken = accessToken; + authState.refreshToken = refreshToken; + sessionStorage.setItem(ACCESS_KEY, accessToken); + sessionStorage.setItem(REFRESH_KEY, refreshToken); +} + +export function clearSession() { + authState.accessToken = ""; + authState.refreshToken = ""; + authState.user = null; + sessionStorage.removeItem(ACCESS_KEY); + sessionStorage.removeItem(REFRESH_KEY); +} diff --git a/sakura_admin_web/src/styles.css b/sakura_admin_web/src/styles.css new file mode 100644 index 0000000..e04e042 --- /dev/null +++ b/sakura_admin_web/src/styles.css @@ -0,0 +1,1040 @@ +:root { + color-scheme: light; + font-family: Inter, "Microsoft YaHei", "PingFang SC", system-ui, sans-serif; + color: #243142; + background: #fff7fb; + --pink: #e87ca5; + --pink-strong: #cf5f8b; + --ink: #243142; + --muted: #728094; + --line: #ead7df; + --panel: #ffffff; + --admin: #47769a; +} + +* { + box-sizing: border-box; +} + +body { + margin: 0; + min-height: 100vh; + background: linear-gradient(180deg, #fff7fb 0%, #f7fbff 100%); +} + +a { + color: inherit; + text-decoration: none; +} + +button, +input, +select { + font: inherit; +} + +.auth-shell, +.empty-page { + min-height: 100vh; + display: grid; + place-items: center; + padding: 24px; +} + +.auth-panel { + width: min(440px, 100%); + padding: 32px; + border: 1px solid var(--line); + border-radius: 8px; + background: rgba(255, 255, 255, 0.9); + box-shadow: 0 20px 50px rgba(216, 128, 166, 0.14); +} + +h1, +h2 { + margin: 0; + font-weight: 650; + letter-spacing: 0; +} + +form, +.detail-grid { + display: grid; + gap: 16px; +} + +label { + display: grid; + gap: 8px; + color: var(--muted); +} + +input, +select { + min-height: 42px; + padding: 0 12px; + border: 1px solid var(--line); + border-radius: 8px; + background: white; + color: var(--ink); +} + +button, +.button-like { + min-height: 40px; + display: inline-flex; + align-items: center; + justify-content: center; + gap: 8px; + padding: 0 16px; + border: 1px solid var(--line); + border-radius: 8px; + background: white; + color: var(--ink); + cursor: pointer; +} + +.primary { + border: 0; + background: var(--pink); + color: white; +} + +.admin-shell .primary { + background: var(--admin); +} + +.danger { + border-color: #e4b4a6; + background: #fff4ef; + color: #b6533e; +} + +.ghost { + background: transparent; +} + +.shell { + display: grid; + grid-template-columns: 220px minmax(0, 1fr); + min-height: 100vh; +} + +.sidebar { + position: sticky; + top: 0; + height: 100vh; + display: flex; + flex-direction: column; + gap: 8px; + padding: 24px 16px; + border-right: 1px solid var(--line); + background: rgba(255, 255, 255, 0.82); +} + +.sidebar strong { + margin-bottom: 16px; + color: var(--pink-strong); + font-size: 20px; +} + +.admin-sidebar strong { + color: var(--admin); +} + +.sidebar a, +.sidebar button { + justify-content: flex-start; + min-height: 38px; + padding: 0 12px; + border-radius: 8px; +} + +.sidebar a.router-link-active { + background: #ffe7f0; + color: #a63f6a; +} + +.admin-sidebar a.router-link-active { + background: #e9f3fb; + color: #315e7e; +} + +.content { + padding: 28px; + overflow: auto; +} + +.page-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + margin-bottom: 20px; +} + +.eyebrow { + margin: 0 0 6px; + color: var(--pink-strong); + font-size: 13px; + font-weight: 700; +} + +.muted, +.error { + color: var(--muted); +} + +.error { + color: #b7465c; +} + +.metric-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); + gap: 14px; +} + +.metric, +.panel { + border: 1px solid var(--line); + border-radius: 8px; + background: var(--panel); +} + +.metric { + display: grid; + gap: 8px; + padding: 18px; +} + +.metric span { + color: var(--muted); +} + +.metric strong { + font-size: 22px; +} + +.panel { + margin-top: 18px; + padding: 18px; +} + +.content-hub { + display: grid; + gap: 18px; +} + +.reader-hero, +.hub-section { + border: 1px solid var(--line); + border-radius: 8px; + background: rgba(255, 255, 255, 0.92); +} + +.reader-hero { + display: flex; + align-items: flex-end; + justify-content: space-between; + gap: 18px; + padding: 26px; +} + +.reader-hero p, +.section-head p, +.empty-inline { + margin: 6px 0 0; + color: var(--muted); +} + +.hero-actions, +.section-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + flex-wrap: wrap; +} + +.hub-section { + display: grid; + gap: 16px; + padding: 18px; +} + +.hub-two-col { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 18px; +} + +.video-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); + gap: 14px; +} + +.video-card { + min-width: 0; + display: grid; + gap: 8px; + padding: 10px; + border: 1px solid var(--line); + border-radius: 8px; + background: #fffafd; +} + +.video-frame { + position: relative; + aspect-ratio: 16 / 9; + overflow: hidden; + border-radius: 8px; + background: #241c22; +} + +.video-frame img { + width: 100%; + height: 100%; + object-fit: cover; +} + +.play-mark { + position: absolute; + inset: 0; + display: grid; + place-items: center; + color: white; + background: rgba(0, 0, 0, 0.18); + font-size: 34px; +} + +.video-card strong, +.video-card span { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.video-card span { + color: var(--muted); + font-size: 13px; +} + +.illust-grid.compact { + grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); +} + +.reading-list { + display: grid; + gap: 10px; +} + +.reading-row { + min-width: 0; + display: grid; + grid-template-columns: 62px minmax(0, 1fr); + gap: 12px; + align-items: center; + padding: 10px; + border: 1px solid var(--line); + border-radius: 8px; + background: #fffafd; +} + +.reading-cover { + width: 62px; + aspect-ratio: 3 / 4; + overflow: hidden; + border-radius: 8px; + background: #ffe8f0; +} + +.reading-cover img { + width: 100%; + height: 100%; + object-fit: cover; +} + +.reading-row strong, +.reading-row span { + display: block; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.reading-row span { + margin-top: 5px; + color: var(--muted); + font-size: 13px; +} + +.reader-nav-row { + display: flex; + gap: 10px; + flex-wrap: wrap; + margin: 14px 0; +} + +.action-row, +.toolbar, +.auth-links, +.row { + display: flex; + align-items: center; + gap: 12px; + flex-wrap: wrap; +} + +.auth-links { + justify-content: space-between; + margin-top: 18px; + color: var(--pink-strong); +} + +.small { + grid-template-columns: auto 1fr; +} + +.list { + display: grid; + gap: 10px; +} + +.list-row { + display: grid; + gap: 4px; + padding: 14px; + border: 1px solid var(--line); + border-radius: 8px; + background: #fffafd; +} + +.list-row span { + color: var(--muted); +} + +.illust-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); + gap: 14px; +} + +.illust-tile { + min-width: 0; + display: grid; + gap: 7px; + padding: 10px; + border: 1px solid var(--line); + border-radius: 8px; + background: #fffafd; + transition: border-color 0.16s ease, box-shadow 0.16s ease, transform 0.16s ease; +} + +.illust-tile:hover { + border-color: #efb5cb; + box-shadow: 0 12px 28px rgba(216, 128, 166, 0.14); + transform: translateY(-1px); +} + +.illust-thumb { + width: 100%; + aspect-ratio: 1 / 1.28; + display: grid; + place-items: center; + overflow: hidden; + border: 1px solid #f3d7e2; + border-radius: 8px; + background: #ffe8f0; + color: var(--pink-strong); + font-weight: 700; +} + +.illust-thumb img { + width: 100%; + height: 100%; + object-fit: cover; +} + +.illust-tile strong, +.illust-tile span { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.illust-tile span { + color: var(--muted); + font-size: 13px; +} + +.online-search-box { + display: grid; + gap: 12px; + margin-bottom: 18px; + padding: 14px; + border: 1px solid #f0d6e1; + border-radius: 8px; + background: #fffafd; +} + +.online-search-head, +.online-source-row { + display: flex; + gap: 12px; + align-items: center; + justify-content: space-between; + flex-wrap: wrap; +} + +.online-search-head p { + margin: 4px 0 0; +} + +.online-source-row input { + flex: 1 1 260px; +} + +.hidden-input { + display: none; +} + +.source-json-input { + width: 100%; + min-height: 88px; + resize: vertical; + padding: 10px 12px; + border: 1px solid var(--line); + border-radius: 8px; + background: white; + color: var(--ink); + font: inherit; +} + +.online-result-list { + display: grid; + gap: 10px; +} + +.online-result-row { + display: grid; + grid-template-columns: 58px minmax(0, 1fr); + gap: 12px; + align-items: center; + padding: 10px; + border: 1px solid var(--line); + border-radius: 8px; + background: white; +} + +.online-cover { + width: 58px; + aspect-ratio: 3 / 4; + display: grid; + place-items: center; + overflow: hidden; + border-radius: 8px; + background: #ffe8f0; + color: var(--pink-strong); + font-weight: 700; +} + +.online-cover img { + width: 100%; + height: 100%; + object-fit: cover; +} + +.online-result-row strong, +.online-result-row p { + min-width: 0; + margin: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.online-result-row p { + margin-top: 4px; + color: var(--muted); + font-size: 13px; +} + +.online-page-panel { + display: grid; + gap: 14px; +} + +.online-mode-row { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 10px; +} + +.online-page-controls, +.online-page-search, +.online-json-actions { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 12px; + align-items: center; +} + +.source-json-input.large { + min-height: 180px; + font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; + font-size: 13px; + line-height: 1.5; +} + +.online-page-results { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); + gap: 12px; +} + +.online-page-card { + min-width: 0; + display: grid; + grid-template-columns: 76px minmax(0, 1fr); + gap: 12px; + padding: 12px; + border: 1px solid var(--line); + border-radius: 8px; + background: #fffafd; + transition: border-color 0.16s ease, box-shadow 0.16s ease, transform 0.16s ease; +} + +.online-page-card:hover { + border-color: #efb5cb; + box-shadow: 0 12px 28px rgba(216, 128, 166, 0.14); + transform: translateY(-1px); +} + +.online-page-cover { + width: 76px; + aspect-ratio: 3 / 4; + display: grid; + place-items: center; + overflow: hidden; + border-radius: 8px; + background: #ffe8f0; + color: var(--pink-strong); + font-weight: 700; +} + +.online-page-cover img { + width: 100%; + height: 100%; + object-fit: cover; +} + +.online-page-main { + min-width: 0; + display: grid; + align-content: start; + gap: 6px; +} + +.online-page-main strong, +.online-page-main p, +.online-page-main span { + min-width: 0; + margin: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.online-page-main span { + color: var(--pink-strong); + font-size: 13px; +} + +.work-list { + display: grid; + gap: 12px; +} + +.work-card { + display: grid; + grid-template-columns: 92px minmax(0, 1fr); + gap: 14px; + padding: 14px; + border: 1px solid var(--line); + border-radius: 8px; + background: #fffafd; + transition: border-color 0.16s ease, box-shadow 0.16s ease, transform 0.16s ease; +} + +.work-card:hover { + border-color: #efb5cb; + box-shadow: 0 12px 28px rgba(216, 128, 166, 0.14); + transform: translateY(-1px); +} + +.work-cover { + width: 92px; + aspect-ratio: 3 / 4; + display: grid; + place-items: center; + overflow: hidden; + border: 1px solid #f3d7e2; + border-radius: 8px; + background: #ffe8f0; + color: var(--pink-strong); + font-weight: 700; +} + +.work-cover img { + width: 100%; + height: 100%; + object-fit: cover; +} + +.work-main { + min-width: 0; + display: grid; + align-content: start; + gap: 7px; +} + +.work-title-row { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 12px; +} + +.work-title-row strong { + min-width: 0; + font-size: 17px; + line-height: 1.35; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.work-badge { + flex: 0 0 auto; + padding: 3px 9px; + border-radius: 999px; + background: #ffe7f0; + color: var(--pink-strong); + font-size: 12px; + font-weight: 700; +} + +.work-meta, +.work-latest, +.work-summary { + margin: 0; + color: var(--muted); + line-height: 1.45; +} + +.work-latest { + color: #a63f6a; +} + +.work-summary { + display: -webkit-box; + overflow: hidden; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; +} + +.chapter-strip { + display: flex; + gap: 8px; + flex-wrap: wrap; + margin-top: 2px; +} + +.chapter-strip span { + max-width: 220px; + padding: 4px 8px; + border: 1px solid #f0d6e1; + border-radius: 8px; + background: white; + color: #7b6570; + font-size: 12px; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +table { + width: 100%; + border-collapse: collapse; +} + +th, +td { + max-width: 280px; + padding: 12px; + border-bottom: 1px solid var(--line); + text-align: left; + vertical-align: top; + overflow-wrap: anywhere; +} + +th { + color: var(--muted); + font-weight: 600; +} + +pre { + white-space: pre-wrap; + overflow-wrap: anywhere; + margin: 0; +} + +.small-link { + color: var(--admin); +} + +.actions { + display: flex; + gap: 8px; + flex-wrap: wrap; +} + +/* ===== 面包屑导航 ===== */ +.breadcrumb { + display: flex; + align-items: center; + gap: 8px; + margin-bottom: 10px; + font-size: 13px; + color: var(--muted); +} + +.breadcrumb a { + color: var(--pink-strong); + transition: opacity 0.14s; +} + +.breadcrumb a:hover { + opacity: 0.7; +} + +.breadcrumb .current { + color: var(--ink); + font-weight: 600; +} + +/* ===== 章节列表页 ===== */ +.series-header { + display: flex; + gap: 18px; + align-items: flex-start; + padding: 18px; + margin-bottom: 20px; + border: 1px solid var(--line); + border-radius: 8px; + background: #fffafd; +} + +.series-cover { + width: 110px; + aspect-ratio: 3 / 4; + border-radius: 8px; + overflow: hidden; + border: 1px solid var(--line); + flex-shrink: 0; +} + +.series-cover img { + width: 100%; + height: 100%; + object-fit: cover; +} + +.series-cover.placeholder { + display: grid; + place-items: center; + background: #ffe8f0; + color: var(--pink-strong); + font-weight: 700; + font-size: 20px; +} + +.series-info { + min-width: 0; + display: grid; + gap: 8px; + align-content: start; +} + +.series-info h2 { + font-size: 20px; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.chapter-list { + display: grid; + gap: 8px; +} + +.chapter-row { + display: flex; + align-items: center; + gap: 12px; + padding: 14px 16px; + border: 1px solid var(--line); + border-radius: 8px; + background: white; + transition: border-color 0.16s, box-shadow 0.16s; +} + +.chapter-row:hover { + border-color: var(--pink); + box-shadow: 0 6px 18px rgba(216, 128, 166, 0.1); +} + +.chapter-row .chapter-index { + flex-shrink: 0; + width: 36px; + height: 36px; + display: grid; + place-items: center; + border-radius: 50%; + background: #ffe7f0; + color: var(--pink-strong); + font-weight: 700; + font-size: 13px; +} + +.chapter-row .chapter-info { + flex: 1; + min-width: 0; +} + +.chapter-row .chapter-info strong { + display: block; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.chapter-row .chapter-info .muted { + font-size: 13px; + margin-top: 3px; +} + +@media (max-width: 760px) { + .shell { + grid-template-columns: 1fr; + } + + .sidebar { + position: static; + height: auto; + flex-direction: row; + flex-wrap: wrap; + } + + .content { + padding: 18px; + } + + .page-head { + align-items: stretch; + flex-direction: column; + } + + .reader-hero, + .section-head { + align-items: stretch; + flex-direction: column; + } + + .hero-actions { + align-items: stretch; + flex-direction: column; + } + + .hero-actions .button-like { + width: 100%; + } + + .hub-two-col { + grid-template-columns: 1fr; + } + + .video-grid { + grid-template-columns: 1fr; + } + + .work-card { + grid-template-columns: 74px minmax(0, 1fr); + gap: 12px; + padding: 12px; + } + + .work-cover { + width: 74px; + } + + .work-title-row { + align-items: stretch; + flex-direction: column; + gap: 6px; + } + + .work-title-row strong { + white-space: normal; + } + + .illust-grid { + grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); + gap: 10px; + } + + .online-source-row { + align-items: stretch; + flex-direction: column; + } + + .online-source-row input, + .online-source-row button { + width: 100%; + } + + .online-page-controls, + .online-page-search, + .online-json-actions { + grid-template-columns: 1fr; + } + + .online-page-controls .button-like, + .online-page-search button, + .online-json-actions button { + width: 100%; + } + + .online-page-results { + grid-template-columns: 1fr; + } + + .online-page-card { + grid-template-columns: 64px minmax(0, 1fr); + padding: 10px; + } + + .online-page-cover { + width: 64px; + } + + .online-page-main span { + white-space: normal; + overflow-wrap: anywhere; + } +} diff --git a/sakura_admin_web/src/views/admin/AdminDashboard.vue b/sakura_admin_web/src/views/admin/AdminDashboard.vue new file mode 100644 index 0000000..88cd2e2 --- /dev/null +++ b/sakura_admin_web/src/views/admin/AdminDashboard.vue @@ -0,0 +1,42 @@ + + + diff --git a/sakura_admin_web/src/views/admin/AdminLayout.vue b/sakura_admin_web/src/views/admin/AdminLayout.vue new file mode 100644 index 0000000..4bf7036 --- /dev/null +++ b/sakura_admin_web/src/views/admin/AdminLayout.vue @@ -0,0 +1,19 @@ + diff --git a/sakura_admin_web/src/views/admin/AdminTablePage.vue b/sakura_admin_web/src/views/admin/AdminTablePage.vue new file mode 100644 index 0000000..1c148d1 --- /dev/null +++ b/sakura_admin_web/src/views/admin/AdminTablePage.vue @@ -0,0 +1,74 @@ + + + diff --git a/sakura_admin_web/src/views/admin/AdminUserDetail.vue b/sakura_admin_web/src/views/admin/AdminUserDetail.vue new file mode 100644 index 0000000..2b4e1ab --- /dev/null +++ b/sakura_admin_web/src/views/admin/AdminUserDetail.vue @@ -0,0 +1,58 @@ + + + diff --git a/sakura_admin_web/src/views/app/AppLayout.vue b/sakura_admin_web/src/views/app/AppLayout.vue new file mode 100644 index 0000000..32babb1 --- /dev/null +++ b/sakura_admin_web/src/views/app/AppLayout.vue @@ -0,0 +1,28 @@ + + + diff --git a/sakura_admin_web/src/views/app/UserChapterListPage.vue b/sakura_admin_web/src/views/app/UserChapterListPage.vue new file mode 100644 index 0000000..5095065 --- /dev/null +++ b/sakura_admin_web/src/views/app/UserChapterListPage.vue @@ -0,0 +1,207 @@ + + + diff --git a/sakura_admin_web/src/views/app/UserDashboard.vue b/sakura_admin_web/src/views/app/UserDashboard.vue new file mode 100644 index 0000000..61da836 --- /dev/null +++ b/sakura_admin_web/src/views/app/UserDashboard.vue @@ -0,0 +1,207 @@ + + + diff --git a/sakura_admin_web/src/views/app/UserDetailPage.vue b/sakura_admin_web/src/views/app/UserDetailPage.vue new file mode 100644 index 0000000..ae65732 --- /dev/null +++ b/sakura_admin_web/src/views/app/UserDetailPage.vue @@ -0,0 +1,203 @@ + + + diff --git a/sakura_admin_web/src/views/app/UserListPage.vue b/sakura_admin_web/src/views/app/UserListPage.vue new file mode 100644 index 0000000..ec16ee4 --- /dev/null +++ b/sakura_admin_web/src/views/app/UserListPage.vue @@ -0,0 +1,414 @@ + + + + diff --git a/sakura_admin_web/src/views/app/UserOnlineSearchPage.vue b/sakura_admin_web/src/views/app/UserOnlineSearchPage.vue new file mode 100644 index 0000000..86d7ada --- /dev/null +++ b/sakura_admin_web/src/views/app/UserOnlineSearchPage.vue @@ -0,0 +1,231 @@ + + + diff --git a/sakura_admin_web/src/views/public/ForbiddenPage.vue b/sakura_admin_web/src/views/public/ForbiddenPage.vue new file mode 100644 index 0000000..36b5890 --- /dev/null +++ b/sakura_admin_web/src/views/public/ForbiddenPage.vue @@ -0,0 +1,7 @@ + diff --git a/sakura_admin_web/src/views/public/ForgotPasswordPage.vue b/sakura_admin_web/src/views/public/ForgotPasswordPage.vue new file mode 100644 index 0000000..3614931 --- /dev/null +++ b/sakura_admin_web/src/views/public/ForgotPasswordPage.vue @@ -0,0 +1,10 @@ + diff --git a/sakura_admin_web/src/views/public/LoginPage.vue b/sakura_admin_web/src/views/public/LoginPage.vue new file mode 100644 index 0000000..f467c36 --- /dev/null +++ b/sakura_admin_web/src/views/public/LoginPage.vue @@ -0,0 +1,54 @@ + + + diff --git a/sakura_admin_web/src/views/public/NotFoundPage.vue b/sakura_admin_web/src/views/public/NotFoundPage.vue new file mode 100644 index 0000000..16c0ac4 --- /dev/null +++ b/sakura_admin_web/src/views/public/NotFoundPage.vue @@ -0,0 +1,7 @@ + diff --git a/sakura_admin_web/src/views/public/RegisterPage.vue b/sakura_admin_web/src/views/public/RegisterPage.vue new file mode 100644 index 0000000..d161d87 --- /dev/null +++ b/sakura_admin_web/src/views/public/RegisterPage.vue @@ -0,0 +1,40 @@ + + + diff --git a/sakura_admin_web/src/vite-env.d.ts b/sakura_admin_web/src/vite-env.d.ts new file mode 100644 index 0000000..11f02fe --- /dev/null +++ b/sakura_admin_web/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/sakura_admin_web/tsconfig.json b/sakura_admin_web/tsconfig.json new file mode 100644 index 0000000..c673651 --- /dev/null +++ b/sakura_admin_web/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "ES2022", + "useDefineForClassFields": true, + "module": "ESNext", + "moduleResolution": "Bundler", + "strict": true, + "jsx": "preserve", + "sourceMap": true, + "resolveJsonModule": true, + "isolatedModules": true, + "esModuleInterop": true, + "lib": ["ES2022", "DOM", "DOM.Iterable"], + "types": ["vite/client"] + }, + "include": ["src/**/*.ts", "src/**/*.vue"] +} diff --git a/sakura_admin_web/vite.config.ts b/sakura_admin_web/vite.config.ts new file mode 100644 index 0000000..9a302ac --- /dev/null +++ b/sakura_admin_web/vite.config.ts @@ -0,0 +1,15 @@ +import { defineConfig } from "vite"; +import vue from "@vitejs/plugin-vue"; + +export default defineConfig({ + plugins: [vue()], + server: { + port: 5174, + proxy: { + "/api": { + target: "http://127.0.0.1:8000", + changeOrigin: true + } + } + } +}); diff --git a/sakura_cloud/__init__.py b/sakura_cloud/__init__.py new file mode 100644 index 0000000..489421c --- /dev/null +++ b/sakura_cloud/__init__.py @@ -0,0 +1 @@ +"""Sakura Cloud backend package.""" diff --git a/sakura_cloud/alembic.ini b/sakura_cloud/alembic.ini new file mode 100644 index 0000000..66bcc51 --- /dev/null +++ b/sakura_cloud/alembic.ini @@ -0,0 +1,38 @@ +[alembic] +script_location = migrations +prepend_sys_path = .. +version_path_separator = os +sqlalchemy.url = postgresql+psycopg://sakura:sakura@localhost:5432/sakura_cloud + +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/sakura_cloud/api/__init__.py b/sakura_cloud/api/__init__.py new file mode 100644 index 0000000..47efa97 --- /dev/null +++ b/sakura_cloud/api/__init__.py @@ -0,0 +1 @@ +"""HTTP API package.""" diff --git a/sakura_cloud/api/deps.py b/sakura_cloud/api/deps.py new file mode 100644 index 0000000..820c656 --- /dev/null +++ b/sakura_cloud/api/deps.py @@ -0,0 +1,65 @@ +from __future__ import annotations + +from typing import Annotated + +from fastapi import Depends, HTTPException, status +from fastapi.security import OAuth2PasswordBearer +from sqlalchemy.orm import Session + +from sakura_cloud.core.config import settings +from sakura_cloud.core.security import decode_token +from sakura_cloud.db.session import get_db +from sakura_cloud.models.user import User, UserRole + + +oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.api_prefix}/auth/login") +DbSession = Annotated[Session, Depends(get_db)] + + +def get_current_user(token: Annotated[str, Depends(oauth2_scheme)], db: DbSession) -> User: + try: + payload = decode_token(token) + except ValueError as exc: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") from exc + if payload.get("type") != "access": + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token type") + user_id = payload.get("sub") + if not user_id: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token subject") + user = db.get(User, str(user_id)) + if user is None: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found") + if not user.is_active: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User is disabled") + if int(payload.get("tv") or 0) != int(user.token_version or 0): + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Token has been revoked") + return user + + +CurrentUser = Annotated[User, Depends(get_current_user)] + + +def require_admin_user(current_user: CurrentUser) -> User: + if current_user.role not in {UserRole.ADMIN, UserRole.SUPER_ADMIN}: + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin privileges required") + return current_user + + +AdminUser = Annotated[User, Depends(require_admin_user)] + + +def require_super_admin_user(current_user: CurrentUser) -> User: + if current_user.role != UserRole.SUPER_ADMIN: + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Super admin privileges required") + return current_user + + +SuperAdminUser = Annotated[User, Depends(require_super_admin_user)] + + +def assert_owner_or_admin(resource_user_id: str, current_user: User) -> None: + if resource_user_id == current_user.id: + return + if current_user.role in {UserRole.ADMIN, UserRole.SUPER_ADMIN}: + return + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Resource not found") diff --git a/sakura_cloud/api/middleware.py b/sakura_cloud/api/middleware.py new file mode 100644 index 0000000..ae4a382 --- /dev/null +++ b/sakura_cloud/api/middleware.py @@ -0,0 +1,87 @@ +from __future__ import annotations + +import time +import uuid +from collections.abc import Callable +from urllib.parse import urlparse + +from fastapi import Request, Response +from loguru import logger + +from sakura_cloud.core.config import settings + + +def _origin(value: str) -> str: + parsed = urlparse(value) + if not parsed.scheme or not parsed.netloc: + return "" + return f"{parsed.scheme}://{parsed.netloc}" + + +def _csp_connect_sources(request: Request) -> str: + sources = {"'self'"} + request_origin = _origin(str(request.base_url)) + if request_origin: + sources.add(request_origin) + for origin in settings.cors_origins: + if origin == "*": + continue + parsed_origin = _origin(origin) + if parsed_origin: + sources.add(parsed_origin) + if settings.debug: + sources.update( + { + "http://localhost:*", + "http://127.0.0.1:*", + "ws://localhost:5173", + "ws://localhost:5174", + "ws://127.0.0.1:5173", + "ws://127.0.0.1:5174", + } + ) + return " ".join(sorted(sources)) + + +async def trace_logging_middleware(request: Request, call_next: Callable) -> Response: + trace_id = request.headers.get("X-Trace-Id") or str(uuid.uuid4()) + start = time.perf_counter() + try: + response = await call_next(request) + except Exception: + logger.bind(trace_id=trace_id, kind="api").exception("request failed") + raise + elapsed_ms = int((time.perf_counter() - start) * 1000) + response.headers["X-Trace-Id"] = trace_id + logger.bind(trace_id=trace_id, kind="api").info( + "{method} {path} {status} {elapsed_ms}ms", + method=request.method, + path=request.url.path, + status=response.status_code, + elapsed_ms=elapsed_ms, + ) + return response + + +async def security_headers_middleware(request: Request, call_next: Callable) -> Response: + response = await call_next(request) + response.headers.setdefault("X-Content-Type-Options", "nosniff") + response.headers.setdefault("X-Frame-Options", "DENY") + response.headers.setdefault("Referrer-Policy", "no-referrer") + response.headers.setdefault("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=()") + csp = ( + "default-src 'self'; " + "base-uri 'self'; " + "object-src 'none'; " + "frame-ancestors 'none'; " + "img-src 'self' data: blob: https: http:; " + "font-src 'self' data:; " + "style-src 'self' 'unsafe-inline'; " + "script-src 'self'; " + f"connect-src {_csp_connect_sources(request)}" + ) + response.headers.setdefault( + "Content-Security-Policy", + csp, + ) + return response diff --git a/sakura_cloud/api/v1/__init__.py b/sakura_cloud/api/v1/__init__.py new file mode 100644 index 0000000..be2f397 --- /dev/null +++ b/sakura_cloud/api/v1/__init__.py @@ -0,0 +1 @@ +"""Versioned API routes.""" diff --git a/sakura_cloud/api/v1/endpoints/__init__.py b/sakura_cloud/api/v1/endpoints/__init__.py new file mode 100644 index 0000000..1bdb261 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/__init__.py @@ -0,0 +1 @@ +"""API endpoint modules.""" diff --git a/sakura_cloud/api/v1/endpoints/admin.py b/sakura_cloud/api/v1/endpoints/admin.py new file mode 100644 index 0000000..d8977fd --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/admin.py @@ -0,0 +1,659 @@ +from __future__ import annotations + +import math +import time +from collections import defaultdict, deque +from pathlib import Path + +from fastapi import APIRouter, HTTPException, Query, Request, status +from sqlalchemy import func, or_, select, text + +from sakura_cloud.api.deps import AdminUser, DbSession, SuperAdminUser +from sakura_cloud.core.config import settings +from sakura_cloud.core.security import create_access_token, create_refresh_token, hash_password, verify_password +from sakura_cloud.models.admin import AdminAuditLog +from sakura_cloud.models.artwork import Artwork +from sakura_cloud.models.file import FileBlob, UploadSession, UserFile +from sakura_cloud.models.follow import CreatorFollow, UpdateAlert +from sakura_cloud.models.novel import Novel +from sakura_cloud.models.ocr import ArtworkOcrText +from sakura_cloud.models.push import PushDeviceToken +from sakura_cloud.models.user import User, UserRole +from sakura_cloud.models.mixins import utc_now +from sakura_cloud.schemas.admin import ( + AdminAlertStatOut, + AdminAlertOut, + AdminAuditLogOut, + AdminArtworkOut, + AdminFileOut, + AdminFileStatOut, + AdminHealthOut, + AdminListResponse, + AdminLogLineOut, + AdminLoginRequest, + AdminMigrationOut, + AdminNovelOut, + AdminOcrTaskOut, + AdminPage, + AdminPushDeviceOut, + AdminStatsOut, + AdminTaskOut, + AdminUserOut, + AdminUserStatusUpdate, + AdminUserRoleUpdate, + AdminUserPasswordReset, +) +from sakura_cloud.schemas.auth import TokenPair +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.services.audit import write_admin_audit_log +from sakura_cloud.services.ocr import run_ocr_for_artwork +from sakura_cloud.services.push import send_push_to_user + + +router = APIRouter() +_login_attempts: dict[str, deque[float]] = defaultdict(deque) + + +@router.post("/login", response_model=ApiResponse[TokenPair]) +def admin_login(payload: AdminLoginRequest, request: Request, db: DbSession) -> ApiResponse[TokenPair]: + _check_login_rate_limit(request) + login_id = payload.username_or_email.strip() + user = db.scalar( + select(User).where( + or_( + func.lower(User.username) == login_id.lower(), + func.lower(User.email) == login_id.lower(), + ) + ) + ) + if ( + user is None + or user.role not in {UserRole.ADMIN, UserRole.SUPER_ADMIN} + or not user.is_active + or not verify_password(payload.password, user.password_hash) + ): + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid admin credentials") + write_admin_audit_log(db, user, action="admin_login", target_type="user", target_id=user.id, request=request) + return ApiResponse( + data=TokenPair( + access_token=create_access_token(user.id, user.token_version), + refresh_token=create_refresh_token(user.id, user.token_version), + ) + ) + + +@router.get("/dashboard", response_model=ApiResponse[AdminStatsOut]) +def dashboard(_: AdminUser, db: DbSession) -> ApiResponse[AdminStatsOut]: + storage_used = int(db.scalar(select(func.coalesce(func.sum(FileBlob.file_size), 0))) or 0) + data = AdminStatsOut( + users=_count(db, User), + active_users=int(db.scalar(select(func.count()).select_from(User).where(User.is_active.is_(True))) or 0), + artworks=_count(db, Artwork), + novels=_count(db, Novel), + files=_count(db, FileBlob), + storage_used=storage_used, + uploads_pending=int(db.scalar(select(func.count()).select_from(UploadSession).where(UploadSession.status != "completed")) or 0), + ocr_pending=int(db.scalar(select(func.count()).select_from(ArtworkOcrText).where(ArtworkOcrText.status.in_(["pending", "running"]))) or 0), + ocr_failed=int(db.scalar(select(func.count()).select_from(ArtworkOcrText).where(ArtworkOcrText.status == "failed")) or 0), + push_devices=_count(db, PushDeviceToken), + follows=_count(db, CreatorFollow), + alerts=_count(db, UpdateAlert), + ) + return ApiResponse(data=data) + + +@router.get("/users", response_model=ApiResponse[AdminListResponse[AdminUserOut]]) +def list_users( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + q: str | None = Query(default=None, max_length=100), + role: str | None = Query(default=None, pattern="^(user|admin|super_admin)$"), + sort: str = Query(default="created_at", pattern="^(created_at|updated_at|username|storage_used)$"), + order: str = Query(default="desc", pattern="^(asc|desc)$"), +) -> ApiResponse[AdminListResponse[AdminUserOut]]: + query = select(User) + count_query = select(func.count()).select_from(User) + filters = [] + if q: + like = f"%{q.strip()}%" + filters.append(or_(User.username.ilike(like), User.email.ilike(like))) + if role: + filters.append(User.role == _role_from_value(role)) + for item in filters: + query = query.where(item) + count_query = count_query.where(item) + total = int(db.scalar(count_query) or 0) + order_col = getattr(User, sort) + if order == "desc": + order_col = order_col.desc() + rows = db.scalars(query.order_by(order_col).offset((page - 1) * limit).limit(limit)).all() + return ApiResponse(data=_page([AdminUserOut.model_validate(row) for row in rows], page, limit, total)) + + +@router.get("/users/{user_id}", response_model=ApiResponse[AdminUserOut]) +def get_user(user_id: str, _: AdminUser, db: DbSession) -> ApiResponse[AdminUserOut]: + user = db.get(User, user_id) + if user is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") + return ApiResponse(data=AdminUserOut.model_validate(user)) + + +@router.patch("/users/{user_id}/status", response_model=ApiResponse[AdminUserOut]) +def set_user_status( + user_id: str, + payload: AdminUserStatusUpdate, + request: Request, + current_admin: AdminUser, + db: DbSession, +) -> ApiResponse[AdminUserOut]: + user = db.get(User, user_id) + if user is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") + _ensure_can_operate_user(current_admin, user, desired_active=payload.is_active) + user.is_active = payload.is_active + user.disabled_at = None if payload.is_active else utc_now() + user.disabled_reason = None if payload.is_active else payload.reason + user.token_version += 1 + db.commit() + db.refresh(user) + write_admin_audit_log( + db, + current_admin, + action="enable_user" if payload.is_active else "disable_user", + target_type="user", + target_id=user.id, + request=request, + detail={"is_active": payload.is_active, "reason": payload.reason}, + ) + return ApiResponse(data=AdminUserOut.model_validate(user)) + + +@router.patch("/users/{user_id}/role", response_model=ApiResponse[AdminUserOut]) +def set_user_role( + user_id: str, + payload: AdminUserRoleUpdate, + request: Request, + current_admin: SuperAdminUser, + db: DbSession, +) -> ApiResponse[AdminUserOut]: + user = db.get(User, user_id) + if user is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") + new_role = _role_from_value(payload.role) + if user.id == current_admin.id and new_role != UserRole.SUPER_ADMIN: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Cannot demote yourself") + if new_role != UserRole.SUPER_ADMIN and user.role == UserRole.SUPER_ADMIN and _super_admin_count(db) <= 1: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="At least one super admin is required") + user.role = new_role + user.token_version += 1 + db.commit() + db.refresh(user) + write_admin_audit_log(db, current_admin, action="change_user_role", target_type="user", target_id=user.id, request=request, detail={"role": payload.role}) + return ApiResponse(data=AdminUserOut.model_validate(user)) + + +@router.post("/users/{user_id}/reset-password", response_model=ApiResponse[dict[str, bool]]) +def reset_user_password( + user_id: str, + payload: AdminUserPasswordReset, + request: Request, + current_admin: AdminUser, + db: DbSession, +) -> ApiResponse[dict[str, bool]]: + user = db.get(User, user_id) + if user is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") + _ensure_can_operate_user(current_admin, user) + user.password_hash = hash_password(payload.new_password) + user.token_version += 1 + db.commit() + write_admin_audit_log(db, current_admin, action="reset_user_password", target_type="user", target_id=user.id, request=request) + return ApiResponse(data={"ok": True}) + + +@router.get("/users/{user_id}/files", response_model=ApiResponse[AdminListResponse[AdminFileOut]]) +def user_files(user_id: str, _: AdminUser, db: DbSession, page: int = Query(default=1, ge=1), limit: int = Query(default=30, ge=1, le=100)): + if db.get(User, user_id) is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") + return _list_files(db, page=page, limit=limit, user_id=user_id) + + +@router.get("/files/stats", response_model=ApiResponse[list[AdminFileStatOut]]) +def file_stats(_: AdminUser, db: DbSession) -> ApiResponse[list[AdminFileStatOut]]: + rows = db.execute( + select(FileBlob.mime_type, func.count(FileBlob.sha256), func.coalesce(func.sum(FileBlob.file_size), 0)).group_by(FileBlob.mime_type) + ).all() + return ApiResponse(data=[AdminFileStatOut(mime_type=row[0], files=int(row[1]), bytes=int(row[2] or 0)) for row in rows]) + + +@router.get("/files", response_model=ApiResponse[AdminListResponse[AdminFileOut]]) +def list_files( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + user_id: str | None = Query(default=None, max_length=36), + mime_type: str | None = Query(default=None, max_length=50), +) -> ApiResponse[AdminListResponse[AdminFileOut]]: + return _list_files(db, page=page, limit=limit, user_id=user_id, mime_type=mime_type) + + +@router.delete("/files/{file_id}", response_model=ApiResponse[dict[str, bool]]) +def delete_file(file_id: str, request: Request, current_admin: AdminUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + row = db.get(UserFile, file_id) + if row is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found") + db.delete(row) + db.commit() + write_admin_audit_log(db, current_admin, action="delete_file", target_type="file", target_id=file_id, request=request) + return ApiResponse(data={"ok": True}) + + +@router.get("/gallery", response_model=ApiResponse[AdminListResponse[AdminArtworkOut]]) +def admin_gallery( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + user_id: str | None = Query(default=None, max_length=36), + platform: str | None = Query(default=None, max_length=20), + content_type: str | None = Query(default=None, max_length=20), + q: str | None = Query(default=None, max_length=100), +) -> ApiResponse[AdminListResponse[AdminArtworkOut]]: + query = select(Artwork) + count_query = select(func.count()).select_from(Artwork) + filters = [] + if user_id: + filters.append(Artwork.user_id == user_id) + if platform: + filters.append(Artwork.source_platform == platform) + if content_type: + filters.append(Artwork.content_type == content_type) + if q: + like = f"%{q.strip()}%" + filters.append(or_(Artwork.title.ilike(like), Artwork.author.ilike(like))) + for item in filters: + query = query.where(item) + count_query = count_query.where(item) + total = int(db.scalar(count_query) or 0) + rows = db.scalars(query.order_by(Artwork.updated_at.desc()).offset((page - 1) * limit).limit(limit)).all() + return ApiResponse(data=_page([AdminArtworkOut.model_validate(row) for row in rows], page, limit, total)) + + +@router.get("/novels", response_model=ApiResponse[AdminListResponse[AdminNovelOut]]) +def admin_novels( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + user_id: str | None = Query(default=None, max_length=36), + q: str | None = Query(default=None, max_length=100), +) -> ApiResponse[AdminListResponse[AdminNovelOut]]: + query = select(Novel) + count_query = select(func.count()).select_from(Novel) + filters = [] + if user_id: + filters.append(Novel.user_id == user_id) + if q: + like = f"%{q.strip()}%" + filters.append(or_(Novel.title.ilike(like), Novel.author.ilike(like), Novel.series_title.ilike(like))) + for item in filters: + query = query.where(item) + count_query = count_query.where(item) + total = int(db.scalar(count_query) or 0) + rows = db.scalars(query.order_by(Novel.updated_at.desc()).offset((page - 1) * limit).limit(limit)).all() + return ApiResponse(data=_page([AdminNovelOut.model_validate(row) for row in rows], page, limit, total)) + + +@router.get("/uploads", response_model=ApiResponse[AdminListResponse[AdminTaskOut]]) +def upload_tasks( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + status_filter: str | None = Query(default=None, alias="status", max_length=30), +) -> ApiResponse[AdminListResponse[AdminTaskOut]]: + query = select(UploadSession) + count_query = select(func.count()).select_from(UploadSession) + if status_filter: + query = query.where(UploadSession.status == status_filter) + count_query = count_query.where(UploadSession.status == status_filter) + total = int(db.scalar(count_query) or 0) + rows = db.scalars(query.order_by(UploadSession.updated_at.desc()).offset((page - 1) * limit).limit(limit)).all() + items = [ + AdminTaskOut( + id=row.id, + user_id=row.user_id, + status=row.status, + file_name=Path(row.file_name).name if row.file_name else None, + resource_type=row.resource_type, + file_size=row.file_size, + updated_at=row.updated_at, + ) + for row in rows + ] + return ApiResponse(data=_page(items, page, limit, total)) + + +@router.get("/ocr/tasks", response_model=ApiResponse[AdminListResponse[AdminOcrTaskOut]]) +def ocr_tasks( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + status_filter: str | None = Query(default=None, alias="status", pattern="^(pending|running|done|failed)$"), +) -> ApiResponse[AdminListResponse[AdminOcrTaskOut]]: + query = select(ArtworkOcrText) + count_query = select(func.count()).select_from(ArtworkOcrText) + if status_filter: + query = query.where(ArtworkOcrText.status == status_filter) + count_query = count_query.where(ArtworkOcrText.status == status_filter) + total = int(db.scalar(count_query) or 0) + rows = db.scalars(query.order_by(ArtworkOcrText.updated_at.desc()).offset((page - 1) * limit).limit(limit)).all() + items = [ + AdminOcrTaskOut( + id=row.id, + user_id=row.user_id, + artwork_id=row.artwork_id, + status=row.status, + language=row.language, + confidence=row.confidence, + engine=row.engine, + attempts=row.attempts, + error=row.error, + text_length=len(row.ocr_text or ""), + updated_at=row.updated_at, + ) + for row in rows + ] + return ApiResponse(data=_page(items, page, limit, total)) + + +@router.post("/ocr/tasks/{task_id}/retry", response_model=ApiResponse[AdminOcrTaskOut]) +def retry_ocr_task(task_id: str, request: Request, current_admin: AdminUser, db: DbSession) -> ApiResponse[AdminOcrTaskOut]: + task = db.get(ArtworkOcrText, task_id) + if task is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="OCR task not found") + artwork = db.get(Artwork, task.artwork_id) + if artwork is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + task.status = "pending" + task.error = None + db.commit() + row = run_ocr_for_artwork(db, artwork) + write_admin_audit_log(db, current_admin, action="retry_ocr", target_type="ocr_task", target_id=task_id, request=request) + return ApiResponse(data=_ocr_out(row)) + + +@router.get("/push/devices", response_model=ApiResponse[AdminListResponse[AdminPushDeviceOut]]) +def push_devices( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), +) -> ApiResponse[AdminListResponse[AdminPushDeviceOut]]: + total = int(db.scalar(select(func.count()).select_from(PushDeviceToken)) or 0) + rows = db.scalars(select(PushDeviceToken).order_by(PushDeviceToken.updated_at.desc()).offset((page - 1) * limit).limit(limit)).all() + items = [_push_device_out(row) for row in rows] + return ApiResponse(data=_page(items, page, limit, total)) + + +@router.get("/push/logs", response_model=ApiResponse[list[AdminLogLineOut]]) +def push_logs(_: AdminUser, limit: int = Query(default=100, ge=1, le=500)) -> ApiResponse[list[AdminLogLineOut]]: + return ApiResponse(data=_read_filtered_log_lines(("FCM", "push"), limit=limit)) + + +@router.post("/push/test", response_model=ApiResponse[dict[str, int]]) +def send_test_push(request: Request, current_admin: AdminUser, db: DbSession, user_id: str = Query(max_length=36)) -> ApiResponse[dict[str, int]]: + user = db.get(User, user_id) + if user is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") + summary = send_push_to_user(db, user_id, "Sakura Cloud test push", "Admin test push has been sent.", {"kind": "admin_test"}) + write_admin_audit_log(db, current_admin, action="send_test_push", target_type="user", target_id=user_id, request=request) + return ApiResponse(data={"attempted": summary.attempted, "sent": summary.sent, "disabled": summary.disabled, "failed": summary.failed}) + + +@router.get("/alerts/stats", response_model=ApiResponse[list[AdminAlertStatOut]]) +def alert_stats(_: AdminUser, db: DbSession) -> ApiResponse[list[AdminAlertStatOut]]: + rows = db.execute( + select(UpdateAlert.platform, func.count(UpdateAlert.id)).group_by(UpdateAlert.platform) + ).all() + data = [] + for platform, total in rows: + unread = int(db.scalar(select(func.count()).select_from(UpdateAlert).where(UpdateAlert.platform == platform, UpdateAlert.is_read.is_(False))) or 0) + data.append(AdminAlertStatOut(platform=platform, alerts=int(total), unread=unread)) + return ApiResponse(data=data) + + +@router.get("/alerts", response_model=ApiResponse[AdminListResponse[AdminAlertOut]]) +def admin_alerts( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + user_id: str | None = Query(default=None, max_length=36), + platform: str | None = Query(default=None, max_length=20), +) -> ApiResponse[AdminListResponse[AdminAlertOut]]: + query = select(UpdateAlert) + count_query = select(func.count()).select_from(UpdateAlert) + filters = [] + if user_id: + filters.append(UpdateAlert.user_id == user_id) + if platform: + filters.append(UpdateAlert.platform == platform) + for item in filters: + query = query.where(item) + count_query = count_query.where(item) + total = int(db.scalar(count_query) or 0) + rows = db.scalars(query.order_by(UpdateAlert.created_at.desc()).offset((page - 1) * limit).limit(limit)).all() + return ApiResponse(data=_page([AdminAlertOut.model_validate(row) for row in rows], page, limit, total)) + + +@router.get("/system/health", response_model=ApiResponse[AdminHealthOut]) +def admin_system_health(current_admin: AdminUser) -> ApiResponse[AdminHealthOut]: + return admin_health(current_admin) + + +@router.get("/health", response_model=ApiResponse[AdminHealthOut]) +def admin_health(_: AdminUser) -> ApiResponse[AdminHealthOut]: + return ApiResponse( + data=AdminHealthOut( + ok=True, + debug=settings.debug, + auto_migrate_on_startup=settings.auto_migrate_on_startup, + database=_redact_url(settings.database_url), + storage_root=str(Path(settings.storage_root).name or "storage"), + log_dir=str(Path(settings.log_dir).name or "logs"), + app_version=settings.app_version, + ) + ) + + +@router.get("/logs/errors", response_model=ApiResponse[list[AdminLogLineOut]]) +def error_logs( + _: AdminUser, + file: str = Query(default="error.log", pattern=r"^[A-Za-z0-9_.-]+$"), + limit: int = Query(default=100, ge=1, le=500), +) -> ApiResponse[list[AdminLogLineOut]]: + path = (Path(settings.log_dir) / file).resolve() + root = Path(settings.log_dir).resolve() + if not str(path).startswith(str(root)) or not path.is_file(): + return ApiResponse(data=[]) + lines = path.read_text(encoding="utf-8", errors="replace").splitlines()[-limit:] + return ApiResponse(data=[AdminLogLineOut(line_no=index + 1, text=_redact_log_line(line)) for index, line in enumerate(lines)]) + + +@router.get("/migrations/version", response_model=ApiResponse[AdminMigrationOut]) +def migration_version(_: AdminUser, db: DbSession) -> ApiResponse[AdminMigrationOut]: + try: + version = db.execute(text("SELECT version_num FROM alembic_version LIMIT 1")).scalar() + except Exception: + version = None + return ApiResponse(data=AdminMigrationOut(version=str(version) if version else None)) + + +@router.get("/audit-logs", response_model=ApiResponse[AdminListResponse[AdminAuditLogOut]]) +def audit_logs( + _: AdminUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + action: str | None = Query(default=None, max_length=100), +) -> ApiResponse[AdminListResponse[AdminAuditLogOut]]: + query = select(AdminAuditLog) + count_query = select(func.count()).select_from(AdminAuditLog) + if action: + query = query.where(AdminAuditLog.action == action) + count_query = count_query.where(AdminAuditLog.action == action) + total = int(db.scalar(count_query) or 0) + rows = db.scalars(query.order_by(AdminAuditLog.created_at.desc()).offset((page - 1) * limit).limit(limit)).all() + return ApiResponse(data=_page([AdminAuditLogOut.model_validate(row) for row in rows], page, limit, total)) + + +def _count(db: DbSession, model: object) -> int: + return int(db.scalar(select(func.count()).select_from(model)) or 0) + + +def _page(items: list, page: int, limit: int, total: int) -> AdminListResponse: + return AdminListResponse(items=items, pagination=AdminPage(page=page, limit=limit, total=total)) + + +def _role_from_value(value: str) -> UserRole: + return { + "user": UserRole.USER, + "admin": UserRole.ADMIN, + "super_admin": UserRole.SUPER_ADMIN, + }[value] + + +def _ensure_can_operate_user(admin: User, target: User, *, desired_active: bool | None = None) -> None: + if target.role == UserRole.SUPER_ADMIN and admin.role != UserRole.SUPER_ADMIN: + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Cannot operate super admin") + if target.id == admin.id and desired_active is False: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Cannot disable yourself") + + +def _super_admin_count(db: DbSession) -> int: + return int(db.scalar(select(func.count()).select_from(User).where(User.role == UserRole.SUPER_ADMIN)) or 0) + + +def _list_files( + db: DbSession, + *, + page: int, + limit: int, + user_id: str | None = None, + mime_type: str | None = None, +) -> ApiResponse[AdminListResponse[AdminFileOut]]: + query = select(UserFile, FileBlob).join(FileBlob, UserFile.file_sha256 == FileBlob.sha256) + count_query = select(func.count()).select_from(UserFile).join(FileBlob, UserFile.file_sha256 == FileBlob.sha256) + filters = [] + if user_id: + filters.append(UserFile.user_id == user_id) + if mime_type: + filters.append(FileBlob.mime_type == mime_type) + for item in filters: + query = query.where(item) + count_query = count_query.where(item) + total = int(db.scalar(count_query) or 0) + rows = db.execute( + query.order_by(UserFile.uploaded_at.desc()) + .offset((page - 1) * limit) + .limit(limit) + ).all() + items = [ + AdminFileOut( + id=user_file.id, + user_id=user_file.user_id, + original_name=Path(user_file.original_name).name, + sha256_prefix=blob.sha256[:12], + file_size=blob.file_size, + mime_type=blob.mime_type, + uploaded_at=user_file.uploaded_at, + ) + for user_file, blob in rows + ] + return ApiResponse(data=_page(items, page, limit, total)) + + +def _ocr_out(row: ArtworkOcrText) -> AdminOcrTaskOut: + return AdminOcrTaskOut( + id=row.id, + user_id=row.user_id, + artwork_id=row.artwork_id, + status=row.status, + language=row.language, + confidence=row.confidence, + engine=row.engine, + attempts=row.attempts, + error=row.error, + text_length=len(row.ocr_text or ""), + updated_at=row.updated_at, + ) + + +def _push_device_out(row: PushDeviceToken) -> AdminPushDeviceOut: + return AdminPushDeviceOut( + id=row.id, + user_id=row.user_id, + device_id=row.device_id, + platform=row.platform, + token_tail=row.fcm_token[-8:] if row.fcm_token else "", + is_active=row.is_active, + last_seen_at=row.last_seen_at, + updated_at=row.updated_at, + ) + + +def _check_login_rate_limit(request: Request) -> None: + key = request.client.host if request.client is not None else "unknown" + now = time.time() + attempts = _login_attempts[key] + while attempts and now - attempts[0] > 60: + attempts.popleft() + if len(attempts) >= settings.admin_login_rate_limit_per_minute: + raise HTTPException(status_code=status.HTTP_429_TOO_MANY_REQUESTS, detail="Too many admin login attempts") + attempts.append(now) + + +def _redact_url(value: str) -> str: + if "@" not in value: + return value + prefix, suffix = value.rsplit("@", 1) + scheme = prefix.split(":", 1)[0] + return f"{scheme}:***@{suffix}" + + +def _redact_log_line(line: str) -> str: + text_value = line + for marker in ("Bearer ", "token=", "password=", "cookie=", "secret="): + index = text_value.lower().find(marker.lower()) + if index >= 0: + text_value = text_value[: index + len(marker)] + "***" + return text_value + + +def _read_filtered_log_lines(keywords: tuple[str, ...], *, limit: int) -> list[AdminLogLineOut]: + root = Path(settings.log_dir).resolve() + if not root.is_dir(): + return [] + candidates = sorted(root.glob("*.log"), key=lambda item: item.stat().st_mtime if item.exists() else 0, reverse=True) + matched: list[str] = [] + lowered_keywords = tuple(item.lower() for item in keywords) + for path in candidates[:5]: + try: + lines = path.read_text(encoding="utf-8", errors="replace").splitlines() + except OSError: + continue + for line in reversed(lines): + lowered = line.lower() + if any(keyword in lowered for keyword in lowered_keywords): + matched.append(_redact_log_line(line)) + if len(matched) >= limit: + break + if len(matched) >= limit: + break + matched.reverse() + return [AdminLogLineOut(line_no=index + 1, text=line) for index, line in enumerate(matched)] diff --git a/sakura_cloud/api/v1/endpoints/auth.py b/sakura_cloud/api/v1/endpoints/auth.py new file mode 100644 index 0000000..58cd588 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/auth.py @@ -0,0 +1,120 @@ +from __future__ import annotations + +from time import monotonic + +from fastapi import APIRouter, HTTPException, Request, status +from sqlalchemy import func, or_, select +from sqlalchemy.exc import IntegrityError, SQLAlchemyError + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.core.security import ( + create_access_token, + create_refresh_token, + decode_token, + hash_password, + verify_password, +) +from sakura_cloud.models.user import User +from sakura_cloud.schemas.auth import LoginRequest, RefreshRequest, RegisterRequest, TokenPair +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.user import UserOut + + +router = APIRouter() +_rate_buckets: dict[str, list[float]] = {} + + +def _rate_limit(request: Request, scope: str, *, limit: int = 5, window_seconds: int = 60) -> None: + client_ip = request.client.host if request.client else "unknown" + key = f"{scope}:{client_ip}" + now = monotonic() + bucket = [item for item in _rate_buckets.get(key, []) if now - item < window_seconds] + if len(bucket) >= limit: + _rate_buckets[key] = bucket + raise HTTPException(status_code=status.HTTP_429_TOO_MANY_REQUESTS, detail="Too many requests") + bucket.append(now) + _rate_buckets[key] = bucket + + +@router.post("/register", response_model=ApiResponse[UserOut], status_code=status.HTTP_201_CREATED) +def register(payload: RegisterRequest, request: Request, db: DbSession) -> ApiResponse[UserOut]: + _rate_limit(request, "register") + username = payload.username.strip() + email = str(payload.email).strip().lower() + exists = db.scalar( + select(User).where( + or_( + func.lower(User.username) == username.lower(), + func.lower(User.email) == email, + ) + ) + ) + if exists is not None: + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="注册失败,请检查输入") + user = User( + username=username, + email=email, + password_hash=hash_password(payload.password), + ) + try: + db.add(user) + db.commit() + db.refresh(user) + except IntegrityError as exc: + db.rollback() + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="注册失败,请检查输入") from exc + except SQLAlchemyError as exc: + db.rollback() + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="注册失败,请稍后重试") from exc + return ApiResponse(code=201, message="created", data=UserOut.model_validate(user)) + + +@router.post("/login", response_model=ApiResponse[TokenPair]) +def login(payload: LoginRequest, request: Request, db: DbSession) -> ApiResponse[TokenPair]: + _rate_limit(request, "login") + login_id = payload.username_or_email.strip() + user = db.scalar( + select(User).where( + or_( + func.lower(User.username) == login_id.lower(), + func.lower(User.email) == login_id.lower(), + ) + ) + ) + if user is None or not verify_password(payload.password, user.password_hash): + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials") + tokens = TokenPair( + access_token=create_access_token(user.id, user.token_version), + refresh_token=create_refresh_token(user.id, user.token_version), + ) + return ApiResponse(data=tokens) + + +@router.post("/logout", response_model=ApiResponse[dict[str, bool]]) +def logout(current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + # 临时方案:只让客户端清本地 token,不在服务端吊销所有端 + return ApiResponse(data={"ok": True}) + + +@router.post("/refresh", response_model=ApiResponse[TokenPair]) +def refresh(payload: RefreshRequest, request: Request, db: DbSession) -> ApiResponse[TokenPair]: + _rate_limit(request, "refresh") + try: + token_payload = decode_token(payload.refresh_token) + except ValueError as exc: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token") from exc + if token_payload.get("type") != "refresh": + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token type") + user_id = token_payload.get("sub") + user = db.get(User, str(user_id)) if user_id else None + if user is None: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found") + if not user.is_active: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User is disabled") + if int(token_payload.get("tv") or 0) != int(user.token_version or 0): + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Token has been revoked") + tokens = TokenPair( + access_token=create_access_token(user.id, user.token_version), + refresh_token=create_refresh_token(user.id, user.token_version), + ) + return ApiResponse(data=tokens) diff --git a/sakura_cloud/api/v1/endpoints/devices.py b/sakura_cloud/api/v1/endpoints/devices.py new file mode 100644 index 0000000..858d120 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/devices.py @@ -0,0 +1,78 @@ +from __future__ import annotations + +from fastapi import APIRouter, HTTPException, status +from sqlalchemy import select + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.device import Device +from sakura_cloud.models.mixins import utc_now +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.device import DeviceOut, DeviceRegister, DeviceUpdate + + +router = APIRouter() + + +@router.post("/register", response_model=ApiResponse[DeviceOut], status_code=status.HTTP_201_CREATED) +def register_device(payload: DeviceRegister, current_user: CurrentUser, db: DbSession) -> ApiResponse[DeviceOut]: + device = Device( + user_id=current_user.id, + device_name=payload.device_name, + device_type=payload.device_type, + device_token=payload.device_token, + last_active_at=utc_now(), + ) + db.add(device) + db.commit() + db.refresh(device) + return ApiResponse(code=201, message="created", data=DeviceOut.model_validate(device)) + + +@router.get("", response_model=ApiResponse[list[DeviceOut]]) +def list_devices(current_user: CurrentUser, db: DbSession) -> ApiResponse[list[DeviceOut]]: + rows = db.scalars(select(Device).where(Device.user_id == current_user.id).order_by(Device.updated_at.desc())).all() + return ApiResponse(data=[DeviceOut.model_validate(row) for row in rows]) + + +@router.get("/sync/status", response_model=ApiResponse[dict[str, str | int | None]]) +def sync_status(current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, str | int | None]]: + latest = db.scalar( + select(Device).where(Device.user_id == current_user.id).order_by(Device.last_sync_at.desc().nulls_last()) + ) + return ApiResponse( + data={ + "user_id": current_user.id, + "storage_used": current_user.storage_used, + "last_sync_at": latest.last_sync_at.isoformat() if latest and latest.last_sync_at else None, + } + ) + + +@router.put("/{device_id}", response_model=ApiResponse[DeviceOut]) +def update_device( + device_id: str, + payload: DeviceUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[DeviceOut]: + device = db.get(Device, device_id) + if device is None or device.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Device not found") + if payload.device_name is not None: + device.device_name = payload.device_name + if payload.device_token is not None: + device.device_token = payload.device_token + device.last_active_at = utc_now() + db.commit() + db.refresh(device) + return ApiResponse(data=DeviceOut.model_validate(device)) + + +@router.delete("/{device_id}", response_model=ApiResponse[dict[str, bool]]) +def delete_device(device_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + device = db.get(Device, device_id) + if device is None or device.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Device not found") + db.delete(device) + db.commit() + return ApiResponse(data={"ok": True}) diff --git a/sakura_cloud/api/v1/endpoints/files.py b/sakura_cloud/api/v1/endpoints/files.py new file mode 100644 index 0000000..494e473 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/files.py @@ -0,0 +1,50 @@ +from __future__ import annotations + +from pathlib import Path + +from fastapi import APIRouter, HTTPException, status +from fastapi.responses import FileResponse + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.file import FileBlob, UserFile +from sakura_cloud.services.thumbnails import generate_webp_thumbnail + + +router = APIRouter() + + +def _owned_user_file(file_id: str, current_user: CurrentUser, db: DbSession) -> tuple[UserFile, FileBlob]: + user_file = db.get(UserFile, file_id) + if user_file is None or user_file.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found") + blob = db.get(FileBlob, user_file.file_sha256) + if blob is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Blob not found") + return user_file, blob + + +@router.get("/{file_id}/content") +def file_content(file_id: str, current_user: CurrentUser, db: DbSession) -> FileResponse: + user_file, blob = _owned_user_file(file_id, current_user, db) + path = Path(blob.storage_path) + if not path.is_file(): + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File missing from storage") + return FileResponse(path, media_type=blob.mime_type, filename=user_file.original_name) + + +@router.get("/{file_id}/thumbnail") +def file_thumbnail(file_id: str, current_user: CurrentUser, db: DbSession) -> FileResponse: + _, blob = _owned_user_file(file_id, current_user, db) + if not blob.thumbnail_path: + blob.thumbnail_path = generate_webp_thumbnail(blob.storage_path) + db.commit() + db.refresh(blob) + path = Path(blob.thumbnail_path or blob.storage_path) + if not path.is_file(): + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Thumbnail missing from storage") + media_type = "image/webp" if path.suffix.lower() == ".webp" else blob.mime_type + return FileResponse( + path, + media_type=media_type, + headers={"Cache-Control": "private, max-age=604800"}, + ) diff --git a/sakura_cloud/api/v1/endpoints/follows.py b/sakura_cloud/api/v1/endpoints/follows.py new file mode 100644 index 0000000..4ba042a --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/follows.py @@ -0,0 +1,208 @@ +from __future__ import annotations + +from fastapi import APIRouter, HTTPException, Query, status +from sqlalchemy import select + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.follow import CreatorFollow, UpdateAlert +from sakura_cloud.models.mixins import utc_now +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.follow import ( + CreatorFollowBulkUpsert, + CreatorFollowBulkUpsertResult, + CreatorFollowOut, + CreatorFollowUpsert, + UpdateAlertCreate, + UpdateAlertOut, + UpdateAlertReadUpdate, +) +from sakura_cloud.services.push import send_update_alert_push + + +router = APIRouter() + + +@router.get("", response_model=ApiResponse[list[CreatorFollowOut]]) +def list_follows( + current_user: CurrentUser, + db: DbSession, + platform: str | None = Query(default=None), + enabled: bool | None = Query(default=None), +) -> ApiResponse[list[CreatorFollowOut]]: + query = select(CreatorFollow).where(CreatorFollow.user_id == current_user.id) + if platform: + query = query.where(CreatorFollow.platform == platform) + if enabled is not None: + query = query.where(CreatorFollow.enabled == enabled) + rows = db.scalars(query.order_by(CreatorFollow.updated_at.desc())).all() + return ApiResponse(data=[CreatorFollowOut.from_row(row) for row in rows]) + + +@router.post("", response_model=ApiResponse[CreatorFollowOut], status_code=status.HTTP_201_CREATED) +def upsert_follow( + payload: CreatorFollowUpsert, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[CreatorFollowOut]: + row, _created = _upsert_follow_row(payload, current_user.id, db) + db.commit() + db.refresh(row) + return ApiResponse(code=201, message="created", data=CreatorFollowOut.from_row(row)) + + +@router.post("/bulk-upsert", response_model=ApiResponse[CreatorFollowBulkUpsertResult]) +def bulk_upsert_follows( + payload: CreatorFollowBulkUpsert, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[CreatorFollowBulkUpsertResult]: + created = 0 + updated = 0 + skipped = 0 + rows: list[CreatorFollow] = [] + seen: set[tuple[str, str]] = set() + for item in payload.items: + key = (item.platform.strip(), item.creator_id.strip()) + if not key[0] or not key[1] or key in seen: + skipped += 1 + continue + seen.add(key) + row, was_created = _upsert_follow_row(item, current_user.id, db) + rows.append(row) + if was_created: + created += 1 + else: + updated += 1 + db.commit() + for row in rows: + db.refresh(row) + return ApiResponse( + data=CreatorFollowBulkUpsertResult( + created=created, + updated=updated, + skipped=skipped, + items=[CreatorFollowOut.from_row(row) for row in rows], + ) + ) + + +def _upsert_follow_row( + payload: CreatorFollowUpsert, + user_id: str, + db: DbSession, +) -> tuple[CreatorFollow, bool]: + row = db.scalar( + select(CreatorFollow).where( + CreatorFollow.user_id == user_id, + CreatorFollow.platform == payload.platform, + CreatorFollow.creator_id == payload.creator_id, + ) + ) + created = row is None + if row is None: + row = CreatorFollow( + user_id=user_id, + platform=payload.platform, + creator_id=payload.creator_id, + ) + db.add(row) + row.creator_name = payload.creator_name + row.creator_handle = payload.creator_handle + row.profile_url = payload.profile_url + row.avatar_url = payload.avatar_url + row.enabled = payload.enabled + row.latest_published_at = payload.latest_published_at + row.metadata_json = payload.metadata + row.last_checked_at = payload.last_checked_at or utc_now() + return row, created + + +@router.delete("/{follow_id}", response_model=ApiResponse[dict[str, bool]]) +def disable_follow(follow_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + row = db.get(CreatorFollow, follow_id) + if row is None or row.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Follow not found") + row.enabled = False + row.last_checked_at = utc_now() + db.commit() + return ApiResponse(data={"ok": True}) + + +@router.get("/alerts", response_model=ApiResponse[list[UpdateAlertOut]]) +def list_alerts( + current_user: CurrentUser, + db: DbSession, + unread_only: bool = Query(default=False), + limit: int = Query(default=50, ge=1, le=200), +) -> ApiResponse[list[UpdateAlertOut]]: + query = select(UpdateAlert).where(UpdateAlert.user_id == current_user.id) + if unread_only: + query = query.where(UpdateAlert.is_read == False) + rows = db.scalars(query.order_by(UpdateAlert.created_at.desc()).limit(limit)).all() + return ApiResponse(data=[UpdateAlertOut.model_validate(row) for row in rows]) + + +@router.post("/alerts", response_model=ApiResponse[UpdateAlertOut], status_code=status.HTTP_201_CREATED) +def create_alert( + payload: UpdateAlertCreate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[UpdateAlertOut]: + follow = db.scalar( + select(CreatorFollow).where( + CreatorFollow.user_id == current_user.id, + CreatorFollow.platform == payload.platform, + CreatorFollow.creator_id == payload.creator_id, + ) + ) + existing = db.scalar( + select(UpdateAlert).where( + UpdateAlert.user_id == current_user.id, + UpdateAlert.platform == payload.platform, + UpdateAlert.creator_id == payload.creator_id, + UpdateAlert.title == payload.title, + UpdateAlert.target_url == payload.target_url, + ) + ) + if existing is not None: + if existing.follow_id is None and follow is not None: + existing.follow_id = follow.id + db.commit() + db.refresh(existing) + return ApiResponse(code=200, message="exists", data=UpdateAlertOut.model_validate(existing)) + row = UpdateAlert( + user_id=current_user.id, + follow_id=follow.id if follow else None, + platform=payload.platform, + creator_id=payload.creator_id, + title=payload.title, + body=payload.body, + content_type=payload.content_type, + target_url=payload.target_url, + cover_url=payload.cover_url, + payload_json=payload.payload, + ) + if payload.created_at is not None: + row.created_at = payload.created_at + row.updated_at = payload.created_at + db.add(row) + db.commit() + db.refresh(row) + send_update_alert_push(db, row) + return ApiResponse(code=201, message="created", data=UpdateAlertOut.model_validate(row)) + + +@router.post("/alerts/{alert_id}/read", response_model=ApiResponse[UpdateAlertOut]) +def mark_alert_read( + alert_id: str, + payload: UpdateAlertReadUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[UpdateAlertOut]: + row = db.get(UpdateAlert, alert_id) + if row is None or row.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Alert not found") + row.is_read = payload.is_read + db.commit() + db.refresh(row) + return ApiResponse(data=UpdateAlertOut.model_validate(row)) diff --git a/sakura_cloud/api/v1/endpoints/gallery.py b/sakura_cloud/api/v1/endpoints/gallery.py new file mode 100644 index 0000000..345f583 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/gallery.py @@ -0,0 +1,520 @@ +from __future__ import annotations + +import math +import hashlib +from datetime import timedelta + +from fastapi import APIRouter, BackgroundTasks, HTTPException, Query, status +from pydantic import BaseModel, Field +from sqlalchemy import String, delete, func, or_, select + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.artwork import Artwork +from sakura_cloud.models.feed_cache import FeedCache +from sakura_cloud.models.mixins import utc_now +from sakura_cloud.models.ocr import ArtworkOcrText +from sakura_cloud.schemas.common import ApiResponse, PageResponse, Pagination +from sakura_cloud.schemas.gallery import ArtworkCreate, ArtworkOut, ArtworkUpdate, ArtworkViewUpdate, TagsUpdate +from sakura_cloud.schemas.ocr import ArtworkOcrTextOut +from sakura_cloud.services.ocr import queue_ocr_for_artwork, run_ocr_for_artwork, run_ocr_for_artwork_id + + +router = APIRouter() + + +class ArtworkBatchDeleteRequest(BaseModel): + ids: list[str] = Field(default_factory=list, min_length=1, max_length=200) + + +class OnlineMangaImportRequest(BaseModel): + source_platform: str = Field(max_length=20) + title: str = Field(min_length=1, max_length=500) + author: str | None = Field(default=None, max_length=200) + detail_url: str = Field(min_length=1, max_length=2048) + cover_url: str | None = Field(default=None, max_length=2048) + page_urls: list[str] = Field(min_length=1, max_length=300) + headers: dict[str, str] = Field(default_factory=dict) + tags: list[str] = Field(default_factory=list, max_length=50) + + +def _ensure_storage_quota(current_user: CurrentUser, incoming_size: int) -> None: + size = max(0, incoming_size) + if size and current_user.storage_used + size > current_user.storage_limit: + raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="Storage quota exceeded") + + +def _online_source_id(value: str) -> str: + return hashlib.sha256(value.encode("utf-8")).hexdigest()[:32] + + +@router.get("", response_model=ApiResponse[PageResponse[ArtworkOut]]) +def list_gallery( + current_user: CurrentUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + source_platform: str | None = None, + author: str | None = None, + tag: str | None = None, + content_type: str | None = None, + is_favorite: bool | None = None, + search: str | None = None, + query: str | None = None, + search_scope: str = Query(default="all", pattern="^(title|tags|ocr|all)$"), + sort: str = Query(default="recent", pattern="^(recent|updated)$"), + include_total: bool = Query(default=True), +) -> ApiResponse[PageResponse[ArtworkOut]]: + search_text = (query or search or "").strip() + query = select(Artwork).where(Artwork.user_id == current_user.id) + count_query = select(func.count()).select_from(Artwork).where(Artwork.user_id == current_user.id) + filters = [] + if source_platform: + filters.append(Artwork.source_platform == source_platform) + if author: + filters.append(Artwork.author.ilike(f"%{author}%")) + if content_type: + filters.append(Artwork.content_type == content_type) + if is_favorite is not None: + filters.append(Artwork.is_favorite == is_favorite) + if search_text: + text_filters = [] + if search_scope in {"title", "all"}: + text_filters.append(Artwork.title.ilike(f"%{search_text}%")) + text_filters.append(Artwork.author.ilike(f"%{search_text}%")) + if db.bind is not None and db.bind.dialect.name == "postgresql": + vector = func.to_tsvector( + "simple", + func.concat_ws(" ", Artwork.title, Artwork.author, Artwork.source_platform), + ) + text_filters.append(vector.op("@@")(func.plainto_tsquery("simple", search_text))) + if search_scope in {"tags", "all"}: + text_filters.append(Artwork.tags.cast(String).ilike(f"%{search_text}%")) + if search_scope in {"ocr", "all"}: + ocr_match = ( + select(ArtworkOcrText.id).where( + ArtworkOcrText.user_id == current_user.id, + ArtworkOcrText.artwork_id == Artwork.id, + ArtworkOcrText.status == "done", + ArtworkOcrText.ocr_text.ilike(f"%{search_text}%"), + ) + ).exists() + if db.bind is not None and db.bind.dialect.name == "postgresql": + ocr_match = ( + select(ArtworkOcrText.id).where( + ArtworkOcrText.user_id == current_user.id, + ArtworkOcrText.artwork_id == Artwork.id, + ArtworkOcrText.status == "done", + func.to_tsvector("simple", ArtworkOcrText.ocr_text).op("@@")( + func.plainto_tsquery("simple", search_text) + ), + ) + ).exists() + text_filters.append(ocr_match) + if text_filters: + filters.append(or_(*text_filters)) + elif db.bind is not None and db.bind.dialect.name == "postgresql": + vector = func.to_tsvector( + "simple", + func.concat_ws(" ", Artwork.title, Artwork.author, Artwork.source_platform), + ) + filters.append(vector.op("@@")(func.plainto_tsquery("simple", search_text))) + if tag: + filters.append(Artwork.tags.contains([tag])) + for item in filters: + query = query.where(item) + count_query = count_query.where(item) + order_by = ( + (Artwork.updated_at.desc(), Artwork.created_at.desc()) + if sort == "updated" + else (Artwork.last_viewed_at.desc().nullslast(), Artwork.updated_at.desc()) + ) + rows = db.scalars(query.order_by(*order_by).offset((page - 1) * limit).limit(limit)).all() + if include_total: + total = int(db.scalar(count_query) or 0) + pages = math.ceil(total / limit) if total else 0 + else: + has_more = len(rows) >= limit + total = (page - 1) * limit + len(rows) + (1 if has_more else 0) + pages = page + 1 if has_more else page + if search_text: + _annotate_gallery_matches(db, rows, current_user.id, search_text, search_scope) + data = PageResponse( + items=[ArtworkOut.model_validate(row) for row in rows], + pagination=Pagination(page=page, limit=limit, total=total, pages=pages), + ) + return ApiResponse(data=data) + + +@router.get("/feed", response_model=ApiResponse[list[ArtworkOut]]) +def gallery_feed( + current_user: CurrentUser, + db: DbSession, + limit: int = Query(default=30, ge=1, le=100), + mode: str = Query(default="created", pattern="^(created|recent)$"), +) -> ApiResponse[list[ArtworkOut]]: + cache_key = f"gallery_feed:{mode}:{limit}" + now = utc_now() + cached = db.scalar( + select(FeedCache).where( + FeedCache.user_id == current_user.id, + FeedCache.cache_key == cache_key, + FeedCache.expires_at > now, + ) + ) + if cached is not None: + return ApiResponse(data=[ArtworkOut.model_validate(item) for item in cached.payload]) + + order_by = Artwork.last_viewed_at.desc() if mode == "recent" else Artwork.created_at.desc() + where = [Artwork.user_id == current_user.id] + if mode == "recent": + where.append(Artwork.last_viewed_at.is_not(None)) + rows = db.scalars( + select(Artwork).where(*where).order_by(order_by).limit(limit) + ).all() + data = [ArtworkOut.model_validate(row) for row in rows] + payload = [item.model_dump(mode="json") for item in data] + existing = db.scalar( + select(FeedCache).where( + FeedCache.user_id == current_user.id, + FeedCache.cache_key == cache_key, + ) + ) + if existing is None: + db.add( + FeedCache( + user_id=current_user.id, + cache_key=cache_key, + payload=payload, + expires_at=now + timedelta(minutes=5), + ) + ) + else: + existing.payload = payload + existing.expires_at = now + timedelta(minutes=5) + db.commit() + return ApiResponse(data=data) + + +@router.get("/search", response_model=ApiResponse[PageResponse[ArtworkOut]]) +def search_gallery( + current_user: CurrentUser, + db: DbSession, + q: str = Query(min_length=1), + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), +) -> ApiResponse[PageResponse[ArtworkOut]]: + return list_gallery(current_user=current_user, db=db, page=page, limit=limit, search=q, search_scope="all") + + +@router.post("/import-online", response_model=ApiResponse[list[ArtworkOut]], status_code=status.HTTP_201_CREATED) +def import_online_manga( + payload: OnlineMangaImportRequest, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[list[ArtworkOut]]: + series_id = _online_source_id(payload.detail_url) + existing = db.scalars( + select(Artwork) + .where( + Artwork.user_id == current_user.id, + Artwork.source_platform == payload.source_platform, + Artwork.source_id.like(f"{series_id}:%"), + Artwork.content_type == "manga", + ) + .order_by(Artwork.title.asc()) + ).all() + if existing: + return ApiResponse(data=[ArtworkOut.model_validate(item) for item in existing]) + + safe_headers = { + key: value + for key, value in payload.headers.items() + if key.lower() in {"user-agent", "referer", "cookie", "accept", "accept-language"} and value + } + rows: list[Artwork] = [] + page_count = len(payload.page_urls) + for index, page_url in enumerate(payload.page_urls, start=1): + url = page_url.strip() + if not url.startswith(("http://", "https://")): + continue + artwork = Artwork( + user_id=current_user.id, + source_id=f"{series_id}:{index:04d}", + source_platform=payload.source_platform, + content_type="manga", + title=f"{payload.title.strip()} #{index:03d}", + author=payload.author, + page_count=page_count, + file_path=url, + file_size=0, + thumbnail_path=payload.cover_url or url, + mime_type="image/jpeg", + tags=payload.tags, + artwork_metadata={ + "online_import": True, + "detail_url": payload.detail_url, + "series_id": series_id, + "page_index": index, + "headers": safe_headers, + }, + ) + db.add(artwork) + rows.append(artwork) + if not rows: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No valid manga page URL") + db.commit() + for row in rows: + db.refresh(row) + _clear_gallery_feed_cache(db, current_user.id) + return ApiResponse(code=201, message="created", data=[ArtworkOut.model_validate(item) for item in rows]) + + +@router.get("/stat", response_model=ApiResponse[dict[str, int]]) +def gallery_stat(current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, int]]: + total = int(db.scalar(select(func.count()).select_from(Artwork).where(Artwork.user_id == current_user.id)) or 0) + favorites = int( + db.scalar( + select(func.count()).select_from(Artwork).where(Artwork.user_id == current_user.id, Artwork.is_favorite == True) + ) + or 0 + ) + return ApiResponse(data={"total": total, "favorites": favorites, "storage_used": current_user.storage_used}) + + +@router.post("", response_model=ApiResponse[ArtworkOut], status_code=status.HTTP_201_CREATED) +def create_artwork( + payload: ArtworkCreate, + current_user: CurrentUser, + db: DbSession, + background_tasks: BackgroundTasks, +) -> ApiResponse[ArtworkOut]: + _ensure_storage_quota(current_user, payload.file_size) + artwork = Artwork( + user_id=current_user.id, + source_id=payload.source_id, + source_platform=payload.source_platform, + content_type=payload.content_type, + title=payload.title, + author=payload.author, + author_id=payload.author_id, + page_count=payload.page_count, + file_path=payload.file_path, + file_size=payload.file_size, + thumbnail_path=payload.thumbnail_path, + mime_type=payload.mime_type, + width=payload.width, + height=payload.height, + tags=payload.tags, + artwork_metadata=payload.metadata, + ) + db.add(artwork) + current_user.storage_used += max(0, payload.file_size) + db.commit() + db.refresh(artwork) + if queue_ocr_for_artwork(db, artwork) is not None: + background_tasks.add_task(run_ocr_for_artwork_id, artwork.id, current_user.id) + _clear_gallery_feed_cache(db, current_user.id) + return ApiResponse(code=201, message="created", data=ArtworkOut.model_validate(artwork)) + + +@router.get("/tags", response_model=ApiResponse[list[str]]) +def list_tags(current_user: CurrentUser, db: DbSession) -> ApiResponse[list[str]]: + rows = db.scalars(select(Artwork.tags).where(Artwork.user_id == current_user.id)).all() + tags = sorted({tag for row in rows for tag in (row or [])}) + return ApiResponse(data=tags) + + +@router.get("/{artwork_id}/ocr", response_model=ApiResponse[ArtworkOcrTextOut | None]) +def get_artwork_ocr(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOcrTextOut | None]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + row = db.scalar( + select(ArtworkOcrText).where( + ArtworkOcrText.user_id == current_user.id, + ArtworkOcrText.artwork_id == artwork_id, + ) + ) + return ApiResponse(data=ArtworkOcrTextOut.model_validate(row) if row is not None else None) + + +@router.post("/{artwork_id}/ocr/retry", response_model=ApiResponse[ArtworkOcrTextOut]) +def retry_artwork_ocr( + artwork_id: str, + current_user: CurrentUser, + db: DbSession, + background_tasks: BackgroundTasks, +) -> ApiResponse[ArtworkOcrTextOut]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + row = queue_ocr_for_artwork(db, artwork) + if row is None: + row = run_ocr_for_artwork(db, artwork) + else: + background_tasks.add_task(run_ocr_for_artwork_id, artwork.id, current_user.id) + db.refresh(row) + return ApiResponse(data=ArtworkOcrTextOut.model_validate(row)) + + +@router.get("/{artwork_id}", response_model=ApiResponse[ArtworkOut]) +def get_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOut]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + return ApiResponse(data=ArtworkOut.model_validate(artwork)) + + +def _annotate_gallery_matches( + db: DbSession, + rows: list[Artwork], + user_id: str, + search_text: str, + search_scope: str, +) -> None: + lowered = search_text.lower() + artwork_ids = [row.id for row in rows] + ocr_hits: set[str] = set() + if artwork_ids and search_scope in {"ocr", "all"}: + ocr_hits = set( + db.scalars( + select(ArtworkOcrText.artwork_id).where( + ArtworkOcrText.user_id == user_id, + ArtworkOcrText.artwork_id.in_(artwork_ids), + ArtworkOcrText.status == "done", + ArtworkOcrText.ocr_text.ilike(f"%{search_text}%"), + ) + ).all() + ) + for row in rows: + title_hit = lowered in (row.title or "").lower() or lowered in (row.author or "").lower() + tag_hit = any(lowered in str(tag).lower() for tag in (row.tags or [])) + if row.id in ocr_hits: + row.matched_by = "ocr" + elif title_hit: + row.matched_by = "title" + elif tag_hit: + row.matched_by = "tags" + else: + row.matched_by = None + + +def _clear_gallery_feed_cache(db: DbSession, user_id: str) -> None: + db.execute( + delete(FeedCache).where( + FeedCache.user_id == user_id, + FeedCache.cache_key.like("gallery_feed:%"), + ) + ) + db.commit() + + +@router.delete("/{artwork_id}", response_model=ApiResponse[dict[str, bool]]) +def delete_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + current_user.storage_used = max(0, current_user.storage_used - max(0, artwork.file_size)) + db.delete(artwork) + db.commit() + return ApiResponse(data={"ok": True}) + + +@router.post("/batch-delete", response_model=ApiResponse[dict[str, int]]) +def batch_delete_artworks( + payload: ArtworkBatchDeleteRequest, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[dict[str, int]]: + ids = sorted({item.strip() for item in payload.ids if item.strip()}) + if not ids: + return ApiResponse(data={"deleted": 0}) + + rows = db.scalars( + select(Artwork).where( + Artwork.user_id == current_user.id, + Artwork.id.in_(ids), + ) + ).all() + deleted = 0 + released = 0 + for artwork in rows: + released += max(0, artwork.file_size) + db.delete(artwork) + deleted += 1 + if deleted: + current_user.storage_used = max(0, current_user.storage_used - released) + db.commit() + return ApiResponse(data={"deleted": deleted}) + + +@router.post("/{artwork_id}/favorite", response_model=ApiResponse[ArtworkOut]) +def favorite_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOut]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + artwork.is_favorite = True + db.commit() + db.refresh(artwork) + return ApiResponse(data=ArtworkOut.model_validate(artwork)) + + +@router.delete("/{artwork_id}/favorite", response_model=ApiResponse[ArtworkOut]) +def unfavorite_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOut]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + artwork.is_favorite = False + db.commit() + db.refresh(artwork) + return ApiResponse(data=ArtworkOut.model_validate(artwork)) + + +@router.put("/{artwork_id}/tags", response_model=ApiResponse[ArtworkOut]) +def update_artwork_tags( + artwork_id: str, + payload: TagsUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[ArtworkOut]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + artwork.tags = sorted(set(payload.tags)) + db.commit() + db.refresh(artwork) + return ApiResponse(data=ArtworkOut.model_validate(artwork)) + + +@router.put("/{artwork_id}", response_model=ApiResponse[ArtworkOut]) +def update_artwork( + artwork_id: str, + payload: ArtworkUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[ArtworkOut]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + artwork.title = payload.title.strip() + artwork.tags = sorted({item.strip() for item in payload.tags if item.strip()}) + db.commit() + db.refresh(artwork) + return ApiResponse(data=ArtworkOut.model_validate(artwork)) + + +@router.post("/{artwork_id}/view", response_model=ApiResponse[ArtworkOut]) +def mark_artwork_viewed( + artwork_id: str, + payload: ArtworkViewUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[ArtworkOut]: + artwork = db.get(Artwork, artwork_id) + if artwork is None or artwork.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found") + if payload.viewed: + artwork.last_viewed_at = utc_now() + db.commit() + db.refresh(artwork) + return ApiResponse(data=ArtworkOut.model_validate(artwork)) diff --git a/sakura_cloud/api/v1/endpoints/health.py b/sakura_cloud/api/v1/endpoints/health.py new file mode 100644 index 0000000..f04110d --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/health.py @@ -0,0 +1,13 @@ +from __future__ import annotations + +from fastapi import APIRouter + +from sakura_cloud.schemas.common import ApiResponse + + +router = APIRouter() + + +@router.get("/health", response_model=ApiResponse[dict[str, str]]) +def health_check() -> ApiResponse[dict[str, str]]: + return ApiResponse(data={"status": "ok", "service": "sakura-cloud"}) diff --git a/sakura_cloud/api/v1/endpoints/me.py b/sakura_cloud/api/v1/endpoints/me.py new file mode 100644 index 0000000..da0f53e --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/me.py @@ -0,0 +1,82 @@ +from __future__ import annotations + +from fastapi import APIRouter, Query + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.api.v1.endpoints import follows, gallery, novels, push, users +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.user import PasswordChangeRequest, UserOut, UserUpdate + + +router = APIRouter() + + +@router.get("", response_model=ApiResponse[UserOut]) +def me(current_user: CurrentUser) -> ApiResponse[UserOut]: + return users.get_me(current_user) + + +@router.patch("/profile", response_model=ApiResponse[UserOut]) +def update_profile(payload: UserUpdate, current_user: CurrentUser, db: DbSession) -> ApiResponse[UserOut]: + return users.update_me(payload, current_user, db) + + +@router.patch("/password", response_model=ApiResponse[dict[str, bool]]) +def update_password(payload: PasswordChangeRequest, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + return users.change_password(payload, current_user, db) + + +@router.get("/gallery") +def my_gallery( + current_user: CurrentUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + search: str | None = None, + query: str | None = None, + search_scope: str = Query(default="all", pattern="^(title|tags|ocr|all)$"), + sort: str = Query(default="recent", pattern="^(recent|updated)$"), + is_favorite: bool | None = None, + content_type: str | None = None, + include_total: bool = Query(default=True), +): + return gallery.list_gallery( + current_user=current_user, + db=db, + page=page, + limit=limit, + search=search, + query=query, + search_scope=search_scope, + sort=sort, + is_favorite=is_favorite, + content_type=content_type, + include_total=include_total, + ) + + +@router.get("/novels") +def my_novels( + current_user: CurrentUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + search: str | None = None, + is_favorite: bool | None = None, +): + return novels.list_novels(current_user=current_user, db=db, page=page, limit=limit, search=search, is_favorite=is_favorite) + + +@router.get("/alerts") +def my_alerts( + current_user: CurrentUser, + db: DbSession, + unread_only: bool = Query(default=False), + limit: int = Query(default=50, ge=1, le=200), +): + return follows.list_alerts(current_user=current_user, db=db, unread_only=unread_only, limit=limit) + + +@router.get("/devices") +def my_push_devices(current_user: CurrentUser, db: DbSession): + return push.list_devices(current_user=current_user, db=db) diff --git a/sakura_cloud/api/v1/endpoints/novels.py b/sakura_cloud/api/v1/endpoints/novels.py new file mode 100644 index 0000000..af45ae8 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/novels.py @@ -0,0 +1,461 @@ +from __future__ import annotations + +import math +import hashlib +from pathlib import Path +from datetime import datetime, timezone + +from fastapi import APIRouter, HTTPException, Query, status +from fastapi.responses import FileResponse +from pydantic import BaseModel, Field, model_validator +from sqlalchemy import func, select + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.core.config import settings +from sakura_cloud.models.file import FileBlob, UserFile +from sakura_cloud.models.mixins import utc_now +from sakura_cloud.models.novel import Novel +from sakura_cloud.schemas.common import ApiResponse, PageResponse, Pagination +from sakura_cloud.schemas.novel import ( + BookmarkUpdate, + NovelOut, + NovelSeriesGroup, + NovelViewUpdate, + ReadingProgressUpdate, +) + + +router = APIRouter() + + +class NovelBatchDeleteRequest(BaseModel): + ids: list[str] = Field(default_factory=list, max_length=200) + series_ids: list[str] = Field(default_factory=list, max_length=50) + + @model_validator(mode="after") + def normalize_targets(self) -> "NovelBatchDeleteRequest": + self.ids = [item.strip() for item in self.ids if item and item.strip()] + self.series_ids = [item.strip() for item in self.series_ids if item and item.strip()] + return self + + +class OnlineNovelImportRequest(BaseModel): + source_platform: str = Field(max_length=20) + title: str = Field(min_length=1, max_length=500) + author: str | None = Field(default=None, max_length=200) + detail_url: str = Field(min_length=1, max_length=2048) + cover_url: str | None = Field(default=None, max_length=2048) + content: str = Field(min_length=1, max_length=2_000_000) + tags: list[str] = Field(default_factory=list, max_length=50) + + +def _latest_non_null(values: list[datetime | None]) -> datetime | None: + normalized = [value for value in values if value is not None] + return max(normalized) if normalized else None + + +def _series_group_from_items(series_id: str, items: list[Novel], *, include_chapters: bool = True) -> NovelSeriesGroup: + sorted_items = sorted(items, key=lambda item: (item.chapter_order, item.updated_at)) + first = sorted_items[0] + return NovelSeriesGroup( + series_id=series_id, + series_title=first.series_title or first.title, + author=first.author, + chapter_count=len(sorted_items), + cover_path=first.cover_path, + last_read_at=_latest_non_null([item.last_read_at for item in sorted_items]), + last_viewed_at=_latest_non_null([item.last_viewed_at for item in sorted_items]), + chapters=[NovelOut.model_validate(item) for item in sorted_items] if include_chapters else [], + ) + + +def _resolve_novel_storage_path(novel: Novel, db: DbSession) -> Path: + path = Path(novel.file_path or "") + if path.is_file(): + return path + + if novel.file_id: + user_file = db.get(UserFile, novel.file_id) + if user_file is not None: + blob = db.get(FileBlob, user_file.file_sha256) + if blob is not None: + blob_path = Path(blob.storage_path) + if blob_path.is_file(): + novel.file_path = blob.storage_path + novel.file_size = blob.file_size + if not novel.cover_path: + novel.cover_path = blob.thumbnail_path + db.commit() + db.refresh(novel) + return blob_path + + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel file missing from storage") + + +def _online_source_id(detail_url: str) -> str: + return hashlib.sha256(detail_url.encode("utf-8")).hexdigest()[:32] + + +def _write_online_novel_file(user_id: str, source_id: str, content: str) -> tuple[str, str, int]: + data = content.encode("utf-8") + sha256 = hashlib.sha256(data).hexdigest() + path = Path(settings.storage_root) / "online_imports" / "novels" / user_id + path.mkdir(parents=True, exist_ok=True) + target = path / f"{source_id}-{sha256[:12]}.txt" + if not target.exists(): + target.write_bytes(data) + return str(target), sha256, len(data) + + +def _ensure_storage_quota(current_user: CurrentUser, incoming_size: int) -> None: + size = max(0, incoming_size) + if size and current_user.storage_used + size > current_user.storage_limit: + raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="Storage quota exceeded") + + +@router.get("", response_model=ApiResponse[PageResponse[NovelOut]]) +def list_novels( + current_user: CurrentUser, + db: DbSession, + page: int = Query(default=1, ge=1), + limit: int = Query(default=30, ge=1, le=100), + search: str | None = None, + is_favorite: bool | None = None, +) -> ApiResponse[PageResponse[NovelOut]]: + query = select(Novel).where(Novel.user_id == current_user.id) + count_query = select(func.count()).select_from(Novel).where(Novel.user_id == current_user.id) + if search: + if db.bind is not None and db.bind.dialect.name == "postgresql": + vector = func.to_tsvector("simple", func.concat_ws(" ", Novel.title, Novel.author, Novel.description)) + condition = vector.op("@@")(func.plainto_tsquery("simple", search)) + else: + condition = Novel.title.ilike(f"%{search}%") | Novel.author.ilike(f"%{search}%") + query = query.where(condition) + count_query = count_query.where(condition) + if is_favorite is not None: + query = query.where(Novel.is_favorite == is_favorite) + count_query = count_query.where(Novel.is_favorite == is_favorite) + total = int(db.scalar(count_query) or 0) + rows = db.scalars( + query.order_by(Novel.last_viewed_at.desc().nullslast(), Novel.updated_at.desc()) + .offset((page - 1) * limit) + .limit(limit) + ).all() + data = PageResponse( + items=[NovelOut.model_validate(row) for row in rows], + pagination=Pagination(page=page, limit=limit, total=total, pages=math.ceil(total / limit) if total else 0), + ) + return ApiResponse(data=data) + + +@router.get("/continue-reading", response_model=ApiResponse[list[NovelOut]]) +def continue_reading(current_user: CurrentUser, db: DbSession) -> ApiResponse[list[NovelOut]]: + rows = db.scalars( + select(Novel) + .where(Novel.user_id == current_user.id, Novel.last_read_at.is_not(None)) + .order_by(Novel.last_read_at.desc()) + .limit(20) + ).all() + return ApiResponse(data=[NovelOut.model_validate(row) for row in rows]) + + +@router.get("/recent", response_model=ApiResponse[list[NovelOut]]) +def recent_novels( + current_user: CurrentUser, + db: DbSession, + limit: int = Query(default=20, ge=1, le=100), +) -> ApiResponse[list[NovelOut]]: + rows = db.scalars( + select(Novel) + .where(Novel.user_id == current_user.id, Novel.last_viewed_at.is_not(None)) + .order_by(Novel.last_viewed_at.desc()) + .limit(limit) + ).all() + return ApiResponse(data=[NovelOut.model_validate(row) for row in rows]) + + +@router.get("/series", response_model=ApiResponse[list[NovelSeriesGroup]]) +def list_novel_series( + current_user: CurrentUser, + db: DbSession, + include_chapters: bool = Query(default=True), +) -> ApiResponse[list[NovelSeriesGroup]]: + # Mobile shelf uses include_chapters=false to avoid loading every chapter for every series. + # The exact series catalog is fetched later by /series/{series_id}. + if not include_chapters: + rows = db.execute( + select( + Novel.series_id, + func.min(Novel.series_title), + func.min(Novel.author), + func.count(Novel.id), + func.min(Novel.cover_path), + func.max(Novel.last_read_at), + func.max(Novel.last_viewed_at), + func.max(Novel.updated_at), + ) + .where(Novel.user_id == current_user.id, Novel.series_id.is_not(None)) + .group_by(Novel.series_id) + .order_by(func.max(Novel.last_viewed_at).desc().nullslast(), func.max(Novel.updated_at).desc()) + ).all() + return ApiResponse( + data=[ + NovelSeriesGroup( + series_id=str(row[0]), + series_title=str(row[1] or "Untitled series"), + author=row[2], + chapter_count=int(row[3] or 0), + cover_path=row[4], + last_read_at=row[5], + last_viewed_at=row[6], + chapters=[], + ) + for row in rows + if row[0] + ] + ) + + rows = db.scalars( + select(Novel) + .where(Novel.user_id == current_user.id, Novel.series_id.is_not(None)) + .order_by(Novel.series_title.asc(), Novel.chapter_order.asc(), Novel.updated_at.desc()) + ).all() + groups: dict[str, list[Novel]] = {} + for row in rows: + if not row.series_id: + continue + groups.setdefault(row.series_id, []).append(row) + data = [] + for series_id, items in groups.items(): + data.append(_series_group_from_items(series_id, items, include_chapters=include_chapters)) + data.sort(key=lambda item: item.last_viewed_at or item.last_read_at or datetime.min.replace(tzinfo=timezone.utc), reverse=True) + return ApiResponse(data=data) + + +@router.get("/series/{series_id}", response_model=ApiResponse[NovelSeriesGroup]) +def get_novel_series(series_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[NovelSeriesGroup]: + rows = db.scalars( + select(Novel) + .where( + Novel.user_id == current_user.id, + Novel.series_id == series_id, + ) + .order_by(Novel.chapter_order.asc(), Novel.updated_at.desc()) + ).all() + if not rows: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel series not found") + return ApiResponse(data=_series_group_from_items(series_id, rows, include_chapters=True)) + + +@router.post("/import-online", response_model=ApiResponse[NovelOut], status_code=status.HTTP_201_CREATED) +def import_online_novel( + payload: OnlineNovelImportRequest, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[NovelOut]: + source_id = _online_source_id(payload.detail_url) + existing = db.scalar( + select(Novel).where( + Novel.user_id == current_user.id, + Novel.source_platform == payload.source_platform, + Novel.source_id == source_id, + ) + ) + if existing is not None: + return ApiResponse(data=NovelOut.model_validate(existing)) + + storage_path, sha256, file_size = _write_online_novel_file(current_user.id, source_id, payload.content) + blob = db.get(FileBlob, sha256) + if blob is None: + blob = FileBlob( + sha256=sha256, + storage_path=storage_path, + thumbnail_path=payload.cover_url, + file_size=file_size, + mime_type="text/plain", + ) + db.add(blob) + + user_file = db.scalar(select(UserFile).where(UserFile.user_id == current_user.id, UserFile.file_sha256 == sha256)) + if user_file is None: + _ensure_storage_quota(current_user, file_size) + user_file = UserFile( + user_id=current_user.id, + file_sha256=sha256, + original_name=f"{payload.title[:80]}.txt", + ) + db.add(user_file) + current_user.storage_used += file_size + db.flush() + + novel = Novel( + user_id=current_user.id, + file_id=user_file.id, + source_id=source_id, + source_platform=payload.source_platform, + title=payload.title.strip(), + author=payload.author, + file_path=storage_path, + file_size=file_size, + cover_path=payload.cover_url, + word_count=len(payload.content), + tags=payload.tags, + description=payload.detail_url, + ) + db.add(novel) + db.commit() + db.refresh(novel) + return ApiResponse(code=201, message="created", data=NovelOut.model_validate(novel)) + + +@router.get("/{novel_id}/content") +def get_novel_content(novel_id: str, current_user: CurrentUser, db: DbSession) -> FileResponse: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + path = _resolve_novel_storage_path(novel, db) + novel.last_viewed_at = utc_now() + db.commit() + return FileResponse(path, media_type="text/plain", filename=path.name) + + +@router.get("/{novel_id}", response_model=ApiResponse[NovelOut]) +def get_novel(novel_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[NovelOut]: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + novel.last_viewed_at = utc_now() + db.commit() + db.refresh(novel) + return ApiResponse(data=NovelOut.model_validate(novel)) + + +@router.delete("/{novel_id}", response_model=ApiResponse[dict[str, bool]]) +def delete_novel(novel_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + current_user.storage_used = max(0, current_user.storage_used - max(0, novel.file_size)) + db.delete(novel) + db.commit() + return ApiResponse(data={"ok": True}) + + +@router.post("/batch-delete", response_model=ApiResponse[dict[str, int]]) +def batch_delete_novels( + payload: NovelBatchDeleteRequest, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[dict[str, int]]: + ids = sorted({item.strip() for item in payload.ids if item.strip()}) + series_ids = sorted({item.strip() for item in payload.series_ids if item.strip()}) + if not ids and not series_ids: + return ApiResponse(data={"deleted": 0}) + + rows: list[Novel] = [] + if ids: + rows.extend( + db.scalars( + select(Novel).where( + Novel.user_id == current_user.id, + Novel.id.in_(ids), + ) + ).all() + ) + if series_ids: + rows.extend( + db.scalars( + select(Novel).where( + Novel.user_id == current_user.id, + Novel.series_id.in_(series_ids), + ) + ).all() + ) + + deleted = 0 + released = 0 + seen: set[str] = set() + for novel in rows: + if novel.id in seen: + continue + seen.add(novel.id) + released += max(0, novel.file_size) + db.delete(novel) + deleted += 1 + if deleted: + current_user.storage_used = max(0, current_user.storage_used - released) + db.commit() + return ApiResponse(data={"deleted": deleted}) + + +@router.post("/{novel_id}/favorite", response_model=ApiResponse[NovelOut]) +def favorite_novel(novel_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[NovelOut]: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + novel.is_favorite = True + db.commit() + db.refresh(novel) + return ApiResponse(data=NovelOut.model_validate(novel)) + + +@router.delete("/{novel_id}/favorite", response_model=ApiResponse[NovelOut]) +def unfavorite_novel(novel_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[NovelOut]: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + novel.is_favorite = False + db.commit() + db.refresh(novel) + return ApiResponse(data=NovelOut.model_validate(novel)) + + +@router.post("/{novel_id}/progress", response_model=ApiResponse[NovelOut]) +def update_progress( + novel_id: str, + payload: ReadingProgressUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[NovelOut]: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + novel.current_page = payload.current_page + novel.last_read_at = utc_now() + novel.last_viewed_at = novel.last_read_at + db.commit() + db.refresh(novel) + return ApiResponse(data=NovelOut.model_validate(novel)) + + +@router.post("/{novel_id}/bookmark", response_model=ApiResponse[NovelOut]) +def update_bookmark( + novel_id: str, + payload: BookmarkUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[NovelOut]: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + novel.bookmark = payload.bookmark + db.commit() + db.refresh(novel) + return ApiResponse(data=NovelOut.model_validate(novel)) + + +@router.post("/{novel_id}/view", response_model=ApiResponse[NovelOut]) +def mark_novel_viewed( + novel_id: str, + payload: NovelViewUpdate, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[NovelOut]: + novel = db.get(Novel, novel_id) + if novel is None or novel.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Novel not found") + if payload.viewed: + novel.last_viewed_at = utc_now() + db.commit() + db.refresh(novel) + return ApiResponse(data=NovelOut.model_validate(novel)) diff --git a/sakura_cloud/api/v1/endpoints/online_search.py b/sakura_cloud/api/v1/endpoints/online_search.py new file mode 100644 index 0000000..7f691fe --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/online_search.py @@ -0,0 +1,100 @@ +from __future__ import annotations + +from typing import Any + +from fastapi import APIRouter, HTTPException, Query, status +from pydantic import BaseModel, Field + +from sakura_cloud.api.deps import CurrentUser +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.services.online_search import OnlineSearchError, parse_source_payload, search_sources +from sakura_cloud.services.online_search_engine import OnlineSearchEngine, SearchEngineError + + +router = APIRouter() + + +class OnlineSearchRequest(BaseModel): + query: str = Field(min_length=1, max_length=80) + sources: Any + limit_sources: int = Field(default=30, ge=1, le=80) + per_source_limit: int = Field(default=20, ge=1, le=50) + + +class OnlineSearchResult(BaseModel): + source_name: str + source_url: str + title: str + author: str | None = None + cover_url: str | None = None + detail_url: str + kind: str + + +class OnlineParseRequest(BaseModel): + detail_url: str = Field(min_length=1, max_length=2048) + + +class OnlineParsedChapter(BaseModel): + name: str + url: str | None = None + content: str | None = None + images: list[str] = Field(default_factory=list) + + +class OnlineParsedContent(BaseModel): + title: str + cover: str | None = None + kind: str + chapters: list[OnlineParsedChapter] + + +@router.post("", response_model=ApiResponse[list[OnlineSearchResult]]) +def online_search( + payload: OnlineSearchRequest, + current_user: CurrentUser, + mode: str = Query(pattern="^(novel|manga)$"), +) -> ApiResponse[list[OnlineSearchResult]]: + try: + sources = parse_source_payload(payload.sources) + rows = search_sources( + sources, + payload.query.strip(), + mode=mode, + limit_sources=payload.limit_sources, + per_source_limit=payload.per_source_limit, + ) + except OnlineSearchError as error: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(error)) from error + except ValueError as error: + raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Invalid source JSON") from error + return ApiResponse(data=[OnlineSearchResult(**item.__dict__) for item in rows]) + + +@router.post("/parse", response_model=ApiResponse[OnlineParsedContent]) +def parse_online_content( + payload: OnlineParseRequest, + current_user: CurrentUser, + mode: str = Query(pattern="^(novel|manga)$"), +) -> ApiResponse[OnlineParsedContent]: + engine = OnlineSearchEngine(timeout=12.0) + try: + if mode == "manga": + parsed = engine.parse_comic(payload.detail_url) + chapters = [ + OnlineParsedChapter(name=item.name, url=item.url, images=list(item.images)) + for item in parsed.chapters + ] + data = OnlineParsedContent(title=parsed.title, cover=parsed.cover, kind="manga", chapters=chapters) + else: + parsed = engine.parse_novel(payload.detail_url) + chapters = [ + OnlineParsedChapter(name=item.name, url=item.url, content=item.content) + for item in parsed.chapters + ] + data = OnlineParsedContent(title=parsed.title, cover=parsed.cover, kind="novel", chapters=chapters) + except SearchEngineError as error: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(error)) from error + except Exception as error: + raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Online source parse failed") from error + return ApiResponse(data=data) diff --git a/sakura_cloud/api/v1/endpoints/push.py b/sakura_cloud/api/v1/endpoints/push.py new file mode 100644 index 0000000..588e6a9 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/push.py @@ -0,0 +1,76 @@ +from __future__ import annotations + +from fastapi import APIRouter, status +from sqlalchemy import select + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.mixins import utc_now +from sakura_cloud.models.push import PushDeviceToken +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.push import PushDeviceTokenOut, PushTokenRegister, PushTokenUnregister + + +router = APIRouter() + + +@router.post("/register-token", response_model=ApiResponse[PushDeviceTokenOut], status_code=status.HTTP_201_CREATED) +def register_token( + payload: PushTokenRegister, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[PushDeviceTokenOut]: + row = db.scalar( + select(PushDeviceToken).where( + PushDeviceToken.user_id == current_user.id, + PushDeviceToken.device_id == payload.device_id, + ) + ) + now = utc_now() + if row is None: + row = PushDeviceToken( + user_id=current_user.id, + device_id=payload.device_id, + platform=payload.platform, + fcm_token=payload.fcm_token, + is_active=True, + last_seen_at=now, + ) + db.add(row) + else: + row.platform = payload.platform + row.fcm_token = payload.fcm_token + row.is_active = True + row.last_seen_at = now + + db.commit() + db.refresh(row) + return ApiResponse(code=201, message="registered", data=PushDeviceTokenOut.model_validate(row)) + + +@router.post("/unregister-token", response_model=ApiResponse[dict[str, bool]]) +def unregister_token( + payload: PushTokenUnregister, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[dict[str, bool]]: + row = db.scalar( + select(PushDeviceToken).where( + PushDeviceToken.user_id == current_user.id, + PushDeviceToken.device_id == payload.device_id, + ) + ) + if row is not None: + row.is_active = False + row.last_seen_at = utc_now() + db.commit() + return ApiResponse(message="unregistered", data={"ok": True}) + + +@router.get("/devices", response_model=ApiResponse[list[PushDeviceTokenOut]]) +def list_devices(current_user: CurrentUser, db: DbSession) -> ApiResponse[list[PushDeviceTokenOut]]: + rows = db.scalars( + select(PushDeviceToken) + .where(PushDeviceToken.user_id == current_user.id) + .order_by(PushDeviceToken.last_seen_at.desc().nullslast(), PushDeviceToken.updated_at.desc()) + ).all() + return ApiResponse(data=[PushDeviceTokenOut.model_validate(row) for row in rows]) diff --git a/sakura_cloud/api/v1/endpoints/sync.py b/sakura_cloud/api/v1/endpoints/sync.py new file mode 100644 index 0000000..d78caa0 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/sync.py @@ -0,0 +1,171 @@ +from __future__ import annotations + +from pydantic import BaseModel, Field +from fastapi import APIRouter, BackgroundTasks, HTTPException, status +from sqlalchemy import select + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.artwork import Artwork +from sakura_cloud.models.device import Device +from sakura_cloud.models.file import FileBlob, UserFile +from sakura_cloud.models.mixins import utc_now +from sakura_cloud.models.novel import Novel +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.device import DeviceOut, DeviceRegister +from sakura_cloud.schemas.gallery import ArtworkOut +from sakura_cloud.schemas.novel import NovelOut +from sakura_cloud.services.ocr import queue_ocr_for_artwork, run_ocr_for_artwork_id + + +router = APIRouter() + + +class SyncCheckRequest(BaseModel): + sha256: str = Field(min_length=64, max_length=64) + file_size: int = Field(ge=0) + + +class SyncMetadataRequest(BaseModel): + resource_type: str = Field(pattern="^(artwork|novel)$") + file_id: str + source_platform: str = Field(max_length=20) + source_id: str | None = Field(default=None, max_length=50) + content_type: str | None = Field(default=None, max_length=20) + title: str = Field(max_length=500) + author: str | None = Field(default=None, max_length=200) + author_id: str | None = Field(default=None, max_length=50) + tags: list[str] = Field(default_factory=list) + metadata: dict = Field(default_factory=dict) + page_count: int = 1 + thumbnail_url: str | None = None + description: str | None = None + word_count: int = 0 + series_id: str | None = Field(default=None, max_length=50) + series_title: str | None = Field(default=None, max_length=500) + chapter_title: str | None = Field(default=None, max_length=500) + chapter_order: int = 0 + + +@router.post("/check", response_model=ApiResponse[dict[str, bool | str | None]]) +def check_file(payload: SyncCheckRequest, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool | str | None]]: + blob = db.get(FileBlob, payload.sha256) + user_file = None + if blob is not None: + user_file = db.scalar(select(UserFile).where(UserFile.user_id == current_user.id, UserFile.file_sha256 == payload.sha256)) + return ApiResponse( + data={ + "exists": blob is not None, + "owned_by_user": user_file is not None, + "file_id": user_file.id if user_file is not None else None, + } + ) + + +@router.post("/metadata", response_model=ApiResponse[ArtworkOut | NovelOut]) +def sync_metadata( + payload: SyncMetadataRequest, + current_user: CurrentUser, + db: DbSession, + background_tasks: BackgroundTasks, +) -> ApiResponse[ArtworkOut | NovelOut]: + user_file = db.get(UserFile, payload.file_id) + if user_file is None or user_file.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found") + blob = db.get(FileBlob, user_file.file_sha256) + if blob is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Blob not found") + + if payload.resource_type == "artwork": + artwork = db.scalar( + select(Artwork).where( + Artwork.user_id == current_user.id, + Artwork.file_id == user_file.id, + ) + ) + if artwork is None: + artwork = Artwork(user_id=current_user.id, file_id=user_file.id) + db.add(artwork) + artwork.source_id = payload.source_id + artwork.source_platform = payload.source_platform + artwork.content_type = payload.content_type or "illust" + artwork.title = payload.title + artwork.author = payload.author + artwork.author_id = payload.author_id + artwork.page_count = payload.page_count + artwork.file_path = blob.storage_path + artwork.file_size = blob.file_size + artwork.thumbnail_path = blob.thumbnail_path or payload.thumbnail_url + artwork.mime_type = blob.mime_type + artwork.tags = payload.tags + artwork.artwork_metadata = payload.metadata + db.commit() + db.refresh(artwork) + if queue_ocr_for_artwork(db, artwork) is not None: + background_tasks.add_task(run_ocr_for_artwork_id, artwork.id, current_user.id) + return ApiResponse(data=ArtworkOut.model_validate(artwork)) + + novel = db.scalar( + select(Novel).where( + Novel.user_id == current_user.id, + Novel.file_id == user_file.id, + ) + ) + if novel is None and payload.source_id: + novel = db.scalar( + select(Novel).where( + Novel.user_id == current_user.id, + Novel.source_platform == payload.source_platform, + Novel.source_id == payload.source_id, + ) + ) + if novel is None: + novel = Novel(user_id=current_user.id, file_id=user_file.id) + db.add(novel) + novel.file_id = user_file.id + novel.source_id = payload.source_id + novel.source_platform = payload.source_platform + novel.title = payload.title + novel.author = payload.author + novel.series_id = payload.series_id + novel.series_title = payload.series_title + novel.chapter_title = payload.chapter_title + novel.chapter_order = payload.chapter_order + novel.description = payload.description + novel.file_path = blob.storage_path + novel.file_size = blob.file_size + novel.word_count = payload.word_count + novel.cover_path = blob.thumbnail_path or payload.thumbnail_url + novel.tags = payload.tags + db.commit() + db.refresh(novel) + return ApiResponse(data=NovelOut.model_validate(novel)) + + +@router.post("/device", response_model=ApiResponse[DeviceOut]) +def register_sync_device(payload: DeviceRegister, current_user: CurrentUser, db: DbSession) -> ApiResponse[DeviceOut]: + device = Device( + user_id=current_user.id, + device_name=payload.device_name, + device_type=payload.device_type, + device_token=payload.device_token, + last_active_at=utc_now(), + last_sync_at=utc_now(), + ) + db.add(device) + db.commit() + db.refresh(device) + return ApiResponse(data=DeviceOut.model_validate(device)) + + +@router.get("/device/{device_id}", response_model=ApiResponse[dict[str, str | None]]) +def device_sync_status(device_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, str | None]]: + device = db.get(Device, device_id) + if device is None or device.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Device not found") + return ApiResponse( + data={ + "device_id": device.id, + "last_sync_at": device.last_sync_at.isoformat() if device.last_sync_at else None, + "status": "idle", + } + ) diff --git a/sakura_cloud/api/v1/endpoints/tags.py b/sakura_cloud/api/v1/endpoints/tags.py new file mode 100644 index 0000000..afc86ba --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/tags.py @@ -0,0 +1,20 @@ +from __future__ import annotations + +from sqlalchemy import select +from fastapi import APIRouter + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.artwork import Artwork +from sakura_cloud.models.novel import Novel +from sakura_cloud.schemas.common import ApiResponse + + +router = APIRouter() + + +@router.get("", response_model=ApiResponse[list[str]]) +def list_all_tags(current_user: CurrentUser, db: DbSession) -> ApiResponse[list[str]]: + artwork_tags = db.scalars(select(Artwork.tags).where(Artwork.user_id == current_user.id)).all() + novel_tags = db.scalars(select(Novel.tags).where(Novel.user_id == current_user.id)).all() + tags = sorted({tag for row in [*artwork_tags, *novel_tags] for tag in (row or [])}) + return ApiResponse(data=tags) diff --git a/sakura_cloud/api/v1/endpoints/upload.py b/sakura_cloud/api/v1/endpoints/upload.py new file mode 100644 index 0000000..e229d18 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/upload.py @@ -0,0 +1,62 @@ +from __future__ import annotations + +from fastapi import APIRouter, File, Form, HTTPException, UploadFile, status + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.models.file import UploadSession +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.upload import ( + UploadCompleteRequest, + UploadCompleteResponse, + UploadInitRequest, + UploadInitResponse, + UploadStatusResponse, +) +from sakura_cloud.services.storage import append_chunk, complete_upload, init_upload + + +router = APIRouter() + + +@router.post("/init", response_model=ApiResponse[UploadInitResponse]) +def upload_init(payload: UploadInitRequest, current_user: CurrentUser, db: DbSession) -> ApiResponse[UploadInitResponse]: + return ApiResponse(data=init_upload(payload, current_user, db)) + + +@router.post("/chunk", response_model=ApiResponse[UploadStatusResponse]) +async def upload_chunk( + current_user: CurrentUser, + db: DbSession, + upload_id: str = Form(...), + chunk: UploadFile = File(...), +) -> ApiResponse[UploadStatusResponse]: + session = await append_chunk(upload_id, chunk, current_user, db) + return ApiResponse(data=_status_from_session(session)) + + +@router.post("/complete", response_model=ApiResponse[UploadCompleteResponse]) +def upload_complete( + payload: UploadCompleteRequest, + current_user: CurrentUser, + db: DbSession, +) -> ApiResponse[UploadCompleteResponse]: + return ApiResponse(data=complete_upload(payload.upload_id, current_user, db)) + + +@router.get("/status/{upload_id}", response_model=ApiResponse[UploadStatusResponse]) +def upload_status(upload_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[UploadStatusResponse]: + session = db.get(UploadSession, upload_id) + if session is None or session.user_id != current_user.id: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Upload session not found") + return ApiResponse(data=_status_from_session(session)) + + +def _status_from_session(session: UploadSession) -> UploadStatusResponse: + return UploadStatusResponse( + upload_id=session.id, + sha256=session.sha256, + file_name=session.file_name, + file_size=session.file_size, + received_size=session.received_size, + status=session.status, + ) diff --git a/sakura_cloud/api/v1/endpoints/users.py b/sakura_cloud/api/v1/endpoints/users.py new file mode 100644 index 0000000..3b08061 --- /dev/null +++ b/sakura_cloud/api/v1/endpoints/users.py @@ -0,0 +1,63 @@ +from __future__ import annotations + +from fastapi import APIRouter, HTTPException, status +from sqlalchemy import or_, select + +from sakura_cloud.api.deps import CurrentUser, DbSession +from sakura_cloud.core.security import hash_password, verify_password +from sakura_cloud.models.user import User +from sakura_cloud.schemas.common import ApiResponse +from sakura_cloud.schemas.user import PasswordChangeRequest, UserOut, UserUpdate + + +router = APIRouter() + + +@router.get("/me", response_model=ApiResponse[UserOut]) +def get_me(current_user: CurrentUser) -> ApiResponse[UserOut]: + return ApiResponse(data=UserOut.model_validate(current_user)) + + +@router.put("/me", response_model=ApiResponse[UserOut]) +def update_me(payload: UserUpdate, current_user: CurrentUser, db: DbSession) -> ApiResponse[UserOut]: + if payload.username or payload.email: + conditions = [] + if payload.username: + conditions.append(User.username == payload.username) + if payload.email: + conditions.append(User.email == str(payload.email)) + exists = db.scalar(select(User).where(or_(*conditions), User.id != current_user.id)) + if exists is not None: + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Username or email already exists") + if payload.username is not None: + current_user.username = payload.username + if payload.email is not None: + current_user.email = str(payload.email) + if payload.avatar is not None: + current_user.avatar = payload.avatar + if payload.reminder_enabled is not None: + current_user.reminder_enabled = payload.reminder_enabled + if payload.reminder_hour is not None: + current_user.reminder_hour = payload.reminder_hour + if payload.reminder_minute is not None: + current_user.reminder_minute = payload.reminder_minute + if payload.reminder_timezone is not None: + current_user.reminder_timezone = payload.reminder_timezone + db.commit() + db.refresh(current_user) + return ApiResponse(data=UserOut.model_validate(current_user)) + + +@router.post("/me/avatar", response_model=ApiResponse[dict[str, str | None]]) +def upload_avatar(current_user: CurrentUser) -> ApiResponse[dict[str, str | None]]: + return ApiResponse(message="avatar upload placeholder", data={"avatar": current_user.avatar}) + + +@router.put("/me/password", response_model=ApiResponse[dict[str, bool]]) +def change_password(payload: PasswordChangeRequest, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]: + if not verify_password(payload.old_password, current_user.password_hash): + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Old password is incorrect") + current_user.password_hash = hash_password(payload.new_password) + current_user.token_version += 1 + db.commit() + return ApiResponse(data={"ok": True}) diff --git a/sakura_cloud/api/v1/router.py b/sakura_cloud/api/v1/router.py new file mode 100644 index 0000000..1e328fc --- /dev/null +++ b/sakura_cloud/api/v1/router.py @@ -0,0 +1,39 @@ +from __future__ import annotations + +from fastapi import APIRouter + +from sakura_cloud.api.v1.endpoints import ( + auth, + admin, + devices, + files, + follows, + gallery, + health, + me, + novels, + online_search, + push, + sync, + tags, + upload, + users, +) + + +api_router = APIRouter() +api_router.include_router(health.router, tags=["health"]) +api_router.include_router(auth.router, prefix="/auth", tags=["auth"]) +api_router.include_router(admin.router, prefix="/admin", tags=["admin"]) +api_router.include_router(users.router, prefix="/users", tags=["users"]) +api_router.include_router(me.router, prefix="/me", tags=["me"]) +api_router.include_router(devices.router, prefix="/devices", tags=["devices"]) +api_router.include_router(upload.router, prefix="/upload", tags=["upload"]) +api_router.include_router(files.router, prefix="/files", tags=["files"]) +api_router.include_router(gallery.router, prefix="/gallery", tags=["gallery"]) +api_router.include_router(novels.router, prefix="/novels", tags=["novels"]) +api_router.include_router(online_search.router, prefix="/online-search", tags=["online-search"]) +api_router.include_router(follows.router, prefix="/follows", tags=["follows"]) +api_router.include_router(push.router, prefix="/push", tags=["push"]) +api_router.include_router(sync.router, prefix="/sync", tags=["sync"]) +api_router.include_router(tags.router, prefix="/tags", tags=["tags"]) diff --git a/sakura_cloud/core/__init__.py b/sakura_cloud/core/__init__.py new file mode 100644 index 0000000..8b7da05 --- /dev/null +++ b/sakura_cloud/core/__init__.py @@ -0,0 +1 @@ +"""Core configuration and security helpers.""" diff --git a/sakura_cloud/core/config.py b/sakura_cloud/core/config.py new file mode 100644 index 0000000..1586c6e --- /dev/null +++ b/sakura_cloud/core/config.py @@ -0,0 +1,149 @@ +from __future__ import annotations + +from functools import lru_cache +from typing import Any + +from pydantic import Field, field_validator, model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + app_name: str = "Sakura Cloud" + app_version: str = "0.1.0" + api_prefix: str = "/api/v1" + debug: bool = False + environment: str = Field(default="development", validation_alias="SAKURA_ENV") + auto_migrate_on_startup: bool = False + + database_url: str = "postgresql+psycopg://sakura:sakura@localhost:5432/sakura_cloud" + redis_url: str = "redis://localhost:6379/0" + + storage_root: str = "data/sakura" + log_dir: str = "data/sakura/logs" + + minio_endpoint: str = "localhost:9000" + minio_access_key: str = "minioadmin" + minio_secret_key: str = "minioadmin" + minio_bucket: str = "sakura-cloud" + minio_secure: bool = False + + jwt_secret_key: str = Field(default="change-this-secret-in-production", min_length=16) + jwt_algorithm: str = "HS256" + jwt_issuer: str = "sakura-cloud" + jwt_audience: str = "sakura-cloud" + jwt_access_token_expire_hours: int = 24 + jwt_refresh_token_expire_days: int = 30 + + default_storage_limit_gb: int = 5 + max_upload_size_mb: int = 100 + max_video_upload_size_mb: int = 2048 + video_storage_limit_gb: int = 30 + max_batch_size_mb: int = 500 + allowed_upload_mime_types: list[str] = [ + "image/jpeg", + "image/png", + "image/webp", + "image/gif", + "video/mp4", + "video/webm", + "video/quicktime", + "video/x-m4v", + "video/x-msvideo", + "text/plain", + ] + allowed_external_url_domains: list[str] = [ + "pixiv.net", + "www.pixiv.net", + "i.pximg.net", + "pximg.net", + "x.com", + "twitter.com", + ] + upload_chunk_size: int = 5 * 1024 * 1024 + temp_upload_ttl_hours: int = 24 + thumbnail_max_size: int = 640 + thumbnail_quality: int = 82 + fcm_enabled: bool = True + firebase_credentials_path: str = "" + firebase_credentials_json: str = "" + ocr_enabled: bool = False + ocr_language: str = "ch" + ocr_max_image_dimension: int = 1800 + ocr_max_attempts: int = 3 + ocr_max_concurrency: int = 1 + + cors_origins: list[str] = [ + "http://localhost:5173", + "http://127.0.0.1:5173", + "http://localhost:5174", + "http://127.0.0.1:5174", + "http://localhost:3000", + "http://127.0.0.1:3000", + "app://.", + ] + admin_login_rate_limit_per_minute: int = 5 + admin_frontend_enabled: bool = True + + model_config = SettingsConfigDict( + env_file=".env", + env_file_encoding="utf-8", + extra="ignore", + ) + + @field_validator("cors_origins", mode="before") + @classmethod + def parse_cors_origins(cls, value: Any) -> list[str]: + if isinstance(value, str): + text = value.strip() + if text.startswith("["): + import json + + return list(json.loads(text)) + return [item.strip() for item in text.split(",") if item.strip()] + return value + + @field_validator("allowed_upload_mime_types", "allowed_external_url_domains", mode="before") + @classmethod + def parse_string_list(cls, value: Any) -> list[str]: + if isinstance(value, str): + text = value.strip() + if text.startswith("["): + import json + + return list(json.loads(text)) + return [item.strip() for item in text.split(",") if item.strip()] + return value + + @model_validator(mode="after") + def reject_insecure_production_defaults(self) -> "Settings": + if self.environment.lower() not in {"prod", "production"}: + return self + insecure: list[str] = [] + if self.jwt_secret_key == "change-this-secret-in-production": + insecure.append("JWT_SECRET_KEY") + if "sakura:sakura@" in self.database_url: + insecure.append("DATABASE_URL") + if self.minio_access_key == "minioadmin" or self.minio_secret_key == "minioadmin": + insecure.append("MINIO_ACCESS_KEY/MINIO_SECRET_KEY") + if insecure: + raise ValueError( + "Refusing to start production with insecure default credentials: " + + ", ".join(insecure) + ) + return self + + @property + def default_storage_limit_bytes(self) -> int: + return self.default_storage_limit_gb * 1024 * 1024 * 1024 + + @property + def video_storage_limit_bytes(self) -> int: + return self.video_storage_limit_gb * 1024 * 1024 * 1024 + + +@lru_cache +def get_settings() -> Settings: + return Settings() + + +settings = get_settings() diff --git a/sakura_cloud/core/logging.py b/sakura_cloud/core/logging.py new file mode 100644 index 0000000..9a3e6d7 --- /dev/null +++ b/sakura_cloud/core/logging.py @@ -0,0 +1,19 @@ +from __future__ import annotations + +import sys +from pathlib import Path + +from loguru import logger + +from sakura_cloud.core.config import settings + + +def configure_logging() -> None: + log_dir = Path(settings.log_dir) + log_dir.mkdir(parents=True, exist_ok=True) + logger.remove() + logger.add(sys.stderr, level="INFO", enqueue=True) + logger.add(log_dir / "app.log", level="INFO", rotation="50 MB", retention=10, enqueue=True) + logger.add(log_dir / "api.log", level="INFO", rotation="100 MB", retention=5, enqueue=True, filter=lambda r: r["extra"].get("kind") == "api") + logger.add(log_dir / "sync.log", level="INFO", rotation="50 MB", retention=10, enqueue=True, filter=lambda r: r["extra"].get("kind") == "sync") + logger.add(log_dir / "error.log", level="ERROR", rotation="50 MB", retention="30 days", enqueue=True) diff --git a/sakura_cloud/core/safety.py b/sakura_cloud/core/safety.py new file mode 100644 index 0000000..5dd8d48 --- /dev/null +++ b/sakura_cloud/core/safety.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +import ipaddress +from pathlib import PurePath +from urllib.parse import urlparse + +from sakura_cloud.core.config import settings + + +def is_safe_upload_filename(file_name: str) -> bool: + name = file_name.strip() + if not name: + return False + path = PurePath(name) + return path.name == name and name not in {".", ".."} and ".." not in path.parts + + +def is_allowed_upload_mime(mime_type: str | None) -> bool: + if not mime_type: + return False + return mime_type.lower().strip() in {item.lower() for item in settings.allowed_upload_mime_types} + + +def is_allowed_external_url(url: str) -> bool: + parsed = urlparse(url.strip()) + if parsed.scheme not in {"http", "https"} or not parsed.hostname: + return False + host = parsed.hostname.rstrip(".").lower() + try: + ip = ipaddress.ip_address(host) + except ValueError: + ip = None + if ip is not None: + return not (ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved or ip.is_multicast) + if host in {"localhost"} or host.endswith(".localhost"): + return False + allowed = {item.lower().strip().rstrip(".") for item in settings.allowed_external_url_domains} + return any(host == domain or host.endswith(f".{domain}") for domain in allowed if domain) diff --git a/sakura_cloud/core/security.py b/sakura_cloud/core/security.py new file mode 100644 index 0000000..c340f4e --- /dev/null +++ b/sakura_cloud/core/security.py @@ -0,0 +1,70 @@ +from __future__ import annotations + +from datetime import datetime, timedelta, timezone +from typing import Any + +from jose import JWTError, jwt +from passlib.context import CryptContext + +from sakura_cloud.core.config import settings + + +pwd_context = CryptContext(schemes=["pbkdf2_sha256", "bcrypt"], deprecated="auto") + + +def hash_password(password: str) -> str: + return pwd_context.hash(password) + + +def verify_password(password: str, password_hash: str) -> bool: + return pwd_context.verify(password, password_hash) + + +def create_token(subject: str, *, token_type: str, expires_delta: timedelta, token_version: int = 0) -> str: + now = datetime.now(timezone.utc) + payload: dict[str, Any] = { + "sub": subject, + "type": token_type, + "tv": token_version, + "iss": settings.jwt_issuer, + "aud": settings.jwt_audience, + "iat": int(now.timestamp()), + "exp": int((now + expires_delta).timestamp()), + } + return jwt.encode(payload, settings.jwt_secret_key, algorithm=settings.jwt_algorithm) + + +def create_access_token(subject: str, token_version: int = 0) -> str: + return create_token( + subject, + token_type="access", + expires_delta=timedelta(hours=settings.jwt_access_token_expire_hours), + token_version=token_version, + ) + + +def create_refresh_token(subject: str, token_version: int = 0) -> str: + return create_token( + subject, + token_type="refresh", + expires_delta=timedelta(days=settings.jwt_refresh_token_expire_days), + token_version=token_version, + ) + + +def decode_token(token: str) -> dict[str, Any]: + try: + payload = jwt.decode( + token, + settings.jwt_secret_key, + algorithms=[settings.jwt_algorithm], + audience=settings.jwt_audience, + issuer=settings.jwt_issuer, + ) + except JWTError as exc: + raise ValueError("Invalid token") from exc + if "exp" not in payload: + raise ValueError("Invalid token") + if payload.get("type") not in {"access", "refresh"}: + raise ValueError("Invalid token") + return payload diff --git a/sakura_cloud/db/__init__.py b/sakura_cloud/db/__init__.py new file mode 100644 index 0000000..b4f3b5d --- /dev/null +++ b/sakura_cloud/db/__init__.py @@ -0,0 +1 @@ +"""Database session and model metadata.""" diff --git a/sakura_cloud/db/base.py b/sakura_cloud/db/base.py new file mode 100644 index 0000000..6d6e58c --- /dev/null +++ b/sakura_cloud/db/base.py @@ -0,0 +1,7 @@ +from __future__ import annotations + +from sqlalchemy.orm import DeclarativeBase + + +class Base(DeclarativeBase): + pass diff --git a/sakura_cloud/db/session.py b/sakura_cloud/db/session.py new file mode 100644 index 0000000..76d57a3 --- /dev/null +++ b/sakura_cloud/db/session.py @@ -0,0 +1,20 @@ +from __future__ import annotations + +from collections.abc import Generator + +from sqlalchemy import create_engine +from sqlalchemy.orm import Session, sessionmaker + +from sakura_cloud.core.config import settings + + +engine = create_engine(settings.database_url, pool_pre_ping=True) +SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) + + +def get_db() -> Generator[Session, None, None]: + db = SessionLocal() + try: + yield db + finally: + db.close() diff --git a/sakura_cloud/docker-compose.yml b/sakura_cloud/docker-compose.yml new file mode 100644 index 0000000..7418b9f --- /dev/null +++ b/sakura_cloud/docker-compose.yml @@ -0,0 +1,23 @@ +services: + postgres: + image: postgres:15 + container_name: sakura-cloud-postgres + restart: unless-stopped + environment: + POSTGRES_USER: sakura + POSTGRES_PASSWORD: sakura + POSTGRES_DB: sakura_cloud + ports: + - "5432:5432" + volumes: + - sakura_cloud_postgres:/var/lib/postgresql/data + + redis: + image: redis:7 + container_name: sakura-cloud-redis + restart: unless-stopped + ports: + - "6379:6379" + +volumes: + sakura_cloud_postgres: diff --git a/sakura_cloud/main.py b/sakura_cloud/main.py new file mode 100644 index 0000000..25ec2ec --- /dev/null +++ b/sakura_cloud/main.py @@ -0,0 +1,410 @@ +from __future__ import annotations + +import asyncio +from pathlib import Path + +from fastapi import FastAPI, HTTPException +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import FileResponse, HTMLResponse +from fastapi.staticfiles import StaticFiles + +from sakura_cloud.api.middleware import security_headers_middleware, trace_logging_middleware +from sakura_cloud.api.v1.router import api_router +from sakura_cloud.core.config import settings +from sakura_cloud.core.logging import configure_logging, logger +from sakura_cloud.db.session import SessionLocal +from sakura_cloud.services.migrations import upgrade_database_to_head +from sakura_cloud.services.reminders import run_daily_reminder_digest +from sakura_cloud.services.storage import cleanup_stale_uploads + + +def _admin_frontend_dist() -> Path: + base_dir = Path(__file__).resolve().parent + candidates = [ + base_dir.parent / "sakura_admin_web" / "dist", + base_dir / "sakura_admin_web" / "dist", + ] + for candidate in candidates: + if (candidate / "index.html").is_file(): + return candidate + return candidates[0] + + +def _cloud_public_dir() -> Path: + return Path(__file__).resolve().parent / "public" + + +def _online_search_page() -> HTMLResponse: + return HTMLResponse( + """ + + + + + Sakura Cloud 在线搜索 + + + +
+
+
+

Sakura 在线搜索

+

搜索漫画和小说,可使用云端内置 JSON 源,也可以粘贴自己的 Legado 书源/漫画源 JSON。

+
+ 手机端已适配 +
+
+
+ + + +
+ +
+

默认使用内置精选源。需要临时测试时,可以展开并粘贴 JSON。

+ +
+ +

+
+
+
+ + +""", + headers={ + "Content-Security-Policy": ( + "default-src 'self'; base-uri 'self'; object-src 'none'; " + "frame-ancestors 'none'; img-src 'self' data: blob: https: http:; " + "style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; " + "connect-src 'self'" + ) + }, + ) + + +def _admin_placeholder() -> HTMLResponse: + return HTMLResponse( + """ + + + + + Sakura Cloud Admin + + +
+

Sakura Cloud Admin

+

Frontend not built. Run: cd sakura_admin_web && npm run build

+
+ +""" + ) + + +def _spa_index(frontend_dist: Path) -> FileResponse | HTMLResponse: + index_path = frontend_dist / "index.html" + if index_path.is_file(): + return FileResponse(index_path) + return _admin_placeholder() + + +async def cleanup_uploads_loop() -> None: + while True: + await asyncio.sleep(3600) + db = SessionLocal() + try: + cleanup_stale_uploads(db) + except Exception: + logger.exception("Cleanup stale uploads failed") + finally: + db.close() + + +async def reminder_digest_loop() -> None: + while True: + await asyncio.sleep(300) + db = SessionLocal() + try: + run_daily_reminder_digest(db) + except Exception: + logger.exception("Reminder digest loop failed") + finally: + db.close() + + +def create_app() -> FastAPI: + configure_logging() + app = FastAPI( + title=settings.app_name, + version=settings.app_version, + openapi_url=f"{settings.api_prefix}/openapi.json", + ) + app.add_middleware( + CORSMiddleware, + allow_origins=settings.cors_origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) + app.middleware("http")(security_headers_middleware) + app.middleware("http")(trace_logging_middleware) + app.include_router(api_router, prefix=settings.api_prefix) + + frontend_dist = _admin_frontend_dist() + public_dir = _cloud_public_dir() + assets_dir = frontend_dist / "assets" + if settings.admin_frontend_enabled and assets_dir.is_dir(): + app.mount("/assets", StaticFiles(directory=assets_dir), name="admin_assets") + sources_dir = public_dir / "sources" + if sources_dir.is_dir(): + app.mount("/sources", StaticFiles(directory=sources_dir), name="source_json") + + @app.get("/online-search", response_model=None) + def online_search_console() -> HTMLResponse: + return _online_search_page() + + @app.get("/search", response_model=None) + def search_console() -> HTMLResponse: + return _online_search_page() + + @app.get("/admin", response_model=None) + def admin_console() -> FileResponse | HTMLResponse: + if not settings.admin_frontend_enabled: + return HTMLResponse("

Admin console disabled

", status_code=404) + return _spa_index(frontend_dist) + + @app.get("/{full_path:path}", include_in_schema=False, response_model=None) + def spa_fallback(full_path: str) -> FileResponse | HTMLResponse: + api_prefix = settings.api_prefix.strip("/") + if full_path == api_prefix or full_path.startswith(api_prefix + "/"): + raise HTTPException(status_code=404, detail="API route not found") + if not settings.admin_frontend_enabled: + raise HTTPException(status_code=404, detail="Frontend disabled") + return _spa_index(frontend_dist) + + @app.on_event("startup") + async def _start_background_tasks() -> None: + if settings.auto_migrate_on_startup: + logger.info("Auto migration enabled, upgrading database to head") + upgrade_database_to_head() + else: + logger.info("Auto migration disabled, skipping database upgrade on startup") + db = SessionLocal() + try: + cleanup_stale_uploads(db) + try: + run_daily_reminder_digest(db) + except Exception: + logger.exception("Initial reminder digest failed during startup") + finally: + db.close() + logger.info("Startup tasks completed") + asyncio.create_task(cleanup_uploads_loop()) + asyncio.create_task(reminder_digest_loop()) + + return app + + +app = create_app() diff --git a/sakura_cloud/migrations/env.py b/sakura_cloud/migrations/env.py new file mode 100644 index 0000000..58a945c --- /dev/null +++ b/sakura_cloud/migrations/env.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +from logging.config import fileConfig + +from alembic import context +from sqlalchemy import engine_from_config, pool + +from sakura_cloud.core.config import settings +from sakura_cloud.db.base import Base +from sakura_cloud import models # noqa: F401 + + +config = context.config +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +config.set_main_option("sqlalchemy.url", settings.database_url) +target_metadata = Base.metadata + + +def run_migrations_offline() -> None: + context.configure( + url=settings.database_url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + with connectable.connect() as connection: + context.configure(connection=connection, target_metadata=target_metadata) + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/sakura_cloud/migrations/script.py.mako b/sakura_cloud/migrations/script.py.mako new file mode 100644 index 0000000..c39cb68 --- /dev/null +++ b/sakura_cloud/migrations/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} +""" + +from __future__ import annotations + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + + +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/sakura_cloud/migrations/versions/0001_initial_cloud_schema.py b/sakura_cloud/migrations/versions/0001_initial_cloud_schema.py new file mode 100644 index 0000000..233bc19 --- /dev/null +++ b/sakura_cloud/migrations/versions/0001_initial_cloud_schema.py @@ -0,0 +1,180 @@ +"""initial cloud schema + +Revision ID: 0001_initial_cloud_schema +Revises: +Create Date: 2026-06-02 +""" + +from __future__ import annotations + +from alembic import op +import sqlalchemy as sa + + +revision = "0001_initial_cloud_schema" +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "users", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("username", sa.String(length=50), nullable=False), + sa.Column("email", sa.String(length=100), nullable=False), + sa.Column("password_hash", sa.String(length=255), nullable=False), + sa.Column("avatar", sa.Text(), nullable=True), + sa.Column("storage_used", sa.BigInteger(), nullable=False), + sa.Column("storage_limit", sa.BigInteger(), nullable=False), + sa.Column("role", sa.Enum("ADMIN", "USER", name="userrole"), nullable=False), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("email"), + sa.UniqueConstraint("username"), + ) + op.create_index("idx_users_email", "users", ["email"]) + op.create_index("idx_users_username", "users", ["username"]) + op.create_table( + "file_blobs", + sa.Column("sha256", sa.String(length=64), nullable=False), + sa.Column("storage_path", sa.Text(), nullable=False), + sa.Column("file_size", sa.BigInteger(), nullable=False), + sa.Column("mime_type", sa.String(length=50), nullable=True), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.PrimaryKeyConstraint("sha256"), + ) + op.create_table( + "devices", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("device_name", sa.String(length=100), nullable=False), + sa.Column("device_type", sa.String(length=20), nullable=False), + sa.Column("device_token", sa.String(length=255), nullable=True), + sa.Column("last_active_at", sa.DateTime(timezone=True), nullable=True), + sa.Column("last_sync_at", sa.DateTime(timezone=True), nullable=True), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("idx_devices_user_id", "devices", ["user_id"]) + op.create_index("idx_devices_last_active", "devices", ["last_active_at"]) + op.create_table( + "user_files", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("file_sha256", sa.String(length=64), nullable=False), + sa.Column("original_name", sa.String(length=255), nullable=False), + sa.Column("uploaded_at", sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(["file_sha256"], ["file_blobs.sha256"], ondelete="CASCADE"), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("idx_user_files_user_id", "user_files", ["user_id"]) + op.create_table( + "upload_sessions", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("sha256", sa.String(length=64), nullable=False), + sa.Column("file_name", sa.String(length=255), nullable=False), + sa.Column("file_size", sa.BigInteger(), nullable=False), + sa.Column("mime_type", sa.String(length=50), nullable=True), + sa.Column("resource_type", sa.String(length=20), nullable=False), + sa.Column("chunk_size", sa.BigInteger(), nullable=False), + sa.Column("received_size", sa.BigInteger(), nullable=False), + sa.Column("temp_path", sa.Text(), nullable=False), + sa.Column("status", sa.String(length=20), nullable=False), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("idx_upload_sessions_user_id", "upload_sessions", ["user_id"]) + op.create_index("ix_upload_sessions_sha256", "upload_sessions", ["sha256"]) + op.create_table( + "artworks", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("file_id", sa.String(length=36), nullable=True), + sa.Column("source_id", sa.String(length=50), nullable=True), + sa.Column("source_platform", sa.String(length=20), nullable=False), + sa.Column("title", sa.String(length=500), nullable=False), + sa.Column("author", sa.String(length=200), nullable=True), + sa.Column("author_id", sa.String(length=50), nullable=True), + sa.Column("page_count", sa.Integer(), nullable=False), + sa.Column("file_path", sa.Text(), nullable=False), + sa.Column("file_size", sa.BigInteger(), nullable=False), + sa.Column("thumbnail_path", sa.Text(), nullable=True), + sa.Column("mime_type", sa.String(length=50), nullable=True), + sa.Column("width", sa.Integer(), nullable=True), + sa.Column("height", sa.Integer(), nullable=True), + sa.Column("tags", sa.JSON(), nullable=False), + sa.Column("is_favorite", sa.Boolean(), nullable=False), + sa.Column("metadata", sa.JSON(), nullable=False), + sa.ForeignKeyConstraint(["file_id"], ["user_files.id"], ondelete="SET NULL"), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("ix_artworks_author", "artworks", ["author"]) + op.create_index("ix_artworks_is_favorite", "artworks", ["is_favorite"]) + op.create_index("ix_artworks_source_platform", "artworks", ["source_platform"]) + op.create_table( + "novels", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("file_id", sa.String(length=36), nullable=True), + sa.Column("source_id", sa.String(length=50), nullable=True), + sa.Column("source_platform", sa.String(length=20), nullable=False), + sa.Column("title", sa.String(length=500), nullable=False), + sa.Column("author", sa.String(length=200), nullable=True), + sa.Column("description", sa.Text(), nullable=True), + sa.Column("file_path", sa.Text(), nullable=False), + sa.Column("file_size", sa.BigInteger(), nullable=False), + sa.Column("cover_path", sa.Text(), nullable=True), + sa.Column("word_count", sa.Integer(), nullable=False), + sa.Column("current_page", sa.Integer(), nullable=False), + sa.Column("bookmark", sa.Text(), nullable=True), + sa.Column("tags", sa.JSON(), nullable=False), + sa.Column("is_favorite", sa.Boolean(), nullable=False), + sa.Column("last_read_at", sa.DateTime(timezone=True), nullable=True), + sa.ForeignKeyConstraint(["file_id"], ["user_files.id"], ondelete="SET NULL"), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("ix_novels_author", "novels", ["author"]) + op.create_index("ix_novels_is_favorite", "novels", ["is_favorite"]) + op.create_index("ix_novels_source_platform", "novels", ["source_platform"]) + op.execute( + "CREATE INDEX IF NOT EXISTS idx_artworks_search ON artworks " + "USING GIN (to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(author, '') || ' ' || coalesce(source_platform, '')))" + ) + op.execute( + "CREATE INDEX IF NOT EXISTS idx_novels_search ON novels " + "USING GIN (to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(author, '') || ' ' || coalesce(description, '')))" + ) + + +def downgrade() -> None: + op.execute("DROP INDEX IF EXISTS idx_novels_search") + op.execute("DROP INDEX IF EXISTS idx_artworks_search") + op.drop_table("novels") + op.drop_table("artworks") + op.drop_index("ix_upload_sessions_sha256", table_name="upload_sessions") + op.drop_index("idx_upload_sessions_user_id", table_name="upload_sessions") + op.drop_table("upload_sessions") + op.drop_index("idx_user_files_user_id", table_name="user_files") + op.drop_table("user_files") + op.drop_index("idx_devices_last_active", table_name="devices") + op.drop_index("idx_devices_user_id", table_name="devices") + op.drop_table("devices") + op.drop_table("file_blobs") + op.drop_index("idx_users_username", table_name="users") + op.drop_index("idx_users_email", table_name="users") + op.drop_table("users") + op.execute("DROP TYPE IF EXISTS userrole") diff --git a/sakura_cloud/migrations/versions/0002_add_file_blob_thumbnail_path.py b/sakura_cloud/migrations/versions/0002_add_file_blob_thumbnail_path.py new file mode 100644 index 0000000..4c6b900 --- /dev/null +++ b/sakura_cloud/migrations/versions/0002_add_file_blob_thumbnail_path.py @@ -0,0 +1,25 @@ +"""add file blob thumbnail path + +Revision ID: 0002_file_blob_thumb +Revises: 0001_initial_cloud_schema +Create Date: 2026-06-02 +""" + +from __future__ import annotations + +from alembic import op +import sqlalchemy as sa + + +revision = "0002_file_blob_thumb" +down_revision = "0001_initial_cloud_schema" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.add_column("file_blobs", sa.Column("thumbnail_path", sa.Text(), nullable=True)) + + +def downgrade() -> None: + op.drop_column("file_blobs", "thumbnail_path") diff --git a/sakura_cloud/migrations/versions/0003_content_features.py b/sakura_cloud/migrations/versions/0003_content_features.py new file mode 100644 index 0000000..13c5f55 --- /dev/null +++ b/sakura_cloud/migrations/versions/0003_content_features.py @@ -0,0 +1,110 @@ +"""add content features and alerts + +Revision ID: 0003_content_features +Revises: 0002_file_blob_thumb +Create Date: 2026-06-03 +""" + +from __future__ import annotations + +from alembic import op +import sqlalchemy as sa + + +revision = "0003_content_features" +down_revision = "0002_file_blob_thumb" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.add_column("artworks", sa.Column("content_type", sa.String(length=20), nullable=False, server_default="illust")) + op.add_column("artworks", sa.Column("last_viewed_at", sa.DateTime(timezone=True), nullable=True)) + op.create_index("ix_artworks_content_type", "artworks", ["content_type"]) + op.create_index("ix_artworks_last_viewed_at", "artworks", ["last_viewed_at"]) + + op.add_column("novels", sa.Column("series_id", sa.String(length=50), nullable=True)) + op.add_column("novels", sa.Column("series_title", sa.String(length=500), nullable=True)) + op.add_column("novels", sa.Column("chapter_title", sa.String(length=500), nullable=True)) + op.add_column("novels", sa.Column("chapter_order", sa.Integer(), nullable=False, server_default="0")) + op.add_column("novels", sa.Column("last_viewed_at", sa.DateTime(timezone=True), nullable=True)) + op.create_index("ix_novels_series_id", "novels", ["series_id"]) + op.create_index("ix_novels_last_viewed_at", "novels", ["last_viewed_at"]) + + op.create_table( + "creator_follows", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("platform", sa.String(length=20), nullable=False), + sa.Column("creator_id", sa.String(length=100), nullable=False), + sa.Column("creator_name", sa.String(length=200), nullable=True), + sa.Column("creator_handle", sa.String(length=200), nullable=True), + sa.Column("profile_url", sa.Text(), nullable=True), + sa.Column("avatar_url", sa.Text(), nullable=True), + sa.Column("enabled", sa.Boolean(), nullable=False), + sa.Column("last_checked_at", sa.DateTime(timezone=True), nullable=True), + sa.Column("latest_published_at", sa.DateTime(timezone=True), nullable=True), + sa.Column("metadata", sa.JSON(), nullable=False), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("ix_creator_follows_user_id", "creator_follows", ["user_id"]) + op.create_index("ix_creator_follows_platform", "creator_follows", ["platform"]) + op.create_index("ix_creator_follows_creator_id", "creator_follows", ["creator_id"]) + op.create_index("ix_creator_follows_enabled", "creator_follows", ["enabled"]) + + op.create_table( + "update_alerts", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("follow_id", sa.String(length=36), nullable=True), + sa.Column("platform", sa.String(length=20), nullable=False), + sa.Column("creator_id", sa.String(length=100), nullable=False), + sa.Column("title", sa.String(length=500), nullable=False), + sa.Column("body", sa.Text(), nullable=True), + sa.Column("content_type", sa.String(length=20), nullable=True), + sa.Column("target_url", sa.Text(), nullable=True), + sa.Column("cover_url", sa.Text(), nullable=True), + sa.Column("is_read", sa.Boolean(), nullable=False), + sa.Column("payload", sa.JSON(), nullable=False), + sa.ForeignKeyConstraint(["follow_id"], ["creator_follows.id"], ondelete="SET NULL"), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("ix_update_alerts_user_id", "update_alerts", ["user_id"]) + op.create_index("ix_update_alerts_follow_id", "update_alerts", ["follow_id"]) + op.create_index("ix_update_alerts_platform", "update_alerts", ["platform"]) + op.create_index("ix_update_alerts_creator_id", "update_alerts", ["creator_id"]) + op.create_index("ix_update_alerts_is_read", "update_alerts", ["is_read"]) + + +def downgrade() -> None: + op.drop_index("ix_update_alerts_is_read", table_name="update_alerts") + op.drop_index("ix_update_alerts_creator_id", table_name="update_alerts") + op.drop_index("ix_update_alerts_platform", table_name="update_alerts") + op.drop_index("ix_update_alerts_follow_id", table_name="update_alerts") + op.drop_index("ix_update_alerts_user_id", table_name="update_alerts") + op.drop_table("update_alerts") + + op.drop_index("ix_creator_follows_enabled", table_name="creator_follows") + op.drop_index("ix_creator_follows_creator_id", table_name="creator_follows") + op.drop_index("ix_creator_follows_platform", table_name="creator_follows") + op.drop_index("ix_creator_follows_user_id", table_name="creator_follows") + op.drop_table("creator_follows") + + op.drop_index("ix_novels_last_viewed_at", table_name="novels") + op.drop_index("ix_novels_series_id", table_name="novels") + op.drop_column("novels", "last_viewed_at") + op.drop_column("novels", "chapter_order") + op.drop_column("novels", "chapter_title") + op.drop_column("novels", "series_title") + op.drop_column("novels", "series_id") + + op.drop_index("ix_artworks_last_viewed_at", table_name="artworks") + op.drop_index("ix_artworks_content_type", table_name="artworks") + op.drop_column("artworks", "last_viewed_at") + op.drop_column("artworks", "content_type") diff --git a/sakura_cloud/migrations/versions/0004_daily_reminder_preferences.py b/sakura_cloud/migrations/versions/0004_daily_reminder_preferences.py new file mode 100644 index 0000000..0542a05 --- /dev/null +++ b/sakura_cloud/migrations/versions/0004_daily_reminder_preferences.py @@ -0,0 +1,36 @@ +"""add daily reminder preferences + +Revision ID: 0004_daily_reminder_preferences +Revises: 0003_content_features +Create Date: 2026-06-03 +""" + +from __future__ import annotations + +from alembic import op +import sqlalchemy as sa + + +revision = "0004_daily_reminder_preferences" +down_revision = "0003_content_features" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.add_column("users", sa.Column("reminder_enabled", sa.Boolean(), nullable=False, server_default=sa.true())) + op.add_column("users", sa.Column("reminder_hour", sa.Integer(), nullable=False, server_default="9")) + op.add_column("users", sa.Column("reminder_minute", sa.Integer(), nullable=False, server_default="0")) + op.add_column( + "users", + sa.Column("reminder_timezone", sa.String(length=64), nullable=False, server_default="Asia/Shanghai"), + ) + op.add_column("users", sa.Column("last_reminder_sent_at", sa.DateTime(timezone=True), nullable=True)) + + +def downgrade() -> None: + op.drop_column("users", "last_reminder_sent_at") + op.drop_column("users", "reminder_timezone") + op.drop_column("users", "reminder_minute") + op.drop_column("users", "reminder_hour") + op.drop_column("users", "reminder_enabled") diff --git a/sakura_cloud/migrations/versions/0005_mobile_query_indexes.py b/sakura_cloud/migrations/versions/0005_mobile_query_indexes.py new file mode 100644 index 0000000..2760bf3 --- /dev/null +++ b/sakura_cloud/migrations/versions/0005_mobile_query_indexes.py @@ -0,0 +1,52 @@ +"""add mobile query composite indexes + +Revision ID: 0005_mobile_query_indexes +Revises: 0004_daily_reminder_preferences +Create Date: 2026-06-06 +""" + +from __future__ import annotations + +from alembic import op + + +revision = "0005_mobile_query_indexes" +down_revision = "0004_daily_reminder_preferences" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_index("ix_artworks_user_content_viewed", "artworks", ["user_id", "content_type", "last_viewed_at"]) + op.create_index("ix_artworks_user_created", "artworks", ["user_id", "created_at"]) + op.create_index("ix_artworks_user_favorite_viewed", "artworks", ["user_id", "is_favorite", "last_viewed_at"]) + + op.create_index("ix_novels_user_viewed", "novels", ["user_id", "last_viewed_at"]) + op.create_index("ix_novels_user_read", "novels", ["user_id", "last_read_at"]) + op.create_index("ix_novels_user_series_chapter", "novels", ["user_id", "series_id", "chapter_order"]) + op.create_index("ix_novels_user_favorite_viewed", "novels", ["user_id", "is_favorite", "last_viewed_at"]) + + op.create_index("ix_creator_follows_user_enabled_updated", "creator_follows", ["user_id", "enabled", "updated_at"]) + op.create_index("ix_creator_follows_user_platform_creator", "creator_follows", ["user_id", "platform", "creator_id"]) + + op.create_index("ix_update_alerts_user_read_created", "update_alerts", ["user_id", "is_read", "created_at"]) + op.create_index("ix_update_alerts_user_created", "update_alerts", ["user_id", "created_at"]) + op.create_index("ix_update_alerts_user_platform_creator", "update_alerts", ["user_id", "platform", "creator_id"]) + + +def downgrade() -> None: + op.drop_index("ix_update_alerts_user_platform_creator", table_name="update_alerts") + op.drop_index("ix_update_alerts_user_created", table_name="update_alerts") + op.drop_index("ix_update_alerts_user_read_created", table_name="update_alerts") + + op.drop_index("ix_creator_follows_user_platform_creator", table_name="creator_follows") + op.drop_index("ix_creator_follows_user_enabled_updated", table_name="creator_follows") + + op.drop_index("ix_novels_user_favorite_viewed", table_name="novels") + op.drop_index("ix_novels_user_series_chapter", table_name="novels") + op.drop_index("ix_novels_user_read", table_name="novels") + op.drop_index("ix_novels_user_viewed", table_name="novels") + + op.drop_index("ix_artworks_user_favorite_viewed", table_name="artworks") + op.drop_index("ix_artworks_user_created", table_name="artworks") + op.drop_index("ix_artworks_user_content_viewed", table_name="artworks") diff --git a/sakura_cloud/migrations/versions/0006_push_device_tokens.py b/sakura_cloud/migrations/versions/0006_push_device_tokens.py new file mode 100644 index 0000000..23aa347 --- /dev/null +++ b/sakura_cloud/migrations/versions/0006_push_device_tokens.py @@ -0,0 +1,43 @@ +"""add push device tokens + +Revision ID: 0006_push_device_tokens +Revises: 0005_mobile_query_indexes +Create Date: 2026-06-06 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + + +revision = "0006_push_device_tokens" +down_revision = "0005_mobile_query_indexes" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "push_device_tokens", + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("device_id", sa.String(length=128), nullable=False), + sa.Column("platform", sa.String(length=20), nullable=False), + sa.Column("fcm_token", sa.Text(), nullable=False), + sa.Column("is_active", sa.Boolean(), nullable=False), + sa.Column("last_seen_at", sa.DateTime(timezone=True), nullable=True), + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("user_id", "device_id", name="uq_push_device_tokens_user_device"), + ) + op.create_index("ix_push_device_tokens_is_active", "push_device_tokens", ["is_active"]) + op.create_index("ix_push_device_tokens_user_active", "push_device_tokens", ["user_id", "is_active"]) + + +def downgrade() -> None: + op.drop_index("ix_push_device_tokens_user_active", table_name="push_device_tokens") + op.drop_index("ix_push_device_tokens_is_active", table_name="push_device_tokens") + op.drop_table("push_device_tokens") diff --git a/sakura_cloud/migrations/versions/0007_artwork_ocr_texts.py b/sakura_cloud/migrations/versions/0007_artwork_ocr_texts.py new file mode 100644 index 0000000..a43f826 --- /dev/null +++ b/sakura_cloud/migrations/versions/0007_artwork_ocr_texts.py @@ -0,0 +1,60 @@ +"""add artwork ocr texts + +Revision ID: 0007_artwork_ocr_texts +Revises: 0006_push_device_tokens +Create Date: 2026-06-06 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + + +revision = "0007_artwork_ocr_texts" +down_revision = "0006_push_device_tokens" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "artwork_ocr_texts", + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("artwork_id", sa.String(length=36), nullable=False), + sa.Column("file_id", sa.String(length=36), nullable=True), + sa.Column("source_platform", sa.String(length=20), nullable=True), + sa.Column("source_id", sa.String(length=50), nullable=True), + sa.Column("language", sa.String(length=20), nullable=False), + sa.Column("ocr_text", sa.Text(), nullable=False), + sa.Column("confidence", sa.Float(), nullable=True), + sa.Column("engine", sa.String(length=50), nullable=False), + sa.Column("status", sa.String(length=20), nullable=False), + sa.Column("attempts", sa.Integer(), nullable=False), + sa.Column("error", sa.Text(), nullable=True), + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(["artwork_id"], ["artworks.id"], ondelete="CASCADE"), + sa.ForeignKeyConstraint(["file_id"], ["user_files.id"], ondelete="SET NULL"), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("user_id", "artwork_id", name="uq_artwork_ocr_texts_user_artwork"), + ) + op.create_index("ix_artwork_ocr_texts_user_artwork", "artwork_ocr_texts", ["user_id", "artwork_id"]) + op.create_index("ix_artwork_ocr_texts_user_status", "artwork_ocr_texts", ["user_id", "status"]) + op.create_index("ix_artwork_ocr_texts_user_source", "artwork_ocr_texts", ["user_id", "source_platform", "source_id"]) + if op.get_bind().dialect.name == "postgresql": + op.execute( + "CREATE INDEX ix_artwork_ocr_texts_ocr_text_gin " + "ON artwork_ocr_texts USING gin (to_tsvector('simple', coalesce(ocr_text, '')))" + ) + + +def downgrade() -> None: + if op.get_bind().dialect.name == "postgresql": + op.execute("DROP INDEX IF EXISTS ix_artwork_ocr_texts_ocr_text_gin") + op.drop_index("ix_artwork_ocr_texts_user_source", table_name="artwork_ocr_texts") + op.drop_index("ix_artwork_ocr_texts_user_status", table_name="artwork_ocr_texts") + op.drop_index("ix_artwork_ocr_texts_user_artwork", table_name="artwork_ocr_texts") + op.drop_table("artwork_ocr_texts") diff --git a/sakura_cloud/migrations/versions/0008_admin_audit_and_user_active.py b/sakura_cloud/migrations/versions/0008_admin_audit_and_user_active.py new file mode 100644 index 0000000..6032090 --- /dev/null +++ b/sakura_cloud/migrations/versions/0008_admin_audit_and_user_active.py @@ -0,0 +1,53 @@ +"""add admin audit logs and user active role support + +Revision ID: 0008_admin_audit_user_active +Revises: 0007_artwork_ocr_texts +Create Date: 2026-06-06 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + + +revision = "0008_admin_audit_user_active" +down_revision = "0007_artwork_ocr_texts" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + bind = op.get_bind() + if bind.dialect.name == "postgresql": + op.execute("ALTER TYPE userrole ADD VALUE IF NOT EXISTS 'SUPER_ADMIN'") + op.add_column("users", sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true())) + op.create_index("ix_users_is_active", "users", ["is_active"]) + op.create_table( + "admin_audit_logs", + sa.Column("admin_user_id", sa.String(length=36), nullable=True), + sa.Column("action", sa.String(length=100), nullable=False), + sa.Column("target_type", sa.String(length=50), nullable=True), + sa.Column("target_id", sa.String(length=100), nullable=True), + sa.Column("ip_address", sa.String(length=64), nullable=True), + sa.Column("user_agent", sa.Text(), nullable=True), + sa.Column("detail_json", sa.JSON(), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("id", sa.String(length=36), nullable=False), + sa.ForeignKeyConstraint(["admin_user_id"], ["users.id"], ondelete="SET NULL"), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index("ix_admin_audit_logs_action", "admin_audit_logs", ["action"]) + op.create_index("ix_admin_audit_logs_admin_created", "admin_audit_logs", ["admin_user_id", "created_at"]) + op.create_index("ix_admin_audit_logs_action_created", "admin_audit_logs", ["action", "created_at"]) + op.create_index("ix_admin_audit_logs_target", "admin_audit_logs", ["target_type", "target_id"]) + + +def downgrade() -> None: + op.drop_index("ix_admin_audit_logs_target", table_name="admin_audit_logs") + op.drop_index("ix_admin_audit_logs_action_created", table_name="admin_audit_logs") + op.drop_index("ix_admin_audit_logs_admin_created", table_name="admin_audit_logs") + op.drop_index("ix_admin_audit_logs_action", table_name="admin_audit_logs") + op.drop_table("admin_audit_logs") + op.drop_index("ix_users_is_active", table_name="users") + op.drop_column("users", "is_active") diff --git a/sakura_cloud/migrations/versions/0009_web_admin_hardening.py b/sakura_cloud/migrations/versions/0009_web_admin_hardening.py new file mode 100644 index 0000000..e004595 --- /dev/null +++ b/sakura_cloud/migrations/versions/0009_web_admin_hardening.py @@ -0,0 +1,29 @@ +"""add web admin hardening user fields + +Revision ID: 0009_web_admin_hardening +Revises: 0008_admin_audit_user_active +Create Date: 2026-06-06 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + + +revision = "0009_web_admin_hardening" +down_revision = "0008_admin_audit_user_active" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.add_column("users", sa.Column("disabled_at", sa.DateTime(timezone=True), nullable=True)) + op.add_column("users", sa.Column("disabled_reason", sa.Text(), nullable=True)) + op.add_column("users", sa.Column("token_version", sa.Integer(), nullable=False, server_default="0")) + + +def downgrade() -> None: + op.drop_column("users", "token_version") + op.drop_column("users", "disabled_reason") + op.drop_column("users", "disabled_at") diff --git a/sakura_cloud/migrations/versions/0010_gallery_feed_indexes.py b/sakura_cloud/migrations/versions/0010_gallery_feed_indexes.py new file mode 100644 index 0000000..2207624 --- /dev/null +++ b/sakura_cloud/migrations/versions/0010_gallery_feed_indexes.py @@ -0,0 +1,24 @@ +"""add gallery feed updated index + +Revision ID: 0010_gallery_feed_indexes +Revises: 0009_web_admin_hardening +Create Date: 2026-06-14 +""" + +from __future__ import annotations + +from alembic import op + + +revision = "0010_gallery_feed_indexes" +down_revision = "0009_web_admin_hardening" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_index("ix_artworks_user_updated", "artworks", ["user_id", "updated_at"]) + + +def downgrade() -> None: + op.drop_index("ix_artworks_user_updated", table_name="artworks") diff --git a/sakura_cloud/migrations/versions/0011_feed_cache.py b/sakura_cloud/migrations/versions/0011_feed_cache.py new file mode 100644 index 0000000..d2f721b --- /dev/null +++ b/sakura_cloud/migrations/versions/0011_feed_cache.py @@ -0,0 +1,39 @@ +"""add feed cache table + +Revision ID: 0011_feed_cache +Revises: 0010_gallery_feed_indexes +Create Date: 2026-06-15 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + + +revision = "0011_feed_cache" +down_revision = "0010_gallery_feed_indexes" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "feed_cache", + sa.Column("user_id", sa.String(length=36), nullable=False), + sa.Column("cache_key", sa.String(length=100), nullable=False), + sa.Column("payload", sa.JSON(), nullable=False), + sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("user_id", "cache_key", name="uq_feed_cache_user_key"), + ) + op.create_index("ix_feed_cache_user_expires", "feed_cache", ["user_id", "expires_at"]) + + +def downgrade() -> None: + op.drop_index("ix_feed_cache_user_expires", table_name="feed_cache") + op.drop_table("feed_cache") diff --git a/sakura_cloud/migrations/versions/0012_p2_query_indexes.py b/sakura_cloud/migrations/versions/0012_p2_query_indexes.py new file mode 100644 index 0000000..7337c27 --- /dev/null +++ b/sakura_cloud/migrations/versions/0012_p2_query_indexes.py @@ -0,0 +1,40 @@ +"""add p2 query performance indexes + +Revision ID: 0012_p2_query_indexes +Revises: 0011_feed_cache +Create Date: 2026-06-15 +""" + +from __future__ import annotations + +from alembic import op + + +revision = "0012_p2_query_indexes" +down_revision = "0011_feed_cache" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.execute("CREATE INDEX IF NOT EXISTS ix_artworks_user_created_desc ON artworks (user_id, created_at DESC)") + op.execute("CREATE INDEX IF NOT EXISTS ix_artworks_user_updated_desc ON artworks (user_id, updated_at DESC)") + op.execute( + "CREATE INDEX IF NOT EXISTS ix_artworks_user_content_created_desc " + "ON artworks (user_id, content_type, created_at DESC)" + ) + op.execute( + "CREATE INDEX IF NOT EXISTS ix_artworks_user_content_updated_desc " + "ON artworks (user_id, content_type, updated_at DESC)" + ) + op.execute("CREATE INDEX IF NOT EXISTS ix_novels_user_updated_desc ON novels (user_id, updated_at DESC)") + op.execute("CREATE INDEX IF NOT EXISTS ix_novels_series_chapter_order ON novels (series_id, chapter_order)") + + +def downgrade() -> None: + op.execute("DROP INDEX IF EXISTS ix_novels_series_chapter_order") + op.execute("DROP INDEX IF EXISTS ix_novels_user_updated_desc") + op.execute("DROP INDEX IF EXISTS ix_artworks_user_content_updated_desc") + op.execute("DROP INDEX IF EXISTS ix_artworks_user_content_created_desc") + op.execute("DROP INDEX IF EXISTS ix_artworks_user_updated_desc") + op.execute("DROP INDEX IF EXISTS ix_artworks_user_created_desc") diff --git a/sakura_cloud/models/__init__.py b/sakura_cloud/models/__init__.py new file mode 100644 index 0000000..8a8d335 --- /dev/null +++ b/sakura_cloud/models/__init__.py @@ -0,0 +1,26 @@ +from sakura_cloud.models.admin import AdminAuditLog +from sakura_cloud.models.artwork import Artwork +from sakura_cloud.models.device import Device +from sakura_cloud.models.file import FileBlob, UploadSession, UserFile +from sakura_cloud.models.follow import CreatorFollow, UpdateAlert +from sakura_cloud.models.feed_cache import FeedCache +from sakura_cloud.models.novel import Novel +from sakura_cloud.models.ocr import ArtworkOcrText +from sakura_cloud.models.push import PushDeviceToken +from sakura_cloud.models.user import User + +__all__ = [ + "Artwork", + "ArtworkOcrText", + "AdminAuditLog", + "CreatorFollow", + "Device", + "FileBlob", + "FeedCache", + "Novel", + "PushDeviceToken", + "UpdateAlert", + "UploadSession", + "User", + "UserFile", +] diff --git a/sakura_cloud/models/admin.py b/sakura_cloud/models/admin.py new file mode 100644 index 0000000..b8b633e --- /dev/null +++ b/sakura_cloud/models/admin.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import DateTime, ForeignKey, Index, JSON, String, Text +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import UUIDPrimaryKeyMixin, utc_now + + +class AdminAuditLog(UUIDPrimaryKeyMixin, Base): + __tablename__ = "admin_audit_logs" + __table_args__ = ( + Index("ix_admin_audit_logs_admin_created", "admin_user_id", "created_at"), + Index("ix_admin_audit_logs_action_created", "action", "created_at"), + Index("ix_admin_audit_logs_target", "target_type", "target_id"), + ) + + admin_user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="SET NULL"), nullable=True) + action: Mapped[str] = mapped_column(String(100), nullable=False, index=True) + target_type: Mapped[str | None] = mapped_column(String(50), nullable=True) + target_id: Mapped[str | None] = mapped_column(String(100), nullable=True) + ip_address: Mapped[str | None] = mapped_column(String(64), nullable=True) + user_agent: Mapped[str | None] = mapped_column(Text, nullable=True) + detail_json: Mapped[dict] = mapped_column("detail_json", JSON, default=dict, nullable=False) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False) + + admin_user = relationship("User") diff --git a/sakura_cloud/models/artwork.py b/sakura_cloud/models/artwork.py new file mode 100644 index 0000000..1cfd807 --- /dev/null +++ b/sakura_cloud/models/artwork.py @@ -0,0 +1,63 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, Index, Integer, JSON, String, Text +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class Artwork(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "artworks" + __table_args__ = ( + Index("ix_artworks_user_content_viewed", "user_id", "content_type", "last_viewed_at"), + Index("ix_artworks_user_created", "user_id", "created_at"), + Index("ix_artworks_user_created_desc", "user_id", "created_at"), + Index("ix_artworks_user_updated", "user_id", "updated_at"), + Index("ix_artworks_user_updated_desc", "user_id", "updated_at"), + Index("ix_artworks_user_content_created_desc", "user_id", "content_type", "created_at"), + Index("ix_artworks_user_content_updated_desc", "user_id", "content_type", "updated_at"), + Index("ix_artworks_user_favorite_viewed", "user_id", "is_favorite", "last_viewed_at"), + ) + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + file_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("user_files.id", ondelete="SET NULL"), nullable=True) + source_id: Mapped[str | None] = mapped_column(String(50), nullable=True) + source_platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True) + content_type: Mapped[str] = mapped_column(String(20), default="illust", nullable=False, index=True) + title: Mapped[str] = mapped_column(String(500), nullable=False) + author: Mapped[str | None] = mapped_column(String(200), nullable=True, index=True) + author_id: Mapped[str | None] = mapped_column(String(50), nullable=True) + page_count: Mapped[int] = mapped_column(Integer, default=1, nullable=False) + file_path: Mapped[str] = mapped_column(Text, nullable=False) + file_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False) + thumbnail_path: Mapped[str | None] = mapped_column(Text, nullable=True) + mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True) + width: Mapped[int | None] = mapped_column(Integer, nullable=True) + height: Mapped[int | None] = mapped_column(Integer, nullable=True) + tags: Mapped[list[str]] = mapped_column(JSON, default=list, nullable=False) + is_favorite: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True) + artwork_metadata: Mapped[dict] = mapped_column("metadata", JSON, default=dict, nullable=False) + last_viewed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True) + + user = relationship("User", back_populates="artworks") + file = relationship("UserFile") + ocr_text = relationship("ArtworkOcrText", back_populates="artwork", cascade="all, delete-orphan", uselist=False) + + @property + def image_url(self) -> str | None: + if self.file_id: + return f"/api/v1/files/{self.file_id}/content" + if self.file_path.startswith(("http://", "https://")): + return self.file_path + return None + + @property + def thumbnail_url(self) -> str | None: + if self.file_id: + return f"/api/v1/files/{self.file_id}/thumbnail" + if self.thumbnail_path and self.thumbnail_path.startswith(("http://", "https://")): + return self.thumbnail_path + return None diff --git a/sakura_cloud/models/device.py b/sakura_cloud/models/device.py new file mode 100644 index 0000000..fded500 --- /dev/null +++ b/sakura_cloud/models/device.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import DateTime, ForeignKey, Index, String +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class Device(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "devices" + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + device_name: Mapped[str] = mapped_column(String(100), nullable=False) + device_type: Mapped[str] = mapped_column(String(20), nullable=False) + device_token: Mapped[str | None] = mapped_column(String(255), nullable=True) + last_active_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + last_sync_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + + user = relationship("User", back_populates="devices") + + __table_args__ = ( + Index("idx_devices_user_id", "user_id"), + Index("idx_devices_last_active", "last_active_at"), + ) diff --git a/sakura_cloud/models/feed_cache.py b/sakura_cloud/models/feed_cache.py new file mode 100644 index 0000000..0d2f858 --- /dev/null +++ b/sakura_cloud/models/feed_cache.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import DateTime, ForeignKey, Index, JSON, String, UniqueConstraint +from sqlalchemy.orm import Mapped, mapped_column + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class FeedCache(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "feed_cache" + __table_args__ = ( + UniqueConstraint("user_id", "cache_key", name="uq_feed_cache_user_key"), + Index("ix_feed_cache_user_expires", "user_id", "expires_at"), + ) + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + cache_key: Mapped[str] = mapped_column(String(100), nullable=False) + payload: Mapped[list[dict]] = mapped_column(JSON, default=list, nullable=False) + expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) diff --git a/sakura_cloud/models/file.py b/sakura_cloud/models/file.py new file mode 100644 index 0000000..1b5b7ba --- /dev/null +++ b/sakura_cloud/models/file.py @@ -0,0 +1,55 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import BigInteger, DateTime, ForeignKey, Index, String, Text +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin, utc_now + + +class FileBlob(Base): + __tablename__ = "file_blobs" + + sha256: Mapped[str] = mapped_column(String(64), primary_key=True) + storage_path: Mapped[str] = mapped_column(Text, nullable=False) + thumbnail_path: Mapped[str | None] = mapped_column(Text, nullable=True) + file_size: Mapped[int] = mapped_column(BigInteger, nullable=False) + mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False) + + user_files = relationship("UserFile", back_populates="blob") + + +class UserFile(UUIDPrimaryKeyMixin, Base): + __tablename__ = "user_files" + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + file_sha256: Mapped[str] = mapped_column(String(64), ForeignKey("file_blobs.sha256", ondelete="CASCADE"), nullable=False) + original_name: Mapped[str] = mapped_column(String(255), nullable=False) + uploaded_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False) + + user = relationship("User", back_populates="files") + blob = relationship("FileBlob", back_populates="user_files") + + __table_args__ = (Index("idx_user_files_user_id", "user_id"),) + + +class UploadSession(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "upload_sessions" + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + sha256: Mapped[str] = mapped_column(String(64), nullable=False, index=True) + file_name: Mapped[str] = mapped_column(String(255), nullable=False) + file_size: Mapped[int] = mapped_column(BigInteger, nullable=False) + mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True) + resource_type: Mapped[str] = mapped_column(String(20), default="image", nullable=False) + chunk_size: Mapped[int] = mapped_column(BigInteger, nullable=False) + received_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False) + temp_path: Mapped[str] = mapped_column(Text, nullable=False) + status: Mapped[str] = mapped_column(String(20), default="pending", nullable=False) + + user = relationship("User") + + __table_args__ = (Index("idx_upload_sessions_user_id", "user_id"),) diff --git a/sakura_cloud/models/follow.py b/sakura_cloud/models/follow.py new file mode 100644 index 0000000..a1f0939 --- /dev/null +++ b/sakura_cloud/models/follow.py @@ -0,0 +1,60 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import Boolean, DateTime, ForeignKey, Index, JSON, String, Text +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class CreatorFollow(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "creator_follows" + __table_args__ = ( + Index("ix_creator_follows_user_enabled_updated", "user_id", "enabled", "updated_at"), + Index("ix_creator_follows_user_platform_creator", "user_id", "platform", "creator_id"), + ) + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) + platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True) + creator_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True) + creator_name: Mapped[str | None] = mapped_column(String(200), nullable=True) + creator_handle: Mapped[str | None] = mapped_column(String(200), nullable=True) + profile_url: Mapped[str | None] = mapped_column(Text, nullable=True) + avatar_url: Mapped[str | None] = mapped_column(Text, nullable=True) + enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False, index=True) + last_checked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + latest_published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + metadata_json: Mapped[dict] = mapped_column("metadata", JSON, default=dict, nullable=False) + + user = relationship("User", back_populates="creator_follows") + + +class UpdateAlert(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "update_alerts" + __table_args__ = ( + Index("ix_update_alerts_user_read_created", "user_id", "is_read", "created_at"), + Index("ix_update_alerts_user_created", "user_id", "created_at"), + Index("ix_update_alerts_user_platform_creator", "user_id", "platform", "creator_id"), + ) + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) + follow_id: Mapped[str | None] = mapped_column( + String(36), + ForeignKey("creator_follows.id", ondelete="SET NULL"), + nullable=True, + index=True, + ) + platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True) + creator_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True) + title: Mapped[str] = mapped_column(String(500), nullable=False) + body: Mapped[str | None] = mapped_column(Text, nullable=True) + content_type: Mapped[str | None] = mapped_column(String(20), nullable=True) + target_url: Mapped[str | None] = mapped_column(Text, nullable=True) + cover_url: Mapped[str | None] = mapped_column(Text, nullable=True) + is_read: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True) + payload_json: Mapped[dict] = mapped_column("payload", JSON, default=dict, nullable=False) + + user = relationship("User", back_populates="update_alerts") + follow = relationship("CreatorFollow") diff --git a/sakura_cloud/models/mixins.py b/sakura_cloud/models/mixins.py new file mode 100644 index 0000000..0efcdf6 --- /dev/null +++ b/sakura_cloud/models/mixins.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +import uuid +from datetime import datetime, timezone + +from sqlalchemy import DateTime, String +from sqlalchemy.orm import Mapped, mapped_column + + +def new_uuid() -> str: + return str(uuid.uuid4()) + + +def utc_now() -> datetime: + return datetime.now(timezone.utc) + + +class UUIDPrimaryKeyMixin: + id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid) + + +class TimestampMixin: + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False) + updated_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + onupdate=utc_now, + nullable=False, + ) diff --git a/sakura_cloud/models/novel.py b/sakura_cloud/models/novel.py new file mode 100644 index 0000000..9f0a1e0 --- /dev/null +++ b/sakura_cloud/models/novel.py @@ -0,0 +1,50 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, Index, Integer, JSON, String, Text +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class Novel(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "novels" + __table_args__ = ( + Index("ix_novels_user_viewed", "user_id", "last_viewed_at"), + Index("ix_novels_user_read", "user_id", "last_read_at"), + Index("ix_novels_user_series_chapter", "user_id", "series_id", "chapter_order"), + Index("ix_novels_user_updated_desc", "user_id", "updated_at"), + Index("ix_novels_series_chapter_order", "series_id", "chapter_order"), + Index("ix_novels_user_favorite_viewed", "user_id", "is_favorite", "last_viewed_at"), + ) + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + file_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("user_files.id", ondelete="SET NULL"), nullable=True) + source_id: Mapped[str | None] = mapped_column(String(50), nullable=True) + source_platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True) + title: Mapped[str] = mapped_column(String(500), nullable=False) + author: Mapped[str | None] = mapped_column(String(200), nullable=True, index=True) + series_id: Mapped[str | None] = mapped_column(String(50), nullable=True, index=True) + series_title: Mapped[str | None] = mapped_column(String(500), nullable=True) + chapter_title: Mapped[str | None] = mapped_column(String(500), nullable=True) + chapter_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False) + description: Mapped[str | None] = mapped_column(Text, nullable=True) + file_path: Mapped[str] = mapped_column(Text, nullable=False) + file_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False) + cover_path: Mapped[str | None] = mapped_column(Text, nullable=True) + word_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False) + current_page: Mapped[int] = mapped_column(Integer, default=0, nullable=False) + bookmark: Mapped[str | None] = mapped_column(Text, nullable=True) + tags: Mapped[list[str]] = mapped_column(JSON, default=list, nullable=False) + is_favorite: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True) + last_read_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + last_viewed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True) + + user = relationship("User", back_populates="novels") + file = relationship("UserFile") + + @property + def content_url(self) -> str | None: + return f"/api/v1/files/{self.file_id}/content" if self.file_id else None diff --git a/sakura_cloud/models/ocr.py b/sakura_cloud/models/ocr.py new file mode 100644 index 0000000..71d7c07 --- /dev/null +++ b/sakura_cloud/models/ocr.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +from sqlalchemy import Float, ForeignKey, Index, Integer, String, Text, UniqueConstraint +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class ArtworkOcrText(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "artwork_ocr_texts" + __table_args__ = ( + UniqueConstraint("user_id", "artwork_id", name="uq_artwork_ocr_texts_user_artwork"), + Index("ix_artwork_ocr_texts_user_artwork", "user_id", "artwork_id"), + Index("ix_artwork_ocr_texts_user_status", "user_id", "status"), + Index("ix_artwork_ocr_texts_user_source", "user_id", "source_platform", "source_id"), + ) + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + artwork_id: Mapped[str] = mapped_column(String(36), ForeignKey("artworks.id", ondelete="CASCADE"), nullable=False) + file_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("user_files.id", ondelete="SET NULL"), nullable=True) + source_platform: Mapped[str | None] = mapped_column(String(20), nullable=True) + source_id: Mapped[str | None] = mapped_column(String(50), nullable=True) + language: Mapped[str] = mapped_column(String(20), default="mixed", nullable=False) + ocr_text: Mapped[str] = mapped_column(Text, default="", nullable=False) + confidence: Mapped[float | None] = mapped_column(Float, nullable=True) + engine: Mapped[str] = mapped_column(String(50), default="paddleocr", nullable=False) + status: Mapped[str] = mapped_column(String(20), default="pending", nullable=False, index=True) + attempts: Mapped[int] = mapped_column(Integer, default=0, nullable=False) + error: Mapped[str | None] = mapped_column(Text, nullable=True) + + artwork = relationship("Artwork", back_populates="ocr_text") + user = relationship("User", back_populates="artwork_ocr_texts") diff --git a/sakura_cloud/models/push.py b/sakura_cloud/models/push.py new file mode 100644 index 0000000..554042b --- /dev/null +++ b/sakura_cloud/models/push.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import Boolean, DateTime, ForeignKey, Index, String, Text, UniqueConstraint +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class PushDeviceToken(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "push_device_tokens" + __table_args__ = ( + UniqueConstraint("user_id", "device_id", name="uq_push_device_tokens_user_device"), + Index("ix_push_device_tokens_user_active", "user_id", "is_active"), + ) + + user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + device_id: Mapped[str] = mapped_column(String(128), nullable=False) + platform: Mapped[str] = mapped_column(String(20), nullable=False) + fcm_token: Mapped[str] = mapped_column(Text, nullable=False) + is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False, index=True) + last_seen_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + + user = relationship("User", back_populates="push_device_tokens") diff --git a/sakura_cloud/models/user.py b/sakura_cloud/models/user.py new file mode 100644 index 0000000..3a9fff2 --- /dev/null +++ b/sakura_cloud/models/user.py @@ -0,0 +1,56 @@ +from __future__ import annotations + +from datetime import datetime +from enum import Enum + +from sqlalchemy import BigInteger, DateTime, Enum as SqlEnum, Index, String, Text +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from sakura_cloud.core.config import settings +from sakura_cloud.db.base import Base +from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin + + +class UserRole(str, Enum): + ADMIN = "admin" + SUPER_ADMIN = "super_admin" + USER = "user" + + +class User(UUIDPrimaryKeyMixin, TimestampMixin, Base): + __tablename__ = "users" + + username: Mapped[str] = mapped_column(String(50), nullable=False, unique=True) + email: Mapped[str] = mapped_column(String(100), nullable=False, unique=True) + password_hash: Mapped[str] = mapped_column(String(255), nullable=False) + avatar: Mapped[str | None] = mapped_column(Text, nullable=True) + storage_used: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False) + storage_limit: Mapped[int] = mapped_column( + BigInteger, + default=lambda: settings.default_storage_limit_bytes, + nullable=False, + ) + role: Mapped[UserRole] = mapped_column(SqlEnum(UserRole), default=UserRole.USER, nullable=False) + is_active: Mapped[bool] = mapped_column(default=True, nullable=False, index=True) + disabled_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + disabled_reason: Mapped[str | None] = mapped_column(Text, nullable=True) + token_version: Mapped[int] = mapped_column(default=0, nullable=False) + reminder_enabled: Mapped[bool] = mapped_column(default=True, nullable=False) + reminder_hour: Mapped[int] = mapped_column(default=9, nullable=False) + reminder_minute: Mapped[int] = mapped_column(default=0, nullable=False) + reminder_timezone: Mapped[str] = mapped_column(String(64), default="Asia/Shanghai", nullable=False) + last_reminder_sent_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) + + artworks = relationship("Artwork", back_populates="user", cascade="all, delete-orphan") + novels = relationship("Novel", back_populates="user", cascade="all, delete-orphan") + devices = relationship("Device", back_populates="user", cascade="all, delete-orphan") + files = relationship("UserFile", back_populates="user", cascade="all, delete-orphan") + creator_follows = relationship("CreatorFollow", back_populates="user", cascade="all, delete-orphan") + update_alerts = relationship("UpdateAlert", back_populates="user", cascade="all, delete-orphan") + push_device_tokens = relationship("PushDeviceToken", back_populates="user", cascade="all, delete-orphan") + artwork_ocr_texts = relationship("ArtworkOcrText", back_populates="user", cascade="all, delete-orphan") + + __table_args__ = ( + Index("idx_users_username", "username"), + Index("idx_users_email", "email"), + ) diff --git a/sakura_cloud/public/sources/source-index.json b/sakura_cloud/public/sources/source-index.json new file mode 100644 index 0000000..1a0183f --- /dev/null +++ b/sakura_cloud/public/sources/source-index.json @@ -0,0 +1,12 @@ +[ + { + "name": "精选50书源", + "mode": "novel", + "url": "/sources/精选50书源.json" + }, + { + "name": "精选50漫画源", + "mode": "manga", + "url": "/sources/精选50漫画源.json" + } +] diff --git a/sakura_cloud/public/sources/精选50书源.json b/sakura_cloud/public/sources/精选50书源.json new file mode 100644 index 0000000..db903a1 --- /dev/null +++ b/sakura_cloud/public/sources/精选50书源.json @@ -0,0 +1,2605 @@ +[ + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "顶点小说", + "bookSourceType": 0, + "bookSourceUrl": "http://api.basusan.com", + "customOrder": 598, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "@js:\nvar sort=[];\npush=(title,url,type,size)=>sort.push({\n \"title\": title,\n \"url\": url?url:\"\",\n \"style\": {\n \"layout_flexGrow\": type,\n \"layout_flexBasisPercent\": size,\n }\n });\n\nbd=[\"必读榜\",\"潜力榜\",\"完本榜\",\n\"更新榜\",\"搜索榜\",\"评论榜\"];\npd=[[\"男频\",\"1\"],\n\t[\"女频\",\"2\"]].map([title,id]=>{\npush(title+'榜单', null, 1, 1);\n\t\tbd.map((title,index)=>{\t\t\t\n\t\t \turl= `/module/rank?type=${index+1}&channel=${id}&page={{page}}`; \n\t\t\t\tpush(title, url, 1, 0.25);\t\t\t\t\t\t\t\t\t\t\n\t\t\t});\n\t});\n \nbook=[[\"男频\",\n[[\"玄幻\",\"lejRej\"],[\"都市\",\"mbk5ez\"],[\"武侠\",\"nel5aK\"],[\"仙侠\",\"vbmOeY\"],[\"军事\",\"penRe7\"],[\"历史\",\"xbojag\"],[\"游戏\",\"mep2bM\"],[\"科幻\",\"zbq2dp\"],[\"轻小说\",\"YerEdO\"]]],\n\t [\"女频\",\n[[\"现代言情\",\"9avmeG\"],[\"古代言情\",\"DdwRb1\"],[\"幻想言情\",\"7ax9by\"],[\"青春校园\",\"Pdy7aQ\"],[\"唯美纯爱\",\"kazYeJ\"],[\"同人衍生\",\"9aAOdv\"]]]].map([tit1,category]=>{\n\t \t\t[[\"连载\",\"0\"],[\"完结\",\"1\"]].map([tit2,id]=>{ \n\t push(tit1+'•'+tit2, null, 1, 1); \n category.map([title,cid]=>{\t \n\turl= `/novel?isComplete=${id}&sort=1&page={{page}}&categoryId=${cid}`;\t\n\t\t\t \tpush(title, url, 1, 0.25);\n });\n });\n });\n\nJSON.stringify(sort)", + "header": "@js:\nvar uid=java.randomUUID(),\nsid=String(uid).replace(/\\-/g,\"\");\nhead={\n\t \"client-device\": sid,\n \"client-brand\": \"vivo\",\n \"client-version\": \"1.0.0\",\n \"client-channel\": \"android\",\n \"client-name\": \"app.maoyankanshu.novel\",\n \"alias-name\": \"maoyankanshu\",\n \"client-source\": \"android\",\n \"Authorization\": \"bearereyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGkuYW53YWJlbi5jb21cL2F1dGhcL3RoaXJkIiwiaWF0IjoxNzA0OTc3ODY5LCJleHAiOjE3OTgyODk4NjksIm5iZiI6MTcwNDk3Nzg2OSwianRpIjoiSG9IcHpNbzNxa1hvYWxYUCIsInN1YiI6OTc5MjYsInBydiI6ImExY2IwMzcxODAyOTZjNmExOTM4ZWYzMGI0Mzc5NDY3MmRkMDE2YzUifQ.Q21n8Tv71pyScM6HsKsiPuPsSXukqBKNm7GObPdR8q4\",\n \"User-Agent\": \"okhttp/4.9.2\"\n };\nJSON.stringify(head)", + "lastUpdateTime": 1732515832869, + "respondTime": 2787, + "ruleBookInfo": { + "author": "$.authorName", + "coverUrl": "$.cover", + "init": "$.data", + "intro": "$.summary", + "kind": "$..className&&$.lastUpdatedAt", + "lastChapter": "$.lastChapter.chapterName", + "name": "$.novelName", + "tocUrl": "http://api.basusan.com/novel/{{$.novelId}}/chapters", + "wordCount": "$.wordNum" + }, + "ruleContent": { + "content": "$.content" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "$.authorName", + "bookList": "$.data[*]", + "bookUrl": "http://api.basusan.com/novel/{{$.novelId}}?isSearch=1", + "coverUrl": "$.cover", + "intro": "$.summary", + "kind": "$..className&&$.createdAt", + "name": "$.novelName", + "wordCount": "$.wordNum" + }, + "ruleToc": { + "chapterList": "$.data.list.*", + "chapterName": "$.chapterName", + "chapterUrl": "$.path\n@js:\nvar data=String(result),\ntype=\"AES/CBC/PKCS5Padding\",\nkey=\"f041c49714d39908\",\niv=\"0123456789abcdef\";\njava.createSymmetricCrypto(type,key,iv).decryptStr(data);", + "updateTime": "{{$.updatedAt}} • {{$.wordNum}}字" + }, + "searchUrl": "http://api.basusan.com/search?keyword={{key}}&page={{page}}", + "weight": 100 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.xbiqusa.com", + "bookUrlPattern": "", + "customOrder": 142, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[\n{\"title\":\"🔖全部🍁分类🔖\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"玄幻\",\"url\":\"/xclass/1/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"修真\",\"url\":\"/xclass/2/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"都市\",\"url\":\"/xclass/3/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"穿越\",\"url\":\"/xclass/4/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"网游\",\"url\":\"/xclass/5/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"科幻\",\"url\":\"/xclass/6/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"其它\",\"url\":\"/xclass/7/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"——\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"——\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},\n\n{\"title\":\"🔖榜单🔖\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"周榜\",\"url\":\"/ph/week_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"月榜\",\"url\":\"/ph/month_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"总榜\",\"url\":\"/ph/all_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},\n\n{\"title\":\"🔖完结🍁分类🔖\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"玄幻\",\"url\":\"/quanben1_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"修真\",\"url\":\"/quanben2_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"都市\",\"url\":\"/quanben3_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"穿越\",\"url\":\"/quanben4_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"网游\",\"url\":\"/quanben5_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"科幻\",\"url\":\"/quanben6_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"其它\",\"url\":\"/quanben7_{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"——\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"——\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}}\n]", + "header": "", + "lastUpdateTime": 0, + "loginUrl": "", + "respondTime": 10078, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "coverUrl": "[property=\"og:image\"]@content", + "init": "", + "intro": "🔔 更新时间:{{@@[property=\"og:novel:update_time\"]@content##\\s##🔸}}\n\n📂 内容简介:{{@@[property=\"og:description\"]@content}}##(^|[。!?]+[”」)】]?)##$1
", + "kind": "[property~=category|status|update_time]@content##\\s.*", + "lastChapter": "[property=\"og:novel:latest_chapter_name\"]@content", + "name": "[property=\"og:novel:book_name\"]@content", + "tocUrl": "", + "wordCount": "" + }, + "ruleContent": { + "content": "#content@html", + "imageStyle": "", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##^(.|\\n)*?最新网址:.*|最新网址:(.|\\n)*$", + "sourceRegex": "", + "webJs": "" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "lastChapter": "", + "name": "", + "wordCount": "" + }, + "ruleSearch": { + "author": ".author a.1@text", + "bookList": ".hot_sale", + "bookUrl": "a.0@href@js:##.+\\D((\\d+)\\d{3})\\D##https://www.xbiqusa.com/$2_$1/###", + "coverUrl": "a.0@href@js:##.+\\D((\\d+)\\d{3})\\D##https://www.xbiqusa.com/files/article/image/$2/$1/$1s.jpg###", + "intro": ".review@text##简介:", + "kind": "{{@@.author.1@text##\\s\\| .*}}{{@@.author.1@textNodes##.*:}}", + "lastChapter": ".author a.2@text", + "name": ".title@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "dd a", + "chapterName": "text", + "chapterUrl": "href", + "isVip": "", + "nextTocUrl": "", + "updateTime": "" + }, + "searchUrl": "https://m.xbiqusa.com/SearchBook.php?keyword={{key}}", + "weight": 2 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.yuyouge.com", + "bookUrlPattern": "", + "concurrentRate": "", + "coverDecodeJs": "", + "customOrder": 934, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreScreen": "", + "exploreUrl": "", + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36\"\n}", + "lastUpdateTime": 1721194277513, + "loginCheckJs": "", + "loginUi": "", + "loginUrl": "", + "respondTime": 10801, + "ruleBookInfo": { + "author": "class.info@tag.dd.0@tag.p.0@text##作者:", + "canReName": "", + "coverUrl": "class.img@img@src", + "init": "", + "intro": "class.summary.1@text", + "kind": "class.info@tag.dd.0@tag.p.1@a@text&&\nclass.info@tag.dd.0@tag.p.2@text##状态:", + "lastChapter": "class.info@tag.dd.0@tag.p.3@a@text", + "name": "class.info@tag.dt.0@tag.span.0@text", + "tocUrl": "text.开始阅读@href@js:\nr=result.match(/(\\d+)-/)\nid=r[1]\n'https://www.yuyouge.com/book/'+id+'/'", + "wordCount": "" + }, + "ruleContent": { + "content": "id.txtContent@html", + "imageStyle": "", + "nextContentUrl": "", + "replaceRegex": "", + "sourceRegex": "", + "webJs": "" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "lastChapter": "", + "name": "", + "wordCount": "" + }, + "ruleSearch": { + "author": "tag.a.0@hrefjava.ajax('https://m.yuyouge.com'+result)class.info@tag.dd.0@tag.p.0@text##作者:", + "bookList": "class.book_textList2@li", + "bookUrl": "tag.a.0@href##$##,{\"webView\":true}", + "coverUrl": "", + "intro": "", + "kind": "", + "lastChapter": "tag.a.1@text", + "name": "tag.a.0@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "id.chapters-list@li", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href@js:'https://www.yuyouge.com'+result", + "isVip": "", + "isVolume": "", + "nextTocUrl": "", + "updateTime": "" + }, + "searchUrl": "https://m.yuyouge.com/search.htm?keyword={{key}}&pn={{page}}", + "variableComment": "", + "weight": 2 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.biqukun.com", + "bookUrlPattern": "https://m.biqukun.com/\\d+/\\d+/", + "concurrentRate": "", + "coverDecodeJs": "", + "customOrder": 1162, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreScreen": "", + "exploreUrl": "[{\"title\":\"玄幻小说\",\"url\":\"/sort/1_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"修真小说\",\"url\":\"/sort/2_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"都市小说\",\"url\":\"/sort/3_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"穿越小说\",\"url\":\"/sort/4_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"网游小说\",\"url\":\"/sort/5_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"科幻小说\",\"url\":\"/sort/6_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}}]", + "header": "", + "lastUpdateTime": 0, + "loginCheckJs": "", + "loginUi": "", + "loginUrl": "", + "respondTime": 2058, + "ruleBookInfo": { + "author": "text.作者@text##作者:", + "canReName": "", + "coverUrl": ".block_img2 img@src", + "init": "", + "intro": ".intro_info@text##最新章节.*", + "kind": "text.状态@text##状态:", + "lastChapter": ".chapter a.1@text", + "name": "h2@text", + "tocUrl": "", + "wordCount": "text.更新@text##更新:|.*更新时间:.*" + }, + "ruleContent": { + "content": "#nr@html", + "imageStyle": "", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##第\\d+章.*|.*本章未完.*", + "sourceRegex": "", + "webJs": "" + }, + "ruleExplore": { + "author": "", + "bookList": ".xbk li", + "bookUrl": "a@href", + "coverUrl": "img@src", + "intro": "", + "kind": "", + "lastChapter": "", + "name": ".xsm@text", + "wordCount": ".tjrs@text" + }, + "ruleSearch": { + "author": "ownText##.*/|/", + "bookList": ".fk li", + "bookUrl": "a@href", + "coverUrl": "a@href##.*/(\\d+)/(\\d+)/##https://www.biqukun.com/d/image/$1/$2/$2s.jpg", + "intro": "", + "kind": "textNodes##/.*|\\[|\\]", + "lastChapter": "", + "name": "a@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": ".chapter a", + "chapterName": "text", + "chapterUrl": "href", + "isVip": "", + "isVolume": "", + "nextTocUrl": "option@value", + "updateTime": "" + }, + "searchUrl": "https://m.biqukun.com/search.php?s=4532499176958073452&submit=&type=articlename&searchkey={{key}}", + "variableComment": "", + "weight": 2 + }, + { + "bookSourceComment": "网站在墙内(无法登录,部分作者的文章看不到,需要可以fq访问esjzone.cc)", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco", + "bookSourceName": "ESJ Zone@zone", + "bookSourceType": 0, + "bookSourceUrl": "https://www.esjzone.one/", + "customOrder": 2, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"全部\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"最新更新\",\"url\":\"/list-01/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最新上架\",\"url\":\"/list-02/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最高评分\",\"url\":\"/list-03/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多观看\",\"url\":\"/list-04/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多文章\",\"url\":\"/list-05/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多讨论\",\"url\":\"/list-06/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多收藏\",\"url\":\"/list-07/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多字数\",\"url\":\"/list-08/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"原創\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"最新更新\",\"url\":\"/list-21/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最新上架\",\"url\":\"/list-22/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最高评分\",\"url\":\"/list-23/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多观看\",\"url\":\"/list-24/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多文章\",\"url\":\"/list-25/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多讨论\",\"url\":\"/list-26/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多收藏\",\"url\":\"/list-27/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多字数\",\"url\":\"/list-28/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"日輕\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"最新更新\",\"url\":\"/list-11/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最新上架\",\"url\":\"/list-12/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最高评分\",\"url\":\"/list-13/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多观看\",\"url\":\"/list-14/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多文章\",\"url\":\"/list-15/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多讨论\",\"url\":\"/list-16/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多收藏\",\"url\":\"/list-17/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多字数\",\"url\":\"/list-18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"韓輕\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"最新更新\",\"url\":\"/list-31/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最新上架\",\"url\":\"/list-32/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最高评分\",\"url\":\"/list-33/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多观看\",\"url\":\"/list-34/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多文章\",\"url\":\"/list-35/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多讨论\",\"url\":\"/list-36/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多收藏\",\"url\":\"/list-37/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多字数\",\"url\":\"/list-38/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"R18\",\"url\":\"\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":1}},\n{\"title\":\"最新更新\",\"url\":\"/tags-01/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最新上架\",\"url\":\"/tags-02/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最高评分\",\"url\":\"/tags-03/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多观看\",\"url\":\"/tags-04/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多文章\",\"url\":\"/tags-05/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多讨论\",\"url\":\"/tags-06/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多收藏\",\"url\":\"/tags-07/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"最多字数\",\"url\":\"/tags-08/R18/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}}]", + "lastUpdateTime": 1756016288361, + "loginUrl": "https://www.esjzone.one/my/profile.html", + "respondTime": 3080, + "ruleBookInfo": { + "author": "ul.list-unstyled@li.1@a@text", + "canReName": "1", + "coverUrl": ".product-gallery@img@src", + "intro": "
{{@@section.m-t-20@a.tag@text##(^|[\\n])##🏷️}}\n{{'‎'}}\n{{@@div.description@html}}", + "kind": "{{@@ul.list-unstyled@li.0@text##類型.\\s}},{{@@ul.list-unstyled@li.-2@text##更新日期.\\s}},{{@@.hidden-xs@div.display-3@text##$##分}}", + "lastChapter": "#chapterList@a.-1@p@text&&.list-unstyled@li.-2@text##更新日期.\\s", + "name": "h2@text", + "wordCount": "span#txt@text##\\D" + }, + "ruleContent": { + "content": "div.forum-content@html" + }, + "ruleSearch": { + "author": ".card-author@a@text", + "bookList": "div.col-lg-3", + "bookUrl": ".card-title@a@href", + "checkKeyWord": "勇者", + "coverUrl": "div.lazyload@html##.*\"(.*)\".*##$1", + "kind": "{{@@div.card-other.1@div.2@text##\\D}}个收藏,{{@@div.card-other.2@div.2@text##\\D}}条留言", + "lastChapter": ".card-ep@a@text", + "name": ".card-title@a@text", + "wordCount": "div.card-other.0@div.column.1@text##\\D" + }, + "ruleToc": { + "chapterList": "#chapterList@a[data-title], p.non, summary", + "chapterName": "text", + "chapterUrl": "href", + "isVolume": "p.non@text||summary@text" + }, + "searchUrl": "/tags-01/{{key}}/{{page}}.html", + "weight": 0 + }, + { + "bookSourceComment": "by·holzora\n详细说明请见:https://n.novelia.cc/forum/66a8e77866bff10c16e51d6a", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "轻小说机翻机器人", + "bookSourceType": 0, + "bookSourceUrl": "https://n.novelia.cc", + "bookUrlPattern": "(?:https?://)?(?:books\\d?.fishhawk.top|n.novelia.cc)/(?:api/)?novel/\\w+/[\\w-]+", + "customOrder": 3, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "\nvar discover = [];\nvar showR18 = false;\nvar push = (title, url, style) => {\n discover.push({\n title: title,\n url: url,\n style: {\n layout_flexGrow: 1,\n layout_flexBasisPercent: style\n }\n });\n};\nvar addSlots = (count, style) => {\n var remainder = count % 3;\n var slotsAdd = remainder === 0 ? 0 : 3 - remainder;\n for (var i = 0; i < slotsAdd; i++) {\n push(\"\", null, style);\n }\n};\npush(\"书架\", null, 1);\ntry {\n var favored = java.ajax(`${baseUrl}/api/user/favored`);\n if (favored.includes(\"Token不合法或者过期\")) {\n \t showR18 = false;\n push(\"账号未登录\", null, 0.25);\n addSlots(1, 0.25);\n } else {\n \t showR18 = true;\n \t push(\"阅读历史\", \"/api/user/read-history?page={{(page-1)}}&pageSize=30\", 0.25);\n var favoredData = JSON.parse(favored).favoredWeb;\n favoredData.forEach(item => {\n push(item.title, `/api/user/favored-web/${item.id}?page={{(page-1)}}&pageSize=30&query=&provider=kakuyomu%2Csyosetu%2Cnovelup%2Chameln%2Cpixiv%2Calphapolis&type=0&level=0&translate=0&sort=update`, 0.25);\n });\n addSlots(1 + favoredData.length, 0.25);\n }\n} catch (e) {\n\t showR18 = false;\n push(\"未知错误\", null, 0.25);\n addSlots(1, 0.25);\n}\npush(\"来源\", null, 1);\nvar providers = [\"全部\", \"kakuyomu\", \"syosetu\", \"novelup\", \"hameln\", \"pixiv\", \"alphapolis\"];\nproviders.forEach(provider => {\n var url = provider === \"全部\" ? \n `/api/novel?page={{(page-1)}}&pageSize=20&query=&provider=${providers.slice(1).join('%2C')}&type=0&level=0&translate=0&sort=0` : \n `/api/novel?page={{(page-1)}}&pageSize=20&query=&provider=${provider}&type=0&level=0&translate=0&sort=0`;\n push(provider, url, 0.25);\n});\naddSlots(providers.length, 0.25);\npush(\"类型\", null, 1);\nvar types = [\"全部\", \"连载中\", \"已完结\", \"短篇\"];\ntypes.forEach((type, index) => {\n var url = `/api/novel?page={{(page-1)}}&pageSize=20&query=&provider=kakuyomu%2Csyosetu%2Cnovelup%2Chameln%2Cpixiv%2Calphapolis&type=${index}&level=0&translate=0&sort=0`;\n push(type, url, 0.25);\n});\naddSlots(types.length, 0.25);\npush(\"分级\", null, 1);\nvar levels = showR18 ? [\"全部\", \"一般向\", \"R18\"] : [\"全部\", \"一般向\"];\nlevels.forEach((level, index) => {\n var url = `/api/novel?page={{(page-1)}}&pageSize=20&query=&provider=kakuyomu%2Csyosetu%2Cnovelup%2Chameln%2Cpixiv%2Calphapolis&type=0&level=${index}&translate=0&sort=0`;\n push(level, url, 0.25);\n});\naddSlots(levels.length, 0.25);\npush(\"排序\", null, 1);\nvar sorts = [\"更新\", \"点击\"];\nsorts.forEach((sort, index) => {\n var url = `/api/novel?page={{(page-1)}}&pageSize=20&query=&provider=kakuyomu%2Csyosetu%2Cnovelup%2Chameln%2Cpixiv%2Calphapolis&type=0&level=0&translate=0&sort=${index}`;\n push(sort, url, 0.25);\n});\naddSlots(sorts.length, 0.25);\nJSON.stringify(discover);\n", + "header": "\nvar header = {\n \"Referer\": baseUrl\n};\nJSON.stringify(header);\n", + "jsLib": "var update = (initUrl, initToken) => {\n try {\n var [novelUrl, chapterId] = initUrl.split('/chapter/');\n var url = novelUrl.replace('/novel/', '/user/read-history/');\n var {Authorization: token} = JSON.parse(initToken);\n return Packages.org.jsoup.Jsoup.connect(url).method(Packages.org.jsoup.Connection.Method.PUT).header(\"Authorization\", token).requestBody(chapterId).execute().body();\n } catch (e) {\n return \"\";\n }\n};\nvar annotate = (text, dict) => {\n var keys = Object.keys(dict).sort((a, b) => b.length - a.length);\n var pat = new RegExp(keys.join('|'), 'g');\n return text.replace(pat, word => {\n var read = dict[word];\n if (/[ぁ-んァ-ン]/.test(word) && /[一-龯々]/.test(word)) {\n var pre = (word.match(/^([ぁ-んァ-ン]+)/) || [''])[0];\n var suf = (word.match(/([ぁ-んァ-ン]+)$/) || [''])[0];\n var kanji = word.substring(pre.length, word.length - suf.length);\n return (!pre || read.startsWith(pre)) && (!suf || read.endsWith(suf)) ? `${pre}${kanji}(${read.substring(pre.length, read.length - suf.length)})${suf}` : `${word}(${read})`;\n }\n return `${word}(${read})`;\n });\n};\nvar regex = content => {\n var displayedImages = new Set();\n return JSON.stringify(content).replace(/<图片>(https?:\\/\\/[^\"\\s]+)/g, (match, url) => {\n if (displayedImages.has(url)) return '';\n displayedImages.add(url);\n var referer = url.match(/https?:\\/\\/[^\\/]+/)[0].replace(/i\\.pximg\\.net/, 'www.pixiv.net');\n var headers = JSON.stringify({headers: {Referer: referer}});\n return ``;\n });\n};\nvar format = content => content.replace(/^\\[|\\]$/g, '').split(',').map(item => item.trim().replace(/^\"|\"$/g, '')).filter(Boolean).map((item, i) => item.startsWith('{\"headers\"') && i > 0 ? ',' + item : ' ' + item).join(\"\\n\");", + "lastUpdateTime": 1756741400517, + "loginUi": "[\n {\n \"name\": \"账号\",\n \"type\": \"text\"\n },\n {\n \"name\": \"密码\",\n \"type\": \"password\"\n }\n]", + "loginUrl": "\nvar login = () => {\n var username = source.getLoginInfoMap().get(\"账号\");\n var password = source.getLoginInfoMap().get(\"密码\");\n var res = java.post(\n `${baseUrl.replace('n', 'auth')}/api/v1/auth/login`,\n JSON.stringify({\n username: username,\n password: password,\n app: \"legado\"\n }),\n {\"Content-Type\": \"application/json\"}\n );\n source.putLoginHeader(JSON.stringify({\n Authorization: `Bearer ${res.body()}`\n }));\n}\nlogin();\n", + "respondTime": 1927, + "ruleBookInfo": { + "author": "authors..name", + "coverUrl": "{{'https://static.kksk.io/image/convert/885e391180d8d1f37ad157338daa61dd.webp'}}", + "init": "\nvar initUrl = book.bookUrl;\nvar url = initUrl.includes('api/novel') ? initUrl : initUrl.replace('/novel/', '/api/novel/');\nvar body = initUrl.includes('api/novel') ? result : java.ajax(url);\nif (body.includes('ID不合适,应当使用')) {\n var match = body.match(/\\/\\w+\\/[\\w-]+/);\n url = `${book.origin}/api/novel${match}`;\n body = java.ajax(url);\n}\nif (body.includes(\"Token不合法或者过期\")) {\n java.toast(\"当前会话已失效,请重新登录\");\n}\njava.put('url', url);\nbody;\n", + "intro": "\nvar value = String(book.getVariable(\"custom\")) || String(source.getVariable());\nvar data = JSON.parse(result);\nvar cnIntro = data.introductionZh;\nvar jpIntro = data.introductionJp;\nvar intro = cnIntro ? (value === '5' ? jpIntro : ['6', '8'].includes(value) ? cnIntro + \"\\n‎\\n\" + jpIntro : ['7', '9'].includes(value) ? jpIntro + \"\\n‎\\n\" + cnIntro : cnIntro) : jpIntro;\nvar timeStamp = '{{$.toc[-1].createAt}}' || '{{$.syncAt}}';\nintro = '  📬最近' + ('{{$.toc[-1].createAt}}' ? '更新' : '同步') + ':' + java.timeFormat(timeStamp * 1000) + '\\n‎\\n' + intro;\nintro;\n", + "kind": "{{$.type##中|已}},{{$.attentions}},{{$.keywords}}", + "name": "\nvar value = String(book.getVariable(\"custom\")) || String(source.getVariable());\nvar data = JSON.parse(result);\nvar name = value === '5' ? data.titleJp : data.titleZh || data.titleJp;\nname;\n", + "tocUrl": "{{java.get('url')}}", + "wordCount": "totalCharacters" + }, + "ruleContent": { + "content": "\nvar token = source.getLoginHeader();\nupdate(baseUrl, token);\nvar value = String(book.getVariable(\"custom\")) || String(source.getVariable());\nvar get = /^[1-9]$/.test(value) ? value : '1';\nvar config = {\n 1: 'sakuraParagraphs',\n 2: 'gptParagraphs',\n 3: 'youdaoParagraphs',\n 4: 'baiduParagraphs',\n 5: 'paragraphs'\n};\nvar data = JSON.parse(result);\nvar content = data[config[get]] || data[config[1]] || data[config[2]] || data[config[3]] || data[config[4]] || data[config[5]];\nif (['6', '7', '8', '9'].includes(get)) {\n var cnContent = data[config[1]] || data[config[2]] || data[config[3]] || data[config[4]];\n var jpContent = data[config[5]];\n if (cnContent) {\n if (['8', '9'].includes(get)) {\n var res = java.post(\n 'https://static.kksk.io/api/kanji',\n JSON.stringify({\"content\": format(regex(jpContent))}),\n {\"Content-Type\": \"application/json\"}\n );\n var kanji = JSON.parse(res.body());\n jpContent = jpContent.map(text => annotate(text, kanji));\n }\n var type = ['6', '8'].includes(get);\n content = jpContent.flatMap((jp, i) => {\n if (!((jp || '').trim() && (cnContent[i] || '').trim())) return [];\n var pair = type ? [cnContent[i], jp] : [jp, cnContent[i]];\n return pair.filter(Boolean).concat(i === jpContent.length - 1 ? [] : '\\u200B');\n }).filter(Boolean);\n } else {\n content = jpContent;\n }\n}\nformat(regex(content));\n" + }, + "ruleSearch": { + "bookList": "\nif (result.includes(\"Token不合法或者过期\")) {\n java.toast(\"当前会话已失效,请重新登录\");\n}\nresult;\n\nitems", + "bookUrl": "/api/novel/{{$.providerId}}/{{$..novelId}}", + "coverUrl": "{{'https://static.kksk.io/image/convert/885e391180d8d1f37ad157338daa61dd.webp'}}", + "intro": "  源站:{{$.providerId}}{{'\\n'}}📬最近同步:{{java.timeFormat(java.getString('$.updateAt')*1000)}}{{'\\n'}}总计{{$.total}}|百度{{$.baidu}}|有道{{$.youdao}}|gpt{{$.gpt}}|sakura{{$.sakura}}", + "kind": "{{$.type##中|已}},{{$.attentions}},{{$.keywords}}", + "name": "\nvar value = String(book.getVariable(\"custom\")) || String(source.getVariable());\nvar name = value === '5' ? result.titleJp : result.titleZh || result.titleJp;\nname;\n" + }, + "ruleToc": { + "chapterList": "\nif (result.includes(\"Token不合法或者过期\")) {\n java.toast(\"当前会话已失效,请重新登录\");\n}\nresult;\n\ntoc\n\nresult.forEach(item => {\n if ('chapterId' in item) {\n item.url = `${baseUrl}/chapter/${item.chapterId}`;\n var timeStamp = item.createAt ? item.createAt * 1000 : null;\n if (timeStamp) {\n item.time = java.timeFormat(timeStamp);\n }\n } else {\n item.isVolume = true;\n }\n});\nresult;\n", + "chapterName": "\nvar value = String(book.getVariable(\"custom\")) || String(source.getVariable());\nvar name = result.titleZh ? (value === '5' ? result.titleJp : ['6', '8'].includes(value) ? result.titleZh + ' ↔ ' + result.titleJp : ['7', '9'].includes(value) ? result.titleJp + ' ↔ ' + result.titleZh : result.titleZh) : result.titleJp;\nname;\n", + "chapterUrl": "url", + "isVolume": "isVolume", + "updateTime": "\nvar value = String(book.getVariable(\"custom\")) || String(source.getVariable());\nvar info = result.isVolume ? null : (result.time ? (result.titleZh ? (['6', '8'].includes(value) ? result.titleJp + ' · ' + result.time : ['7', '9'].includes(value) ? result.titleZh + ' · ' + result.time : result.time) : result.time) : (['7', '9'].includes(value) ? result.titleZh : ['6', '8'].includes(value) ? result.titleJp : null));\ninfo;\n" + }, + "searchUrl": "\nvar providers = {\n kakuyomu: /(?:https?:\\/\\/)?kakuyomu\\.jp\\/works\\/(\\d+)/,\n syosetu: /(?:https?:\\/\\/)?(ncode|novel18)\\.syosetu\\.com\\/(\\w+)/,\n novelup: /(?:https?:\\/\\/)?novelup\\.plus\\/story\\/(\\d+)/,\n hameln: /(?:https?:\\/\\/)?syosetu\\.org\\/novel\\/(\\d+)/,\n pixiv: /(?:https?:\\/\\/)?(www\\.pixiv\\.net\\/novel\\/series\\/(\\d+)|www\\.pixiv\\.net\\/novel\\/show\\.php\\?id=(\\d+))/,\n alphapolis: /(?:https?:\\/\\/)?www\\.alphapolis\\.co\\.jp\\/novel\\/(\\d+)\\/(\\d+)/,\n sakura: /(?:https?:\\/\\/)?(?:books\\d?\\.fishhawk\\.top|n\\.novelia\\.cc)\\/(?:api\\/)?novel\\/(\\w+)\\/([\\w-]+)/\n};\nvar url;\nfor (var provider in providers) {\n var match = key.match(providers[provider]);\n if (match) {\n var providerId = provider === 'sakura' ? match[1] : provider;\n var novelId = provider === 'alphapolis' ? `${match[1]}-${match[2]}` : provider === 'pixiv' ? (match[3] ? `s${match[3]}` : match[2]) : (match[2] || match[1]);\n url = `${baseUrl}/api/novel/${providerId}/${novelId}`;\n break;\n }\n}\nif (!url) {\n url = `${baseUrl}/api/novel?page=${page-1}&pageSize=20&query=${key}&provider=kakuyomu,syosetu,novelup,hameln,pixiv,alphapolis&type=0&level=0&translate=0&sort=1`;\n}\nurl;\n", + "variableComment": "输入对应数字刷新即可获取对应文本,{1: \"sakura译文\", 2: \"gpt译文\", 3: \"有道译文\", 4: \"百度译文\", 5: \"原文\", 6: \"中日对照\", 7: \"日中对照\", 8: \"6+平假注音\", 9: \"7+平假注音\"},默认sakura译文。书籍变量作用于一本书,源变量作用于整个书源,书籍变量优先级高于源变量。", + "weight": 0 + }, + { + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "顶点小说", + "bookSourceType": 0, + "bookSourceUrl": "https://www.dingdian.com/", + "customOrder": 4, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "lastUpdateTime": 1758282114453, + "respondTime": 2952, + "ruleBookInfo": { + "coverUrl": "class.border_left_a@img@src", + "intro": "class.hang_3@text" + }, + "ruleContent": { + "content": "class.neirong@div.4@textNodes" + }, + "ruleSearch": { + "author": "class.zuo@text", + "bookList": "class.lastupdate@tag.li", + "bookUrl": "class.name@a.0@href", + "checkKeyWord": "沧元图", + "kind": "class.lei@text", + "lastChapter": "class.jie@text", + "name": "class.name@text" + }, + "ruleToc": { + "chapterList": "class.fen_4@li", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href" + }, + "searchUrl": "https://www.dingdian888.com/s.php,{\n \"body\": \"s={{key}}&submit=\",\n \"method\": \"POST\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "ryw笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.biquge.cc", + "customOrder": 5, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "", + "lastUpdateTime": 1759304196420, + "respondTime": 11090, + "ruleBookInfo": { + "author": "p.booktag@a@text", + "intro": "p.bookintro@text", + "kind": "ol.breadcrumb@li.1@text", + "lastChapter": "a.bookchapter@text", + "name": "h1.booktitle@text", + "tocUrl": "div.list-chapterAll@dd@a@href", + "wordCount": "p.booktag@span.0@text" + }, + "ruleContent": { + "content": "class.readcontent@tag.p@html", + "nextContentUrl": "id.linkNext@href" + }, + "ruleSearch": { + "author": "div.author.0@text", + "bookList": "div.bookbox", + "bookUrl": "a.del_but@href", + "checkKeyWord": "借剑", + "intro": "div.update@text", + "lastChapter": "div.cat@text", + "name": "h4.bookname@text" + }, + "ruleToc": { + "chapterList": "#list-chapterAll@dd", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href", + "preUpdateJs": "" + }, + "searchUrl": "{{cookie.removeCookie(source.getKey())}}/search/,{\n\"method\": \"POST\",\n \"body\": \"searchkey={{key}}\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.biquge.one", + "bookUrlPattern": "https://m.biquge.one/\\d+", + "customOrder": 12, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "全部::/xclass/0/{{page}}.html\n玄幻::/xclass/1/{{page}}.html\n仙侠::/xclass/2/{{page}}.html\n都市::/xclass/3/{{page}}.html\n穿越::/xclass/4/{{page}}.html\n科幻::/xclass/5/{{page}}.html\n网游::/xclass/6/{{page}}.html\n精品::/xclass/7/{{page}}.html", + "header": "{\n 'User-Agent': 'Mozilla/5.0 (Linux; Android 11; Pixel 3 XL Build/RQ3A.211001.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.4988.0 Mobile Safari/537.36 SearchCraft/3.9.2 (Baidu; P1 11) '\n}", + "lastUpdateTime": 0, + "respondTime": 2718, + "ruleBookInfo": { + "coverUrl": "class.synopsisArea_detail@img@src", + "intro": "class.review@text", + "lastChapter": "class.directoryArea@p.0@text", + "name": "", + "tocUrl": "text.查看完整目录@href" + }, + "ruleContent": { + "content": "id.chaptercontent@html", + "replaceRegex": "##.*加入书签.*" + }, + "ruleExplore": { + "coverUrl": "", + "intro": "", + "lastChapter": "" + }, + "ruleSearch": { + "author": ".author.0@text##精品 .*作者:", + "bookList": "class.hot_sale", + "bookUrl": "a@href", + "checkKeyWord": "剑来", + "coverUrl": "tag.a@href\nvar id = result.match(/(\\d+)\\/?$/)[1];\nvar iid = parseInt(id/1000);\n'http://img.biquge.one/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "kind": "", + "lastChapter": ".author.1@text", + "name": ".title@text" + }, + "ruleToc": { + "chapterList": "id.chapterlist@p!0@a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "https://m.biquge.one/s.php,{\n \"body\": \"keyword={{key}}&submit=搜索\",\n \"method\": \"POST\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "新笔趣阁 wap.xinbiquge.org", + "bookSourceType": 0, + "bookSourceUrl": "https://wap.xinbiquge.org", + "customOrder": 20, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "header": "{ \"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 1739290382884, + "respondTime": 2738, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "coverUrl": "[property=\"og:image\"]@content", + "intro": "[property=\"og:description\"]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property=\"og:novel:latest_chapter_name\"]@content", + "name": "class.name@text", + "wordCount": "class.dd_box.1@span.1@text##字数:" + }, + "ruleContent": { + "content": "id.chaptercontent@html", + "nextContentUrl": "text.下一@href", + "replaceRegex": "##.*\\(.*\\).*\\s|\\s.*新笔趣阁.*|.*本章未完.*\\s|.*继续阅读.*\\s|.*阅读完毕,更多请搜索.*\\s" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "class.author@span@text", + "bookList": "class.hot_sale hot_saleEm", + "bookUrl": "a@href", + "checkKeyWord": "剑来", + "name": "class.title@text" + }, + "ruleToc": { + "chapterList": "class.book_last.2@dl@dd", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "https://wap2.xinbiquge.org/book/search.aspx?ie=utf-8&siteid=xinbiquge.org&s=000&key={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁㉘①", + "bookSourceType": 0, + "bookSourceUrl": "https://www.yingsx.com/", + "bookUrlPattern": "https://www.yingsx.com/\\d+_\\d+/", + "customOrder": 40, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "", + "lastUpdateTime": 1715033688040, + "loginUrl": "", + "respondTime": 3124, + "ruleBookInfo": { + "author": "[property=og:novel:author]@content", + "coverUrl": "[property=og:image]@content", + "init": "", + "intro": "[property=og:description]@content", + "kind": "[property=og:novel:update_time]@content&&\n[property=og:novel:category]@content&&\n[property=og:novel:status]@content", + "lastChapter": "[property=og:novel:latest_chapter_name]@content##免费章节 |正文卷 |正文 |VIP章节 ", + "name": "[property=og:novel:book_name]@content##\\(.*|\\(.*|免费阅读|全文.*阅读|最新章节|小说|笔趣阁" + }, + "ruleContent": { + "content": "id.content@textNodes" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "kind": "", + "lastChapter": "", + "name": "" + }, + "ruleSearch": { + "author": "class.s4@a@text", + "bookList": "class.novelslist2@ul@li", + "bookUrl": "class.s2@a@href", + "coverUrl": "class.s2@tag.a@href\nvar id = result.match(/(\\d+).html\\/?$/)[1];\nvar iid = parseInt(id/1000);\n'https://www.yingsx.comfiles/article/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "kind": "class.s6@text", + "lastChapter": "class.s3@a@text##免费章节 |正文卷 |正文 |VIP章节 ", + "name": "class.s2@a@text##\\(.*|\\(.*|免费阅读|全文.*阅读|最新章节|小说|笔趣阁" + }, + "ruleToc": { + "chapterList": "//*[@id=\"list\"]//dt[2]/following-sibling::dd/a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "https://www.yingsx.com/cse/search?q={{key}}&s=", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.yuyouku.com", + "customOrder": 55, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "", + "header": "{\n 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',\n \"Cookie\":\"0\"\n}", + "lastUpdateTime": 0, + "respondTime": 11023, + "ruleBookInfo": { + "author": "@get:{a}", + "coverUrl": "@get:{c}", + "init": "@put:{\nn:\"[property$=book_name|title]@content\",\na:\"[property$=author]@content\",\nk:\"[property~=category|status|update_time]@content\",\nl:\"[property$=latest_chapter_name]@content\",\ni:\"[property$=description]@content\",\nc:\"[property$=image]@content\",\ntac:\"[property$=read_url]@content\"\n}", + "intro": "@get:{i}", + "kind": "@get:{k}", + "lastChapter": "@get:{l}", + "name": "@get:{n}", + "tocUrl": "@get:{tac}", + "wordCount": "" + }, + "ruleContent": { + "content": "#txtContent@html\n@js:\n/*\nresult=result.replace(/
/g,\"

\").replace(/ /g,\"

\")\n*/\nresult", + "nextContentUrl": "text.下一@href\n@js:\nif(result){\n result=result[0].includes(\"_\")?result:\"\";\n}", + "replaceRegex": "" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "lastChapter": "", + "name": "" + }, + "ruleSearch": { + "author": "div.3@text", + "bookList": ".list-group-item[1:-2]", + "bookUrl": "div.1@a@href", + "checkKeyWord": "剑来", + "coverUrl": "", + "intro": "", + "kind": "div.0@i@text", + "lastChapter": "div.2@a@text", + "name": "div.1@a@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "#chapters-list@li@a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "option@value", + "updateTime": "" + }, + "searchUrl": "@js:\nvar su=source.getKey()\nvar htmlUrl=java.get(su,{}).headers('Location')\njava.log(`重定向:${htmlUrl[0]}`)\nvar posturl=htmlUrl[0]?htmlUrl[0]:(su.includes(\"#\")?su.match(/([^\\#]+)\\#/)[1]:su)\nposturl = posturl.slice(-1)=='/'?posturl.slice(0,-1):posturl\ncookie.removeCookie(posturl)\njava.log(`请求:${posturl}`)\n\n\nvar html = java.ajax(posturl);\n\nsearch = org.jsoup.Jsoup.parse(html).select('form[role=form]').attr('action');\n\n\nposturl+search+\"?keyword={{key}},\"+JSON.stringify({\r\n \"method\": \"GET\"\r\n})", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.ibiquges.com", + "bookUrlPattern": "", + "customOrder": 65, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "玄幻小说::/fenlei/1_{{page}}.html\n修真小说::/fenlei/2_{{page}}.html\n都市小说::/fenlei/3_{{page}}.html\n穿越小说::/fenlei/4_{{page}}.html\n网游小说::/fenlei/5_{{page}}.html\n科幻小说::/fenlei/6_{{page}}.html", + "header": "", + "lastUpdateTime": 1734838028643, + "loginUrl": "", + "respondTime": 10066, + "ruleBookInfo": { + "author": "//meta[@property='og:novel:author']/@content", + "coverUrl": "//meta[@property='og:image']/@content", + "intro": "//meta[@property='og:description']/@content", + "kind": "//meta[@property='og:novel:category']/@content", + "lastChapter": "id.info.0@tag.a.-1@text", + "name": "//meta[@property='og:novel:book_name']/@content" + }, + "ruleContent": { + "content": "id.content.0@textNodes", + "replaceRegex": "##.*第.*章.*\\s" + }, + "ruleExplore": { + "author": "class.s5.0@text", + "bookList": "class.l@li", + "bookUrl": "class.s2.0@tag.a.0@href", + "coverUrl": "", + "intro": "", + "kind": "", + "lastChapter": "class.s3.0@tag.a.0@text", + "name": "class.s2.0@tag.a.0@text" + }, + "ruleSearch": { + "author": "tag.td.2@text", + "bookList": "tag.tbody.0@tag.tr.!0", + "bookUrl": "tag.td.0@tag.a.0@href", + "kind": "", + "lastChapter": "tag.td.1@text", + "name": "tag.td.0@text" + }, + "ruleToc": { + "chapterList": "id.list.0@a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "/modules/article/waps.php?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.biqusa.com", + "customOrder": 77, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "lastUpdateTime": 1735040240827, + "respondTime": 3284, + "ruleBookInfo": { + "coverUrl": "//*[@id=\"thumb\"]/img/@src", + "kind": "//li[contains(@class, \"sort\")]/text()", + "tocUrl": "//*[contains(text(), \"查看完整目录\")]/@href" + }, + "ruleContent": { + "content": "#chaptercontent@textNodes", + "nextContentUrl": "text.下一页@href" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "/p[1]/a[2]/text()", + "bookList": "//div[contains(@class, \"slide-item\")]/div", + "bookUrl": "/a/@href", + "coverUrl": "", + "lastChapter": "/p[2]/a/text()", + "name": ".title@text" + }, + "ruleToc": { + "chapterList": "#chapterlist@p!0", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "/SearchBook.php?keyword={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.mayiwxw.com/", + "bookUrlPattern": "https://www.mayiwxw.com/\\d+_\\d+/", + "customOrder": 87, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "", + "lastUpdateTime": 1743586456661, + "loginUrl": "", + "respondTime": 10000, + "ruleBookInfo": { + "author": "id.info@tag.p.0@text##作 者:", + "coverUrl": "id.fmimg@tag.img.0@src", + "intro": "id.intro@text", + "kind": "id.info@tag.p.2@text##最后更新:", + "lastChapter": "id.info@tag.p.3@a@text", + "name": "id.info@tag.h1.0@text" + }, + "ruleContent": { + "content": "id.content@html##请记住本书首发域名:|.net。|顶点小说手机版阅读网址:|最新网址.*com" + }, + "ruleExplore": { + "author": "class.newbook_author@a@text", + "bookList": "class.newbook_list", + "bookUrl": "class.newbook_title@a@href@js:'https://www.mayiwxw.com'+result", + "coverUrl": "img@src", + "intro": "class.newbook_intor@text", + "name": "class.newbook_title@a@text" + }, + "ruleSearch": { + "author": "tag.td.2@text", + "bookList": "class.grid@tag.tr!0", + "bookUrl": "tag.td.0@tag.a@href", + "coverUrl": "tag.td.0@tag.a@href\nvar id = result.match(/(\\d+)\\/?$/)[1];\nvar iid = parseInt(id/1000);\n'https://www.mayiwxw.com/files/article/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "lastChapter": "tag.td.1@tag.a@text", + "name": "tag.td.0@tag.a@text" + }, + "ruleToc": { + "chapterList": "id.list@tag.dd", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href" + }, + "searchUrl": "https://www.mayiwxw.com/modules/article/search.php?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.biquge.casa", + "bookUrlPattern": "", + "customOrder": 100, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[\n {\n \"title\": \"校园言情\",\n \"url\": \"/xiaoyuan.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"都市言情\",\n \"url\": \"/dushi.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"古代言情\",\n \"url\": \"/gudai.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"罗曼史\",\n \"url\": \"/luoman.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"耽美\",\n \"url\": \"/danmei.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"百合\",\n \"url\": \"/baihe.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"同人\",\n \"url\": \"/tongren.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"奇幻玄幻\",\n \"url\": \"/qihuan.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"网游竞技\",\n \"url\": \"/wangyou.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"科幻异能\",\n \"url\": \"/kehuan.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"恐怖灵异\",\n \"url\": \"/kongbu.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"惊悚推理\",\n \"url\": \"/jingsong.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"武侠仙侠\",\n \"url\": \"/wuxia.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"历史军事\",\n \"url\": \"/lishi.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"穿越重生\",\n \"url\": \"/chuanyue.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"综合其它\",\n \"url\": \"/qita.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n }\n]", + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Linux; Android 8.0.0; MI 5s Plus Build/OPR1.170623.032; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36\"\n}", + "lastUpdateTime": 1754708405013, + "respondTime": 10003, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "coverUrl": "[property=\"og:image\"]@content", + "intro": "[property=\"og:description\"]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property~=las?test_chapter_name]@content", + "name": "[property=\"og:novel:book_name\"]@content", + "tocUrl": "" + }, + "ruleContent": { + "content": "@js:\nid=java.get(\"id\")\nif (/baidu\\.com/.test(baseUrl)) {\n pre = baseUrl.replace(/baidu\\.com/, \"biquge.casa\");\n rev = org.jsoup.Jsoup.parse(java.ajax(pre));\n url = source.key + \"/\" + id + rev.select('#linkPrev').attr('href');\n java.setContent(result = java.ajax(url));\n} else if (/bing\\.com/.test(baseUrl)) {\n nex = baseUrl.replace(/cn\\.bing\\.com/, \"www.biquge.casa\");\n next = org.jsoup.Jsoup.parse(java.ajax(nex));\n nexUrl = source.key + \"/\" + id + next.select('#linkNext').attr('href');\n java.setContent(result = java.ajax(nexUrl));\n } else { result; }\nresult = java.getString('.readcontent@html');" + }, + "ruleExplore": { + "author": ".author.0@text##作者:", + "bookList": ".bookbox", + "bookUrl": "a@href", + "coverUrl": "a.0@href##\\D((\\d+)\\d{3})##https://img.biquge.casa/image/$2/$1/$1s.jpg###", + "intro": ".update@ownText", + "kind": ".author.2@text", + "lastChapter": ".cat@a.0@text", + "name": "h4@text", + "wordCount": "" + }, + "ruleSearch": { + "author": ".author.0@text##作者:", + "bookList": ".bookbox", + "bookUrl": "a.0@href", + "checkKeyWord": "大理寺", + "coverUrl": "a.0@href##\\D((\\d+)\\d{3})##https://img.biquge.casa/image/$2/$1/$1s.jpg###", + "intro": ".update@ownText", + "kind": ".author.1@text", + "lastChapter": ".cat@a.0@text", + "name": "h4@text" + }, + "ruleToc": { + "chapterList": "\nbase = baseUrl;\nid = baseUrl.match(/(\\d+\\/)/)[1]\njava.put(\"id\",id)\nlis = java.getElements('#list-chapterAll@dd');\nvar list = [];\n\nfor (var i = 0; i < lis.length; i++) {\n var href = lis[i].select('a').attr('href');\n var text = lis[i].text();\n if (href==0) {\n if ( i < lis.length - 1 ) {\n var url = \"https://www.baidu.com/\" + id + lis[i + 1].select('a').attr('href'); \n } else {\n var url = \"https://cn.bing.com/\" + id + lis[i - 1].select('a').attr('href');\n }\n } else { var url = base + href; }\n java.log(lis[i].text()+\":\"+url);\n list.push({\"text\": text, \"href\": url});\n}\n\nlist\n", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "/modules/article/search.php,{\n \"body\": \"searchkey={{key}}&searchtype=all&submit=\",\n \"method\": \"POST\",\n \"charset\": \"GBK\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "//2024.10.27 by.GEd520\n//2024.10.29 完善部分规则,目录判断与正文解密代码 by.夜泽川", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁_书友", + "bookSourceType": 0, + "bookSourceUrl": "https://m.suimeng.la", + "customOrder": 103, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"全部\",\"url\":\"/sort/0-{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻魔法\",\"url\":\"/sort/1-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"武侠修真\",\"url\":\"/sort/2-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"都市言情\",\"url\":\"/sort/3-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"军事历史\",\"url\":\"/sort/4-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"侦探科幻\",\"url\":\"/sort/5-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"网游动漫\",\"url\":\"/sort/6-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"灵异耽美\",\"url\":\"/sort/7-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"其他小说\",\"url\":\"/sort/8-{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}}]", + "header": "{\"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 1730365071992, + "respondTime": 4985, + "ruleBookInfo": { + "author": "i.0@text##\\/著", + "intro": ".xs_introduce@ownText", + "name": "h2.0@text", + "tocUrl": "" + }, + "ruleContent": { + "content": "\nvar bes = result.match(/PHA\\+[A-Za-z0-9+\\/]+={0,2}/g);\n//java.log(String(bes))\nif (!bes) {\n var ml = java.webView(null,baseUrl,null);\n var txt = java.getString(\"#txt@html\");\n java.longToast(\"正文解密失败,转为webView动态加载。\");\n} else {\nvar result = [];\nfor (var i = 0; i < bes.length; i++) {\n // 遍历数组,使用阅读内置Base64解码方法\n var bsej = java.base64Decode(bes[i]);\n var txtt = String(bsej);\n result.push(txtt);\n }\n\tvar txt = result.join('\\n');\n}\ntxt;\n", + "nextContentUrl": "@js: var nex = result.match(/hhekgv='([^']*)'\\;/)[1];\nvar ul = source.key + nex;\nif (ul.length === String(book.bookUrl).length) {\n\tresult = \"\"\n\t} else{\n\t\tresult = ul;\n\t\t}\n\t\tjava.log(result)\nresult;", + "replaceRegex": "##.*退出浏览器阅读模式.*|(?m)^\\s*();()", + "title": "" + }, + "ruleExplore": {}, + "ruleSearch": { + "bookList": ".index_qb > a", + "bookUrl": "a@href", + "coverUrl": "img@data-original", + "intro": ".info@text", + "name": "h4@text" + }, + "ruleToc": { + "chapterList": "@js: var result = []\nif (!baseUrl.includes('index')) {\n //java.log(\"当前目录在详情页\")\n var chnList = java.getStringList(\"ul[class=ph_list].1@a@text\");\n var chfList = java.getStringList(\"ul[class=ph_list].1@a@href\");\n} else {\n //java.log(\"当前目录跳转新页面\")\n var chnList = java.getStringList(\".ph_list a@text\");\n var chfList = java.getStringList(\".ph_list a@onclick\");\n}\n\nfor (var i = 0; i < chfList.length; i++) {\n\tchfList[i] = String(chfList[i]).replace(/read_tz\\((\\d+)\\)\\;/g,book.bookUrl + \"$1.html\");\n\tvar chn = Array.from(chnList);\n var chf = Array.from(chfList);\n result.push({\n title: chn[i],\n url: chf[i]\n });\n}\n\nif (result.length === 0) {\n // 如果目录数据为空,则弹窗提示\n java.longToast(\"当前书籍无数据\");\n} else {\n result = result;\n}\nresult;", + "chapterName": "title", + "chapterUrl": "url", + "formatJs": "", + "isVolume": "", + "nextTocUrl": "@js: var ml = source.key + java.getString(\"text.章节目录@href\");\nvar src = java.ajax(ml);\nvar next = java.getStringList(\"option@value\",src);\nnext;", + "preUpdateJs": "" + }, + "searchUrl": "{{cookie.removeCookie(source.getKey())}}/search.html,{\n \"body\": \"s={{key}}\",\n \"method\": \"POST\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "\n\n//20250331\n晋南/塔客搜索重定向至upkj.net", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁(晋南/塔客同库)", + "bookSourceType": 0, + "bookSourceUrl": "https://www.upkj.net", + "bookUrlPattern": "https?:\\/\\/www.(jinnanyq|upkj|211zw)\\.(com|net)/[a-z]{2}\\/\\d+\\/(\\d+\\/)?", + "coverDecodeJs": "", + "customOrder": 114, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"玄幻\",\"url\":\"/xuanhuanxiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"修真\",\"url\":\"/xiuzhenxiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"都市\",\"url\":\"/dushixiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"历史\",\"url\":\"/lishixiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"网游\",\"url\":\"/wangyouxiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"科幻\",\"url\":\"/kehuanxiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"言情\",\"url\":\"/yanqingxiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"其它\",\"url\":\"/qitaxiaoshuo/{{page}}.html\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}}]", + "header": "{\"User-Agent\":\"Mozilla/5.0 (Linux; U; Android 13; zh-Hans-CN; PFJM10 Build/TP1A.220905.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/100.0.4896.58 Quark/6.13.6.581 Mobile Safari/537.36\"}", + "lastUpdateTime": 1744158178097, + "respondTime": 10000, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "canReName": "", + "coverUrl": "[property=\"og:image\"]@content", + "init": "", + "intro": "[property=\"og:description\"]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property~=las?test_chapter_name]@content", + "name": "[property=\"og:novel:book_name\"]@content", + "tocUrl": "" + }, + "ruleContent": { + "content": "[id~=(chapter)?content]@html\n\nString(result).replace(/<\\/i>/g,(m,p1)=>String.fromCharCode(parseInt(p1,16)))\n", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##(https?://\\w+.)?jinnanyq.com|\\s*网站公告:.*\\s*|\\s*(https?:.+)?爱读免费小说.*|本章未完.*", + "title": "" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "lastChapter": "", + "name": "", + "wordCount": "" + }, + "ruleSearch": { + "author": ".btm a@text", + "bookList": "if(result.match(/Just a/)){\n\njava.startBrowserAwait(baseUrl,\"验证\")\nresult=java.ajax(baseUrl);\n}\nresult\n.item", + "bookUrl": ".image a@href", + "checkKeyWord": "快穿", + "coverUrl": "img@data-original", + "intro": "dd@html", + "kind": ".blue@text", + "lastChapter": "", + "name": "dt@text", + "wordCount": ".orange@text" + }, + "ruleToc": { + "chapterList": "#list@.listpage:nth-of-type(1)~a||.section-list.fix.1@li a||dt:nth-of-type(2)~dd a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "select.0@option!0@value" + }, + "searchUrl": "https://www.upkj.net/search.php?keyword={{key}}&page={{page}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁l", + "bookSourceType": 0, + "bookSourceUrl": "https://m.biqugl.com", + "bookUrlPattern": "https://m.biqugl.com/book/\\d+", + "customOrder": 117, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": false, + "exploreUrl": "[{\"title\":\"玄幻奇幻\",\"url\":\"https://m.biqugl.com/sort/{{page}}/1/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"武侠修真\",\"url\":\"https://m.biqugl.com/sort/2/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"都市言情\",\"url\":\"https://m.biqugl.com/sort/3/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"历史军事\",\"url\":\"https://m.biqugl.com/sort/4/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"科幻悬疑\",\"url\":\"https://m.biqugl.com/sort/5/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"游戏竞技\",\"url\":\"https://m.biqugl.com/sort/6/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"女生专区\",\"url\":\"https://m.biqugl.com/sort/7/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}},{\"title\":\"其他小说\",\"url\":\"https://m.biqugl.com/sort/8/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.4}}]", + "header": "{ \"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 1685708637242, + "respondTime": 10016, + "ruleBookInfo": { + "author": "[property$=author]@content", + "coverUrl": "[property$=image]@content", + "intro": "[property$=description]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property$=latest_chapter_name]@content", + "name": "[property$=book_name]@content", + "tocUrl": "text.全部章节@href", + "wordCount": "class.gray fss@span.0@text##字" + }, + "ruleContent": { + "content": "#acontent@html", + "nextContentUrl": "text.下一页@href" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "p@textNodes##\\|.*(\\s.*)*", + "bookList": "class.c_row", + "bookUrl": "a@href", + "checkKeyWord": "剑来", + "coverUrl": "img@src", + "intro": "p@textNodes##.*\\|\\s", + "kind": "0", + "name": "h4@text" + }, + "ruleToc": { + "chapterList": "dl@dd", + "chapterName": "a@text", + "chapterUrl": "a@href", + "nextTocUrl": "#pagestats@textNodes\n@js:var go = result[0];\nvar page = go.match(/(\\d+)/) [1];\nvar list = [];\nfor (var i = 2; i <= page; i++) {\n list.push(baseUrl.replace(/1\\/asc/, i + '/asc'));\n}\nlist;", + "updateTime": "a@title##更新,共##," + }, + "searchUrl": "https://m.biqugl.com/s/all/{{key}}/{{page}}.html", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.bequgew.net", + "bookUrlPattern": "https://www.bequgew.com/\\d+/", + "concurrentRate": "", + "coverDecodeJs": "", + "customOrder": 126, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "", + "header": "", + "lastUpdateTime": 1721152018871, + "loginCheckJs": "", + "loginUi": "", + "loginUrl": "{\n \"url\": \"\"\n}", + "respondTime": 10013, + "ruleBookInfo": { + "author": "class.info@h3@text", + "canReName": "", + "coverUrl": "class.img@img@src", + "init": "class.content", + "intro": "id.intro@tag.p.0@text", + "kind": "class.info@tag.p.0@text&&class.info@tag.p.2@text&&class.info@tag.p.1@text##所属类型:|更新时间:|连载完成:", + "lastChapter": "class.info@tag.span@tag.a@text##百度搜索.*", + "name": "class.info@h1@text", + "tocUrl": "", + "wordCount": "" + }, + "ruleContent": { + "content": "id.book_text@html", + "imageStyle": "", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##记住本站网址(.*|\\n)本站|最新网址.*com|快捷c作: 按键盘上方向键.*加入书签” 功能!|.*免费电子书下载|手机用户请.*阅读更方便。|\\\\|\\║|作者.*|.*本书公告.*|推荐本章到.*|qq空间|.*微博|百度贴吧|.*网|qq收藏|百.*藏|.*址", + "sourceRegex": "", + "webJs": "" + }, + "ruleExplore": { + "author": "class.name@tag.span.1@text", + "bookList": "id.BookList@li", + "bookUrl": "class.name@a@href", + "coverUrl": "img@src", + "intro": "p@text", + "kind": "dl@em@text", + "lastChapter": "dl@a@text##最新章节:", + "name": "class.name@a@text" + }, + "ruleSearch": { + "author": "tag.td.2@text", + "bookList": "class.grid@tr!0", + "bookUrl": "tag.td.0@tag.a@href", + "coverUrl": "tag.td.0@tag.a@href\nvar id = result.match(/(\\d+)\\/?$/)[1];\nvar iid = parseInt(id/1000);\n'https://www.bequgew.com/files/article/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "intro": "", + "kind": "tag.td.5:4@text", + "lastChapter": "tag.td.1@tag.a@text##百度搜索.*", + "name": "tag.td.0@tag.a@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "class.article_texttitleb@li", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href", + "isVip": "", + "isVolume": "", + "nextTocUrl": "", + "updateTime": "" + }, + "searchUrl": "https://www.bequgew.net/modules/article/search.php?searchkey={{key}}", + "variableComment": "", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.biqugege.cc", + "bookUrlPattern": "", + "customOrder": 127, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "", + "header": "", + "lastUpdateTime": 0, + "loginCheckJs": "", + "respondTime": 10006, + "ruleBookInfo": { + "author": "[property=og:novel:author]@content", + "coverUrl": "[property=og:image]@content", + "init": "meta", + "intro": "[property=og:description]@content", + "kind": "[property=og:novel:category],[property=og:novel:status],[property=og:novel:update_time]@content##小说|\\s.*", + "lastChapter": "[property=og:novel:lastest_chapter_name],[property=og:novel:update_time]@content", + "name": "[property=og:novel:book_name]@content" + }, + "ruleContent": { + "content": "class.content@tag.p!-1@html", + "nextContentUrl": "#next_url@href" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "name": "" + }, + "ruleSearch": { + "author": ".s1@text", + "bookList": ".txt-list>li~li", + "bookUrl": "a@href", + "coverUrl": "##", + "kind": ".s4@text", + "lastChapter": ".s3@text", + "name": ".s2@text" + }, + "ruleToc": { + "chapterList": ".section-box~.section-box a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "\nvar list=[]\nfor (var i=2;i<=Number(src.slice(src.indexOf(\"let i =1 ; i <= \")+16,src.indexOf(\"; i++){\")));i++)\n{\tlist.push(book.tocUrl+\"index_\"+i.toString()+\".html\");\n}\nnextChapterUrl=list\n" + }, + "searchUrl": "/search/,{\"body\":\"searchkey={{key}}&Submit=\",\"method\": \"POST\",\"headers\":{\"Referer\":\"https://lingjingxingzhe.com/\"}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.biquge.cc-By Dark", + "customOrder": 130, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko, By Black Prism) Chrome/99.0 Safari/537.36\"\n}", + "lastUpdateTime": 0, + "respondTime": 10001, + "ruleBookInfo": { + "coverUrl": "id.fmimg.0@tag.img.0@src", + "intro": "id.intro.0@tag.p.0@text" + }, + "ruleContent": { + "content": "id.content@tag.p@text" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "class.odd.1@text", + "bookList": "class.grid@tag.tr", + "bookUrl": "class.odd.0@tag.a.0@href", + "lastChapter": "class.even.0@tag.a.0@text", + "name": "class.odd.0@tag.a.0@text" + }, + "ruleToc": { + "chapterList": "class.box_con@tag.dd!0:1:2:3:4:5:6:7:8", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href" + }, + "searchUrl": "https://www.biquge5200.cc/modules/article/search.php?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "爱笔趣阁 im", + "bookSourceType": 0, + "bookSourceUrl": "https://m.ibiquge.info", + "customOrder": 143, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "lastUpdateTime": 1702226733807, + "respondTime": 10160, + "ruleBookInfo": { + "author": "li.0@a@text", + "coverUrl": "id.thumb@src", + "intro": ".review@text", + "kind": ".sort@text##类别:", + "lastChapter": "li.3@text##更新: ", + "name": ".title@text", + "tocUrl": "id.chapterlist@h2@a@href" + }, + "ruleContent": { + "content": "id.chaptercontent@textNodes##第().*", + "nextContentUrl": ".Readpage@a.-1@href" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "p.1@a.1@text", + "bookList": ".hot_sale", + "bookUrl": "a.0@href", + "lastChapter": "p.-1@a@text", + "name": "p.0@text" + }, + "ruleToc": { + "chapterList": "id.chapterlist@p!0@a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "https://m.ibiquge.info/SearchBook.php?keyword={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "i笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.ibqg.cc", + "bookUrlPattern": "https?://www.ibqg.cc/book/\\d+/", + "customOrder": 147, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "首页::https://www.ibqg.cc\n玄幻小说::https://www.ibqg.cc/xiaoshuo/xuanhuan_{{page}}/\n武侠小说::https://www.ibqg.cc/xiaoshuo/wuxia_{{page}}/\n都市小说::https://www.ibqg.cc/xiaoshuo/dushi_{{page}}/\n历史小说::https://www.ibqg.cc/xiaoshuo/lishi_{{page}}/\n科幻小说::https://www.ibqg.cc/xiaoshuo/kehuan_{{page}}/\n网游小说::https://www.ibqg.cc/xiaoshuo/wangyou_{{page}}/\n其他小说::https://www.ibqg.cc/xiaoshuo/qita_{{page}}/\n足迹记录::https://www.ibqg.cc/history.html", + "header": "{\n\t\"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\",\n\"Referer\": \"{{baseUrl}}\"\n\t}", + "lastUpdateTime": 1694958878247, + "respondTime": 10806, + "ruleBookInfo": { + "author": "@get:{a}", + "coverUrl": "@get:{c}", + "init": "@put:{n:\"[property$=book_name]@content\",\na:\"[property$=author]@content\",\nk:\"[property~=category|status|update_time]@content\",\nl:\"[property$=lastest_chapter_name]@content\",\ni:\".m-desc.xs-show p@text\",\nc:\"[property$=image]@content\"}", + "intro": "@get:{i}", + "kind": "@get:{k}", + "lastChapter": "@get:{l}", + "name": "@get:{n}", + "tocUrl": "{{baseUrl}},{'webView': true}" + }, + "ruleContent": { + "content": "#content p!-1@html", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##天才一秒记住本站地址:.*|/最快更新!无广告!|本站已更改域名,最新域名:.*", + "title": "" + }, + "ruleExplore": { + "author": ".s4@text", + "bookList": ".txt-list li", + "bookUrl": "a@href", + "kind": ".s1@text##\\[|\\]", + "lastChapter": "a.1@text", + "name": ".s2@text" + }, + "ruleSearch": { + "author": ".s1@text", + "bookList": ".txt-list@li!0", + "bookUrl": "a.0@href", + "checkKeyWord": "剑来", + "kind": ".s4@text", + "lastChapter": "a.1@text", + "name": ".s2@text" + }, + "ruleToc": { + "chapterList": ".section-list.1@li a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "option@value||text.下一页@href" + }, + "searchUrl": "{{url=source.getKey();\ncookie.removeCookie(url)}}\n/search/,{\n \"body\": \"searchkey={{key}}\",\n \"method\": \"POST\",\n \"charset\": \"UTF-8\",\n \"headers\": {\n \"Host\": \"www.ibqg.cc\",\n \"Connection\": \"keep-alive\",\n \"Origin\": \"https://www.ibqg.cc\",\n \"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\",\n \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\",\n \"X-Requested-With\": \"mark.via\",\n \"Referer\": \"https://www.ibqg.cc/\",\n \"Accept-Language\": \"zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7\"\n }\n}", + "variableComment": "", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.yuzhaiwuh.xyz", + "customOrder": 150, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "[{'title':'排 行','url':'','style':{layout_flexGrow: 1,layout_flexBasisPercent:1}},\n{'title':'最近更新','url':'/top/weekwords/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'最近入库','url':'/top/postdate/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'总点击','url':'/top/allvisit/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'月点击','url':'/top/monthvisit/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'周点击','url':'/top/weekvisit/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'日点击','url':'/top/dayvisit/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'总推荐','url':'/top/allvote/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'月推荐','url':'/top/monthvote/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'周推荐','url':'/top/weekvote/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'日推荐','url':'/top/dayvote/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'总月票','url':'/top/allvipvote/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'总鲜花','url':'/top/allflower/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'总鸡蛋','url':'/top/allegg/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'月勤更','url':'/top/monthwords/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'周勤更','url':'/top/weekwords/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'收藏榜','url':'/top/goodnum/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'编辑推荐','url':'/top/toptime/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'新书榜','url':'/top/newhot/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'分 类','url':'','style':{layout_flexGrow: 1,layout_flexBasisPercent:1}},\n{'title':'全部','url':'/sort/0/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'玄幻奇幻','url':'/sort/1/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'武侠仙侠','url':'/sort/2/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'都市生活','url':'/sort/3/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'穿越历史','url':'/sort/4/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'科幻未来','url':'/sort/5/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'游戏竞技','url':'/sort/6/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'其他类型','url':'/sort/7/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}},\n{'title':'全本','url':'/quanben/{{page}}.html','style':{layout_flexGrow: 1,layout_flexBasisPercent:0.25}}]", + "lastUpdateTime": 0, + "respondTime": 10091, + "ruleBookInfo": { + "author": "class.red.0@text", + "coverUrl": "img@src", + "intro": "class.bookintro@text", + "kind": "", + "lastChapter": "class.book chapterlist@dd.0@text", + "name": "class.booktitle@text", + "tocUrl": "h2@a.0@href", + "wordCount": "" + }, + "ruleContent": { + "content": "class.readcontent@html", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##↑返回顶部↑" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "lastChapter": "", + "name": "", + "wordCount": "" + }, + "ruleSearch": { + "author": "class.author.0@text", + "bookList": "class.bookinfo", + "bookUrl": "class.bookinfo@tag.a@href", + "checkKeyWord": "", + "coverUrl": "a.0@href##.*/book/(\\d+)(\\d{3})/##http://img.po18.asia/image/$1/$1$2/$1$2s.jpg", + "intro": "class.update@text##.*:", + "kind": "tag.td.4@text", + "lastChapter": "class.cat@text##.*:", + "name": "class.bookname@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "id.list-chapterAll@a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "text.下一页@href" + }, + "searchUrl": "/modules/article/search.php,{\n'charset':'utf-8',\n'method':'POST',\n'body':'searchkey={{key}}'\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.xxbiqu.com", + "bookUrlPattern": "https://www.xxbiqu.com/book/\\d+/", + "customOrder": 155, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"🔹分类🔹\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻\",\"url\":\"/sort/1/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"武侠\",\"url\":\"/sort/2/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/sort/3/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"军事\",\"url\":\"/sort/4/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"网游\",\"url\":\"/sort/5/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/sort/6/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"女频\",\"url\":\"/sort/7/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"综合\",\"url\":\"/sort/8/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"🔹排行🔹\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"收藏榜\",\"url\":\"/rank/collect/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"下载榜\",\"url\":\"/rank/download/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"点击榜\",\"url\":\"/rank/visit/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"推荐榜\",\"url\":\"/rank/vote/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"字数榜\",\"url\":\"/rank/words/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"评分榜\",\"url\":\"/rank/rating/{{page}}/\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}}]", + "header": "{\"User-Agent\": \"Mozilla/5.0 (Linux; Android 9; MIX 2 Build/PKQ1.190118.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/80.0.3987.99 Mobile Safari/537.36\"}", + "lastUpdateTime": 1745159395277, + "respondTime": 10658, + "ruleBookInfo": { + "author": "@get:{a}", + "coverUrl": "@get:{c}", + "init": "@put:{n:\"[property$=book_name]@content\",\na:\"[property$=author]@content\",\nk:\"[property~=category|status|update_time]@content\",\nl:\"[property~=las?test_chapter_name]@content\",\ni:\"[property$=description]@content\",\nc:\"[property$=image]@content\"}", + "intro": "@get:{i}", + "kind": "@get:{k}", + "lastChapter": "@get:{l}", + "name": "@get:{n}" + }, + "ruleContent": { + "content": "#TextContent@html" + }, + "ruleExplore": { + "author": ".book_other@span.0@text", + "bookList": "#sitebox dl", + "bookUrl": "h3@a@href", + "coverUrl": "img@data-original", + "intro": ".book_des@text", + "kind": ".book_other@span.1@text&&h3@span@text", + "lastChapter": ".book_other@a@text", + "name": "h3@a@text", + "wordCount": "" + }, + "ruleSearch": { + "author": ".book_other@span.0@text", + "bookList": "#nr", + "bookUrl": "h3@a@href", + "coverUrl": "img@data-original", + "intro": ".book_des@text", + "kind": ".book_other@span.1@text&&h3@span@text", + "lastChapter": ".book_other@a@text", + "name": "h3@a@text", + "wordCount": ".book_other@span.2@text" + }, + "ruleToc": { + "chapterList": "#chapterList li a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "/search/{{key}}/{{page}}/", + "weight": 0 + }, + { + "bookSourceComment": "\n\nBy小新20250507", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.conglianhao.com/", + "customOrder": 162, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"全部分类\",\"url\":\"/list/{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"玄幻魔法\",\"url\":\"/list/1_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"武侠修真\",\"url\":\"/list/2_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"都市言情\",\"url\":\"/list/3_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"历史军事\",\"url\":\"/list/4_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"科幻灵异\",\"url\":\"/list/5_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"游戏竞技\",\"url\":\"/list/6_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}},{\"title\":\"其他类型\",\"url\":\"/list/7_{{page}}/\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.25}}]", + "lastUpdateTime": 1746630662336, + "respondTime": 10001, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "coverUrl": "[property=\"og:image\"]@content", + "intro": "[property=\"og:description\"]@content", + "kind": "[property=\"og:novel:category\"]@content&&[property=\"og:novel:update_time\"]@content", + "lastChapter": "[property=\"og:novel:lastest_chapter_name\"]@content", + "name": "[property=\"og:title\"]@content" + }, + "ruleContent": { + "content": "[id=\"article\"]@html", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "@js:result\n.replace(/搜书名找不到, 可以试试搜作者哦, 也许只是改名了!/g,'')\n.replace(/如果书架和阅读记录丢失了, 可以前往.*网域,再打开书架试试!/g,'')\n", + "sourceRegex": "", + "webJs": "" + }, + "ruleExplore": { + "author": "a.2@text", + "bookList": "[class=\"flex\"]@li", + "bookUrl": "a.1@href", + "coverUrl": "img@src", + "intro": "p.0@text", + "kind": "a.0@text", + "name": "a.1@text", + "wordCount": "[class=\"li_bottom\"]@div.-1@text" + }, + "ruleSearch": { + "author": "p.0@textNodes", + "bookList": "[class=\"searchresult\"]", + "bookUrl": "a.1@href", + "checkKeyWord": "", + "coverUrl": "img@src", + "intro": "p.-2@text", + "kind": "a.0@text", + "lastChapter": "p.-1@text", + "name": "a.1@text", + "wordCount": "p.0@span.0@text" + }, + "ruleToc": { + "chapterList": "[id=\"ul_all_chapters\"]@li", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "/search/,{\n\t\"body\": \"searchkey={{key}}&searchtype=all&Submit=\",\n\t\"method\": \"POST\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.yuzhai.info", + "bookUrlPattern": "https://m.yuzhai.info/yu/\\d+/", + "customOrder": 172, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "快穿::/tag/kc/{{page}}.html&&美攻::/tag/mg/{{page}}.html&&肉文::/tag/rw/{{page}}.html&&年下::/tag/nx/{{page}}.html&&年上::/tag/ns/{{page}}.html&&生子::/tag/sz/{{page}}.html&&人兽::/tag/rs/{{page}}.html&&强强::/tag/qq/{{page}}.html&&穿越::/tag/cy/{{page}}.html&&双性::/tag/sx/{{page}}.html&&强受::/tag/qs/{{page}}.html&&ABO::/tag/abo/{{page}}.html&&重生::/tag/cs/{{page}}.html&&HP::/tag/hp/{{page}}.html&&虐文::/tag/nw/{{page}}.html&&温馨::/tag/wx/{{page}}.html&&NP::/tag/np/{{page}}.html&&父子::/tag/fz/{{page}}.html&&兄弟::/tag/xd/{{page}}.html&&萌文::/tag/mw/{{page}}.html&&BE::/tag/be/{{page}}.html&&末世::/tag/ms/{{page}}.html&&人鱼::/tag/ry/{{page}}.html&&师生::/tag/ss/{{page}}.html&&网游::/tag/wy/{{page}}.html&&娱乐圈::/tag/ylq/{{page}}.html&&高干::/tag/gg/{{page}}.html&&金手指::/tag/jsz/{{page}}.html&&反攻::/tag/fg/{{page}}.html&&诱受::/tag/ys/{{page}}.html&&种田::/tag/zt/{{page}}.html&&SM调教::/tag/sm/{{page}}.html", + "header": "{\"User-Agent\": \"Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36 Edg/116.0.0.0\"}", + "lastUpdateTime": 0, + "respondTime": 10002, + "ruleBookInfo": { + "author": "@css:.article_info_td h2@text", + "intro": "@css:#novelMain pre@text", + "kind": "@css:.article_info_td a[title=\"BL\"]@text", + "lastChapter": "//td[@class=\"article_info_td\"][1]/div[3]/a/text()", + "name": "@css:.h_nav_items li:nth-of-type(2)@text", + "tocUrl": "@css:.chapterList a[style=\"color:red;\"]@href", + "wordCount": "//td[@class=\"article_info_td\"][2]/div[2]/text()##字数:" + }, + "ruleContent": { + "content": "@css:#nr1 p@text", + "nextContentUrl": "//a[@id=\"pt_next\" and text()=\"下一页\"]/@href" + }, + "ruleExplore": { + "author": "//p[1]/strong/text()", + "bookList": "//*[@class=\"common-bookele\"]", + "bookUrl": "@css:h3>a@href", + "intro": "@css:.abstract>a@text", + "kind": "", + "name": "//h3/a/text()" + }, + "ruleSearch": { + "author": "//p[1]/strong/text()", + "bookList": "//*[@class=\"common-bookele\"]", + "bookUrl": "@css:h3>a@href", + "intro": "@css:.abstract>a@text", + "kind": "", + "name": "//h3/a/text()" + }, + "ruleToc": { + "chapterList": "@css:ul a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "//select/option[not(@selected)]/@value" + }, + "searchUrl": "/search/,{\n \"body\": \"searchkey={{key}}\",\n \"method\": \"POST\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "tpyyc笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.tpyyc.com", + "customOrder": 183, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "", + "header": "{\n\"User-Agent\":\"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\",\"referer\":\"{{baseUrl}}\",\n}", + "lastUpdateTime": 1745162431332, + "respondTime": 10000, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "coverUrl": "[property=\"og:image\"]@content", + "intro": "[property=\"og:description\"]@content", + "kind": "[property=\"og:novel:category\"]@content", + "lastChapter": "[property=\"og:novel:lastest_chapter_name\"]@content", + "name": "[property=\"og:title\"]@content", + "tocUrl": "", + "wordCount": "" + }, + "ruleContent": { + "content": "#article@html", + "nextContentUrl": "" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "intro": "", + "name": "", + "wordCount": "" + }, + "ruleSearch": { + "author": ".s4@text", + "bookList": ".search-list li!0", + "bookUrl": ".s2 a@href", + "coverUrl": "", + "intro": "", + "kind": ".s1@text", + "lastChapter": ".s3@text", + "name": ".s2 a@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "dl dd", + "chapterName": "a@text", + "chapterUrl": "a@href", + "isPay": "", + "nextTocUrl": "" + }, + "searchUrl": "{{cookie.removeCookie(source.getKey())}}/search/?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://bqg.net", + "bookUrlPattern": "https?://(mjjxs|bqg123).net/v3.+", + "customOrder": 189, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Linux; Android 13; M2012K11AC Build/TKQ1.220829.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/104.0.5112.97 Mobile Safari/537.36\",\n\"Referer\": \"{{baseUrl}}\"\n}", + "lastUpdateTime": 0, + "respondTime": 4711, + "ruleBookInfo": { + "author": ".info p.0@text", + "intro": ".intro@html##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".info p.-2:-3:-4:-5@text", + "lastChapter": ".info@a.0@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": ".info h1@text", + "tocUrl": "", + "wordCount": "" + }, + "ruleContent": { + "content": "id.nr1@p!0:-1@html##\\[.*\\]" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "$.author", + "bookList": "\npi=src.match(/search_book_str']=\"(.+)\"/)[1]\n//java.log(pi)\nvar je=java.base64Decode(pi,\"utf-8\")\nvar st=decodeURIComponent(je)\n//java.log(st)\nresult=st;\nresult;\n\n$.book_list.*", + "bookUrl": "https://bqg123.net/v3_uni_0705?1#/v3/{{$.book_uni_id}}/{{$.book_id}}/,{'webView': true}", + "checkKeyWord": "剑来", + "coverUrl": "", + "kind": "@js:\nzjs=java.getString(\"$.chapter_count_geted\")\ndata=java.getString(\"$.time_update\");\nvar time=java.timeFormat(data);\nresult=String(`章节总数:共${zjs}章,更新时间:${time}`);\nresult;", + "lastChapter": "$.last_chapter_name", + "name": "$.book_name" + }, + "ruleToc": { + "chapterList": "ul.1@.even", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "{{@@a@href}},{\"webView\": true}" + }, + "searchUrl": "https://bv-jp.booktt.cc/v3/search1?ws=676360&pf=linux,armv81&keyword={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "\n\n壬二酸修复12.16", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "新笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.qidianxin.com", + "bookUrlPattern": "https?://www.qidianxin.com/book/\\d+/", + "customOrder": 191, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "玄幻奇幻::/sort/1/{{page}}/\n武侠修真::/sort/2/{{page}}/\n都市言情::/sort/3/{{page}}/\n历史军事::/sort/4/{{page}}/\n游戏竞技::/sort/5/{{page}}/\n科幻灵异::/sort/6/{{page}}/\n其他类型::/sort/7/{{page}}/\n全本小说::/quanben/sort/{{page}}/", + "header": "", + "lastUpdateTime": 1734341112882, + "loginCheckJs": "", + "loginUrl": "", + "respondTime": 10003, + "ruleBookInfo": { + "author": "[property$=author]@content", + "coverUrl": "", + "init": "", + "intro": "id.bookIntro@text", + "kind": "class.booktag@tag.span!0@text", + "lastChapter": "[property$=latest_chapter_name]@content##章节目录", + "name": "[property$=book_name]@content", + "wordCount": "class.booktag@tag.span.0@text" + }, + "ruleContent": { + "content": "id.booktxt@tag.p@text##请关闭浏览器的阅读/畅读/小说模式并且关闭广告屏蔽过滤功能,避免出现内容无法显示或者段落错乱。", + "nextContentUrl": "text.下一页@href" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "", + "bookList": "class.col-xs-4 book-coverlist", + "bookUrl": "a.0@href", + "checkKeyWord": "快穿", + "coverUrl": "a.0@style##background-image:url\\(|\\)", + "intro": ".update@ownText", + "kind": "", + "lastChapter": "a.1@text", + "name": "h4@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "#newlist@dd", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "{{cookie.removeCookie(source.key)}}/search/?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.biqu.net", + "customOrder": 192, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[\n{\n \"title\": \"♕ 分类 ♕\",\n \"url\": \"\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 1\n }\n },\n {\n \"title\": \"玄幻魔法\",\n \"url\": \"/fenlei/1_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"武侠修真\",\n \"url\": \"/fenlei/2_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"都市言情\",\n \"url\": \"/fenlei/3_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"历史军事\",\n \"url\": \"/fenlei/4_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"科幻灵异\",\n \"url\": \"/fenlei/6_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"游戏竞技\",\n \"url\": \"/fenlei/5_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"女生耽美\",\n \"url\": \"/fenlei/7_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"其他类型\",\n \"url\": \"/fenlei/8_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"♕ 排行榜 ♕\",\n \"url\": \"\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 1\n }\n },\n {\n \"title\": \"总排行榜\",\n \"url\": \"/rank/allvisit/\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"月排行榜\",\n \"url\": \"/rank/monthvisit/\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"周排行榜\",\n \"url\": \"/rank/weekvisit/\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"收 藏 榜\",\n \"url\": \"/rank/goodnum/\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"♕ 全本 ♕\",\n \"url\": \"\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 1\n }\n },\n {\n \"title\": \"玄幻魔法\",\n \"url\": \"/quanben/fenlei/1_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"武侠修真\",\n \"url\": \"/quanben/fenlei/2_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"都市言情\",\n \"url\": \"/quanben/fenlei/3_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"历史军事\",\n \"url\": \"/quanben/fenlei/4_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"科幻灵异\",\n \"url\": \"/quanben/fenlei/6_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"游戏竞技\",\n \"url\": \"/quanben/fenlei/5_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"女生耽美\",\n \"url\": \"/quanben/fenlei/7_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n },\n {\n \"title\": \"其他类型\",\n \"url\": \"/quanben/fenlei/8_{{page}}.html\",\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": 0.25\n }\n }\n]", + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Linux; Android 8.0.0; MI 5s Plus Build/OPR1.170623.032; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36\"\n}", + "lastUpdateTime": 0, + "respondTime": 10732, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "coverUrl": "[property=\"og:image\"]@content", + "intro": "[property=\"og:description\"]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property~=las?test_chapter_name]@content", + "name": "[property=\"og:novel:book_name\"]@content", + "tocUrl": "" + }, + "ruleContent": { + "content": "#chaptercontent@p@html##本章未完.*", + "nextContentUrl": "#pt_next@href" + }, + "ruleExplore": { + "author": ".author@text##作者:|(\\d.*)", + "bookList": ".hot_sale", + "bookUrl": "a@href", + "coverUrl": "a.0@href##.+\\D((\\d+)\\d{3})\\D##https://img.22biqu.com/$2/$1/$1s.jpg###", + "intro": ".review@text", + "name": ".title@text##^\\d+\\." + }, + "ruleSearch": { + "author": ".author.0@text##作者:", + "bookList": ".bookbox", + "bookUrl": "a.0@href", + "coverUrl": "a.0@img@src", + "kind": ".author.1@text##类型:", + "lastChapter": ".update@a@text", + "name": ".bookname@text" + }, + "ruleToc": { + "chapterList": ".directoryArea.1@p@a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "option@value" + }, + "searchUrl": "{{cookie.removeCookie(source.key)}}/ss/,{\n \"body\": \"searchkey={{key}}&submit=\",\n \"method\": \"POST\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁bigee", + "bookSourceType": 0, + "bookSourceUrl": "https://m.bigee.cc", + "bookUrlPattern": "https?://m.biqg.cc/book/\\d+/", + "customOrder": 202, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "玄幻::/xuanhuan/<,{{page}}.html>\n武侠::/wuxia/<,{{page}}.html>\n都市::/dushi/<,{{page}}.html>\n历史::/lishi/<,{{page}}.html>\n网游::/wangyou/<,{{page}}.html>\n科幻::/kehuan/<,{{page}}.html>\n女生::/mm/<,{{page}}.html>\n全本::/finish/<,{{page}}.html>", + "header": "{\"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 0, + "respondTime": 3812, + "ruleBookInfo": { + "author": "[property$=author]@content", + "coverUrl": "[property$=image]@content", + "intro": "[property$=description]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property$=latest_chapter_name]@content", + "name": "[property$=book_name]@content", + "tocUrl": "{{$.}}list.html", + "wordCount": "{{@@text.字数@text##字数:}}" + }, + "ruleContent": { + "content": "#chaptercontent@html", + "nextContentUrl": "text.下一@href", + "replaceRegex": "##\\s*(.*阅读模式.*)\\s*|\\s*请收藏.*cc\\s*" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "name": "" + }, + "ruleSearch": { + "author": "span@text", + "bookList": ".item", + "bookUrl": "a.1@href", + "checkKeyWord": "剑来", + "coverUrl": "img@src", + "intro": "dd@text", + "kind": "0", + "name": "a.1@text" + }, + "ruleToc": { + "chapterList": "dl@dd!0", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "/s?q={{key}},{\n \"webView\": true\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁⑫", + "bookSourceType": 0, + "bookSourceUrl": "https://www.biqg.com/", + "bookUrlPattern": "", + "customOrder": 206, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "", + "lastUpdateTime": 1687525261532, + "loginUrl": "", + "respondTime": 10001, + "ruleBookInfo": { + "author": "[property=og:novel:author]@content", + "coverUrl": "[property=og:image]@content", + "init": "", + "intro": "[property=og:description]@content", + "kind": "[property=og:novel:update_time]@content&&\n[property=og:novel:category]@content&&\n[property=og:novel:status]@content", + "lastChapter": "[property=og:novel:latest_chapter_name]@content##免费章节 |正文卷 |正文 |VIP章节 |卷1", + "name": "[property=og:novel:book_name]@content##|\\(.*|\\(.*|免费阅读|全文.*阅读|最新章节|小说|笔趣阁|免费.*", + "wordCount": "" + }, + "ruleContent": { + "content": "id.content@html##一秒记住.*无弹窗免费阅读!|笔趣阁.*最新章节!" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "kind": "", + "lastChapter": "", + "name": "" + }, + "ruleSearch": { + "author": "tag.td.2@text", + "bookList": "class.grid@tag.tr", + "bookUrl": "tag.td.0@tag.a@href", + "coverUrl": "", + "kind": "tag.td.4@text", + "lastChapter": "tag.td.1@tag.a@text##免费章节 |正文卷 |正文 |VIP章节 |卷1", + "name": "tag.td.0@tag.a@text##\\(+.*", + "wordCount": "tag.td.3@text" + }, + "ruleToc": { + "chapterList": "//*[@id=\"list\"]//dt[2]/following-sibling::dd", + "chapterName": "a@text", + "chapterUrl": "a@href", + "nextTocUrl": "" + }, + "searchUrl": "/modules/article/search.php?searchkey={{key}}&page={{page}},{\n \"charset\": \"gbk\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.xbiquwx.la", + "customOrder": 210, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "lastUpdateTime": 1756113098443, + "respondTime": 10001, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "coverUrl": "[property=\"og:image\"]@content", + "intro": "#intro@html##本站提示.*", + "kind": "[property=\"og:novel:category\"]@content&&[property=\"og:novel:status\"]@content&&[property=\"og:novel:update_time\"]@content##小说|\\s.*", + "lastChapter": "[property=\"og:novel:latest_chapter_name\"]@content##[\\((【].*?[求更谢乐发订合补加].*?[】)\\)]", + "name": "[property=\"og:novel:book_name\"]@content" + }, + "ruleContent": { + "content": "@js:dt=lr='';c=1;动=java.get(\"动\");\nif(动!=''&&!~baseUrl.indexOf(\",\")){\nresult=String(java.ajax(baseUrl+动))\n}else if(java.get(\"静\")==动)c=dt=2;\n\nr=org.jsoup.Jsoup.parse(r1=result.replace(/ /g,' '));\n查=i=java.get(\"序\");\n\nif(java.get(\"文\")==1){for(;c;c--){\nd=[\"img[data-src],img[src~=[^a-z]cid[^a-z]]\"\n,\"img:not([src~=(?i)^$|^javascript:|\\\\.gif|\\\\.png|[^a-z](cover|css|ic(on)?|load(ing|ed)?)[^a-z]])\"\n,\"img[src~=(?i)\\\\.png]:not([src~=(?i)[^a-z](cover|css|ic(on)?|load(ing|ed)?)[^a-z]])\"\n,'img[src~=(?i)\\\\.gif]:not([src~=(?i)[^a-z](cover|css|ic(on)?|load(ing|ed)?)[^a-z]])'];\n\nif(!(查!=\"\"&&(查!=-1&&((lr=r.select(d[查])).size(),true)))){\n$=it=>(lr=r.select(it)).size();\nif($(d[i=0])||$(d[i=1])||$(d[i=2])||false)break;\n\nif(c==2){dt=1;\nr=org.jsoup.Jsoup.parse(java.ajax(baseUrl+',{\"webView\":true}'))\n}else i=$(d[3])?3:-1}}\nif(i==0)lr=String(lr).replace(/(?:src=['\"][^'\"]+['\"] +)?data-/g,'');\nif(查==\"\")book.putVariable(\"序\",i);\n\n}else{\nsc=java.get(\"文\")==2?\n'[style~=(?i)text-align:center|(^| |;)color: *(rgb.(?!255[ ,]+255[ ,]+255)[\\\\d, ]*2\\\\d\\\\d|#(?=[a-f\\\\d]{3}([^a-f\\\\d]|$))(?!fff)[a-f\\\\d]*[d-f]|#(?=[a-f\\\\d]{4})(?!ffffff)([\\\\da-f]{2})*[d-f][\\\\da-f]|green|red|blue|yellow|purple|pink|brown)],script,noscript,style,header,footer,[class~=^foot|^head],[id~=^foot|^head],:has(>a):not(:has(p:matchesOwn(\\\\S),br)),a>*,:has(a):not(:matchesOwn([\\\\S\\\\s]{50,}),:has(:matchesOwn([\\\\S\\\\s]{50,}))),:matchesOwn([\\\\s\\\\S]{50})>:not(br,a,:matchesOwn([\\\\s\\\\S]{50})),:not(br,p,a,:matches([\\\\s\\\\S]{200}),:has(p,br,div:matchesOwn(,|。)+div:matchesOwn(,|。)),:has(p,br,div:matchesOwn(,|。)+div:matchesOwn(,|。)) :matchesOwn(\\\\S):not(:not(p,div,span:has(br))))'\n:\n'[style~=(?i)text-align:center|(^| |;)color: *(rgb.(?!255[ ,]+255[ ,]+255)[\\\\d, ]*2\\\\d\\\\d|#(?=[a-f\\\\d]{3}([^a-f\\\\d]|$))(?!fff)[a-f\\\\d]*[d-f]|#(?=[a-f\\\\d]{4})(?!ffffff)([\\\\da-f]{2})*[d-f][\\\\da-f]|green|red|blue|yellow|purple|pink|brown)],script,noscript,style,header,footer,[class~=^foot|^head],[id~=^foot|^head],:has(>a):not(:has(p:matchesOwn(\\\\S),br,img:not([src~=(?i)^$|^javascript:|[^a-z](css|ic(on)?|load(ing|ed)?)[^a-z]|/\\\\d+s\\\\.jpg]))),img[src~=(?i)^$|^javascript:|[^a-z](css|ic(on)?|load(ing|ed)?)[^a-z]|/\\\\d+s\\\\.jpg],a:not(:matches(^$)>img)>*,:has(a):not(img,:matchesOwn([\\\\S\\\\s]{50,}),:has(img,:matchesOwn([\\\\S\\\\s]{50,}))),:matchesOwn([\\\\s\\\\S]{50})>:not(img,br,a,:has(img),:matchesOwn([\\\\s\\\\S]{50})),:not(img,br,p,a,:matches([\\\\s\\\\S]{200}),:has(p,br,img,div:matchesOwn(,|。)+div:matchesOwn(,|。)),:has(img,p,br,div:matchesOwn(,|。)+div:matchesOwn(,|。)) :matchesOwn(\\\\S):not(:not(p,div,span:has(br))))';\n\nd=[\":matchesOwn(\\\\S):has(br):has(:matchesOwn(\\\\S):has(br))\"\n,\":matchesOwn(\\\\S):has(br)\"\n,\":has(>:matchesOwn(\\\\S):not(:has(*))+:matchesOwn(\\\\S):not(:has(*)))\"\n,\":has(>:has(>p:only-child:matchesOwn(\\\\S):not(:has(*)))+:has(>p:only-child:matchesOwn(\\\\S):not(:has(*))))\"\n,\"img\"\n,\":matchesOwn(\\\\S)\"];\n\ntry{for(查=i!=''?i:java.get(\"元\");c;c--){\n\nif(!(c>1&&(String(r.text()).length<400||r.select(':matchesOwn(内容未加载完成|关闭(阅读|小说)模式)').size()))){\nr.select(sc).remove();\n\nif(!(查!=\"\"&&(lr=r.select(i?d[i==6?5:i]:查)).size()))for(i=0;i<6&&(lr=r.select(d[i]),i==4&&c==1?!lr.size():String(lr.text()).length<200);i++);\nif(c<2||i<6)break;}\n\nr=org.jsoup.Jsoup.parse(r2=String(java.ajax(baseUrl+',{\"webView\":true}')).replace(/(<[a-z]+) /g,'$1 '));\ndt=r1.length==r2.length?2:1}\n\nfor(c=lr.first(),v=1;vString(lr.get(1).text()).length?lr.first():lr.get(1):(c=lr.size()==1)?lr.first():lr);\n\nif(查==\"\"){\nif(c&&(查=lr.match(/<([a-z]+) ([^>]+)>/))&&(查[2]=查[2].match(/(?:id|class|style)=(?:\"[^\"]+\"|'[^']+')|[^= ]+(?==\"[^\"]+\"|'[^']+')/g))){\nbook.putVariable(\"元\",查[1]+'['+查[2].join('][')+']')\n}else book.putVariable(\"序\",i)}\n\nlr=lr.replace(/<([a-z]+)[^>]*\"-\\d+\"[^>]*>[^<]+<\\/\\1>|[^<>]*[^<]*|<[; ]?\\/?[a-z]+(?= |\\/?>)(?:[ a-z=-]+|\"[^\"]+\"|'[^']+')*\\/?>[; ]?|[☯📑⚙️🌕︴]/g,\"\").replace(/\\s+(?:\\s|(?:(?:(?:n?b)?s)?p)?;)/g,\"  \");\n\nif(java.get(\"原\")!=1)lr=(!lr.indexOf(\"  \")?lr.replace(/>(?!  |\\s*(?:(?:(?:n?b)?s)?p)?;)\\s*(?=[^\\s<>])/g,\">︴\"):lr)\n.replace(/((?:[〖【『「(《〈〔[\\[(][^〖【『「(《〈〔[\\[()]〕〉》)」』】〗\\]]*[)]〕〉》)」』】〗\\]]\\s*)*(?:第?\\s*[一二三四五六七八九十百千万〇零0-9]+\\s*[章节回話话::..,,、]*\\s*)?{{\nn=(t=title.match(/\\S+$/)[0].replace(/[*$|?+\\\\\\^\\[\\](){}/]/g,\".?\")).replace(/^(正文[^\\u4e00-\\u9fa5A-Za-z]*|第?[一二三四五六七八九十百千万〇零0-9]+[章节回話话\\s::..,,、]*)+/,\"\"),n!=t&&/\\S/.test(n)?\"(?:第?\\\\s*[一二三四五六七八九十百千万〇零0-9]+\\\\s*[章节回話话::..,,、]*\\\\s*\"+n+\"|\"+t+\")\":t\n}}(?:\\s*[〖【『「(《〈〔[\\[(][^〖【『「(《〈〔[\\[()]〕〉》)」』】〗\\]]*[)]〕〉》)」』】〗\\]])*)/g,\"⚙️$1⚙️\")+\"📑\"\n\n}catch(e){}}\nif(dt)book.putVariable(dt==1?\"动\":\"静\",',{\"webView\":true}');lr" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "td.2@text", + "bookList": "tbody tr!0", + "bookUrl": "a.0@href", + "coverUrl": "a.0@href##.+\\D((\\d+)\\d{3})\\D##https://www.xbiquwx.la/files/article/image/$2/$1/$1s.jpg###", + "kind": "td.5:4@text", + "lastChapter": "a.1@text##[\\((【].*?[求更谢乐发订合补加].*?[】)\\)]", + "name": "a.0@text", + "wordCount": "td.3@text" + }, + "ruleToc": { + "chapterList": "#list dd a", + "chapterName": "text##[\\((【].*?[求更谢乐发订合补加].*?[】)\\)]", + "chapterUrl": "href" + }, + "searchUrl": "/modules/article/search.php?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "同站一大堆", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.bqgam.com", + "bookUrlPattern": "https?://m.bqgam.com/index/\\d+/", + "customOrder": 215, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "玄幻::/xuanhuan/<,{{page}}.html>\n武侠::/wuxia/<,{{page}}.html>\n都市::/dushi/<,{{page}}.html>\n历史::/lishi/<,{{page}}.html>\n网游::/wangyou/<,{{page}}.html>\n科幻::/kehuan/<,{{page}}.html>\n女生::/mm/<,{{page}}.html>\n全本::/finish/<,{{page}}.html>", + "header": "{\"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 1719013489630, + "respondTime": 3787, + "ruleBookInfo": { + "author": "[property$=author]@content", + "coverUrl": "[property$=image]@content", + "intro": "[property$=description]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property$=latest_chapter_name]@content", + "name": "[property$=book_name]@content", + "tocUrl": "{{$.}}list.html", + "wordCount": "{{@@text.字数.0@text##字数:}}" + }, + "ruleContent": { + "content": "#chaptercontent@html", + "nextContentUrl": "text.下一@href", + "replaceRegex": "##(.*阅读模式.*)|请收藏.*com" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "name": "" + }, + "ruleSearch": { + "author": "span@text", + "bookList": ".item", + "bookUrl": "a.1@href", + "checkKeyWord": "剑来", + "coverUrl": "img@src", + "intro": "dd@text", + "kind": "0", + "name": "a.1@text" + }, + "ruleToc": { + "chapterList": "dl@dd!0", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "/s?q={{key}},{\n \"webView\": true\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.biquzw.info", + "bookUrlPattern": "", + "customOrder": 217, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": false, + "exploreUrl": "[{\"title\":\"玄幻小说\",\"url\":\"/list/1_{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"修真小说\",\"url\":\"/list/2_{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"都市小说\",\"url\":\"/list/3_{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"穿越小说\",\"url\":\"/list/4_{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"网游小说\",\"url\":\"/list/5_{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"科幻小说\",\"url\":\"/list/6_{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}}]", + "header": "", + "lastUpdateTime": 1731748536539, + "loginUrl": "", + "respondTime": 10002, + "ruleBookInfo": { + "author": "id.info@tag.p.0@text##作 者:", + "coverUrl": "id.fmimg@tag.img@src", + "init": "", + "intro": "id.intro@tag.p.0@text", + "kind": "[property=og:novel:update_time]@content&&\n[property=og:novel:category]@content&&\n[property=og:novel:status]@content", + "lastChapter": "id.info@tag.p.3@a@text##免费章节 |正文卷 |正文 |VIP章节 ", + "name": "id.info@h1@text" + }, + "ruleContent": { + "content": "id.content@textNodes" + }, + "ruleExplore": { + "author": "class.s4@text", + "bookList": "class.l@li", + "bookUrl": "class.s2@a@href", + "coverUrl": "class.s2@a@href\nvar id = result.match(/_(\\d+)/)[1];\nvar iid = parseInt(id/1000);\n'https://www.xbiquwx.la/files/article/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "kind": "class.s5@text", + "lastChapter": "class.s3@a@text", + "name": "class.s2@a@text" + }, + "ruleSearch": { + "author": "class.odd.1@text", + "bookList": "class.grid@tag.tr!0", + "bookUrl": "class.odd.0@tag.a@href", + "coverUrl": "class.odd.0@tag.a@href\nvar id = result.match(/_(\\d+)/)[1];\nvar iid = parseInt(id/1000);\n'https://www.xbiquwx.la/files/article/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "lastChapter": "class.even.0@tag.a@text##免费章节 |正文卷 |正文 |VIP章节 ", + "name": "class.odd.0@tag.a@text", + "wordCount": "class.even.1@text##k##万字" + }, + "ruleToc": { + "chapterList": "id.list@tag.dd", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href" + }, + "searchUrl": "https://www.biquzw.info/modules/article/search.php?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "\n\n2022/12/23修", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "✎笔趣阁⑬④", + "bookSourceType": 0, + "bookSourceUrl": "https://m.bqgxsw.com/modules/article/search.php?searchtype=all&searchkey=%E%%%E%A%", + "bookUrlPattern": "", + "concurrentRate": "", + "customOrder": 238, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "", + "header": "{'User-Agent': 'Mozilla/5.0 (Linux; Android 8.0.0; MI 5s Plus Build/OPR1.170623.032; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36'}", + "lastUpdateTime": 1677665561964, + "loginUrl": "", + "respondTime": 10284, + "ruleBookInfo": { + "author": "@get:{x}", + "canReName": "true", + "coverUrl": "@js:(s=java.getStringList('@css:img[alt*='+book.name+']@src||meta[property$=image]@content||img[src~=(cover|file|article)[^a-z]|/\\\\d+[/_-]\\\\d+(s?\\\\.|$)]@src||img[data-src~=\\\\S]@data-src||img[src*=/img]@src||img[src~=^(data|https?):|^[^:]+/]@src')).size()?/^data:/.test(s=s.get(0))?java.base64Encode(s):s:null", + "init": "", + "intro": "@js:'  '+java.get(\"g\")", + "kind": "@js:java.get(\"v\")", + "lastChapter": "@get:{z}", + "name": "@js:\nj=String(book.customIntro).match(/^\\{((?:[录原单动静直全跳逆字图]|\\[[^\\[\\]]+\\]|\\d+#[^#]+#)+)\\}/);\n\nkey=String((u=(baseUrl=String(baseUrl).replace(/,{[^{}]+}$/,'')).match(/^(.+)\\?((?:[录原单动静直全跳逆字图]|\\[[^\\[\\]]+\\]|\\d+#[^#]+#)+)$/))&&(baseUrl=u[1])&&j?j[1]+u[2]:u?u[2]:j?j[1]:'');\n\n$=it=>(fn=r.select(it)).size()&&(fn=fn.first());\n\nr=org.jsoup.Jsoup.parse(result);\nr.select(\"script,noscript,style,head>:not(meta,title),footer,[class~=^foot],[id~=^foot],a:has(>:last-child:matchesOwn(^分类$))\").remove();\n\nm=String(r).replace(/(?: )+/g,\" \");\n\n书=false;n=String(($('[property$=book_name]')&&(书=fn.attr('content')))||(function(){\nif($('title')&&(书=String(fn.text()).replace(/^(简介页|详情页|目录页|正版|全本|免费阅读|[\\s。.,_/|《「『【〖((\\[\\]))〗】』」─—-]+)+/,\"\"))){\nfor(x=0,c=r.select(\"h1,h2,h3,strong\").eachText();x\\s*([^>]+?)(?:\\s*<\\/[a-z]+>\\s*|\\s+)著\\s*<|[\\s\\[\\];?!,.()、,;?!。…─()[]〖〗【】>《》](?:小说|漫画)?作\\s*者(?![^>]+->)(?:[::\\s〖【(《[\\[\\(]|<[^it\\/][^>]*>|<\\/[^>]+>)+([^\\s<\">,,//||\\)\\]]》)】〗]+)/);\nx=x?x[1]?x[1]:x[2]:$('#author,.author')?String(fn.text()).replace(/(?!^)[//||,,\\s][^⚙️]*$/,\"\"):\"\"}\njava.put(\"x\",x);\n\nc=(fn=r.select(\"meta[property~=category$]\")).size()?String(fn.attr(\"content\"))\n.replace(/(?!^)\\s*[,,.//||]\\s*/,\",\"):(fn=m.match(/(?:[\\s\\[\\];?!,.()、,;?!。…─()[]〖〗【】》]|<[^a/][^>]*>|<\\/[^>]+>)(?:[分大]\\s*类|类\\s*[型别])(?:[::\\s]|<[^>]+>)+([^\\s<.\"//||>]+)/))&&fn[1];\nif(c)java.put(\"v\",c);\n\nc=(fn=r.select(\"meta[property~=status$]\")).size()?fn.attr(\"content\"):(fn=m.match(/(?:[\\s\\[\\];?!,.()、,;?!。…─()[]〖〗【】》]|<[^a/][^>]*>|<\\/[^>]+>)状\\s*态(?:[::\\s]|<[^>]+>)+([^\\s<.\"//||>]+)/))&&fn[1];\nif(c)java.put(\"s\",c);\n\nc=$('meta[property~=latest_chapter_name$]')?fn.attr(\"content\"):(fn=m.match(/>(?:\\s*[更最]\\s*[新近])+(?:\\s*章\\s*节)?(?:[::\\s\\[]|<[^>]+>)+(?!\\s*(?:-|>)\\s*<|[::\\s0-9T年月日时分秒*-]{5,}<|[^::]+[^章\\s]\\s*[::]\\s*<|更新(?:时间)?[::])([^<\"//||\\]>]+)/))&&fn[1];\nif(c)java.put(\"z\",c);\n\n正=true;\nif(key.length){\nif(~key.indexOf(\"全\"))java.put(\"全\",1);\nif((\nu=key.match(/[^\\[\\]]+(?=\\])/),\nc=~key.indexOf(\"录\"),\ny=~key.indexOf(\"单\"),\nbaseUrl=u?u[0]:baseUrl,\n(c||u)&&(baseUrl=c||y?(baseUrl=String((c=baseUrl.match(/(.+[^\\d])(\\d+)([^\\d]*)$/))[1]+1+c[3]),\nc=c[2]+'🌕'+c[1]+'🌕'+2+'🌕'+c[3],\nbaseUrl):baseUrl)\n)||~key.indexOf(\"直\")\n)java.put(\"目\",1),正=1;\nif(~key.indexOf(\"录\"))java.put(\"录\",c),正=1;\nif(y||~key.indexOf(\"#\")){\nif(!u){\nif((网=key.match(/(\\d+)#([^#]+)/))\n&&(尾=网[1],网=网[2].match(/^(.*[^\\d])([12])([^/?\\d]*)$/))\n||(尾=r.select('a[href~=\\\\d[^/?\\\\d]*$]:matches(^(尾|末|最后一)[頁页篇章回节節话話]$)')).size()\n&&(网=r.select('a:matches(^2$)')).size()\n&&(网=String(网.first().attr('href')).match(/^(.*[^\\d])([12])([^/?\\d]*)$/))\n&&(尾=String(尾.first().attr('href')).match(/\\d+(?=[^/?\\d]*$)/)[0])){\nc=尾+'🌕'+网[1]+'🌕'+网[2]+'🌕'+网[3]\n}else if((c=r.select('a:matches(^(\\\\d+|…+|\\\\.+)$)')).size()){\nif((网=String(c).split(/]+>[^\\d<]+<\\/a>/)).length==2){\nfor(c=网[0],尾=网[1],x=+网[1].match(/>([^<]+)/)[1],网=网[0].match(/href=\"([^\"]*[^\\d])(\\d+)([^/?\\d\"]*)\"[^>]*>([^<]+)<[^<]+$/),j=+网[4],i=+网[2]-j;j\"+j+\"\";\nc+=尾}\n}else c=1}\njava.put(\"单\",c),正=false}\nif(~key.indexOf(\"跳\"))java.put(\"跳\",1);\nif(~key.indexOf(\"逆\"))book.setReverseToc(true);\nelse book.setReverseToc(false);\nif((u=~key.indexOf(\"图\"))||~key.indexOf(\"原\"))java.put(\"原\",1);\nif(~key.indexOf(\"动\"))java.put(\"动\",',{\"webView\":true}');\nelse if(~key.indexOf(\"静\"))java.put(\"静\",1);\nif(u||~key.indexOf(\"字\"))java.put(\"文\",u?1:2)\n}else book.setReverseToc(false);\n\nc=(fn=r.select(\"meta[property$=description][content~=\\\\S]\")).size()?fn.get(fn.size()-1).attr(\"content\"):(r.select(':matchesOwn([\\\\u4e00-\\\\u9fa5]{2,})>:not(br),:not(body,br,:matchesOwn([\\\\s\\\\S]{50}),:has(body,:matchesOwn([\\\\s\\\\S]{50})))').remove(),r.select(\":matchesOwn(\\\\S)\").text());\n\njava.put(\"g\",c=String(c).replace(/[\\snbsp;]*(?: |\\s){2,}|\\s*([?!。]+[”」』\\]\\}\\))}】〗〕〉]?)\\s*/g,\"$1  \").replace(/(?=  )/g,\"\\n\"));\n\nif(c.length&&(c=c.match(/《([^《》]+)》(?!作品集)/))&&(c=c[1],书?~String(书).indexOf(c)&&~~c.indexOf(n):true))n=c;\n\nif(正==1)m=java.ajax(baseUrl);\njava.setContent(m,baseUrl);\n\nif(正){\nzl=java.getStringList(\"@css:[property$=latest_chapter_url]@content||:matches(^最新章节)>a:only-child:not([href~=^$|#|javascript:])@href||a:matches(^正文\\\\s*[\\\\d第一二三四五六七八九十〇零百千]|^[【《]?(\"+n+\")?[\\\\s》】(\\\\u0028::__--]*(第[\\\\s0〇零]*[一1]\\\\s*[\\\\u4e00-\\\\u9fa5]|([\\\\u4e00-\\\\u9fa5]{2}阅读[(\\\\u0028::__--]?)?(0*1([)\\\\u0029.、::_-]|$)|[〇零]*一([)\\\\u0029\\\\s.、::_-]|$)))):not([href~=(^|[^/])/[vV][iI][pP]|([A-Za-z]\\\\d+|\\\\d[A-Za-z]+|[A-Z][a-z]+|[a-z][A-Z]+){3,}[^/?&_-]*$|^$|#|javascript:|\"+(bas=baseUrl.replace(/\\/$|\\.[a-zA-Z]+$/,'')).match(/[^?/]+$/)[0].replace(/([*$|?+\\\\\\^\\[\\](){}])/g,'\\\\$1')+\"(?:[/_-]1)?(?:\\\\/|\\\\.[a-zA-Z]+)?$])@href||a:matches(^[^\\\\u4e00-\\\\u9fa5]*(免费|在线|开始|立即|全文|正文|从头)+[试阅]读[^\\\\u4e00-\\\\u9fa5]*$):not([href~=^$|#|javascript:])@href||a:matches(^0*1[^\\\\d]):not([href~=(^|[^/])/[vV][iI][pP]|([A-Za-z]\\\\d+|\\\\d[A-Za-z]+|[A-Z][a-z]+|[a-z][A-Z]+){3,}[^/?&_-]*$|^$|#|javascript:])@href\");\n\nif(zl.size()&&(ck=String(zl.get(0)).match(/^(.*\\/\\/[^/]+)?([/?]?[^/].+[?&/_-])[^&/_-]+\\/?$/)))(jd=ck[1])&&(h=baseUrl.lastIndexOf('/',baseUrl.indexOf(jd.match(/(?:\\.[^.]+){2,}$|[^./]+\\.[^.]+$/)[0])))>8&&(\nq=baseUrl.indexOf(':'),\n(bas=java.get(baseUrl=baseUrl.slice(0,q+2)+baseUrl.slice(h),{})).statusCode()==200&&java.setContent(bas.body(),baseUrl)\n),java.put(\"ck\",ck[2])}\nn", + "tocUrl": "@js:if(java.get(\"单\")==''){\nif(java.get(\"录\")==java.get(\"目\")){\nr=org.jsoup.Jsoup.parse(result);\n\nscript=r.select(\":matchesOwn(^$)>a[href^=javascript:]:matches(全文|章[節节]|目[錄录]):not(:matches(最新))\");re=false;\n\nif(!script.size()){\nba=(bas=baseUrl.replace(/\\/$|\\.[a-zA-Z]+$/,'')).match(/(http....[^/?]+)(?:([?/])(.*))?$/);b=ba[1];v=ba[3];\n\nr.select(\"a[href~=[^a-z]page[^a-z]]:not(:matches([反正顺順逆倒]序|[全正]文|更多|全部|所有|章[節节]|作品|目[錄录]|列表)),a:not([href~=^(?!//)[^#:]+$|\"+b.split(':')[1]+\"]),:matchesOwn(\\\\S)>a,a[href~=javascript:|#],a:matches(^\\\\S{1,4}$):not(:matches([反正顺順逆倒]序|[阅閱][讀读]|查看|展[开開]|进入|[全正]文|更多|完整|全部|所有|目[錄录]|列表|章[節节]):not(:contains(分类)))\").remove();\n\ny=r.select(\"a[href~=(?i)catalog|contents|chapters|mulu|(^|[^a-z])ml|showchapter|(^|chapter[/_-]?)(more|list|all)|sort[/=_-]asc]\");\n\nif(!y.size()){b3='';\n\nif(ba[2]==\"?\"){\nb3='[href~=^[^?]+$],'\n}else if(ba[3]){\nb3=ba[3].replace(/[*$|?+\\\\\\^\\[\\](){}]/g,'\\\\$0');\n\nb3=\"[href~=\"+b3+\"(\\\\.[^.]+|/\\\\d+(\\\\.[^.]+|/)?)?$],[href~=[/?]\"+b3.replace(/[&/_-][^/_-]+$/,'')+\".*$]:not([href~=[/?]\"+b3.replace(/[/_-]/g,'[/_-]')+\"]),\"}\n\ny=r.select(\"a:not(\"+b3+\"[href~=(?i)(^|[^/])[?/].*((book|[^a-z])(info|case)[^a-z]|(cid|buy)[^a-z]|desc|coins|nutrition|review|ticket|update|(app|author|xiazai|down)(?!=))|/chapter|/index/|/d/],:matches((?i)^$|[0-9零〇一二三四五六七八九十百千万、,:;?!。…‘’“”()()]|[票榜::.]|推荐|排行|等级|说明|收藏|书评|简介|分[类卷]|简介|作者|手机|软件|应用|安装|客户|移动|pc|电脑|安卓|苹果|下载|最新|ap[kp]|ipa|plx|deb|exe|zip|rar|txt|epub))\")}\n\nif(y.size()){\nys=y.select(\"a[href~=(?i)catalog|contents|list|chapter|mulu|(^|[^a-z])ml|more|read|all]:matches([反正顺順逆倒]序|全文|章[節节]|目[錄录]):not(:matches(阅读)),a[href~=(?i)catalog|contents|chapter|(^|[^a-z])ml|mulu|read]:matches(更多|列表),a[href~=(?i)catalog|contents|list|chapter|mulu|(^|[^a-z])ml|more|all]:matches(^$),a:matches(^[^\\\\u4e00-\\\\u9fa50-9]*([反正顺順逆倒]序|全文(免[費费])?[阅閱][讀读]|(点击|查看|展[开開]|进入|返?回到?)*([全正]文|(更多|完整|全部|所有)?(章[節节]|(作品)?目[錄录])+)+(列表)?(\\\\s*(查看|展开)?更多)?)[^\\\\u4e00-\\\\u9fa50-9]*$)\");zt=false;\n\nif(!ys.size()){\nba=bas.match(/(\\?(?:[^=]+=)+)(.+)$/)||bas.match(/(?:[^/_-][/_-]([^/._-]+))?[/_-]([^/._-]+)(?:\\/index[^/]*)?$/);\n\nif(xi=(id=ba[2]).match(/\\?[^=]+=([^&]+)/)||id.match(/^[^\\d%]*(\\d{2,}|[1-9])$/))id=xi[1];\nif(ba[1])if(xi=ba[1].match(/^[^\\d%]*(\\d{2,})$/))id=/^\\d$/.test(id)||!xi[1].indexOf(id)?xi[1]:\"(\"+id+\"|\"+xi[1]+\")\";\n\nys=y.select(\"[href~=(?i)^((.*//[^/]+/)?[^=.]*[^\\\\d=.])?\"+id+\"([?_-][a-z=_-]*0|\\\\.[^.]+|[/?]([^\\\\d]*|[^/\\\\d]*/?|(list|more|all)([=_-][a-z]*)?\\\\d+[^\\\\d]*)?)?(&.+)?$]:not(:contains(\"+(bs=String(book.name)[0])+\"))\");\n\nif(!ys.size()){zt=true;\nys=y.select(\"[href~=(?i)^((.*//[^/]+/)?[^=.]*[^\\\\d=.])?(\\\\d+/\"+id+\"[_-]\\\\d+[^\\\\d]*|\"+id+\"[_-][a-z_-]*1[^\\\\d]*)$]:not(:matches(阅读|\"+bs+\"))\")}}\n\nif(ys.size()){\nif((re=ys.select(\"[href~=(/|^)[^.]+$]\")).size())ys=re;\nre=String(ys.first().attr(\"href\"));\n\nfor(x=1;xre.length)re=xs;\n\nif(/(?:[2-9]\\d*|1\\d+)[^\\d]*$/.test(re)){\nif(!zt&&(q=re.match(/^(.*[^/])?[&?/].*(?:catalog(ue)?|contents|(?:show)?chapters?|mulu|ml|more|all|list|page)(?:[=_-][a-z]*)?\\d+[^\\d]*$/i))&&v==(q[1]||'').replace(/^http....[^/?]+/,''))zt=true;\nif(zt)re=re.replace(/\\d+(?=[^\\d]*$)/,\"☯1\")}\n\n}}}\n\nre=String(!re?baseUrl:(java.put(\"基\",baseUrl),/^\\/[^/]/.test(re)?b+re:/^https?:/.test(re)?re:/^\\/\\//.test(re)?b.split('//')[0]+re:String(baseUrl).replace(/[^/]*$/,'')+re))}else re=baseUrl;\n\nr=re.replace(/(?:[☯?&/_-][^\\d?/&_-]*[01])+[^\\d]*$/,\"\");\njava.put(\"ba\",r);\nre=re.replace(\"☯\",\"\")}else re=baseUrl;\n\njava.get(\"跳\")==1||re==baseUrl&&/,/.test(book.bookUrl)?re+',{\"webView\":true}':re", + "wordCount": "@get:{s}" + }, + "ruleContent": { + "content": "@js:dt=lr='';c=1;动=java.get(\"动\");\nif(动!=''&&!~baseUrl.indexOf(\",\")){\nresult=String(java.ajax(baseUrl+动))\n}else if(java.get(\"静\")==动)c=dt=2;\n\nr=org.jsoup.Jsoup.parse(r1=result.replace(/ /g,' '));\n查=i=java.get(\"序\");\n\nif(java.get(\"文\")==1){for(;c;c--){\nd=[\"img[data-src],img[src~=[^a-z]cid[^a-z]]\"\n,\"img:not([src~=(?i)^$|^javascript:|\\\\.gif|\\\\.png|[^a-z](cover|css|ic(on)?|load(ing|ed)?)[^a-z]])\"\n,\"img[src~=(?i)\\\\.png]:not([src~=(?i)[^a-z](cover|css|ic(on)?|load(ing|ed)?)[^a-z]])\"\n,'img[src~=(?i)\\\\.gif]:not([src~=(?i)[^a-z](cover|css|ic(on)?|load(ing|ed)?)[^a-z]])'];\n\nif(!(查!=\"\"&&(查!=-1&&((lr=r.select(d[查])).size(),true)))){\n$=it=>(lr=r.select(it)).size();\nif($(d[i=0])||$(d[i=1])||$(d[i=2])||false)break;\n\nif(c==2){dt=1;\nr=org.jsoup.Jsoup.parse(java.ajax(baseUrl+',{\"webView\":true}'))\n}else i=$(d[3])?3:-1}}\nif(i==0)lr=String(lr).replace(/(?:src=['\"][^'\"]+['\"] +)?data-/g,'');\nif(查==\"\")book.putVariable(\"序\",i);\n\n}else{\nsc=java.get(\"文\")==2?\n'[style~=(?i)text-align:center|(^| |;)color: *(rgb.(?!255[ ,]+255[ ,]+255)[\\\\d, ]*2\\\\d\\\\d|#(?=[a-f\\\\d]{3}([^a-f\\\\d]|$))(?!fff)[a-f\\\\d]*[d-f]|#(?=[a-f\\\\d]{4})(?!ffffff)([\\\\da-f]{2})*[d-f][\\\\da-f]|green|red|blue|yellow|purple|pink|brown)],script,noscript,style,header,footer,[class~=^foot|^head],[id~=^foot|^head],:has(>a):not(:has(p:matchesOwn(\\\\S),br)),a>*,:has(a):not(:matchesOwn([\\\\S\\\\s]{50,}),:has(:matchesOwn([\\\\S\\\\s]{50,}))),:matchesOwn([\\\\s\\\\S]{50})>:not(br,a,:matchesOwn([\\\\s\\\\S]{50})),:not(br,p,a,:matches([\\\\s\\\\S]{200}),:has(p,br,div:matchesOwn(,|。)+div:matchesOwn(,|。)),:has(p,br,div:matchesOwn(,|。)+div:matchesOwn(,|。)) :matchesOwn(\\\\S):not(:not(p,div,span:has(br))))'\n:\n'[style~=(?i)text-align:center|(^| |;)color: *(rgb.(?!255[ ,]+255[ ,]+255)[\\\\d, ]*2\\\\d\\\\d|#(?=[a-f\\\\d]{3}([^a-f\\\\d]|$))(?!fff)[a-f\\\\d]*[d-f]|#(?=[a-f\\\\d]{4})(?!ffffff)([\\\\da-f]{2})*[d-f][\\\\da-f]|green|red|blue|yellow|purple|pink|brown)],script,noscript,style,header,footer,[class~=^foot|^head],[id~=^foot|^head],:has(>a):not(:has(p:matchesOwn(\\\\S),br,img:not([src~=(?i)^$|^javascript:|[^a-z](css|ic(on)?|load(ing|ed)?)[^a-z]|/\\\\d+s\\\\.jpg]))),img[src~=(?i)^$|^javascript:|[^a-z](css|ic(on)?|load(ing|ed)?)[^a-z]|/\\\\d+s\\\\.jpg],a:not(:matches(^$)>img)>*,:has(a):not(img,:matchesOwn([\\\\S\\\\s]{50,}),:has(img,:matchesOwn([\\\\S\\\\s]{50,}))),:matchesOwn([\\\\s\\\\S]{50})>:not(img,br,a,:has(img),:matchesOwn([\\\\s\\\\S]{50})),:not(img,br,p,a,:matches([\\\\s\\\\S]{200}),:has(p,br,img,div:matchesOwn(,|。)+div:matchesOwn(,|。)),:has(img,p,br,div:matchesOwn(,|。)+div:matchesOwn(,|。)) :matchesOwn(\\\\S):not(:not(p,div,span:has(br))))';\n\nd=[\":matchesOwn(\\\\S):has(br):has(:matchesOwn(\\\\S):has(br))\"\n,\":matchesOwn(\\\\S):has(br)\"\n,\":has(>:matchesOwn(\\\\S):not(:has(*))+:matchesOwn(\\\\S):not(:has(*)))\"\n,\":has(>:has(>p:only-child:matchesOwn(\\\\S):not(:has(*)))+:has(>p:only-child:matchesOwn(\\\\S):not(:has(*))))\"\n,\"img\"\n,\":matchesOwn(\\\\S)\"];\n\ntry{for(查=i!=''?i:java.get(\"元\");c;c--){\n\nif(!(c>1&&(String(r.text()).length<400||r.select(':matchesOwn(内容未加载完成|关闭(阅读|小说)模式)').size()))){\nr.select(sc).remove();\n\nif(!(查!=\"\"&&(lr=r.select(i?d[i==6?5:i]:查)).size()))for(i=0;i<6&&(lr=r.select(d[i]),i==4&&c==1?!lr.size():String(lr.text()).length<200);i++);\nif(c<2||i<6)break;}\n\nr=org.jsoup.Jsoup.parse(r2=String(java.ajax(baseUrl+',{\"webView\":true}')).replace(/(<[a-z]+) /g,'$1 '));\ndt=r1.length==r2.length?2:1}\n\nfor(c=lr.first(),v=1;vString(lr.get(1).text()).length?lr.first():lr.get(1):(c=lr.size()==1)?lr.first():lr);\n\nif(查==\"\"){\nif(c&&(查=lr.match(/<([a-z]+) ([^>]+)>/))&&(查[2]=查[2].match(/(?:id|class|style)=(?:\"[^\"]+\"|'[^']+')|[^= ]+(?==\"[^\"]+\"|'[^']+')/g))){\nbook.putVariable(\"元\",查[1]+'['+查[2].join('][')+']')\n}else book.putVariable(\"序\",i)}\n\nlr=lr.replace(/<([a-z]+)[^>]*\"-\\d+\"[^>]*>[^<]+<\\/\\1>|[^<>]*[^<]*|<[; ]?\\/?[a-z]+(?= |\\/?>)(?:[ a-z=-]+|\"[^\"]+\"|'[^']+')*\\/?>[; ]?|[☯📑⚙️🌕︴]/g,\"\").replace(/\\s+(?:\\s|(?:(?:(?:n?b)?s)?p)?;)/g,\"  \");\n\nif(java.get(\"原\")!=1)lr=(!lr.indexOf(\"  \")?lr.replace(/>(?!  |\\s*(?:(?:(?:n?b)?s)?p)?;)\\s*(?=[^\\s<>])/g,\">︴\"):lr).replace(/((?:[〖【『「(《〈〔[\\[(][^〖【『「(《〈〔[\\[()]〕〉》)」』】〗\\]]*[)]〕〉》)」』】〗\\]]\\s*)*(?:第?\\s*[一二三四五六七八九十百千万〇零0-9]+\\s*[章节回話话::..,,、]*\\s*)?{{n=(t=title.match(/\\S+$/)[0].replace(/[*$|?+\\\\\\^\\[\\](){}/]/g,\".?\")).replace(/^(正文[^\\u4e00-\\u9fa5A-Za-z]*|第?[一二三四五六七八九十百千万〇零0-9]+[章节回話话\\s::..,,、]*)+/,\"\"),n!=t&&/\\S/.test(n)?\"(?:第?\\\\s*[一二三四五六七八九十百千万〇零0-9]+\\\\s*[章节回話话::..,,、]*\\\\s*\"+n+\"|\"+t+\")\":t}}(?:\\s*[〖【『「(《〈〔[\\[(][^〖【『「(《〈〔[\\[()]〕〉》)」』】〗\\]]*[)]〕〉》)」』】〗\\]])*)/g,\"⚙️$1⚙️\")+\"📑\"\n}catch(e){}}\nif(dt)book.putVariable(dt==1?\"动\":\"静\",',{\"webView\":true}');lr", + "imageStyle": "FULL", + "nextContentUrl": "@js:if((r=java.getStringList((nx=java.get(\"next\"))+\"a:matches(第二[頁页]|下[一\\\\s]*[頁页]):not([href~=^javascript:|^#])@href||a:matches(下[一\\\\s]*[篇章回节節话話]):not([href~=^javascript:|^#])@href||a[href~=[_-]\\\\d+(/|\\\\.[a-z]+)?$]:has(i,img):not(:matches(\\\\S),[href~=^javascript:|^#])@href\")).size()){\nif(~String(book.tocUrl+(nextChapterUrl||'')).indexOf(r=String(r.get(r.size()-1)))||~r.indexOf(nextChapterUrl||String(book.tocUrl).replace(/.+(?=_\\d+\\/$)/,'')))r=null\n}else r=null;\nif(r){if(nx!='')chapter.putVariable(\"next\",(n=r.replace(/\\d+(?=[^\\d]*$)/,it=>+it+1))==r?'':'a[href=\"'+n+'\"]@href||');\nr+java.get(\"动\")}", + "replaceRegex": "@js:if(java.get(\"原\")==1){result}else{\nresult=result.replace(/⚙️([^\\u4e00-\\u9fa5⚙️]+|[零〇一二三四五六七八九十百千万\\s]+|(?!第[零〇一二三四五六七八九十])[\\u4e00-\\u9fa5]+)⚙️/g,'$1').split('📑');\nl=result.length-1;\nwhile(l)if(r=result[--l].match(/^(\\n+(?:[^\\n]{1,30}\\n+){1,3})([\\S\\s]+)$/))result[l]=r[1]+r[2].replace(/⚙️/g,'');\nresult.join('📑')\n.replace(/—/g,\"─\")\n.replace(/[·۰•・]/g,\"・\")\n.replace(/\\s*(?:︴[^\\n ]+\\s+)*📑\\s*|^\\s*(︴[^\\n ]+\\s+)*|\\s*(?:  |\\n)(?:[\\s&#a-zA-Z0-9]*;)?[\\s ?]*/g,\"☯\")\n.replace(/(?:︴[^☯]*☯)+(?=︴)|☯(?:温馨|[^a-zA-Z0-9\\u4e00-\\u9fa5☯]+)提示.?[::][^☯]+|☯[^☯]+字体不会安装.?请百度[^☯]+|☯.{0,2}正版订阅在阅文旗下的起点和QQ书城,可以第一时间看到最新章节.{0,2}(?=☯)|︴\\s*第?[〖【『「(《〈〔[{\\[{(][^☯]+(?=☯)|☯第\\s*[((][^(())]+[))][頁页](?=☯)|[^☯]*(?:关注|[微vVVv][^\\u4e00-\\u9fa5A-Za-z0-9]*[信xXxX]|公[^\\u4e00-\\u9fa5]*众[^\\u4e00-\\u9fa5]*号){2,}[^☯]*|(?:&#?[a-z0-9]*;?)+(?=;|☯);*|read[sx][;;??]+/g,\"\")\n.replace(/\\s+/g,\" \")\n.replace(/☯[ ☯]*/g,\"\\n\")\n.replace(/(\\n[〖【『「(《〈〔[{\\[{(]?)([pPPp][SssS]+(?=[ ::;;,,、。.─\\d\\u4e00-\\u9fa5])[^\\n]+)/g,\"$1🌕$2\")\n.replace(/\\[\\[\\[[^\\[\\]]+http:file2.qidian.comchapters(.{5,6})(..)([^\\[\\]]{35})\\]\\]\\]/g,'')\n.replace(/(]*(src=\"[^\"]+\")[^>]*(>)|((?: ?[++--]?[0-90-9]+(?:[..//][0-90-9]+)?[%%::]?[a-zA-Z]*)+ ?|[ __--]?[a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9]+)/g,\"$1$2$3☯$4☯\")\n.replace(/☯[・。](?=☯)/g,\"☯.\")\n.replace(/,\\n/g,\",\")\n.replace(/!(?=\\n)/g,\"!\")\n.replace(/\\?(?=\\n)/g,\"?\")\n.replace(/(<[^<]+)☯☯/g,\"⚙️$1⚙️\\n\")\n.replace(/(?:\\n[^āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ\\u4e00-\\u9fa5\\n〖【『「《“‘<(〈〔[{({\\[\\]})}]〕〉)>’”》」』】〗、,:;?!。…─🌕︴\\n]+|([。?!…”』」】〗])[^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5\\n〖【『「《“‘<(〈〔[{({\\[\\]})}]〕〉)>’”》」』】〗、,:;?!。…─☯︴\\n]+[\\u4e00-\\u9fa5]+)?[^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5\\n〖【『「《“‘<(〈〔[{({\\[\\]})}]〕〉)>’”》」』】〗、,:;?!。…─⚙️☯︴\\n]+(?=\\n)|[^☯!\"'(),\\s\\u4e00-\\u9fa5、,:;?!。…─〖【『「《“‘<(〈〔[{}]〕〉)>’”》」』】〗]*☯([a-zA-ZA-Za-z]*[āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ][a-zA-ZA-Za-z]*)☯[^☯!\"'(),\\s\\u4e00-\\u9fa5、,:;?!。…─〖【『「《“‘<(〈〔[{}]〕〉)>’”》」』】〗]*/g,\"$1$2\")\n.replace(/[^!\"'(),\\s\\u4e00-\\u9fa5、,:;?!。…─〖【『「《“‘<<(〈〔[{({\\[\\]})}]〕〉)>>’”》」』】〗︴🌕]*(?:☯[^..!(),\\n\\u4e00-\\u9fa5、,:;?!。…─〖【『「《“‘<(〈〔[{({\\[\\]})}]〕〉)>’”》」』】〗⚙️]+|([^☯!(),\\na-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5、,:;?!。…─〖【『「《“‘<<(〈〔[{({\\[\\]})}]〕〉)>>’”》」』】〗⚙️])(?!\\1)|☯(?=[^☯!(),\\na-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5、,:;?!。…─〖【『「《“‘<(〈〔[{({\\[\\]})}]〕〉)>’”》」』】〗][!(),\\s\\u4e00-\\u9fa5、,:;?!。…─〖【『「《“‘<(〈〔[{({\\[\\]})}]〕〉)>’”》」』】〗])|[〖【『「(《〈〔[{\\[{(][^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]*[)}]〕〉》)」』】〗\\]}]){4,}|[^;;%%~~??!!\"'”’。…─\\na-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]*[〖【『「(《〈〔[{\\[{(][^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]*[)}]〕〉》)」』】〗\\]}](?:[^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]+(?=[)}]〕〉》)」』】〗\\]}]))?/g,\"📑\")\n.replace(/([;;%%~~??!!\"'”’。…─])📑(?=[)}]〕〉》)」』】〗\\]}])/g,'$1')\n.replace(/(?:([\\n。?!])[^\\n。?!]{1,10})?[〖【『「(《〈〔[{\\[{(][^〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]*(?:☯[^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]☯|📑|🌕)[^〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]*[}]〕〉》)」』】〗\\]})](?:[^。]{0,5}(?:[〖【『「(《〈〔[{\\[{(][^〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]*(?:☯[^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]☯|📑)[^〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]*[}]〕〉》)」』】〗\\]})]|[^📑!,()\\s\\u4e00-\\u9fa5、,:;?!。…─‘’“”()《》〖〗【】]+))*(?:(?:[^\\n]{1,10}|(?:[\\u4e00-\\u9fa5]{0,5}[,、])*[\\u4e00-\\u9fa5]+[!。]?)(?=\\n))?/g,\"$1\")\n.replace(/((?:[请請][记記]住)?[^⚙️🌕📑︴*\\n(),\\u4e00-\\u9fa5、,:;?!。…─「」‘’“”()《》〖〗【】]*(?:(?:[^⚙️🌕📑︴*\\n(),\\u4e00-\\u9fa5、:,;?!。…─「」‘’“”()《》〖〗【】]+[*(), \\u4e00-\\u9fa5、,::;?!。…─「」‘’“”()《》〖〗【】]{1,3})*[^*︴📑☯⚙️🌕\\s&;(),āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜa-zA-ZA-Za-z0-90-9\\u4e00-\\u9fa5、,,::;;??!!。…~~─'\"‘’“”【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}%%・//==__..++--]+(?!(?:[^>]+>)?⚙️)|☯[^☯\\n]+☯\\.☯[^☯\\n]+☯|本[文书](?:首发|来自)|(?:章节不完整.?|完整章节)?请.{2,6}(?:搜索|百度|引擎|关键[词字])+|(?:[阅閱][讀读]|访问|訪問|[網网地][站址]|链接|鏈接|[\\u4e00-\\u9fa50-9a-zA-Z]{0,4}(?:文学|中文|[網网]))+[::]|[\\u4e00-\\u9fa50-9a-zA-Z]{0,6}(?:[網网地][站址]|链接|鏈接)为|.{2,4}文[学學][網网]|[请請].{2,4}收藏(?:.{0,2}推[荐薦])?|(?:天才)一秒[記记]住|,?請?訪問|(?:[更最][快新]|[快高]速|热门|(?:[阅閱][讀读])?完整|本|(?:官[網网]|本站).{0,2})(?:章[節节]|小说|更新|[網网地][址站][::]?|快速)|更新(?:速度)?[最更]?快|(?:(?:无错)?小说|正版|免费|全[文本]|在线|访问|訪問|txt|TXT|继续|各种|推[荐薦]|收藏|分享|交流|你?喜欢的)+(?:全文|免费|在线|[阅閱]?[讀读]|下载|章[節节]|小说|好书)|章[節节](?:目录|列表)|小说[網网](?:更新)?|手打全文|[纯全](?:手打|文字)|手机看书|手机版|笔趣阁|(?:本站|[網网]站|站内|看)?无(?:任何)?(?:广告|弹窗|防盗)(?:章节)?|不弹窗|(?:(?:請|请|或者?)?(?:訪問|访问|[電电手][腦脑机機]|[網网][址站]|用[户戶]|浏览|瀏覽|閱讀|阅读|看小说|看漫画)){2,}|看小说|看漫画|(?:用[^\\n]{2,6})?(?:[追看]书|小说)(?:就用|软件|☯[^☯]+☯),?|用[^\\n]{2,6}[追看]书|(?:..的)?(?:最佳选择|阅读体验)|(?:推[荐薦]|收藏|分享)(?:本站|我们)|小说..版|文章质量更好|谢谢各位(?:读者|书友)支持|水印广告测试|本章未完|请翻页|点击下一页|领.{0,2}红包|同步更新《[^《》]+》|关注[〖【『「(《〈〔[{\\[{(][^〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]+[)}]〕〉》)」』】〗\\]}])(?:[* (),\\u4e00-\\u9fa5、,:;?!。…─‘’“”()《》〖〗【】]{0,3}[^⚙️📑*\\n(),\\u4e00-\\u9fa5、,:;?!。…─「」‘’“”()《》〖〗【】]+|[….。!!]+(?=\\n))*|⚙️(?!<)[^⚙️\\n]+⚙️)/g,\"\\n⚙️$1⚙️\\n\")\n.replace(/⚙️(<[^<>]+>)⚙️\\n|[〖【『「(《〈〔[{\\[{(](?:[^〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]{0,3}\\n⚙️(?!\\s*\\S\\s*⚙️).+⚙️\\n*)+[^〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]{0,3}[)}]〕〉》)」』】〗\\]}]|(🌕)\\s*⚙️|⚙️\\n[^\\u4e00-\\u9fa5]{0,5}\\n⚙️|\\n⚙️\\s*(\\S)\\s*⚙️\\n(?![^⚙️]{1,3}⚙️)|(⚙️\\n)|([,、;])[\\s,,。、…::;;??!!`’]*[,,。、…::;;??!!`’](?!⚙️)|\\n[\\s,,。、…::;;??!!`’]*(?=[,,。、::;;??!!])|([:?!。…─])(?:[^⚙️☯a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5\\n…─“‘’”〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]+(?=\\n)|[^⚙️☯a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5\\n…─“‘’”〖【『「(《〈〔[{\\[{()}]〕〉》)」』】〗\\]}]+)|☯/g,\"$1$2$3$4$5$6\")\n.replace(/([))?!?!…」』”\\s。])(?:[^\\n((🌕))”』」’。]{0,5}(?:(?:\\n⚙️.+⚙️\\n|[^(())\\n”』」。]{0,5}📑)[^(())\\n。]{0,5}(?:\\n⚙️.+⚙️(?:\\n(?=\\n⚙️|[^(())\\n]{1,5}\\n))?|[^(())\\n。]{0,5}📑(?:[^📑]{1,5}(?=\\n⚙️))?)+(?:[^(())\\n]{1,5}(?=\\n))?|(?:(?:(?:\\n⚙️.+⚙️\\n|[^(())\\n。]{0,5}📑)[^(())\\n]{0,5}){2,}|[^((🌕))”』」’\\n。]{0,5}(?:(?:請|关键[词字]|[電电手][腦機]|用戶|瀏覽|[阅閱]讀|推[薦荐]|收藏|分享|文章质量|谢谢(各位|支持|大家)|水印|翻页|点击|下[一\\s]*[页頁篇章回节節话話]|免费|热门|官网|正版|在线|同步|下载|文学|中文|章节|[本网][站址]|站内|小说|更新|无错|[本全][文本章]|首发|广个?告|弹窗|防盗|访问|訪問|txt|TXT|读者|书友|百度|[插说]一?[下句]话?,[\\u4e00-\\u9fa5]{0,5})[^(())\\n]{0,5})*(?:📑|\\n⚙️[^\\n]+⚙️\\n)[^(())\\n]{0,5}(?:(?:請|关键[词字]|[電电手][腦機]|用戶|瀏覽|[阅閱]讀|推[薦荐]|收藏|分享|文章质量|谢谢(各位|支持|大家)|水印|翻页|点击|下[一\\s]*[页頁篇章回节節话話]|免费|热门|官网|正版|在线|下载|同步|文学|中文|章节|[本网][站址]|站内|小说|更新|无错|[本全][文本章]|首发|广个?告|弹窗|防盗|访问|訪問|txt|TXT|读者|书友|百度)[^(())\\n]{0,5})*|[^((🌕))\\n”』」’。]{0,3}\\n⚙️.+⚙️\\n[^(())\\n。]{0,3})(?:[\\u4e00-\\u9fa5]+[!。]?|[^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5\\n]+)?(?=\\n)))+|\\n⚙️[^\\u4e00-\\u9fa5]+⚙️\\n|📑|\\s*︴\\s*|\\n?⚙️\\n?|\\n[^a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5\\n]{2,}\\n[^🌕a-zA-ZA-Za-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ0-90-9\\u4e00-\\u9fa5\\n“《‘【〖『「]*/g,\"$1\")\n.replace(/(?:(?:\\n🌕[^\\n]*)+(?:\\n+[^\\n]+){0,4}|\\n(?:[(({{][^((<{{}}>))]*(?:(?:[(({{][^((<{{}}>))]*[))}}])[^((<{{}}>))]*)*[))}}]?\\s*)+|[^\\n]*起点(?:原创|中文)[^\\n]*|[〖【『「(《〈〔[{\\[{(]?未完待续[^\\n]*)[^\\u4e00-\\u9fa5<>]*$|^\\s*(?:(?:[^\\n]+\\n+){0,4}(?:🌕[^\\n]*\\n?)+|(?:[(({{][^((<{{}}>))]*(?:(?:[(({{][^((<{{}}>))]*[))}}])[^((<{{}}>))]*)*[))}}]?\\s*)+|,[^\\n]+)\\n|(?:^|\\n)[^a-z0-9A-Z\\u4e00-\\u9fa5]*(?=\\n)|🌕|\\n\\s*[a-zA-Z]+\\s*$/g,\"\")}", + "sourceRegex": "", + "webJs": "" + }, + "ruleExplore": { + "bookList": "", + "bookUrl": "" + }, + "ruleSearch": { + "author": "text.作者:@text##作者:", + "bookList": ".bookbox", + "bookUrl": "a@href##必填。现有的只是碰运气‼️⑸", + "coverUrl": "img@src", + "intro": "text.简介:@text##简介:", + "kind": "text.状态:@text&&text.分类:@text&&text.更新:@text&&text.更新时间:@text##状态:|分类:|更新:|更新时间:", + "lastChapter": "text.最新章节@text##最新章节:", + "name": "a@text##必填。现有的只是碰运气‼️⑷", + "wordCount": "text.字数:@text##字数:" + }, + "ruleToc": { + "chapterList": "@js:if(java.get(\"单\")==''){\nsrc=org.jsoup.Jsoup.parse(src);\n\nif((result=java.get(\"录\"))==\"\"){if(页=(result=src.select('a[href~=\\\\S]:matches(下[一\\\\s]*[页頁]|下[一二三四五六七八九十百千万〇零0-9]{2,}章):not([href~=^#|javascript:])')).size())result=result.first().attr('href')\n}else{网=String(result).split(\"🌕\");\nfor(i=3,页=+网[0],result=网[1]+2+网[3];i<=页;i++)result+='\\n'+网[1]+i+网[3];\nresult=String(result)}\n\nif(页)java.put(\"页\",/,/.test(book.tocUrl)?result.split('\\n').join(',{\"webView\":true}\\n')+',{\"webView\":true}':result);\n\n嗅=()=>String(src).match(/[\\[(]([\"'])<[a-z]+[ >][^\\[\\]()]+<\\/a>(?:[^\\[\\],()]*<\\/[a-z]+>)?\\1[\\])]/);\n转=it=>it.replace(/\\\\[Uu]([0-9a-zA-Z]{4})/g,(_,it)=>String.fromCharCode(parseInt('0x'+it)));\n兜=()=>src.select(':matchesOwn(^$|[0-9〇一二三四五六七八九十])>a:matches(\\\\S):not(:has(*>*>:not(span)),[href~=(?i)passport|\\\\.aspx$|\\\\.php$|^https://[^/]+(/|index\\\\.[a-z]+)?$|(^|[^/])[?/].*((book|[^a-z])(info|case|page|reg|Game|Play)[^a-z]|buy[^a-z]|SystemInfo|hot|sort|desc|asc|top|coins|nutrition|review|ticket|update|(app|author|xiazai|down)(?!=))|/list\\\\d*([/_-][^/_-]+/?)?$|\\\\d+_\\\\d+_\\\\d+|target=iframe|https%],:matches((?i)^((点击|软件|应用|安装|客户|移动|手机|电脑|安卓|苹果|下载|阅读|pc|ap[kp]|ipa|plx|deb|exe|zip|rar|txt|epub)[\\\\s.端版]*(?=$|点击|软件|应用|安装|客户|移动|手机|电脑|安卓|苹果|下载|阅读|pc|ap[kp]|ipa|plx|deb|exe|zip|rar|txt|epub)|[A-Za-z0-9\\\\u4e00-\\\\u9fa5]?返回.*(简介|书页|目录)[A-Za-z0-9\\\\u4e00-\\\\u9fa5]?|[<>-]+|\\\\d+-\\\\d+章|书页|目录|简介|[上下首尾]([一\\\\s]*[页頁]|[\\\\s\\\\d零〇一二三四五六七八九十百]*章)|[↑\\\\[]?[倒正反逆顺順]序[↓\\\\]]?)$))');\n\nif((zt=java.get(\"嗅\"))!=''){\nif(zt>0&&(嗅=嗅())){\nsrc=嗅[0];\nif(zt==2)src=转(src);\nsrc=org.jsoup.Jsoup.parse(src)}\nsrc.select(java.get(\"除\")).remove();\nif(java.get(\"兜\")==1)src=兜();\nsrc=src.select(java.get(\"查\"))\n\nif(!页){for(首=String(java.get(\"首\")).split('\\n'),ss=src.size(),i=0;i1)src.subList(1,i).clear();break;}}\n\n}else{\nbook.putVariable(\"除\",除=\"meta,link,a:has(img),\"+((zt=java.get(\"全\")!=1)?\"a[href$=\"+String(book.tocUrl).replace(/,\\{\"webView\":true\\}|^.{8}[^/]*/g,'')+\"],\":\"\")+(基=String(java.get(\"基\")),基==''?'':\"a[href$=\"+基.replace(/^.{8}[^/]*/,'')+\"],\")+\"a[href~=javascript:|#|[a-z]+[A-Z][a-z]+Id[=_-]|[^/][/?&]sub[A-Z]],a:matches(^$|最新章节$|^[^\\\\u4e00-\\\\u9fa5A-Z0-9]*(正文|.{0,2}书架|(免费|在线|开始|立即|全文|从头|点击|正文)+[试阅]读|[^\\\\s\\\\d外内楔前后卷篇章]*(更新调整|[两一二三四五六七八九十]+连更|作者[::给要有]|双倍月票|感言|推书|推[a-z0-9A-Z_\\\\u4e00-\\\\u9fa5-]+书|[求个请投点下张](月?票|收藏|订阅|推荐)|(感谢|作者)[^\\\\s]*(读者|书友|大家|各位)|[书点]评[^\\\\s]*活动|[没有空]更新|没时间更新|请个?假|关于本书|关于更新|打赏名单|起点活动)[^\\\\s]*)[^\\\\u4e00-\\\\u9fa5A-Z0-9]*$)\");\n\nbook.putVariable(\"嗅\",(基=嗅())?(src=org.jsoup.Jsoup.parse((zt=/\\\\[Uu]([0-9a-zA-Z]{4})/.test(基=基[0]))?转(基):基),zt)?2:1:0);\nsrc.select(除).remove();\n\nif(zt=java.get(\"全\")!=1){\n找=ll=null;\n$=it=>(qc=ll,查=找,ll=src.select(找=it)).size()>14&&(查=it,src=re=ll);\n\nif(!(((ck=java.get(\"ck\"))!=\"\"&&$(\"[href~=\"+ck+\"(?!index(/|\\.[a-z]+)?$)[^.?/_-][^&/_-]*/?$|/[vV][iI][pP][_-]?([Rr]ead|[Cc]hapter)|([A-Za-z]\\\\d+|\\\\d[A-Za-z]+|[A-Z][a-z]+|[a-z][A-Z]+){3,}[^/?&]*$]\"))||$(\"[href~=^[a-z0-9]+(/|\\\\.[^./]+)?$]\"))){\nif(qc&&qc.size()>ll.size())找=查,ll=qc;\n\n$=it=>(re=src.select(it)).size()&&(查=it,src=re);\n\nif(!($(\"[data-cid]\")||$(\"[href~=(?i)(^|[/_-])(chapter|read)+([_-]?id)?/[^/_-]+[/_-][^/_-]+]\")||$(\"[href~=(?i)(^|[&?/_-](book|novel|comic|manhua|mh?)?)(chapter|read)+([_-]?id)?[?/=]]\")||$(\"[href~=(?i)[&?/_-]cid[&?/_=-]]\")||$(\"[data-href]\"))){\nsrc=兜();\nbook.putVariable(\"兜\",1);\n\nba=(ba=String(java.get(\"ba\"))).match(/(\\?(?:[^=]+=)+)(.+)$/)||ba.match(/(?:[^/_-][/_-]([^/._-]+))?[/_-]([^/._-]+)(?:\\/|\\/index[^/]*|\\.[^/.]+)?$/);\n\nif(xi=(id=ba[2]).match(/\\?[^=]+=([^&]+)/)||id.match(/^[^\\d%]*(\\d{2,}|[1-9])$/)||ba[1]&&ba[1].match(/^[^\\d%]*(\\d{2,})$/))id=xi[1];\n\n$(\"[href~=([^\\\\d]|^)\"+id+\"[/_&-][^\\\\d]*\\\\d+]:not([href~=\"+id+\"[^\\\\d]*$]),[href~=/[vV][iI][pP]|([A-Za-z]\\\\d+|\\\\d[a-zA-Z]+|[A-Z][a-z]+|[a-z][A-Z]+){3,}[^/?]*$|([^\\\\d]|^)\"+id+\"[/_&-][^\\\\d]*\"+id+\"(/|\\\\.[^.]+|&.+)?$]\")||$(\"[title]\")||$(\"[href~=/view/\\\\d+\\\\.[a-zA-Z]+$]\")}\nif(re.size()1){\nfor(i=+网[2],x=+网[0],j=2,list=\"正文1\";i<=x;i++,j++)list+=\"正文\"+j+\"\"}\n\norg.jsoup.Jsoup.parse(list).select('a')\n\n}else [\"正文\"]", + "chapterName": "text", + "chapterUrl": "href@js:chapter.putVariable(\"next\",'a[href~=(?:^|[?/])'+(r=result.trim().replace(/([*$|?+\\\\\\^\\[\\](){}/])/g,'\\\\$1')).match(/[^?/=.]+(?:=(?![01]$)[^=]+)*(?=\\/?$|\\.[^?/.]+$|=[01]$)/)[0]+'[=/_-]2(?:\\\\/|\\\\.[^?/.]+)? *$]@href||a[href~=(?:^|[?/])'+r.replace(/\\d+(?=[^\\d]*$)/,it=>+it+1)+' *$]@href||');result+java.get(\"动\")", + "isVip": "", + "nextTocUrl": "@js:java.get(\"页\")", + "updateTime": "" + }, + "searchUrl": "?searchtype=all&searchkey={{key}},{\"charset\":\"utf8\",\"headers\":{\"User-Agent\":\"Mozilla/5.0 (Linux; Android 7.0; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/48.0.2564.116 Mobile Safari/537.36 T7/10.3 SearchCraft/2.6.2 (baiduboxapp; P1 7.0)\",\"Referer\":\"https://m.bqgxsw.com/modules/article/search.php?searchtype=all&searchkey=%E6%88%91%E7%9A%84\",\"Cookie\":\"__tins__20043175=%7B%22sid%22%3A%201671638079332%2C%20%22vd%22%3A%201%2C%20%22expires%22%3A%201671639879332%7D; __51cke__=; __51laig__=1\"},\"webView\":true}", + "weight": 0 + }, + { + "bookSourceComment": "\n\n25.4.27", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.bvquge.com/", + "customOrder": 308, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "@js:\n\nvar sort=[],\npush=(title,surl,size)=>sort.push({\n title: title,\n url: surl,\n style: {\n layout_flexGrow: 1,\n layout_flexBasisPercent: size,\n }\n });\n\npush(\"༺ˇ»`ʚ排行榜ɞ´«ˇ༻\",null,1,1);\n\n[[\"玄幻小说\",\"xuanhuan\"],[\"仙侠小说\",\"xianxia\"],[\"都市小说\",\"dushi\"],[\"历史小说\",\"lishi\"],[\"科幻小说\",\"kehuan\"],[\"轻小说\",\"qing\"],[\"悬疑小说\",\"xuanyi\"],[\"奇幻小说\",\"qihuan\"],[\"游戏小说\",\"youxi\"],[\"诸天无限\",\"zhutianwuxian\"],[\"军事小说\",\"junshi\"],[\"武侠小说\",\"wuxia\"],[\"体育小说\",\"tiyu\"],[\"穿越小说\",\"chuanyue\"],[\"言情小说\",\"yanqing\"]].map([title,uri]=>push(title, `paihang/${uri}-{{page}}`, 0.25));\n\n\n\npush(\"༺ˇ»`ʚ最近更新ɞ´«ˇ༻\",null,1,1);\n\n\n[[\"玄幻小说\",\"xuanhuan\"],[\"仙侠小说\",\"xianxia\"],[\"都市小说\",\"dushi\"],[\"历史小说\",\"lishi\"],[\"科幻小说\",\"kehuan\"],[\"轻小说\",\"qing\"],[\"悬疑小说\",\"xuanyi\"],[\"奇幻小说\",\"qihuan\"],[\"游戏小说\",\"youxi\"],[\"诸天无限\",\"zhutianwuxian\"],[\"军事小说\",\"junshi\"],[\"武侠小说\",\"wuxia\"],[\"体育小说\",\"tiyu\"],[\"穿越小说\",\"chuanyue\"],[\"言情小说\",\"yanqing\"]].map([title,uri]=>push(title, `${uri}<,/{{page}}>`, 0.25));\n\n\n\npush(\"༺ˇ»`ʚ完本小说ɞ´«ˇ༻\",null,1,1);\n\n\n\n[[\"玄幻小说\",\"xuanhuan\"],[\"仙侠小说\",\"xianxia\"],[\"都市小说\",\"dushi\"],[\"历史小说\",\"lishi\"],[\"科幻小说\",\"kehuan\"],[\"轻小说\",\"qing\"],[\"悬疑小说\",\"xuanyi\"],[\"奇幻小说\",\"qihuan\"],[\"游戏小说\",\"youxi\"],[\"诸天无限\",\"zhutianwuxian\"],[\"军事小说\",\"junshi\"],[\"武侠小说\",\"wuxia\"],[\"体育小说\",\"tiyu\"],[\"穿越小说\",\"chuanyue\"],[\"言情小说\",\"yanqing\"]].map([title,uri]=>push(title, `wanjie/${uri}-{{page}}`, 0.25));\n\n\n\n\n\n\nJSON.stringify(sort);", + "lastUpdateTime": 1750423575996, + "respondTime": 10173, + "ruleBookInfo": { + "author": "##作者:([^<]+)<##$1###", + "coverUrl": ".bookdetail@img@src", + "intro": " \n🕰 更新:{{@@.booktxt@p.-1@text##更新:|\\s.*}}\n👤 阅读:{{@@##阅读:([^<]+)<##$1###}}\n📜 简介:\n{{@@.des@html}}\n{{\"\\n\"+\"​\"}}\n❤️ 猜你喜欢:\n{{@@class.list clear@li@a.1@text}}\n📖 新章抢先看:\n{{@@.newest@a.1@html}}", + "kind": "{{@@##状态:([^<]+)<##$1###}}\n{{@@##类别:([^<]+)<##$1###}}\n{{@@.booktxt@p.-1@text##更新:|\\s.*}}", + "lastChapter": "{{@@.newest@h3@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求含理更谢乐发推票盟补加字].*?[】)\\)]}}•{{@@.booktxt@p.-1@text##更新:|\\s.*}}\n@js:result\n.replace(\"••\",\"\")\n.replace(/^(\\d+).第/,'第')\n.replace(/^(\\d+)[、.]第.+章/,'第$1章')\n.replace(/^(\\d+)、\\d+、/,'第$1章 ')\n.replace(/^(\\d+)、\\d+/,'第$1章')\n.replace(/^(\\d+)、/,'第$1章 ')\n.replace(/^(第.+章)\\s?第.+章/,'$1')\n.replace(/第\\s(.+)\\s章/,'第$1章')\n.replace(/.*(chapter|Chapter)\\s?(\\d+)\\s?/,'$1 $2 ')\n.replace(/\\(.+\\)/,'')\n.replace(/\\[|。/,'')\n.replace(/第([零一二两三四五六七八九十百千]+)章/g,java.toNumChapter(result))\n##(章)([^\\s]+)(\\s·)##$1 $2$3", + "name": ".booktxt@h1@text" + }, + "ruleContent": { + "content": ".con@p@html", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##如果您中途有事离开.*接着观看.|\\(\\s*\\)|.本章完.|第.*章.*|本作品由.*整理上传.*" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "##作者:([^<]+)<##$1###", + "bookList": ".item", + "bookUrl": "a.0@href", + "checkKeyWord": "我的模拟长生路", + "coverUrl": "img@src", + "kind": "{{@@span@text}}\n{{@@i.1@text}}", + "lastChapter": "{{@@li.0@a@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求含理更谢乐发推票盟补加字].*?[】)\\)]}}•{{@@i.1@text}}\n@js:result\n.replace(\"••\",\"\")\n.replace(/^(\\d+).第/,'第')\n.replace(/^(\\d+)[、.]第.+章/,'第$1章')\n.replace(/^(\\d+)、\\d+、/,'第$1章 ')\n.replace(/^(\\d+)、\\d+/,'第$1章')\n.replace(/^(\\d+)、/,'第$1章 ')\n.replace(/^(第.+章)\\s?第.+章/,'$1')\n.replace(/第\\s(.+)\\s章/,'第$1章')\n.replace(/.*(chapter|Chapter)\\s?(\\d+)\\s?/,'$1 $2 ')\n.replace(/\\(.+\\)/,'')\n.replace(/\\[|。/,'')\n.replace(/第([零一二两三四五六七八九十百千]+)章/g,java.toNumChapter(result))\n##(章)([^\\s]+)(\\s·)##$1 $2$3", + "name": "h3@a@text" + }, + "ruleToc": { + "chapterList": "#list@li@a", + "chapterName": "text##^\\d+.|(章\\D*)\\d*\\.?##$1", + "chapterUrl": "href", + "nextTocUrl": "text.下一页@href" + }, + "searchUrl": "so/{{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁⑬③", + "bookSourceType": 0, + "bookSourceUrl": "https://www.yuanzunxs.cc/", + "bookUrlPattern": "https://www.yuanzunxs.cc/go/\\d+/", + "customOrder": 314, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "全部小说::/all-{{page}}.html\n玄幻小说::/xuanhuan-{{page}}.html\n仙侠小说::/xianxia-{{page}}.html\n都市小说::/dushi-{{page}}.html\n军史小说::/junshi-{{page}}.html\n网游小说::/wangyou-{{page}}.html\n科幻小说::/kehuan-{{page}}.html\n灵异小说::/lingyi-{{page}}.html\n言情小说::/yanqing-{{page}}.html\n其他小说::/qita-{{page}}.html", + "lastUpdateTime": 1693363407706, + "respondTime": 10066, + "ruleBookInfo": { + "author": "@css:[property=og:novel:author]@content", + "coverUrl": "@css:[property=og:image]@content", + "intro": "@css:[property=og:description]@content", + "kind": "@css:[property=og:novel:update_time]@content&&\n[property=og:novel:category]@content&&\n[property=og:novel:status]@content", + "lastChapter": "@css:[property=og:novel:lastest_chapter_name]@content", + "name": "@css:[property=og:novel:book_name]@content", + "wordCount": "" + }, + "ruleContent": { + "content": "class.readcontent@textNodes", + "nextContentUrl": "text.下一页@href" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "class.author.0@text", + "bookList": "class.bookbox", + "bookUrl": "h4@a@href", + "coverUrl": "h4@a@href\nvar id = result.match(/(\\d+)\\/?$/)[1];\nvar iid = parseInt(id/1000);\n'https://www.yuanzunxs.cc/files/article/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "intro": ".update@textNodes", + "lastChapter": "class.cat@a@text", + "name": "h4@a@text" + }, + "ruleToc": { + "chapterList": "id.list-chapterAll@dd", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "https://www.yuanzunxs.cc/modules/article/search.php,{\n\"charset\": \"gbk\",\n\"method\": \"POST\",\n\"body\": \"searchkey={{key}}&action=login&submit=\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "顶点小说", + "bookSourceType": 0, + "bookSourceUrl": "https://www.ddw.net", + "customOrder": 334, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "@js:\nvar result = [];\njava.toast(\"🔥正在刷新列表,请稍等!\");\nvar push = (title, url, size) => result.push({\n \"title\": title,\n \"url\": url,\n \"style\": {\n \"layout_flexGrow\": 1,\n \"layout_flexBasisPercent\": size\n }\n});\nvar baseUrl = \"https://www.23ddw.net/\";\nvar tag = java.ajax(baseUrl);\nif (tag) {\n var a = org.jsoup.Jsoup.parse(tag).select(\".nav ul li a\");\n push(\"🔥分类小说🔥\", null, 1);\n for (var i = 1; i < a.length-1 ; i++) {\n var title = a[i].text();\n var url = a[i].attr(\"href\");\n var size = 0.25;\n title = String(title).replace(/\\s/g, \"\");\n url = String(url).replace(/_1/,'_{{page}}'); \n push(title, url, size);\n }\n} else {\n java.toast(\"🤔列表刷新失败!!!\"); \n} \nJSON.stringify(result);", + "header": "@js:\nJSON.stringify({\n\"Accept-Language\": \"zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7\",\n\"accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\"\n})", + "lastUpdateTime": 0, + "loginCheckJs": "cookie.removeCookie(source.key);\nresult", + "respondTime": 11180, + "ruleBookInfo": { + "intro": "[property=\"og:description\"]@content", + "kind": "[property=\"og:novel:category\"]@content" + }, + "ruleContent": { + "content": "#booktxt@p@html", + "nextContentUrl": "text.下一@href", + "replaceRegex": "##(?<=[\\u4e00-\\u9fa5]|[,、])[\\n]" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": ".btm@a@text||.s4@text", + "bookList": ".item,#newscontent ul li", + "bookUrl": "a@href", + "checkKeyWord": "剑来", + "coverUrl": "img@data-original", + "kind": ".s1@text", + "lastChapter": ".s3@text", + "name": "dt@text||.s2@text", + "wordCount": "em@text##\\s##," + }, + "ruleToc": { + "chapterList": "\na=String(result).replace(/(
)[\\s\\S]*dt>/,'$1')\n\n#list a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "\ncookie.removeCookie(source.key);\nurl=source.key;\na=java.ajax(url);\n//java.log(a)\nb=a.match(/action=\\\"([^\"]*)\\\"/)[1];\nc=`?searchkey=${key},{\"method\": \"get\"}`;\nb+=c\n", + "weight": 0 + }, + { + "bookSourceComment": "\n\n伤白阅ZJH112校验整理", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "️顶点小说", + "bookSourceType": 0, + "bookSourceUrl": "https://www.uswx.com/", + "bookUrlPattern": "https://www.23uswx.com/\\d+_\\d+/", + "customOrder": 407, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"全部小说\",\"url\":\"https://m.23uswx.com/xclass/0/{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"玄幻小说\",\"url\":\"https://m.23uswx.com/xclass/1/{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"修真小说\",\"url\":\"https://m.23uswx.com/xclass/2/{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"都市小说\",\"url\":\"https://m.23uswx.com/xclass/3/{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"穿越小说\",\"url\":\"https://m.23uswx.com/xclass/4/{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"网游小说\",\"url\":\"https://m.23uswx.com/xclass/5/{{page}}.html\",\"style\":{\"layout_flexGrow\":0.25}},{\"title\":\"科幻小说\",\"url\":\"https://m.23uswx.com/xclass/6/{{page}}.html\",\"style\":{\"layout_flexGrow\":0,\"layout_flexBasisPercent\":0.29}}]", + "lastUpdateTime": 1720605652309, + "loginUrl": "", + "respondTime": 10002, + "ruleBookInfo": { + "author": "id.info@tag.p.0@text##作 者:", + "coverUrl": "id.fmimg@tag.img@src", + "init": "", + "intro": "id.intro@text", + "kind": "[property=og:novel:update_time]@content&&\n[property=og:novel:category]@content&&\n[property=og:novel:status]@content", + "lastChapter": "id.info@tag.p.3@a@text", + "name": "id.info@tag.h1.0@text##\\(.*|\\(.*|免费阅读|全文.*阅读|最新章节|笔趣阁|小说" + }, + "ruleContent": { + "content": "id.content@html##最新网址.*com|顶点小说.*com|最快更新.*最新章节!", + "nextContentUrl": "" + }, + "ruleExplore": { + "author": "class.author@a@text", + "bookList": "class.hot_sale", + "bookUrl": "class.title@a@href@js:\nvar id = result.match(/(\\d+)\\/?$/)[1];\nvar iid = parseInt(id/1000);\n'https://www.23uswx.com/'+iid+'_'+id+'/'", + "coverUrl": "img@data-original", + "intro": "class.review@a@text", + "name": "class.title@a@text" + }, + "ruleSearch": { + "author": "tag.td.2@text", + "bookList": "class.grid@tag.tr!0", + "bookUrl": "tag.td.0@tag.a@href", + "coverUrl": "tag.td.0@tag.a@href\nvar id = result.match(/(\\d+)\\/?$/)[1];\nvar iid = parseInt(id/1000);\n'https://www.23uswx.com/files/article/image/'+iid+'/'+id+'/'+id+'s.jpg';\n", + "lastChapter": "tag.td.1@tag.a@text", + "name": "tag.td.0@tag.a@text##\\(.*|\\(.*|免费阅读|全文.*阅读|最新章节|笔趣阁|小说" + }, + "ruleToc": { + "chapterList": "id.list@tag.dd", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href" + }, + "searchUrl": "https://www.23uswx.com/modules/article/search.php?q={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.wduec.com", + "customOrder": 463, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "", + "header": "", + "lastUpdateTime": 0, + "respondTime": 10057, + "ruleBookInfo": { + "author": "@get:{a}", + "coverUrl": "@get:{c}", + "init": "@put:{n:\"[property$=book_name]@content\",\na:\"[property$=author]@content\",\nk:\"[property~=category|status|update_time]@content\",\nl:\"[property$=lastest_chapter_name]@content\",\ni:\"[property$=description]@content\",\nc:\"[property$=image]@content\"}", + "intro": "@get:{i}", + "kind": "@get:{k}", + "lastChapter": "@get:{l}", + "name": "@get:{n}" + }, + "ruleContent": { + "content": "#article@html" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "kind": "", + "lastChapter": "", + "name": "" + }, + "ruleSearch": { + "author": ".s4@text", + "bookList": ".search-list > ul > li!0", + "bookUrl": "a.1@href", + "checkKeyWord": "剑来", + "coverUrl": "@js:\"/static/nocover.jpg\"", + "kind": "span.0:-1:-2@text", + "lastChapter": ".s3@text", + "name": ".s2@text" + }, + "ruleToc": { + "chapterList": "#list dl dd a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "https://www.wduec.com/search/?searchkey={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁(xinbiquge)", + "bookSourceType": 0, + "bookSourceUrl": "https://wap.xinbiquge.org旅途", + "customOrder": 512, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "lastUpdateTime": 1721063860043, + "respondTime": 10001, + "ruleBookInfo": { + "author": "[property$=author]@content", + "coverUrl": "[property=og:image]@content", + "init": "", + "intro": "[property=og:description]@content", + "kind": "[property~=(category|status|update_time)]@content", + "lastChapter": "[property=og:novel:latest_chapter_name]@content", + "name": "[property=og:novel:book_name]@content" + }, + "ruleContent": { + "content": "#chaptercontent@html", + "nextContentUrl": "text.下一章@href", + "replaceRegex": "##.*\\(\\d+ / \\d+\\).*\\n|新笔趣阁.*\\s*|\\s*.*下一页继续阅读.*|.本章阅读完毕.*\\s*" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": ".author@span@text", + "bookList": ".hot_sale", + "bookUrl": "a@href", + "name": ".title@text" + }, + "ruleToc": { + "chapterList": ".book_last.-1@dd a", + "chapterName": "text", + "chapterUrl": "href" + }, + "searchUrl": "https://wap2.xinbiquge.org/book/search.aspx?ie=utf-8&siteid=xinbiquge.org&s=000&key={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://www.sypvm.com", + "customOrder": 596, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "玄幻小说::/sort_1_{{page}}/\n都市小说::/sort_2_{{page}}/\n穿越小说::/sort_3_{{page}}/\n言情小说::/sort_4_{{page}}/\n武侠小说::/sort_5_{{page}}/\n网游小说::/sort_6_{{page}}/\n科幻小说::/sort_7_{{page}}/", + "lastUpdateTime": 1699554853717, + "respondTime": 10183, + "ruleBookInfo": { + "author": "text.作 者:@text", + "coverUrl": "img@src", + "intro": "class.intro@text", + "kind": "class.position@a.1@text", + "name": "class.infobar@h1@text" + }, + "ruleContent": { + "content": "id.htmlContent@html" + }, + "ruleExplore": { + "author": "span@text", + "bookList": "class.item", + "bookUrl": "a.0@href", + "coverUrl": "img@src", + "intro": "dd@text", + "name": "a.1@text" + }, + "ruleSearch": { + "author": "class.s4.0@text", + "bookList": "id.book-list@ul@li!0", + "bookUrl": "a.0@href", + "checkKeyWord": "三千", + "intro": "", + "kind": "class.s2.0@text", + "lastChapter": "a.-1@text", + "name": "a.0@text" + }, + "ruleToc": { + "chapterList": "dl@dd", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "{{cookie.removeCookie(source.getKey())}}\nhttps://www.sypvm.com/user/search.html?q={{key}},{'webView': true}", + "weight": 0 + }, + { + "bookSourceComment": "//by 天天的鸟蛋蛋更改url\n// By情无羁修复,25.01.27,搜索重定向后把网址复制到URL即可\n/*同站一大堆\n修复搜索,可设置源变量限制搜索数量\n优化by:游客2580\n搜索加载by:Thomas&&youke2580\n以下同库网站可以自行替换网址\nm.wnxsw.cc\nm.quge.cc\nm.bq9.cc\nm.bqg18.cc\nm.bqg113.com\nm.bqg119.com\nm.bqg222.com\nm.bqg227.com\nm.p2wt.com\nm.wpxs.cc\nm.lwp7.com\nm.4z6p.com\nm.aofce.com\nm.fqxs123.com\nm.1q1m.com\nm.wnxsw.com\nm.shuhui8.cc\nm.ok120.net\nm.ecc6.com\nm.bqg332.com\nm.bqg122.com\nm.bqg18.cc\nm.shuhui8.cc\n*/", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.soushuwang.cc", + "bookUrlPattern": "", + "customOrder": 609, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "玄幻::/json?sortid=1&page={{page}}\n武侠::/json?sortid=2&page={{page}}\n都市::/json?sortid=3&page={{page}}\n历史::/json?sortid=4&page={{page}}\n网游::/json?sortid=5&page={{page}}\n科幻::/json?sortid=6&page={{page}}\n女生::/json?sortid=7&page={{page}}\n完本::/json?sortid=0&page={{page}}", + "header": "{\"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 1753975395523, + "respondTime": 4231, + "ruleBookInfo": { + "author": "[property$=author]@content", + "coverUrl": "[property$=image]@content", + "intro": "[property$=description]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property$=latest_chapter_name]@content", + "name": "[property$=book_name]@content", + "tocUrl": ".book_more@a@href##^##/userverify", + "wordCount": "{{@@text.字数@text##字数:}}" + }, + "ruleContent": { + "content": "#chaptercontent@html", + "nextContentUrl": "text.下一@href", + "replaceRegex": "##\\s*(.*阅读模式.*)\\s*|\\s*请收藏.*(com|cc)" + }, + "ruleExplore": { + "author": "", + "bookList": "", + "bookUrl": "", + "coverUrl": "", + "intro": "", + "kind": "", + "name": "" + }, + "ruleSearch": { + "author": "$.author", + "bookList": "\nfunction loading(){\nvar su=source.getKey(),\nsk=java.get(\"sk\"),\nq=\"?q=\"+java.encodeURI(sk);\n\nvar c=\"Hm_lvt_=\"+~~(Date.now()/1e3);\nvar head={\n\taccept: \"application/json\",\n\t\"accept-language\": \"zh-CN,en-US;q=0.9\",\n\treferer: baseUrl,\n\tcookie: String(c)\n};\n\nvar hm=\";hm=\"+java.head(su+\"/user/hm.html\"+q,head).cookies().hm;\nhead.cookie = String(head.cookie+hm);\n\nvar get=java.get(su+\"/user/search.html\"+q, head).body();\n\nvar j=JSON.parse(get),\nm=j.map(_=>JSON.stringify(_));\n\nvar b=[],\nv=source.getVariable();\n//条件判断,存在不为空和0,且小于长度\nif(v&&v!=\"\"&&v!=0&&v\n$.*", + "bookUrl": "$.url_list", + "checkKeyWord": "剑来", + "coverUrl": "$.url_img", + "intro": "$.intro", + "kind": "0", + "lastChapter": "", + "name": "$.articlename" + }, + "ruleToc": { + "chapterList": "dl@dd!0", + "chapterName": "a@text", + "chapterUrl": "a@href", + "preUpdateJs": "" + }, + "searchUrl": "{{source.getKey()}}/s?q={{java.put(\"sk\",key)}}", + "variableComment": "输入数字设置搜索列表数量,过大无效。\n1为1个,2为2个,3为3个,以此类推。\n不设置或0清空显示全部列表。", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁㉝", + "bookSourceType": 0, + "bookSourceUrl": "https://www.bbiquge.com", + "bookUrlPattern": "", + "customOrder": 651, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko, By Black Prism) Chrome/99.0 Safari/537.36\"\n}", + "lastUpdateTime": 0, + "loginUrl": "", + "respondTime": 12220, + "ruleBookInfo": { + "coverUrl": "id.fmimg@tag.img.0@src", + "intro": "id.intro@textNodes", + "lastChapter": "id.info@tag.p.3@a@text##正文" + }, + "ruleContent": { + "content": "id.content@textNodes##一秒记住【.*窗免费阅读!" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "class.s4@text||tag.p.0@tag.a@text", + "bookList": "class.novelslistss@tag.li||id.info", + "bookUrl": "class.s2@tag.a.0@href", + "coverUrl": "tag.a.0@href##/book_(\\d+)(\\d{3})/##/files/article/image/$1/$1$2/$1$2s.jpg", + "kind": "class.s1@text##\\[||\\]", + "lastChapter": "class.s3@tag.a.0@text||tag.p.3@tag.a@text", + "name": "class.s2@tag.a@text||tag.h1@text" + }, + "ruleToc": { + "chapterList": "id.list@tag.dd", + "chapterName": "tag.a.0@text", + "chapterUrl": "tag.a.0@href" + }, + "searchUrl": "https://www.bbiquge.com/modules/article/search.php?searchkey={{key}},{\n \"charset\": \"gbk\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁 bbiquge", + "bookSourceType": 0, + "bookSourceUrl": "https://www.bbiquge.com-By Dark", + "bookUrlPattern": "", + "customOrder": 656, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko, By Black Prism) Chrome/99.0 Safari/537.36\"\n}", + "lastUpdateTime": 1605335289746, + "loginUrl": "", + "respondTime": 12114, + "ruleBookInfo": { + "coverUrl": "id.fmimg@tag.img.0@src", + "intro": "id.intro@textNodes" + }, + "ruleContent": { + "content": "id.content@textNodes##一秒记住【.*窗免费阅读!" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "class.s4@text||tag.p.0@tag.a@text", + "bookList": "class.novelslistss@tag.li||id.info", + "bookUrl": "class.s2@tag.a.0@href", + "kind": "class.s1@text##\\[|\\]", + "lastChapter": "class.s3@tag.a.0@text||tag.p.3@tag.a@text", + "name": "class.s2@tag.a@text||tag.h1@text" + }, + "ruleToc": { + "chapterList": "id.list@tag.dd", + "chapterName": "tag.a.0@text", + "chapterUrl": "tag.a.0@href" + }, + "searchUrl": "https://www.bbiquge.com/modules/article/search.php?searchkey={{key}},{\n \"charset\": \"gbk\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "\n\n同站一大堆", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "笔趣阁", + "bookSourceType": 0, + "bookSourceUrl": "https://m.egyguard.com", + "bookUrlPattern": "https://m.egyguard.com/book.*", + "customOrder": 661, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "玄幻::/xuanhuan/<,{{page}}.html>\n武侠::/wuxia/<,{{page}}.html>\n都市::/dushi/<,{{page}}.html>\n历史::/lishi/<,{{page}}.html>\n网游::/wangyou/<,{{page}}.html>\n科幻::/kehuan/<,{{page}}.html>\n女生::/mm/<,{{page}}.html>\n全本::/finish/<,{{page}}.html>", + "header": "{ \"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 1691662300914, + "respondTime": 10827, + "ruleBookInfo": { + "author": "[property$=author]@content", + "coverUrl": "[property$=image]@content", + "intro": "[property$=description]@content", + "kind": "[property~=category|status|update_time]@content", + "lastChapter": "[property$=latest_chapter_name]@content", + "name": "[property$=book_name]@content", + "tocUrl": "{{$.}}list.html", + "wordCount": "{{@@text.字数@text##字数:}}" + }, + "ruleContent": { + "content": "#chaptercontent@html", + "nextContentUrl": "text.下一@href", + "replaceRegex": "##请收藏.*com|(温馨提示.*正常显示)" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "span@text", + "bookList": "class.item", + "bookUrl": "a@href", + "checkKeyWord": "剑来", + "coverUrl": "img@src", + "intro": "dd@text##最新章节地址:.*", + "kind": "0", + "name": "a.1@text" + }, + "ruleToc": { + "chapterList": "dl@dd!0", + "chapterName": "a@text", + "chapterUrl": "a@href" + }, + "searchUrl": "/s?q={{key}},{\n \"webView\": true\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,小说", + "bookSourceName": "顶点小说", + "bookSourceType": 0, + "bookSourceUrl": "https://www.ddyueshu.com/", + "customOrder": 683, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "", + "lastUpdateTime": 1721323613650, + "respondTime": 10000, + "ruleBookInfo": { + "author": "id.info@tag.p.0@text", + "coverUrl": "id.fmimg@tag.img@src", + "init": "", + "intro": "id.intro@text", + "kind": "class.tag@class.blue@text", + "lastChapter": "class.update-text@text", + "name": "id.info@tag.h1@text", + "tocUrl": "" + }, + "ruleContent": { + "content": "id.content@all##请记住本书首发域名:ddyueshu.com。顶点小说手机版阅读网址:m.ddyueshu.com", + "nextContentUrl": "class.bottem1@tag.a.2@href" + }, + "ruleExplore": { + "bookList": "tag.li.!0:1:2:3:4:5:6:7:8:9", + "bookUrl": "tag.a@href", + "name": "tag.a@text" + }, + "ruleSearch": { + "author": "", + "bookList": "tag.li.!0:1:2:3:4:5:6:7:8:9", + "bookUrl": "tag.a@href", + "name": "tag.a@text" + }, + "ruleToc": { + "chapterList": "tag.dl@tag.dd.!0:1:2:3:4:5", + "chapterName": "tag.a@text", + "chapterUrl": "tag.a@href" + }, + "searchUrl": "https://www.ddyueshu.com/xiaoshuodaquan/", + "weight": 0 + } +] \ No newline at end of file diff --git a/sakura_cloud/public/sources/精选50漫画源.json b/sakura_cloud/public/sources/精选50漫画源.json new file mode 100644 index 0000000..eedcb64 --- /dev/null +++ b/sakura_cloud/public/sources/精选50漫画源.json @@ -0,0 +1,2503 @@ +[ + { + "bookSourceComment": "", + "bookSourceGroup": "✔️2025-11-2去重校验 by-coco,漫画", + "bookSourceName": "腾讯漫画 Haxc", + "bookSourceType": 2, + "bookSourceUrl": "https://m.ac.qq.comHaxc", + "bookUrlPattern": "", + "customOrder": 245, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "条漫::https://m.ac.qq.com/category/listAll?type=tm&rank=upt&pageSize=30&page={{page}}\n独家::https://m.ac.qq.com/category/listAll?type=dj&rank=upt&pageSize=30&page={{page}}\n完结::https://m.ac.qq.com/category/listAll?type=wj&rank=upt&pageSize=30&page={{page}}\n日漫::https://m.ac.qq.com/category/listAll?type=rm&rank=upt&pageSize=30&page={{page}}\n恐怖::https://m.ac.qq.com/category/listAll?type=kb&rank=pgv&pageSize=30&page={{page}}\n妖怪::https://m.ac.qq.com/category/listAll?type=yg&rank=pgv&pageSize=30&page={{page}}\n恋爱::https://m.ac.qq.com/category/listAll?type=na&rank=pgv&pageSize=30&page={{page}}\n玄幻::https://m.ac.qq.com/category/listAll?type=xh&rank=pgv&pageSize=30&page={{page}}\n热血::https://m.ac.qq.com/category/listAll?type=rx&rank=pgv&pageSize=30&page={{page}}\n悬疑::https://m.ac.qq.com/category/listAll?type=xy&rank=pgv&pageSize=30&page={{page}}\n彩虹::https://m.ac.qq.com/category/listAll?type=dm&rank=pgv&pageSize=30&page={{page}}\n少女::https://m.ac.qq.com/category/listAll?type=sv&rank=pgv&pageSize=30&page={{page}}\n韩漫::https://m.ac.qq.com/category/listAll?type=hm&rank=pgv&pageSize=30&page={{page}}\n科幻::https://m.ac.qq.com/category/listAll?type=kh&rank=pgv&pageSize=30&page={{page}}\n逗比::https://m.ac.qq.com/category/listAll?type=db&rank=pgv&pageSize=30&page={{page}}\n校园::https://m.ac.qq.com/category/listAll?type=qcxy&rank=pgv&pageSize=30&page={{page}}\n都市::https://m.ac.qq.com/category/listAll?type=ds&rank=pgv&pageSize=30&page={{page}}\n治愈::https://m.ac.qq.com/category/listAll?type=zy&rank=pgv&pageSize=30&page={{page}}\n飙升榜::https://m.ac.qq.com/rank/index?type=rise&pageSize=10&page={{page}}\n畅销榜::https://m.ac.qq.com/rank/index?type=pay&pageSize=10&page={{page}}\n新作榜::https://m.ac.qq.com/rank/index?type=new&pageSize=10&page={{page}}\n真香榜::https://m.ac.qq.com/rank/index?type=hot&pageSize=10&page={{page}}", + "lastUpdateTime": 1687400646204, + "loginUrl": "", + "respondTime": 3201, + "ruleBookInfo": { + "author": "class.head-info-author@text##作者:", + "intro": "class.head-info-desc@text", + "lastChapter": "class.mod-chapter-title@tag.span.0@text##,", + "name": "class.head-title-tags@h1@text" + }, + "ruleContent": { + "content": "@js:\ndata=result.match(/data:\\s*'(.*?)'/)[1];\nnonce=result.match(/', + caseSensitive: false, dotAll: true), + ' ') + .replaceAll( + RegExp(r']*>.*?', + caseSensitive: false, dotAll: true), + ' ') + .replaceAll(RegExp(r'', caseSensitive: false), '\n') + .replaceAll(RegExp(r'', caseSensitive: false), '\n\n') + .replaceAll(RegExp(r'<[^>]+>'), ' ') + .replaceAll(' ', ' ') + .replaceAll('&', '&') + .replaceAll('<', '<') + .replaceAll('>', '>') + .replaceAll(RegExp(r'[ \t]+'), ' ') + .replaceAll(RegExp(r'\n\s+\n'), '\n\n') + .trim(); + } + + String _cleanText(String? value) { + return (value ?? '').replaceAll(RegExp(r'\s+'), ' ').trim(); + } + + bool _looksLikeImage(String url) { + final path = Uri.tryParse(url)?.path.toLowerCase() ?? url.toLowerCase(); + return path.endsWith('.jpg') || + path.endsWith('.jpeg') || + path.endsWith('.png') || + path.endsWith('.webp') || + path.endsWith('.gif'); + } + + String _plainTextFromJson(String body) { + final decoded = _tryJsonDecode(body); + if (decoded == null) return ''; + final values = []; + void collect(Object? node) { + if (values.length >= 80) return; + if (node is Map) { + for (final entry in node.entries) { + final key = entry.key.toString().toLowerCase(); + if (entry.value is String && + (key.contains('content') || + key.contains('text') || + key.contains('body') || + key.contains('chapter'))) { + final text = (entry.value as String).trim(); + if (text.length > 20) values.add(text); + continue; + } + collect(entry.value); + } + } else if (node is List) { + for (final item in node) { + collect(item); + } + } + } + + collect(decoded); + return values.join('\n\n').trim(); + } + + List _imageUrlsFromJson(String body) { + final decoded = _tryJsonDecode(body); + if (decoded == null) return const []; + final urls = {}; + void collect(Object? node) { + if (node is String) { + final value = node.trim(); + if (_looksLikeImage(value)) urls.add(value); + } else if (node is Map) { + for (final entry in node.entries) { + collect(entry.value); + } + } else if (node is List) { + for (final item in node) { + collect(item); + } + } + } + + collect(decoded); + return urls.toList(growable: false); + } + + Object? _tryJsonDecode(String body) { + var text = body.trimLeft(); + if (!text.startsWith('{') && !text.startsWith('[')) return null; + if (text.isNotEmpty && text.codeUnitAt(0) == 0xFEFF) { + text = text.substring(1); + } + try { + return jsonDecode(text); + } on FormatException { + return null; + } + } +} + +class LegadoRequestSpec { + const LegadoRequestSpec({ + required this.url, + this.method, + this.body, + this.headers = const {}, + }); + + final String url; + final String? method; + final String? body; + final Map headers; +} + +LegadoRequestSpec parseLegadoRequestSpec(String raw) { + final value = raw.trim(); + final markers = RegExp(r',\s*\{').allMatches(value).toList(); + if (markers.isEmpty) return LegadoRequestSpec(url: value); + for (final marker in markers.reversed) { + final optionText = value.substring(marker.start + 1).trim(); + try { + final decoded = _decodeLooseLegadoOptions(optionText); + if (decoded is! Map) continue; + final options = Map.from(decoded); + final rawHeaders = options['headers']; + final headers = rawHeaders is Map + ? rawHeaders.map( + (key, item) => MapEntry(key.toString(), item.toString()), + ) + : _parseLooseHeaders(rawHeaders?.toString()); + return LegadoRequestSpec( + url: value.substring(0, marker.start).trim(), + method: options['method']?.toString(), + body: options['body']?.toString(), + headers: headers, + ); + } on FormatException { + continue; + } + } + return LegadoRequestSpec(url: value); +} + +Object? _decodeLooseLegadoOptions(String value) { + try { + return jsonDecode(value); + } on FormatException { + var normalized = value + // Legado exports occasionally use semicolons between option fields. + // Request option bodies are strings here, so normalizing separators is + // preferable to treating the whole searchUrl as a literal URL. + .replaceAll(';', ',') + .replaceAll(RegExp(r',\s*([}\]])'), r'$1'); + normalized = normalized.replaceAllMapped( + RegExp(r"'((?:\\.|[^'\\])*)'"), + (match) => jsonEncode(match.group(1)!), + ); + normalized = normalized.replaceAllMapped( + RegExp(r'([{,]\s*)([A-Za-z_$][\w$-]*)(\s*:)'), + (match) => '${match.group(1)}"${match.group(2)}"${match.group(3)}', + ); + try { + return jsonDecode(normalized); + } on FormatException { + // A few community sources use a JavaScript-object style configuration + // that cannot be made JSON safely (for example mixed quote styles). + // Keep this fallback deliberately narrow: only the request fields that + // the client supports are read. + final result = {}; + for (final key in const ['method', 'body', 'headers', 'charset']) { + final match = RegExp( + "[\\\"']${RegExp.escape(key)}[\\\"']\\s*:\\s*([\\\"'])([\\s\\S]*?)\\1", + caseSensitive: false, + ).firstMatch(value); + if (match != null) result[key] = match.group(2) ?? ''; + } + return result.isEmpty ? null : result; + } + } +} + +Map _parseLooseHeaders(String? value) { + if (value == null || value.trim().isEmpty) return const {}; + var text = value.trim(); + if (text.startsWith('{') && text.endsWith('}')) { + text = text.substring(1, text.length - 1); + } + final headers = {}; + for (final pair in text.split(',')) { + final separator = pair.indexOf(':'); + if (separator <= 0) continue; + final key = pair + .substring(0, separator) + .trim() + .replaceAll(RegExp(r'''^["']|["']$'''), ''); + final content = pair + .substring(separator + 1) + .trim() + .replaceAll(RegExp(r'''^["']|["']$'''), ''); + if (key.isNotEmpty && content.isNotEmpty) headers[key] = content; + } + return headers; +} + +class _LegadoRequest { + const _LegadoRequest({ + required this.method, + required this.url, + this.body, + this.headers = const {}, + }); + + final String method; + final String url; + final String? body; + final Map headers; +} + +class _LegadoHttpException implements Exception { + const _LegadoHttpException(this.statusCode, this.host); + final int statusCode; + final String host; + @override + String toString() => 'HTTP $statusCode from $host'; +} diff --git a/sakura_mobile/lib/src/core/legado_js_runner.dart b/sakura_mobile/lib/src/core/legado_js_runner.dart new file mode 100644 index 0000000..1a0ef47 --- /dev/null +++ b/sakura_mobile/lib/src/core/legado_js_runner.dart @@ -0,0 +1,131 @@ +import 'dart:convert'; + +import 'package:flutter_js/flutter_js.dart'; + +class LegadoJsRunner { + static const timeout = Duration(seconds: 3); + static const stackLimitBytes = 1024 * 1024; + + String run({ + required String script, + required String result, + required String rawHtml, + required String baseUrl, + required Map context, + }) { + final resultExpression = _resultExpression(script, result); + final runtime = QuickJsRuntime2( + timeout: timeout.inMilliseconds, + stackSize: stackLimitBytes, + ); + try { + final bootstrap = ''' + var result = $resultExpression; + var rawHtml = ${jsonEncode(rawHtml)}; + var baseUrl = ${jsonEncode(baseUrl)}; + var __context = ${jsonEncode(context)}; + var src = rawHtml; + var key = __context.key || ''; + var page = Number(__context.page || 1); + var source = Object.freeze({ + key: __context.sourceUrl || baseUrl, + bookSourceUrl: __context.sourceUrl || baseUrl, + bookSourceName: __context.sourceName || '', + getKey: function() { return this.key; }, + getVariable: function() { return __context.sourceVariable || ''; } + }); + function __decodeBase64(value) { + var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; + var input = String(value || '').replace(new RegExp('[^A-Za-z0-9+/=]', 'g'), ''); + var output = ''; + for (var i = 0; i < input.length;) { + var e1 = chars.indexOf(input.charAt(i++)); + var e2 = chars.indexOf(input.charAt(i++)); + var e3 = chars.indexOf(input.charAt(i++)); + var e4 = chars.indexOf(input.charAt(i++)); + output += String.fromCharCode((e1 << 2) | (e2 >> 4)); + if (e3 !== 64 && e3 !== -1) { + output += String.fromCharCode(((e2 & 15) << 4) | (e3 >> 2)); + } + if (e4 !== 64 && e4 !== -1) { + output += String.fromCharCode(((e3 & 3) << 6) | e4); + } + } + try { return decodeURIComponent(escape(output)); } catch (_) { return output; } + } + var java = Object.freeze({ + get: function(key) { return __context[String(key)] || ''; }, + put: function(key, value) { + __context[String(key)] = value == null ? '' : String(value); + return value; + }, + base64Decode: function(value) { + return __decodeBase64(value); + }, + encodeURI: function(value) { return encodeURIComponent(String(value)); }, + ajax: function() { + throw new Error('Network access is disabled in Legado JS rules'); + }, + getElements: function() { + throw new Error('DOM bridge is unavailable in the secure sandbox'); + }, + log: function() {} + }); + var fetch = undefined; + var XMLHttpRequest = undefined; + var WebSocket = undefined; + var localStorage = undefined; + '''; + final init = runtime.evaluate(bootstrap); + if (init.isError) throw FormatException(init.stringResult); + + final evaluated = runtime.evaluate(script, sourceUrl: 'legado-rule.js'); + if (evaluated.isError) throw FormatException(evaluated.stringResult); + final contextResult = runtime.evaluate('JSON.stringify(__context)'); + if (!contextResult.isError) { + try { + final decoded = jsonDecode(contextResult.stringResult); + if (decoded is Map) { + for (final entry in decoded.entries) { + context[entry.key.toString()] = entry.value?.toString() ?? ''; + } + } + } on FormatException { + // A malformed context must not discard a successful rule result. + } + } + return _normalizeResult(evaluated); + } finally { + runtime.dispose(); + } + } + + String _resultExpression(String script, String result) { + final value = result.trim(); + final accessesObject = + RegExp(r'\bresult\s*\.[A-Za-z_$]').hasMatch(script) && + !RegExp(r'JSON\s*\.\s*parse\s*\(\s*result\s*\)').hasMatch(script); + if (accessesObject && + ((value.startsWith('{') && value.endsWith('}')) || + (value.startsWith('[') && value.endsWith(']')))) { + try { + jsonDecode(value); + return value; + } on FormatException { + // Fall back to a string when the fragment only resembles JSON. + } + } + return jsonEncode(result); + } + + String _normalizeResult(JsEvalResult value) { + final raw = value.rawResult; + if (raw == null) return value.stringResult; + if (raw is String) return raw; + if (raw is Iterable) return raw.join('\n'); + if (raw is Map) return jsonEncode(raw); + final text = value.stringResult; + if (text == 'undefined' || text == 'null') return ''; + return text; + } +} diff --git a/sakura_mobile/lib/src/core/legado_models.dart b/sakura_mobile/lib/src/core/legado_models.dart new file mode 100644 index 0000000..658bea3 --- /dev/null +++ b/sakura_mobile/lib/src/core/legado_models.dart @@ -0,0 +1,631 @@ +import 'dart:convert'; + +import 'manga_image.dart'; + +class SourceRepo { + const SourceRepo({ + required this.name, + required this.url, + required this.description, + required this.category, + this.expectedCount, + this.isSubscription = false, + }); + + final String name; + final String url; + final String description; + final String category; + final int? expectedCount; + final bool isSubscription; +} + +class LegadoSource { + const LegadoSource({ + required this.id, + required this.name, + required this.url, + required this.type, + required this.enabled, + this.userAgent, + this.headers = const {}, + this.searchUrl, + this.searchListRule, + this.searchNameRule, + this.searchAuthorRule, + this.searchCoverRule, + this.searchDetailRule, + this.searchMethod, + this.searchBody, + this.ruleKind, + this.charset, + this.urlEncoding, + this.bookInfoUrl, + this.bookInfoNameRule, + this.bookInfoAuthorRule, + this.bookInfoCoverRule, + this.bookInfoIntroRule, + this.bookInfoTocRule, + this.tocUrl, + this.tocListRule, + this.tocNameRule, + this.tocUrlRule, + this.tocNextPageRule, + this.contentRule, + this.contentNextRule, + this.contentPrevRule, + }); + + final String id; + final String name; + final String url; + final int type; + final bool enabled; + final String? userAgent; + final Map headers; + final String? searchUrl; + final String? searchListRule; + final String? searchNameRule; + final String? searchAuthorRule; + final String? searchCoverRule; + final String? searchDetailRule; + final String? searchMethod; + final String? searchBody; + final String? ruleKind; + final String? charset; + final String? urlEncoding; + final String? bookInfoUrl; + final String? bookInfoNameRule; + final String? bookInfoAuthorRule; + final String? bookInfoCoverRule; + final String? bookInfoIntroRule; + final String? bookInfoTocRule; + final String? tocUrl; + final String? tocListRule; + final String? tocNameRule; + final String? tocUrlRule; + final String? tocNextPageRule; + final String? contentRule; + final String? contentNextRule; + final String? contentPrevRule; + + bool get isNovel => type == 0; + bool get isManga => type == 2; + + LegadoSource copyWith({bool? enabled}) { + return LegadoSource( + id: id, + name: name, + url: url, + type: type, + enabled: enabled ?? this.enabled, + userAgent: userAgent, + headers: headers, + searchUrl: searchUrl, + searchListRule: searchListRule, + searchNameRule: searchNameRule, + searchAuthorRule: searchAuthorRule, + searchCoverRule: searchCoverRule, + searchDetailRule: searchDetailRule, + searchMethod: searchMethod, + searchBody: searchBody, + ruleKind: ruleKind, + charset: charset, + urlEncoding: urlEncoding, + bookInfoUrl: bookInfoUrl, + bookInfoNameRule: bookInfoNameRule, + bookInfoAuthorRule: bookInfoAuthorRule, + bookInfoCoverRule: bookInfoCoverRule, + bookInfoIntroRule: bookInfoIntroRule, + bookInfoTocRule: bookInfoTocRule, + tocUrl: tocUrl, + tocListRule: tocListRule, + tocNameRule: tocNameRule, + tocUrlRule: tocUrlRule, + tocNextPageRule: tocNextPageRule, + contentRule: contentRule, + contentNextRule: contentNextRule, + contentPrevRule: contentPrevRule, + ); + } + + factory LegadoSource.fromJson(Map json) { + final name = _string(json['bookSourceName']) ?? + _string(json['sourceName']) ?? + _string(json['name']) ?? + '未命名的书源'; + final url = _string(json['bookSourceUrl']) ?? + _string(json['sourceUrl']) ?? + _string(json['url']) ?? + ''; + final search = _map(json['ruleSearch']); + final bookInfo = _map(json['ruleBookInfo']); + final toc = _map(json['ruleToc']); + final content = _map(json['ruleContent']); + final type = _sourceType( + json['bookSourceType'] ?? json['sourceType'] ?? json['type'], + json: json, + search: search, + content: content, + ); + final headers = _headers(json['header'] ?? json['headers']); + final cookie = _string(json['cookie'] ?? json['cookies']); + final mergedHeaders = { + ...headers, + if (cookie != null && + !headers.keys.any((key) => key.toLowerCase() == 'cookie')) + 'Cookie': cookie, + }; + return LegadoSource( + id: _sourceId(name, url), + name: name, + url: url, + type: type, + enabled: json['enabled'] == true, + userAgent: _string(json['httpUserAgent']), + headers: mergedHeaders, + searchUrl: _firstString([ + search['url'], + search['searchUrl'], + search['bookUrlPattern'], + search['comicUrlPattern'], + json['searchUrl'], + ]), + searchListRule: _firstString([ + search['bookList'], + search['comicList'], + search['list'], + search['result'], + search['items'], + search['data'], + search['results'], + ]), + searchNameRule: _firstString([ + search['name'], + search['bookName'], + search['comicName'], + search['title'], + ]), + searchAuthorRule: _firstString([ + search['author'], + search['bookAuthor'], + search['comicAuthor'], + search['writer'], + ]), + searchCoverRule: _firstString([ + search['coverUrl'], + search['cover'], + search['bookCover'], + search['comicCover'], + search['poster'], + search['img'], + search['image'], + ]), + searchDetailRule: _firstString([ + search['detailUrl'], + search['bookUrl'], + search['comicUrl'], + search['chapterUrl'], + search['tocUrl'], + search['url'], + search['href'], + search['link'], + ]), + searchMethod: _firstString([ + search['method'], + search['httpMethod'], + search['requestMethod'], + ]), + searchBody: _firstString([ + search['body'], + search['data'], + search['payload'], + search['postBody'], + ]), + ruleKind: _string(search['kind']) ?? _string(search['type']), + charset: _string(json['charset']) ?? + _string(json['encoding']) ?? + _string(json['bookSourceCharset']), + urlEncoding: _string(json['urlEncoding']) ?? + _string(json['searchEncoding']) ?? + _string(json['bookSourceEncoding']), + bookInfoUrl: _firstString([bookInfo['url'], bookInfo['bookUrl']]), + bookInfoNameRule: _firstString([bookInfo['name'], bookInfo['bookName']]), + bookInfoAuthorRule: + _firstString([bookInfo['author'], bookInfo['bookAuthor']]), + bookInfoCoverRule: + _firstString([bookInfo['coverUrl'], bookInfo['cover']]), + bookInfoIntroRule: _firstString( + [bookInfo['intro'], bookInfo['introduce'], bookInfo['desc']]), + bookInfoTocRule: _firstString([ + bookInfo['tocUrl'], + bookInfo['chapterUrl'], + bookInfo['catalogUrl'], + bookInfo['directoryUrl'], + ]), + tocUrl: _firstString([toc['url'], toc['tocUrl']]), + tocListRule: + _firstString([toc['chapterList'], toc['chapters'], toc['list']]), + tocNameRule: + _firstString([toc['chapterName'], toc['name'], toc['title']]), + tocUrlRule: _firstString([toc['chapterUrl'], toc['url'], toc['href']]), + tocNextPageRule: _firstString([ + toc['tocNextPageRule'], + toc['nextTocUrl'], + toc['nextUrl'], + toc['nextPage'], + toc['tocNextPage'], + toc['nextPageUrl'], + json['tocNextPageRule'], + ]), + contentRule: + _firstString([content['content'], content['text'], content['body']]), + contentNextRule: _firstString([ + content['nextChapterUrl'], + content['nextContentUrl'], + content['next'], + content['nextPage'], + ]), + contentPrevRule: _firstString([ + content['prevChapterUrl'], + content['prevContentUrl'], + content['prev'], + content['prevPage'], + ]), + ); + } + + Map toJson() { + return { + 'bookSourceName': name, + 'bookSourceUrl': url, + 'bookSourceType': type, + 'enabled': enabled, + if (userAgent != null) 'httpUserAgent': userAgent, + if (charset != null) 'charset': charset, + if (urlEncoding != null) 'urlEncoding': urlEncoding, + if (headers.isNotEmpty) 'headers': headers, + 'ruleSearch': { + if (searchUrl != null) 'url': searchUrl, + if (searchListRule != null) 'bookList': searchListRule, + if (searchNameRule != null) 'name': searchNameRule, + if (searchAuthorRule != null) 'author': searchAuthorRule, + if (searchCoverRule != null) 'coverUrl': searchCoverRule, + if (searchDetailRule != null) 'detailUrl': searchDetailRule, + if (searchMethod != null) 'method': searchMethod, + if (searchBody != null) 'body': searchBody, + if (ruleKind != null) 'kind': ruleKind, + }, + 'ruleBookInfo': { + if (bookInfoUrl != null) 'url': bookInfoUrl, + if (bookInfoNameRule != null) 'name': bookInfoNameRule, + if (bookInfoAuthorRule != null) 'author': bookInfoAuthorRule, + if (bookInfoCoverRule != null) 'coverUrl': bookInfoCoverRule, + if (bookInfoIntroRule != null) 'intro': bookInfoIntroRule, + if (bookInfoTocRule != null) 'tocUrl': bookInfoTocRule, + }, + 'ruleToc': { + if (tocUrl != null) 'url': tocUrl, + if (tocListRule != null) 'chapterList': tocListRule, + if (tocNameRule != null) 'chapterName': tocNameRule, + if (tocUrlRule != null) 'chapterUrl': tocUrlRule, + if (tocNextPageRule != null) 'nextTocUrl': tocNextPageRule, + }, + 'ruleContent': { + if (contentRule != null) 'content': contentRule, + if (contentNextRule != null) 'nextChapterUrl': contentNextRule, + if (contentPrevRule != null) 'prevChapterUrl': contentPrevRule, + }, + }; + } +} + +class OnlineBookInfo { + const OnlineBookInfo({ + required this.title, + required this.tocUrl, + required this.sourceName, + this.author, + this.coverUrl, + this.intro, + }); + + final String title; + final String? author; + final String? coverUrl; + final String? intro; + final String tocUrl; + final String sourceName; +} + +class OnlineChapter { + const OnlineChapter({ + required this.title, + required this.url, + required this.index, + }); + + final String title; + final String url; + final int index; +} + +class OnlineChapterContent { + const OnlineChapterContent({ + required this.title, + this.textContent, + this.imageUrls = const [], + this.pageImages = const [], + this.nextChapterUrl, + this.prevChapterUrl, + }); + + final String title; + final String? textContent; + final List imageUrls; + final List pageImages; + final String? nextChapterUrl; + final String? prevChapterUrl; +} + +class OnlineReadingRecord { + const OnlineReadingRecord({ + required this.sourceId, + required this.sourceName, + required this.bookTitle, + required this.detailUrl, + required this.chapterUrl, + required this.chapterTitle, + required this.chapterIndex, + required this.totalChapters, + required this.lastReadTime, + }); + + final String sourceId; + final String sourceName; + final String bookTitle; + final String detailUrl; + final String chapterUrl; + final String chapterTitle; + final int chapterIndex; + final int totalChapters; + final DateTime lastReadTime; + + factory OnlineReadingRecord.fromJson(Map json) => + OnlineReadingRecord( + sourceId: json['sourceId']?.toString() ?? '', + sourceName: json['sourceName']?.toString() ?? '', + bookTitle: json['bookTitle']?.toString() ?? '', + detailUrl: json['detailUrl']?.toString() ?? '', + chapterUrl: json['chapterUrl']?.toString() ?? '', + chapterTitle: json['chapterTitle']?.toString() ?? '', + chapterIndex: _int(json['chapterIndex']) ?? 0, + totalChapters: _int(json['totalChapters']) ?? 0, + lastReadTime: + DateTime.tryParse(json['lastReadTime']?.toString() ?? '') ?? + DateTime.fromMillisecondsSinceEpoch(0), + ); + + Map toJson() => { + 'sourceId': sourceId, + 'sourceName': sourceName, + 'bookTitle': bookTitle, + 'detailUrl': detailUrl, + 'chapterUrl': chapterUrl, + 'chapterTitle': chapterTitle, + 'chapterIndex': chapterIndex, + 'totalChapters': totalChapters, + 'lastReadTime': lastReadTime.toIso8601String(), + }; +} + +enum LoadFailureReason { + network, + httpError, + ruleNotSupported, + ruleParseEmpty, + encoding, + unknown, +} + +class LegadoLoadException implements Exception { + const LegadoLoadException(this.reason, this.message, {this.cause}); + + final LoadFailureReason reason; + final String message; + final Object? cause; + + @override + String toString() => message; +} + +enum SourceCompatLevel { + full, + partial, + needsJs, + needsPost, + needsHeaders, + broken, + unknown, +} + +class SourceCompatInfo { + const SourceCompatInfo({ + required this.level, + required this.searchOk, + required this.bookInfoOk, + required this.tocOk, + required this.contentOk, + required this.hasJsRule, + required this.hasPutGet, + this.warnings = const [], + }); + + final SourceCompatLevel level; + final bool searchOk; + final bool bookInfoOk; + final bool tocOk; + final bool contentOk; + final bool hasJsRule; + final bool hasPutGet; + final List warnings; + + String get label { + switch (level) { + case SourceCompatLevel.full: + return '完全兼容'; + case SourceCompatLevel.partial: + return '部分兼容'; + case SourceCompatLevel.needsJs: + return '需要 JS'; + case SourceCompatLevel.needsPost: + return 'POST 源'; + case SourceCompatLevel.needsHeaders: + return '需请求头'; + case SourceCompatLevel.broken: + return '规则不完整'; + case SourceCompatLevel.unknown: + return '待检测'; + } + } +} + +class UnifiedSearchResult { + const UnifiedSearchResult({ + required this.sourceId, + required this.sourceName, + required this.title, + this.author, + this.coverUrl, + this.detailUrl, + this.errorMessage, + }); + + const UnifiedSearchResult.error({ + required this.sourceId, + required this.sourceName, + required this.errorMessage, + }) : title = '搜索失败', + author = null, + coverUrl = null, + detailUrl = null; + + final String sourceId; + final String sourceName; + final String title; + final String? author; + final String? coverUrl; + final String? detailUrl; + final String? errorMessage; + + bool get isError => errorMessage?.trim().isNotEmpty == true; +} + +String _sourceId(String name, String url) { + final raw = '$name|$url'; + return raw.codeUnits.fold(0, (value, unit) => value + unit).toString(); +} + +String? _string(Object? value) { + final text = value?.toString().trim(); + return text == null || text.isEmpty ? null : text; +} + +int? _int(Object? value) { + if (value is int) return value; + if (value is num) return value.toInt(); + return int.tryParse(value?.toString() ?? ''); +} + +int _sourceType( + Object? value, { + Map json = const {}, + Map search = const {}, + Map content = const {}, +}) { + final number = _int(value); + if (number == 2) return 2; + final text = [ + value, + json['bookSourceName'], + json['bookSourceGroup'], + json['bookSourceUrl'], + ].whereType().join(' ').toLowerCase(); + final imageStyle = _string(content['imageStyle'])?.toLowerCase() ?? ''; + final contentRule = _string(content['content'])?.toLowerCase() ?? ''; + final hasComicFields = const [ + 'comicList', + 'comicName', + 'comicUrl', + 'comicCover', + ].any(search.containsKey); + final hasImageContent = imageStyle == 'full' || + contentRule.contains(' values) { + for (final value in values) { + final text = _string(value); + if (text != null) return text; + } + return null; +} + +Map _map(Object? value) { + return value is Map ? Map.from(value) : {}; +} + +Map _headers(Object? value) { + if (value is Map) { + return value.map((key, item) => MapEntry(key.toString(), item.toString())); + } + if (value is String) { + final trimmed = value.trim(); + if (trimmed.isEmpty) return const {}; + try { + final decoded = _mapFromJsonString(trimmed); + if (decoded.isNotEmpty) return _headers(decoded); + } catch (_) { + // Fall through to the line parser used by several Legado repositories. + } + final headers = {}; + for (final line in trimmed.split(RegExp(r'[\r\n]+'))) { + final index = line.indexOf(':'); + if (index <= 0) continue; + final key = line.substring(0, index).trim(); + final item = line.substring(index + 1).trim(); + if (key.isNotEmpty && item.isNotEmpty) headers[key] = item; + } + return headers; + } + return const {}; +} + +Map _mapFromJsonString(String value) { + final decoded = value.startsWith('{') ? _jsonDecode(value) : null; + return decoded is Map + ? Map.from(decoded) + : {}; +} + +Object? _jsonDecode(String value) { + // Kept local so malformed header strings do not affect source import. + try { + return jsonDecode(value); + } catch (_) { + return null; + } +} diff --git a/sakura_mobile/lib/src/core/legado_rule_executor.dart b/sakura_mobile/lib/src/core/legado_rule_executor.dart new file mode 100644 index 0000000..e775ee8 --- /dev/null +++ b/sakura_mobile/lib/src/core/legado_rule_executor.dart @@ -0,0 +1,484 @@ +import 'package:html/dom.dart'; +import 'package:html/parser.dart' as html_parser; + +import 'legado_rule_parser.dart'; + +typedef LegadoJsCallback = String Function({ + required String script, + required String result, + required String rawHtml, + required String baseUrl, + required Map context, +}); + +class UnsupportedLegadoRule implements Exception { + const UnsupportedLegadoRule(this.rule, this.feature); + + final String rule; + final String feature; + + @override + String toString() => 'Unsupported Legado rule feature: $feature'; +} + +class LegadoRuleExecutor { + LegadoRuleExecutor({LegadoJsCallback? runJs}) : _runJs = runJs; + + final LegadoJsCallback? _runJs; + final Map _context = {}; + + Map get context => Map.unmodifiable(_context); + + void clearContext() => _context.clear(); + + void setContext(String key, Object? value) { + _context[key] = value?.toString() ?? ''; + } + + String expandContext(String value) { + final withGet = value.replaceAllMapped( + RegExp(r'@get:\{([^}]+)\}'), + (match) { + final key = (match.group(1) ?? '').trim(); + return _context[key] ?? match.group(0) ?? ''; + }, + ); + return withGet.replaceAllMapped( + RegExp(r'\{\{(?!\$\.)([^}]+)\}\}'), + (match) { + final key = (match.group(1) ?? '').trim(); + return _context[key] ?? match.group(0) ?? ''; + }, + ); + } + + String select( + String rawHtml, + String fragment, + String? rule, { + required String baseUrl, + String? kind, + }) { + final values = selectList( + rawHtml, + fragment, + rule, + baseUrl: baseUrl, + kind: kind, + ); + return values.join('\n'); + } + + List selectList( + String rawHtml, + String fragment, + String? rule, { + required String baseUrl, + String? kind, + }) { + final normalized = rule?.trim() ?? ''; + if (normalized.isEmpty) return const []; + + for (final alternative in _splitOutsideJs(normalized, '||')) { + final joined = []; + var failed = false; + for (final part in _splitOutsideJs(alternative, '&&')) { + final values = _executePart( + rawHtml, + fragment, + part, + baseUrl: baseUrl, + kind: kind, + ); + if (values.isEmpty) { + failed = true; + break; + } + joined.addAll(values); + } + if (!failed && joined.isNotEmpty) return joined; + } + return const []; + } + + List _executePart( + String rawHtml, + String fragment, + String rawRule, { + required String baseUrl, + String? kind, + }) { + var rule = expandContext(rawRule.trim()); + final js = _extractJs(rule); + rule = js.rule; + final replacements = _extractReplacements(rule); + rule = replacements.rule.trim(); + final put = RegExp(r'@put:\{([^:}]+):([^}]+)\}').firstMatch(rawRule); + rule = rule.replaceAll(RegExp(r'@put:\{[^}]+\}'), '').trim(); + + var values = rule.isEmpty + ? [fragment] + : _executeChain(fragment, rule, baseUrl: baseUrl, kind: kind); + + if (put != null) { + final key = put.group(1)?.trim() ?? ''; + final valueRule = put.group(2)?.trim() ?? ''; + var value = values.isEmpty ? '' : values.first; + if (valueRule.isNotEmpty) { + for (final candidate in valueRule.split('||')) { + final candidateRule = candidate.trim().startsWith(r'$') + ? candidate.trim() + : r'$.' + candidate.trim(); + value = RuleParser(fragment, baseUrl: baseUrl) + .select(fragment, candidateRule, kind: 'json'); + if (value.isNotEmpty) break; + } + } + if (key.isNotEmpty && value.isNotEmpty) _context[key] = value; + } + + if (js.script != null) { + final callback = _runJs; + if (callback == null) { + throw UnsupportedLegadoRule(rawRule, 'javascript'); + } + final input = values.join('\n'); + final output = callback( + script: js.script!, + result: input.isEmpty ? rawHtml : input, + rawHtml: rawHtml, + baseUrl: baseUrl, + context: _context, + ); + values = output + .split(RegExp(r'[\r\n]+')) + .map((item) => item.trim()) + .where((item) => item.isNotEmpty) + .toList(); + if (js.afterRule.trim().isNotEmpty && values.isNotEmpty) { + values = _executeChain( + values.join('\n'), + expandContext(js.afterRule), + baseUrl: baseUrl, + kind: kind, + ); + } + } + + for (final replacement in replacements.items) { + values = values + .map((value) => value.replaceAll( + RegExp(replacement.pattern, dotAll: true), + replacement.replacement, + )) + .map((value) => value.trim()) + .where((value) => value.isNotEmpty) + .toList(); + } + return values; + } + + List _executeChain( + String fragment, + String rule, { + required String baseUrl, + String? kind, + }) { + final expandedRule = _expandJsonTemplate(rule, fragment, baseUrl); + if (_looksLikeLiteral(expandedRule)) return [expandedRule]; + if (_isAtomicRule(expandedRule, kind)) { + final parser = RuleParser(fragment, baseUrl: baseUrl); + final values = parser.selectList(expandedRule, kind: kind); + if (values.isNotEmpty) return values; + final single = parser.select(fragment, expandedRule, kind: kind); + return single.isEmpty ? const [] : [single]; + } + + final tokens = _chainTokens(expandedRule); + if (tokens.isEmpty) return const []; + var current = [fragment]; + for (var index = 0; index < tokens.length; index++) { + final token = tokens[index]; + if (token.startsWith('put:') || token.startsWith('js:')) continue; + if (token.startsWith('get:')) { + final key = token.substring(4).replaceAll(RegExp(r'[{}]'), '').trim(); + final value = _context[key] ?? ''; + current = value.isEmpty ? const [] : [value]; + } else if (token == 'text') { + current = current.map(_elementText).where(_notEmpty).toList(); + } else if (token == 'html') { + current = current.map(_elementHtml).where(_notEmpty).toList(); + } else if (_isAttribute(token)) { + current = current + .map((value) => _attribute(value, token)) + .where(_notEmpty) + .toList(); + } else { + final selectorToken = token.startsWith('css:') + ? token.substring(4) + : token.startsWith('tag.') + ? token.substring(4) + : token; + current = _selectElements(current, selectorToken); + } + if (current.isEmpty) return const []; + } + return current; + } + + List _selectElements(List inputs, String rawSelector) { + final spec = _selectorSpec(rawSelector); + final output = []; + for (final input in inputs) { + try { + final document = html_parser.parseFragment(input); + var elements = spec.textContains == null + ? document.querySelectorAll(spec.selector) + : document + .querySelectorAll('*') + .where((element) => element.text.contains(spec.textContains!)) + .toList(growable: false); + if (spec.excludedIndex != null && elements.isNotEmpty) { + final excluded = + _normalizedIndex(spec.excludedIndex!, elements.length); + elements = [ + for (var i = 0; i < elements.length; i++) + if (i != excluded) elements[i], + ]; + } + if (spec.index != null && elements.isNotEmpty) { + final selected = _normalizedIndex(spec.index!, elements.length); + if (selected >= 0 && selected < elements.length) { + output.add(elements[selected].outerHtml); + } + } else { + output.addAll(elements.map((element) => element.outerHtml)); + } + } catch (_) { + // A malformed selector returns no values and allows the next || rule. + } + } + return output; + } + + _SelectorSpec _selectorSpec(String raw) { + var value = raw.trim(); + int? excluded; + final exclusion = RegExp(r'!(\-?\d+)$').firstMatch(value); + if (exclusion != null) { + excluded = int.tryParse(exclusion.group(1) ?? ''); + value = value.substring(0, exclusion.start); + } + int? index; + final indexed = RegExp(r'\.(\-?\d+)$').firstMatch(value); + if (indexed != null) { + index = int.tryParse(indexed.group(1) ?? ''); + value = value.substring(0, indexed.start); + } + if (value.startsWith('class.')) { + value = '.${value.substring(6)}'; + } else if (value.startsWith('id.')) { + value = '#${value.substring(3)}'; + } else if (value.startsWith('tag.')) { + value = value.substring(4); + } else if (value.startsWith('text.')) { + final text = value.substring(5).trim(); + return _SelectorSpec('*', index: index, textContains: text); + } + if (value.isEmpty) value = '*'; + return _SelectorSpec(value, index: index, excludedIndex: excluded); + } + + int _normalizedIndex(int index, int length) => + index < 0 ? length + index : index; + + List _chainTokens(String rule) { + final normalized = rule.replaceAll(RegExp(r'@put:\{[^}]+\}'), ''); + final pieces = normalized.split('@'); + final tokens = []; + if (pieces.first.trim().isNotEmpty) tokens.add(pieces.first.trim()); + for (var i = 1; i < pieces.length; i++) { + final piece = pieces[i].trim(); + if (piece.isEmpty) continue; + if (piece.startsWith('tag.') || piece.startsWith('css:')) { + tokens.add(piece); + } else { + tokens.add(piece); + } + } + return tokens; + } + + bool _isAtomicRule(String rule, String? kind) { + final lower = rule.toLowerCase(); + return kind == 'json' || + kind == 'regex' || + lower.startsWith(r'$.') || + lower.startsWith('json:') || + lower.startsWith('regex:'); + } + + String _expandJsonTemplate(String rule, String fragment, String baseUrl) { + return rule.replaceAllMapped(RegExp(r'\{\{(\$\.[^}]+)\}\}'), (match) { + final path = match.group(1) ?? ''; + final value = RuleParser(fragment, baseUrl: baseUrl) + .select(fragment, path, kind: 'json'); + return value.isEmpty ? match.group(0) ?? '' : value; + }); + } + + bool _looksLikeLiteral(String rule) { + final value = rule.trim().toLowerCase(); + return value.startsWith('http://') || + value.startsWith('https://') || + value.startsWith('//') || + (value.startsWith('/') && !value.startsWith('//##')); + } + + bool _isAttribute(String token) { + const attributes = { + 'src', + 'href', + 'class', + 'alt', + 'title', + 'content', + 'value', + 'style', + }; + return attributes.contains(token) || token.startsWith('data-'); + } + + String _attribute(String html, String name) { + final fragment = html_parser.parseFragment(html); + return (fragment.querySelector('*')?.attributes[name] ?? '').trim(); + } + + String _elementText(String html) => + (html_parser.parseFragment(html).text ?? '') + .replaceAll(RegExp(r'\s+'), ' ') + .trim(); + + String _elementHtml(String html) { + final fragment = html_parser.parseFragment(html); + final element = fragment.querySelector('*'); + return element?.innerHtml.trim() ?? ''; + } + + _JsRule _extractJs(String rule) { + final block = + RegExp(r'([\s\S]*?)', caseSensitive: false).firstMatch(rule); + if (block != null) { + return _JsRule( + rule.substring(0, block.start), + block.group(1)?.trim(), + rule.substring(block.end), + ); + } + final marker = RegExp(r'@js:', caseSensitive: false).firstMatch(rule); + if (marker != null) { + return _JsRule( + rule.substring(0, marker.start), + rule.substring(marker.end).trim(), + '', + ); + } + return _JsRule(rule, null, ''); + } + + _ReplaceRule _extractReplacements(String rule) { + final items = <_Replacement>[]; + var base = rule; + final marker = rule.indexOf('###'); + if (marker >= 0) { + final tail = rule.substring(marker + 3); + final end = tail.indexOf('###'); + final pattern = end >= 0 ? tail.substring(0, end) : tail; + if (pattern.isNotEmpty) items.add(_Replacement(pattern, '')); + base = rule.substring(0, marker); + } + final replaceMarker = base.indexOf('##'); + if (replaceMarker >= 0) { + final tail = base.substring(replaceMarker + 2); + final separator = tail.indexOf('##'); + final pattern = separator >= 0 ? tail.substring(0, separator) : tail; + final replacement = separator >= 0 ? tail.substring(separator + 2) : ''; + if (pattern.isNotEmpty) items.add(_Replacement(pattern, replacement)); + base = base.substring(0, replaceMarker); + } + return _ReplaceRule(base, items); + } + + List _splitOutsideJs(String rule, String separator) { + final jsIndex = [ + rule.indexOf('@js:'), + rule.toLowerCase().indexOf(''), + ] + .where((index) => index >= 0) + .fold(rule.length, (a, b) => a < b ? a : b); + final prefix = rule.substring(0, jsIndex); + final suffix = rule.substring(jsIndex); + final parts = []; + var start = 0; + var braceDepth = 0; + String? quote; + for (var index = 0; index <= prefix.length - separator.length; index++) { + final char = prefix[index]; + if (quote != null) { + if (char == quote && (index == 0 || prefix[index - 1] != '\\')) { + quote = null; + } + continue; + } + if (char == '"' || char == "'") { + quote = char; + continue; + } + if (char == '{') braceDepth++; + if (char == '}' && braceDepth > 0) braceDepth--; + if (braceDepth == 0 && prefix.startsWith(separator, index)) { + parts.add(prefix.substring(start, index)); + start = index + separator.length; + index += separator.length - 1; + } + } + parts.add(prefix.substring(start)); + if (parts.length == 1) return [rule]; + parts[parts.length - 1] = '${parts.last}$suffix'; + return parts.map((part) => part.trim()).where(_notEmpty).toList(); + } + + bool _notEmpty(String value) => value.trim().isNotEmpty; +} + +class _SelectorSpec { + const _SelectorSpec( + this.selector, { + this.index, + this.excludedIndex, + this.textContains, + }); + final String selector; + final int? index; + final int? excludedIndex; + final String? textContains; +} + +class _JsRule { + const _JsRule(this.rule, this.script, this.afterRule); + final String rule; + final String? script; + final String afterRule; +} + +class _ReplaceRule { + const _ReplaceRule(this.rule, this.items); + final String rule; + final List<_Replacement> items; +} + +class _Replacement { + const _Replacement(this.pattern, this.replacement); + final String pattern; + final String replacement; +} diff --git a/sakura_mobile/lib/src/core/legado_rule_parser.dart b/sakura_mobile/lib/src/core/legado_rule_parser.dart new file mode 100644 index 0000000..682a535 --- /dev/null +++ b/sakura_mobile/lib/src/core/legado_rule_parser.dart @@ -0,0 +1,266 @@ +import 'dart:convert'; + +import 'package:html/parser.dart' as html_parser; + +class RuleParser { + RuleParser(this.body, {required this.baseUrl}); + + final String body; + final String baseUrl; + + Object? _decodedJson; + + List selectList(String? rule, {String? kind}) { + for (final normalized in _ruleAlternatives(rule)) { + final type = _resolveKind(normalized, kind); + if (type == 'json') { + final values = _jsonPath(normalized); + final result = + values.map(_stringifyJsonValue).where(_notEmpty).toList(); + if (result.isNotEmpty) return result; + continue; + } + if (type == 'regex') { + final matches = RegExp(_stripPrefix(normalized), dotAll: true) + .allMatches(body) + .map((match) => + match.groupCount >= 1 ? match.group(1) : match.group(0)) + .whereType() + .where(_notEmpty) + .toList(); + if (matches.isNotEmpty) return matches; + continue; + } + final fragments = _cssFragments(normalized); + if (fragments.isNotEmpty) return fragments; + } + return const []; + } + + String select(String fragment, String? rule, {String? kind}) { + for (final normalized in _ruleAlternatives(rule)) { + final type = _resolveKind(normalized, kind); + if (type == 'json') { + final values = + RuleParser(fragment, baseUrl: baseUrl)._jsonPath(normalized); + final value = values.isEmpty ? '' : _stringifyJsonValue(values.first); + if (value.isNotEmpty) return value; + continue; + } + if (type == 'regex') { + final match = + RegExp(_stripPrefix(normalized), dotAll: true).firstMatch(fragment); + final value = + _clean(match?.groupCount == 0 ? match?.group(0) : match?.group(1)); + if (value.isNotEmpty) return value; + continue; + } + final value = _cssText(fragment, normalized); + if (value.isNotEmpty) return value; + } + return ''; + } + + List _jsonPath(String rule) { + final path = _stripPrefix(rule) + .replaceFirst(RegExp(r'^\$\.?'), '') + .split('.') + .where((item) => item.isNotEmpty) + .toList(); + var current = _jsonBody(); + for (final segment in path) { + if (segment.endsWith('[*]')) { + final key = segment.substring(0, segment.length - 3); + final list = _child(current, key); + current = list is List ? list : const []; + continue; + } + current = _child(current, segment); + } + if (current is List) return current; + if (current == null) return const []; + return [current]; + } + + Object? _jsonBody() { + if (_decodedJson != null) return _decodedJson; + try { + var text = body.trimLeft(); + if (text.isNotEmpty && text.codeUnitAt(0) == 0xFEFF) { + text = text.substring(1); + } + final jsonp = RegExp( + r'^[a-zA-Z_$][\w$]*\s*\(\s*(.*)\s*\)\s*;?\s*$', + dotAll: true, + ).firstMatch(text.trim()); + if (jsonp != null) { + text = jsonp.group(1) ?? text; + } + _decodedJson = jsonDecode(text); + } on FormatException { + _decodedJson = null; + } + return _decodedJson; + } + + Object? _child(Object? node, String key) { + if (key.isEmpty) return node; + if (key == '*') { + if (node is List) return node; + if (node is Map) return node.values.toList(growable: false); + return null; + } + final indexed = RegExp(r'^([^\[]*)\[(\d+|\*)\]$').firstMatch(key); + if (indexed != null) { + final parentKey = indexed.group(1) ?? ''; + final indexText = indexed.group(2) ?? ''; + final parent = parentKey.isEmpty ? node : _child(node, parentKey); + if (parent is List) { + if (indexText == '*') return parent; + final index = int.tryParse(indexText); + if (index != null && index >= 0 && index < parent.length) { + return parent[index]; + } + } + return null; + } + if (node is Map) return node[key]; + if (node is List) { + return node + .map((item) => _child(item, key)) + .where((item) => item != null) + .toList(); + } + return null; + } + + List _cssFragments(String selector) { + final cleanSelector = _selectorWithoutAttr(selector); + if (cleanSelector.isEmpty) return const []; + try { + final document = html_parser.parse(body); + final elements = document.querySelectorAll(cleanSelector); + final values = elements + .map((element) => element.outerHtml) + .where(_notEmpty) + .toList(growable: false); + if (values.isNotEmpty) return values; + } catch (_) { + // Fall through to the legacy regex parser for malformed selectors. + } + return _legacyCssFragments(cleanSelector); + } + + String _cssText(String fragment, String selector) { + final attr = RegExp(r'@([a-zA-Z0-9_-]+)$').firstMatch(selector)?.group(1); + final cleanSelector = _selectorWithoutAttr(selector); + try { + final document = html_parser.parse(fragment); + final element = cleanSelector.isEmpty + ? document.body + : document.querySelector(cleanSelector); + if (element != null) { + if (attr != null) return _clean(element.attributes[attr]); + return _clean(element.text); + } + } catch (_) { + // Fall through to the legacy regex parser for malformed selectors. + } + return _legacyCssText(fragment, selector); + } + + List _legacyCssFragments(String selector) { + final tag = _selectorTag(selector); + if (tag == null) return const []; + final pattern = + RegExp('<$tag\\b[^>]*>.*?', caseSensitive: false, dotAll: true); + return pattern + .allMatches(body) + .map((match) => match.group(0) ?? '') + .where(_notEmpty) + .toList(); + } + + String _legacyCssText(String fragment, String selector) { + final attr = RegExp(r'@([a-zA-Z0-9_-]+)$').firstMatch(selector)?.group(1); + final cleanSelector = _selectorWithoutAttr(selector); + final tag = _selectorTag(cleanSelector); + final target = tag == null + ? fragment + : RegExp('<$tag\\b[^>]*>.*?', caseSensitive: false, dotAll: true) + .firstMatch(fragment) + ?.group(0) ?? + ''; + if (attr != null) { + return _clean( + RegExp( + "$attr=[\"']([^\"']+)[\"']", + caseSensitive: false, + ).firstMatch(target)?.group(1), + ); + } + return _clean(target.replaceAll(RegExp(r'<[^>]+>'), ' ')); + } + + String _selectorWithoutAttr(String selector) { + return selector.replaceFirst(RegExp(r'@([a-zA-Z0-9_-]+)$'), '').trim(); + } + + String? _selectorTag(String selector) { + final match = + RegExp(r'^[a-zA-Z][a-zA-Z0-9_-]*').firstMatch(selector.trim()); + return match?.group(0); + } + + String _normalizeRule(String? rule) { + return rule?.split('&&').first.trim() ?? ''; + } + + List _ruleAlternatives(String? rule) { + final normalized = _normalizeRule(rule); + if (normalized.isEmpty) return const []; + return normalized + .split('||') + .map((item) => item.trim()) + .where((item) => item.isNotEmpty) + .toList(growable: false); + } + + String _resolveKind(String rule, String? explicitKind) { + final explicit = explicitKind?.trim().toLowerCase(); + if (explicit == 'json' || explicit == 'regex' || explicit == 'css') { + return explicit!; + } + final lower = rule.toLowerCase(); + if (lower.startsWith('json:') || lower.startsWith(r'$.')) return 'json'; + if (lower.startsWith('regex:')) return 'regex'; + return 'css'; + } + + String _stripPrefix(String rule) { + final stripped = rule + .replaceFirst(RegExp(r'^(json|regex):', caseSensitive: false), '') + .trim(); + return stripped.replaceAllMapped( + RegExp(r'''\[['"]([^'"]+)['"]\]'''), + (match) => '.${match.group(1)}', + ); + } + + String _stringifyJsonValue(Object? value) { + if (value == null) return ''; + if (value is String) return _clean(value); + if (value is Map || value is List) return jsonEncode(value); + return _clean(value.toString()); + } + + bool _notEmpty(String value) => value.trim().isNotEmpty; + + String _clean(String? value) { + return (value ?? '') + .replaceAll(RegExp(r'\s+'), ' ') + .replaceAll(' ', ' ') + .replaceAll('&', '&') + .trim(); + } +} diff --git a/sakura_mobile/lib/src/core/legado_toc_pagination.dart b/sakura_mobile/lib/src/core/legado_toc_pagination.dart new file mode 100644 index 0000000..5c79179 --- /dev/null +++ b/sakura_mobile/lib/src/core/legado_toc_pagination.dart @@ -0,0 +1,111 @@ +import 'package:html/parser.dart' as html_parser; + +import 'legado_rule_executor.dart'; + +class TocPaginationResolver { + TocPaginationResolver(this._rules); + + final LegadoRuleExecutor _rules; + + String? resolveNextPage({ + required String currentUrl, + required String currentHtml, + required String? rule, + required int pageIndex, + }) { + final raw = rule?.trim() ?? ''; + if (raw.isEmpty) return null; + + if (raw.startsWith('!')) { + return _applySlice(currentUrl, raw.substring(1)); + } + if (raw.contains('{{page')) { + return _resolve(currentUrl, renderPageTemplate(raw, pageIndex + 1)); + } + if (RegExp(r'(?:[?&]|\b)(offset|start)=\d+').hasMatch(raw)) { + return _resolve(currentUrl, _renderOffset(raw, pageIndex)); + } + if (raw.startsWith('//')) { + final value = _selectSimpleXPath(currentHtml, raw); + return value == null ? null : _resolve(currentUrl, value); + } + if (_isSelector(raw) || raw.startsWith('') || raw.startsWith('@js:')) { + final value = _rules.select( + currentHtml, + currentHtml, + raw, + baseUrl: currentUrl, + ); + return value.trim().isEmpty ? null : _resolve(currentUrl, value.trim()); + } + return _resolve(currentUrl, renderPageTemplate(raw, pageIndex + 1)); + } + + String renderPageTemplate(String template, int page) { + return template + .replaceAll('{{page+1}}', '${page + 1}') + .replaceAll('{{page-1}}', '${page - 1}') + .replaceAll('{{page}}', '$page') + .replaceAll('{page+1}', '${page + 1}') + .replaceAll('{page-1}', '${page - 1}') + .replaceAll('{page}', '$page'); + } + + String _renderOffset(String template, int pageIndex) { + return template.replaceAllMapped( + RegExp(r'(offset|start)=(\d+)'), + (match) { + final name = match.group(1)!; + final initial = int.parse(match.group(2)!); + return '$name=${initial + initial * pageIndex}'; + }, + ); + } + + String? _applySlice(String value, String expression) { + final parts = expression.split(':'); + if (parts.length != 2) return null; + final rawStart = int.tryParse(parts[0]) ?? 0; + final rawEnd = parts[1].isEmpty ? null : int.tryParse(parts[1]); + final start = (rawStart < 0 ? value.length + rawStart : rawStart) + .clamp(0, value.length); + final end = (rawEnd == null + ? value.length + : rawEnd < 0 + ? value.length + rawEnd + : rawEnd) + .clamp(start, value.length); + return value.substring(start, end); + } + + String? _selectSimpleXPath(String html, String xpath) { + final text = RegExp( + r'''contains\s*\(\s*text\(\)\s*,\s*['"]([^'"]+)['"]\s*\)''', + caseSensitive: false, + ).firstMatch(xpath)?.group(1); + final attribute = + RegExp(r'/@([a-zA-Z0-9_-]+)\s*$').firstMatch(xpath)?.group(1) ?? 'href'; + final document = html_parser.parse(html); + for (final element in document.querySelectorAll('a')) { + if (text == null || element.text.contains(text)) { + final value = element.attributes[attribute]; + if (value?.trim().isNotEmpty == true) return value!.trim(); + } + } + return null; + } + + bool _isSelector(String value) { + return value.startsWith('text.') || + value.startsWith('class.') || + value.startsWith('id.') || + value.startsWith('tag.') || + value.startsWith('@'); + } + + String _resolve(String baseUrl, String value) { + final parsed = Uri.tryParse(value); + if (parsed?.hasScheme == true) return value; + return Uri.parse(baseUrl).resolve(value).toString(); + } +} diff --git a/sakura_mobile/lib/src/core/manga_collections.dart b/sakura_mobile/lib/src/core/manga_collections.dart new file mode 100644 index 0000000..4da4364 --- /dev/null +++ b/sakura_mobile/lib/src/core/manga_collections.dart @@ -0,0 +1,208 @@ +import '../models/artwork.dart'; + +class MangaCollection { + const MangaCollection({ + required this.id, + required this.title, + required this.author, + required this.cover, + required this.items, + }); + + final String id; + final String title; + final String? author; + final Artwork cover; + final List items; + + String get latestItemTitle => items.isEmpty ? '' : items.last.title; + + int get readPageCount => + items.where((item) => item.lastViewedAt != null).length; + + String get readProgressLabel => + readPageCount <= 0 ? '未开始' : '已读 $readPageCount / ${items.length} 页'; + + DateTime? get latestActivityAt { + return items + .map((item) => item.lastViewedAt ?? item.updatedAt ?? item.createdAt) + .whereType() + .fold( + null, + (value, item) => value == null || item.isAfter(value) ? item : value, + ); + } + + String get itemTitlesPreview => + items.take(3).map((item) => item.title).join(' · '); + + int get preferredStartIndex { + var bestIndex = 0; + DateTime? bestTime; + for (var index = 0; index < items.length; index += 1) { + final value = items[index].lastViewedAt; + if (value == null) { + continue; + } + if (bestTime == null || value.isAfter(bestTime)) { + bestTime = value; + bestIndex = index; + } + } + return bestIndex; + } +} + +List buildMangaCollections(List items) { + final groups = >{}; + for (final item in items.where((item) => item.contentType == 'manga')) { + final key = '${item.author ?? ''}::${normalizeMangaTitle(item.title)}'; + groups.putIfAbsent(key, () => []).add(item); + } + final entries = groups.entries.map((entry) { + final sortedItems = sortMangaItems(entry.value); + final cover = sortedItems.first; + return MangaCollection( + id: entry.key, + title: normalizeMangaTitle(cover.title), + author: cover.author, + cover: cover, + items: sortedItems, + ); + }).toList(); + entries.sort((a, b) { + final aTime = a.latestActivityAt; + final bTime = b.latestActivityAt; + if (aTime != null && bTime != null) { + return bTime.compareTo(aTime); + } + if (aTime != null) { + return -1; + } + if (bTime != null) { + return 1; + } + return a.title.compareTo(b.title); + }); + return entries; +} + +MangaCollection? findMangaCollectionForArtwork( + List items, + Artwork artwork, +) { + for (final entry in buildMangaCollections(items)) { + if (entry.items.any((item) => item.id == artwork.id)) { + return entry; + } + } + return null; +} + +List sortMangaItems(Iterable items) { + final sortedItems = [...items]..sort(compareMangaItems); + return sortedItems; +} + +int compareMangaItems(Artwork a, Artwork b) { + final aOrder = extractMangaItemOrder(a.title); + final bOrder = extractMangaItemOrder(b.title); + if (aOrder != bOrder) { + return aOrder.compareTo(bOrder); + } + final aViewed = a.lastViewedAt; + final bViewed = b.lastViewedAt; + if (aViewed != null && bViewed != null) { + return aViewed.compareTo(bViewed); + } + return a.title.compareTo(b.title); +} + +String normalizeMangaTitle(String title) { + final trimmed = title.trim(); + final patterns = [ + RegExp( + r'\s*[-_·•]\s*第?[0-9一二三四五六七八九十百千零两]+(?:话|章|卷|册|回|节|篇)?\s*$', + caseSensitive: false, + ), + RegExp( + r'\s*第?[0-9一二三四五六七八九十百千零两]+(?:话|章|卷|册|回|节|篇)?\s*$', + caseSensitive: false, + ), + RegExp(r'\s*(?:ep|episode|chapter|part)\s*\d+\s*$', caseSensitive: false), + RegExp(r'\s*#\d+\s*$', caseSensitive: false), + RegExp(r'\s*(?:page|p)\s*\d+\s*$', caseSensitive: false), + ]; + var value = trimmed; + for (final pattern in patterns) { + value = value.replaceFirst(pattern, '').trim(); + } + return value.isEmpty ? trimmed : value; +} + +int extractMangaItemOrder(String title) { + final direct = RegExp( + r'第?\s*([0-9]+|[一二三四五六七八九十百千零两]+)\s*(?:话|章|卷|册|回|节|篇)', + caseSensitive: false, + ).firstMatch(title); + if (direct != null) { + return _parseMangaNumber(direct.group(1) ?? '') ?? 0; + } + final english = RegExp( + r'(?:ep|episode|chapter|part|page|p)\s*(\d+)', + caseSensitive: false, + ).firstMatch(title); + if (english != null) { + return int.tryParse(english.group(1) ?? '') ?? 0; + } + final hash = RegExp(r'#(\d+)').firstMatch(title); + return int.tryParse(hash?.group(1) ?? '') ?? 0; +} + +int? _parseMangaNumber(String value) { + final trimmed = value.trim(); + if (trimmed.isEmpty) { + return null; + } + final direct = int.tryParse(trimmed); + if (direct != null) { + return direct; + } + const digits = { + '零': 0, + '一': 1, + '二': 2, + '两': 2, + '三': 3, + '四': 4, + '五': 5, + '六': 6, + '七': 7, + '八': 8, + '九': 9, + }; + const units = { + '十': 10, + '百': 100, + '千': 1000, + }; + var total = 0; + var current = 0; + for (final rune in trimmed.runes) { + final char = String.fromCharCode(rune); + if (digits.containsKey(char)) { + current = digits[char]!; + total += current; + continue; + } + if (units.containsKey(char)) { + final unit = units[char]!; + if (current == 0) { + current = 1; + } + total = total - current + current * unit; + current = 0; + } + } + return total == 0 ? null : total; +} diff --git a/sakura_mobile/lib/src/core/manga_image.dart b/sakura_mobile/lib/src/core/manga_image.dart new file mode 100644 index 0000000..236a0a3 --- /dev/null +++ b/sakura_mobile/lib/src/core/manga_image.dart @@ -0,0 +1,240 @@ +import 'dart:convert'; + +import 'package:dio/dio.dart'; + +const mangaDefaultUserAgent = + 'Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 Chrome/120 Mobile Safari/537.36'; + +class ImageCandidate { + const ImageCandidate({ + required this.raw, + required this.url, + required this.isValid, + this.reason, + }); + + final String raw; + final String? url; + final bool isValid; + final String? reason; +} + +class ParsedImageSpec { + const ParsedImageSpec({required this.url, this.headers = const {}}); + final String url; + final Map headers; +} + +class PageImage { + const PageImage({ + required this.index, + required this.raw, + required this.url, + required this.headers, + }); + + final int index; + final String raw; + final String url; + final Map headers; +} + +class ImageProbeResult { + const ImageProbeResult({ + required this.url, + required this.contentLength, + required this.first16BytesHex, + required this.looksLikeImage, + this.statusCode, + this.contentType, + this.error, + }); + + final String url; + final int? statusCode; + final String? contentType; + final int contentLength; + final String first16BytesHex; + final bool looksLikeImage; + final String? error; +} + +ParsedImageSpec splitLegadoImageSpec(String raw) { + final normalizedRaw = raw.replaceAll('"', '"').replaceAll('&', '&'); + final index = normalizedRaw.indexOf(',{'); + if (index < 0) return ParsedImageSpec(url: normalizedRaw.trim()); + final headers = {}; + try { + final decoded = jsonDecode(normalizedRaw.substring(index + 1).trim()); + if (decoded is Map) { + final value = decoded['headers']; + final headerMap = value is String ? jsonDecode(value) : value; + if (headerMap is Map) { + headerMap.forEach((key, item) { + headers[key.toString()] = item.toString(); + }); + } + } + } catch (_) { + // A malformed optional header suffix must not discard a valid URL. + } + return ParsedImageSpec( + url: normalizedRaw.substring(0, index).trim(), + headers: headers, + ); +} + +String normalizeMangaImageUrl(String raw, String baseUrl) { + var value = raw.trim().replaceAll(r'\/', '/').replaceAll(r'\u002F', '/'); + if (value.startsWith('//')) { + final scheme = Uri.tryParse(baseUrl)?.scheme; + value = '${scheme?.isNotEmpty == true ? scheme : 'https'}:$value'; + } + return Uri.parse(baseUrl).resolve(value).toString(); +} + +ImageCandidate validateImageCandidate(String raw, String baseUrl) { + final parsed = splitLegadoImageSpec(raw); + final value = parsed.url.trim(); + if (value.isEmpty) { + return ImageCandidate(raw: raw, url: null, isValid: false, reason: 'empty'); + } + if (value.toLowerCase().startsWith('data:')) { + return ImageCandidate( + raw: raw, + url: null, + isValid: false, + reason: 'data-uri', + ); + } + final lower = value.toLowerCase(); + if (const ['loading', 'placeholder', 'logo', 'icon'].any(lower.contains)) { + return ImageCandidate( + raw: raw, + url: value, + isValid: false, + reason: 'placeholder', + ); + } + try { + final normalized = normalizeMangaImageUrl(value, baseUrl); + final uri = Uri.tryParse(normalized); + if (uri == null || !uri.hasScheme || uri.host.isEmpty) { + return ImageCandidate( + raw: raw, + url: normalized, + isValid: false, + reason: 'invalid-url', + ); + } + return ImageCandidate(raw: raw, url: normalized, isValid: true); + } catch (_) { + return ImageCandidate( + raw: raw, + url: value, + isValid: false, + reason: 'invalid-url', + ); + } +} + +PageImage? buildPageImage({ + required String raw, + required String chapterUrl, + required int index, + required Map sourceHeaders, + String? userAgent, +}) { + final candidate = validateImageCandidate(raw, chapterUrl); + if (!candidate.isValid || candidate.url == null) return null; + final parsed = splitLegadoImageSpec(raw); + return PageImage( + index: index, + raw: raw, + url: candidate.url!, + headers: { + ...sourceHeaders, + 'User-Agent': userAgent?.trim().isNotEmpty == true + ? userAgent!.trim() + : mangaDefaultUserAgent, + 'Referer': chapterUrl, + ...parsed.headers, + }, + ); +} + +Future probeImage(Dio dio, PageImage image) async { + try { + final response = await dio.get>( + image.url, + options: Options( + responseType: ResponseType.bytes, + headers: image.headers, + validateStatus: (_) => true, + ), + ); + final bytes = response.data ?? const []; + return ImageProbeResult( + url: image.url, + statusCode: response.statusCode, + contentType: response.headers.value('content-type'), + contentLength: bytes.length, + first16BytesHex: bytes + .take(16) + .map((value) => value.toRadixString(16).padLeft(2, '0')) + .join(), + looksLikeImage: isImageMagic(bytes), + ); + } catch (error) { + return ImageProbeResult( + url: image.url, + contentLength: 0, + first16BytesHex: '', + looksLikeImage: false, + error: error.toString(), + ); + } +} + +bool isImageMagic(List bytes) { + if (bytes.length < 4) return false; + if (bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF) { + return true; + } + if (bytes[0] == 0x89 && + bytes[1] == 0x50 && + bytes[2] == 0x4E && + bytes[3] == 0x47) { + return true; + } + if (bytes.length >= 12 && + bytes[0] == 0x52 && + bytes[1] == 0x49 && + bytes[2] == 0x46 && + bytes[3] == 0x46 && + bytes[8] == 0x57 && + bytes[9] == 0x45 && + bytes[10] == 0x42 && + bytes[11] == 0x50) { + return true; + } + if (bytes[0] == 0x47 && + bytes[1] == 0x49 && + bytes[2] == 0x46 && + bytes[3] == 0x38) { + return true; + } + return bytes[0] == 0x42 && bytes[1] == 0x4D; +} + +double? extractMangaChapterNumber(String title) { + for (final pattern in [ + RegExp(r'第\s*(\d+(?:\.\d+)?)\s*[回话章节节]'), + RegExp(r'\bch\.?\s*(\d+(?:\.\d+)?)', caseSensitive: false), + RegExp(r'^\s*(\d+(?:\.\d+)?)\s*(?:话|回|章|节)?'), + ]) { + final value = pattern.firstMatch(title)?.group(1); + if (value != null) return double.tryParse(value); + } + return null; +} diff --git a/sakura_mobile/lib/src/core/online_cache.dart b/sakura_mobile/lib/src/core/online_cache.dart new file mode 100644 index 0000000..c17050a --- /dev/null +++ b/sakura_mobile/lib/src/core/online_cache.dart @@ -0,0 +1,24 @@ +import 'legado_models.dart'; + +class OnlineContentCache { + final _bookInfo = {}; + final _toc = >{}; + final _chapters = {}; + + String _key(String sourceId, String url) => '$sourceId|$url'; + + OnlineBookInfo? getBookInfo(String sourceId, String url) => + _bookInfo[_key(sourceId, url)]; + void putBookInfo(String sourceId, String url, OnlineBookInfo value) => + _bookInfo[_key(sourceId, url)] = value; + + List? getToc(String sourceId, String url) => + _toc[_key(sourceId, url)]; + void putToc(String sourceId, String url, List value) => + _toc[_key(sourceId, url)] = List.unmodifiable(value); + + OnlineChapterContent? getChapter(String sourceId, String url) => + _chapters[_key(sourceId, url)]; + void putChapter(String sourceId, String url, OnlineChapterContent value) => + _chapters[_key(sourceId, url)] = value; +} diff --git a/sakura_mobile/lib/src/core/online_reading_history.dart b/sakura_mobile/lib/src/core/online_reading_history.dart new file mode 100644 index 0000000..e04db4a --- /dev/null +++ b/sakura_mobile/lib/src/core/online_reading_history.dart @@ -0,0 +1,56 @@ +import 'dart:convert'; + +import 'package:shared_preferences/shared_preferences.dart'; + +import 'legado_models.dart'; + +class OnlineReadingHistory { + static const _storageKey = 'online_reading_history_v1'; + static const _maxRecords = 50; + + static Future> all() async { + final prefs = await SharedPreferences.getInstance(); + final raw = prefs.getString(_storageKey); + if (raw == null || raw.isEmpty) return const []; + try { + final decoded = jsonDecode(raw); + if (decoded is! List) return const []; + return decoded + .whereType() + .map((item) => OnlineReadingRecord.fromJson( + Map.from(item), + )) + .toList(growable: false); + } on FormatException { + return const []; + } + } + + static Future find( + String sourceId, + String detailUrl, + ) async { + final records = await all(); + for (final record in records) { + if (record.sourceId == sourceId && record.detailUrl == detailUrl) { + return record; + } + } + return null; + } + + static Future save(OnlineReadingRecord record) async { + final records = (await all()).toList(); + records.removeWhere((item) => + item.sourceId == record.sourceId && item.detailUrl == record.detailUrl); + records.insert(0, record); + if (records.length > _maxRecords) { + records.removeRange(_maxRecords, records.length); + } + final prefs = await SharedPreferences.getInstance(); + await prefs.setString( + _storageKey, + jsonEncode(records.map((item) => item.toJson()).toList()), + ); + } +} diff --git a/sakura_mobile/lib/src/core/online_shelf.dart b/sakura_mobile/lib/src/core/online_shelf.dart new file mode 100644 index 0000000..5a2bd7a --- /dev/null +++ b/sakura_mobile/lib/src/core/online_shelf.dart @@ -0,0 +1,108 @@ +import 'dart:convert'; + +import 'package:flutter/foundation.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class OnlineShelfEntry { + const OnlineShelfEntry({ + required this.sourceId, + required this.sourceName, + required this.title, + required this.detailUrl, + required this.savedAt, + this.contentType = 'novel', + this.author, + this.coverUrl, + }); + + final String sourceId; + final String sourceName; + final String title; + final String? author; + final String? coverUrl; + final String detailUrl; + final DateTime savedAt; + final String contentType; + + String get id => '$contentType|$sourceId|$detailUrl'; + bool get isManga => contentType == 'manga'; + bool get isNovel => !isManga; + + Map toJson() => { + 'sourceId': sourceId, + 'sourceName': sourceName, + 'title': title, + 'author': author, + 'coverUrl': coverUrl, + 'detailUrl': detailUrl, + 'savedAt': savedAt.toIso8601String(), + 'contentType': contentType, + }; + + factory OnlineShelfEntry.fromJson(Map json) => + OnlineShelfEntry( + sourceId: json['sourceId']?.toString() ?? '', + sourceName: json['sourceName']?.toString() ?? '', + title: json['title']?.toString() ?? '未命名作品', + author: json['author']?.toString(), + coverUrl: json['coverUrl']?.toString(), + detailUrl: json['detailUrl']?.toString() ?? '', + savedAt: DateTime.tryParse(json['savedAt']?.toString() ?? '') ?? + DateTime.now(), + contentType: + json['contentType']?.toString() == 'manga' ? 'manga' : 'novel', + ); +} + +class OnlineShelfStore extends ChangeNotifier { + static const _storageKey = 'online_novel_shelf_v1'; + final List _entries = []; + bool _loaded = false; + + List get entries => List.unmodifiable(_entries); + + Future load() async { + if (_loaded) return; + final prefs = await SharedPreferences.getInstance(); + final raw = prefs.getString(_storageKey); + if (raw != null) { + try { + final decoded = jsonDecode(raw); + if (decoded is List) { + _entries.addAll(decoded.whereType().map( + (item) => OnlineShelfEntry.fromJson( + Map.from(item), + ), + )); + } + } catch (_) { + // Keep the shelf usable even if an old local record is malformed. + } + } + _loaded = true; + notifyListeners(); + } + + Future save(OnlineShelfEntry entry) async { + await load(); + _entries.removeWhere((item) => item.id == entry.id); + _entries.insert(0, entry); + await _persist(); + notifyListeners(); + } + + Future remove(String id) async { + await load(); + _entries.removeWhere((item) => item.id == id); + await _persist(); + notifyListeners(); + } + + Future _persist() async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setString( + _storageKey, + jsonEncode(_entries.map((item) => item.toJson()).toList()), + ); + } +} diff --git a/sakura_mobile/lib/src/core/providers.dart b/sakura_mobile/lib/src/core/providers.dart new file mode 100644 index 0000000..fc59d18 --- /dev/null +++ b/sakura_mobile/lib/src/core/providers.dart @@ -0,0 +1,55 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import 'api_client.dart'; +import 'legado_engine.dart'; +import 'session.dart'; +import 'source_health.dart'; +import 'source_manager.dart'; +import 'search_scheduler.dart'; +import 'online_shelf.dart'; +import 'unified_search.dart'; + +final sessionProvider = StateNotifierProvider( + (ref) => SessionController()); + +final apiClientProvider = Provider((ref) { + ref.watch(sessionProvider); + return ref.read(sessionProvider.notifier).client(); +}); + +final sourceManagerProvider = ChangeNotifierProvider((ref) { + final manager = SourceManager(); + manager.initializeIfFirstRun(); + return manager; +}); + +final legadoEngineProvider = Provider((ref) => LegadoEngine()); + +final sourceHealthProvider = Provider((ref) { + final store = SourceHealthStore(); + store.load(); + ref.onDispose(store.flush); + return store; +}); + +final searchSchedulerProvider = Provider((ref) { + return SearchScheduler( + ref.watch(legadoEngineProvider), + ref.watch(sourceHealthProvider), + ); +}); + +final onlineShelfProvider = ChangeNotifierProvider((ref) { + final store = OnlineShelfStore(); + store.load(); + return store; +}); + +final unifiedSearchProvider = Provider((ref) { + return UnifiedSearch( + api: ref.watch(apiClientProvider), + sourceManager: ref.watch(sourceManagerProvider), + engine: ref.watch(legadoEngineProvider), + scheduler: ref.watch(searchSchedulerProvider), + ); +}); diff --git a/sakura_mobile/lib/src/core/push_notifications.dart b/sakura_mobile/lib/src/core/push_notifications.dart new file mode 100644 index 0000000..2993385 --- /dev/null +++ b/sakura_mobile/lib/src/core/push_notifications.dart @@ -0,0 +1,134 @@ +import 'dart:async'; +import 'dart:math'; + +import 'package:firebase_core/firebase_core.dart'; +import 'package:firebase_messaging/firebase_messaging.dart'; +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import '../pages/reminder_center_page.dart'; +import 'api_client.dart'; + +class PushNotifications { + PushNotifications._(); + + static final GlobalKey navigatorKey = + GlobalKey(); + + static const _deviceIdKey = 'sakura.push.deviceId'; + static StreamSubscription? _tokenRefreshSubscription; + static bool _initialized = false; + static bool _available = false; + + static Future initialize() async { + if (_initialized) { + return; + } + try { + await Firebase.initializeApp(); + _available = true; + _initialized = true; + await FirebaseMessaging.instance.requestPermission(); + FirebaseMessaging.onMessageOpenedApp.listen(_openReminderCenter); + final initialMessage = + await FirebaseMessaging.instance.getInitialMessage(); + if (initialMessage != null) { + _openReminderCenter(initialMessage); + } + } catch (_) { + _available = false; + } + } + + static Future registerWithSession({ + required String baseUrl, + required String accessToken, + }) async { + if (accessToken.isEmpty) { + return; + } + await initialize(); + if (!_available) { + return; + } + + final token = await FirebaseMessaging.instance.getToken(); + if (token == null || token.isEmpty) { + return; + } + final deviceId = await _deviceId(); + await SakuraApiClient(baseUrl: baseUrl, token: accessToken) + .registerPushToken( + deviceId: deviceId, + platform: 'android', + fcmToken: token, + ); + + await _tokenRefreshSubscription?.cancel(); + _tokenRefreshSubscription = + FirebaseMessaging.instance.onTokenRefresh.listen((newToken) async { + if (newToken.isEmpty) { + return; + } + try { + final prefs = await SharedPreferences.getInstance(); + final latestBaseUrl = prefs.getString('sakura.baseUrl') ?? baseUrl; + final latestAccessToken = + prefs.getString('sakura.accessToken') ?? accessToken; + if (latestAccessToken.isEmpty) { + return; + } + await SakuraApiClient(baseUrl: latestBaseUrl, token: latestAccessToken) + .registerPushToken( + deviceId: deviceId, + platform: 'android', + fcmToken: newToken, + ); + } catch (_) { + // Token refresh can happen while the network is unavailable; the next + // app start or login will register again. + } + }); + } + + static Future unregisterWithSession({ + required String baseUrl, + required String accessToken, + }) async { + await _tokenRefreshSubscription?.cancel(); + _tokenRefreshSubscription = null; + if (accessToken.isEmpty) { + return; + } + final deviceId = await _deviceId(); + try { + await SakuraApiClient(baseUrl: baseUrl, token: accessToken) + .unregisterPushToken(deviceId: deviceId); + } catch (_) { + // Logout should not be blocked by a transient network failure. + } + } + + static void _openReminderCenter(RemoteMessage message) { + final navigator = navigatorKey.currentState; + if (navigator == null) { + return; + } + navigator.push( + MaterialPageRoute(builder: (_) => const ReminderCenterPage()), + ); + } + + static Future _deviceId() async { + final prefs = await SharedPreferences.getInstance(); + final existing = prefs.getString(_deviceIdKey); + if (existing != null && existing.isNotEmpty) { + return existing; + } + final random = Random.secure().nextInt(1 << 32).toRadixString(16); + final generated = + 'android-${DateTime.now().microsecondsSinceEpoch.toRadixString(16)}-$random'; + await prefs.setString(_deviceIdKey, generated); + return generated; + } +} diff --git a/sakura_mobile/lib/src/core/search_scheduler.dart b/sakura_mobile/lib/src/core/search_scheduler.dart new file mode 100644 index 0000000..596771c --- /dev/null +++ b/sakura_mobile/lib/src/core/search_scheduler.dart @@ -0,0 +1,185 @@ +import 'dart:async'; + +import 'legado_engine.dart'; +import 'legado_models.dart'; +import 'source_health.dart'; + +class OnlineSearchPhase { + const OnlineSearchPhase({ + required this.name, + this.items = const [], + this.doneCount = 0, + this.totalCount = 0, + this.message, + }); + + final String name; + final List items; + final int doneCount; + final int totalCount; + final String? message; +} + +class SearchScheduler { + SearchScheduler(this._engine, this._health); + + static const fastTimeout = Duration(seconds: 5); + static const normalTimeout = Duration(seconds: 8); + static const maxConcurrent = 6; + + final LegadoEngine _engine; + final SourceHealthStore _health; + int _searchToken = 0; + + void cancel() => _searchToken++; + + Stream search( + String keyword, + List allSources, + ) async* { + await _health.load(); + final token = ++_searchToken; + final usable = allSources.toList() + ..sort((a, b) => _health.score(b.id).compareTo(_health.score(a.id))); + if (usable.isEmpty) { + yield const OnlineSearchPhase( + name: 'error', + message: '没有可用的在线书源,请到书源管理中检查。', + ); + return; + } + + final fast = + usable.where((source) => _health.score(source.id) >= 70).toList(); + final normal = usable.where((source) { + final score = _health.score(source.id); + return score >= 40 && score < 70; + }).toList(); + final slow = + usable.where((source) => _health.score(source.id) < 40).toList(); + final results = []; + final seen = {}; + var doneCount = 0; + + yield OnlineSearchPhase( + name: 'searching', + totalCount: usable.length, + message: '正在搜索 ${usable.length} 个源…', + ); + + for (final stage in <(List, Duration, String)>[ + (fast, fastTimeout, 'fast'), + (normal, normalTimeout, 'normal'), + (slow, normalTimeout, 'slow'), + ]) { + if (stage.$1.isEmpty) continue; + if (stage.$3 == 'slow') { + yield OnlineSearchPhase( + name: 'pending_slow', + items: List.unmodifiable(results), + doneCount: doneCount, + totalCount: usable.length, + message: '${stage.$1.length} 个慢源正在后台搜索…', + ); + } + await for (final completed in _pool( + stage.$1, + keyword, + timeout: stage.$2, + token: token, + )) { + if (token != _searchToken) return; + doneCount++; + for (final item in completed.items) { + final key = '${item.sourceId}|${item.detailUrl ?? item.title}'; + if (seen.add(key)) results.add(item); + } + yield OnlineSearchPhase( + name: stage.$3 == 'slow' ? 'slow_append' : 'partial', + items: List.unmodifiable(results), + doneCount: doneCount, + totalCount: usable.length, + ); + } + } + + if (token != _searchToken) return; + yield OnlineSearchPhase( + name: 'complete', + items: List.unmodifiable(results), + doneCount: doneCount, + totalCount: usable.length, + ); + unawaited(_health.save()); + } + + Stream<_CompletedSource> _pool( + List sources, + String keyword, { + required Duration timeout, + required int token, + }) async* { + final pending = sources.toList(); + final running = >{}; + var slotId = 0; + while (pending.isNotEmpty || running.isNotEmpty) { + while (running.length < maxConcurrent && pending.isNotEmpty) { + final source = pending.removeAt(0); + final id = slotId++; + running[id] = _searchOne(source, keyword, timeout, token: token) + .then((value) => _CompletedSlot(id, value)); + } + if (running.isEmpty) break; + final completed = await Future.any(running.values); + running.remove(completed.slotId); + if (completed.result != null) yield completed.result!; + } + } + + Future<_CompletedSource?> _searchOne( + LegadoSource source, + String keyword, + Duration timeout, { + required int token, + }) async { + if (token != _searchToken) return null; + final stopwatch = Stopwatch()..start(); + try { + final items = await _engine.search(source, keyword).timeout(timeout); + if (token != _searchToken) return null; + stopwatch.stop(); + _health.recordSuccess(source.id, stopwatch.elapsedMilliseconds); + return _CompletedSource(source.id, items); + } on TimeoutException { + _health.recordFailure(source.id, timeout: true); + return _CompletedSource(source.id, [ + UnifiedSearchResult.error( + sourceId: source.id, + sourceName: source.name, + errorMessage: '连接超时,已降低该书源的搜索优先级。', + ), + ]); + } catch (error) { + _health.recordFailure(source.id); + return _CompletedSource(source.id, [ + UnifiedSearchResult.error( + sourceId: source.id, + sourceName: source.name, + errorMessage: error.toString(), + ), + ]); + } + } +} + +class _CompletedSource { + const _CompletedSource(this.sourceId, this.items); + final String sourceId; + final List items; +} + +class _CompletedSlot { + const _CompletedSlot(this.slotId, this.result); + final int slotId; + final _CompletedSource? result; +} diff --git a/sakura_mobile/lib/src/core/session.dart b/sakura_mobile/lib/src/core/session.dart new file mode 100644 index 0000000..baa2816 --- /dev/null +++ b/sakura_mobile/lib/src/core/session.dart @@ -0,0 +1,304 @@ +import 'package:dio/dio.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'api_client.dart'; +import 'push_notifications.dart'; + +class SessionState { + const SessionState({ + required this.baseUrl, + required this.accessToken, + this.refreshToken = '', + this.username = '', + this.loading = false, + this.error, + }); + + final String baseUrl; + final String accessToken; + final String refreshToken; + final String username; + final bool loading; + final String? error; + + bool get signedIn => accessToken.isNotEmpty; + + SessionState copyWith({ + String? baseUrl, + String? accessToken, + String? refreshToken, + String? username, + bool? loading, + String? error, + }) { + return SessionState( + baseUrl: baseUrl ?? this.baseUrl, + accessToken: accessToken ?? this.accessToken, + refreshToken: refreshToken ?? this.refreshToken, + username: username ?? this.username, + loading: loading ?? this.loading, + error: error, + ); + } +} + +class SessionController extends StateNotifier { + SessionController() + : super(const SessionState( + baseUrl: defaultCloudApi, accessToken: '', loading: true)) { + _load(); + } + + static const _baseUrlKey = 'sakura.baseUrl'; + static const _accessTokenKey = 'sakura.accessToken'; + static const _refreshTokenKey = 'sakura.refreshToken'; + static const _usernameKey = 'sakura.username'; + static const _oldDefaultCloudApi = 'http://api.kunkunxiaomao.top:8000'; + + Future _load() async { + final prefs = await SharedPreferences.getInstance(); + final savedBaseUrl = prefs.getString(_baseUrlKey); + final baseUrl = normalizeCloudBaseUrl( + savedBaseUrl == _oldDefaultCloudApi ? defaultCloudApi : savedBaseUrl ?? defaultCloudApi, + ); + await prefs.setString(_baseUrlKey, baseUrl); + final accessToken = prefs.getString(_accessTokenKey) ?? ''; + final refreshToken = prefs.getString(_refreshTokenKey) ?? ''; + final validated = await _validateStoredSession( + baseUrl: baseUrl, + accessToken: accessToken, + refreshToken: refreshToken, + ); + state = SessionState( + baseUrl: baseUrl, + accessToken: validated?.accessToken ?? '', + refreshToken: validated?.refreshToken ?? '', + username: validated?.username ?? '', + ); + if (state.signedIn) { + await PushNotifications.registerWithSession( + baseUrl: state.baseUrl, + accessToken: state.accessToken, + ); + } + } + + SakuraApiClient client() => SakuraApiClient( + baseUrl: state.baseUrl, + token: state.accessToken, + refreshToken: state.refreshToken, + onTokensRefreshed: _storeRefreshedTokens, + ); + + Future saveServer(String baseUrl) async { + final normalized = normalizeCloudBaseUrl(baseUrl); + final prefs = await SharedPreferences.getInstance(); + await prefs.setString(_baseUrlKey, normalized); + state = state.copyWith(baseUrl: normalized, error: null); + } + + Future login(String usernameOrEmail, String password) async { + state = state.copyWith(loading: true, error: null); + try { + final api = SakuraApiClient(baseUrl: state.baseUrl); + final tokens = await api.login(usernameOrEmail, password); + final accessToken = tokens['access_token']?.toString() ?? ''; + final refreshToken = tokens['refresh_token']?.toString() ?? ''; + final prefs = await SharedPreferences.getInstance(); + await _clearAuthPrefs(prefs); + final authedApi = + SakuraApiClient(baseUrl: state.baseUrl, token: accessToken); + final me = await authedApi.me(); + final username = me['username']?.toString() ?? ''; + if (username.isEmpty) { + throw Exception('登录成功但无法获取用户身份信息。'); + } + await prefs.setString(_accessTokenKey, accessToken); + await prefs.setString(_refreshTokenKey, refreshToken); + await prefs.setString(_usernameKey, username); + state = state.copyWith( + accessToken: accessToken, + refreshToken: refreshToken, + username: username, + loading: false, + error: null, + ); + await PushNotifications.registerWithSession( + baseUrl: state.baseUrl, + accessToken: accessToken, + ); + } catch (error) { + state = state.copyWith(loading: false, error: _friendlyError(error)); + } + } + + Future register(String username, String email, String password) async { + state = state.copyWith(loading: true, error: null); + try { + final api = SakuraApiClient(baseUrl: state.baseUrl); + await api.register(username, email, password); + state = state.copyWith(loading: false, error: null); + } catch (error) { + state = state.copyWith(loading: false, error: _friendlyError(error)); + rethrow; + } + } + + Future logout() async { + // 不调用 /api/v1/auth/logout,避免手机退出时吊销 exe 端 token + try { + await PushNotifications.unregisterWithSession( + baseUrl: state.baseUrl, + accessToken: state.accessToken, + ); + } catch (_) { + // 推送注销失败不影响本地退出 + } + + final prefs = await SharedPreferences.getInstance(); + await _clearAuthPrefs(prefs); + + state = state.copyWith( + accessToken: '', + refreshToken: '', + username: '', + error: null, + ); + } + + Future<_ValidatedSession?> _validateStoredSession({ + required String baseUrl, + required String accessToken, + required String refreshToken, + }) async { + if (accessToken.isEmpty) { + return null; + } + final prefs = await SharedPreferences.getInstance(); + try { + var currentAccessToken = accessToken; + var currentRefreshToken = refreshToken; + var api = SakuraApiClient(baseUrl: baseUrl, token: currentAccessToken); + Map me; + try { + me = await api.me(); + } on DioException catch (error) { + if (error.response?.statusCode != 401 || currentRefreshToken.isEmpty) { + rethrow; + } + final tokens = await api.refresh(currentRefreshToken); + currentAccessToken = tokens['access_token']?.toString() ?? ''; + currentRefreshToken = tokens['refresh_token']?.toString() ?? ''; + if (currentAccessToken.isEmpty || currentRefreshToken.isEmpty) { + throw StateError('Refresh response did not contain a token pair'); + } + api = SakuraApiClient(baseUrl: baseUrl, token: currentAccessToken); + me = await api.me(); + await prefs.setString(_accessTokenKey, currentAccessToken); + await prefs.setString(_refreshTokenKey, currentRefreshToken); + } + + final savedUsername = prefs.getString(_usernameKey) ?? ''; + final actualUsername = me['username']?.toString() ?? ''; + + if (actualUsername.isEmpty) { + return _ValidatedSession( + accessToken: accessToken, + refreshToken: refreshToken, + username: savedUsername, + ); + } + + // 如果刷新后返回了 sakura/其他账号,不覆盖本地账号 + if (savedUsername.isNotEmpty && actualUsername != savedUsername) { + return _ValidatedSession( + accessToken: accessToken, + refreshToken: refreshToken, + username: savedUsername, + ); + } + + await prefs.setString(_usernameKey, actualUsername); + return _ValidatedSession( + accessToken: currentAccessToken, + refreshToken: currentRefreshToken, + username: actualUsername, + ); + } catch (_) { + await _clearAuthPrefs(prefs); + return null; + } + } + + Future _clearAuthPrefs(SharedPreferences prefs) async { + await prefs.remove(_accessTokenKey); + await prefs.remove(_refreshTokenKey); + await prefs.remove(_usernameKey); + } + + Future _storeRefreshedTokens( + String accessToken, + String refreshToken, + ) async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setString(_accessTokenKey, accessToken); + await prefs.setString(_refreshTokenKey, refreshToken); + state = state.copyWith( + accessToken: accessToken, + refreshToken: refreshToken, + error: null, + ); + } + + void clearError() { + state = state.copyWith(error: null); + } + + String _friendlyError(Object error) { + if (error is DioException) { + final data = error.response?.data; + if (data is Map) { + final detail = data['detail']?.toString(); + final message = data['message']?.toString(); + final text = (detail?.isNotEmpty ?? false) + ? detail! + : (message?.isNotEmpty ?? false) + ? message! + : ''; + if (text.isNotEmpty) { + final normalized = text.toLowerCase(); + if (normalized.contains('internal server error')) { + return '服务器暂时开小差了,请稍后再试。'; + } + return text; + } + } + if (error.type == DioExceptionType.connectionError || + error.type == DioExceptionType.connectionTimeout || + error.type == DioExceptionType.receiveTimeout) { + return '无法连接服务器,请检查网络状态。'; + } + final statusCode = error.response?.statusCode; + if (statusCode == 500) { + return '服务器暂时开小差了,请稍后再试。'; + } + if (statusCode != null) { + return '请求失败(HTTP $statusCode),请稍后重试。'; + } + } + return '操作失败,请稍后重试。'; + } +} + +class _ValidatedSession { + const _ValidatedSession({ + required this.accessToken, + required this.refreshToken, + required this.username, + }); + + final String accessToken; + final String refreshToken; + final String username; +} diff --git a/sakura_mobile/lib/src/core/source_health.dart b/sakura_mobile/lib/src/core/source_health.dart new file mode 100644 index 0000000..835ef45 --- /dev/null +++ b/sakura_mobile/lib/src/core/source_health.dart @@ -0,0 +1,141 @@ +import 'dart:async'; +import 'dart:convert'; + +import 'package:shared_preferences/shared_preferences.dart'; + +class SourceHealth { + SourceHealth({required this.sourceId}); + + final String sourceId; + int successCount = 0; + int failCount = 0; + int timeoutCount = 0; + int totalLatencyMs = 0; + DateTime? lastSuccessAt; + DateTime? lastFailAt; + int consecutiveFails = 0; + + bool get isUsable { + if (consecutiveFails >= 5) { + final last = lastFailAt; + return last != null && DateTime.now().difference(last).inHours >= 1; + } + return !(failCount >= 5 && successCount == 0); + } + + double get score { + final latest = lastSuccessAt ?? lastFailAt; + final decay = + latest != null && DateTime.now().difference(latest).inDays >= 7 + ? 0.5 + : 1.0; + final total = successCount + failCount + timeoutCount; + if (total == 0) return 50 * decay; + var value = 60.0 * successCount / total; + if (successCount > 0 && totalLatencyMs > 0) { + final average = totalLatencyMs / successCount; + if (average < 500) { + value += 25; + } else if (average < 2000) { + value += 25 * (1 - (average - 500) / 1500); + } + } + if (lastSuccessAt != null) { + final hours = DateTime.now().difference(lastSuccessAt!).inHours; + if (hours < 1) { + value += 15; + } else if (hours < 24) { + value += 15 * (1 - hours / 24); + } + } + return (value * decay).clamp(0, 100); + } + + void recordSuccess(int latencyMs) { + successCount++; + totalLatencyMs += latencyMs; + lastSuccessAt = DateTime.now(); + consecutiveFails = 0; + } + + void recordFailure({bool timeout = false}) { + timeout ? timeoutCount++ : failCount++; + lastFailAt = DateTime.now(); + consecutiveFails++; + } + + Map toJson() => { + 'sourceId': sourceId, + 'successCount': successCount, + 'failCount': failCount, + 'timeoutCount': timeoutCount, + 'totalLatencyMs': totalLatencyMs, + 'lastSuccessAt': lastSuccessAt?.toIso8601String(), + 'lastFailAt': lastFailAt?.toIso8601String(), + 'consecutiveFails': consecutiveFails, + }; + + factory SourceHealth.fromJson(Map json) { + return SourceHealth(sourceId: json['sourceId']?.toString() ?? '') + ..successCount = (json['successCount'] as num?)?.toInt() ?? 0 + ..failCount = (json['failCount'] as num?)?.toInt() ?? 0 + ..timeoutCount = (json['timeoutCount'] as num?)?.toInt() ?? 0 + ..totalLatencyMs = (json['totalLatencyMs'] as num?)?.toInt() ?? 0 + ..lastSuccessAt = + DateTime.tryParse(json['lastSuccessAt']?.toString() ?? '') + ..lastFailAt = DateTime.tryParse(json['lastFailAt']?.toString() ?? '') + ..consecutiveFails = (json['consecutiveFails'] as num?)?.toInt() ?? 0; + } +} + +class SourceHealthStore { + static const _storageKey = 'source_health_v1'; + final Map _data = {}; + Timer? _saveTimer; + bool _loaded = false; + + Future load() async { + if (_loaded) return; + final prefs = await SharedPreferences.getInstance(); + for (final raw in prefs.getStringList(_storageKey) ?? const []) { + try { + final health = SourceHealth.fromJson( + Map.from(jsonDecode(raw) as Map), + ); + if (health.sourceId.isNotEmpty) _data[health.sourceId] = health; + } catch (_) { + // One corrupt record must not discard health data for other sources. + } + } + _loaded = true; + } + + SourceHealth health(String id) => + _data.putIfAbsent(id, () => SourceHealth(sourceId: id)); + bool isUsable(String id) => health(id).isUsable; + double score(String id) => health(id).score; + void recordSuccess(String id, int latencyMs) => + health(id).recordSuccess(latencyMs); + void recordFailure(String id, {bool timeout = false}) => + health(id).recordFailure(timeout: timeout); + + void resetAll() { + for (final item in _data.values) { + item.consecutiveFails = 0; + } + } + + Future save() async { + _saveTimer?.cancel(); + _saveTimer = Timer(const Duration(seconds: 2), flush); + } + + Future flush() async { + _saveTimer?.cancel(); + final prefs = await SharedPreferences.getInstance(); + await prefs.setStringList( + _storageKey, + _data.values.map((item) => jsonEncode(item.toJson())).toList(), + ); + } +} diff --git a/sakura_mobile/lib/src/core/source_manager.dart b/sakura_mobile/lib/src/core/source_manager.dart new file mode 100644 index 0000000..412435c --- /dev/null +++ b/sakura_mobile/lib/src/core/source_manager.dart @@ -0,0 +1,434 @@ +import 'dart:async'; +import 'dart:convert'; + +import 'package:dio/dio.dart'; +import 'package:flutter/foundation.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'legado_engine.dart'; +import 'legado_models.dart'; + +class SourceManager extends ChangeNotifier { + SourceManager({LegadoEngine? engine, Dio? http}) + : _engine = engine ?? LegadoEngine(), + _http = http ?? + Dio( + BaseOptions( + connectTimeout: LegadoEngine.perSourceTimeout, + receiveTimeout: LegadoEngine.perSourceTimeout, + ), + ); + + static const _sourcesKey = 'legado_sources_v1'; + static const _initializedKey = 'source_manager_initialized_v1'; + static const _subscriptionsKey = 'legado_subscription_urls_v1'; + static const _lastSubscriptionUpdateKey = + 'legado_last_subscription_update_v1'; + + // P0: do not ship third-party repository URLs or placeholder sources. + static const List builtinRepos = []; + + final LegadoEngine _engine; + final Dio _http; + final List _sources = []; + final List _lastChecks = []; + Timer? _autoUpdateTimer; + bool _loaded = false; + bool _busy = false; + String? _message; + DateTime? _lastSubscriptionUpdate; + + List get sources => List.unmodifiable(_sources); + List get enabledSources => + _sources.where((source) => source.enabled).toList(growable: false); + List get enabledNovelSources => + enabledSources.where((source) => source.isNovel).toList(growable: false); + List get enabledMangaSources => + enabledSources.where((source) => source.isManga).toList(growable: false); + bool get loaded => _loaded; + bool get busy => _busy; + String? get message => _message; + DateTime? get lastSubscriptionUpdate => _lastSubscriptionUpdate; + List get lastChecks => List.unmodifiable(_lastChecks); + + LegadoSource? sourceById(String sourceId) { + for (final source in _sources) { + if (source.id == sourceId) return source; + } + return null; + } + + SourceCompatInfo compatibilityFor(LegadoSource source) { + final rules = [ + source.searchUrl, + source.searchListRule, + source.searchNameRule, + source.searchAuthorRule, + source.searchCoverRule, + source.searchDetailRule, + source.bookInfoNameRule, + source.bookInfoAuthorRule, + source.bookInfoCoverRule, + source.bookInfoIntroRule, + source.bookInfoTocRule, + source.tocUrl, + source.tocListRule, + source.tocNameRule, + source.tocUrlRule, + source.tocNextPageRule, + source.contentRule, + source.contentNextRule, + source.contentPrevRule, + ].whereType().toList(growable: false); + final allRules = rules.join('\n').toLowerCase(); + final hasJs = allRules.contains('@js') || allRules.contains(''); + final hasPutGet = allRules.contains('@put') || allRules.contains('@get'); + final searchOk = source.searchUrl?.trim().isNotEmpty == true && + source.searchListRule?.trim().isNotEmpty == true; + final bookInfoOk = source.bookInfoNameRule?.trim().isNotEmpty == true || + source.bookInfoTocRule?.trim().isNotEmpty == true; + final tocOk = source.tocListRule?.trim().isNotEmpty == true && + source.tocUrlRule?.trim().isNotEmpty == true; + final contentOk = source.contentRule?.trim().isNotEmpty == true; + final warnings = [ + if (!searchOk) '搜索规则不完整', + if (!bookInfoOk) '详情规则不完整', + if (!tocOk) '目录规则不完整', + if (!contentOk) '正文规则不完整', + if (allRules.contains('java.ajax')) 'JS 规则请求外部网络,安全沙箱会拒绝', + if (allRules.contains('org.jsoup')) 'JS 规则依赖 Legado Java DOM 桥接', + ]; + final method = source.searchMethod?.trim().toUpperCase(); + late final SourceCompatLevel level; + if (!searchOk && !contentOk) { + level = SourceCompatLevel.broken; + } else if (hasJs) { + level = SourceCompatLevel.needsJs; + } else if (hasPutGet || !bookInfoOk || !tocOk || !contentOk) { + level = SourceCompatLevel.partial; + } else if (method == 'POST') { + level = SourceCompatLevel.needsPost; + } else if (source.headers.isNotEmpty) { + level = SourceCompatLevel.needsHeaders; + } else { + level = SourceCompatLevel.full; + } + return SourceCompatInfo( + level: level, + searchOk: searchOk, + bookInfoOk: bookInfoOk, + tocOk: tocOk, + contentOk: contentOk, + hasJsRule: hasJs, + hasPutGet: hasPutGet, + warnings: warnings, + ); + } + + Future load() async { + if (_loaded) return; + final prefs = await SharedPreferences.getInstance(); + _lastSubscriptionUpdate = DateTime.tryParse( + prefs.getString(_lastSubscriptionUpdateKey) ?? '', + ); + final raw = prefs.getString(_sourcesKey); + if (raw != null && raw.isNotEmpty) { + final decoded = jsonDecode(raw); + if (decoded is List) { + _sources + ..clear() + ..addAll( + decoded.whereType().map( + (item) => LegadoSource.fromJson( + Map.from(item), + ), + ), + ); + } + } + _loaded = true; + notifyListeners(); + } + + Future initializeIfFirstRun() async { + await load(); + final prefs = await SharedPreferences.getInstance(); + if (prefs.getBool(_initializedKey) != true) { + await prefs.setBool(_initializedKey, true); + _message = '当前没有可用书源,请从你信任的仓库导入。'; + notifyListeners(); + } + startAutoUpdateTimer(); + await checkSubscriptionUpdatesIfDue(); + } + + void startAutoUpdateTimer() { + _autoUpdateTimer ??= Timer.periodic( + const Duration(hours: 6), + (_) => checkSubscriptionUpdatesIfDue(), + ); + } + + Future importFromRepo(SourceRepo repo) async { + if (repo.url.trim().isEmpty) { + _message = '请手动输入你信任的书源仓库 URL。'; + notifyListeners(); + return 0; + } + _setBusy('正在导入 ${repo.name}...'); + try { + if (repo.isSubscription) { + await _addSubscription(repo.url); + } + final response = await _http + .get( + repo.url, + options: Options(responseType: ResponseType.plain), + ) + .timeout(LegadoEngine.perSourceTimeout); + return await importFromJsonText(response.data ?? ''); + } catch (error) { + _message = '导入失败:${_shortError(error)}'; + notifyListeners(); + return 0; + } finally { + _setIdle(); + } + } + + Future importFromUrl(String url) async { + final normalized = url.trim(); + if (normalized.isEmpty) { + _message = '请输入书源仓库 URL。'; + notifyListeners(); + return 0; + } + _setBusy('正在导入自定义书源...'); + try { + final response = await _http + .get( + normalized, + options: Options(responseType: ResponseType.plain), + ) + .timeout(LegadoEngine.perSourceTimeout); + return await importFromJsonText(response.data ?? ''); + } catch (error) { + _message = '导入失败:${_shortError(error)}'; + notifyListeners(); + return 0; + } finally { + _setIdle(); + } + } + + Future checkSubscriptionUpdatesIfDue({bool force = false}) async { + await load(); + final prefs = await SharedPreferences.getInstance(); + final urls = prefs.getStringList(_subscriptionsKey) ?? const []; + if (urls.isEmpty) return 0; + + final last = _lastSubscriptionUpdate; + if (!force && + last != null && + DateTime.now().difference(last) < const Duration(hours: 24)) { + return 0; + } + + _setBusy('正在检查订阅书源...'); + var added = 0; + try { + for (final url in urls) { + try { + final response = await _http + .get( + url, + options: Options(responseType: ResponseType.plain), + ) + .timeout(LegadoEngine.perSourceTimeout); + added += await importFromJsonText(response.data ?? ''); + } catch (error) { + _message = '部分订阅书源更新失败:${_shortError(error)}'; + } + } + _lastSubscriptionUpdate = DateTime.now(); + await prefs.setString( + _lastSubscriptionUpdateKey, + _lastSubscriptionUpdate!.toIso8601String(), + ); + _message = '订阅更新完成,新增 $added 个书源。'; + notifyListeners(); + return added; + } finally { + _setIdle(); + } + } + + Future> checkEnabledSources() async { + await load(); + _setBusy('正在检测已启用的书源...'); + final targets = enabledSources.take(LegadoEngine.maxConcurrency).toList(); + try { + final results = await Future.wait(targets.map(_checkSource)); + _lastChecks + ..clear() + ..addAll(results); + final healthy = results.where((item) => item.ok).length; + _message = '已检测 ${results.length} 个书源,$healthy 个可用。'; + notifyListeners(); + return List.unmodifiable(_lastChecks); + } finally { + _setIdle(); + } + } + + Future importFromJsonText(String body) async { + await load(); + final imported = _engine.parseSources(body); + if (imported.isEmpty) { + _message = '没有识别到书源,请确认链接返回的是书源 JSON。'; + notifyListeners(); + return 0; + } + var added = 0; + for (final source in imported) { + final existing = _sources.indexWhere((item) => item.id == source.id); + final next = source.copyWith( + enabled: existing >= 0 ? _sources[existing].enabled : source.enabled, + ); + if (existing >= 0) { + _sources[existing] = next; + } else { + _sources.add(next); + added += 1; + } + } + await _save(); + _message = '已导入 $added 个新书源。'; + notifyListeners(); + return added; + } + + Future setEnabled(String sourceId, bool enabled) async { + await load(); + final index = _sources.indexWhere((source) => source.id == sourceId); + if (index < 0) return; + _sources[index] = _sources[index].copyWith(enabled: enabled); + await _save(); + notifyListeners(); + } + + Future _save() async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setString( + _sourcesKey, + jsonEncode(_sources.map((source) => source.toJson()).toList()), + ); + } + + Future _addSubscription(String url) async { + final prefs = await SharedPreferences.getInstance(); + final urls = (prefs.getStringList(_subscriptionsKey) ?? []).toSet() + ..add(url); + await prefs.setStringList(_subscriptionsKey, urls.toList()); + } + + Future _checkSource(LegadoSource source) async { + final stopwatch = Stopwatch()..start(); + try { + final url = _healthCheckUrl(source); + final response = await _http + .get( + url, + options: Options( + responseType: ResponseType.plain, + headers: { + ...source.headers, + if (source.userAgent?.isNotEmpty == true) + 'User-Agent': source.userAgent!, + }, + ), + ) + .timeout(LegadoEngine.perSourceTimeout); + stopwatch.stop(); + final status = response.statusCode ?? 0; + return SourceCheckResult( + sourceId: source.id, + sourceName: source.name, + ok: status >= 200 && status < 400, + elapsedMs: stopwatch.elapsedMilliseconds, + message: 'HTTP $status', + ); + } catch (error) { + stopwatch.stop(); + return SourceCheckResult( + sourceId: source.id, + sourceName: source.name, + ok: false, + elapsedMs: stopwatch.elapsedMilliseconds, + message: _shortError(error), + ); + } + } + + String _healthCheckUrl(LegadoSource source) { + return (source.searchUrl ?? source.url) + .replaceAll('{{key}}', Uri.encodeComponent('test')) + .replaceAll('{{page}}', '1'); + } + + String _shortError(Object error) { + if (error is FormatException && error.message == 'source-html') { + return '这个链接返回的是网页,不是书源 JSON。请复制书源仓库的 raw/json 地址。'; + } + if (error is DioException) { + final host = error.requestOptions.uri.host; + switch (error.type) { + case DioExceptionType.connectionTimeout: + case DioExceptionType.receiveTimeout: + case DioExceptionType.sendTimeout: + return '连接超时:${host.isEmpty ? '目标站点' : host} 暂时没有响应。'; + case DioExceptionType.connectionError: + return '连接失败:${host.isEmpty ? '目标站点' : host} 断开了连接,请稍后重试或更换 raw/json 链接。'; + case DioExceptionType.badResponse: + return '服务器返回 HTTP ${error.response?.statusCode ?? '-'},请确认链接可公开访问。'; + default: + break; + } + } + final message = error.toString(); + return message.length > 120 ? '${message.substring(0, 120)}...' : message; + } + + void _setBusy(String message) { + _busy = true; + _message = message; + notifyListeners(); + } + + void _setIdle() { + _busy = false; + notifyListeners(); + } + + @override + void dispose() { + _autoUpdateTimer?.cancel(); + super.dispose(); + } +} + +class SourceCheckResult { + const SourceCheckResult({ + required this.sourceId, + required this.sourceName, + required this.ok, + required this.elapsedMs, + required this.message, + }); + + final String sourceId; + final String sourceName; + final bool ok; + final int elapsedMs; + final String message; +} diff --git a/sakura_mobile/lib/src/core/unified_search.dart b/sakura_mobile/lib/src/core/unified_search.dart new file mode 100644 index 0000000..d443c7a --- /dev/null +++ b/sakura_mobile/lib/src/core/unified_search.dart @@ -0,0 +1,239 @@ +import 'dart:async'; + +import '../models/artwork.dart'; +import '../models/novel.dart'; +import 'api_client.dart'; +import 'legado_engine.dart'; +import 'legado_models.dart'; +import 'source_manager.dart'; +import 'search_scheduler.dart'; +import 'source_health.dart'; + +enum UnifiedSearchMode { manga, novel } + +class UnifiedSearch { + UnifiedSearch({ + required SakuraApiClient api, + required SourceManager sourceManager, + required LegadoEngine engine, + SearchScheduler? scheduler, + }) : _api = api, + _sourceManager = sourceManager, + _scheduler = scheduler ?? SearchScheduler(engine, SourceHealthStore()); + + final SakuraApiClient _api; + final SourceManager _sourceManager; + final SearchScheduler _scheduler; + + Future> search({ + required String query, + required UnifiedSearchMode mode, + }) async { + final normalized = query.trim(); + if (normalized.isEmpty) { + return const []; + } + await _sourceManager.initializeIfFirstRun(); + + var result = const []; + await for (final snapshot in searchStream(query: query, mode: mode)) { + result = snapshot.sections; + } + return result; + } + + Stream searchStream({ + required String query, + required UnifiedSearchMode mode, + }) { + final normalized = query.trim(); + if (normalized.isEmpty) return const Stream.empty(); + late final StreamController controller; + var cloudItems = []; + var onlineItems = []; + var cloudDone = false; + var onlineDone = false; + var doneCount = 0; + var totalCount = 0; + String? message; + + List sections() => [ + if (cloudItems.isNotEmpty) + SearchSection(id: 'cloud', title: '我的云库', items: cloudItems), + if (onlineItems.isNotEmpty) + SearchSection(id: 'online', title: '在线源', items: onlineItems), + ]; + + void emit() { + if (controller.isClosed) return; + controller.add(UnifiedSearchSnapshot( + sections: sections(), + message: message, + doneCount: doneCount, + totalCount: totalCount, + isComplete: cloudDone && onlineDone, + )); + } + + Future finishIfDone() async { + if (cloudDone && onlineDone && !controller.isClosed) { + emit(); + await controller.close(); + } + } + + controller = StreamController( + onListen: () { + unawaited(() async { + await _sourceManager.initializeIfFirstRun(); + try { + cloudItems = await (mode == UnifiedSearchMode.manga + ? _searchCloudManga(normalized) + : _searchCloudNovels(normalized)) + .timeout(const Duration(seconds: 6)); + } catch (_) { + cloudItems = []; + } finally { + cloudDone = true; + emit(); + await finishIfDone(); + } + }()); + unawaited(() async { + final sources = mode == UnifiedSearchMode.manga + ? _sourceManager.enabledMangaSources + : _sourceManager.enabledNovelSources; + await for (final phase in _scheduler.search(normalized, sources)) { + onlineItems = phase.items.map(SearchItem.online).toList(); + doneCount = phase.doneCount; + totalCount = phase.totalCount; + message = phase.message; + if (phase.name == 'complete' || phase.name == 'error') { + onlineDone = true; + } + emit(); + } + onlineDone = true; + await finishIfDone(); + }()); + }, + onCancel: _scheduler.cancel, + ); + return controller.stream; + } + + Future> _searchCloudManga(String query) async { + final rows = await _api.gallery(search: query, contentType: 'manga'); + return rows + .map((item) => Artwork.fromJson(item as Map)) + .map(SearchItem.cloudArtwork) + .toList(growable: false); + } + + Future> _searchCloudNovels(String query) async { + final rows = await _api.novels(search: query); + return rows + .map((item) => Novel.fromJson(item as Map)) + .map(SearchItem.cloudNovel) + .toList(growable: false); + } +} + +class UnifiedSearchSnapshot { + const UnifiedSearchSnapshot({ + this.sections = const [], + this.message, + this.doneCount = 0, + this.totalCount = 0, + this.isComplete = false, + }); + + final List sections; + final String? message; + final int doneCount; + final int totalCount; + final bool isComplete; +} + +class SearchSection { + const SearchSection({ + required this.id, + required this.title, + required this.items, + }); + + final String id; + final String title; + final List items; +} + +class SearchItem { + const SearchItem._({ + required this.title, + required this.sourceName, + this.author, + this.coverUrl, + this.detailUrl, + this.artwork, + this.novel, + this.online, + this.errorMessage, + }); + + factory SearchItem.cloudArtwork(Artwork artwork) { + return SearchItem._( + title: artwork.title, + sourceName: artwork.sourcePlatform, + author: artwork.author, + coverUrl: artwork.thumbnailUrl ?? artwork.thumbnailPath, + artwork: artwork, + ); + } + + factory SearchItem.cloudNovel(Novel novel) { + return SearchItem._( + title: novel.displayChapterTitle, + sourceName: novel.sourcePlatform, + author: novel.author, + coverUrl: novel.coverPath, + detailUrl: novel.contentUrl, + novel: novel, + ); + } + + factory SearchItem.online(UnifiedSearchResult result) { + return SearchItem._( + title: result.title, + sourceName: result.sourceName, + author: result.author, + coverUrl: result.coverUrl, + detailUrl: result.detailUrl, + online: result, + errorMessage: result.errorMessage, + ); + } + + factory SearchItem.error({ + required String sourceName, + required String message, + }) { + return SearchItem._( + title: '搜索失败', + sourceName: sourceName, + errorMessage: message, + ); + } + + final String title; + final String sourceName; + final String? author; + final String? coverUrl; + final String? detailUrl; + final Artwork? artwork; + final Novel? novel; + final UnifiedSearchResult? online; + final String? errorMessage; + + bool get isCloud => artwork != null || novel != null; + bool get isError => errorMessage?.trim().isNotEmpty == true; +} diff --git a/sakura_mobile/lib/src/models/artwork.dart b/sakura_mobile/lib/src/models/artwork.dart new file mode 100644 index 0000000..0581c79 --- /dev/null +++ b/sakura_mobile/lib/src/models/artwork.dart @@ -0,0 +1,119 @@ +class Artwork { + const Artwork({ + required this.id, + required this.title, + required this.sourcePlatform, + this.contentType = 'illust', + this.author, + this.thumbnailPath, + this.imageUrl, + this.thumbnailUrl, + this.mimeType, + this.fileSize = 0, + this.tags = const [], + this.isFavorite = false, + this.lastViewedAt, + this.createdAt, + this.updatedAt, + this.pageCount = 1, + this.matchedBy, + this.artworkMetadata = const {}, + }); + + final String id; + final String title; + final String sourcePlatform; + final String contentType; + final String? author; + final String? thumbnailPath; + final String? imageUrl; + final String? thumbnailUrl; + final String? mimeType; + final int fileSize; + final List tags; + final bool isFavorite; + final DateTime? lastViewedAt; + final DateTime? createdAt; + final DateTime? updatedAt; + final int pageCount; + final String? matchedBy; + final Map artworkMetadata; + + Map get imageHeaders { + final raw = artworkMetadata['headers']; + if (raw is! Map) return const {}; + return raw.map((key, value) => MapEntry(key.toString(), value.toString())); + } + + bool get isVideo => + contentType == 'video' || (mimeType ?? '').toLowerCase().startsWith('video/'); + + String? get primaryMediaPath => imageUrl ?? thumbnailUrl ?? thumbnailPath; + + Artwork copyWith({ + String? title, + String? author, + String? contentType, + String? thumbnailPath, + String? imageUrl, + String? thumbnailUrl, + String? mimeType, + int? fileSize, + List? tags, + bool? isFavorite, + DateTime? lastViewedAt, + DateTime? createdAt, + DateTime? updatedAt, + int? pageCount, + String? matchedBy, + Map? artworkMetadata, + }) { + return Artwork( + id: id, + title: title ?? this.title, + sourcePlatform: sourcePlatform, + contentType: contentType ?? this.contentType, + author: author ?? this.author, + thumbnailPath: thumbnailPath ?? this.thumbnailPath, + imageUrl: imageUrl ?? this.imageUrl, + thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl, + mimeType: mimeType ?? this.mimeType, + fileSize: fileSize ?? this.fileSize, + tags: tags ?? this.tags, + isFavorite: isFavorite ?? this.isFavorite, + lastViewedAt: lastViewedAt ?? this.lastViewedAt, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + pageCount: pageCount ?? this.pageCount, + matchedBy: matchedBy ?? this.matchedBy, + artworkMetadata: artworkMetadata ?? this.artworkMetadata, + ); + } + + factory Artwork.fromJson(Map json) { + return Artwork( + id: json['id'] as String, + title: json['title'] as String? ?? '未命名', + sourcePlatform: json['source_platform'] as String? ?? '未知', + contentType: json['content_type'] as String? ?? 'illust', + author: json['author'] as String?, + thumbnailPath: json['thumbnail_path'] as String?, + imageUrl: json['image_url'] as String?, + thumbnailUrl: json['thumbnail_url'] as String?, + mimeType: json['mime_type'] as String?, + fileSize: (json['file_size'] as num?)?.toInt() ?? 0, + tags: ((json['tags'] as List?) ?? const []) + .map((item) => item.toString()) + .toList(), + isFavorite: json['is_favorite'] as bool? ?? false, + lastViewedAt: DateTime.tryParse(json['last_viewed_at']?.toString() ?? ''), + createdAt: DateTime.tryParse(json['created_at']?.toString() ?? ''), + updatedAt: DateTime.tryParse(json['updated_at']?.toString() ?? ''), + pageCount: (json['page_count'] as num?)?.toInt() ?? 1, + matchedBy: json['matched_by'] as String?, + artworkMetadata: json['artwork_metadata'] is Map + ? Map.from(json['artwork_metadata'] as Map) + : const {}, + ); + } +} diff --git a/sakura_mobile/lib/src/models/novel.dart b/sakura_mobile/lib/src/models/novel.dart new file mode 100644 index 0000000..cffc4e8 --- /dev/null +++ b/sakura_mobile/lib/src/models/novel.dart @@ -0,0 +1,147 @@ +class Novel { + const Novel({ + required this.id, + required this.title, + required this.sourcePlatform, + this.author, + this.seriesId, + this.seriesTitle, + this.chapterTitle, + this.chapterOrder = 0, + this.description, + this.contentUrl, + this.coverPath, + this.wordCount = 0, + this.currentPage = 0, + this.bookmark, + this.tags = const [], + this.isFavorite = false, + this.lastReadAt, + this.lastViewedAt, + }); + + final String id; + final String title; + final String sourcePlatform; + final String? author; + final String? seriesId; + final String? seriesTitle; + final String? chapterTitle; + final int chapterOrder; + final String? description; + final String? contentUrl; + final String? coverPath; + final int wordCount; + final int currentPage; + final String? bookmark; + final List tags; + final bool isFavorite; + final DateTime? lastReadAt; + final DateTime? lastViewedAt; + + bool get isSeriesEntry => (seriesId?.isNotEmpty ?? false); + String get displayChapterTitle => + chapterTitle?.trim().isNotEmpty == true ? chapterTitle! : title; + + Novel copyWith({ + String? title, + String? author, + String? seriesId, + String? seriesTitle, + String? chapterTitle, + int? chapterOrder, + String? description, + String? contentUrl, + String? coverPath, + int? wordCount, + int? currentPage, + String? bookmark, + List? tags, + bool? isFavorite, + DateTime? lastReadAt, + DateTime? lastViewedAt, + }) { + return Novel( + id: id, + title: title ?? this.title, + sourcePlatform: sourcePlatform, + author: author ?? this.author, + seriesId: seriesId ?? this.seriesId, + seriesTitle: seriesTitle ?? this.seriesTitle, + chapterTitle: chapterTitle ?? this.chapterTitle, + chapterOrder: chapterOrder ?? this.chapterOrder, + description: description ?? this.description, + contentUrl: contentUrl ?? this.contentUrl, + coverPath: coverPath ?? this.coverPath, + wordCount: wordCount ?? this.wordCount, + currentPage: currentPage ?? this.currentPage, + bookmark: bookmark ?? this.bookmark, + tags: tags ?? this.tags, + isFavorite: isFavorite ?? this.isFavorite, + lastReadAt: lastReadAt ?? this.lastReadAt, + lastViewedAt: lastViewedAt ?? this.lastViewedAt, + ); + } + + factory Novel.fromJson(Map json) { + return Novel( + id: json['id'] as String, + title: json['title'] as String? ?? '未命名', + sourcePlatform: json['source_platform'] as String? ?? '未知', + author: json['author'] as String?, + seriesId: json['series_id'] as String?, + seriesTitle: json['series_title'] as String?, + chapterTitle: json['chapter_title'] as String?, + chapterOrder: json['chapter_order'] as int? ?? 0, + description: json['description'] as String?, + contentUrl: json['content_url'] as String?, + coverPath: json['cover_path'] as String?, + wordCount: json['word_count'] as int? ?? 0, + currentPage: json['current_page'] as int? ?? 0, + bookmark: json['bookmark'] as String?, + tags: ((json['tags'] as List?) ?? const []) + .map((item) => item.toString()) + .toList(), + isFavorite: json['is_favorite'] as bool? ?? false, + lastReadAt: DateTime.tryParse(json['last_read_at']?.toString() ?? ''), + lastViewedAt: DateTime.tryParse(json['last_viewed_at']?.toString() ?? ''), + ); + } +} + +class NovelSeries { + const NovelSeries({ + required this.seriesId, + required this.seriesTitle, + required this.chapterCount, + required this.chapters, + this.author, + this.coverPath, + this.lastReadAt, + this.lastViewedAt, + }); + + final String seriesId; + final String seriesTitle; + final String? author; + final int chapterCount; + final String? coverPath; + final DateTime? lastReadAt; + final DateTime? lastViewedAt; + final List chapters; + + factory NovelSeries.fromJson(Map json) { + return NovelSeries( + seriesId: json['series_id'] as String? ?? '', + seriesTitle: json['series_title'] as String? ?? '未命名系列', + author: json['author'] as String?, + chapterCount: json['chapter_count'] as int? ?? 0, + coverPath: json['cover_path'] as String?, + lastReadAt: DateTime.tryParse(json['last_read_at']?.toString() ?? ''), + lastViewedAt: DateTime.tryParse(json['last_viewed_at']?.toString() ?? ''), + chapters: ((json['chapters'] as List?) ?? const []) + .map((item) => Novel.fromJson(item as Map)) + .toList(), + ); + } +} diff --git a/sakura_mobile/lib/src/pages/artwork_detail_page.dart b/sakura_mobile/lib/src/pages/artwork_detail_page.dart new file mode 100644 index 0000000..2e824ef --- /dev/null +++ b/sakura_mobile/lib/src/pages/artwork_detail_page.dart @@ -0,0 +1,967 @@ +import 'package:cached_network_image/cached_network_image.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter/services.dart'; +import 'package:photo_view/photo_view.dart'; + +import '../core/manga_collections.dart'; +import '../core/api_client.dart'; +import '../core/providers.dart'; +import '../models/artwork.dart'; + +const MethodChannel _mediaChannel = + MethodChannel('top.kunkunxiaomao.sakura_mobile/media'); + +class ArtworkDetailPage extends ConsumerStatefulWidget { + const ArtworkDetailPage({ + super.key, + required this.artwork, + this.artworkSequence, + this.titleOverride, + this.initialIndex, + }); + + final Artwork artwork; + final List? artworkSequence; + final String? titleOverride; + final int? initialIndex; + + @override + ConsumerState createState() => _ArtworkDetailPageState(); +} + +class _ArtworkDetailPageState extends ConsumerState { + late List artworks; + late int currentIndex; + late PageController mangaController; + bool saving = false; + bool changed = false; + + Artwork get currentArtwork => artworks[currentIndex]; + + bool get _isManga => currentArtwork.contentType == 'manga'; + + bool get _hasMangaSequence => _isManga && artworks.length > 1; + + String get _displayTitle { + final override = widget.titleOverride?.trim(); + if (override != null && override.isNotEmpty) { + return override; + } + return currentArtwork.title; + } + + @override + void initState() { + super.initState(); + _syncFromWidget(); + Future.microtask(_markCurrentArtworkViewed); + } + + @override + void didUpdateWidget(covariant ArtworkDetailPage oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.artwork.id != widget.artwork.id || + !_sameArtworkIds(oldWidget.artworkSequence, widget.artworkSequence) || + oldWidget.initialIndex != widget.initialIndex || + oldWidget.titleOverride != widget.titleOverride) { + final previousController = mangaController; + _syncFromWidget(); + previousController.dispose(); + Future.microtask(_markCurrentArtworkViewed); + } + } + + void _syncFromWidget() { + artworks = _resolveArtworks(); + currentIndex = _resolveInitialIndex(); + mangaController = PageController(initialPage: currentIndex); + } + + List _resolveArtworks() { + final seed = widget.artworkSequence?.isNotEmpty == true + ? widget.artworkSequence! + : [widget.artwork]; + final unique = {}; + for (final item in seed) { + unique[item.id] = item; + } + unique.putIfAbsent(widget.artwork.id, () => widget.artwork); + final resolved = unique.values.toList(growable: false); + if (resolved.every((item) => item.contentType == 'manga')) { + return sortMangaItems(resolved); + } + return resolved; + } + + int _resolveInitialIndex() { + final rawIndex = widget.initialIndex; + if (rawIndex != null) { + if (rawIndex < 0) { + return 0; + } + if (rawIndex >= artworks.length) { + return artworks.length - 1; + } + return rawIndex; + } + final matched = artworks.indexWhere((item) => item.id == widget.artwork.id); + return matched < 0 ? 0 : matched; + } + + Future _markCurrentArtworkViewed() async { + changed = true; + try { + await ref.read(apiClientProvider).markArtworkViewed(currentArtwork.id); + } catch (_) { + // Viewing state is best-effort and shouldn't block the reader. + } + } + + void _replaceCurrentArtwork(Artwork updated) { + artworks = [...artworks]..[currentIndex] = updated; + } + + Future _toggleFavorite() async { + if (saving) { + return; + } + setState(() { + saving = true; + }); + try { + final api = ref.read(apiClientProvider); + final response = currentArtwork.isFavorite + ? await api.unfavoriteArtwork(currentArtwork.id) + : await api.favoriteArtwork(currentArtwork.id); + if (!mounted) { + return; + } + setState(() { + _replaceCurrentArtwork(Artwork.fromJson(response)); + changed = true; + }); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('收藏状态更新失败,请稍后再试。')), + ); + } finally { + if (mounted) { + setState(() { + saving = false; + }); + } + } + } + + Future _downloadArtwork() async { + if (saving) { + return; + } + setState(() { + saving = true; + }); + try { + final api = ref.read(apiClientProvider); + final targets = _hasMangaSequence ? artworks : [currentArtwork]; + String? lastPath; + var saved = 0; + for (var index = 0; index < targets.length; index += 1) { + final item = targets[index]; + lastPath = await api.downloadProtectedFile( + item.imageUrl ?? item.thumbnailUrl ?? item.thumbnailPath, + fileName: _hasMangaSequence + ? '${_displayTitle}_${(index + 1).toString().padLeft(3, '0')}' + : '${item.title}_${item.id}', + subdirectory: item.isVideo + ? 'videos' + : item.contentType == 'manga' + ? 'manga' + : 'images', + ); + saved += 1; + } + if (!mounted) { + return; + } + final message = + _hasMangaSequence ? '已保存 $saved 页到本地。' : '已保存到本地:${lastPath ?? ''}'; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(message)), + ); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('下载失败,请稍后再试。')), + ); + } finally { + if (mounted) { + setState(() { + saving = false; + }); + } + } + } + + Future _editArtwork() async { + final titleController = TextEditingController(text: currentArtwork.title); + final tagsController = + TextEditingController(text: currentArtwork.tags.join(',')); + final updated = await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('编辑图片信息'), + content: SizedBox( + width: 420, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + TextField( + controller: titleController, + decoration: const InputDecoration(labelText: '图片名称'), + ), + const SizedBox(height: 12), + TextField( + controller: tagsController, + minLines: 2, + maxLines: 4, + decoration: const InputDecoration( + labelText: '图片标签', + hintText: '多个标签请用中文逗号、英文逗号或换行分隔', + ), + ), + ], + ), + ), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: const Text('取消'), + ), + FilledButton( + onPressed: () async { + final title = titleController.text.trim(); + final tags = _parseTags(tagsController.text); + if (title.isEmpty) { + return; + } + final response = + await ref.read(apiClientProvider).updateArtwork( + currentArtwork.id, + title: title, + tags: tags, + ); + if (!context.mounted) { + return; + } + Navigator.of(context).pop(Artwork.fromJson(response)); + }, + child: const Text('保存'), + ), + ], + ); + }, + ); + titleController.dispose(); + tagsController.dispose(); + + if (updated == null || !mounted) { + return; + } + setState(() { + _replaceCurrentArtwork(updated); + changed = true; + }); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('图片信息已更新。')), + ); + } + + Future _deleteArtwork() async { + final confirmed = await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('删除图片'), + content: Text('确定要删除《${currentArtwork.title}》吗?删除后会从图库移除。'), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: const Text('取消'), + ), + FilledButton( + onPressed: () => Navigator.of(context).pop(true), + child: const Text('删除'), + ), + ], + ); + }, + ); + if (confirmed != true || !mounted) { + return; + } + + try { + await ref.read(apiClientProvider).deleteArtwork(currentArtwork.id); + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('图片已删除。')), + ); + Navigator.of(context).pop(true); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('删除失败,请稍后再试。')), + ); + } + } + + void _handleMangaPageChanged(int index) { + if (index == currentIndex) { + return; + } + setState(() { + currentIndex = index; + changed = true; + }); + Future.microtask(_markCurrentArtworkViewed); + } + + void _close() { + Navigator.of(context).pop(changed); + } + + Future _openVideo() async { + if (saving) { + return; + } + setState(() { + saving = true; + }); + try { + final api = ref.read(apiClientProvider); + final localPath = await api.downloadProtectedFile( + currentArtwork.primaryMediaPath, + fileName: '${currentArtwork.title}_${currentArtwork.id}', + subdirectory: 'videos', + ); + await _mediaChannel.invokeMethod('openUrl', { + 'url': localPath.startsWith('file://') ? localPath : 'file://$localPath', + 'mimeType': currentArtwork.mimeType ?? 'video/*', + }); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('视频打开失败,请先下载后用本地播放器查看。')), + ); + } finally { + if (mounted) { + setState(() { + saving = false; + }); + } + } + } + + @override + void dispose() { + mangaController.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final api = ref.watch(apiClientProvider); + final theme = Theme.of(context); + + return Scaffold( + appBar: AppBar( + leading: IconButton( + icon: const Icon(Icons.arrow_back), + onPressed: _close, + ), + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Text( + _displayTitle, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + Text( + _hasMangaSequence + ? '第 ${currentIndex + 1} / ${artworks.length} 页' + : (currentArtwork.author?.isNotEmpty == true + ? currentArtwork.author! + : currentArtwork.sourcePlatform), + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: theme.textTheme.labelMedium?.copyWith( + color: theme.colorScheme.onSurfaceVariant, + ), + ), + ], + ), + actions: [ + IconButton( + tooltip: currentArtwork.isFavorite ? '取消收藏' : '加入收藏', + onPressed: saving ? null : _toggleFavorite, + icon: Icon( + currentArtwork.isFavorite + ? Icons.favorite + : Icons.favorite_border, + ), + ), + IconButton( + tooltip: '编辑名称和标签', + onPressed: saving ? null : _editArtwork, + icon: const Icon(Icons.edit_outlined), + ), + IconButton( + tooltip: _hasMangaSequence ? '下载整组' : '下载到本地', + onPressed: saving ? null : _downloadArtwork, + icon: const Icon(Icons.download_rounded), + ), + PopupMenuButton( + onSelected: (value) async { + if (value == 'delete') { + await _deleteArtwork(); + } + }, + itemBuilder: (context) => const [ + PopupMenuItem( + value: 'delete', + child: Text('删除图片'), + ), + ], + ), + ], + ), + body: _isManga + ? _buildMangaBody(context: context, api: api) + : currentArtwork.isVideo + ? _buildVideoBody(context: context, api: api) + : _buildIllustrationBody( + context: context, + api: api, + imageUrl: api.resolveUrl( + currentArtwork.imageUrl ?? + currentArtwork.thumbnailUrl ?? + currentArtwork.thumbnailPath, + ), + thumbUrl: api.resolveUrl( + currentArtwork.thumbnailUrl ?? currentArtwork.thumbnailPath, + ), + imageHeaders: currentArtwork.imageHeaders, + ), + ); + } + + Widget _buildVideoBody({ + required BuildContext context, + required SakuraApiClient api, + }) { + final thumbUrl = api.resolveUrl( + currentArtwork.thumbnailUrl ?? currentArtwork.thumbnailPath, + ); + return ListView( + padding: const EdgeInsets.fromLTRB(16, 16, 16, 28), + children: [ + AspectRatio( + aspectRatio: 16 / 9, + child: ClipRRect( + borderRadius: BorderRadius.circular(24), + child: DecoratedBox( + decoration: const BoxDecoration(color: Color(0xFF241C22)), + child: Stack( + fit: StackFit.expand, + children: [ + if (thumbUrl.isNotEmpty) + CachedNetworkImage( + imageUrl: thumbUrl, + httpHeaders: api.authHeaders(), + fit: BoxFit.cover, + ), + Container(color: Colors.black.withValues(alpha: 0.24)), + Center( + child: IconButton.filled( + iconSize: 40, + onPressed: saving ? null : _openVideo, + icon: const Icon(Icons.play_arrow_rounded), + ), + ), + ], + ), + ), + ), + ), + const SizedBox(height: 18), + Text(currentArtwork.title, style: Theme.of(context).textTheme.titleLarge), + if (currentArtwork.author?.isNotEmpty == true) ...[ + const SizedBox(height: 4), + Text(currentArtwork.author!), + ], + const SizedBox(height: 12), + Wrap( + spacing: 8, + runSpacing: 8, + children: [ + const Chip(label: Text('视频')), + if ((currentArtwork.mimeType ?? '').isNotEmpty) + Chip(label: Text(currentArtwork.mimeType!)), + if (currentArtwork.fileSize > 0) + Chip(label: Text(_formatBytes(currentArtwork.fileSize))), + ], + ), + const SizedBox(height: 18), + Wrap( + spacing: 10, + runSpacing: 10, + children: [ + FilledButton.icon( + onPressed: saving ? null : _openVideo, + icon: const Icon(Icons.play_circle_outline_rounded), + label: const Text('播放视频'), + ), + OutlinedButton.icon( + onPressed: saving ? null : _downloadArtwork, + icon: const Icon(Icons.download_rounded), + label: const Text('下载到本地'), + ), + FilledButton.tonalIcon( + onPressed: saving ? null : _toggleFavorite, + icon: Icon( + currentArtwork.isFavorite ? Icons.favorite : Icons.favorite_border, + ), + label: Text(currentArtwork.isFavorite ? '已收藏' : '收藏'), + ), + ], + ), + ], + ); + } + + Widget _buildMangaBody({ + required BuildContext context, + required SakuraApiClient api, + }) { + return DecoratedBox( + decoration: const BoxDecoration(color: Color(0xFF161316)), + child: Stack( + children: [ + PageView.builder( + key: ValueKey( + artworks.map((item) => item.id).join(':'), + ), + controller: mangaController, + scrollDirection: Axis.vertical, + itemCount: artworks.length, + onPageChanged: _handleMangaPageChanged, + itemBuilder: (context, index) { + final item = artworks[index]; + final imageUrl = api.resolveUrl( + item.imageUrl ?? item.thumbnailUrl ?? item.thumbnailPath, + ); + final thumbUrl = api.resolveUrl( + item.thumbnailUrl ?? item.thumbnailPath, + ); + final imageHeaders = item.imageHeaders; + return SafeArea( + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 12, 16, 24), + child: Column( + children: [ + Expanded( + child: ClipRRect( + borderRadius: BorderRadius.circular(24), + child: DecoratedBox( + decoration: const BoxDecoration( + color: Color(0xFF221D22), + ), + child: imageUrl.isEmpty + ? const Center( + child: Icon( + Icons.image_not_supported_outlined, + size: 48, + color: Colors.white70, + ), + ) + : InteractiveViewer( + minScale: 1, + maxScale: 4, + child: Center( + child: CachedNetworkImage( + imageUrl: imageUrl, + httpHeaders: api.headersForUrl( + imageUrl, + extraHeaders: imageHeaders, + ), + fit: BoxFit.contain, + placeholder: (context, _) => + _buildImageLoadingPlaceholder( + api: api, + thumbUrl: thumbUrl, + imageHeaders: imageHeaders, + fit: BoxFit.contain, + ), + errorWidget: (context, _, __) => + const Center( + child: Icon( + Icons.broken_image_outlined, + size: 42, + color: Colors.white70, + ), + ), + ), + ), + ), + ), + ), + ), + ], + ), + ), + ); + }, + ), + Positioned( + left: 16, + right: 16, + bottom: 18, + child: IgnorePointer( + child: Center( + child: DecoratedBox( + decoration: BoxDecoration( + color: Colors.black.withValues(alpha: 0.62), + borderRadius: BorderRadius.circular(999), + ), + child: Padding( + padding: const EdgeInsets.symmetric( + horizontal: 14, + vertical: 8, + ), + child: Text( + artworks.length > 1 + ? '第 ${currentIndex + 1} 页,共 ${artworks.length} 页' + : '单页阅读', + style: Theme.of(context).textTheme.labelLarge?.copyWith( + color: Colors.white, + fontWeight: FontWeight.w700, + ), + ), + ), + ), + ), + ), + ), + ], + ), + ); + } + + Widget _buildIllustrationBody({ + required BuildContext context, + required SakuraApiClient api, + required String imageUrl, + required String thumbUrl, + required Map imageHeaders, + }) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: imageUrl.isEmpty + ? const Center( + child: Icon(Icons.image_not_supported_outlined, size: 48), + ) + : PhotoView( + imageProvider: CachedNetworkImageProvider( + imageUrl, + headers: api.headersForUrl( + imageUrl, + extraHeaders: imageHeaders, + ), + ), + loadingBuilder: (context, event) => + _buildImageLoadingPlaceholder( + api: api, + thumbUrl: thumbUrl, + imageHeaders: imageHeaders, + fit: BoxFit.contain, + ), + backgroundDecoration: BoxDecoration( + color: Theme.of(context).colorScheme.surface, + ), + minScale: PhotoViewComputedScale.contained, + maxScale: PhotoViewComputedScale.covered * 4, + ), + ), + Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + currentArtwork.title, + style: Theme.of(context).textTheme.titleLarge, + ), + if (currentArtwork.author != null && + currentArtwork.author!.isNotEmpty) ...[ + const SizedBox(height: 4), + Text(currentArtwork.author!), + ], + const SizedBox(height: 10), + Wrap( + spacing: 8, + runSpacing: 8, + children: currentArtwork.tags.isEmpty + ? const [Chip(label: Text('暂无标签'))] + : currentArtwork.tags + .take(20) + .map((tag) => Chip(label: Text(tag))) + .toList(), + ), + const SizedBox(height: 14), + _ArtworkOcrPanel(artworkId: currentArtwork.id), + const SizedBox(height: 14), + Wrap( + spacing: 10, + runSpacing: 10, + children: [ + FilledButton.tonalIcon( + onPressed: saving ? null : _toggleFavorite, + icon: Icon( + currentArtwork.isFavorite + ? Icons.favorite + : Icons.favorite_border, + ), + label: Text(currentArtwork.isFavorite ? '已收藏' : '收藏'), + ), + OutlinedButton.icon( + onPressed: saving ? null : _editArtwork, + icon: const Icon(Icons.sell_outlined), + label: const Text('改名和标签'), + ), + OutlinedButton.icon( + onPressed: saving ? null : _downloadArtwork, + icon: const Icon(Icons.download_rounded), + label: const Text('下载到本地'), + ), + ], + ), + ], + ), + ), + ], + ); + } + + Widget _buildImageLoadingPlaceholder({ + required SakuraApiClient api, + required String thumbUrl, + Map imageHeaders = const {}, + required BoxFit fit, + }) { + return Stack( + fit: StackFit.expand, + children: [ + if (thumbUrl.isNotEmpty) + CachedNetworkImage( + imageUrl: thumbUrl, + httpHeaders: api.headersForUrl( + thumbUrl, + extraHeaders: imageHeaders, + ), + fit: fit, + ), + const Center(child: CircularProgressIndicator()), + ], + ); + } +} + +bool _sameArtworkIds(List? a, List? b) { + final aIds = (a ?? const []).map((item) => item.id).toList(); + final bIds = (b ?? const []).map((item) => item.id).toList(); + if (aIds.length != bIds.length) { + return false; + } + for (var index = 0; index < aIds.length; index += 1) { + if (aIds[index] != bIds[index]) { + return false; + } + } + return true; +} + +String _formatBytes(int bytes) { + if (bytes <= 0) return '0 B'; + const units = ['B', 'KB', 'MB', 'GB']; + var value = bytes.toDouble(); + var index = 0; + while (value >= 1024 && index < units.length - 1) { + value /= 1024; + index += 1; + } + return '${value.toStringAsFixed(index == 0 ? 0 : 1)} ${units[index]}'; +} + +class _ArtworkOcrPanel extends ConsumerStatefulWidget { + const _ArtworkOcrPanel({required this.artworkId}); + + final String artworkId; + + @override + ConsumerState<_ArtworkOcrPanel> createState() => _ArtworkOcrPanelState(); +} + +class _ArtworkOcrPanelState extends ConsumerState<_ArtworkOcrPanel> { + late Future?> future = _load(); + + @override + void didUpdateWidget(covariant _ArtworkOcrPanel oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.artworkId != widget.artworkId) { + future = _load(); + } + } + + Future?> _load() { + return ref.read(apiClientProvider).artworkOcr(widget.artworkId); + } + + Future _retry() async { + await ref.read(apiClientProvider).retryArtworkOcr(widget.artworkId); + if (!mounted) { + return; + } + setState(() { + future = _load(); + }); + } + + @override + Widget build(BuildContext context) { + return FutureBuilder?>( + future: future, + builder: (context, snapshot) { + final row = snapshot.data; + final status = row?['status']?.toString() ?? ''; + final text = row?['ocr_text']?.toString() ?? ''; + return ExpansionTile( + tilePadding: EdgeInsets.zero, + childrenPadding: EdgeInsets.zero, + leading: const Icon(Icons.document_scanner_outlined), + title: const Text('图片文字'), + subtitle: Text(_statusText(status, snapshot.connectionState)), + children: [ + if (snapshot.connectionState == ConnectionState.waiting) + const Padding( + padding: EdgeInsets.only(bottom: 12), + child: LinearProgressIndicator(), + ) + else if (status == 'done' && text.trim().isNotEmpty) + Container( + width: double.infinity, + padding: const EdgeInsets.all(14), + decoration: BoxDecoration( + color: const Color(0xFFFFF5F8), + borderRadius: BorderRadius.circular(16), + border: Border.all(color: const Color(0xFFF1CAD7)), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + text, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + height: 1.6, + ), + ), + const SizedBox(height: 10), + Align( + alignment: Alignment.centerRight, + child: OutlinedButton.icon( + onPressed: () async { + await Clipboard.setData(ClipboardData(text: text)); + if (!context.mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('已复制图片文字')), + ); + }, + icon: const Icon(Icons.copy_rounded), + label: const Text('复制'), + ), + ), + ], + ), + ) + else + Padding( + padding: const EdgeInsets.only(bottom: 12), + child: Row( + children: [ + Expanded( + child: Text( + status == 'failed' + ? '这张图片暂时没有识别出文字,可以稍后重试。' + : '图片文字还在等待识别。', + ), + ), + TextButton.icon( + onPressed: _retry, + icon: const Icon(Icons.refresh_rounded), + label: const Text('重新识别'), + ), + ], + ), + ), + ], + ); + }, + ); + } + + String _statusText(String status, ConnectionState state) { + if (state == ConnectionState.waiting) { + return '正在读取 OCR 状态'; + } + switch (status) { + case 'done': + return '已识别'; + case 'running': + return '正在识别'; + case 'pending': + return '等待识别'; + case 'failed': + return '识别失败'; + default: + return '暂无识别结果'; + } + } +} + +List _parseTags(String raw) { + return raw + .split(RegExp(r'[\n,,]+')) + .map((item) => item.trim()) + .where((item) => item.isNotEmpty) + .toSet() + .toList() + ..sort(); +} diff --git a/sakura_mobile/lib/src/pages/explore_page.dart b/sakura_mobile/lib/src/pages/explore_page.dart new file mode 100644 index 0000000..e221a12 --- /dev/null +++ b/sakura_mobile/lib/src/pages/explore_page.dart @@ -0,0 +1,511 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/providers.dart'; +import '../core/online_shelf.dart'; +import '../core/unified_search.dart'; +import 'artwork_detail_page.dart'; +import 'novel_reader_page.dart'; +import 'online_book_detail_page.dart'; +import 'source_manager_page.dart'; + +class ExplorePage extends ConsumerStatefulWidget { + const ExplorePage({ + super.key, + this.initialMode = UnifiedSearchMode.manga, + }); + + final UnifiedSearchMode initialMode; + + @override + ConsumerState createState() => _ExplorePageState(); +} + +class _ExplorePageState extends ConsumerState { + final search = TextEditingController(); + late UnifiedSearchMode mode = widget.initialMode; + Stream? resultStream; + final savingOnline = {}; + + @override + void dispose() { + search.dispose(); + super.dispose(); + } + + void _submit() { + final query = search.text.trim(); + if (query.isEmpty) return; + setState(() { + resultStream = ref + .read(unifiedSearchProvider) + .searchStream(query: query, mode: mode); + }); + } + + @override + Widget build(BuildContext context) { + final manager = ref.watch(sourceManagerProvider); + return Scaffold( + appBar: AppBar( + title: const Text('在线搜索'), + actions: [ + IconButton( + tooltip: '书源管理', + onPressed: () { + Navigator.of(context).push( + MaterialPageRoute(builder: (_) => const SourceManagerPage()), + ); + }, + icon: const Icon(Icons.tune_rounded), + ), + ], + ), + body: ListView( + padding: const EdgeInsets.fromLTRB(16, 10, 16, 28), + children: [ + SearchBar( + controller: search, + hintText: mode == UnifiedSearchMode.manga + ? '在云库和已启用的书源中搜索漫画' + : '在云库和已启用的书源中搜索小说', + leading: const Icon(Icons.search), + trailing: [ + IconButton( + tooltip: '搜索', + onPressed: _submit, + icon: const Icon(Icons.arrow_forward_rounded), + ), + ], + onSubmitted: (_) => _submit(), + ), + const SizedBox(height: 12), + SegmentedButton( + segments: const [ + ButtonSegment( + value: UnifiedSearchMode.manga, + icon: Icon(Icons.menu_book_rounded), + label: Text('漫画'), + ), + ButtonSegment( + value: UnifiedSearchMode.novel, + icon: Icon(Icons.auto_stories_rounded), + label: Text('小说'), + ), + ], + selected: {mode}, + onSelectionChanged: (value) { + setState(() { + mode = value.first; + resultStream = null; + }); + }, + ), + const SizedBox(height: 12), + _SourceSummary( + enabledCount: mode == UnifiedSearchMode.manga + ? manager.enabledMangaSources.length + : manager.enabledNovelSources.length, + ), + const SizedBox(height: 16), + StreamBuilder( + stream: resultStream, + builder: (context, snapshot) { + if (resultStream == null) { + return const _ExploreEmptyState( + icon: Icons.travel_explore_outlined, + title: '搜索云库和在线书源', + message: '在线结果可以预览,也可以保存到漫画或书架后用原阅读模式继续阅读。', + ); + } + if (!snapshot.hasData && + snapshot.connectionState == ConnectionState.waiting) { + return const Padding( + padding: EdgeInsets.only(top: 80), + child: Center(child: CircularProgressIndicator()), + ); + } + if (snapshot.hasError) { + return const _ExploreEmptyState( + icon: Icons.cloud_off_outlined, + title: '搜索失败', + message: '云端或书源规则暂时不可用。', + ); + } + final result = snapshot.data ?? const UnifiedSearchSnapshot(); + final sections = result.sections; + if (sections.isEmpty && !result.isComplete) { + return _SearchProgress( + message: result.message ?? '正在寻找新内容…', + doneCount: result.doneCount, + totalCount: result.totalCount, + ); + } + if (sections.isEmpty && result.isComplete) { + return const _ExploreEmptyState( + icon: Icons.search_off_rounded, + title: '暂无搜索结果', + message: '试试其他关键词,或启用更多书源。', + ); + } + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (!result.isComplete) ...[ + _SearchProgress( + message: result.message ?? '在线书源仍在搜索…', + doneCount: result.doneCount, + totalCount: result.totalCount, + ), + const SizedBox(height: 14), + ], + for (final section in sections) ...[ + Text( + '${section.title} (${section.items.length})', + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w800, + ), + ), + const SizedBox(height: 8), + for (final item in section.items) + _SearchResultTile( + item: item, + mode: mode, + onTap: () => _openItem(item), + onSave: item.isCloud || item.isError + ? null + : () => _saveOnlineItem(item), + saving: savingOnline.contains(_itemKey(item)), + ), + const SizedBox(height: 14), + ], + ], + ); + }, + ), + ], + ), + ); + } + + void _openItem(SearchItem item) { + if (item.artwork != null) { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => ArtworkDetailPage(artwork: item.artwork!), + ), + ); + return; + } + if (item.novel != null) { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => NovelReaderPage(novel: item.novel!), + ), + ); + return; + } + final online = item.online; + if (item.isError) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(item.errorMessage ?? '搜索失败,请稍后再试。')), + ); + return; + } + if (online == null) return; + final source = ref.read(sourceManagerProvider).sourceById(online.sourceId); + if (source == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('书源已不可用。')), + ); + return; + } + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => OnlineBookDetailPage( + source: source, + initialTitle: item.title, + initialAuthor: item.author, + initialCoverUrl: item.coverUrl, + detailUrl: item.detailUrl ?? '', + onSave: () => _saveOnlineItem(item), + ), + ), + ); + } + + String _itemKey(SearchItem item) { + return [ + item.online?.sourceId ?? item.sourceName, + item.detailUrl ?? item.coverUrl ?? item.title, + mode.name, + ].join('|'); + } + + Future _saveOnlineItem(SearchItem item) async { + final online = item.online; + if (online == null || item.isError) return; + final key = _itemKey(item); + if (savingOnline.contains(key)) return; + final source = ref.read(sourceManagerProvider).sourceById(online.sourceId); + if (source == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('书源已不可用。')), + ); + return; + } + setState(() { + savingOnline.add(key); + }); + try { + final detailUrl = item.detailUrl ?? item.coverUrl ?? ''; + if (detailUrl.isEmpty) { + throw Exception('结果缺少详情链接'); + } + if (mode == UnifiedSearchMode.manga) { + await ref.read(onlineShelfProvider).save(OnlineShelfEntry( + sourceId: online.sourceId, + sourceName: item.sourceName, + title: item.title, + author: item.author, + detailUrl: detailUrl, + coverUrl: item.coverUrl, + savedAt: DateTime.now(), + contentType: 'manga', + )); + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('已添加到漫画,打开时按原书源加载。')), + ); + } else { + await ref.read(onlineShelfProvider).save(OnlineShelfEntry( + sourceId: online.sourceId, + sourceName: item.sourceName, + title: item.title, + author: item.author, + detailUrl: detailUrl, + coverUrl: item.coverUrl, + savedAt: DateTime.now(), + contentType: 'novel', + )); + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('已保存到书架,阅读时再加载章节。')), + ); + } + } catch (error) { + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('保存失败:$error')), + ); + } finally { + if (mounted) { + setState(() { + savingOnline.remove(key); + }); + } + } + } +} + +class _SourceSummary extends StatelessWidget { + const _SourceSummary({required this.enabledCount}); + + final int enabledCount; + + @override + Widget build(BuildContext context) { + return Card( + color: const Color(0xFFFFF7FA), + child: Padding( + padding: const EdgeInsets.all(14), + child: Row( + children: [ + const Icon(Icons.speed_rounded, color: Color(0xFFD95A83)), + const SizedBox(width: 10), + Expanded( + child: Text( + '已启用 $enabledCount 个书源,本次会搜索全部来源。', + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ), + ], + ), + ), + ); + } +} + +class _SearchResultTile extends StatelessWidget { + const _SearchResultTile({ + required this.item, + required this.mode, + required this.onTap, + this.onSave, + this.saving = false, + }); + + final SearchItem item; + final UnifiedSearchMode mode; + final VoidCallback onTap; + final VoidCallback? onSave; + final bool saving; + + @override + Widget build(BuildContext context) { + return Card( + color: item.isError ? const Color(0xFFFFF7FA) : null, + margin: const EdgeInsets.only(bottom: 10), + child: ListTile( + leading: _ResultThumb( + url: item.coverUrl, + icon: item.isError + ? Icons.warning_amber_rounded + : mode == UnifiedSearchMode.manga + ? Icons.menu_book_rounded + : Icons.auto_stories_rounded, + ), + title: Text(item.title, maxLines: 1, overflow: TextOverflow.ellipsis), + subtitle: Text( + item.isError + ? '${item.sourceName} - ${item.errorMessage ?? '搜索失败'}' + : [ + item.author?.trim().isNotEmpty == true + ? item.author! + : '未知作者', + item.sourceName, + item.isCloud ? '云库' : '在线', + ].join(' - '), + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + trailing: onSave == null + ? Icon(item.isError ? Icons.info_outline : Icons.chevron_right) + : IconButton( + tooltip: mode == UnifiedSearchMode.manga ? '添加到漫画' : '加入书架', + onPressed: saving ? null : onSave, + icon: saving + ? const SizedBox( + width: 18, + height: 18, + child: CircularProgressIndicator(strokeWidth: 2), + ) + : Icon( + mode == UnifiedSearchMode.manga + ? Icons.library_add_rounded + : Icons.bookmark_add_outlined, + ), + ), + onTap: item.isError ? null : onTap, + ), + ); + } +} + +class _SearchProgress extends StatelessWidget { + const _SearchProgress({ + required this.message, + required this.doneCount, + required this.totalCount, + }); + + final String message; + final int doneCount; + final int totalCount; + + @override + Widget build(BuildContext context) { + final progress = totalCount > 0 ? doneCount / totalCount : null; + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + LinearProgressIndicator(value: progress), + const SizedBox(height: 8), + Text( + totalCount > 0 ? '$message $doneCount/$totalCount' : message, + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ], + ); + } +} + +class _ResultThumb extends StatelessWidget { + const _ResultThumb({required this.url, required this.icon}); + + final String? url; + final IconData icon; + + @override + Widget build(BuildContext context) { + final value = url?.trim() ?? ''; + return ClipRRect( + borderRadius: BorderRadius.circular(12), + child: SizedBox( + width: 52, + height: 64, + child: value.startsWith('http') + ? Image.network( + value, + fit: BoxFit.cover, + cacheWidth: 160, + cacheHeight: 220, + errorBuilder: (_, __, ___) => _ThumbFallback(icon: icon), + ) + : _ThumbFallback(icon: icon), + ), + ); + } +} + +class _ThumbFallback extends StatelessWidget { + const _ThumbFallback({required this.icon}); + + final IconData icon; + + @override + Widget build(BuildContext context) { + return ColoredBox( + color: const Color(0xFFFFE8EE), + child: Icon(icon, color: const Color(0xFFD95A83)), + ); + } +} + +class _ExploreEmptyState extends StatelessWidget { + const _ExploreEmptyState({ + required this.icon, + required this.title, + required this.message, + }); + + final IconData icon; + final String title; + final String message; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.only(top: 96), + child: Column( + children: [ + Icon(icon, size: 44, color: const Color(0xFFD95A83)), + const SizedBox(height: 12), + Text(title, style: Theme.of(context).textTheme.titleLarge), + const SizedBox(height: 8), + Text( + message, + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ], + ), + ); + } +} diff --git a/sakura_mobile/lib/src/pages/favorites_page.dart b/sakura_mobile/lib/src/pages/favorites_page.dart new file mode 100644 index 0000000..174845f --- /dev/null +++ b/sakura_mobile/lib/src/pages/favorites_page.dart @@ -0,0 +1,355 @@ +import 'package:cached_network_image/cached_network_image.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/manga_collections.dart'; +import '../core/providers.dart'; +import '../models/artwork.dart'; +import '../models/novel.dart'; +import 'artwork_detail_page.dart'; +import 'novel_reader_page.dart'; + +class FavoritesPage extends ConsumerStatefulWidget { + const FavoritesPage({super.key, this.initialIndex = 0}); + + final int initialIndex; + + @override + ConsumerState createState() => _FavoritesPageState(); +} + +class _FavoritesPageState extends ConsumerState with SingleTickerProviderStateMixin { + late final TabController controller = TabController( + length: 2, + vsync: this, + initialIndex: widget.initialIndex.clamp(0, 1), + ); + late Future<_FavoritesData> future = _load(); + + Future<_FavoritesData> _load() async { + final api = ref.read(apiClientProvider); + final results = await Future.wait>([ + api.gallery(isFavorite: true, limit: 100), + api.novels(isFavorite: true), + ]); + return _FavoritesData( + artworks: results[0].map((item) => Artwork.fromJson(item as Map)).toList(), + novels: results[1].map((item) => Novel.fromJson(item as Map)).toList(), + ); + } + + void _refresh() { + ref.invalidate(apiClientProvider); + setState(() { + future = _load(); + }); + } + + Future _openArtwork(Artwork item, List artworks) async { + final mangaCollection = item.contentType == 'manga' + ? findMangaCollectionForArtwork(artworks, item) + : null; + final changed = await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => ArtworkDetailPage( + artwork: item, + artworkSequence: mangaCollection?.items, + titleOverride: mangaCollection?.title, + initialIndex: mangaCollection?.items + .indexWhere((entry) => entry.id == item.id), + ), + ), + ); + if (changed == true && mounted) { + _refresh(); + } + } + + Future _openNovel(Novel item) async { + final changed = await Navigator.of(context).push( + MaterialPageRoute(builder: (_) => NovelReaderPage(novel: item)), + ); + if (changed == true && mounted) { + _refresh(); + } + } + + @override + void dispose() { + controller.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final api = ref.watch(apiClientProvider); + final headers = api.authHeaders(); + final theme = Theme.of(context); + + return Scaffold( + appBar: AppBar( + title: const Text('我的收藏'), + bottom: TabBar( + controller: controller, + tabs: const [ + Tab(text: '收藏图片'), + Tab(text: '收藏小说'), + ], + ), + ), + body: FutureBuilder<_FavoritesData>( + future: future, + builder: (context, snapshot) { + if (snapshot.hasError) { + return const Center(child: Text('收藏内容加载失败,请稍后再试。')); + } + if (!snapshot.hasData) { + return const Center(child: CircularProgressIndicator()); + } + + final data = snapshot.data!; + return TabBarView( + controller: controller, + children: [ + data.artworks.isEmpty + ? const _FavoriteEmptyState( + icon: Icons.favorite_border, + title: '还没有收藏图片', + message: '在画廊里点一下爱心,喜欢的图片就会出现在这里。', + ) + : GridView.builder( + padding: const EdgeInsets.all(12), + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 2, + crossAxisSpacing: 10, + mainAxisSpacing: 10, + childAspectRatio: 0.72, + ), + itemCount: data.artworks.length, + itemBuilder: (context, index) { + final item = data.artworks[index]; + final thumbUrl = api.resolveUrl(item.thumbnailUrl ?? item.thumbnailPath); + + return Card( + clipBehavior: Clip.antiAlias, + child: InkWell( + onTap: () => _openArtwork(item, data.artworks), + child: Padding( + padding: const EdgeInsets.all(12), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ClipRRect( + borderRadius: BorderRadius.circular(16), + child: thumbUrl.isEmpty + ? DecoratedBox( + decoration: BoxDecoration( + color: theme.colorScheme.surfaceContainerHighest, + ), + child: const Center(child: Icon(Icons.image_outlined, size: 38)), + ) + : CachedNetworkImage( + imageUrl: thumbUrl, + httpHeaders: headers, + fit: BoxFit.cover, + fadeInDuration: const Duration(milliseconds: 120), + placeholder: (_, __) => const Center( + child: CircularProgressIndicator(strokeWidth: 2), + ), + errorWidget: (_, __, ___) => DecoratedBox( + decoration: BoxDecoration( + color: theme.colorScheme.surfaceContainerHighest, + ), + child: const Center( + child: Icon(Icons.broken_image_outlined, size: 32), + ), + ), + ), + ), + ), + const SizedBox(height: 10), + Text(item.title, maxLines: 2, overflow: TextOverflow.ellipsis), + const SizedBox(height: 4), + Text( + item.author?.isNotEmpty == true ? item.author! : item.sourcePlatform, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: theme.textTheme.bodySmall?.copyWith( + color: const Color(0xFF6E727A), + ), + ), + if (item.tags.isNotEmpty) ...[ + const SizedBox(height: 8), + Text( + item.tags.take(2).join(' '), + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: theme.textTheme.bodySmall?.copyWith( + color: const Color(0xFFB0662B), + fontWeight: FontWeight.w600, + ), + ), + ], + ], + ), + ), + ), + ); + }, + ), + data.novels.isEmpty + ? const _FavoriteEmptyState( + icon: Icons.auto_stories_outlined, + title: '还没有收藏小说', + message: '在书架或阅读页点一下爱心,喜欢的小说就会收进这里。', + ) + : GridView.builder( + padding: const EdgeInsets.all(12), + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 3, + crossAxisSpacing: 12, + mainAxisSpacing: 16, + childAspectRatio: 0.56, + ), + itemCount: data.novels.length, + itemBuilder: (context, index) { + final item = data.novels[index]; + final coverUrl = api.resolveUrl(item.coverPath); + + return InkWell( + borderRadius: BorderRadius.circular(20), + onTap: () => _openNovel(item), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ClipRRect( + borderRadius: BorderRadius.circular(18), + child: coverUrl.isEmpty + ? DecoratedBox( + decoration: BoxDecoration( + gradient: LinearGradient( + colors: _coverColors(index), + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ), + ), + child: Padding( + padding: const EdgeInsets.all(12), + child: Align( + alignment: Alignment.topLeft, + child: Text( + item.title, + maxLines: 5, + overflow: TextOverflow.ellipsis, + style: theme.textTheme.titleMedium?.copyWith( + color: Colors.white, + fontWeight: FontWeight.w800, + height: 1.25, + ), + ), + ), + ), + ) + : CachedNetworkImage( + imageUrl: coverUrl, + httpHeaders: headers, + fit: BoxFit.cover, + fadeInDuration: const Duration(milliseconds: 120), + placeholder: (_, __) => DecoratedBox( + decoration: BoxDecoration( + gradient: LinearGradient( + colors: _coverColors(index), + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ), + ), + child: const Center( + child: CircularProgressIndicator(strokeWidth: 2), + ), + ), + errorWidget: (_, __, ___) => DecoratedBox( + decoration: BoxDecoration( + gradient: LinearGradient( + colors: _coverColors(index), + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ), + ), + child: const Icon( + Icons.menu_book_outlined, + color: Colors.white, + size: 34, + ), + ), + ), + ), + ), + const SizedBox(height: 8), + Text(item.title, maxLines: 2, overflow: TextOverflow.ellipsis), + const SizedBox(height: 4), + Text( + '读到第 ${item.currentPage + 1} 页', + style: theme.textTheme.bodySmall?.copyWith( + color: const Color(0xFF6E727A), + ), + ), + ], + ), + ); + }, + ), + ], + ); + }, + ), + ); + } + + static List _coverColors(int index) { + const palettes = [ + [Color(0xFF7A4D2A), Color(0xFFC48B54)], + [Color(0xFF32558C), Color(0xFF6E8DD0)], + [Color(0xFF5B3447), Color(0xFFB36B8D)], + [Color(0xFF21504A), Color(0xFF59A093)], + ]; + return palettes[index % palettes.length]; + } +} + +class _FavoritesData { + const _FavoritesData({required this.artworks, required this.novels}); + + final List artworks; + final List novels; +} + +class _FavoriteEmptyState extends StatelessWidget { + const _FavoriteEmptyState({ + required this.icon, + required this.title, + required this.message, + }); + + final IconData icon; + final String title; + final String message; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon(icon, size: 44, color: Theme.of(context).colorScheme.primary), + const SizedBox(height: 14), + Text(title, style: Theme.of(context).textTheme.titleLarge), + const SizedBox(height: 8), + Text(message, textAlign: TextAlign.center), + ], + ), + ); + } +} diff --git a/sakura_mobile/lib/src/pages/home_page.dart b/sakura_mobile/lib/src/pages/home_page.dart new file mode 100644 index 0000000..58bd270 --- /dev/null +++ b/sakura_mobile/lib/src/pages/home_page.dart @@ -0,0 +1,83 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/providers.dart'; +import 'login_page.dart'; +import 'tabs/feed_tab.dart'; +import 'tabs/gallery_tab.dart'; +import 'tabs/novels_tab.dart'; +import 'tabs/profile_tab.dart'; + +class HomePage extends ConsumerStatefulWidget { + const HomePage({super.key}); + + @override + ConsumerState createState() => _HomePageState(); +} + +class _HomePageState extends ConsumerState { + int index = 0; + + final pages = const [ + FeedTab(key: ValueKey('feed-tab')), + GalleryTab( + key: ValueKey('gallery-illust-tab'), + pageTitle: '图片图库', + fixedContentType: 'illust', + showTypeFilters: false, + ), + GalleryTab( + key: ValueKey('gallery-manga-tab'), + pageTitle: '漫画图库', + fixedContentType: 'manga', + showTypeFilters: false, + ), + NovelsTab(key: ValueKey('novels-tab')), + ProfileTab(key: ValueKey('profile-tab')), + ]; + + @override + Widget build(BuildContext context) { + final session = ref.watch(sessionProvider); + if (session.loading) { + return const Scaffold(body: Center(child: CircularProgressIndicator())); + } + if (!session.signedIn) { + return const LoginPage(); + } + return Scaffold( + body: SafeArea( + child: AnimatedSwitcher( + duration: const Duration(milliseconds: 220), + child: pages[index], + ), + ), + bottomNavigationBar: NavigationBar( + selectedIndex: index, + onDestinationSelected: (value) => setState(() => index = value), + destinations: const [ + NavigationDestination( + icon: Icon(Icons.local_florist_rounded), + label: '发现', + ), + NavigationDestination( + icon: Icon(Icons.image_outlined), + label: '图片', + ), + NavigationDestination( + icon: Icon(Icons.menu_book_rounded), + label: '漫画', + ), + NavigationDestination( + icon: Icon(Icons.auto_stories_rounded), + label: '书架', + ), + NavigationDestination( + icon: Icon(Icons.person_outline), + label: '我的', + ), + ], + ), + ); + } +} diff --git a/sakura_mobile/lib/src/pages/login_page.dart b/sakura_mobile/lib/src/pages/login_page.dart new file mode 100644 index 0000000..df8032e --- /dev/null +++ b/sakura_mobile/lib/src/pages/login_page.dart @@ -0,0 +1,411 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/providers.dart'; + +class LoginPage extends ConsumerStatefulWidget { + const LoginPage({super.key}); + + @override + ConsumerState createState() => _LoginPageState(); +} + +class _LoginPageState extends ConsumerState with SingleTickerProviderStateMixin { + final account = TextEditingController(); + final password = TextEditingController(); + final registerUsername = TextEditingController(); + final registerEmail = TextEditingController(); + final registerPassword = TextEditingController(); + final registerConfirmPassword = TextEditingController(); + + late final TabController _tabController; + String? _message; + bool _messageIsError = false; + + @override + void initState() { + super.initState(); + _tabController = TabController(length: 2, vsync: this); + _tabController.addListener(() { + if (!_tabController.indexIsChanging && mounted) { + ref.read(sessionProvider.notifier).clearError(); + setState(() { + _message = null; + _messageIsError = false; + }); + } + }); + } + + @override + void dispose() { + _tabController.dispose(); + account.dispose(); + password.dispose(); + registerUsername.dispose(); + registerEmail.dispose(); + registerPassword.dispose(); + registerConfirmPassword.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final session = ref.watch(sessionProvider); + final bannerText = session.error ?? _message; + final bannerIsError = session.error != null || _messageIsError; + final theme = Theme.of(context); + + return Scaffold( + body: DecoratedBox( + decoration: const BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + colors: [Color(0xFFFFF7FA), Color(0xFFFFE8F0)], + ), + ), + child: SafeArea( + child: Center( + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 520), + child: ListView( + padding: const EdgeInsets.fromLTRB(20, 24, 20, 28), + children: [ + Container( + padding: const EdgeInsets.fromLTRB(22, 24, 22, 24), + decoration: BoxDecoration( + color: Colors.white.withValues(alpha: 0.96), + borderRadius: BorderRadius.circular(30), + boxShadow: const [ + BoxShadow( + color: Color(0x160F2740), + blurRadius: 28, + offset: Offset(0, 12), + ), + ], + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( + width: 58, + height: 58, + decoration: BoxDecoration( + color: const Color(0xFFD95A83).withValues(alpha: 0.12), + borderRadius: BorderRadius.circular(18), + ), + child: const Icon(Icons.cloud_done_outlined, color: Color(0xFFD95A83), size: 30), + ), + const SizedBox(height: 18), + Text( + '樱云同步', + style: theme.textTheme.headlineMedium?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 10), + Text( + '一个账号管理你的收藏、阅读进度和移动端浏览体验。', + style: theme.textTheme.bodyLarge?.copyWith( + color: const Color(0xFF607287), + height: 1.55, + ), + ), + const SizedBox(height: 20), + Container( + decoration: BoxDecoration( + color: const Color(0xFFFFF1F6), + borderRadius: BorderRadius.circular(18), + ), + child: TabBar( + controller: _tabController, + indicator: const UnderlineTabIndicator( + borderSide: BorderSide(color: Color(0xFFD95A83), width: 3), + insets: EdgeInsets.symmetric(horizontal: 28), + ), + indicatorSize: TabBarIndicatorSize.label, + labelColor: const Color(0xFFD95A83), + unselectedLabelColor: const Color(0xFF5A6D80), + dividerColor: Colors.transparent, + tabs: const [ + Tab(text: '登录'), + Tab(text: '注册'), + ], + ), + ), + if (bannerText != null) ...[ + const SizedBox(height: 16), + _StatusBanner(text: bannerText, isError: bannerIsError), + ], + const SizedBox(height: 18), + SizedBox( + height: 430, + child: TabBarView( + controller: _tabController, + children: [ + _LoginForm( + account: account, + password: password, + loading: session.loading, + onSubmit: _submitLogin, + ), + _RegisterForm( + username: registerUsername, + email: registerEmail, + password: registerPassword, + confirmPassword: registerConfirmPassword, + loading: session.loading, + onSubmit: _submitRegister, + ), + ], + ), + ), + ], + ), + ), + ], + ), + ), + ), + ), + ), + ); + } + + Future _submitLogin() async { + final loginId = account.text.trim(); + final pwd = password.text; + if (loginId.isEmpty || pwd.isEmpty) { + _showMessage('请输入账号和密码。', isError: true); + return; + } + + _showMessage(null); + final notifier = ref.read(sessionProvider.notifier); + notifier.clearError(); + await notifier.login(loginId, pwd); + } + + Future _submitRegister() async { + final username = registerUsername.text.trim(); + final email = registerEmail.text.trim(); + final pwd = registerPassword.text; + final confirmPwd = registerConfirmPassword.text; + + if (username.length < 3) { + _showMessage('用户名至少需要 3 个字符。', isError: true); + return; + } + if (!_looksLikeEmail(email)) { + _showMessage('请输入有效的邮箱地址。', isError: true); + return; + } + if (pwd.length < 8) { + _showMessage('密码至少需要 8 位。', isError: true); + return; + } + if (pwd != confirmPwd) { + _showMessage('两次输入的密码不一致。', isError: true); + return; + } + + _showMessage(null); + final notifier = ref.read(sessionProvider.notifier); + notifier.clearError(); + try { + await notifier.register(username, email, pwd); + if (!mounted) return; + account.text = email; + password.clear(); + _tabController.animateTo(0); + _showMessage('注册成功,请使用刚刚创建的账号登录。'); + } catch (_) { + if (!mounted) return; + } + } + + bool _looksLikeEmail(String value) { + return RegExp(r'^[^@\s]+@[^@\s]+\.[^@\s]+$').hasMatch(value); + } + + void _showMessage(String? text, {bool isError = false}) { + if (!mounted) return; + setState(() { + _message = text; + _messageIsError = isError; + }); + } +} + +class _LoginForm extends StatelessWidget { + const _LoginForm({ + required this.account, + required this.password, + required this.loading, + required this.onSubmit, + }); + + final TextEditingController account; + final TextEditingController password; + final bool loading; + final Future Function() onSubmit; + + @override + Widget build(BuildContext context) { + return ListView( + physics: const NeverScrollableScrollPhysics(), + children: [ + const SizedBox(height: 4), + TextField( + controller: account, + decoration: const InputDecoration( + labelText: '邮箱或用户名', + prefixIcon: Icon(Icons.person_outline), + hintText: '请输入邮箱或用户名', + ), + textInputAction: TextInputAction.next, + ), + const SizedBox(height: 14), + TextField( + controller: password, + decoration: const InputDecoration( + labelText: '密码', + prefixIcon: Icon(Icons.lock_outline), + hintText: '请输入密码', + ), + obscureText: true, + onSubmitted: (_) => onSubmit(), + ), + const SizedBox(height: 16), + Text( + '推荐使用邮箱登录,后续找回账号和多设备同步都会更方便。', + style: Theme.of(context).textTheme.bodySmall?.copyWith(color: const Color(0xFF6B7B8D), height: 1.5), + ), + const SizedBox(height: 24), + FilledButton.icon( + onPressed: loading ? null : onSubmit, + icon: loading + ? const SizedBox(width: 18, height: 18, child: CircularProgressIndicator(strokeWidth: 2)) + : const Icon(Icons.login), + label: const Text('登录'), + ), + ], + ); + } +} + +class _RegisterForm extends StatelessWidget { + const _RegisterForm({ + required this.username, + required this.email, + required this.password, + required this.confirmPassword, + required this.loading, + required this.onSubmit, + }); + + final TextEditingController username; + final TextEditingController email; + final TextEditingController password; + final TextEditingController confirmPassword; + final bool loading; + final Future Function() onSubmit; + + @override + Widget build(BuildContext context) { + return ListView( + physics: const NeverScrollableScrollPhysics(), + children: [ + const SizedBox(height: 4), + TextField( + controller: username, + decoration: const InputDecoration( + labelText: '用户名', + prefixIcon: Icon(Icons.badge_outlined), + hintText: '至少 3 个字符', + ), + textInputAction: TextInputAction.next, + ), + const SizedBox(height: 12), + TextField( + controller: email, + decoration: const InputDecoration( + labelText: '邮箱', + prefixIcon: Icon(Icons.mail_outline), + hintText: 'name@example.com', + ), + keyboardType: TextInputType.emailAddress, + textInputAction: TextInputAction.next, + ), + const SizedBox(height: 12), + TextField( + controller: password, + decoration: const InputDecoration( + labelText: '密码', + prefixIcon: Icon(Icons.lock_outline), + hintText: '至少 8 位', + ), + obscureText: true, + textInputAction: TextInputAction.next, + ), + const SizedBox(height: 12), + TextField( + controller: confirmPassword, + decoration: const InputDecoration( + labelText: '确认密码', + prefixIcon: Icon(Icons.verified_user_outlined), + hintText: '请再次输入密码', + ), + obscureText: true, + onSubmitted: (_) => onSubmit(), + ), + const SizedBox(height: 16), + Text( + '注册完成后不会自动登录,返回登录页即可继续使用。', + style: Theme.of(context).textTheme.bodySmall?.copyWith(color: const Color(0xFF6B7B8D), height: 1.5), + ), + const SizedBox(height: 24), + FilledButton.icon( + onPressed: loading ? null : onSubmit, + icon: loading + ? const SizedBox(width: 18, height: 18, child: CircularProgressIndicator(strokeWidth: 2)) + : const Icon(Icons.person_add_alt_1), + label: const Text('注册'), + ), + ], + ); + } +} + +class _StatusBanner extends StatelessWidget { + const _StatusBanner({required this.text, required this.isError}); + + final String text; + final bool isError; + + @override + Widget build(BuildContext context) { + final foreground = isError ? const Color(0xFF8A1C1C) : const Color(0xFF0E6245); + final background = isError ? const Color(0xFFFFEFEF) : const Color(0xFFEAF9F1); + final icon = isError ? Icons.error_outline : Icons.check_circle_outline; + return Container( + padding: const EdgeInsets.all(14), + decoration: BoxDecoration( + color: background, + borderRadius: BorderRadius.circular(16), + ), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Icon(icon, color: foreground, size: 20), + const SizedBox(width: 10), + Expanded( + child: Text( + text, + style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: foreground, height: 1.45), + ), + ), + ], + ), + ); + } +} diff --git a/sakura_mobile/lib/src/pages/novel_detail_page.dart b/sakura_mobile/lib/src/pages/novel_detail_page.dart new file mode 100644 index 0000000..df2d13c --- /dev/null +++ b/sakura_mobile/lib/src/pages/novel_detail_page.dart @@ -0,0 +1,401 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/providers.dart'; +import '../models/novel.dart'; +import 'novel_reader_page.dart'; + +class NovelDetailPage extends ConsumerStatefulWidget { + const NovelDetailPage({ + super.key, + required this.title, + required this.chapters, + this.author, + }); + + final String title; + final String? author; + final List chapters; + + @override + ConsumerState createState() => _NovelDetailPageState(); +} + +class _NovelDetailPageState extends ConsumerState { + final ScrollController _scrollController = ScrollController(); + bool _changed = false; + + List get _chapters { + final chapters = [...widget.chapters]; + chapters.sort((a, b) { + final orderDelta = a.chapterOrder.compareTo(b.chapterOrder); + if (orderDelta != 0) { + return orderDelta; + } + return a.displayChapterTitle.compareTo(b.displayChapterTitle); + }); + return chapters; + } + + Novel get _firstChapter => _chapters.first; + + Novel get _continueChapter { + final chapters = _chapters; + final read = chapters + .where((item) => item.lastReadAt != null || item.currentPage > 0) + .toList() + ..sort((a, b) { + final aTime = a.lastReadAt ?? DateTime.fromMillisecondsSinceEpoch(0); + final bTime = b.lastReadAt ?? DateTime.fromMillisecondsSinceEpoch(0); + return bTime.compareTo(aTime); + }); + return read.isNotEmpty ? read.first : chapters.first; + } + + Future _openReader(Novel novel, {bool fromStart = false}) async { + final target = fromStart ? novel.copyWith(currentPage: 0) : novel; + final changed = await Navigator.of(context).push( + MaterialPageRoute(builder: (_) => NovelReaderPage(novel: target)), + ); + if (changed == true) { + _changed = true; + } + } + + void _scrollToCatalog() { + _scrollController.animateTo( + 430, + duration: const Duration(milliseconds: 260), + curve: Curves.easeOutCubic, + ); + } + + void _close() { + Navigator.of(context).pop(_changed); + } + + @override + void dispose() { + _scrollController.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final chapters = _chapters; + final first = _firstChapter; + final title = widget.title.trim().isNotEmpty ? widget.title : first.title; + final author = widget.author?.trim().isNotEmpty == true + ? widget.author! + : (first.author?.trim().isNotEmpty == true ? first.author! : '未知作者'); + final latest = chapters.last; + final coverUrl = ref.read(apiClientProvider).resolveUrl(first.coverPath); + final description = first.description?.trim(); + + return Scaffold( + appBar: AppBar( + title: Text(title, maxLines: 1, overflow: TextOverflow.ellipsis), + leading: IconButton( + icon: const Icon(Icons.arrow_back), + onPressed: _close, + ), + ), + body: ListView( + controller: _scrollController, + padding: const EdgeInsets.fromLTRB(16, 12, 16, 28), + children: [ + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + _NovelDetailCover(coverUrl: coverUrl, title: title), + const SizedBox(width: 16), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + title, + style: Theme.of(context).textTheme.headlineSmall?.copyWith( + fontWeight: FontWeight.w800, + ), + ), + const SizedBox(height: 8), + _MetaLine(label: '作者', value: author), + _MetaLine(label: '章节', value: '${chapters.length} 章'), + _MetaLine( + label: '最新', + value: latest.displayChapterTitle, + highlight: true, + ), + ], + ), + ), + ], + ), + const SizedBox(height: 18), + _ActionGrid( + onContinue: () => _openReader(_continueChapter), + onCatalog: _scrollToCatalog, + onStart: () => _openReader(first, fromStart: true), + ), + if (description != null && description.isNotEmpty) ...[ + const SizedBox(height: 18), + Text( + '内容简介', + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w800, + ), + ), + const SizedBox(height: 8), + Text( + description, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF6F6470), + height: 1.6, + ), + ), + ], + const SizedBox(height: 22), + Row( + children: [ + Expanded( + child: Text( + '目录', + style: Theme.of(context).textTheme.titleLarge?.copyWith( + fontWeight: FontWeight.w800, + ), + ), + ), + Text( + '${chapters.length} 章', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF8D7B84), + ), + ), + ], + ), + const SizedBox(height: 10), + DecoratedBox( + decoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.circular(18), + border: Border.all(color: const Color(0xFFFFD5E1)), + ), + child: Column( + children: [ + for (var index = 0; index < chapters.length; index++) + _ChapterRow( + index: index, + chapter: chapters[index], + isLast: index == chapters.length - 1, + onTap: () => _openReader(chapters[index]), + ), + ], + ), + ), + ], + ), + ); + } +} + +class _NovelDetailCover extends StatelessWidget { + const _NovelDetailCover({required this.coverUrl, required this.title}); + + final String coverUrl; + final String title; + + @override + Widget build(BuildContext context) { + return ClipRRect( + borderRadius: BorderRadius.circular(16), + child: SizedBox( + width: 104, + child: AspectRatio( + aspectRatio: 0.72, + child: ColoredBox( + color: const Color(0xFFFFE7EF), + child: coverUrl.isEmpty + ? _CoverFallback(title: title) + : Image.network( + coverUrl, + fit: BoxFit.cover, + errorBuilder: (_, __, ___) => _CoverFallback(title: title), + ), + ), + ), + ), + ); + } +} + +class _CoverFallback extends StatelessWidget { + const _CoverFallback({required this.title}); + + final String title; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.all(12), + child: Center( + child: Text( + title, + maxLines: 5, + overflow: TextOverflow.ellipsis, + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.titleSmall?.copyWith( + color: const Color(0xFFD95A83), + fontWeight: FontWeight.w800, + height: 1.3, + ), + ), + ), + ); + } +} + +class _MetaLine extends StatelessWidget { + const _MetaLine({ + required this.label, + required this.value, + this.highlight = false, + }); + + final String label; + final String value; + final bool highlight; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.only(bottom: 6), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '$label:', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + Expanded( + child: Text( + value, + maxLines: 2, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: highlight + ? const Color(0xFFD95A83) + : const Color(0xFF4F4650), + fontWeight: highlight ? FontWeight.w700 : FontWeight.w500, + ), + ), + ), + ], + ), + ); + } +} + +class _ActionGrid extends StatelessWidget { + const _ActionGrid({ + required this.onContinue, + required this.onCatalog, + required this.onStart, + }); + + final VoidCallback onContinue; + final VoidCallback onCatalog; + final VoidCallback onStart; + + @override + Widget build(BuildContext context) { + return LayoutBuilder( + builder: (context, constraints) { + final buttons = [ + FilledButton.icon( + onPressed: onContinue, + icon: const Icon(Icons.play_arrow_rounded), + label: const Text('继续阅读'), + ), + FilledButton.tonalIcon( + onPressed: onCatalog, + icon: const Icon(Icons.list_alt_rounded), + label: const Text('目录'), + ), + OutlinedButton.icon( + onPressed: onStart, + icon: const Icon(Icons.first_page_rounded), + label: const Text('从头开始'), + ), + ]; + if (constraints.maxWidth < 390) { + return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + for (var index = 0; index < buttons.length; index++) ...[ + buttons[index], + if (index != buttons.length - 1) const SizedBox(height: 8), + ], + ], + ); + } + return Row( + children: [ + for (var index = 0; index < buttons.length; index++) ...[ + Expanded(child: buttons[index]), + if (index != buttons.length - 1) const SizedBox(width: 8), + ], + ], + ); + }, + ); + } +} + +class _ChapterRow extends StatelessWidget { + const _ChapterRow({ + required this.index, + required this.chapter, + required this.isLast, + required this.onTap, + }); + + final int index; + final Novel chapter; + final bool isLast; + final VoidCallback onTap; + + @override + Widget build(BuildContext context) { + final readLabel = chapter.lastReadAt != null || chapter.currentPage > 0 + ? '已读到第 ${chapter.currentPage + 1} 页' + : '未开始'; + return Column( + children: [ + ListTile( + contentPadding: const EdgeInsets.symmetric(horizontal: 14), + leading: Text( + '${index + 1}'.padLeft(2, '0'), + style: Theme.of(context).textTheme.labelLarge?.copyWith( + color: const Color(0xFFD95A83), + fontWeight: FontWeight.w800, + ), + ), + title: Text( + chapter.displayChapterTitle, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: const TextStyle(fontWeight: FontWeight.w700), + ), + subtitle: Text(readLabel), + trailing: const Icon(Icons.chevron_right), + onTap: onTap, + ), + if (!isLast) + const Divider(height: 1, indent: 14, endIndent: 14), + ], + ); + } +} diff --git a/sakura_mobile/lib/src/pages/novel_reader_page.dart b/sakura_mobile/lib/src/pages/novel_reader_page.dart new file mode 100644 index 0000000..16f36b5 --- /dev/null +++ b/sakura_mobile/lib/src/pages/novel_reader_page.dart @@ -0,0 +1,1040 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/providers.dart'; +import '../models/novel.dart'; + +class NovelReaderPage extends ConsumerStatefulWidget { + const NovelReaderPage({super.key, required this.novel}); + + final Novel novel; + + @override + ConsumerState createState() => _NovelReaderPageState(); +} + +class _NovelReaderPageState extends ConsumerState { + late Novel currentNovel; + late PageController controller; + late Future> pages; + final Map translatedPages = {}; + final Map> chapterCache = >{}; + Timer? _progressDebounce; + + int currentPage = 0; + double fontSize = 18; + double lineHeight = 1.95; + int paletteIndex = 0; + bool showTranslation = false; + bool translating = false; + bool changed = false; + bool loadingChapters = false; + String? translationError; + List seriesChapters = const []; + + _ReaderPalette get palette => _readerPalettes[paletteIndex]; + + @override + void initState() { + super.initState(); + _applyNovel(widget.novel, markViewed: false); + Future.microtask(() async { + await _markCurrentNovelViewed(); + await _ensureSeriesChapters(); + }); + } + + @override + void didUpdateWidget(covariant NovelReaderPage oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.novel.id != widget.novel.id) { + final previousController = controller; + _applyNovel(widget.novel, markViewed: false); + previousController.dispose(); + Future.microtask(() async { + await _markCurrentNovelViewed(); + await _ensureSeriesChapters(force: true); + }); + } + } + + void _applyNovel(Novel novel, {required bool markViewed}) { + currentNovel = novel; + currentPage = novel.currentPage < 0 ? 0 : novel.currentPage; + controller = PageController(initialPage: currentPage); + pages = _load(); + translatedPages.clear(); + showTranslation = false; + translating = false; + translationError = null; + final seriesId = novel.seriesId?.trim(); + if (seriesId == null || seriesId.isEmpty) { + seriesChapters = const []; + } else if (chapterCache.containsKey(seriesId)) { + seriesChapters = chapterCache[seriesId]!; + } else { + seriesChapters = const []; + } + if (markViewed) { + Future.microtask(_markCurrentNovelViewed); + } + } + + Future _markCurrentNovelViewed() async { + try { + await ref.read(apiClientProvider).markNovelViewed(currentNovel.id); + } catch (_) { + // Reading should continue even if view tracking fails. + } + } + + Future> _load() async { + final api = ref.read(apiClientProvider); + var novel = currentNovel; + try { + novel = Novel.fromJson(await api.novel(currentNovel.id)); + currentNovel = novel; + } catch (_) { + novel = currentNovel; + } + + var text = ''; + if (novel.contentUrl?.trim().isNotEmpty == true) { + try { + text = await api.textFile(novel.contentUrl); + } catch (_) { + text = ''; + } + } + if (text.trim().isEmpty) { + text = await api.novelText(novel.id); + } + return _paginateText(text); + } + + List _paginateText(String text) { + final normalized = text.replaceAll('\r\n', '\n').trim(); + if (normalized.isEmpty) { + return const ['暂无正文内容。']; + } + + const targetSize = 1800; + final paragraphs = normalized.split(RegExp(r'\n{2,}')); + final result = []; + final bucket = []; + var currentLength = 0; + + void flushBucket() { + if (bucket.isEmpty) { + return; + } + result.add(bucket.join('\n\n').trim()); + bucket.clear(); + currentLength = 0; + } + + for (final rawParagraph in paragraphs) { + final paragraph = rawParagraph.trim(); + if (paragraph.isEmpty) { + continue; + } + if (paragraph.length > targetSize) { + flushBucket(); + for (var index = 0; index < paragraph.length; index += targetSize) { + final end = index + targetSize > paragraph.length + ? paragraph.length + : index + targetSize; + result.add(paragraph.substring(index, end).trim()); + } + continue; + } + final projectedLength = + currentLength + paragraph.length + (bucket.isEmpty ? 0 : 2); + if (projectedLength > targetSize) { + flushBucket(); + } + bucket.add(paragraph); + currentLength += paragraph.length + (bucket.length == 1 ? 0 : 2); + } + flushBucket(); + return result.isEmpty ? const ['暂无正文内容。'] : result; + } + + Future _translateCurrentPage() async { + final items = await pages; + if (!mounted) { + return; + } + + final pageIndex = currentPage < 0 + ? 0 + : currentPage >= items.length + ? items.length - 1 + : currentPage; + final cached = translatedPages[pageIndex]; + if (cached != null && cached.isNotEmpty) { + setState(() { + showTranslation = true; + translationError = null; + }); + return; + } + + setState(() { + translating = true; + translationError = null; + }); + + try { + final translated = + await ref.read(apiClientProvider).translateText(items[pageIndex]); + if (!mounted) { + return; + } + setState(() { + translatedPages[pageIndex] = translated; + showTranslation = true; + }); + } catch (error) { + if (!mounted) { + return; + } + setState(() { + translationError = error.toString().replaceFirst('Exception: ', ''); + }); + } finally { + if (mounted) { + setState(() { + translating = false; + }); + } + } + } + + Future _handleTranslationAction() async { + if (translatedPages.containsKey(currentPage)) { + setState(() { + showTranslation = !showTranslation; + translationError = null; + }); + return; + } + await _translateCurrentPage(); + } + + Future _ensureSeriesChapters({bool force = false}) async { + final seriesId = currentNovel.seriesId?.trim(); + if (seriesId == null || seriesId.isEmpty) { + if (mounted && seriesChapters.isNotEmpty) { + setState(() { + seriesChapters = const []; + }); + } + return; + } + if (!force && chapterCache.containsKey(seriesId)) { + if (mounted) { + setState(() { + seriesChapters = chapterCache[seriesId]!; + }); + } + return; + } + if (loadingChapters) { + return; + } + if (mounted) { + setState(() { + loadingChapters = true; + }); + } else { + loadingChapters = true; + } + try { + final rows = await ref.read(apiClientProvider).novelSeries(); + NovelSeries? matched; + for (final item in rows) { + final entry = NovelSeries.fromJson(item as Map); + if (entry.seriesId == seriesId) { + matched = entry; + break; + } + } + final chapters = [...?matched?.chapters]; + if (chapters.every((item) => item.id != currentNovel.id)) { + chapters.add(currentNovel); + } + chapters.sort((a, b) { + final orderDelta = a.chapterOrder.compareTo(b.chapterOrder); + if (orderDelta != 0) { + return orderDelta; + } + return a.displayChapterTitle.compareTo(b.displayChapterTitle); + }); + chapterCache[seriesId] = chapters; + if (mounted) { + setState(() { + seriesChapters = chapters; + }); + } else { + seriesChapters = chapters; + } + } catch (_) { + if (mounted) { + setState(() { + seriesChapters = const []; + }); + } + } finally { + if (mounted) { + setState(() { + loadingChapters = false; + }); + } else { + loadingChapters = false; + } + } + } + + Future _switchChapter(Novel novel) async { + if (novel.id == currentNovel.id) { + return; + } + final previousController = controller; + setState(() { + _applyNovel(novel, markViewed: false); + changed = true; + }); + previousController.dispose(); + await _markCurrentNovelViewed(); + await _ensureSeriesChapters(); + } + + Future _toggleFavorite() async { + try { + final api = ref.read(apiClientProvider); + final response = currentNovel.isFavorite + ? await api.unfavoriteNovel(currentNovel.id) + : await api.favoriteNovel(currentNovel.id); + if (!mounted) { + return; + } + setState(() { + currentNovel = Novel.fromJson(response); + changed = true; + }); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('收藏状态更新失败,请稍后再试。')), + ); + } + } + + Future _confirmDelete() async { + final confirmed = await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('删除小说'), + content: const Text('删除后会从当前账户的云端书库移除,确定继续吗?'), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: const Text('取消'), + ), + FilledButton( + onPressed: () => Navigator.of(context).pop(true), + child: const Text('删除'), + ), + ], + ); + }, + ); + if (confirmed != true || !mounted) { + return; + } + + try { + await ref.read(apiClientProvider).deleteNovel(currentNovel.id); + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('小说已删除。')), + ); + Navigator.of(context).pop(true); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('删除失败,请稍后再试。')), + ); + } + } + + Future _goToPage(int page, int total) async { + if (page < 0 || page >= total) { + return; + } + await controller.animateToPage( + page, + duration: const Duration(milliseconds: 220), + curve: Curves.easeOutCubic, + ); + } + + Future _openReaderPanel() async { + if (currentNovel.isSeriesEntry && + seriesChapters.isEmpty && + !loadingChapters) { + Future.microtask(_ensureSeriesChapters); + } + await showModalBottomSheet( + context: context, + showDragHandle: true, + isScrollControlled: true, + backgroundColor: palette.panelColor, + builder: (context) { + return StatefulBuilder( + builder: (context, setModalState) { + final theme = Theme.of(context); + return SafeArea( + child: Padding( + padding: const EdgeInsets.fromLTRB(20, 8, 20, 20), + child: SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + currentNovel.displayChapterTitle, + style: theme.textTheme.titleLarge?.copyWith( + fontWeight: FontWeight.w800, + color: palette.textColor, + ), + ), + const SizedBox(height: 6), + Text( + currentNovel.author?.isNotEmpty == true + ? currentNovel.author! + : currentNovel.sourcePlatform, + style: theme.textTheme.bodyMedium?.copyWith( + color: palette.mutedTextColor, + ), + ), + const SizedBox(height: 18), + Text( + '阅读设置', + style: theme.textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + color: palette.textColor, + ), + ), + const SizedBox(height: 12), + _SettingLabel( + label: '字体大小', + value: '${fontSize.toStringAsFixed(0)} pt', + color: palette.textColor, + ), + Slider( + value: fontSize, + min: 15, + max: 28, + divisions: 13, + activeColor: palette.accentColor, + onChanged: (value) { + setState(() { + fontSize = value; + }); + setModalState(() {}); + }, + ), + const SizedBox(height: 8), + _SettingLabel( + label: '行高', + value: lineHeight.toStringAsFixed(2), + color: palette.textColor, + ), + Slider( + value: lineHeight, + min: 1.6, + max: 2.4, + divisions: 8, + activeColor: palette.accentColor, + onChanged: (value) { + setState(() { + lineHeight = value; + }); + setModalState(() {}); + }, + ), + const SizedBox(height: 8), + Text( + '背景色', + style: theme.textTheme.titleSmall?.copyWith( + color: palette.textColor, + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 12), + Wrap( + spacing: 10, + runSpacing: 10, + children: List.generate( + _readerPalettes.length, + (index) { + final option = _readerPalettes[index]; + final selected = paletteIndex == index; + return InkWell( + borderRadius: BorderRadius.circular(20), + onTap: () { + setState(() { + paletteIndex = index; + }); + setModalState(() {}); + }, + child: Container( + width: 92, + padding: const EdgeInsets.symmetric( + horizontal: 10, + vertical: 12, + ), + decoration: BoxDecoration( + color: option.backgroundColor, + borderRadius: BorderRadius.circular(20), + border: Border.all( + color: selected + ? option.accentColor + : option.borderColor, + width: selected ? 2 : 1, + ), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( + height: 10, + decoration: BoxDecoration( + color: option.textColor, + borderRadius: + BorderRadius.circular(999), + ), + ), + const SizedBox(height: 8), + Text( + option.label, + style: + theme.textTheme.bodySmall?.copyWith( + color: option.textColor, + fontWeight: FontWeight.w700, + ), + ), + ], + ), + ), + ); + }, + ), + ), + if (currentNovel.isSeriesEntry) ...[ + const SizedBox(height: 24), + Row( + children: [ + Expanded( + child: Text( + '章节', + style: theme.textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + color: palette.textColor, + ), + ), + ), + if (loadingChapters) + const SizedBox( + width: 16, + height: 16, + child: + CircularProgressIndicator(strokeWidth: 2), + ), + ], + ), + const SizedBox(height: 12), + if (!loadingChapters && seriesChapters.isEmpty) + Text( + '暂时还没拿到章节列表。', + style: theme.textTheme.bodyMedium?.copyWith( + color: palette.mutedTextColor, + ), + ) + else + ConstrainedBox( + constraints: const BoxConstraints(maxHeight: 320), + child: ListView.separated( + shrinkWrap: true, + itemCount: seriesChapters.length, + separatorBuilder: (_, __) => Divider( + color: palette.borderColor, height: 1), + itemBuilder: (context, index) { + final chapter = seriesChapters[index]; + final selected = chapter.id == currentNovel.id; + return ListTile( + contentPadding: EdgeInsets.zero, + title: Text( + chapter.displayChapterTitle, + style: theme.textTheme.bodyLarge?.copyWith( + color: palette.textColor, + fontWeight: selected + ? FontWeight.w800 + : FontWeight.w500, + ), + ), + subtitle: Text( + chapter.lastReadAt != null + ? '上次阅读 ${_formatReaderTime(chapter.lastReadAt)}' + : '未开始阅读', + style: theme.textTheme.bodySmall?.copyWith( + color: palette.mutedTextColor, + ), + ), + trailing: selected + ? Icon( + Icons.radio_button_checked, + color: palette.accentColor, + ) + : const Icon(Icons.chevron_right), + onTap: () async { + Navigator.of(context).pop(); + await _switchChapter(chapter); + }, + ); + }, + ), + ), + ], + ], + ), + ), + ), + ); + }, + ); + }, + ); + } + + void _close() { + Navigator.of(context).pop(changed); + } + + void _scheduleProgressUpdate(String novelId, int page) { + _progressDebounce?.cancel(); + _progressDebounce = Timer(const Duration(milliseconds: 500), () { + ref.read(apiClientProvider).updateNovelProgress(novelId, page); + }); + } + + @override + void dispose() { + _progressDebounce?.cancel(); + controller.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final appBarTitle = currentNovel.seriesTitle?.trim().isNotEmpty == true + ? currentNovel.seriesTitle! + : currentNovel.title; + + return Scaffold( + backgroundColor: palette.backgroundColor, + appBar: AppBar( + backgroundColor: palette.backgroundColor, + leading: IconButton( + icon: Icon(Icons.arrow_back, color: palette.textColor), + onPressed: _close, + ), + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Text( + appBarTitle, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle(color: palette.textColor), + ), + Text( + currentNovel.displayChapterTitle, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.labelMedium?.copyWith( + color: palette.mutedTextColor, + ), + ), + ], + ), + actions: [ + IconButton( + tooltip: currentNovel.isFavorite ? '取消收藏' : '加入收藏', + onPressed: _toggleFavorite, + icon: Icon( + currentNovel.isFavorite ? Icons.favorite : Icons.favorite_border, + color: palette.textColor, + ), + ), + IconButton( + tooltip: translatedPages.containsKey(currentPage) + ? (showTranslation ? '切回原文' : '查看译文') + : '翻译当前页', + onPressed: translating ? null : _handleTranslationAction, + icon: translating + ? SizedBox( + width: 18, + height: 18, + child: CircularProgressIndicator( + strokeWidth: 2, + color: palette.textColor, + ), + ) + : Icon( + showTranslation + ? Icons.translate_rounded + : Icons.g_translate_outlined, + color: palette.textColor, + ), + ), + PopupMenuButton( + iconColor: palette.textColor, + onSelected: (value) async { + if (value == 'delete') { + await _confirmDelete(); + } + }, + itemBuilder: (context) => const [ + PopupMenuItem( + value: 'delete', + child: Text('删除小说'), + ), + ], + ), + ], + ), + body: FutureBuilder>( + future: pages, + builder: (context, snapshot) { + if (snapshot.hasError) { + return Center( + child: Text( + '正文加载失败,请稍后再试。', + style: TextStyle(color: palette.textColor), + ), + ); + } + if (!snapshot.hasData) { + return const Center(child: CircularProgressIndicator()); + } + + final items = snapshot.data!; + final safePage = currentPage < 0 + ? 0 + : currentPage >= items.length + ? items.length - 1 + : currentPage; + if (safePage != currentPage) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted) { + return; + } + setState(() { + currentPage = safePage; + currentNovel = currentNovel.copyWith(currentPage: safePage); + changed = true; + }); + if (controller.hasClients) { + controller.jumpToPage(safePage); + } + }); + } + + return Column( + children: [ + if (translationError != null) + Padding( + padding: const EdgeInsets.fromLTRB(16, 4, 16, 8), + child: Container( + width: double.infinity, + padding: const EdgeInsets.all(14), + decoration: BoxDecoration( + color: palette.errorBackgroundColor, + borderRadius: BorderRadius.circular(18), + ), + child: Text( + translationError!, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: palette.errorTextColor, + ), + ), + ), + ), + Expanded( + child: PageView.builder( + controller: controller, + itemCount: items.length, + onPageChanged: (page) { + setState(() { + currentPage = page; + showTranslation = + translatedPages.containsKey(page) && showTranslation; + translationError = null; + currentNovel = currentNovel.copyWith(currentPage: page); + changed = true; + }); + _scheduleProgressUpdate(currentNovel.id, page); + }, + itemBuilder: (context, index) { + final translated = translatedPages[index]; + final text = showTranslation && + index == currentPage && + translated != null && + translated.isNotEmpty + ? translated + : items[index]; + return ListView( + padding: const EdgeInsets.fromLTRB(20, 10, 20, 28), + children: [ + Row( + children: [ + Container( + padding: const EdgeInsets.symmetric( + horizontal: 10, + vertical: 5, + ), + decoration: BoxDecoration( + color: palette.panelColor, + borderRadius: BorderRadius.circular(999), + border: Border.all(color: palette.borderColor), + ), + child: Text( + showTranslation && + index == currentPage && + translated != null + ? '译文' + : '正文', + style: Theme.of(context) + .textTheme + .labelMedium + ?.copyWith( + color: palette.mutedTextColor, + fontWeight: FontWeight.w700, + ), + ), + ), + const Spacer(), + Text( + '${index + 1} / ${items.length}', + style: Theme.of(context) + .textTheme + .labelMedium + ?.copyWith(color: palette.mutedTextColor), + ), + ], + ), + const SizedBox(height: 18), + SelectableText( + text.trim().isEmpty ? '暂无正文内容。' : text, + style: + Theme.of(context).textTheme.bodyLarge?.copyWith( + fontSize: fontSize, + height: lineHeight, + color: palette.textColor, + ), + ), + ], + ); + }, + ), + ), + Padding( + padding: const EdgeInsets.fromLTRB(16, 10, 16, 16), + child: DecoratedBox( + decoration: BoxDecoration( + color: palette.panelColor, + borderRadius: BorderRadius.circular(28), + border: Border.all(color: palette.borderColor), + boxShadow: [ + BoxShadow( + color: Colors.black.withValues(alpha: 0.06), + blurRadius: 18, + offset: const Offset(0, 8), + ), + ], + ), + child: Padding( + padding: const EdgeInsets.symmetric( + horizontal: 10, + vertical: 8, + ), + child: Row( + children: [ + IconButton( + tooltip: '上一页', + onPressed: safePage > 0 + ? () => _goToPage(safePage - 1, items.length) + : null, + icon: const Icon(Icons.chevron_left), + ), + Expanded( + child: TextButton.icon( + onPressed: _openReaderPanel, + icon: const Icon(Icons.menu_book_outlined), + label: Text( + currentNovel.displayChapterTitle, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + ), + ), + IconButton( + tooltip: '阅读设置', + onPressed: _openReaderPanel, + icon: const Icon(Icons.tune_rounded), + ), + IconButton( + tooltip: '下一页', + onPressed: safePage + 1 < items.length + ? () => _goToPage(safePage + 1, items.length) + : null, + icon: const Icon(Icons.chevron_right), + ), + ], + ), + ), + ), + ), + ], + ); + }, + ), + ); + } +} + +class _SettingLabel extends StatelessWidget { + const _SettingLabel({ + required this.label, + required this.value, + required this.color, + }); + + final String label; + final String value; + final Color color; + + @override + Widget build(BuildContext context) { + return Row( + children: [ + Expanded( + child: Text( + label, + style: Theme.of(context).textTheme.titleSmall?.copyWith( + color: color, + fontWeight: FontWeight.w700, + ), + ), + ), + Text( + value, + style: Theme.of(context).textTheme.bodySmall?.copyWith(color: color), + ), + ], + ); + } +} + +class _ReaderPalette { + const _ReaderPalette({ + required this.label, + required this.backgroundColor, + required this.panelColor, + required this.textColor, + required this.mutedTextColor, + required this.accentColor, + required this.borderColor, + required this.errorBackgroundColor, + required this.errorTextColor, + }); + + final String label; + final Color backgroundColor; + final Color panelColor; + final Color textColor; + final Color mutedTextColor; + final Color accentColor; + final Color borderColor; + final Color errorBackgroundColor; + final Color errorTextColor; +} + +const List<_ReaderPalette> _readerPalettes = <_ReaderPalette>[ + _ReaderPalette( + label: '暖纸', + backgroundColor: Color(0xFFF8F1E5), + panelColor: Color(0xFFFDF9F0), + textColor: Color(0xFF2E241A), + mutedTextColor: Color(0xFF7A6857), + accentColor: Color(0xFFC27A39), + borderColor: Color(0xFFE6D7C2), + errorBackgroundColor: Color(0xFFFFEFE6), + errorTextColor: Color(0xFF8A381A), + ), + _ReaderPalette( + label: '竹青', + backgroundColor: Color(0xFFEAF1E8), + panelColor: Color(0xFFF7FBF5), + textColor: Color(0xFF243124), + mutedTextColor: Color(0xFF687565), + accentColor: Color(0xFF4E7B57), + borderColor: Color(0xFFD5E1D2), + errorBackgroundColor: Color(0xFFFFEFE6), + errorTextColor: Color(0xFF8A381A), + ), + _ReaderPalette( + label: '夜读', + backgroundColor: Color(0xFF18161A), + panelColor: Color(0xFF232028), + textColor: Color(0xFFF2EEE8), + mutedTextColor: Color(0xFFB5AEA3), + accentColor: Color(0xFFE1A15B), + borderColor: Color(0xFF34303A), + errorBackgroundColor: Color(0xFF3A251E), + errorTextColor: Color(0xFFFFC8AE), + ), +]; + +String _formatReaderTime(DateTime? value) { + if (value == null) { + return '刚刚阅读'; + } + final now = DateTime.now(); + final diff = now.difference(value); + if (diff.inMinutes < 1) { + return '刚刚阅读'; + } + if (diff.inHours < 1) { + return '${diff.inMinutes} 分钟前'; + } + if (diff.inDays < 1) { + return '${diff.inHours} 小时前'; + } + if (diff.inDays < 30) { + return '${diff.inDays} 天前'; + } + return '${value.month}月${value.day}日'; +} diff --git a/sakura_mobile/lib/src/pages/online_book_detail_page.dart b/sakura_mobile/lib/src/pages/online_book_detail_page.dart new file mode 100644 index 0000000..82c3b60 --- /dev/null +++ b/sakura_mobile/lib/src/pages/online_book_detail_page.dart @@ -0,0 +1,464 @@ +import 'package:cached_network_image/cached_network_image.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/legado_models.dart'; +import '../core/providers.dart'; +import '../core/online_reading_history.dart'; +import '../core/api_client.dart'; +import 'online_manga_reader_page.dart'; +import 'online_novel_reader_page.dart'; + +class OnlineBookDetailPage extends ConsumerStatefulWidget { + const OnlineBookDetailPage({ + super.key, + required this.source, + required this.detailUrl, + required this.initialTitle, + this.initialAuthor, + this.initialCoverUrl, + this.onSave, + }); + + final LegadoSource source; + final String detailUrl; + final String initialTitle; + final String? initialAuthor; + final String? initialCoverUrl; + final Future Function()? onSave; + + @override + ConsumerState createState() => + _OnlineBookDetailPageState(); +} + +class _OnlineBookDetailPageState extends ConsumerState { + OnlineBookInfo? info; + List chapters = const []; + Object? infoError; + Object? tocError; + bool loadingInfo = true; + bool loadingToc = true; + bool descending = false; + bool saving = false; + OnlineReadingRecord? readingRecord; + final parsedContentByUrl = {}; + + @override + void initState() { + super.initState(); + _load(); + } + + Future _load() async { + if (widget.detailUrl.trim().isEmpty) { + setState(() { + loadingInfo = false; + loadingToc = false; + infoError = StateError('搜索结果缺少详情链接。'); + }); + return; + } + final engine = ref.read(legadoEngineProvider); + try { + final value = await engine.fetchBookInfo( + widget.source, + widget.detailUrl, + initialTitle: widget.initialTitle, + initialAuthor: widget.initialAuthor, + initialCoverUrl: widget.initialCoverUrl, + ); + if (!mounted) return; + setState(() { + info = value; + loadingInfo = false; + loadingToc = true; + }); + await _loadToc(value.tocUrl); + final record = await OnlineReadingHistory.find( + widget.source.id, + widget.detailUrl, + ); + if (mounted) setState(() => readingRecord = record); + } catch (error) { + await _loadFromCloudParser(error); + } + } + + Future _loadToc(String url) async { + setState(() { + tocError = null; + loadingToc = true; + }); + try { + final value = await ref.read(legadoEngineProvider).fetchToc( + widget.source, + url, + ); + if (!mounted) return; + if (value.isEmpty) { + await _loadFromCloudParser(StateError('Empty TOC'), keepInfo: true); + return; + } + setState(() { + chapters = value; + loadingToc = false; + }); + } catch (error) { + if (!mounted) return; + setState(() { + tocError = error; + loadingToc = false; + }); + await _loadFromCloudParser(error, keepInfo: true); + } + } + + Future _loadFromCloudParser( + Object originalError, { + bool keepInfo = false, + }) async { + if (!mounted) return; + try { + final parsed = await ref.read(apiClientProvider).parseOnlineContent( + mode: widget.source.isManga ? 'manga' : 'novel', + detailUrl: widget.detailUrl, + ); + final converted = _convertParsedContent(parsed); + if (converted.chapters.isEmpty) { + throw StateError('Cloud parser returned empty chapters'); + } + final record = await OnlineReadingHistory.find( + widget.source.id, + widget.detailUrl, + ); + if (!mounted) return; + setState(() { + info = keepInfo && info != null + ? info + : OnlineBookInfo( + title: converted.title, + author: widget.initialAuthor, + coverUrl: converted.coverUrl, + intro: null, + tocUrl: widget.detailUrl, + sourceName: widget.source.name, + ); + chapters = converted.chapters; + parsedContentByUrl + ..clear() + ..addAll(converted.contentByUrl); + readingRecord = record; + infoError = null; + tocError = null; + loadingInfo = false; + loadingToc = false; + }); + } catch (_) { + if (!mounted) return; + setState(() { + if (keepInfo) { + tocError = originalError; + loadingToc = false; + } else { + infoError = originalError; + loadingInfo = false; + loadingToc = false; + } + }); + } + } + + _ParsedOnlineBook _convertParsedContent(OnlineParsedContent parsed) { + final title = parsed.title.trim().isNotEmpty + ? parsed.title.trim() + : widget.initialTitle; + final cover = parsed.cover?.trim().isNotEmpty == true + ? parsed.cover!.trim() + : widget.initialCoverUrl; + final chapters = []; + final contentByUrl = {}; + for (final item in parsed.chapters) { + final rawUrl = item.url?.trim(); + final chapterUrl = rawUrl == null || rawUrl.isEmpty + ? '${widget.detailUrl}#parsed-${chapters.length + 1}' + : rawUrl; + final chapterTitle = item.name.trim().isNotEmpty + ? item.name.trim() + : '第 ${chapters.length + 1} 章'; + final chapter = OnlineChapter( + title: chapterTitle, + url: chapterUrl, + index: chapters.length + 1, + ); + chapters.add(chapter); + final text = item.content?.trim(); + if ((text != null && text.isNotEmpty) || item.images.isNotEmpty) { + contentByUrl[chapterUrl] = OnlineChapterContent( + title: chapterTitle, + textContent: text, + imageUrls: item.images, + ); + } + } + return _ParsedOnlineBook( + title: title, + coverUrl: cover, + chapters: chapters, + contentByUrl: contentByUrl, + ); + } + + Future _openChapter(int index) async { + final page = widget.source.isManga + ? OnlineMangaReaderPage( + source: widget.source, + title: info?.title ?? widget.initialTitle, + chapters: chapters, + currentChapterIndex: index, + detailUrl: widget.detailUrl, + initialContentByUrl: parsedContentByUrl, + ) + : OnlineNovelReaderPage( + source: widget.source, + title: info?.title ?? widget.initialTitle, + chapters: chapters, + currentChapterIndex: index, + detailUrl: widget.detailUrl, + initialContentByUrl: parsedContentByUrl, + ); + await Navigator.of(context).push(MaterialPageRoute(builder: (_) => page)); + final record = await OnlineReadingHistory.find( + widget.source.id, + widget.detailUrl, + ); + if (mounted) setState(() => readingRecord = record); + } + + Future _save() async { + final callback = widget.onSave; + if (callback == null || saving) return; + setState(() => saving = true); + try { + await callback(); + } finally { + if (mounted) setState(() => saving = false); + } + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: const Text('书籍详情')), + body: loadingInfo + ? const Center(child: CircularProgressIndicator()) + : infoError != null + ? _ErrorState( + message: _failureMessage( + infoError, + '详情加载失败,请检查书源后重试。', + ), + onRetry: _load, + ) + : _buildContent(context), + ); + } + + Widget _buildContent(BuildContext context) { + final value = info!; + final ordered = descending ? chapters.reversed.toList() : chapters; + return ListView( + padding: const EdgeInsets.fromLTRB(18, 12, 18, 32), + children: [ + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ClipRRect( + borderRadius: BorderRadius.circular(8), + child: SizedBox( + width: 104, + height: 142, + child: value.coverUrl?.isNotEmpty == true + ? CachedNetworkImage( + imageUrl: value.coverUrl!, + httpHeaders: widget.source.headers, + fit: BoxFit.cover, + errorWidget: (_, __, ___) => const _CoverPlaceholder(), + ) + : const _CoverPlaceholder(), + ), + ), + const SizedBox(width: 16), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(value.title, + style: Theme.of(context).textTheme.headlineSmall), + const SizedBox(height: 10), + Text(value.author?.trim().isNotEmpty == true + ? value.author! + : '未知作者'), + const SizedBox(height: 6), + Text( + value.sourceName, + style: const TextStyle(color: Color(0xFF7B6570)), + ), + const SizedBox(height: 10), + Text('${chapters.length} 章', + style: const TextStyle(color: Color(0xFFD05A82))), + ], + ), + ), + ], + ), + if (value.intro?.trim().isNotEmpty == true) ...[ + const SizedBox(height: 18), + ExpansionTile( + tilePadding: EdgeInsets.zero, + title: const Text('简介'), + children: [ + Padding( + padding: const EdgeInsets.only(bottom: 12), + child: Text(value.intro!, style: const TextStyle(height: 1.65)), + ), + ], + ), + ], + const SizedBox(height: 16), + FilledButton.icon( + onPressed: chapters.isEmpty + ? null + : () => _openChapter( + readingRecord == null + ? 0 + : readingRecord!.chapterIndex + .clamp(0, chapters.length - 1), + ), + icon: Icon(readingRecord == null + ? Icons.play_arrow_rounded + : Icons.history_rounded), + label: Text(readingRecord == null ? '从第一章开始阅读' : '继续阅读'), + ), + if (widget.onSave != null) ...[ + const SizedBox(height: 10), + OutlinedButton.icon( + onPressed: saving ? null : _save, + icon: saving + ? const SizedBox.square( + dimension: 18, + child: CircularProgressIndicator(strokeWidth: 2)) + : const Icon(Icons.bookmark_add_outlined), + label: const Text('保存到书架'), + ), + ], + const SizedBox(height: 24), + Row( + children: [ + Expanded( + child: Text( + '章节目录${chapters.isEmpty ? '' : '(共 ${chapters.length} 章)'}', + style: Theme.of(context).textTheme.titleLarge, + ), + ), + TextButton.icon( + onPressed: chapters.isEmpty + ? null + : () => setState(() => descending = !descending), + icon: Icon(descending + ? Icons.arrow_downward_rounded + : Icons.arrow_upward_rounded), + label: Text(descending ? '倒序' : '正序'), + ), + ], + ), + if (loadingToc) + const Padding( + padding: EdgeInsets.all(28), + child: Center(child: CircularProgressIndicator()), + ) + else if (tocError != null) + _ErrorState( + message: _failureMessage( + tocError, + '目录加载失败,点这里再试一次。', + ), + onRetry: () => _loadToc(value.tocUrl), + ) + else if (chapters.isEmpty) + const Padding( + padding: EdgeInsets.symmetric(vertical: 30), + child: Center(child: Text('这本书的目录还没有被书源找到。')), + ) + else + for (final chapter in ordered) + ListTile( + contentPadding: EdgeInsets.zero, + leading: const Icon(Icons.menu_book_outlined), + title: Text(chapter.title, + maxLines: 2, overflow: TextOverflow.ellipsis), + trailing: readingRecord != null && + chapter.index - 1 <= readingRecord!.chapterIndex + ? const Icon(Icons.check_rounded, color: Color(0xFFD05A82)) + : const Icon(Icons.chevron_right_rounded), + onTap: () => _openChapter(chapter.index - 1), + ), + ], + ); + } + + String _failureMessage(Object? error, String fallback) { + if (error is LegadoLoadException) return error.message; + return fallback; + } +} + +class _CoverPlaceholder extends StatelessWidget { + const _CoverPlaceholder(); + @override + Widget build(BuildContext context) => Container( + color: const Color(0xFFFFECF2), + alignment: Alignment.center, + child: const Icon(Icons.auto_stories_outlined, + size: 38, color: Color(0xFFD05A82)), + ); +} + +class _ParsedOnlineBook { + const _ParsedOnlineBook({ + required this.title, + required this.chapters, + required this.contentByUrl, + this.coverUrl, + }); + + final String title; + final String? coverUrl; + final List chapters; + final Map contentByUrl; +} + +class _ErrorState extends StatelessWidget { + const _ErrorState({required this.message, required this.onRetry}); + final String message; + final VoidCallback onRetry; + @override + Widget build(BuildContext context) => Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Icon(Icons.cloud_off_outlined, + size: 44, color: Color(0xFFD05A82)), + const SizedBox(height: 10), + Text(message, textAlign: TextAlign.center), + const SizedBox(height: 12), + OutlinedButton.icon( + onPressed: onRetry, + icon: const Icon(Icons.refresh_rounded), + label: const Text('重试'), + ), + ], + ), + ); +} diff --git a/sakura_mobile/lib/src/pages/online_manga_reader_page.dart b/sakura_mobile/lib/src/pages/online_manga_reader_page.dart new file mode 100644 index 0000000..e55e299 --- /dev/null +++ b/sakura_mobile/lib/src/pages/online_manga_reader_page.dart @@ -0,0 +1,234 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/legado_models.dart'; +import '../core/manga_image.dart'; +import '../core/providers.dart'; +import '../core/online_reading_history.dart'; + +class OnlineMangaReaderPage extends ConsumerStatefulWidget { + const OnlineMangaReaderPage({ + super.key, + required this.source, + required this.title, + required this.chapters, + required this.currentChapterIndex, + required this.detailUrl, + this.initialContentByUrl = const {}, + }); + + final LegadoSource source; + final String title; + final List chapters; + final int currentChapterIndex; + final String detailUrl; + final Map initialContentByUrl; + + @override + ConsumerState createState() => + _OnlineMangaReaderPageState(); +} + +class _OnlineMangaReaderPageState extends ConsumerState { + late int chapterIndex = widget.currentChapterIndex; + late Future contentFuture = _load(); + final pageController = PageController(); + int pageIndex = 0; + + OnlineChapter get chapter => widget.chapters[chapterIndex]; + + Future _load() async { + final preloaded = widget.initialContentByUrl[chapter.url]; + if (preloaded != null && + (preloaded.imageUrls.isNotEmpty || preloaded.pageImages.isNotEmpty)) { + await _saveHistory(); + return preloaded; + } + final engine = ref.read(legadoEngineProvider); + final content = await engine.fetchChapterContent(widget.source, chapter); + await _saveHistory(); + final next = chapterIndex + 1; + if (next < widget.chapters.length) { + engine + .fetchChapterContent(widget.source, widget.chapters[next]) + .catchError((_) => OnlineChapterContent( + title: widget.chapters[next].title, + )); + } + return content; + } + + Future _saveHistory() { + return OnlineReadingHistory.save(OnlineReadingRecord( + sourceId: widget.source.id, + sourceName: widget.source.name, + bookTitle: widget.title, + detailUrl: widget.detailUrl, + chapterUrl: chapter.url, + chapterTitle: chapter.title, + chapterIndex: chapterIndex, + totalChapters: widget.chapters.length, + lastReadTime: DateTime.now(), + )); + } + + void _move(int delta) { + final target = chapterIndex + delta; + if (target < 0 || target >= widget.chapters.length) return; + setState(() { + chapterIndex = target; + pageIndex = 0; + contentFuture = _load(); + }); + if (pageController.hasClients) pageController.jumpToPage(0); + } + + @override + void dispose() { + pageController.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text( + '${chapter.title}(${chapterIndex + 1}/${widget.chapters.length})', + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + ), + body: FutureBuilder( + future: contentFuture, + builder: (context, snapshot) { + if (snapshot.hasError) { + return _MangaState( + message: '这个章节暂时没有加载成功。', + onRetry: () => setState(() => contentFuture = _load()), + ); + } + if (!snapshot.hasData) { + return const Center(child: CircularProgressIndicator()); + } + final content = snapshot.data!; + final pages = content.pageImages.isNotEmpty + ? content.pageImages + : content.imageUrls + .asMap() + .entries + .map((entry) => buildPageImage( + raw: entry.value, + chapterUrl: chapter.url, + index: entry.key, + sourceHeaders: widget.source.headers, + userAgent: widget.source.userAgent, + )) + .whereType() + .toList(growable: false); + if (pages.isEmpty) { + return _MangaState( + message: '这一章还没有找到漫画页面,可以换个书源试试。', + onRetry: () => setState(() => contentFuture = _load()), + ); + } + return Stack( + children: [ + PageView.builder( + controller: pageController, + itemCount: pages.length, + onPageChanged: (value) => setState(() => pageIndex = value), + itemBuilder: (context, index) => InteractiveViewer( + child: Center( + child: Image.network( + pages[index].url, + headers: pages[index].headers, + fit: BoxFit.contain, + loadingBuilder: (_, child, progress) => progress == null + ? child + : const Center(child: CircularProgressIndicator()), + errorBuilder: (_, __, ___) => + const Icon(Icons.broken_image_outlined, size: 48), + ), + ), + ), + ), + Positioned( + right: 12, + bottom: 10, + child: DecoratedBox( + decoration: BoxDecoration( + color: Colors.black54, + borderRadius: BorderRadius.circular(6), + ), + child: Padding( + padding: + const EdgeInsets.symmetric(horizontal: 9, vertical: 4), + child: Text('${pageIndex + 1}/${pages.length}', + style: const TextStyle(color: Colors.white)), + ), + ), + ), + ], + ); + }, + ), + bottomNavigationBar: SafeArea( + child: Row( + children: [ + Expanded( + child: TextButton.icon( + onPressed: chapterIndex > 0 ? () => _move(-1) : null, + icon: const Icon(Icons.arrow_back_rounded), + label: const Text('上一章'), + ), + ), + Expanded( + child: TextButton.icon( + onPressed: () => Navigator.of(context).pop(), + icon: const Icon(Icons.list_rounded), + label: const Text('目录'), + ), + ), + Expanded( + child: TextButton.icon( + onPressed: chapterIndex < widget.chapters.length - 1 + ? () => _move(1) + : null, + icon: const Icon(Icons.arrow_forward_rounded), + label: const Text('下一章'), + ), + ), + ], + ), + ), + ); + } +} + +class _MangaState extends StatelessWidget { + const _MangaState({required this.message, required this.onRetry}); + final String message; + final VoidCallback onRetry; + @override + Widget build(BuildContext context) => Center( + child: Padding( + padding: const EdgeInsets.all(28), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const Icon(Icons.image_not_supported_outlined, + size: 44, color: Color(0xFFD05A82)), + const SizedBox(height: 12), + Text(message, textAlign: TextAlign.center), + const SizedBox(height: 12), + OutlinedButton.icon( + onPressed: onRetry, + icon: const Icon(Icons.refresh_rounded), + label: const Text('重试'), + ), + ], + ), + ), + ); +} diff --git a/sakura_mobile/lib/src/pages/online_novel_reader_page.dart b/sakura_mobile/lib/src/pages/online_novel_reader_page.dart new file mode 100644 index 0000000..0453e3e --- /dev/null +++ b/sakura_mobile/lib/src/pages/online_novel_reader_page.dart @@ -0,0 +1,206 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../core/legado_models.dart'; +import '../core/providers.dart'; +import '../core/online_reading_history.dart'; + +class OnlineNovelReaderPage extends ConsumerStatefulWidget { + const OnlineNovelReaderPage({ + super.key, + required this.source, + required this.title, + required this.chapters, + required this.currentChapterIndex, + required this.detailUrl, + this.initialContentByUrl = const {}, + }); + + final LegadoSource source; + final String title; + final List chapters; + final int currentChapterIndex; + final String detailUrl; + final Map initialContentByUrl; + + @override + ConsumerState createState() => + _OnlineNovelReaderPageState(); +} + +class _OnlineNovelReaderPageState extends ConsumerState { + late int chapterIndex = widget.currentChapterIndex; + late Future contentFuture = _load(); + double fontSize = 18; + + OnlineChapter get chapter => widget.chapters[chapterIndex]; + + Future _load() async { + final preloaded = widget.initialContentByUrl[chapter.url]; + if (preloaded != null && + (preloaded.textContent?.trim().isNotEmpty == true)) { + await _saveHistory(); + return preloaded; + } + final engine = ref.read(legadoEngineProvider); + final content = await engine.fetchChapterContent(widget.source, chapter); + await _saveHistory(); + final next = chapterIndex + 1; + if (next < widget.chapters.length) { + engine + .fetchChapterContent(widget.source, widget.chapters[next]) + .catchError((_) => OnlineChapterContent( + title: widget.chapters[next].title, + )); + } + return content; + } + + Future _saveHistory() { + return OnlineReadingHistory.save(OnlineReadingRecord( + sourceId: widget.source.id, + sourceName: widget.source.name, + bookTitle: widget.title, + detailUrl: widget.detailUrl, + chapterUrl: chapter.url, + chapterTitle: chapter.title, + chapterIndex: chapterIndex, + totalChapters: widget.chapters.length, + lastReadTime: DateTime.now(), + )); + } + + void _move(int delta) { + final target = chapterIndex + delta; + if (target < 0 || target >= widget.chapters.length) return; + setState(() { + chapterIndex = target; + contentFuture = _load(); + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text( + '${chapter.title}(${chapterIndex + 1}/${widget.chapters.length})', + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + actions: [ + IconButton( + tooltip: '缩小字体', + onPressed: () => + setState(() => fontSize = (fontSize - 1).clamp(14, 26)), + icon: const Icon(Icons.text_decrease), + ), + IconButton( + tooltip: '放大字体', + onPressed: () => + setState(() => fontSize = (fontSize + 1).clamp(14, 26)), + icon: const Icon(Icons.text_increase), + ), + ], + ), + body: FutureBuilder( + future: contentFuture, + builder: (context, snapshot) { + if (snapshot.hasError) { + return _ReaderState( + icon: Icons.cloud_off_outlined, + title: '章节加载失败', + message: '这个章节暂时没有读到,稍后再试一次吧。', + onRetry: () => setState(() => contentFuture = _load()), + ); + } + if (!snapshot.hasData) { + return const Center(child: CircularProgressIndicator()); + } + final text = snapshot.data!.textContent?.trim() ?? ''; + if (text.isEmpty) { + return _ReaderState( + icon: Icons.article_outlined, + title: '这一章还是空白', + message: '可以换一个书源,或者回目录选择其他章节。', + onRetry: () => setState(() => contentFuture = _load()), + ); + } + return ListView( + padding: const EdgeInsets.fromLTRB(20, 14, 20, 32), + children: [ + Text(chapter.title, + style: Theme.of(context).textTheme.titleLarge), + const SizedBox(height: 18), + Text(text, style: TextStyle(fontSize: fontSize, height: 1.75)), + ], + ); + }, + ), + bottomNavigationBar: SafeArea( + child: Row( + children: [ + Expanded( + child: TextButton.icon( + onPressed: chapterIndex > 0 ? () => _move(-1) : null, + icon: const Icon(Icons.arrow_back_rounded), + label: const Text('上一章'), + ), + ), + Expanded( + child: TextButton.icon( + onPressed: () => Navigator.of(context).pop(), + icon: const Icon(Icons.list_rounded), + label: const Text('目录'), + ), + ), + Expanded( + child: TextButton.icon( + onPressed: chapterIndex < widget.chapters.length - 1 + ? () => _move(1) + : null, + icon: const Icon(Icons.arrow_forward_rounded), + label: const Text('下一章'), + ), + ), + ], + ), + ), + ); + } +} + +class _ReaderState extends StatelessWidget { + const _ReaderState({ + required this.icon, + required this.title, + required this.message, + required this.onRetry, + }); + final IconData icon; + final String title; + final String message; + final VoidCallback onRetry; + @override + Widget build(BuildContext context) => Center( + child: Padding( + padding: const EdgeInsets.all(28), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Icon(icon, size: 44, color: const Color(0xFFD05A82)), + const SizedBox(height: 12), + Text(title, style: Theme.of(context).textTheme.titleLarge), + const SizedBox(height: 8), + Text(message, textAlign: TextAlign.center), + const SizedBox(height: 12), + OutlinedButton.icon( + onPressed: onRetry, + icon: const Icon(Icons.refresh_rounded), + label: const Text('重试'), + ), + ], + ), + ), + ); +} diff --git a/sakura_mobile/lib/src/pages/reminder_center_page.dart b/sakura_mobile/lib/src/pages/reminder_center_page.dart new file mode 100644 index 0000000..efb8988 --- /dev/null +++ b/sakura_mobile/lib/src/pages/reminder_center_page.dart @@ -0,0 +1,911 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:dio/dio.dart'; + +import '../core/providers.dart'; + +class ReminderCenterPage extends ConsumerStatefulWidget { + const ReminderCenterPage({super.key}); + + @override + ConsumerState createState() => _ReminderCenterPageState(); +} + +class _ReminderCenterPageState extends ConsumerState { + late Future<_ReminderCenterData> _future = _load(); + bool _savingReminder = false; + final Set _markingAlertIds = {}; + final Set _deletingFollowIds = {}; + + Future<_ReminderCenterData> _load() async { + final api = ref.read(apiClientProvider); + final results = await Future.wait([ + api.me(), + api.follows(enabled: true), + api.alerts(limit: 50), + ]); + return _ReminderCenterData( + user: Map.from(results[0]! as Map), + follows: (results[1]! as List) + .map((item) => Map.from(item as Map)) + .toList(), + alerts: (results[2]! as List) + .map((item) => Map.from(item as Map)) + .toList(), + ); + } + + void _refresh() { + ref.invalidate(apiClientProvider); + setState(() { + _future = _load(); + }); + } + + Future _updateReminder( + Map user, { + bool? reminderEnabled, + int? reminderHour, + int? reminderMinute, + }) async { + if (_savingReminder) { + return; + } + + final enabled = + reminderEnabled ?? (user['reminder_enabled'] as bool? ?? true); + final hour = reminderHour ?? (user['reminder_hour'] as int? ?? 9); + final minute = reminderMinute ?? (user['reminder_minute'] as int? ?? 0); + final timezone = user['reminder_timezone']?.toString() ?? 'Asia/Shanghai'; + + setState(() { + _savingReminder = true; + }); + + try { + await ref.read(apiClientProvider).updateMe( + reminderEnabled: enabled, + reminderHour: hour, + reminderMinute: minute, + reminderTimezone: timezone, + ); + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + enabled + ? '已更新每日 ${_timeLabel(hour, minute)} 提醒' + : '已关闭每日更新提醒', + ), + ), + ); + _refresh(); + } catch (error) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('提醒设置保存失败:$error')), + ); + } finally { + if (mounted) { + setState(() { + _savingReminder = false; + }); + } + } + } + + Future _pickReminderTime(Map user) async { + final initialTime = TimeOfDay( + hour: user['reminder_hour'] as int? ?? 9, + minute: user['reminder_minute'] as int? ?? 0, + ); + final picked = await showTimePicker( + context: context, + initialTime: initialTime, + helpText: '选择每日提醒时间', + builder: (context, child) { + return Theme( + data: Theme.of(context).copyWith( + colorScheme: Theme.of(context).colorScheme.copyWith( + primary: const Color(0xFFD95A83), + surface: const Color(0xFFFFFBFD), + ), + ), + child: child ?? const SizedBox.shrink(), + ); + }, + ); + if (picked == null) { + return; + } + await _updateReminder( + user, + reminderEnabled: true, + reminderHour: picked.hour, + reminderMinute: picked.minute, + ); + } + + Future _openAlert(Map alert) async { + final alertId = alert['id']?.toString() ?? ''; + final isRead = alert['is_read'] as bool? ?? false; + if (alertId.isNotEmpty && !isRead) { + setState(() { + _markingAlertIds.add(alertId); + }); + try { + await ref.read(apiClientProvider).markAlertRead(alertId); + } catch (_) { + // Keep the detail page usable even if read-state sync fails. + } finally { + if (mounted) { + setState(() { + _markingAlertIds.remove(alertId); + }); + } + } + } + if (!mounted) { + return; + } + await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => _ReminderAlertDetailPage(alert: alert), + ), + ); + if (mounted) { + _refresh(); + } + } + + Future _deleteFollow(Map follow) async { + final followId = follow['id']?.toString() ?? ''; + if (followId.isEmpty || _deletingFollowIds.contains(followId)) { + return; + } + final creatorName = _creatorName(follow); + final confirmed = await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('取消关注'), + content: Text('确定不再关注 $creatorName 吗?手机端会从云端关闭这位作者的更新提醒。'), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: const Text('先保留'), + ), + FilledButton( + onPressed: () => Navigator.of(context).pop(true), + child: const Text('取消关注'), + ), + ], + ); + }, + ); + if (confirmed != true) { + return; + } + setState(() { + _deletingFollowIds.add(followId); + }); + try { + await ref.read(apiClientProvider).deleteFollow(followId); + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('已取消关注 $creatorName')), + ); + _refresh(); + } catch (error) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('取消关注失败:${_friendlyLoadError(error)}')), + ); + } finally { + if (mounted) { + setState(() { + _deletingFollowIds.remove(followId); + }); + } + } + } + + @override + Widget build(BuildContext context) { + return FutureBuilder<_ReminderCenterData>( + future: _future, + builder: (context, snapshot) { + final user = snapshot.data?.user ?? {}; + final follows = snapshot.data?.follows ?? >[]; + final alerts = snapshot.data?.alerts ?? >[]; + final unreadCount = + alerts.where((item) => !(item['is_read'] as bool? ?? false)).length; + final reminderEnabled = user['reminder_enabled'] as bool? ?? true; + final reminderHour = user['reminder_hour'] as int? ?? 9; + final reminderMinute = user['reminder_minute'] as int? ?? 0; + + return Scaffold( + appBar: AppBar(title: const Text('每日提醒')), + body: RefreshIndicator( + color: const Color(0xFFD95A83), + onRefresh: () async { + _refresh(); + await _future; + }, + child: ListView( + padding: const EdgeInsets.fromLTRB(16, 14, 16, 28), + children: [ + Text( + '提醒中心', + style: Theme.of(context) + .textTheme + .headlineMedium + ?.copyWith(fontWeight: FontWeight.w900), + ), + const SizedBox(height: 8), + Text( + '在这里调整每日提醒时间,查看最近提醒,以及你已经同步到云端的关注作者。', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.55, + ), + ), + const SizedBox(height: 14), + _ReminderSummaryCard( + unreadCount: unreadCount, + followCount: follows.length, + reminderLabel: _timeLabel(reminderHour, reminderMinute), + ), + if (snapshot.hasError) ...[ + const SizedBox(height: 12), + Card( + child: ListTile( + leading: const Icon(Icons.cloud_off_outlined), + title: const Text('提醒数据暂时加载失败'), + subtitle: Text( + '下拉刷新后再试:${_friendlyLoadError(snapshot.error)}', + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + ), + ), + ], + const SizedBox(height: 12), + Card( + child: Padding( + padding: const EdgeInsets.all(18), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + const Icon( + Icons.notifications_active_outlined, + color: Color(0xFFD95A83), + ), + const SizedBox(width: 10), + Expanded( + child: Text( + '每日更新提醒', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + ), + Switch.adaptive( + value: reminderEnabled, + activeThumbColor: const Color(0xFFD95A83), + onChanged: _savingReminder + ? null + : (value) => _updateReminder( + user, + reminderEnabled: value, + ), + ), + ], + ), + const SizedBox(height: 8), + Text( + '云端会在设定时间后检查你关注的 Pixiv / X 作者更新,并生成新的摘要提醒。', + style: + Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.5, + ), + ), + const SizedBox(height: 14), + Row( + children: [ + FilledButton.tonalIcon( + onPressed: _savingReminder + ? null + : () => _pickReminderTime(user), + icon: const Icon(Icons.schedule_outlined), + label: Text( + '提醒时间 ${_timeLabel(reminderHour, reminderMinute)}', + ), + ), + const SizedBox(width: 10), + if (_savingReminder) + const SizedBox( + width: 20, + height: 20, + child: + CircularProgressIndicator(strokeWidth: 2), + ), + ], + ), + ], + ), + ), + ), + const SizedBox(height: 12), + _SectionCard( + icon: Icons.people_alt_outlined, + title: '已关注作者', + subtitle: '这里展示已经同步到云端的关注作者,手机提醒会以此为准。', + child: follows.isEmpty + ? const _EmptyMessage( + icon: Icons.person_search_outlined, + title: '还没有已同步的关注作者', + message: '先在桌面端同步 Pixiv / X 关注,稍后回到这里刷新即可。', + ) + : Column( + children: [ + for (final follow in follows) + _FollowTile( + follow: follow, + busy: _deletingFollowIds + .contains(follow['id']?.toString() ?? ''), + onDelete: () => _deleteFollow(follow), + ), + ], + ), + ), + const SizedBox(height: 12), + _SectionCard( + icon: Icons.mark_email_unread_outlined, + title: '最近提醒', + subtitle: '点开单条提醒可以查看完整内容,并把它标记为已读。', + child: alerts.isEmpty + ? const _EmptyMessage( + icon: Icons.notifications_off_outlined, + title: '还没有提醒', + message: '当你关注的作者出现新内容后,这里会自动出现提醒摘要。', + ) + : Column( + children: [ + for (final alert in alerts) + _AlertTile( + alert: alert, + busy: _markingAlertIds + .contains(alert['id']?.toString() ?? ''), + onTap: () => _openAlert(alert), + ), + ], + ), + ), + ], + ), + ), + ); + }, + ); + } +} + +class _ReminderCenterData { + const _ReminderCenterData({ + required this.user, + required this.follows, + required this.alerts, + }); + + final Map user; + final List> follows; + final List> alerts; +} + +class _ReminderSummaryCard extends StatelessWidget { + const _ReminderSummaryCard({ + required this.unreadCount, + required this.followCount, + required this.reminderLabel, + }); + + final int unreadCount; + final int followCount; + final String reminderLabel; + + @override + Widget build(BuildContext context) { + return Container( + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(28), + gradient: const LinearGradient( + colors: [Color(0xFFFFE6EE), Color(0xFFFFF8FB)], + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ), + ), + child: Padding( + padding: const EdgeInsets.all(18), + child: Row( + children: [ + const CircleAvatar( + radius: 26, + backgroundColor: Colors.white, + child: Icon( + Icons.auto_awesome_outlined, + color: Color(0xFFD95A83), + ), + ), + const SizedBox(width: 14), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '今天的提醒总览', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 4), + Text( + '未读提醒 $unreadCount 条,已关注作者 $followCount 位,每日 $reminderLabel 推送摘要。', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.45, + ), + ), + ], + ), + ), + ], + ), + ), + ); + } +} + +class _SectionCard extends StatelessWidget { + const _SectionCard({ + required this.icon, + required this.title, + required this.subtitle, + required this.child, + }); + + final IconData icon; + final String title; + final String subtitle; + final Widget child; + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(18), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Icon(icon, color: const Color(0xFFD95A83)), + const SizedBox(width: 10), + Expanded( + child: Text( + title, + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + ), + ], + ), + const SizedBox(height: 6), + Text( + subtitle, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.5, + ), + ), + const SizedBox(height: 14), + child, + ], + ), + ), + ); + } +} + +class _EmptyMessage extends StatelessWidget { + const _EmptyMessage({ + required this.icon, + required this.title, + required this.message, + }); + + final IconData icon; + final String title; + final String message; + + @override + Widget build(BuildContext context) { + return Container( + width: double.infinity, + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 18), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(22), + color: const Color(0xFFFFF8FB), + ), + child: Column( + children: [ + Icon(icon, color: const Color(0xFFD95A83)), + const SizedBox(height: 10), + Text( + title, + style: Theme.of(context) + .textTheme + .titleSmall + ?.copyWith(fontWeight: FontWeight.w700), + textAlign: TextAlign.center, + ), + const SizedBox(height: 6), + Text( + message, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.5, + ), + textAlign: TextAlign.center, + ), + ], + ), + ); + } +} + +class _FollowTile extends StatelessWidget { + const _FollowTile({ + required this.follow, + required this.busy, + required this.onDelete, + }); + + final Map follow; + final bool busy; + final VoidCallback onDelete; + + @override + Widget build(BuildContext context) { + final creatorName = _creatorName(follow); + final handle = follow['creator_handle']?.toString() ?? ''; + final platform = follow['platform']?.toString() ?? 'unknown'; + final checkedAt = _formatDateTime(follow['last_checked_at']?.toString()); + final publishedAt = + _formatDateTime(follow['latest_published_at']?.toString()); + + return Container( + margin: const EdgeInsets.only(bottom: 10), + padding: const EdgeInsets.all(14), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(20), + color: const Color(0xFFFFF8FB), + ), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + CircleAvatar( + backgroundColor: const Color(0xFFFFE1EA), + child: Text( + creatorName.isNotEmpty + ? creatorName.substring(0, 1).toUpperCase() + : '?', + style: const TextStyle( + color: Color(0xFFD95A83), + fontWeight: FontWeight.w700, + ), + ), + ), + const SizedBox(width: 12), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Expanded( + child: Text( + creatorName, + style: Theme.of(context) + .textTheme + .titleSmall + ?.copyWith(fontWeight: FontWeight.w700), + ), + ), + _PlatformPill(platform: platform), + ], + ), + if (handle.isNotEmpty) ...[ + const SizedBox(height: 4), + Text( + '@$handle', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ], + const SizedBox(height: 6), + Text( + '最近检查:$checkedAt', + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + Text( + '最近更新:$publishedAt', + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ], + ), + ), + const SizedBox(width: 8), + IconButton( + tooltip: '取消关注', + onPressed: busy ? null : onDelete, + icon: busy + ? const SizedBox( + width: 18, + height: 18, + child: CircularProgressIndicator(strokeWidth: 2), + ) + : const Icon(Icons.person_remove_outlined), + color: const Color(0xFFD95A83), + ), + ], + ), + ); + } +} + +class _AlertTile extends StatelessWidget { + const _AlertTile({ + required this.alert, + required this.busy, + required this.onTap, + }); + + final Map alert; + final bool busy; + final VoidCallback onTap; + + @override + Widget build(BuildContext context) { + final title = alert['title']?.toString() ?? '新动态'; + final body = alert['body']?.toString() ?? '你关注的作者有更新了。'; + final platform = alert['platform']?.toString() ?? ''; + final createdAt = _formatDateTime(alert['created_at']?.toString()); + final isRead = alert['is_read'] as bool? ?? false; + + return Card( + margin: const EdgeInsets.only(bottom: 10), + child: ListTile( + contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + onTap: onTap, + leading: CircleAvatar( + backgroundColor: + isRead ? const Color(0xFFF0EEF0) : const Color(0xFFFFE1EA), + child: busy + ? const SizedBox( + width: 18, + height: 18, + child: CircularProgressIndicator(strokeWidth: 2), + ) + : Icon( + isRead + ? Icons.notifications_none_outlined + : Icons.notifications_active_outlined, + color: isRead + ? const Color(0xFF8A7E84) + : const Color(0xFFD95A83), + ), + ), + title: Text( + title, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + subtitle: Text( + '$platform · $createdAt\n$body', + maxLines: 3, + overflow: TextOverflow.ellipsis, + ), + trailing: const Icon(Icons.chevron_right), + ), + ); + } +} + +class _PlatformPill extends StatelessWidget { + const _PlatformPill({required this.platform}); + + final String platform; + + @override + Widget build(BuildContext context) { + final label = platform.toUpperCase(); + return Container( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(999), + color: const Color(0xFFFFE8EE), + ), + child: Text( + label, + style: Theme.of(context).textTheme.labelMedium?.copyWith( + color: const Color(0xFFD95A83), + fontWeight: FontWeight.w700, + ), + ), + ); + } +} + +class _ReminderAlertDetailPage extends StatelessWidget { + const _ReminderAlertDetailPage({required this.alert}); + + final Map alert; + + @override + Widget build(BuildContext context) { + final title = alert['title']?.toString() ?? '提醒详情'; + final body = alert['body']?.toString() ?? '暂无详细说明'; + final platform = alert['platform']?.toString() ?? ''; + final creatorName = _creatorName(alert['payload'] is Map + ? Map.from(alert['payload'] as Map) + : {}); + final targetUrl = alert['target_url']?.toString() ?? ''; + final createdAt = _formatDateTime(alert['created_at']?.toString()); + + return Scaffold( + appBar: AppBar(title: const Text('提醒详情')), + body: ListView( + padding: const EdgeInsets.fromLTRB(16, 14, 16, 28), + children: [ + Text( + title, + style: Theme.of(context) + .textTheme + .headlineSmall + ?.copyWith(fontWeight: FontWeight.w900), + ), + const SizedBox(height: 12), + Card( + child: Padding( + padding: const EdgeInsets.all(18), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + _PlatformPill(platform: platform), + const SizedBox(width: 10), + Expanded( + child: Text( + createdAt, + style: + Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ), + ], + ), + if (creatorName.isNotEmpty) ...[ + const SizedBox(height: 12), + Text( + '作者:$creatorName', + style: + Theme.of(context).textTheme.titleSmall?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + ], + const SizedBox(height: 12), + Text( + body, + style: Theme.of(context).textTheme.bodyLarge?.copyWith( + height: 1.6, + ), + ), + if (targetUrl.isNotEmpty) ...[ + const SizedBox(height: 16), + Text( + '目标链接', + style: + Theme.of(context).textTheme.titleSmall?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 6), + SelectionArea( + child: Text( + targetUrl, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.5, + ), + ), + ), + ], + ], + ), + ), + ), + ], + ), + ); + } +} + +String _creatorName(Map item) { + final creatorName = item['creator_name']?.toString() ?? ''; + if (creatorName.isNotEmpty) { + return creatorName; + } + final handle = item['creator_handle']?.toString() ?? ''; + if (handle.isNotEmpty) { + return '@$handle'; + } + final creatorId = item['creator_id']?.toString() ?? ''; + return creatorId.isNotEmpty ? creatorId : '未命名作者'; +} + +String _timeLabel(int hour, int minute) { + return '${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}'; +} + +String _formatDateTime(String? raw) { + if (raw == null || raw.isEmpty) { + return '暂未记录'; + } + final parsed = DateTime.tryParse(raw)?.toLocal(); + if (parsed == null) { + return raw; + } + return '${parsed.year}-${parsed.month.toString().padLeft(2, '0')}-${parsed.day.toString().padLeft(2, '0')} ' + '${parsed.hour.toString().padLeft(2, '0')}:${parsed.minute.toString().padLeft(2, '0')}'; +} + +String _friendlyLoadError(Object? error) { + if (error is DioException) { + final statusCode = error.response?.statusCode; + final data = error.response?.data; + var detail = ''; + if (data is Map) { + detail = (data['detail'] ?? data['message'] ?? data['error'] ?? '').toString(); + } else if (data != null) { + detail = data.toString(); + } + final status = statusCode == null ? '' : 'HTTP $statusCode'; + final message = detail.isNotEmpty ? detail : (error.message ?? error.type.name); + return [status, message].where((item) => item.isNotEmpty).join(' - '); + } + return error?.toString() ?? 'unknown error'; +} diff --git a/sakura_mobile/lib/src/pages/source_manager_page.dart b/sakura_mobile/lib/src/pages/source_manager_page.dart new file mode 100644 index 0000000..1d95d3f --- /dev/null +++ b/sakura_mobile/lib/src/pages/source_manager_page.dart @@ -0,0 +1,679 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter/services.dart'; + +import '../core/legado_models.dart'; +import '../core/providers.dart'; +import '../core/source_manager.dart'; + +class SourceManagerPage extends ConsumerStatefulWidget { + const SourceManagerPage({super.key}); + + @override + ConsumerState createState() => _SourceManagerPageState(); +} + +class _SourceManagerPageState extends ConsumerState { + static const _sourceImportChannel = + MethodChannel('top.kunkunxiaomao.sakura_mobile/source_import'); + + final customUrl = TextEditingController(); + final jsonText = TextEditingController(); + final searchController = TextEditingController(); + Timer? _debounce; + String filter = ''; + + @override + void initState() { + super.initState(); + Future.microtask( + () => ref.read(sourceManagerProvider).initializeIfFirstRun(), + ); + } + + @override + void dispose() { + _debounce?.cancel(); + customUrl.dispose(); + jsonText.dispose(); + searchController.dispose(); + super.dispose(); + } + + void _onSearchChanged(String value) { + _debounce?.cancel(); + _debounce = Timer(const Duration(milliseconds: 180), () { + if (!mounted) return; + setState(() { + filter = value; + }); + }); + } + + @override + Widget build(BuildContext context) { + final manager = ref.watch(sourceManagerProvider); + final sources = _filterSources(manager); + + return Scaffold( + body: RefreshIndicator( + color: const Color(0xFFD95A83), + onRefresh: () async { + await ref.read(sourceManagerProvider).load(); + }, + child: CustomScrollView( + slivers: [ + SliverAppBar( + pinned: true, + title: const Text('书源管理'), + actions: [ + IconButton( + tooltip: '刷新', + onPressed: manager.busy + ? null + : () => ref.read(sourceManagerProvider).load(), + icon: const Icon(Icons.refresh), + ), + ], + bottom: PreferredSize( + preferredSize: const Size.fromHeight(64), + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 10), + child: SearchBar( + controller: searchController, + hintText: '搜索已导入的书源', + leading: const Icon(Icons.search), + onChanged: _onSearchChanged, + ), + ), + ), + ), + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 12, 16, 0), + child: Column( + children: [ + _StatsCard(manager: manager), + const SizedBox(height: 12), + _MaintenanceCard( + manager: manager, + onUpdateSubscriptions: _updateSubscriptions, + onCheckSources: _checkSources, + ), + const SizedBox(height: 12), + if (manager.lastChecks.isNotEmpty) ...[ + _CheckResultsCard(results: manager.lastChecks), + const SizedBox(height: 12), + ], + _RepoSection(manager: manager, onImport: _importRepo), + const SizedBox(height: 12), + _CustomImportCard( + controller: customUrl, + jsonController: jsonText, + busy: manager.busy, + onImport: _importCustomUrl, + onImportJson: _importJsonText, + onPickJson: _pickJsonFile, + ), + if (manager.busy) ...[ + const SizedBox(height: 12), + const LinearProgressIndicator(), + ], + if (manager.message?.isNotEmpty == true) ...[ + const SizedBox(height: 10), + Align( + alignment: Alignment.centerLeft, + child: Text( + manager.message!, + style: + Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ), + ], + const SizedBox(height: 16), + Align( + alignment: Alignment.centerLeft, + child: Text( + '已导入 (${sources.length})', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + ), + const SizedBox(height: 8), + ], + ), + ), + ), + if (sources.isEmpty) + const SliverToBoxAdapter( + child: Padding( + padding: EdgeInsets.all(28), + child: Center( + child: Text('还没有可用书源,请先从你信任的 URL 导入。'), + ), + ), + ) + else + SliverPadding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 28), + sliver: SliverList.builder( + itemCount: sources.length, + itemBuilder: (context, index) { + final source = sources[index]; + return _SourceTile( + source: source, + busy: manager.busy, + onChanged: (value) => ref + .read(sourceManagerProvider) + .setEnabled(source.id, value), + ); + }, + ), + ), + ], + ), + ), + ); + } + + List _filterSources(SourceManager manager) { + final query = filter.trim().toLowerCase(); + if (query.isEmpty) return manager.sources; + return manager.sources.where((source) { + return '${source.name} ${source.url}'.toLowerCase().contains(query); + }).toList(growable: false); + } + + Future _importRepo(SourceRepo repo) async { + final manager = ref.read(sourceManagerProvider); + final count = await manager.importFromRepo(repo); + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + count > 0 ? '已导入 $count 个新书源。' : manager.message ?? '没有导入新书源。'), + ), + ); + } + + Future _updateSubscriptions() async { + final count = await ref + .read(sourceManagerProvider) + .checkSubscriptionUpdatesIfDue(force: true); + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('订阅更新添加了 $count 个书源。')), + ); + } + + Future _checkSources() async { + final results = await ref.read(sourceManagerProvider).checkEnabledSources(); + if (!mounted) return; + final healthy = results.where((item) => item.ok).length; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('已检测 ${results.length} 个书源,$healthy 个可用。')), + ); + } + + Future _importCustomUrl() async { + final url = customUrl.text.trim(); + if (url.isEmpty) return; + final manager = ref.read(sourceManagerProvider); + final count = await manager.importFromUrl(url); + if (!mounted) return; + if (count > 0) { + customUrl.clear(); + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + count > 0 ? '已导入 $count 个新书源。' : manager.message ?? '导入失败,请检查链接。'), + ), + ); + } + + Future _importJsonText() async { + final body = jsonText.text.trim(); + if (body.isEmpty) return; + final manager = ref.read(sourceManagerProvider); + final count = await manager.importFromJsonText(body); + if (!mounted) return; + if (count > 0) { + jsonText.clear(); + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + count > 0 ? '已导入 $count 个新书源。' : manager.message ?? '没有识别到书源。'), + ), + ); + } + + Future _pickJsonFile() async { + try { + final body = await _sourceImportChannel.invokeMethod('pickJson'); + if (body == null || body.trim().isEmpty) return; + final manager = ref.read(sourceManagerProvider); + final count = await manager.importFromJsonText(body); + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(count > 0 + ? '已从 JSON 文件导入 $count 个新书源。' + : manager.message ?? '文件里没有识别到书源。'), + ), + ); + } on PlatformException catch (error) { + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(error.message ?? '无法读取 JSON 文件。')), + ); + } + } +} + +class _SourceTile extends StatelessWidget { + const _SourceTile({ + required this.source, + required this.busy, + required this.onChanged, + }); + + final LegadoSource source; + final bool busy; + final ValueChanged onChanged; + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.fromLTRB(14, 12, 10, 12), + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Icon( + source.isManga + ? Icons.menu_book_rounded + : Icons.auto_stories_rounded, + color: const Color(0xFFD95A83), + ), + const SizedBox(width: 12), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Text( + source.name, + maxLines: 2, + overflow: TextOverflow.ellipsis, + softWrap: false, + style: Theme.of(context).textTheme.titleSmall?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 4), + Text( + '${source.isManga ? '漫画' : '小说'} - ${source.url}', + maxLines: 2, + overflow: TextOverflow.ellipsis, + softWrap: false, + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + const SizedBox(height: 6), + _CompatBadge(source: source), + ], + ), + ), + const SizedBox(width: 8), + Switch( + value: source.enabled, + activeThumbColor: const Color(0xFFD95A83), + onChanged: busy ? null : onChanged, + ), + ], + ), + ), + ); + } +} + +class _CompatBadge extends ConsumerWidget { + const _CompatBadge({required this.source}); + + final LegadoSource source; + + @override + Widget build(BuildContext context, WidgetRef ref) { + final info = ref.read(sourceManagerProvider).compatibilityFor(source); + final color = switch (info.level) { + SourceCompatLevel.full => const Color(0xFF3A8C62), + SourceCompatLevel.needsJs => const Color(0xFFC56A3D), + SourceCompatLevel.broken => const Color(0xFF8B737D), + _ => const Color(0xFFD05A82), + }; + return Tooltip( + message: + info.warnings.isEmpty ? '当前规则可以由本地引擎执行' : info.warnings.join('\n'), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Icon(Icons.circle, size: 8, color: color), + const SizedBox(width: 5), + Text( + info.label, + style: Theme.of(context).textTheme.labelSmall?.copyWith( + color: color, + fontWeight: FontWeight.w700, + ), + ), + ], + ), + ); + } +} + +class _MaintenanceCard extends StatelessWidget { + const _MaintenanceCard({ + required this.manager, + required this.onUpdateSubscriptions, + required this.onCheckSources, + }); + + final SourceManager manager; + final VoidCallback onUpdateSubscriptions; + final VoidCallback onCheckSources; + + @override + Widget build(BuildContext context) { + final last = manager.lastSubscriptionUpdate; + final lastLabel = last == null + ? '还没有订阅更新记录。' + : '上次订阅更新:${last.month}月${last.day}日 ${last.hour.toString().padLeft(2, '0')}:${last.minute.toString().padLeft(2, '0')}'; + return Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '维护', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 6), + Text( + lastLabel, + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + const SizedBox(height: 12), + Wrap( + spacing: 10, + runSpacing: 10, + children: [ + FilledButton.tonalIcon( + onPressed: manager.busy ? null : onUpdateSubscriptions, + icon: const Icon(Icons.sync_rounded), + label: const Text('更新订阅'), + ), + OutlinedButton.icon( + onPressed: manager.busy || manager.enabledSources.isEmpty + ? null + : onCheckSources, + icon: const Icon(Icons.speed_rounded), + label: const Text('检测已启用书源'), + ), + ], + ), + ], + ), + ), + ); + } +} + +class _CheckResultsCard extends StatelessWidget { + const _CheckResultsCard({required this.results}); + + final List results; + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '最近书源检测', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 10), + for (final result in results.take(10)) + Padding( + padding: const EdgeInsets.only(bottom: 8), + child: Row( + children: [ + Icon( + result.ok + ? Icons.check_circle_outline + : Icons.error_outline, + color: result.ok + ? const Color(0xFF0E6245) + : const Color(0xFFB3261E), + size: 20, + ), + const SizedBox(width: 8), + Expanded( + child: Text( + result.sourceName, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + ), + const SizedBox(width: 8), + Text('${result.elapsedMs} ms'), + ], + ), + ), + ], + ), + ), + ); + } +} + +class _StatsCard extends StatelessWidget { + const _StatsCard({required this.manager}); + + final SourceManager manager; + + @override + Widget build(BuildContext context) { + return Card( + color: const Color(0xFFFFF7FA), + child: Padding( + padding: const EdgeInsets.all(16), + child: Row( + children: [ + const CircleAvatar( + backgroundColor: Color(0xFFFFE1EA), + child: Icon(Icons.tune_rounded, color: Color(0xFFD95A83)), + ), + const SizedBox(width: 14), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '已导入 ${manager.sources.length} 个,已启用 ${manager.enabledSources.length} 个', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 4), + Text( + '搜索最多使用 10 个已启用书源。客户端不内置第三方仓库,请只导入你信任且有使用权限的书源。', + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ], + ), + ), + ], + ), + ), + ); + } +} + +class _RepoSection extends StatelessWidget { + const _RepoSection({ + required this.manager, + required this.onImport, + }); + + final SourceManager manager; + final ValueChanged onImport; + + @override + Widget build(BuildContext context) { + return Card( + child: ExpansionTile( + initiallyExpanded: false, + leading: const Icon(Icons.cloud_download_outlined), + title: const Text('内置书源仓库'), + subtitle: const Text('当前版本不内置第三方仓库 URL'), + children: [ + if (SourceManager.builtinRepos.isEmpty) + const Padding( + padding: EdgeInsets.fromLTRB(16, 0, 16, 16), + child: Align( + alignment: Alignment.centerLeft, + child: Text('请使用下方自定义 URL 导入你信任的书源。'), + ), + ) + else + for (final repo in SourceManager.builtinRepos) + ListTile( + leading: const Icon(Icons.cloud_download_outlined), + title: Text(repo.name), + subtitle: Text(repo.description), + trailing: FilledButton.tonal( + onPressed: manager.busy ? null : () => onImport(repo), + child: Text(repo.isSubscription ? '订阅' : '导入'), + ), + ), + ], + ), + ); + } +} + +class _CustomImportCard extends StatelessWidget { + const _CustomImportCard({ + required this.controller, + required this.jsonController, + required this.busy, + required this.onImport, + required this.onImportJson, + required this.onPickJson, + }); + + final TextEditingController controller; + final TextEditingController jsonController; + final bool busy; + final VoidCallback onImport; + final VoidCallback onImportJson; + final VoidCallback onPickJson; + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '导入书源', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 10), + Text( + '可以选择本地 JSON 文件,也可以粘贴 JSON 内容或输入 URL。', + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + const SizedBox(height: 12), + FilledButton.icon( + onPressed: busy ? null : onPickJson, + icon: const Icon(Icons.upload_file_rounded), + label: const Text('选择 JSON 文件'), + ), + const SizedBox(height: 12), + TextField( + controller: jsonController, + decoration: const InputDecoration( + hintText: '粘贴 source.json 内容', + prefixIcon: Icon(Icons.data_object_rounded), + ), + minLines: 3, + maxLines: 6, + keyboardType: TextInputType.multiline, + ), + const SizedBox(height: 10), + Align( + alignment: Alignment.centerRight, + child: OutlinedButton.icon( + onPressed: busy ? null : onImportJson, + icon: const Icon(Icons.input_rounded), + label: const Text('导入 JSON 内容'), + ), + ), + const SizedBox(height: 16), + TextField( + controller: controller, + decoration: const InputDecoration( + hintText: 'https://example.com/source.json', + prefixIcon: Icon(Icons.link), + ), + keyboardType: TextInputType.url, + ), + const SizedBox(height: 10), + Align( + alignment: Alignment.centerRight, + child: FilledButton.icon( + onPressed: busy ? null : onImport, + icon: const Icon(Icons.add_link), + label: const Text('从 URL 导入'), + ), + ), + ], + ), + ), + ); + } +} diff --git a/sakura_mobile/lib/src/pages/tabs/feed_tab.dart b/sakura_mobile/lib/src/pages/tabs/feed_tab.dart new file mode 100644 index 0000000..30fa58d --- /dev/null +++ b/sakura_mobile/lib/src/pages/tabs/feed_tab.dart @@ -0,0 +1,881 @@ +import 'package:cached_network_image/cached_network_image.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../../core/api_client.dart'; +import '../../core/manga_collections.dart'; +import '../../core/providers.dart'; +import '../../models/artwork.dart'; +import '../../models/novel.dart'; +import '../artwork_detail_page.dart'; +import '../novel_reader_page.dart'; +import '../reminder_center_page.dart'; + +class FeedTab extends ConsumerStatefulWidget { + const FeedTab({super.key}); + + @override + ConsumerState createState() => _FeedTabState(); +} + +class _FeedTabState extends ConsumerState { + final _scrollController = ScrollController(); + Future>? _statFuture; + Future>? _todayFuture; + Future>? _recentFuture; + Future>? _novelFuture; + Future>>? _alertFuture; + Future<_TodaySummary>? _todaySummaryFuture; + + @override + void initState() { + super.initState(); + _ensureFutures(); + } + + void _ensureFutures() { + final api = ref.read(apiClientProvider); + _statFuture ??= api.galleryStat().timeout(const Duration(seconds: 3), + onTimeout: () => {}); + _recentFuture ??= _loadRecentArtworks(api); + _todayFuture ??= Future.delayed(const Duration(milliseconds: 200)) + .then((_) => _loadUpdatedArtworks(api)); + _novelFuture ??= Future.delayed(const Duration(milliseconds: 500)) + .then((_) => api.recentNovels(limit: 12)) + .then((items) => items + .map((item) => Novel.fromJson(item as Map)) + .toList()); + _alertFuture ??= Future.delayed(const Duration(milliseconds: 500)) + .then((_) => api.unreadAlerts(limit: 12)) + .then((items) => + items.map((item) => item as Map).toList()); + _todaySummaryFuture ??= _loadTodaySummary(); + } + + Future _refresh() async { + await ref.read(apiClientProvider).invalidateCache(); + setState(() { + _statFuture = null; + _todayFuture = null; + _recentFuture = null; + _novelFuture = null; + _alertFuture = null; + _todaySummaryFuture = null; + _ensureFutures(); + }); + } + + @override + void dispose() { + _scrollController.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return RefreshIndicator( + color: const Color(0xFFD95A83), + onRefresh: _refresh, + child: ListView( + controller: _scrollController, + physics: const ClampingScrollPhysics( + parent: AlwaysScrollableScrollPhysics(), + ), + keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag, + padding: const EdgeInsets.fromLTRB(16, 14, 16, 24), + children: [ + Text( + '发现', + style: Theme.of(context).textTheme.headlineMedium?.copyWith( + fontWeight: FontWeight.w600, + ), + ), + const SizedBox(height: 12), + FutureBuilder<_TodaySummary>( + future: _todaySummaryFuture, + builder: (context, snapshot) { + final data = snapshot.data ?? _TodaySummary.empty; + return Text( + snapshot.connectionState == ConnectionState.waiting + ? '正在看看今天有没有新内容...' + : '今日新增 ${data.total} 个作品,先把新鲜内容放到眼前。', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.55, + ), + ); + }, + ), + const SizedBox(height: 14), + FutureBuilder<_TodaySummary>( + future: _todaySummaryFuture, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return const _SkeletonCard(height: 112); + } + final data = snapshot.data ?? _TodaySummary.empty; + return _TodayUpdateCard(summary: data); + }, + ), + const SizedBox(height: 22), + const _SectionHeader( + title: '最近更新', + subtitle: '按内容类型整理,新作品会优先出现。', + ), + const SizedBox(height: 12), + FutureBuilder>( + future: _todayFuture, + builder: (context, snapshot) { + final items = (snapshot.data ?? []) + ..sort(_compareArtworkUpdateDesc); + final illusts = + items.where((item) => item.contentType != 'manga').toList(); + return _ArtworkStrip( + title: '插画', + items: illusts, + loading: snapshot.connectionState == ConnectionState.waiting, + emptyText: '今天的插画还在路上', + emptyMessage: '同步新作品后,这里会开出第一朵小花。', + ); + }, + ), + const SizedBox(height: 18), + FutureBuilder>( + future: _todayFuture, + builder: (context, snapshot) { + final items = snapshot.data ?? []; + final mangas = + items.where((item) => item.contentType == 'manga').toList(); + return _ArtworkStrip( + title: '漫画', + items: mangas, + loading: snapshot.connectionState == ConnectionState.waiting, + emptyText: '今天还没有新漫画', + emptyMessage: '漫画同步完成后,会按页整理好放在这里。', + ); + }, + ), + const SizedBox(height: 18), + FutureBuilder>( + future: _novelFuture, + builder: (context, snapshot) => _NovelStrip( + novels: snapshot.data ?? [], + loading: snapshot.connectionState == ConnectionState.waiting, + ), + ), + const SizedBox(height: 18), + FutureBuilder>>( + future: _alertFuture, + builder: (context, snapshot) { + final alerts = snapshot.data ?? >[]; + final xAlerts = alerts.where(_isXAlert).toList(); + return _AlertStrip( + alerts: xAlerts.isEmpty ? alerts : xAlerts, + loading: snapshot.connectionState == ConnectionState.waiting, + ); + }, + ), + const SizedBox(height: 26), + const _SectionHeader( + title: '最近浏览', + subtitle: '保留上次看过的内容,想接着翻也方便。', + ), + const SizedBox(height: 12), + FutureBuilder>( + future: _recentFuture, + builder: (context, snapshot) => _ArtworkStrip( + title: '刚刚看过', + items: snapshot.data ?? [], + loading: snapshot.connectionState == ConnectionState.waiting, + emptyText: '还没有浏览记录', + emptyMessage: '打开一张图或一页漫画后,它会回到这里。', + ), + ), + const SizedBox(height: 22), + SizedBox( + height: 106, + child: FutureBuilder>( + future: _statFuture, + builder: (context, snapshot) => + _StatCard(stat: snapshot.data ?? const {}), + ), + ), + ], + ), + ); + } + + Future<_TodaySummary> _loadTodaySummary() async { + final results = await Future.wait([ + _todayFuture ?? Future.value([]), + _novelFuture ?? Future.value([]), + _alertFuture ?? Future.value(>[]), + ]); + final artworks = results[0] as List; + final novels = results[1] as List; + final alerts = results[2] as List>; + final illusts = + artworks.where((item) => item.contentType != 'manga').length; + final mangas = artworks.where((item) => item.contentType == 'manga').length; + final todayCount = [ + ...artworks.map((item) => item.createdAt ?? item.updatedAt), + ...novels.map((item) => item.lastViewedAt ?? item.lastReadAt), + ...alerts.map((item) => _parseDate(item['created_at'])), + ].whereType().where(_isToday).length; + return _TodaySummary( + total: todayCount, + illustCount: illusts, + mangaCount: mangas, + novelCount: novels.length, + alertCount: alerts.where(_isXAlert).length, + ); + } + + Future> _loadUpdatedArtworks(SakuraApiClient api) async { + final rows = + await api.gallery(sort: 'updated', limit: 36, includeTotal: false); + return _artworksFromPayload(rows)..sort(_compareArtworkUpdateDesc); + } + + Future> _loadRecentArtworks(SakuraApiClient api) async { + try { + final rows = await api.gallery(limit: 24, includeTotal: false); + final items = _artworksFromPayload(rows); + final viewed = items + .where((item) => item.lastViewedAt != null) + .toList(growable: false) + ..sort(_compareArtworkRecentDesc); + if (viewed.isNotEmpty) { + return viewed; + } + return items..sort(_compareArtworkRecentDesc); + } catch (_) { + return api + .galleryFeed(mode: 'recent', limit: 24) + .then(_artworksFromPayload); + } + } +} + +List _artworksFromPayload(List items) { + return items + .map((item) => Artwork.fromJson(item as Map)) + .toList(); +} + +class _TodaySummary { + const _TodaySummary({ + required this.total, + required this.illustCount, + required this.mangaCount, + required this.novelCount, + required this.alertCount, + }); + + static const empty = _TodaySummary( + total: 0, + illustCount: 0, + mangaCount: 0, + novelCount: 0, + alertCount: 0, + ); + + final int total; + final int illustCount; + final int mangaCount; + final int novelCount; + final int alertCount; +} + +class _TodayUpdateCard extends StatelessWidget { + const _TodayUpdateCard({required this.summary}); + + final _TodaySummary summary; + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(18), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + const CircleAvatar( + backgroundColor: Color(0xFFFFE1EA), + child: Icon(Icons.auto_awesome, color: Color(0xFFD95A83)), + ), + const SizedBox(width: 12), + Expanded( + child: Text( + summary.total > 0 + ? '今日新增 ${summary.total} 个作品' + : '今天还在安静等待更新', + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + ), + ], + ), + const SizedBox(height: 14), + Wrap( + spacing: 8, + runSpacing: 8, + children: [ + _UpdatePill(label: '插画 ${summary.illustCount}'), + _UpdatePill(label: '漫画 ${summary.mangaCount}'), + _UpdatePill(label: '小说 ${summary.novelCount}'), + _UpdatePill(label: 'X 动态/视频 ${summary.alertCount}'), + ], + ), + ], + ), + ), + ); + } +} + +class _UpdatePill extends StatelessWidget { + const _UpdatePill({required this.label}); + + final String label; + + @override + Widget build(BuildContext context) { + return DecoratedBox( + decoration: BoxDecoration( + color: const Color(0xFFFFEFF5), + borderRadius: BorderRadius.circular(999), + ), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 7), + child: Text( + label, + style: Theme.of(context).textTheme.labelMedium?.copyWith( + color: const Color(0xFFD95A83), + fontWeight: FontWeight.w700, + ), + ), + ), + ); + } +} + +class _SectionHeader extends StatelessWidget { + const _SectionHeader({required this.title, required this.subtitle}); + + final String title; + final String subtitle; + + @override + Widget build(BuildContext context) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + title, + style: Theme.of(context).textTheme.titleLarge?.copyWith( + fontWeight: FontWeight.w600, + ), + ), + const SizedBox(height: 4), + Text( + subtitle, + style: Theme.of(context) + .textTheme + .bodyMedium + ?.copyWith(color: const Color(0xFF7B6570)), + ), + ], + ); + } +} + +class _StatCard extends StatelessWidget { + const _StatCard({required this.stat}); + + final Map stat; + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(18), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceAround, + children: [ + _Metric(label: '作品', value: '${stat['total'] ?? '--'}'), + _Metric(label: '收藏', value: '${stat['favorites'] ?? '--'}'), + _Metric( + label: '空间', + value: stat['storage_used'] == null + ? '--' + : '${((stat['storage_used'] ?? 0) / 1024 / 1024).toStringAsFixed(1)} MB', + ), + ], + ), + ), + ); + } +} + +class _Metric extends StatelessWidget { + const _Metric({required this.label, required this.value}); + + final String label; + final String value; + + @override + Widget build(BuildContext context) { + return Column( + children: [ + Text( + value, + style: Theme.of(context).textTheme.titleLarge?.copyWith( + fontWeight: FontWeight.w800, + ), + ), + const SizedBox(height: 4), + Text(label, style: Theme.of(context).textTheme.bodySmall), + ], + ); + } +} + +class _ArtworkStrip extends ConsumerWidget { + const _ArtworkStrip({ + required this.title, + required this.items, + required this.loading, + required this.emptyText, + required this.emptyMessage, + }); + + final String title; + final List items; + final bool loading; + final String emptyText; + final String emptyMessage; + + @override + Widget build(BuildContext context, WidgetRef ref) { + final api = ref.watch(apiClientProvider); + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + title, + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 10), + if (loading) + const _SkeletonStrip() + else if (items.isEmpty) + _MessageCard( + icon: Icons.image_outlined, + title: emptyText, + message: emptyMessage, + ) + else + SizedBox( + height: 210, + child: ListView.separated( + scrollDirection: Axis.horizontal, + itemCount: items.take(10).length, + separatorBuilder: (_, __) => const SizedBox(width: 12), + itemBuilder: (context, index) { + final item = items[index]; + final thumbUrl = api.resolveUrl( + item.thumbnailUrl ?? item.imageUrl ?? item.thumbnailPath, + ); + return SizedBox( + width: 148, + child: InkWell( + borderRadius: BorderRadius.circular(22), + onTap: () { + final mangaCollection = item.contentType == 'manga' + ? findMangaCollectionForArtwork(items, item) + : null; + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => ArtworkDetailPage( + artwork: item, + artworkSequence: mangaCollection?.items, + titleOverride: mangaCollection?.title, + initialIndex: mangaCollection?.items.indexWhere( + (entry) => entry.id == item.id, + ), + ), + ), + ); + }, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ClipRRect( + borderRadius: BorderRadius.circular(22), + child: thumbUrl.isEmpty + ? _ImagePlaceholder(title: item.title) + : CachedNetworkImage( + imageUrl: thumbUrl, + httpHeaders: api.authHeaders(), + fit: BoxFit.cover, + memCacheWidth: 320, + memCacheHeight: 420, + placeholder: (_, __) => + _ImagePlaceholder(title: item.title), + errorWidget: (_, __, ___) => + _ImagePlaceholder(title: item.title), + ), + ), + ), + const SizedBox(height: 8), + Text( + item.title, + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + ], + ), + ), + ); + }, + ), + ), + ], + ); + } +} + +class _NovelStrip extends StatelessWidget { + const _NovelStrip({required this.novels, required this.loading}); + + final List novels; + final bool loading; + + @override + Widget build(BuildContext context) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '小说', + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 10), + if (loading) + const _SkeletonCard(height: 96) + else if (novels.isEmpty) + const _MessageCard( + icon: Icons.menu_book_outlined, + title: '今天还没有新小说', + message: '同步完成后,新的章节会在这里排好队。', + ) + else + ...novels.take(4).map((novel) => _RecentNovelTile(novel: novel)), + ], + ); + } +} + +class _AlertStrip extends StatelessWidget { + const _AlertStrip({required this.alerts, required this.loading}); + + final List> alerts; + final bool loading; + + @override + Widget build(BuildContext context) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Expanded( + child: Text( + 'X 动态 / 视频', + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + ), + TextButton.icon( + onPressed: () { + Navigator.of(context).push( + MaterialPageRoute(builder: (_) => const ReminderCenterPage()), + ); + }, + icon: const Icon(Icons.tune_outlined), + label: const Text('提醒中心'), + ), + ], + ), + const SizedBox(height: 10), + if (loading) + const _SkeletonCard(height: 96) + else if (alerts.isEmpty) + const _MessageCard( + icon: Icons.notifications_active_outlined, + title: '还没有新的动态提醒', + message: '关注作者更新后,X 动态和视频提醒会出现在这里。', + ) + else + ...alerts.take(4).map((item) => _AlertCard(item: item)), + ], + ); + } +} + +class _AlertCard extends StatelessWidget { + const _AlertCard({required this.item}); + + final Map item; + + @override + Widget build(BuildContext context) { + final title = item['title']?.toString() ?? '新的动态'; + final body = item['body']?.toString() ?? '你关注的作者有更新了。'; + final platform = item['platform']?.toString() ?? ''; + return Card( + margin: const EdgeInsets.only(bottom: 10), + child: ListTile( + leading: const CircleAvatar( + backgroundColor: Color(0xFFFFE1EA), + child: Icon( + Icons.notifications_active_outlined, + color: Color(0xFFD95A83), + ), + ), + title: Text(title, maxLines: 1, overflow: TextOverflow.ellipsis), + subtitle: Text( + [platform, body].where((item) => item.isNotEmpty).join(' - '), + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + trailing: const Icon(Icons.chevron_right), + onTap: () { + Navigator.of(context).push( + MaterialPageRoute(builder: (_) => const ReminderCenterPage()), + ); + }, + ), + ); + } +} + +class _RecentNovelTile extends ConsumerWidget { + const _RecentNovelTile({required this.novel}); + + final Novel novel; + + @override + Widget build(BuildContext context, WidgetRef ref) { + final api = ref.watch(apiClientProvider); + return Card( + margin: const EdgeInsets.only(bottom: 10), + child: ListTile( + contentPadding: + const EdgeInsets.symmetric(horizontal: 16, vertical: 10), + leading: SizedBox( + width: 48, + child: ClipRRect( + borderRadius: BorderRadius.circular(14), + child: _NovelCover( + coverUrl: api.resolveUrl(novel.coverPath), + title: novel.title, + ), + ), + ), + title: Text( + novel.seriesTitle?.isNotEmpty == true + ? novel.seriesTitle! + : novel.title, + ), + subtitle: Text( + novel.chapterTitle?.isNotEmpty == true + ? novel.chapterTitle! + : (novel.author ?? '未知作者'), + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + trailing: const Icon(Icons.chevron_right), + onTap: () => Navigator.of(context).push( + MaterialPageRoute(builder: (_) => NovelReaderPage(novel: novel)), + ), + ), + ); + } +} + +class _NovelCover extends StatelessWidget { + const _NovelCover({required this.coverUrl, required this.title}); + + final String coverUrl; + final String title; + + @override + Widget build(BuildContext context) { + if (coverUrl.isEmpty) { + return _ImagePlaceholder(title: title, icon: Icons.menu_book_outlined); + } + return CachedNetworkImage( + imageUrl: coverUrl, + fit: BoxFit.cover, + memCacheWidth: 160, + memCacheHeight: 220, + placeholder: (_, __) => + _ImagePlaceholder(title: title, icon: Icons.menu_book_outlined), + errorWidget: (_, __, ___) => + _ImagePlaceholder(title: title, icon: Icons.menu_book_outlined), + ); + } +} + +class _ImagePlaceholder extends StatelessWidget { + const _ImagePlaceholder( + {required this.title, this.icon = Icons.image_outlined}); + + final String title; + final IconData icon; + + @override + Widget build(BuildContext context) { + return Container( + color: const Color(0xFFFFE8EE), + alignment: Alignment.center, + child: Icon(icon, color: const Color(0xFFD95A83)), + ); + } +} + +class _MessageCard extends StatelessWidget { + const _MessageCard({ + required this.icon, + required this.title, + required this.message, + }); + + final IconData icon; + final String title; + final String message; + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(18), + child: Column( + children: [ + Icon(icon, color: const Color(0xFFD95A83)), + const SizedBox(height: 10), + Text( + title, + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + ), + textAlign: TextAlign.center, + ), + const SizedBox(height: 6), + Text( + message, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.5, + ), + textAlign: TextAlign.center, + ), + ], + ), + ), + ); + } +} + +class _SkeletonStrip extends StatelessWidget { + const _SkeletonStrip(); + + @override + Widget build(BuildContext context) { + return SizedBox( + height: 210, + child: ListView.separated( + scrollDirection: Axis.horizontal, + physics: const NeverScrollableScrollPhysics(), + itemCount: 3, + separatorBuilder: (_, __) => const SizedBox(width: 12), + itemBuilder: (_, __) => const SizedBox( + width: 148, + child: _SkeletonCard(height: 210), + ), + ), + ); + } +} + +class _SkeletonCard extends StatelessWidget { + const _SkeletonCard({required this.height}); + + final double height; + + @override + Widget build(BuildContext context) { + return Container( + height: height, + decoration: BoxDecoration( + color: const Color(0xFFFFE8EE), + borderRadius: BorderRadius.circular(18), + ), + ); + } +} + +int _compareArtworkUpdateDesc(Artwork a, Artwork b) { + final aTime = a.createdAt ?? a.updatedAt ?? a.lastViewedAt; + final bTime = b.createdAt ?? b.updatedAt ?? b.lastViewedAt; + if (aTime != null && bTime != null) { + return bTime.compareTo(aTime); + } + if (aTime != null) { + return -1; + } + if (bTime != null) { + return 1; + } + return a.title.compareTo(b.title); +} + +int _compareArtworkRecentDesc(Artwork a, Artwork b) { + final aTime = a.lastViewedAt ?? a.updatedAt ?? a.createdAt; + final bTime = b.lastViewedAt ?? b.updatedAt ?? b.createdAt; + if (aTime != null && bTime != null) { + return bTime.compareTo(aTime); + } + if (aTime != null) return -1; + if (bTime != null) return 1; + return a.title.compareTo(b.title); +} + +DateTime? _parseDate(Object? value) { + return DateTime.tryParse(value?.toString() ?? ''); +} + +bool _isToday(DateTime value) { + final local = value.toLocal(); + final now = DateTime.now(); + return local.year == now.year && + local.month == now.month && + local.day == now.day; +} + +bool _isXAlert(Map item) { + final platform = item['platform']?.toString().toLowerCase() ?? ''; + final source = item['source_platform']?.toString().toLowerCase() ?? ''; + return platform == 'x' || platform == 'twitter' || source == 'x'; +} diff --git a/sakura_mobile/lib/src/pages/tabs/gallery_tab.dart b/sakura_mobile/lib/src/pages/tabs/gallery_tab.dart new file mode 100644 index 0000000..c0a80e0 --- /dev/null +++ b/sakura_mobile/lib/src/pages/tabs/gallery_tab.dart @@ -0,0 +1,1270 @@ +import 'package:cached_network_image/cached_network_image.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../../core/manga_collections.dart'; +import '../../core/online_shelf.dart'; +import '../../core/providers.dart'; +import '../../core/unified_search.dart'; +import '../../models/artwork.dart'; +import '../artwork_detail_page.dart'; +import '../explore_page.dart'; +import '../online_book_detail_page.dart'; +import '../../widgets/sakura_search_bar.dart'; + +class GalleryTab extends ConsumerStatefulWidget { + const GalleryTab({ + super.key, + this.pageTitle = '图片', + this.fixedContentType, + this.showTypeFilters = true, + }); + + final String pageTitle; + final String? fixedContentType; + final bool showTypeFilters; + + @override + ConsumerState createState() => _GalleryTabState(); +} + +class _GalleryTabState extends ConsumerState { + final search = TextEditingController(); + final _scrollController = ScrollController(); + bool favoritesOnly = false; + bool recentUpdatesOnly = true; + String contentType = ''; + String searchScope = 'all'; + bool selectionMode = false; + final Set selectedIds = {}; + static const _pageSize = 30; + final List _items = []; + int _page = 1; + bool _hasMore = true; + bool _loadingMore = false; + late Future> future = _load(); + + bool get _isMangaShelfMode => _effectiveContentType == 'manga'; + + String? get _effectiveContentType { + if (widget.fixedContentType != null && + widget.fixedContentType!.trim().isNotEmpty) { + return widget.fixedContentType!.trim(); + } + return contentType.trim().isEmpty ? null : contentType.trim(); + } + + // ignore: unused_element + String get _allChipLabel { + switch (_effectiveContentType ?? widget.fixedContentType) { + case 'manga': + return '全部漫画'; + case 'illust': + return '全部插画'; + default: + return '全部'; + } + } + + String get _emptyTitle { + if (_effectiveContentType == 'manga') { + return '还没有同步漫画'; + } + if (_effectiveContentType == 'illust') { + return '还没有同步插画'; + } + return '还没有同步图片'; + } + + String get _selectionHint { + if (selectedIds.isEmpty) { + return '长按卡片开始多选,支持批量下载和删除。'; + } + return '已选 ${selectedIds.length} 项,可以继续点选调整。'; + } + + @override + void initState() { + super.initState(); + _scrollController.addListener(_onScroll); + } + + @override + void dispose() { + _scrollController.dispose(); + search.dispose(); + super.dispose(); + } + + @override + void didUpdateWidget(covariant GalleryTab oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.fixedContentType != widget.fixedContentType || + oldWidget.pageTitle != widget.pageTitle || + oldWidget.showTypeFilters != widget.showTypeFilters) { + setState(() { + if (widget.fixedContentType?.trim().isNotEmpty ?? false) { + contentType = ''; + } + selectionMode = false; + selectedIds.clear(); + future = _load(); + }); + } + } + + Future> _load({bool reset = true}) async { + if (reset) { + _page = 1; + _hasMore = true; + _items.clear(); + selectedIds.clear(); + selectionMode = false; + } + final rows = await ref.read(apiClientProvider).gallery( + search: search.text.trim().isEmpty ? null : search.text.trim(), + searchScope: searchScope, + isFavorite: favoritesOnly ? true : null, + contentType: _effectiveContentType, + sort: recentUpdatesOnly ? 'updated' : null, + page: _page, + limit: _pageSize, + includeTotal: false, + ); + final items = rows + .map((item) => Artwork.fromJson(item as Map)) + .toList(); + items.sort(_compareArtworkNewestFirst); + _hasMore = items.length >= _pageSize; + if (reset) { + _items + ..clear() + ..addAll(items); + } else { + _items.addAll(items); + } + return List.unmodifiable(_items); + } + + void _onScroll() { + if (!_scrollController.hasClients || !_hasMore || _loadingMore) { + return; + } + final position = _scrollController.position; + if (position.pixels >= position.maxScrollExtent - 420) { + _loadMore(); + } + } + + Future _loadMore() async { + if (_loadingMore || !_hasMore) { + return; + } + setState(() { + _loadingMore = true; + }); + try { + _page += 1; + await _load(reset: false); + if (!mounted) { + return; + } + setState(() { + future = Future.value(List.unmodifiable(_items)); + }); + } catch (_) { + _page = _page > 1 ? _page - 1 : 1; + } finally { + if (mounted) { + setState(() { + _loadingMore = false; + }); + } + } + } + + Future _refresh() async { + await ref.read(apiClientProvider).invalidateCache(); + final f = _load(); + setState(() { + future = f; + }); + await f; + } + + void _openOnlineSearch() { + if (!_isMangaShelfMode) { + return; + } + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const ExplorePage( + initialMode: UnifiedSearchMode.manga, + ), + ), + ); + } + + void _exitSelectionMode() { + setState(() { + selectionMode = false; + selectedIds.clear(); + }); + } + + void _toggleSelection(Artwork item) { + setState(() { + selectionMode = true; + if (!selectedIds.add(item.id)) { + selectedIds.remove(item.id); + } + if (selectedIds.isEmpty) { + selectionMode = false; + } + }); + } + + void _toggleSelectionForIds(Iterable ids) { + final idSet = ids.toSet(); + if (idSet.isEmpty) { + return; + } + setState(() { + selectionMode = true; + final fullySelected = idSet.every(selectedIds.contains); + if (fullySelected) { + selectedIds.removeAll(idSet); + } else { + selectedIds.addAll(idSet); + } + if (selectedIds.isEmpty) { + selectionMode = false; + } + }); + } + + Future _openArtwork(Artwork item, {List? allArtworks}) async { + if (selectionMode) { + _toggleSelection(item); + return; + } + final mangaCollection = item.contentType == 'manga' && allArtworks != null + ? findMangaCollectionForArtwork(allArtworks, item) + : null; + final changed = await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => ArtworkDetailPage( + artwork: item, + artworkSequence: mangaCollection?.items, + titleOverride: mangaCollection?.title, + initialIndex: + mangaCollection?.items.indexWhere((entry) => entry.id == item.id), + ), + ), + ); + if (changed == true && mounted) { + _refresh(); + } + } + + Future _openMangaEntry(MangaCollection entry) async { + if (selectionMode) { + _toggleSelectionForIds(entry.items.map((item) => item.id)); + return; + } + final changed = await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => ArtworkDetailPage( + artwork: entry.items[entry.preferredStartIndex], + artworkSequence: entry.items, + titleOverride: entry.title, + initialIndex: entry.preferredStartIndex, + ), + ), + ); + if (changed == true && mounted) { + _refresh(); + } + } + + Future _openOnlineMangaEntry(OnlineShelfEntry entry) async { + final source = ref.read(sourceManagerProvider).sourceById(entry.sourceId); + if (source == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('原漫画源已被删除,请重新导入后再阅读。')), + ); + return; + } + await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => OnlineBookDetailPage( + source: source, + detailUrl: entry.detailUrl, + initialTitle: entry.title, + initialAuthor: entry.author, + initialCoverUrl: entry.coverUrl, + ), + ), + ); + } + + Future _toggleFavorite(Artwork item) async { + try { + final api = ref.read(apiClientProvider); + await (item.isFavorite + ? api.unfavoriteArtwork(item.id) + : api.favoriteArtwork(item.id)); + if (!mounted) { + return; + } + _refresh(); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('收藏操作失败,请稍后再试。')), + ); + } + } + + Future _deleteSelected() async { + if (selectedIds.isEmpty) { + return; + } + final confirmed = await showDialog( + context: context, + builder: (context) => AlertDialog( + title: const Text('批量删除'), + content: Text('确定删除已选中的 ${selectedIds.length} 项内容吗?'), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: const Text('取消'), + ), + FilledButton( + onPressed: () => Navigator.of(context).pop(true), + child: const Text('删除'), + ), + ], + ), + ); + if (confirmed != true || !mounted) { + return; + } + + try { + final deleted = await ref + .read(apiClientProvider) + .batchDeleteArtworks(selectedIds.toList(growable: false)); + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('已删除 $deleted 项内容。')), + ); + _exitSelectionMode(); + _refresh(); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('批量删除失败,请稍后再试。')), + ); + } + } + + Future _downloadSelected(List items) async { + final selected = + items.where((item) => selectedIds.contains(item.id)).toList(); + if (selected.isEmpty) { + return; + } + final api = ref.read(apiClientProvider); + var saved = 0; + try { + for (final item in selected) { + await api.downloadProtectedFile( + item.imageUrl ?? item.thumbnailUrl ?? item.thumbnailPath, + fileName: '${item.title}_${item.id}', + subdirectory: item.isVideo + ? 'videos' + : item.contentType == 'manga' + ? 'manga' + : 'images', + ); + saved += 1; + } + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('已下载 $saved 项到系统图库。')), + ); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('下载失败,请稍后再试。')), + ); + } + } + + @override + Widget build(BuildContext context) { + final api = ref.watch(apiClientProvider); + final onlineShelf = ref.watch(onlineShelfProvider); + final onlineQuery = search.text.trim().toLowerCase(); + final onlineMangaEntries = onlineShelf.entries.where((entry) { + if (!entry.isManga) return false; + if (onlineQuery.isEmpty) return true; + return '${entry.title} ${entry.author ?? ''} ${entry.sourceName}' + .toLowerCase() + .contains(onlineQuery); + }).toList(growable: false); + + return RefreshIndicator( + color: const Color(0xFFD95A83), + onRefresh: _refresh, + child: FutureBuilder>( + future: future, + builder: (context, snapshot) { + final items = snapshot.data ?? _items; + final mangaEntries = _isMangaShelfMode + ? buildMangaCollections(items) + : const []; + return CustomScrollView( + controller: _scrollController, + physics: const AlwaysScrollableScrollPhysics(), + slivers: [ + SliverAppBar( + pinned: true, + title: Text( + selectionMode + ? '已选 ${selectedIds.length} 项' + : (_isMangaShelfMode ? '漫画' : '图库'), + style: const TextStyle( + fontWeight: FontWeight.w700, + fontSize: 20, + ), + ), + actions: [ + if (!selectionMode) + IconButton( + tooltip: '多选管理', + onPressed: () { + setState(() { + selectionMode = true; + }); + }, + icon: const Icon(Icons.checklist_rounded), + ), + ], + bottom: PreferredSize( + preferredSize: Size.fromHeight(selectionMode ? 224 : 178), + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 12), + child: Column( + children: [ + SakuraSearchBar( + controller: search, + hintText: _isMangaShelfMode + ? '搜索漫画标题、作者或页名' + : '搜索图片标题、作者或标签', + onExploreTap: + _isMangaShelfMode ? _openOnlineSearch : null, + onSubmitted: (_) => _refresh(), + onRefreshTap: _refresh, + ), + const SizedBox(height: 8), + Row( + children: [ + ChoiceChip( + label: Text( + _isMangaShelfMode + ? '漫画 ${mangaEntries.length + onlineMangaEntries.length}' + : '图库 ${items.length}', + ), + selected: true, + onSelected: (_) {}, + ), + const SizedBox(width: 8), + ChoiceChip( + label: const Text('收藏'), + selected: favoritesOnly, + onSelected: (_) { + setState(() { + favoritesOnly = !favoritesOnly; + recentUpdatesOnly = false; + future = _load(); + }); + }, + ), + if (widget.showTypeFilters && + widget.fixedContentType == null) ...[ + const SizedBox(width: 8), + ChoiceChip( + label: const Text('鎻掔敾'), + selected: contentType == 'illust', + onSelected: (_) { + setState(() { + contentType = contentType == 'illust' + ? '' + : 'illust'; + future = _load(); + }); + }, + ), + const SizedBox(width: 8), + ChoiceChip( + label: const Text('视频'), + selected: contentType == 'video', + onSelected: (_) { + setState(() { + contentType = + contentType == 'video' ? '' : 'video'; + future = _load(); + }); + }, + ), + ], + ], + ), + if (selectionMode) ...[ + const SizedBox(height: 10), + _SelectionPanel( + message: _selectionHint, + onCancel: _exitSelectionMode, + onDownload: selectedIds.isEmpty + ? null + : () => _downloadSelected(items), + onDelete: + selectedIds.isEmpty ? null : _deleteSelected, + ), + ], + const SizedBox(height: 10), + Align( + alignment: Alignment.centerLeft, + child: Text( + _isMangaShelfMode + ? '按分组连续阅读漫画,支持收藏和批量管理。' + : '浏览云端同步的插画与图片,支持收藏和批量管理。', + style: + Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7C8087), + ), + ), + ), + ], + ), + ), + ), + ), + if (snapshot.hasData && + _isMangaShelfMode && + onlineMangaEntries.isNotEmpty) + SliverToBoxAdapter( + child: _OnlineMangaShelfSection( + entries: onlineMangaEntries, + onOpen: _openOnlineMangaEntry, + onRemove: (entry) => + ref.read(onlineShelfProvider).remove(entry.id), + ), + ), + if (snapshot.hasError) + const SliverFillRemaining( + child: _EmptyState( + icon: Icons.cloud_off_outlined, + title: '图片加载失败', + message: '云端接口暂时不可用,请稍后再试。', + ), + ) + else if (!snapshot.hasData) + const SliverFillRemaining( + child: Center(child: CircularProgressIndicator()), + ) + else if ((_isMangaShelfMode + ? mangaEntries.isEmpty && onlineMangaEntries.isEmpty + : items.isEmpty)) + SliverFillRemaining( + child: _EmptyState( + icon: _effectiveContentType == 'manga' + ? Icons.menu_book_rounded + : Icons.collections_bookmark_outlined, + title: _emptyTitle, + message: '同步内容后,这里会自动更新,你也可以切换筛选条件继续整理。', + ), + ) + else if (_isMangaShelfMode) + SliverPadding( + padding: const EdgeInsets.fromLTRB(12, 14, 12, 16), + sliver: SliverList( + delegate: SliverChildBuilderDelegate( + (context, index) { + final entry = mangaEntries[index]; + final thumbUrl = api.resolveUrl( + entry.cover.thumbnailUrl ?? entry.cover.thumbnailPath, + ); + final selected = entry.items + .every((item) => selectedIds.contains(item.id)); + return Padding( + padding: EdgeInsets.only( + bottom: + index == mangaEntries.length - 1 ? 0 : 12), + child: Card( + clipBehavior: Clip.antiAlias, + child: InkWell( + onTap: () => _openMangaEntry(entry), + onLongPress: () => _toggleSelectionForIds( + entry.items.map((item) => item.id), + ), + child: Container( + decoration: selected + ? BoxDecoration( + border: Border.all( + color: const Color(0xFFD95A83), + width: 2, + ), + ) + : null, + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + SizedBox( + width: 116, + height: 152, + child: thumbUrl.isEmpty + ? Container( + color: const Color(0xFFFFE8EE), + child: const Center( + child: Icon( + Icons.menu_book_rounded), + ), + ) + : CachedNetworkImage( + imageUrl: thumbUrl, + httpHeaders: api.authHeaders(), + fit: BoxFit.cover, + memCacheWidth: 320, + memCacheHeight: 420, + ), + ), + Expanded( + child: Padding( + padding: const EdgeInsets.fromLTRB( + 14, 14, 12, 12), + child: Column( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Row( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Expanded( + child: Text( + entry.title, + maxLines: 2, + overflow: + TextOverflow.ellipsis, + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith( + fontWeight: + FontWeight.w700, + ), + ), + ), + const SizedBox(width: 8), + if (selectionMode) + IconButton.filled( + style: IconButton.styleFrom( + backgroundColor: selected + ? const Color( + 0xFFD95A83) + : Colors.white, + ), + onPressed: () => + _toggleSelectionForIds( + entry.items.map( + (item) => item.id), + ), + icon: Icon( + selected + ? Icons.check_rounded + : Icons + .radio_button_unchecked, + color: selected + ? Colors.white + : const Color( + 0xFFD95A83), + ), + ) + else + IconButton.filledTonal( + onPressed: () => + _toggleFavorite( + entry.cover), + icon: Icon( + entry.cover.isFavorite + ? Icons.favorite + : Icons + .favorite_border, + ), + ), + ], + ), + const SizedBox(height: 6), + Text( + entry.author?.isNotEmpty == true + ? entry.author! + : entry.cover.sourcePlatform, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context) + .textTheme + .bodySmall + ?.copyWith( + color: + const Color(0xFF7B6570), + ), + ), + const SizedBox(height: 10), + Wrap( + spacing: 8, + runSpacing: 8, + children: [ + _ShelfBadge( + label: + '${entry.items.length} 页', + ), + const _ShelfBadge( + label: '连续阅读'), + _ShelfBadge( + label: + entry.readProgressLabel, + ), + if (entry + .latestItemTitle.isNotEmpty) + _ShelfBadge( + label: + '当前 ${entry.latestItemTitle}', + ), + ], + ), + const SizedBox(height: 12), + Text( + entry.itemTitlesPreview, + maxLines: 2, + overflow: TextOverflow.ellipsis, + style: Theme.of(context) + .textTheme + .bodyMedium + ?.copyWith(height: 1.5), + ), + ], + ), + ), + ), + ], + ), + ), + ), + ), + ); + }, + childCount: mangaEntries.length, + ), + ), + ) + else + SliverPadding( + padding: const EdgeInsets.all(12), + sliver: SliverGrid.builder( + gridDelegate: + const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 2, + crossAxisSpacing: 12, + mainAxisSpacing: 12, + childAspectRatio: 0.72, + ), + itemCount: items.length, + itemBuilder: (context, index) { + final item = items[index]; + final thumbUrl = api + .resolveUrl(item.thumbnailUrl ?? item.thumbnailPath); + final mediaUrl = api.resolveUrl(item.primaryMediaPath); + final selected = selectedIds.contains(item.id); + return Card( + clipBehavior: Clip.antiAlias, + child: InkWell( + onTap: () => _openArtwork(item, allArtworks: items), + onLongPress: () => _toggleSelection(item), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: Stack( + fit: StackFit.expand, + children: [ + thumbUrl.isEmpty + ? Container( + color: const Color(0xFFFFE8EE), + child: const Center( + child: Icon(Icons.image_outlined), + ), + ) + : CachedNetworkImage( + imageUrl: thumbUrl, + httpHeaders: api.authHeaders(), + fit: BoxFit.cover, + memCacheWidth: 360, + memCacheHeight: 500, + ), + if (item.isVideo && thumbUrl.isEmpty && mediaUrl.isNotEmpty) + const Positioned.fill( + child: DecoratedBox( + decoration: BoxDecoration( + gradient: LinearGradient( + colors: [ + Color(0xFF251B23), + Color(0xFFD95A83), + ], + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ), + ), + ), + ), + if (item.isVideo) + Positioned.fill( + child: DecoratedBox( + decoration: BoxDecoration( + color: Colors.black + .withValues(alpha: 0.18), + ), + child: const Center( + child: Icon( + Icons.play_circle_fill_rounded, + color: Colors.white, + size: 48, + ), + ), + ), + ), + Positioned( + top: 10, + left: 10, + child: Chip( + label: Text( + item.isVideo + ? '视频' + : item.contentType == 'manga' + ? '漫画' + : '插画', + ), + backgroundColor: Colors.white + .withValues(alpha: 0.88), + ), + ), + if (selectionMode) + Positioned( + top: 8, + right: 8, + child: IconButton.filled( + style: IconButton.styleFrom( + backgroundColor: selected + ? const Color(0xFFD95A83) + : Colors.white + .withValues(alpha: 0.88), + ), + onPressed: () => + _toggleSelection(item), + icon: Icon( + selected + ? Icons.check_rounded + : Icons.radio_button_unchecked, + color: selected + ? Colors.white + : const Color(0xFFD95A83), + ), + ), + ) + else + Positioned( + top: 8, + right: 8, + child: IconButton.filledTonal( + onPressed: () => + _toggleFavorite(item), + icon: Icon( + item.isFavorite + ? Icons.favorite + : Icons.favorite_border, + ), + ), + ), + if (selected) + Positioned.fill( + child: DecoratedBox( + decoration: BoxDecoration( + color: const Color(0x55D95A83), + border: Border.all( + color: const Color(0xFFD95A83), + width: 2, + ), + ), + ), + ), + ], + ), + ), + Padding( + padding: + const EdgeInsets.fromLTRB(12, 10, 12, 4), + child: Text( + item.title, + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + ), + Padding( + padding: + const EdgeInsets.fromLTRB(12, 0, 12, 12), + child: Text( + item.author?.isNotEmpty == true + ? item.author! + : item.sourcePlatform, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context) + .textTheme + .bodySmall + ?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ), + ], + ), + ), + ); + }, + ), + ), + if (snapshot.hasData && + (_isMangaShelfMode + ? mangaEntries.isNotEmpty + : items.isNotEmpty)) + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 4, 16, 24), + child: Center( + child: _loadingMore + ? const CircularProgressIndicator() + : _hasMore + ? TextButton.icon( + onPressed: _loadMore, + icon: const Icon(Icons.expand_more_rounded), + label: const Text('加载更多'), + ) + : const Text('已经到底啦'), + ), + ), + ), + ], + ); + }, + ), + ); + } +} + +class _OnlineMangaShelfSection extends StatelessWidget { + const _OnlineMangaShelfSection({ + required this.entries, + required this.onOpen, + required this.onRemove, + }); + + final List entries; + final ValueChanged onOpen; + final ValueChanged onRemove; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.fromLTRB(16, 18, 16, 4), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('在线漫画', style: Theme.of(context).textTheme.titleMedium), + const SizedBox(height: 4), + Text( + '保存书源信息,打开时加载目录并使用原漫画阅读模式。', + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7C8087), + ), + ), + const SizedBox(height: 10), + SizedBox( + height: 166, + child: ListView.separated( + scrollDirection: Axis.horizontal, + itemCount: entries.length, + separatorBuilder: (_, __) => const SizedBox(width: 10), + itemBuilder: (context, index) { + final entry = entries[index]; + final cover = entry.coverUrl?.trim() ?? ''; + return SizedBox( + width: 300, + child: Card( + clipBehavior: Clip.antiAlias, + child: InkWell( + onTap: () => onOpen(entry), + child: Row( + children: [ + SizedBox( + width: 104, + height: double.infinity, + child: cover.startsWith('http') + ? CachedNetworkImage( + imageUrl: cover, + fit: BoxFit.cover, + errorWidget: (_, __, ___) => + const _OnlineMangaCover(), + ) + : const _OnlineMangaCover(), + ), + Expanded( + child: Padding( + padding: const EdgeInsets.fromLTRB(12, 12, 4, 8), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + entry.title, + maxLines: 2, + overflow: TextOverflow.ellipsis, + style: Theme.of(context) + .textTheme + .titleSmall + ?.copyWith(fontWeight: FontWeight.w700), + ), + const SizedBox(height: 6), + Text( + entry.author?.trim().isNotEmpty == true + ? entry.author! + : entry.sourceName, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context) + .textTheme + .bodySmall + ?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + const Spacer(), + Row( + children: [ + const Icon( + Icons.menu_book_rounded, + size: 18, + color: Color(0xFFD95A83), + ), + const SizedBox(width: 5), + const Expanded(child: Text('原源阅读')), + IconButton( + tooltip: '移出漫画', + onPressed: () => onRemove(entry), + icon: const Icon(Icons.delete_outline), + ), + ], + ), + ], + ), + ), + ), + ], + ), + ), + ), + ); + }, + ), + ), + ], + ), + ); + } +} + +class _OnlineMangaCover extends StatelessWidget { + const _OnlineMangaCover(); + + @override + Widget build(BuildContext context) { + return const ColoredBox( + color: Color(0xFFFFE8EE), + child: Center( + child: Icon(Icons.menu_book_rounded, color: Color(0xFFD95A83)), + ), + ); + } +} + +class _SelectionPanel extends StatelessWidget { + const _SelectionPanel({ + required this.message, + required this.onCancel, + required this.onDownload, + required this.onDelete, + }); + + final String message; + final VoidCallback onCancel; + final VoidCallback? onDownload; + final VoidCallback? onDelete; + + @override + Widget build(BuildContext context) { + return Container( + width: double.infinity, + padding: const EdgeInsets.all(12), + decoration: BoxDecoration( + color: const Color(0xFFFFF7FA), + borderRadius: BorderRadius.circular(18), + border: Border.all(color: const Color(0xFFF2D7E0)), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + message, + style: Theme.of(context).textTheme.bodySmall, + ), + const SizedBox(height: 10), + Wrap( + spacing: 8, + runSpacing: 8, + children: [ + TextButton.icon( + onPressed: onCancel, + icon: const Icon(Icons.close_rounded), + label: const Text('取消'), + ), + OutlinedButton.icon( + onPressed: onDownload, + icon: const Icon(Icons.download_rounded), + label: const Text('下载'), + ), + FilledButton.tonalIcon( + onPressed: onDelete, + icon: const Icon(Icons.delete_outline), + label: const Text('删除'), + ), + ], + ), + ], + ), + ); + } +} + +// ignore: unused_element +class _FilterChip extends StatelessWidget { + const _FilterChip({ + required this.label, + required this.selected, + required this.onTap, + }); + + final String label; + final bool selected; + final VoidCallback onTap; + + @override + Widget build(BuildContext context) { + return ChoiceChip( + label: Text(label), + selected: selected, + onSelected: (_) => onTap(), + ); + } +} + +class _EmptyState extends StatelessWidget { + const _EmptyState({ + required this.icon, + required this.title, + required this.message, + }); + + final IconData icon; + final String title; + final String message; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon(icon, size: 42, color: Theme.of(context).colorScheme.primary), + const SizedBox(height: 14), + Text(title, style: Theme.of(context).textTheme.titleLarge), + const SizedBox(height: 8), + Text( + message, + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.bodyMedium, + ), + ], + ), + ); + } +} + +class _ShelfBadge extends StatelessWidget { + const _ShelfBadge({required this.label}); + + final String label; + + @override + Widget build(BuildContext context) { + return Container( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6), + decoration: BoxDecoration( + color: const Color(0xFFFFEFF5), + borderRadius: BorderRadius.circular(999), + ), + child: Text( + label, + style: Theme.of(context).textTheme.labelMedium?.copyWith( + color: const Color(0xFFD95A83), + fontWeight: FontWeight.w700, + ), + ), + ); + } +} + +int _compareArtworkNewestFirst(Artwork a, Artwork b) { + final aTime = a.updatedAt ?? a.createdAt ?? a.lastViewedAt; + final bTime = b.updatedAt ?? b.createdAt ?? b.lastViewedAt; + if (aTime != null && bTime != null) { + return bTime.compareTo(aTime); + } + if (aTime != null) { + return -1; + } + if (bTime != null) { + return 1; + } + return a.title.compareTo(b.title); +} diff --git a/sakura_mobile/lib/src/pages/tabs/novels_tab.dart b/sakura_mobile/lib/src/pages/tabs/novels_tab.dart new file mode 100644 index 0000000..a558258 --- /dev/null +++ b/sakura_mobile/lib/src/pages/tabs/novels_tab.dart @@ -0,0 +1,1040 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../../core/providers.dart'; +import '../../core/online_shelf.dart'; +import '../../core/unified_search.dart'; +import '../../models/novel.dart'; +import '../explore_page.dart'; +import '../../widgets/sakura_search_bar.dart'; +import '../novel_detail_page.dart'; +import '../novel_reader_page.dart'; +import '../online_book_detail_page.dart'; + +class NovelsTab extends ConsumerStatefulWidget { + const NovelsTab({super.key}); + + @override + ConsumerState createState() => _NovelsTabState(); +} + +class _NovelsTabState extends ConsumerState { + final search = TextEditingController(); + bool selectionMode = false; + final Set selectedEntryIds = {}; + late Future<_NovelShelfData> future = _load(); + + @override + void dispose() { + search.dispose(); + super.dispose(); + } + + Future<_NovelShelfData> _load() async { + final api = ref.read(apiClientProvider); + final query = search.text.trim(); + final results = await Future.wait>([ + _guardShelfRequest(api.novels(search: query.isEmpty ? null : query)), + query.isEmpty + ? _guardShelfRequest(api.novelSeries(includeChapters: false)) + : Future.value([]), + query.isEmpty + ? _guardShelfRequest(api.continueReadingNovels()) + : Future.value([]), + ]); + + final novels = results[0] + .map((item) => Novel.fromJson(item as Map)) + .toList(); + final series = results[1] + .map((item) => NovelSeries.fromJson(item as Map)) + .toList(); + final continueNovels = results[2] + .map((item) => Novel.fromJson(item as Map)) + .toList(); + + return _NovelShelfData( + entries: _buildShelfEntries( + novels: novels, + series: series, + query: query, + ), + continueNovels: continueNovels.take(3).toList(), + ); + } + + Future> _guardShelfRequest(Future> request) { + return request + .timeout(const Duration(seconds: 6)) + .catchError((_) => []); + } + + List<_NovelShelfEntry> _buildShelfEntries({ + required List novels, + required List series, + required String query, + }) { + final entries = <_NovelShelfEntry>[ + ...series.map(_NovelShelfEntry.fromSeries), + ...novels + .where((item) => !(item.seriesId?.trim().isNotEmpty ?? false)) + .map(_NovelShelfEntry.fromSingleNovel), + ]; + + final normalizedQuery = query.trim().toLowerCase(); + final filtered = normalizedQuery.isEmpty + ? entries + : entries.where((entry) { + final text = [ + entry.title, + entry.author ?? '', + ...entry.chapters.map((item) => item.displayChapterTitle), + ].join(' ').toLowerCase(); + return text.contains(normalizedQuery); + }).toList(); + + filtered.sort((a, b) { + final bLast = b.lastReadAt?.millisecondsSinceEpoch ?? + b.lastViewedAt?.millisecondsSinceEpoch ?? + b.updatedAt.millisecondsSinceEpoch; + final aLast = a.lastReadAt?.millisecondsSinceEpoch ?? + a.lastViewedAt?.millisecondsSinceEpoch ?? + a.updatedAt.millisecondsSinceEpoch; + final delta = bLast.compareTo(aLast); + if (delta != 0) { + return delta; + } + return a.title.compareTo(b.title); + }); + return filtered; + } + + Future _refresh() async { + final f = _load(); + setState(() { + future = f; + }); + await f; + } + + void _openOnlineSearch() { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const ExplorePage( + initialMode: UnifiedSearchMode.novel, + ), + ), + ); + } + + void _clearSelection() { + setState(() { + selectionMode = false; + selectedEntryIds.clear(); + }); + } + + void _toggleSelection(_NovelShelfEntry entry) { + setState(() { + selectionMode = true; + if (!selectedEntryIds.add(entry.id)) { + selectedEntryIds.remove(entry.id); + } + if (selectedEntryIds.isEmpty) { + selectionMode = false; + } + }); + } + + Future _openEntry(_NovelShelfEntry entry) async { + if (selectionMode) { + _toggleSelection(entry); + return; + } + var chapters = entry.chapters; + if (chapters.isEmpty && entry.seriesId != null) { + try { + final detail = NovelSeries.fromJson( + await ref.read(apiClientProvider).novelSeriesDetail(entry.seriesId!), + ); + chapters = detail.chapters; + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('目录加载失败,请稍后再试。')), + ); + return; + } + } + if (chapters.isEmpty) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('这本小说暂时没有可阅读章节。')), + ); + return; + } + if (!mounted) { + return; + } + final changed = await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => NovelDetailPage( + title: entry.title, + author: entry.author, + chapters: chapters, + ), + ), + ); + if (changed == true && mounted) { + _refresh(); + } + } + + Future _openOnlineEntry(OnlineShelfEntry entry) async { + final source = ref.read(sourceManagerProvider).sourceById(entry.sourceId); + if (source == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('原书源已被删除,请重新导入后再阅读。')), + ); + return; + } + await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => OnlineBookDetailPage( + source: source, + detailUrl: entry.detailUrl, + initialTitle: entry.title, + initialAuthor: entry.author, + initialCoverUrl: entry.coverUrl, + ), + ), + ); + } + + Future _openContinueNovel(Novel novel) async { + final changed = await Navigator.of(context).push( + MaterialPageRoute(builder: (_) => NovelReaderPage(novel: novel)), + ); + if (changed == true && mounted) { + _refresh(); + } + } + + Future _deleteSelectedEntries(List<_NovelShelfEntry> entries) async { + if (selectedEntryIds.isEmpty) { + return; + } + final selected = + entries.where((entry) => selectedEntryIds.contains(entry.id)).toList(); + final novelIds = {}; + final seriesIds = {}; + for (final entry in selected) { + if (entry.seriesId != null && entry.chapterCount > 1) { + seriesIds.add(entry.seriesId!); + continue; + } + if (entry.chapters.isNotEmpty) { + novelIds.addAll(entry.chapters.map((item) => item.id)); + continue; + } + if (entry.seriesId != null) { + seriesIds.add(entry.seriesId!); + } + } + if (novelIds.isEmpty && seriesIds.isEmpty) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('没有可删除的小说章节,请稍后再试。')), + ); + return; + } + + final shelfCount = selected.length; + final chapterHint = seriesIds.isNotEmpty + ? '将删除 $shelfCount 个书架条目(系列会连同全部章节一起删除)。' + : '将删除 $shelfCount 个书架条目。'; + + final confirmed = await showDialog( + context: context, + builder: (context) => AlertDialog( + title: const Text('批量删除小说'), + content: Text('确定删除已选中的内容吗?\n$chapterHint'), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: const Text('取消'), + ), + FilledButton( + onPressed: () => Navigator.of(context).pop(true), + child: const Text('删除'), + ), + ], + ), + ); + if (confirmed != true || !mounted) { + return; + } + + try { + final deleted = await ref.read(apiClientProvider).batchDeleteNovels( + ids: novelIds.toList(growable: false), + seriesIds: seriesIds.toList(growable: false), + ); + if (!mounted) { + return; + } + if (deleted == 0) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('没有删除任何内容,可能已被移除。')), + ); + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('已删除 $deleted 个章节文件。')), + ); + _clearSelection(); + _refresh(); + } catch (_) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('批量删除失败,请稍后再试。')), + ); + } + } + + @override + Widget build(BuildContext context) { + final onlineShelf = ref.watch(onlineShelfProvider); + final query = search.text.trim().toLowerCase(); + final onlineEntries = onlineShelf.entries.where((entry) { + if (!entry.isNovel) return false; + if (query.isEmpty) return true; + return '${entry.title} ${entry.author ?? ''} ${entry.sourceName}' + .toLowerCase() + .contains(query); + }).toList(); + return RefreshIndicator( + color: const Color(0xFFD95A83), + onRefresh: _refresh, + child: FutureBuilder<_NovelShelfData>( + future: future, + builder: (context, snapshot) { + final data = snapshot.data ?? const _NovelShelfData(); + final entries = data.entries; + return CustomScrollView( + physics: const AlwaysScrollableScrollPhysics(), + slivers: [ + SliverAppBar( + pinned: true, + title: Text( + selectionMode ? '已选 ${selectedEntryIds.length} 项' : '系列书架', + style: const TextStyle( + fontWeight: FontWeight.w700, + fontSize: 20, + ), + ), + actions: [ + if (!selectionMode) + IconButton( + tooltip: '多选管理', + onPressed: () { + setState(() { + selectionMode = true; + }); + }, + icon: const Icon(Icons.checklist_rounded), + ), + ], + bottom: PreferredSize( + preferredSize: Size.fromHeight(selectionMode ? 224 : 178), + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 12), + child: Column( + children: [ + SakuraSearchBar( + controller: search, + hintText: '搜索书名、作者或章节名', + onExploreTap: _openOnlineSearch, + onSubmitted: (_) => _refresh(), + onRefreshTap: _refresh, + ), + const SizedBox(height: 8), + Row( + children: [ + ChoiceChip( + label: Text('云库 + 在线 ${onlineEntries.length}'), + selected: true, + onSelected: (_) {}, + ), + ], + ), + if (selectionMode) ...[ + const SizedBox(height: 10), + _NovelSelectionPanel( + message: selectedEntryIds.isEmpty + ? '长按卡片开始多选,支持批量删除。' + : '已选 ${selectedEntryIds.length} 项,可以继续点选调整。', + onCancel: _clearSelection, + onDelete: selectedEntryIds.isEmpty + ? null + : () => _deleteSelectedEntries(entries), + ), + ], + const SizedBox(height: 10), + Align( + alignment: Alignment.centerLeft, + child: Text( + '单章节直接进入正文,多章节显示为合集。', + style: + Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7C8087), + ), + ), + ), + ], + ), + ), + ), + ), + if (snapshot.hasError) + const SliverFillRemaining( + child: _NovelEmptyState( + icon: Icons.cloud_off_outlined, + title: '小说内容加载失败', + message: '云端接口暂时不可用,请稍后再试。', + ), + ) + else if (!snapshot.hasData) + const SliverFillRemaining( + child: Center(child: CircularProgressIndicator()), + ) + else if (entries.isEmpty && onlineEntries.isEmpty) + const SliverFillRemaining( + child: _NovelEmptyState( + icon: Icons.auto_stories_outlined, + title: '还没有小说', + message: '同步小说后,这里会自动显示系列或单章节书架。', + ), + ) + else ...[ + if (onlineEntries.isNotEmpty) + SliverToBoxAdapter( + child: _OnlineShelfSection( + entries: onlineEntries, + onOpen: _openOnlineEntry, + onRemove: (entry) => + ref.read(onlineShelfProvider).remove(entry.id), + ), + ), + if (data.continueNovels.isNotEmpty) + SliverToBoxAdapter( + child: _ContinueReadingSection( + novels: data.continueNovels, + onOpen: _openContinueNovel, + ), + ), + SliverPadding( + padding: const EdgeInsets.fromLTRB(16, 20, 16, 24), + sliver: SliverGrid.builder( + gridDelegate: + const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 3, + crossAxisSpacing: 12, + mainAxisSpacing: 18, + childAspectRatio: 0.62, + ), + itemCount: entries.length, + itemBuilder: (context, index) { + final entry = entries[index]; + return _ShelfEntryCard( + entry: entry, + paletteIndex: index, + selected: selectedEntryIds.contains(entry.id), + selectionMode: selectionMode, + onTap: () => _openEntry(entry), + onSelect: () => _toggleSelection(entry), + ); + }, + ), + ), + ], + ], + ); + }, + ), + ); + } +} + +class _OnlineShelfSection extends StatelessWidget { + const _OnlineShelfSection({ + required this.entries, + required this.onOpen, + required this.onRemove, + }); + + final List entries; + final ValueChanged onOpen; + final ValueChanged onRemove; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.fromLTRB(16, 18, 16, 0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('在线书架', style: Theme.of(context).textTheme.titleMedium), + const SizedBox(height: 4), + Text( + '这里只保存书籍信息,打开时才加载目录和正文。', + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7C8087), + ), + ), + const SizedBox(height: 8), + for (final entry in entries) + ListTile( + contentPadding: EdgeInsets.zero, + leading: ClipRRect( + borderRadius: BorderRadius.circular(6), + child: SizedBox( + width: 44, + height: 58, + child: entry.coverUrl?.trim().isNotEmpty == true + ? Image.network( + entry.coverUrl!, + fit: BoxFit.cover, + errorBuilder: (_, __, ___) => + const _OnlineShelfCover(), + ) + : const _OnlineShelfCover(), + ), + ), + title: Text(entry.title, + maxLines: 1, overflow: TextOverflow.ellipsis), + subtitle: Text( + [entry.author ?? '未知作者', entry.sourceName].join(' · '), + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + trailing: IconButton( + tooltip: '移出书架', + onPressed: () => onRemove(entry), + icon: const Icon(Icons.bookmark_remove_outlined), + ), + onTap: () => onOpen(entry), + ), + ], + ), + ); + } +} + +class _OnlineShelfCover extends StatelessWidget { + const _OnlineShelfCover(); + + @override + Widget build(BuildContext context) => Container( + color: const Color(0xFFFFECF2), + alignment: Alignment.center, + child: + const Icon(Icons.auto_stories_outlined, color: Color(0xFFD05A82)), + ); +} + +class _NovelShelfEntry { + const _NovelShelfEntry({ + required this.id, + required this.title, + required this.chapters, + required this.updatedAt, + required this.chapterCount, + this.author, + this.seriesId, + this.lastReadAt, + this.lastViewedAt, + }); + + final String id; + final String title; + final String? author; + final String? seriesId; + final DateTime? lastReadAt; + final DateTime? lastViewedAt; + final DateTime updatedAt; + final List chapters; + final int chapterCount; + + bool get isSeries => chapterCount > 1; + + factory _NovelShelfEntry.fromSeries(NovelSeries series) { + final updatedAt = series.chapters + .map((item) => item.lastViewedAt ?? item.lastReadAt) + .whereType() + .fold( + null, + (latest, current) => + latest == null || current.isAfter(latest) ? current : latest, + ); + return _NovelShelfEntry( + id: 'series:${series.seriesId}', + title: series.seriesTitle, + author: series.author, + seriesId: series.seriesId, + lastReadAt: series.lastReadAt, + lastViewedAt: series.lastViewedAt, + updatedAt: updatedAt ?? DateTime.fromMillisecondsSinceEpoch(0), + chapterCount: series.chapterCount, + chapters: series.chapters, + ); + } + + factory _NovelShelfEntry.fromSingleNovel(Novel novel) { + return _NovelShelfEntry( + id: 'novel:${novel.id}', + title: novel.displayChapterTitle, + author: novel.author, + lastReadAt: novel.lastReadAt, + lastViewedAt: novel.lastViewedAt, + updatedAt: novel.lastViewedAt ?? + novel.lastReadAt ?? + DateTime.fromMillisecondsSinceEpoch(0), + chapterCount: 1, + chapters: [novel], + ); + } +} + +class _NovelShelfData { + const _NovelShelfData({ + this.entries = const <_NovelShelfEntry>[], + this.continueNovels = const [], + }); + + final List<_NovelShelfEntry> entries; + final List continueNovels; +} + +class _ContinueReadingSection extends StatelessWidget { + const _ContinueReadingSection({ + required this.novels, + required this.onOpen, + }); + + final List novels; + final ValueChanged onOpen; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.fromLTRB(16, 18, 16, 0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '继续阅读', + style: Theme.of(context).textTheme.titleLarge?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 6), + Text( + '从上次停下的章节和页码接着看。', + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + const SizedBox(height: 12), + ...novels.map((novel) { + final title = novel.seriesTitle?.trim().isNotEmpty == true + ? novel.seriesTitle! + : novel.title; + final chapter = novel.chapterTitle?.trim().isNotEmpty == true + ? novel.chapterTitle! + : novel.displayChapterTitle; + return Card( + margin: const EdgeInsets.only(bottom: 10), + child: ListTile( + leading: const CircleAvatar( + backgroundColor: Color(0xFFFFE1EA), + child: Icon( + Icons.menu_book_rounded, + color: Color(0xFFD95A83), + ), + ), + title: + Text(title, maxLines: 1, overflow: TextOverflow.ellipsis), + subtitle: Text( + '$chapter · 第 ${novel.currentPage + 1} 页', + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + trailing: const Icon(Icons.chevron_right), + onTap: () => onOpen(novel), + ), + ); + }), + ], + ), + ); + } +} + +class _NovelSelectionPanel extends StatelessWidget { + const _NovelSelectionPanel({ + required this.message, + required this.onCancel, + required this.onDelete, + }); + + final String message; + final VoidCallback onCancel; + final VoidCallback? onDelete; + + @override + Widget build(BuildContext context) { + return Container( + width: double.infinity, + padding: const EdgeInsets.all(12), + decoration: BoxDecoration( + color: const Color(0xFFFFF7FA), + borderRadius: BorderRadius.circular(18), + border: Border.all(color: const Color(0xFFF2D7E0)), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(message, style: Theme.of(context).textTheme.bodySmall), + const SizedBox(height: 10), + Wrap( + spacing: 8, + runSpacing: 8, + children: [ + TextButton.icon( + onPressed: onCancel, + icon: const Icon(Icons.close_rounded), + label: const Text('取消'), + ), + FilledButton.tonalIcon( + onPressed: onDelete, + icon: const Icon(Icons.delete_outline), + label: const Text('删除'), + ), + ], + ), + ], + ), + ); + } +} + +class _ShelfEntryCard extends StatelessWidget { + const _ShelfEntryCard({ + required this.entry, + required this.paletteIndex, + required this.selected, + required this.selectionMode, + required this.onTap, + required this.onSelect, + }); + + final _NovelShelfEntry entry; + final int paletteIndex; + final bool selected; + final bool selectionMode; + final VoidCallback onTap; + final VoidCallback onSelect; + + @override + Widget build(BuildContext context) { + return InkWell( + borderRadius: BorderRadius.circular(24), + onTap: onTap, + onLongPress: onSelect, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: DecoratedBox( + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(22), + boxShadow: [ + BoxShadow( + color: + _coverColors(paletteIndex).last.withValues(alpha: 0.22), + blurRadius: 16, + offset: const Offset(0, 10), + ), + ], + ), + child: Stack( + children: [ + ClipRRect( + borderRadius: BorderRadius.circular(22), + child: + _BookCoverSurface(colors: _coverColors(paletteIndex)), + ), + Positioned( + top: 10, + left: 10, + child: DecoratedBox( + decoration: BoxDecoration( + color: Colors.white.withValues(alpha: 0.92), + borderRadius: BorderRadius.circular(999), + ), + child: Padding( + padding: const EdgeInsets.all(7), + child: Icon( + entry.isSeries + ? Icons.layers_rounded + : Icons.article_rounded, + size: 16, + color: const Color(0xFF6D5260), + ), + ), + ), + ), + Positioned( + top: 8, + right: 8, + child: selectionMode + ? IconButton.filled( + style: IconButton.styleFrom( + backgroundColor: selected + ? const Color(0xFFD95A83) + : Colors.white.withValues(alpha: 0.88), + ), + onPressed: onSelect, + icon: Icon( + selected + ? Icons.check_rounded + : Icons.radio_button_unchecked, + color: selected + ? Colors.white + : const Color(0xFFD95A83), + ), + ) + : DecoratedBox( + decoration: BoxDecoration( + color: Colors.black.withValues(alpha: 0.24), + borderRadius: BorderRadius.circular(999), + ), + child: Padding( + padding: const EdgeInsets.all(7), + child: Icon( + entry.isSeries + ? Icons.auto_stories_rounded + : Icons.menu_book_rounded, + size: 16, + color: Colors.white, + ), + ), + ), + ), + if (selected) + Positioned.fill( + child: DecoratedBox( + decoration: BoxDecoration( + color: const Color(0x33D95A83), + border: Border.all( + color: const Color(0xFFD95A83), + width: 2.5, + ), + borderRadius: BorderRadius.circular(22), + ), + ), + ), + ], + ), + ), + ), + const SizedBox(height: 10), + Text( + entry.title, + maxLines: 2, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + fontWeight: FontWeight.w800, + height: 1.28, + ), + ), + const SizedBox(height: 4), + Text( + entry.author?.isNotEmpty == true ? entry.author! : '未知作者', + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF7C8087), + ), + ), + const SizedBox(height: 4), + Text( + _entryMeta(entry), + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: const Color(0xFF9C6A7C), + fontWeight: FontWeight.w600, + ), + ), + ], + ), + ); + } +} + +class _BookCoverSurface extends StatelessWidget { + const _BookCoverSurface({required this.colors}); + + final List colors; + + @override + Widget build(BuildContext context) { + return DecoratedBox( + decoration: BoxDecoration( + gradient: LinearGradient( + colors: colors, + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ), + ), + child: Stack( + children: [ + Positioned( + top: -18, + right: -14, + child: Container( + width: 72, + height: 72, + decoration: BoxDecoration( + color: Colors.white.withValues(alpha: 0.14), + shape: BoxShape.circle, + ), + ), + ), + Positioned( + bottom: 14, + left: 14, + child: Container( + width: 34, + height: 6, + decoration: BoxDecoration( + color: Colors.white.withValues(alpha: 0.78), + borderRadius: BorderRadius.circular(999), + ), + ), + ), + Positioned( + bottom: 26, + left: 14, + right: 32, + child: Container( + height: 8, + decoration: BoxDecoration( + color: Colors.white.withValues(alpha: 0.32), + borderRadius: BorderRadius.circular(999), + ), + ), + ), + Positioned( + bottom: 40, + left: 14, + right: 18, + child: Container( + height: 8, + decoration: BoxDecoration( + color: Colors.white.withValues(alpha: 0.2), + borderRadius: BorderRadius.circular(999), + ), + ), + ), + ], + ), + ); + } +} + +class _NovelEmptyState extends StatelessWidget { + const _NovelEmptyState({ + required this.icon, + required this.title, + required this.message, + }); + + final IconData icon; + final String title; + final String message; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon(icon, size: 42, color: Theme.of(context).colorScheme.primary), + const SizedBox(height: 14), + Text(title, style: Theme.of(context).textTheme.titleLarge), + const SizedBox(height: 8), + Text( + message, + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.bodyMedium, + ), + ], + ), + ); + } +} + +List _coverColors(int index) { + const palettes = [ + [Color(0xFFB86E45), Color(0xFFE4B06C)], + [Color(0xFF4468B1), Color(0xFF8CABEA)], + [Color(0xFF8A5476), Color(0xFFDEA1C0)], + [Color(0xFF2E857A), Color(0xFF82D2C4)], + [Color(0xFF7E5BC8), Color(0xFFBFA5F7)], + [Color(0xFFD3647E), Color(0xFFF2B1C3)], + [Color(0xFF5B8A3C), Color(0xFFAFD17E)], + [Color(0xFFCC7A34), Color(0xFFF0C27A)], + ]; + return palettes[index % palettes.length]; +} + +String _entryMeta(_NovelShelfEntry entry) { + final type = entry.isSeries ? '${entry.chapterCount} 章合集' : '单章正文'; + if (entry.lastReadAt != null) { + return '$type · ${_formatLastReadAt(entry.lastReadAt)}'; + } + return '$type · 点击进入阅读'; +} + +String _formatLastReadAt(DateTime? value) { + if (value == null) { + return '刚刚整理'; + } + final now = DateTime.now(); + final diff = now.difference(value); + if (diff.inMinutes < 1) { + return '刚刚阅读'; + } + if (diff.inHours < 1) { + return '${diff.inMinutes} 分钟前'; + } + if (diff.inDays < 1) { + return '${diff.inHours} 小时前'; + } + if (diff.inDays < 30) { + return '${diff.inDays} 天前'; + } + return '${value.month}月${value.day}日'; +} diff --git a/sakura_mobile/lib/src/pages/tabs/profile_tab.dart b/sakura_mobile/lib/src/pages/tabs/profile_tab.dart new file mode 100644 index 0000000..ce7465a --- /dev/null +++ b/sakura_mobile/lib/src/pages/tabs/profile_tab.dart @@ -0,0 +1,431 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../../core/providers.dart'; +import '../favorites_page.dart'; +import '../reminder_center_page.dart'; +import '../source_manager_page.dart'; + +class ProfileTab extends ConsumerStatefulWidget { + const ProfileTab({super.key}); + + @override + ConsumerState createState() => _ProfileTabState(); +} + +class _ProfileTabState extends ConsumerState { + late Future<_ProfileData> _future = _load(); + bool _savingReminder = false; + + Future<_ProfileData> _load() async { + final api = ref.read(apiClientProvider); + Map user = {}; + Map stat = {}; + try { + // ignore: unused_local_variable + final session = ref.read(sessionProvider); + } catch (_) { + // keep empty user map + } + try { + stat = await api.galleryStat().timeout(const Duration(seconds: 3), + onTimeout: () => {}); + } catch (_) { + // keep empty stat map + } + return _ProfileData(user: user, stat: stat); + } + + void _refresh() { + ref.invalidate(apiClientProvider); + setState(() { + _future = _load(); + }); + } + + Future _updateReminder( + Map user, { + bool? reminderEnabled, + int? reminderHour, + int? reminderMinute, + }) async { + if (_savingReminder) { + return; + } + + final enabled = + reminderEnabled ?? (user['reminder_enabled'] as bool? ?? true); + final hour = reminderHour ?? (user['reminder_hour'] as int? ?? 9); + final minute = reminderMinute ?? (user['reminder_minute'] as int? ?? 0); + final timezone = user['reminder_timezone']?.toString() ?? 'Asia/Shanghai'; + + setState(() { + _savingReminder = true; + }); + + try { + await ref.read(apiClientProvider).updateMe( + reminderEnabled: enabled, + reminderHour: hour, + reminderMinute: minute, + reminderTimezone: timezone, + ); + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + enabled ? '已更新每日 ${_timeLabel(hour, minute)} 提醒' : '已关闭每日更新提醒', + ), + ), + ); + _refresh(); + } catch (error) { + if (!mounted) { + return; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('提醒设置保存失败:$error')), + ); + } finally { + if (mounted) { + setState(() { + _savingReminder = false; + }); + } + } + } + + Future _pickReminderTime(Map user) async { + final initialTime = TimeOfDay( + hour: user['reminder_hour'] as int? ?? 9, + minute: user['reminder_minute'] as int? ?? 0, + ); + final picked = await showTimePicker( + context: context, + initialTime: initialTime, + helpText: '选择每日提醒时间', + builder: (context, child) { + return Theme( + data: Theme.of(context).copyWith( + colorScheme: Theme.of(context).colorScheme.copyWith( + primary: const Color(0xFFD95A83), + surface: const Color(0xFFFFFBFD), + ), + ), + child: child ?? const SizedBox.shrink(), + ); + }, + ); + if (picked == null) { + return; + } + await _updateReminder( + user, + reminderEnabled: true, + reminderHour: picked.hour, + reminderMinute: picked.minute, + ); + } + + @override + Widget build(BuildContext context) { + return FutureBuilder<_ProfileData>( + future: _future, + builder: (context, snapshot) { + final data = snapshot.data ?? const _ProfileData(); + final user = data.user; + final stat = data.stat; + final session = ref.read(sessionProvider); + final username = session.username; + final email = (user['email'] as String?) ?? '欢迎回来'; + final initial = + username.isNotEmpty ? username.substring(0, 1).toUpperCase() : 'S'; + final reminderEnabled = user['reminder_enabled'] as bool? ?? true; + final reminderHour = user['reminder_hour'] as int? ?? 9; + final reminderMinute = user['reminder_minute'] as int? ?? 0; + final reminderLabel = _timeLabel(reminderHour, reminderMinute); + + // prefer stat endpoint for gallery numbers, fall back to user payload + final storageUsed = + (stat['storage_used'] ?? user['storage_used'] ?? 0) as num; + final totalWorks = stat['total'] ?? user['gallery_total'] ?? 0; + final totalFavorites = + stat['favorites'] ?? user['gallery_favorites'] ?? 0; + + return RefreshIndicator( + color: const Color(0xFFD95A83), + onRefresh: () async { + _refresh(); + await _future; + }, + child: ListView( + padding: const EdgeInsets.fromLTRB(16, 14, 16, 28), + children: [ + Text( + '我的', + style: Theme.of(context) + .textTheme + .headlineMedium + ?.copyWith(fontWeight: FontWeight.w900), + ), + const SizedBox(height: 14), + Container( + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(28), + gradient: const LinearGradient( + colors: [Color(0xFFFFE6EE), Color(0xFFFFF8FB)], + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ), + ), + child: Padding( + padding: const EdgeInsets.all(20), + child: Row( + children: [ + CircleAvatar( + radius: 30, + backgroundColor: Colors.white, + child: Text( + initial, + style: + Theme.of(context).textTheme.titleLarge?.copyWith( + color: const Color(0xFFD95A83), + fontWeight: FontWeight.w900, + ), + ), + ), + const SizedBox(width: 16), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + username, + style: Theme.of(context) + .textTheme + .titleLarge + ?.copyWith(fontWeight: FontWeight.w800), + ), + const SizedBox(height: 4), + Text( + email, + style: Theme.of(context) + .textTheme + .bodyMedium + ?.copyWith( + color: const Color(0xFF7B6570), + ), + ), + ], + ), + ), + ], + ), + ), + ), + if (snapshot.hasError) ...[ + const SizedBox(height: 16), + const Card( + child: ListTile( + leading: Icon(Icons.cloud_off_outlined), + title: Text('资料暂时加载失败'), + subtitle: Text('服务端可能正在忙,稍后下拉刷新再试。'), + ), + ), + ] else ...[ + const SizedBox(height: 18), + Card( + child: Padding( + padding: const EdgeInsets.all(18), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + const Icon( + Icons.notifications_active_outlined, + color: Color(0xFFD95A83), + ), + const SizedBox(width: 10), + Expanded( + child: Text( + '每日更新提醒', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.w800), + ), + ), + Switch.adaptive( + value: reminderEnabled, + activeThumbColor: const Color(0xFFD95A83), + onChanged: _savingReminder + ? null + : (value) => _updateReminder( + user, + reminderEnabled: value, + ), + ), + ], + ), + const SizedBox(height: 8), + Text( + '云端会在每天设定时间后检查你关注的 Pixiv / X 作者更新,并生成摘要提醒。关注入口请先到电脑端的缩略图墙里操作。', + style: + Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFF7B6570), + height: 1.5, + ), + ), + const SizedBox(height: 14), + Row( + children: [ + FilledButton.tonalIcon( + onPressed: _savingReminder + ? null + : () => _pickReminderTime(user), + icon: const Icon(Icons.schedule_outlined), + label: Text('提醒时间 $reminderLabel'), + ), + const SizedBox(width: 10), + if (_savingReminder) + const SizedBox( + width: 20, + height: 20, + child: + CircularProgressIndicator(strokeWidth: 2), + ), + ], + ), + const SizedBox(height: 12), + OutlinedButton.icon( + onPressed: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const ReminderCenterPage(), + ), + ); + }, + icon: const Icon(Icons.open_in_new_outlined), + label: const Text('打开提醒中心'), + ), + ], + ), + ), + ), + const SizedBox(height: 12), + Card( + color: const Color(0xFFFFF7FA), + child: ListTile( + leading: const CircleAvatar( + backgroundColor: Color(0xFFFFE1EA), + child: Icon( + Icons.favorite, + color: Color(0xFFD95A83), + ), + ), + title: const Text('我的收藏'), + subtitle: const Text('喜欢的插画、漫画和小说都在这里。'), + trailing: const Icon(Icons.chevron_right), + onTap: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const FavoritesPage(), + ), + ); + }, + ), + ), + Card( + child: ListTile( + leading: const Icon(Icons.storage_outlined), + title: const Text('云端空间'), + subtitle: Text( + '${(storageUsed.toDouble() / 1024 / 1024).toStringAsFixed(1)} MB 已使用' + ' · $totalWorks 个作品' + ' · $totalFavorites 个收藏', + ), + ), + ), + Card( + child: ListTile( + leading: const Icon(Icons.travel_explore_outlined), + title: const Text('书源管理'), + subtitle: const Text('导入和启用小说或漫画书源。'), + trailing: const Icon(Icons.chevron_right), + onTap: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const SourceManagerPage(), + ), + ); + }, + ), + ), + Card( + child: ListTile( + leading: const Icon(Icons.photo_library_outlined), + title: const Text('收藏图片'), + subtitle: const Text('最近收藏的插画和漫画都会在这里整理。'), + trailing: const Icon(Icons.chevron_right), + onTap: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const FavoritesPage(initialIndex: 0), + ), + ); + }, + ), + ), + Card( + child: ListTile( + leading: const Icon(Icons.menu_book_outlined), + title: const Text('收藏小说'), + subtitle: const Text('系列小说会按书名和章节顺序归档。'), + trailing: const Icon(Icons.chevron_right), + onTap: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const FavoritesPage(initialIndex: 1), + ), + ); + }, + ), + ), + const Card( + child: ListTile( + leading: Icon(Icons.sync_outlined), + title: Text('同步说明'), + subtitle: Text('登录后会同步最近浏览、收藏、阅读进度和每日提醒设置。'), + ), + ), + ], + const SizedBox(height: 16), + OutlinedButton.icon( + onPressed: () => ref.read(sessionProvider.notifier).logout(), + icon: const Icon(Icons.logout), + label: const Text('退出登录'), + ), + ], + ), + ); + }, + ); + } +} + +class _ProfileData { + const _ProfileData({ + this.user = const {}, + this.stat = const {}, + }); + + final Map user; + final Map stat; +} + +String _timeLabel(int hour, int minute) { + return '${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}'; +} diff --git a/sakura_mobile/lib/src/widgets/sakura_search_bar.dart b/sakura_mobile/lib/src/widgets/sakura_search_bar.dart new file mode 100644 index 0000000..332f793 --- /dev/null +++ b/sakura_mobile/lib/src/widgets/sakura_search_bar.dart @@ -0,0 +1,43 @@ +import 'package:flutter/material.dart'; + +class SakuraSearchBar extends StatelessWidget { + const SakuraSearchBar({ + super.key, + required this.hintText, + this.controller, + this.onSubmitted, + this.onExploreTap, + this.onRefreshTap, + }); + + final String hintText; + final TextEditingController? controller; + final ValueChanged? onSubmitted; + final VoidCallback? onExploreTap; + final VoidCallback? onRefreshTap; + + @override + Widget build(BuildContext context) { + return SearchBar( + controller: controller, + hintText: hintText, + leading: const Icon(Icons.search), + trailing: [ + if (onExploreTap != null) + IconButton( + tooltip: '在线搜索', + icon: const Icon(Icons.travel_explore_outlined), + onPressed: onExploreTap, + ), + if (onRefreshTap != null) + IconButton( + tooltip: '刷新', + icon: const Icon(Icons.refresh_rounded), + onPressed: onRefreshTap, + ), + ], + onSubmitted: onSubmitted, + padding: const WidgetStatePropertyAll(EdgeInsets.symmetric(horizontal: 4)), + ); + } +} diff --git a/sakura_mobile/pubspec.lock b/sakura_mobile/pubspec.lock new file mode 100644 index 0000000..eeee66f --- /dev/null +++ b/sakura_mobile/pubspec.lock @@ -0,0 +1,754 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + _flutterfire_internals: + dependency: transitive + description: + name: _flutterfire_internals + sha256: ff0a84a2734d9e1089f8aedd5c0af0061b82fb94e95260d943404e0ef2134b11 + url: "https://pub.dev" + source: hosted + version: "1.3.59" + args: + dependency: transitive + description: + name: args + sha256: d0481093c50b1da8910eb0bb301626d4d8eb7284aa739614d2b394ee09e3ea04 + url: "https://pub.dev" + source: hosted + version: "2.7.0" + async: + dependency: transitive + description: + name: async + sha256: e2eb0491ba5ddb6177742d2da23904574082139b07c1e33b8503b9f46f3e1a37 + url: "https://pub.dev" + source: hosted + version: "2.13.1" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + cached_network_image: + dependency: "direct main" + description: + name: cached_network_image + sha256: "7c1183e361e5c8b0a0f21a28401eecdbde252441106a9816400dd4c2b2424916" + url: "https://pub.dev" + source: hosted + version: "3.4.1" + cached_network_image_platform_interface: + dependency: transitive + description: + name: cached_network_image_platform_interface + sha256: "35814b016e37fbdc91f7ae18c8caf49ba5c88501813f73ce8a07027a395e2829" + url: "https://pub.dev" + source: hosted + version: "4.1.1" + cached_network_image_web: + dependency: transitive + description: + name: cached_network_image_web + sha256: "980842f4e8e2535b8dbd3d5ca0b1f0ba66bf61d14cc3a17a9b4788a3685ba062" + url: "https://pub.dev" + source: hosted + version: "1.3.1" + characters: + dependency: transitive + description: + name: characters + sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b + url: "https://pub.dev" + source: hosted + version: "1.4.1" + clock: + dependency: transitive + description: + name: clock + sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b + url: "https://pub.dev" + source: hosted + version: "1.1.2" + code_assets: + dependency: transitive + description: + name: code_assets + sha256: bf394f466ba9205f1812a0433b392d6af280f155f56651eda7c18cc32ed493b8 + url: "https://pub.dev" + source: hosted + version: "1.2.1" + collection: + dependency: transitive + description: + name: collection + sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76" + url: "https://pub.dev" + source: hosted + version: "1.19.1" + crypto: + dependency: transitive + description: + name: crypto + sha256: c8ea0233063ba03258fbcf2ca4d6dadfefe14f02fab57702265467a19f27fadf + url: "https://pub.dev" + source: hosted + version: "3.0.7" + csslib: + dependency: transitive + description: + name: csslib + sha256: "09bad715f418841f976c77db72d5398dc1253c21fb9c0c7f0b0b985860b2d58e" + url: "https://pub.dev" + source: hosted + version: "1.0.2" + dio: + dependency: "direct main" + description: + name: dio + sha256: aff32c08f92787a557dd5c0145ac91536481831a01b4648136373cddb0e64f8c + url: "https://pub.dev" + source: hosted + version: "5.9.2" + dio_web_adapter: + dependency: transitive + description: + name: dio_web_adapter + sha256: "2f9e64323a7c3c7ef69567d5c800424a11f8337b8b228bad02524c9fb3c1f340" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + fake_async: + dependency: transitive + description: + name: fake_async + sha256: "5368f224a74523e8d2e7399ea1638b37aecfca824a3cc4dfdf77bf1fa905ac44" + url: "https://pub.dev" + source: hosted + version: "1.3.3" + ffi: + dependency: transitive + description: + name: ffi + sha256: "6d7fd89431262d8f3125e81b50d3847a091d846eafcd4fdb88dd06f36d705a45" + url: "https://pub.dev" + source: hosted + version: "2.2.0" + file: + dependency: transitive + description: + name: file + sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4 + url: "https://pub.dev" + source: hosted + version: "7.0.1" + firebase_core: + dependency: "direct main" + description: + name: firebase_core + sha256: "7be63a3f841fc9663342f7f3a011a42aef6a61066943c90b1c434d79d5c995c5" + url: "https://pub.dev" + source: hosted + version: "3.15.2" + firebase_core_platform_interface: + dependency: transitive + description: + name: firebase_core_platform_interface + sha256: "0ecda14c1bfc9ed8cac303dd0f8d04a320811b479362a9a4efb14fd331a473ce" + url: "https://pub.dev" + source: hosted + version: "6.0.3" + firebase_core_web: + dependency: transitive + description: + name: firebase_core_web + sha256: "0ed0dc292e8f9ac50992e2394e9d336a0275b6ae400d64163fdf0a8a8b556c37" + url: "https://pub.dev" + source: hosted + version: "2.24.1" + firebase_messaging: + dependency: "direct main" + description: + name: firebase_messaging + sha256: "60be38574f8b5658e2f22b7e311ff2064bea835c248424a383783464e8e02fcc" + url: "https://pub.dev" + source: hosted + version: "15.2.10" + firebase_messaging_platform_interface: + dependency: transitive + description: + name: firebase_messaging_platform_interface + sha256: "685e1771b3d1f9c8502771ccc9f91485b376ffe16d553533f335b9183ea99754" + url: "https://pub.dev" + source: hosted + version: "4.6.10" + firebase_messaging_web: + dependency: transitive + description: + name: firebase_messaging_web + sha256: "0d1be17bc89ed3ff5001789c92df678b2e963a51b6fa2bdb467532cc9dbed390" + url: "https://pub.dev" + source: hosted + version: "3.10.10" + fixnum: + dependency: transitive + description: + name: fixnum + sha256: b6dc7065e46c974bc7c5f143080a6764ec7a4be6da1285ececdc37be96de53be + url: "https://pub.dev" + source: hosted + version: "1.1.1" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_cache_manager: + dependency: transitive + description: + name: flutter_cache_manager + sha256: "400b6592f16a4409a7f2bb929a9a7e38c72cceb8ffb99ee57bbf2cb2cecf8386" + url: "https://pub.dev" + source: hosted + version: "3.4.1" + flutter_js: + dependency: "direct main" + description: + name: flutter_js + sha256: "046428059ac7bef71e305dc35fc24be933b684684a43e83eab7ebd0dc407ad85" + url: "https://pub.dev" + source: hosted + version: "0.8.7" + flutter_lints: + dependency: "direct dev" + description: + name: flutter_lints + sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c" + url: "https://pub.dev" + source: hosted + version: "4.0.0" + flutter_riverpod: + dependency: "direct main" + description: + name: flutter_riverpod + sha256: "9532ee6db4a943a1ed8383072a2e3eeda041db5657cdf6d2acecf3c21ecbe7e1" + url: "https://pub.dev" + source: hosted + version: "2.6.1" + flutter_staggered_grid_view: + dependency: "direct main" + description: + name: flutter_staggered_grid_view + sha256: "19e7abb550c96fbfeb546b23f3ff356ee7c59a019a651f8f102a4ba9b7349395" + url: "https://pub.dev" + source: hosted + version: "0.7.0" + flutter_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + gbk_codec: + dependency: "direct main" + description: + name: gbk_codec + sha256: "3af5311fc9393115e3650ae6023862adf998051a804a08fb804f042724999f61" + url: "https://pub.dev" + source: hosted + version: "0.4.0" + hooks: + dependency: transitive + description: + name: hooks + sha256: "62ae9bb76d02526c7c2110a19b6e6ad788fe28d35e553e35efb02a41a46ab43a" + url: "https://pub.dev" + source: hosted + version: "2.0.1" + html: + dependency: "direct main" + description: + name: html + sha256: "6d1264f2dffa1b1101c25a91dff0dc2daee4c18e87cd8538729773c073dbf602" + url: "https://pub.dev" + source: hosted + version: "0.15.6" + http: + dependency: transitive + description: + name: http + sha256: "87721a4a50b19c7f1d49001e51409bddc46303966ce89a65af4f4e6004896412" + url: "https://pub.dev" + source: hosted + version: "1.6.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.dev" + source: hosted + version: "4.1.2" + jni: + dependency: transitive + description: + name: jni + sha256: c2230682d5bc2362c1c9e8d3c7f406d9cbba23ab3f2e203a025dd47e0fb2e68f + url: "https://pub.dev" + source: hosted + version: "1.0.0" + jni_flutter: + dependency: transitive + description: + name: jni_flutter + sha256: "8b59e590786050b1cd866677dddaf76b1ade5e7bc751abe04b86e84d379d3ba6" + url: "https://pub.dev" + source: hosted + version: "1.0.1" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de" + url: "https://pub.dev" + source: hosted + version: "11.0.2" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1" + url: "https://pub.dev" + source: hosted + version: "3.0.10" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1" + url: "https://pub.dev" + source: hosted + version: "3.0.2" + lints: + dependency: transitive + description: + name: lints + sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235" + url: "https://pub.dev" + source: hosted + version: "4.0.0" + logging: + dependency: transitive + description: + name: logging + sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61 + url: "https://pub.dev" + source: hosted + version: "1.3.0" + matcher: + dependency: transitive + description: + name: matcher + sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861 + url: "https://pub.dev" + source: hosted + version: "0.12.19" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b" + url: "https://pub.dev" + source: hosted + version: "0.13.0" + meta: + dependency: transitive + description: + name: meta + sha256: "1741988757a65eb6b36abe716829688cf01910bbf91c34354ff7ec1c3de2b349" + url: "https://pub.dev" + source: hosted + version: "1.18.0" + mime: + dependency: transitive + description: + name: mime + sha256: "41a20518f0cb1256669420fdba0cd90d21561e560ac240f26ef8322e45bb7ed6" + url: "https://pub.dev" + source: hosted + version: "2.0.0" + objective_c: + dependency: transitive + description: + name: objective_c + sha256: "6cb691c686fa2838c6deb34980d426145c2a5d537491cb83d463c33cdbc726ed" + url: "https://pub.dev" + source: hosted + version: "9.4.1" + octo_image: + dependency: transitive + description: + name: octo_image + sha256: "34faa6639a78c7e3cbe79be6f9f96535867e879748ade7d17c9b1ae7536293bd" + url: "https://pub.dev" + source: hosted + version: "2.1.0" + package_config: + dependency: transitive + description: + name: package_config + sha256: f096c55ebb7deb7e384101542bfba8c52696c1b56fca2eb62827989ef2353bbc + url: "https://pub.dev" + source: hosted + version: "2.2.0" + path: + dependency: transitive + description: + name: path + sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5" + url: "https://pub.dev" + source: hosted + version: "1.9.1" + path_provider: + dependency: "direct main" + description: + name: path_provider + sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd" + url: "https://pub.dev" + source: hosted + version: "2.1.5" + path_provider_android: + dependency: transitive + description: + name: path_provider_android + sha256: "69cbd515a62b94d32a7944f086b2f82b4ac40a1d45bebfc00813a430ab2dabcd" + url: "https://pub.dev" + source: hosted + version: "2.3.1" + path_provider_foundation: + dependency: transitive + description: + name: path_provider_foundation + sha256: "2a376b7d6392d80cd3705782d2caa734ca4727776db0b6ec36ef3f1855197699" + url: "https://pub.dev" + source: hosted + version: "2.6.0" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7 + url: "https://pub.dev" + source: hosted + version: "2.3.0" + photo_view: + dependency: "direct main" + description: + name: photo_view + sha256: "1fc3d970a91295fbd1364296575f854c9863f225505c28c46e0a03e48960c75e" + url: "https://pub.dev" + source: hosted + version: "0.15.0" + platform: + dependency: transitive + description: + name: platform + sha256: "5d6b1b0036a5f331ebc77c850ebc8506cbc1e9416c27e59b439f917a902a4984" + url: "https://pub.dev" + source: hosted + version: "3.1.6" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" + url: "https://pub.dev" + source: hosted + version: "2.1.8" + pub_semver: + dependency: transitive + description: + name: pub_semver + sha256: "5bfcf68ca79ef689f8990d1160781b4bad40a3bd5e5218ad4076ddb7f4081585" + url: "https://pub.dev" + source: hosted + version: "2.2.0" + record_use: + dependency: transitive + description: + name: record_use + sha256: "2551bd8eecfe95d14ae75f6021ad0248be5c27f138c2ec12fcb52b500b3ba1ed" + url: "https://pub.dev" + source: hosted + version: "0.6.0" + riverpod: + dependency: transitive + description: + name: riverpod + sha256: "59062512288d3056b2321804332a13ffdd1bf16df70dcc8e506e411280a72959" + url: "https://pub.dev" + source: hosted + version: "2.6.1" + rxdart: + dependency: transitive + description: + name: rxdart + sha256: "5c3004a4a8dbb94bd4bf5412a4def4acdaa12e12f269737a5751369e12d1a962" + url: "https://pub.dev" + source: hosted + version: "0.28.0" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: c3025c5534b01739267eb7d76959bbc25a6d10f6988e1c2a3036940133dd10bf + url: "https://pub.dev" + source: hosted + version: "2.5.5" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: e8d4762b1e2e8578fc4d0fd548cebf24afd24f49719c08974df92834565e2c53 + url: "https://pub.dev" + source: hosted + version: "2.4.23" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "4e7eaffc2b17ba398759f1151415869a34771ba11ebbccd1b0145472a619a64f" + url: "https://pub.dev" + source: hosted + version: "2.5.6" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f" + url: "https://pub.dev" + source: hosted + version: "2.4.1" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "649dc798a33931919ea356c4305c2d1f81619ea6e92244070b520187b5140ef9" + url: "https://pub.dev" + source: hosted + version: "2.4.2" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: c49bd060261c9a3f0ff445892695d6212ff603ef3115edbb448509d407600019 + url: "https://pub.dev" + source: hosted + version: "2.4.3" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1" + url: "https://pub.dev" + source: hosted + version: "2.4.1" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + source_span: + dependency: transitive + description: + name: source_span + sha256: "56a02f1f4cd1a2d96303c0144c93bd6d909eea6bee6bf5a0e0b685edbd4c47ab" + url: "https://pub.dev" + source: hosted + version: "1.10.2" + sqflite: + dependency: transitive + description: + name: sqflite + sha256: "58a799e6ac17dd32fbab93813d39ed835a75ccc0f8f85b8955fe318c6712b082" + url: "https://pub.dev" + source: hosted + version: "2.4.3" + sqflite_android: + dependency: transitive + description: + name: sqflite_android + sha256: d0548f9d7422a2dae99ec6f8b0a3074463b132d216fa5ba0d230eeefc901983b + url: "https://pub.dev" + source: hosted + version: "2.4.3" + sqflite_common: + dependency: transitive + description: + name: sqflite_common + sha256: cce558075afe2a83f3fd7fc123acd6b090683e4f23910d44fbb31ecd7800b014 + url: "https://pub.dev" + source: hosted + version: "2.5.9" + sqflite_darwin: + dependency: transitive + description: + name: sqflite_darwin + sha256: "164a5d73ab87a134566057219988bafde837029a64264e61f1f04376ef3cfcd2" + url: "https://pub.dev" + source: hosted + version: "2.4.3" + sqflite_platform_interface: + dependency: transitive + description: + name: sqflite_platform_interface + sha256: f84939f84350d92d04416f8bc4dc52d3896aec7716cc9e80cf0146342139dc50 + url: "https://pub.dev" + source: hosted + version: "2.4.1" + stack_trace: + dependency: transitive + description: + name: stack_trace + sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1" + url: "https://pub.dev" + source: hosted + version: "1.12.1" + state_notifier: + dependency: transitive + description: + name: state_notifier + sha256: b8677376aa54f2d7c58280d5a007f9e8774f1968d1fb1c096adcb4792fba29bb + url: "https://pub.dev" + source: hosted + version: "1.0.0" + stream_channel: + dependency: transitive + description: + name: stream_channel + sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d" + url: "https://pub.dev" + source: hosted + version: "2.1.4" + string_scanner: + dependency: transitive + description: + name: string_scanner + sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43" + url: "https://pub.dev" + source: hosted + version: "1.4.1" + sync_http: + dependency: transitive + description: + name: sync_http + sha256: "7f0cd72eca000d2e026bcd6f990b81d0ca06022ef4e32fb257b30d3d1014a961" + url: "https://pub.dev" + source: hosted + version: "0.3.1" + synchronized: + dependency: transitive + description: + name: synchronized + sha256: "93b153dcb6a26dcddee6ca087dd634b53e38c10b5aa163e8e49501a776456153" + url: "https://pub.dev" + source: hosted + version: "3.4.1" + term_glyph: + dependency: transitive + description: + name: term_glyph + sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e" + url: "https://pub.dev" + source: hosted + version: "1.2.2" + test_api: + dependency: transitive + description: + name: test_api + sha256: "949a932224383300f01be9221c39180316445ecb8e7547f70a41a35bf421fb9e" + url: "https://pub.dev" + source: hosted + version: "0.7.11" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.dev" + source: hosted + version: "1.4.0" + uuid: + dependency: transitive + description: + name: uuid + sha256: "1fef9e8e11e2991bb773070d4656b7bd5d850967a2456cfc83cf47925ba79489" + url: "https://pub.dev" + source: hosted + version: "4.5.3" + vector_math: + dependency: transitive + description: + name: vector_math + sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b + url: "https://pub.dev" + source: hosted + version: "2.2.0" + vm_service: + dependency: transitive + description: + name: vm_service + sha256: "0016aef94fc66495ac78af5859181e3f3bf2026bd8eecc72b9565601e19ab360" + url: "https://pub.dev" + source: hosted + version: "15.2.0" + web: + dependency: transitive + description: + name: web + sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a" + url: "https://pub.dev" + source: hosted + version: "1.1.1" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: "7a3f37b05d989967cdddcbb571f1ea834867ae2faa29725fd085180e0883aa15" + url: "https://pub.dev" + source: hosted + version: "1.1.0" + yaml: + dependency: transitive + description: + name: yaml + sha256: b9da305ac7c39faa3f030eccd175340f968459dae4af175130b3fc47e40d76ce + url: "https://pub.dev" + source: hosted + version: "3.1.3" +sdks: + dart: ">=3.12.0 <4.0.0" + flutter: ">=3.44.0" diff --git a/sakura_mobile/pubspec.yaml b/sakura_mobile/pubspec.yaml new file mode 100644 index 0000000..545b048 --- /dev/null +++ b/sakura_mobile/pubspec.yaml @@ -0,0 +1,31 @@ +name: sakura_mobile +description: Sakura Cloud mobile client. +publish_to: "none" +version: 1.0.0+1 + +environment: + sdk: ">=3.4.0 <4.0.0" + +dependencies: + flutter: + sdk: flutter + dio: ^5.7.0 + flutter_riverpod: ^2.5.1 + cached_network_image: ^3.4.1 + photo_view: ^0.15.0 + flutter_staggered_grid_view: ^0.7.0 + html: ^0.15.5 + shared_preferences: ^2.3.2 + path_provider: ^2.1.4 + firebase_core: ^3.8.0 + firebase_messaging: ^15.1.5 + gbk_codec: ^0.4.0 + flutter_js: ^0.8.7 + +dev_dependencies: + flutter_test: + sdk: flutter + flutter_lints: ^4.0.0 + +flutter: + uses-material-design: true diff --git a/sakura_mobile/test/api_client_test.dart b/sakura_mobile/test/api_client_test.dart new file mode 100644 index 0000000..80e77a6 --- /dev/null +++ b/sakura_mobile/test/api_client_test.dart @@ -0,0 +1,27 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:sakura_mobile/src/core/api_client.dart'; + +void main() { + group('cloud API base URL normalization', () { + test('keeps valid http URL', () { + expect( + normalizeCloudBaseUrl('http://api.kunkunxiaomao.top:8000'), + 'http://api.kunkunxiaomao.top:8000', + ); + }); + + test('repairs missing slashes after scheme', () { + expect( + normalizeCloudBaseUrl('http:api.kunkunxiaomao.top:8000'), + 'http://api.kunkunxiaomao.top:8000', + ); + }); + + test('adds http scheme and strips api prefix', () { + expect( + normalizeCloudBaseUrl('api.kunkunxiaomao.top:8000/api/v1/'), + 'http://api.kunkunxiaomao.top:8000', + ); + }); + }); +} diff --git a/sakura_mobile/test/core_improvements_test.dart b/sakura_mobile/test/core_improvements_test.dart new file mode 100644 index 0000000..8e70d71 --- /dev/null +++ b/sakura_mobile/test/core_improvements_test.dart @@ -0,0 +1,200 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:sakura_mobile/src/core/legado_engine.dart'; +import 'package:sakura_mobile/src/core/legado_models.dart'; +import 'package:sakura_mobile/src/core/legado_rule_executor.dart'; +import 'package:sakura_mobile/src/core/legado_toc_pagination.dart'; +import 'package:sakura_mobile/src/core/online_shelf.dart'; +import 'package:sakura_mobile/src/core/search_scheduler.dart'; +import 'package:sakura_mobile/src/core/source_health.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + group('TOC 分页规则', () { + final resolver = TocPaginationResolver(LegadoRuleExecutor()); + + test('解析字段别名并渲染页码偏移', () { + final source = LegadoSource.fromJson({ + 'bookSourceName': '分页源', + 'bookSourceUrl': 'https://example.com', + 'ruleToc': { + 'chapterList': '.list a', + 'chapterUrl': 'a@href', + 'nextUrl': '/catalog?page={{page+1}}', + }, + }); + expect(source.tocNextPageRule, '/catalog?page={{page+1}}'); + expect( + resolver.resolveNextPage( + currentUrl: 'https://example.com/catalog?page=1', + currentHtml: '', + rule: source.tocNextPageRule, + pageIndex: 0, + ), + 'https://example.com/catalog?page=2', + ); + }); + + test('支持 CSS、XPath、切片和 offset', () { + const html = ''; + expect( + resolver.resolveNextPage( + currentUrl: 'https://example.com/list/1', + currentHtml: html, + rule: 'class.next@href', + pageIndex: 1, + ), + 'https://example.com/list/2', + ); + expect( + resolver.resolveNextPage( + currentUrl: 'https://example.com/list/1', + currentHtml: html, + rule: "//a[contains(text(),'下一页')]/@href", + pageIndex: 1, + ), + 'https://example.com/list/2', + ); + expect( + resolver.resolveNextPage( + currentUrl: 'https://example.com/list/123.html', + currentHtml: '', + rule: '!0:-5', + pageIndex: 1, + ), + 'https://example.com/list/123', + ); + expect( + resolver.resolveNextPage( + currentUrl: 'https://example.com/list?offset=50', + currentHtml: '', + rule: '/list?offset=50', + pageIndex: 1, + ), + 'https://example.com/list?offset=100', + ); + }); + }); + + group('搜索健康分与滑动窗口', () { + setUp(() => SharedPreferences.setMockInitialValues({})); + + test('连续失败熔断,成功后恢复并记录延迟', () { + final health = SourceHealth(sourceId: 'a'); + for (var i = 0; i < 5; i++) { + health.recordFailure(timeout: i.isEven); + } + expect(health.isUsable, isFalse); + health.recordSuccess(300); + expect(health.isUsable, isTrue); + expect(health.score, greaterThan(0)); + }); + + test('滑动窗口先返回先完成的源', () async { + final engine = _FakeSearchEngine({ + 'slow': const Duration(milliseconds: 80), + 'fast': const Duration(milliseconds: 5), + }); + final scheduler = SearchScheduler(engine, SourceHealthStore()); + final phases = await scheduler + .search('测试', [_source('slow'), _source('fast')]) + .where((phase) => phase.items.isNotEmpty) + .toList(); + expect(phases.first.items.first.sourceId, 'fast'); + expect(phases.last.name, 'complete'); + expect(phases.last.doneCount, 2); + }); + + test('在线搜索覆盖超过十个的全部已启用来源', () async { + final engine = _FakeSearchEngine(const {}); + final scheduler = SearchScheduler(engine, SourceHealthStore()); + final sources = List.generate(15, (index) => _source('source-$index')); + final phases = await scheduler.search('全量', sources).toList(); + + expect(phases.last.name, 'complete'); + expect(phases.last.totalCount, 15); + expect(phases.last.doneCount, 15); + expect(phases.last.items, hasLength(15)); + }); + }); + + group('在线书架保存解耦', () { + setUp(() => SharedPreferences.setMockInitialValues({})); + + test('只保存元数据、重复保存更新、支持移除', () async { + final store = OnlineShelfStore(); + final first = OnlineShelfEntry( + sourceId: 'source', + sourceName: '示例源', + title: '第一版标题', + detailUrl: 'https://example.com/book/1', + savedAt: DateTime(2026, 6, 22), + ); + await store.save(first); + await store.save(OnlineShelfEntry( + sourceId: first.sourceId, + sourceName: first.sourceName, + title: '更新后的标题', + detailUrl: first.detailUrl, + savedAt: DateTime(2026, 6, 22, 1), + )); + expect(store.entries, hasLength(1)); + expect(store.entries.single.title, '更新后的标题'); + + await store.save(OnlineShelfEntry( + sourceId: 'manga-source', + sourceName: '漫画源', + title: '在线漫画', + detailUrl: 'https://example.com/manga/1', + contentType: 'manga', + savedAt: DateTime(2026, 6, 23), + )); + expect(store.entries.where((entry) => entry.isManga), hasLength(1)); + expect(store.entries.where((entry) => entry.isNovel), hasLength(1)); + + final restored = OnlineShelfStore(); + await restored.load(); + expect(restored.entries, hasLength(2)); + expect( + restored.entries.singleWhere((entry) => entry.isNovel).detailUrl, + first.detailUrl, + ); + await restored.remove(first.id); + expect(restored.entries.single.isManga, isTrue); + }); + }); +} + +LegadoSource _source(String id) => LegadoSource( + id: id, + name: id, + url: 'https://$id.example.com', + type: 0, + enabled: true, + searchUrl: '/search?q={{key}}', + searchListRule: '.result', + ); + +class _FakeSearchEngine extends LegadoEngine { + _FakeSearchEngine(this.delays); + + final Map delays; + + @override + Future> search( + LegadoSource source, + String keyword, { + int page = 1, + }) async { + await Future.delayed(delays[source.id] ?? Duration.zero); + return [ + UnifiedSearchResult( + sourceId: source.id, + sourceName: source.name, + title: '$keyword-${source.id}', + detailUrl: '${source.url}/book', + ), + ]; + } +} diff --git a/sakura_mobile/test/fixtures/yiciyuan_1012.json b/sakura_mobile/test/fixtures/yiciyuan_1012.json new file mode 100644 index 0000000..c3f314c --- /dev/null +++ b/sakura_mobile/test/fixtures/yiciyuan_1012.json @@ -0,0 +1,10566 @@ +[ + { + "bookSourceComment": "/*修复正文不全\n\t访问过快偶尔宕机,出错了会重试加载。\n以下代码勿动!可在js中通过eval(String(source.bookSourceComment))\n调用*/\n\t\nvar error=/503 Service Temporarily/;\nwhile(error.test(result)){\n\tjava.log(\"网站出错,重新加载\");\n\tcookie.removeCookie(baseUrl);\n\tresult=java.ajax(baseUrl);\n\t}\n\t\tresult;", + "bookSourceGroup": "", + "bookSourceName": "新笔趣中文/笔尖中文", + "bookSourceType": 0, + "bookSourceUrl": "http://wap.xbiquzw.com", + "bookUrlPattern": "http://wap.xbiquzw.com/[\\d_]+/", + "customOrder": -16, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "玄幻小说::/list/1_{{page}}.html\n修真小说::/list/2_{{page}}.html\n都市小说::/list/3_{{page}}.html\n穿越小说::/list/4_{{page}}.html\n网游小说::/list/5_{{page}}.html\n科幻小说::/list/6_{{page}}.html\n完本小说::/wanjiexiaoshuo/{{page}}", + "header": "{\"User-Agent\": \"Mozilla/5.0 (Linux; Android 9) Mobile Safari/537.36\"}", + "lastUpdateTime": 1698744067484, + "loginCheckJs": "", + "respondTime": 6285, + "ruleBookInfo": { + "author": "@get:{a}", + "coverUrl": "@get:{c}", + "init": "\neval(String(source.bookSourceComment))\n\n@put:{n:\"[property$=book_name]@content\",\na:\"[property$=author]@content\",\nk:\"[property~=category|status|update_time]@content\",\nl:\"[property$=latest_chapter_name]@content\",\ni:\"[property$=description]@content\",\nc:\"[property$=image]@content\"}", + "intro": "@get:{i}", + "kind": "@get:{k}", + "lastChapter": "@get:{l}", + "name": "@get:{n}" + }, + "ruleContent": { + "content": "\neval(String(source.bookSourceComment))\n#nr@html", + "nextContentUrl": "text.下一页@href", + "replaceRegex": "##.本章未完.*|{{ try{chapter.title}catch(e){\"\"} }}|\\(第\\d+.*页\\)" + }, + "ruleExplore": { + "author": "span.1@text", + "bookList": "\neval(String(source.bookSourceComment))\nul.xbk", + "bookUrl": "a.1@href", + "coverUrl": "img@src", + "intro": "span.-2@text", + "kind": "tag.i@text", + "name": "a.1@text" + }, + "ruleSearch": { + "author": "", + "bookList": "\neval(String(source.bookSourceComment))\n.bookone", + "bookUrl": "a.0@href", + "checkKeyWord": "剑来", + "kind": "0", + "lastChapter": "a.1@text", + "name": "a.0@text" + }, + "ruleToc": { + "chapterList": "\neval(String(source.bookSourceComment))\nul.1@li a", + "chapterName": "text", + "chapterUrl": "href", + "nextTocUrl": "option@value||text.下一页@href" + }, + "searchUrl": "/modules/article/search.php,{\n \"body\": \"type=articlename&q={{key}}\",\n \"method\": \"POST\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "西红柿网:https://www.xhsxsw.com", + "bookSourceGroup": "", + "bookSourceName": "🎉 西红柿网", + "bookSourceType": 0, + "bookSourceUrl": "http://154.37.154.143", + "customOrder": -14, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "header": "{\n\t\"Host\": \"m.xhsxsw.com\",\n\t\"User-Agent\": \"Mozilla/5.0 (Linux; Android 10; PCAM10 Build/QP1A.190711.020) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.92 Mobile Safari/537.36\",\n\t\"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\"\n}", + "lastUpdateTime": 1701430204057, + "respondTime": 904, + "ruleBookInfo": { + "author": "id.info@p.0@text", + "coverUrl": "id.fmimg@img@src", + "intro": "id.intro@p.0@text##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".con_top@a.1@text&&id.info@p.1@ownText&&id.info@p.2@text##.*:|\\, ,|\\s..:.*", + "lastChapter": "id.info@p.3@a@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "id.info@h1@text" + }, + "ruleContent": { + "content": "id.content@html", + "replaceRegex": "##\\s*【.*最新章节。】\\s*|(未完待续)|【.*换源.pp.*】" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": ".author@text", + "bookList": ".bookbox", + "bookUrl": ".bookname@a@href##https:\\/\\/m\\.xhsxsw\\.com\n@js:\noption = {\n\t\"headers\": {\n\t\t\"Host\": \"www.xhsxsw.com\",\n\t\t\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62\",\n\t\t\"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\"\n\t}\n}\nresult + \",\" + JSON.stringify(option)", + "checkKeyWord": "", + "lastChapter": ".update@a@text", + "name": ".bookname@text##\\[.*\\]" + }, + "ruleToc": { + "chapterList": "id.list@a", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href\n@js:\noption = {\n\t\"headers\": {\n\t\t\"Host\": \"www.xhsxsw.com\",\n\t\t\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62\",\n\t\t\"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\"\n\t}\n}\nresult + \",\" + JSON.stringify(option)" + }, + "searchUrl": "/modules/article/search.php,{\n \"method\": \"POST\",\n \"body\": \"keyword={{key}}&submit=搜索\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎉 歌书小说", + "bookSourceType": 0, + "bookSourceUrl": "http://m.gashuw.com", + "customOrder": -13, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "lastUpdateTime": 1701433257339, + "respondTime": 38692, + "ruleBookInfo": { + "author": ".synopsisArea_detail@p.0@text", + "coverUrl": ".synopsisArea_detail@img@src", + "intro": ".review@text##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".synopsisArea_detail@p.1:2:4@text##.*:|小说", + "lastChapter": ".directoryArea@a.0@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "span.title@text" + }, + "ruleContent": { + "content": "id.chaptercontent@textNodes", + "nextContentUrl": "id.pt_next@href", + "replaceRegex": "##\\s*.*点击下一页.*\\s*|www.gebiqu.com" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "p.1@text##.*:", + "bookList": ".hot_sale", + "bookUrl": "a@href", + "coverUrl": "a@href##.+\\D((\\d+)\\d{3})\\D##http://image.gashuw.com/$2/$1/$1s.jpg###", + "kind": "p.1:2@text##小说|\\s.*", + "lastChapter": "p.2@text##.*:|正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "p.0@text" + }, + "ruleToc": { + "chapterList": ".directoryArea@p@a", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href", + "nextTocUrl": ".right@a@href" + }, + "searchUrl": "/s.php,{\n \"body\": \"keyword={{key}}&t=1\",\n \"method\": \"post\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎉 求小说网", + "bookSourceType": 0, + "bookSourceUrl": "http://www.81ht.com", + "customOrder": -12, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"玄幻\",\"url\":\"/xuanhuanxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"奇幻\",\"url\":\"/qihuanxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"修真\",\"url\":\"/xiuzhenxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/dushixiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"言情\",\"url\":\"/yanqingxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"历史\",\"url\":\"/lishixiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"同人\",\"url\":\"/danmeixiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"武侠\",\"url\":\"/wuxiaxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/kehuanxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"游戏\",\"url\":\"/youxixiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"军事\",\"url\":\"/junshixiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"竞技\",\"url\":\"/jingjixiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"灵异\",\"url\":\"/lingyixiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"商战\",\"url\":\"/shangzhanxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"校园\",\"url\":\"/xiaoyuanxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"官场\",\"url\":\"/guanchangxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"职场\",\"url\":\"/zhichangxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"其他\",\"url\":\"/qitaxiaoshuo/{{page}}.htm\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}}]", + "lastUpdateTime": 1696685456842, + "respondTime": 5789, + "ruleBookInfo": { + "author": ".list-group[0]@small@text", + "coverUrl": ".col-xs-2@img@src", + "intro": "id.all@ownText##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".list-group[0]@li.1:2:3@text##.*:|小说|\\s..:.*", + "lastChapter": ".list-group[0]@a.1@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": ".list-group[0]@h1@text", + "tocUrl": ".tac@a@href" + }, + "ruleContent": { + "content": "id.txtContent@textNodes", + "replaceRegex": "##http.*|由于各.*避免迷路|网页版.*最新内容|请退出.*最新章节.|新笔趣阁.*" + }, + "ruleExplore": { + "author": "div.3@text", + "bookList": ".clearfix!0", + "bookUrl": "a.0@href", + "kind": "i,span@text##\\s.*", + "lastChapter": "a.1@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "a.0@text" + }, + "ruleSearch": { + "author": "textNodes##\\/|\\s", + "bookList": ".book_textList2@li", + "bookUrl": "a.0@href@js:result.replace(/^/g,\"http://www.81ht.com\").replace(/-/g,\"/\").replace(/\\/$/g,\".html\")", + "kind": "i,span@text##\\s.*", + "lastChapter": "a.1@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "a.0@text" + }, + "ruleToc": { + "chapterList": "id.chapters-list@li@a", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href" + }, + "searchUrl": "{{cookie.removeCookie(source.getKey())}}\nhttp://m.81ht.com/search.htm?keyword={{key}}&pn={{page}}", + "weight": 0 + }, + { + "bookSourceComment": "备用源URL:https://sma.yueyouxs.com\n备用源URL:http://m.suixkan.com", + "bookSourceGroup": "", + "bookSourceName": "🎉 阅友小说", + "bookSourceType": 0, + "bookSourceUrl": "https://sma.yueyouxs.com", + "customOrder": -11, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"推荐\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},\n{\"title\":\"重磅推荐\",\"url\":\"/l/s/28/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.4,\"layout_flexGrow\":1}},\n{\"title\":\"男生必读\",\"url\":\"/l/s/29/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.4,\"layout_flexGrow\":1}},\n{\"title\":\"女生爱看\",\"url\":\"/l/s/30/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.4,\"layout_flexGrow\":1}},\n{\"title\":\"小编推荐\",\"url\":\"/l/s/31/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.4,\"layout_flexGrow\":1}},\n{\"title\":\"男频\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},\n{\"title\":\"都市人生\",\"url\":\"/l/f/1100/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"玄幻奇幻\",\"url\":\"/l/f/1101/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"武侠仙侠\",\"url\":\"/l/f/1102/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"军事历史\",\"url\":\"/l/f/1103/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"科幻末世\",\"url\":\"/l/f/1104/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"游戏体育\",\"url\":\"/l/f/1105/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"热血青春\",\"url\":\"/l/f/1106/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"悬疑灵异\",\"url\":\"/l/f/1107/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"脑洞大开\",\"url\":\"/l/f/1108/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"女频\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},\n{\"title\":\"现代言情\",\"url\":\"/l/f/2100/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"古代言情\",\"url\":\"/l/f/2101/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"幻想言情\",\"url\":\"/l/f/2102/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"\",\"url\":\"/l/f/2103/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"穿越时空\",\"url\":\"/l/f/2104/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"宫闱争斗\",\"url\":\"/l/f/2105/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"豪门总裁\",\"url\":\"/l/f/2106/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"婚恋爱情\",\"url\":\"/l/f/2107/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"经商种田\",\"url\":\"/l/f/2108/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"图书\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},\n{\"title\":\"出版读物\",\"url\":\"/l/f/3101/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"文学小说\",\"url\":\"/l/f/3102/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},\n{\"title\":\"古代典籍\",\"url\":\"/l/f/3103/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}}]", + "lastUpdateTime": 1701433751840, + "respondTime": 1345, + "ruleBookInfo": { + "author": ".face-info@span.0@text##.*:", + "coverUrl": ".face-cover@img@src", + "init": "", + "intro": "id.intro@text##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".face-info@span.1:3@text&&id.idNewIds@id.chapter-ps-id@text##.*:", + "lastChapter": "id.idNewIds@.chapter-entrance@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": ".face-info-title@text", + "tocUrl": ".sumchapter@a@href", + "wordCount": ".face-info@span.2@text##.*:" + }, + "ruleContent": { + "content": ".con@html", + "replaceRegex": "##[\\((]本章未完.*[)\\)]|[\\((]本章完[)\\)]" + }, + "ruleExplore": { + "author": ".v-author@text##\\s", + "bookList": ".v-list-item", + "bookUrl": "@onclick@js:result.match(/\\('(.*?)'\\)/)[1]", + "coverUrl": "img@src", + "intro": ".v-intro@text", + "name": ".v-title@text", + "wordCount": ".v-words@text" + }, + "ruleSearch": { + "author": ".v-author@text##\\s", + "bookList": ".v-list-item", + "bookUrl": "@onclick@js:result.match(/\\('(.*?)', '', ''\\)/)[1]", + "coverUrl": "img@src", + "intro": ".v-intro@text", + "kind": ".base-label@text", + "name": ".v-title@text", + "wordCount": ".v-words@text" + }, + "ruleToc": { + "chapterList": ".catalog_ls@li@a", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href" + }, + "searchUrl": "/s/1.html?keyword={{key}}&page={{page}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎉 唐三中文", + "bookSourceType": 0, + "bookSourceUrl": "http://www.xtangsanshu.com", + "customOrder": -10, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "lastUpdateTime": 1696074722593, + "respondTime": 925, + "ruleBookInfo": { + "author": ".small@span.0@text", + "coverUrl": ".cover@img@src", + "intro": ".intro@textNodes##(^|[。!?]+[”」)】]?)##$1
@js:result.replace(/作者.*《.*|无弹窗推荐.*/g,\"\")", + "kind": ".small@span.1:2:4@text##小说|.*:|\\s..:.*", + "lastChapter": ".last@a@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": ".info@h2@text", + "wordCount": ".small@span.3@text##.*:" + }, + "ruleContent": { + "content": "id.content@html", + "replaceRegex": "##\\s*http.*//.*\\s*|\\s*请记住本书首.*\\s*" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": ".author@text", + "bookList": ".bookbox", + "bookUrl": "a.0@href", + "coverUrl": "img@src", + "kind": ".cat@text##.*:|小说", + "lastChapter": ".update@a@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": ".bookname@text" + }, + "ruleToc": { + "chapterList": ".listmain@dd@a", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href" + }, + "searchUrl": "/s.php?ie=utf-8&s=&q={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎉 蚂蚁阅读", + "bookSourceType": 0, + "bookSourceUrl": "http://www.xmayitxt.com", + "bookUrlPattern": "", + "customOrder": -9, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"榜单\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"总榜\",\"url\":\"/top/allvisit/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"月榜\",\"url\":\"/top/monthvisit/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"周榜\",\"url\":\"/top/weekvisit/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"总推\",\"url\":\"/top/allvote/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"月推\",\"url\":\"/top/monthvote/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"周推\",\"url\":\"/top/weekvote/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"最新\",\"url\":\"/top/postdate/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"最近\",\"url\":\"/top/allvisit/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"收藏\",\"url\":\"/top/goodnum/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"字数\",\"url\":\"/top/size/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"分类\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻\",\"url\":\"/sort/1/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"仙侠\",\"url\":\"/sort/2/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/sort/3/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"历史\",\"url\":\"/sort/4/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"军事\",\"url\":\"/sort/5/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"灵异\",\"url\":\"/sort/6/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/sort/7/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"游戏\",\"url\":\"/sort/8/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"现言\",\"url\":\"/sort/9/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"古言\",\"url\":\"/sort/10/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"穿越\",\"url\":\"/sort/11/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"青春\",\"url\":\"/sort/12/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"豪门\",\"url\":\"/sort/14/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"耽美\",\"url\":\"/sort/15/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"其他\",\"url\":\"/sort/13/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}}]", + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36\"\n}", + "lastUpdateTime": 1696076607509, + "loginUrl": "", + "respondTime": 8470, + "ruleBookInfo": { + "author": ".author[0]@text", + "coverUrl": "img.0@src", + "init": "", + "intro": ".breviary@html", + "kind": ".data_list@i.0@text&&.list@i@text##\\s.*", + "lastChapter": ".list@a@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "id.main_title@h1@text##全文.*", + "tocUrl": ".button_list@a.2@href", + "wordCount": ".data_list@em.0@text##字" + }, + "ruleContent": { + "content": "id.ChapterContents@html", + "imageStyle": "", + "replaceRegex": "##一秒记住.*|免费小说.*|.本章完.|正在手打中.*|.*蚂蚁阅读.*|.*精华书阁.*|为您提供.*好书签.|.*免费阅读." + }, + "ruleExplore": { + "bookList": "" + }, + "ruleSearch": { + "author": "span@text", + "bookList": "tbody@tr!0", + "bookUrl": "a.1@href", + "checkKeyWord": "", + "coverUrl": "a.1@href##.+\\D((\\d+)\\d{3})\\D##/files/article/image/$2/$1/$1s.jpg###", + "kind": "a.0@text&&td.6:4@text##\\[|\\]", + "lastChapter": "a.2@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "a.1@text##全文.*", + "wordCount": "" + }, + "ruleToc": { + "chapterList": ".float-list@li", + "chapterName": "a@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "a@href", + "isVip": "", + "updateTime": "createdate" + }, + "searchUrl": "/modules/article/search.php?q={{key}}&searchtype=all&s=17333194950446968473", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎉 乐文阁网", + "bookSourceType": 0, + "bookSourceUrl": "http://www.lewenge.la", + "customOrder": -8, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"榜单\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"总点击榜\",\"url\":\"/allvisit/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"月点击榜\",\"url\":\"/monthvisit/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"周点击榜\",\"url\":\"/weekvisit/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"日点击榜\",\"url\":\"/dayvisit/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"总推荐榜\",\"url\":\"/allvote/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"月推荐榜\",\"url\":\"/monthvote/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"周推荐榜\",\"url\":\"/weekvote/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"日推荐榜\",\"url\":\"/dayvote/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"总收藏榜\",\"url\":\"/goodnum/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"总字数榜\",\"url\":\"/size/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"最新入库\",\"url\":\"/postdate/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"最近更新\",\"url\":\"/lastupdate/{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"分类\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"魔幻玄幻\",\"url\":\"/fenlei1_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"仙侠修真\",\"url\":\"/fenlei2_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"都市青春\",\"url\":\"/fenlei3_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"历史军事\",\"url\":\"/fenlei4_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"科幻游戏\",\"url\":\"/fenlei5_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"近代现代\",\"url\":\"/fenlei6_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"同人耽美\",\"url\":\"/fenlei7_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"架空历史\",\"url\":\"/fenlei8_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"重生异能\",\"url\":\"/fenlei9_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"恐怖推理\",\"url\":\"/fenlei10_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"完本小说\",\"url\":\"/wanben1_{{page}}.shtml\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.25,\"layout_flexGrow\":1}}]", + "lastUpdateTime": 1701434189878, + "loginUrl": "", + "respondTime": 3585, + "ruleBookInfo": { + "author": ".ui_tb1@em@text", + "coverUrl": ".pic@img@src", + "init": "", + "intro": ".intro@text##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".ui_tb1@tr.4@td.0@textNodes&&.ui_tb1@tr.4@td.2@textNodes&&.ui_tb1@tr.5@td.3@textNodes", + "lastChapter": ".con@a.0@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": ".ui_tb1@h1@ownText", + "tocUrl": ".btopt@a@href", + "wordCount": "tbody@tr.5@td.1@textNodes##字" + }, + "ruleContent": { + "content": "id.content@p@html" + }, + "ruleExplore": { + "bookList": "" + }, + "ruleSearch": { + "author": "span.0@text##.*:", + "bookList": "id.alistbox", + "bookUrl": "a.0@href", + "coverUrl": "img@src", + "intro": ".intro@text", + "lastChapter": "a.2@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "a.1@text" + }, + "ruleToc": { + "chapterList": "tbody.0@td@a", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href" + }, + "searchUrl": "/modules/article/search.php,{\n \"method\": \"post\",\n \"body\": \"searchkey={{key}}\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎉 八一中文", + "bookSourceType": 0, + "bookSourceUrl": "https://m.bayizww.com", + "customOrder": -5, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"周榜\",\"url\":\"/yd/ph/week.html\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻\",\"url\":\"/yd/ph/week1_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"修真\",\"url\":\"/yd/ph/week2_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/yd/ph/week3_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"穿越\",\"url\":\"/yd/ph/week4_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"网游\",\"url\":\"/yd/ph/week5_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/yd/ph/week6_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"其他\",\"url\":\"/yd/ph/week7_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"月榜\",\"url\":\"/yd/ph/month.html\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻\",\"url\":\"/yd/ph/month1_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"修真\",\"url\":\"/yd/ph/month2_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/yd/ph/month3_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"穿越\",\"url\":\"/yd/ph/month4_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"网游\",\"url\":\"/yd/ph/month5_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/yd/ph/month6_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"其他\",\"url\":\"/yd/ph/month7_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"总榜\",\"url\":\"/yd/ph/all.html\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻\",\"url\":\"/yd/ph/all1_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"修真\",\"url\":\"/yd/ph/all2_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/yd/ph/all3_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"穿越\",\"url\":\"/yd/ph/all4_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"网游\",\"url\":\"/yd/ph/all5_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/yd/ph/all6_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"其他\",\"url\":\"/yd/ph/all7_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"分类\",\"url\":\"/yd/xclass/0/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻\",\"url\":\"/yd/xclass/1/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"修真\",\"url\":\"/yd/xclass/2/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/yd/xclass/3/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"穿越\",\"url\":\"/yd/xclass/4/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"网游\",\"url\":\"/yd/xclass/5/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/yd/xclass/6/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"其他\",\"url\":\"/yd/xclass/7/{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"全本\",\"url\":\"/yd/quanben.html\",\"style\":{\"layout_flexBasisPercent\":1,\"layout_flexGrow\":1}},{\"title\":\"玄幻\",\"url\":\"/yd/quanben1_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"修真\",\"url\":\"/yd/quanben2_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/yd/quanben3_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"穿越\",\"url\":\"/yd/quanben4_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"网游\",\"url\":\"/yd/quanben5_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/yd/quanben6_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"其他\",\"url\":\"/yd/quanben7_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}}]", + "lastUpdateTime": 1701425597176, + "respondTime": 1424, + "ruleBookInfo": { + "author": ".synopsisArea_detail@p.0@text", + "coverUrl": ".synopsisArea_detail@img@src", + "intro": ".review@text##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".synopsisArea_detail@p.1:2:4@text##.*:|小说", + "lastChapter": ".directoryArea@a.0@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": ".channelHeader@span@text", + "tocUrl": ".btn@a.0@href" + }, + "ruleContent": { + "content": "id.chaptercontent@textNodes", + "nextContentUrl": "id.pt_next@href", + "replaceRegex": "##\\s*喜欢.*请大家收藏.*\\s*|\\s*.*更新速度全网最快。\\s*|\\s*最新网址:www.*com\\s*|wap.|\\s*【请收藏本站.*】\\s*|\\(\\)\\;" + }, + "ruleExplore": { + "author": "p.1@text", + "bookList": ".hot_sale", + "bookUrl": "a@href", + "coverUrl": "a@href##.+\\D((\\d+)\\d{3})\\D##http://img.bayizww.com/$2/$1/$1s.jpg###", + "intro": "p.2@text##简介.", + "lastChapter": "", + "name": "p.0@text" + }, + "ruleSearch": { + "author": "p.1@text##.*:", + "bookList": ".hot_sale", + "bookUrl": "a@href", + "checkKeyWord": "青春", + "coverUrl": "a@href##.+\\D((\\d+)\\d{3})\\D##http://img.bayizww.com/$2/$1/$1s.jpg###", + "kind": "p.1:2@text##小说|\\s.*", + "lastChapter": "p.2@text##.*:|正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "p.0@text" + }, + "ruleToc": { + "chapterList": "id.chapterlist@p@a", + "chapterName": "text##.直达页面底部|正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href", + "nextTocUrl": "" + }, + "searchUrl": "/s.php,{\n \"method\": \"post\",\n \"body\": \"keyword={{key}}\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎉 手机小说", + "bookSourceType": 0, + "bookSourceUrl": "https://www.shoujixs.net", + "bookUrlPattern": "", + "concurrentRate": "", + "customOrder": -4, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"玄幻\",\"url\":\"/xhqh_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"武侠\",\"url\":\"/wx_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"仙侠\",\"url\":\"/xx_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"都市\",\"url\":\"/ds_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"军事\",\"url\":\"/js_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"历史\",\"url\":\"/ls_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"游戏\",\"url\":\"/yxty_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"科幻\",\"url\":\"/kh_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"灵异\",\"url\":\"/ly_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"二次\",\"url\":\"/ecy_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"言情\",\"url\":\"/yq_{{page}}.html\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}},{\"title\":\"\",\"url\":\"\",\"style\":{\"layout_flexBasisPercent\":0.2,\"layout_flexGrow\":1}}]", + "header": "", + "lastUpdateTime": 1701434296651, + "loginCheckJs": "", + "loginUrl": "", + "respondTime": 6981, + "ruleBookInfo": { + "author": "id.muluzuoceh@p.0@text", + "coverUrl": "id.fmimg@img@src", + "intro": "id.shojixsinto@p.0@text##(^|[。!?]+[”」)】]?)##$1
", + "kind": ".shoujixsml2@a.2@text&&id.muluzuoceh@p.1@text##小说|.*:|\\s..:.*", + "lastChapter": "id.muluzuoceh@a@text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "name": "id.muluzuoceh@h1@text", + "wordCount": "id.muluzuoceh@p.3@text##.*:" + }, + "ruleContent": { + "content": "id.zjny@html" + }, + "ruleExplore": { + "author": ".s5@text", + "bookList": ".shoujixsl@li", + "bookUrl": "a.0@href", + "coverUrl": "a.0@href##.+\\D((\\d+)\\d{3})\\D##https://images.shoujixs.net/$2/$1/$1s.jpg###", + "intro": "", + "kind": "", + "lastChapter": "a.1@text", + "name": "a.0@text", + "wordCount": "" + }, + "ruleSearch": { + "author": "p.1@text##.*作者.", + "bookList": ".hot_sale", + "bookUrl": "a@href##^##https://www.shoujixs.net", + "checkKeyWord": "", + "coverUrl": "a@href##.+\\D((\\d+)\\d{3})\\D##https://images.shoujixs.net/$2/$1/$1s.jpg###", + "intro": "", + "kind": "p.1:2@text##小说|\\s.*", + "lastChapter": "p.2@text##.*更新.", + "name": "p.0@text", + "wordCount": "" + }, + "ruleToc": { + "chapterList": "id.lbks@dd@a", + "chapterName": "text##正文卷.|正文.|VIP卷.|默认卷.|卷_|VIP章节.|免费章节.|章节目录.|最新章节.|[\\((【].*?[求更票谢乐发订合补加架字修Kk].*?[】)\\)]", + "chapterUrl": "href", + "nextTocUrl": ".right@a@href" + }, + "searchUrl": "https://wap.shoujixs.net/s.php,{\n \"method\": \"post\",\n \"body\": \"keyword={{key}}\"\n}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🎨 漫客栈", + "bookSourceType": 2, + "bookSourceUrl": "https://comic.mkzhan.com/", + "bookUrlPattern": "", + "customOrder": 1, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "热门人气::https://comic.mkzcdn.com/search/filter/?order=1&page_num={{page}}&page_size=12\n更新时间::https://comic.mkzcdn.com/search/filter/?order=2&page_num={{page}}&page_size=12\n连载::https://comic.mkzcdn.com/search/filter/?finish=1&page_num={{page}}&page_size=12\n完结::https://comic.mkzcdn.com/search/filter/?finish=2&page_num={{page}}&page_size=12\n霸总::https://comic.mkzcdn.com/search/filter/?theme_id=1&page_num={{page}}&page_size=12\n修真::https://comic.mkzcdn.com/search/filter/?theme_id=2&page_num={{page}}&page_size=12\n恋爱::https://comic.mkzcdn.com/search/filter/?theme_id=3&page_num={{page}}&page_size=12\n校园::https://comic.mkzcdn.com/search/filter/?theme_id=4&page_num={{page}}&page_size=12\n冒险::https://comic.mkzcdn.com/search/filter/?theme_id=5&page_num={{page}}&page_size=12\n搞笑::https://comic.mkzcdn.com/search/filter/?theme_id=6&page_num={{page}}&page_size=12\n生活::https://comic.mkzcdn.com/search/filter/?theme_id=7&page_num={{page}}&page_size=12\n热血::https://comic.mkzcdn.com/search/filter/?theme_id=8&page_num={{page}}&page_size=12\n架空::https://comic.mkzcdn.com/search/filter/?theme_id=9&page_num={{page}}&page_size=12\n后宫::https://comic.mkzcdn.com/search/filter/?theme_id=10&page_num={{page}}&page_size=12\n耽美::https://comic.mkzcdn.com/search/filter/?theme_id=11&page_num={{page}}&page_size=12\n玄幻::https://comic.mkzcdn.com/search/filter/?theme_id=12&page_num={{page}}&page_size=12\n悬疑::https://comic.mkzcdn.com/search/filter/?theme_id=13&page_num={{page}}&page_size=12\n恐怖::https://comic.mkzcdn.com/search/filter/?theme_id=14&page_num={{page}}&page_size=12\n灵异::https://comic.mkzcdn.com/search/filter/?theme_id=15&page_num={{page}}&page_size=12\n动作::https://comic.mkzcdn.com/search/filter/?theme_id=16&page_num={{page}}&page_size=12\n科幻::https://comic.mkzcdn.com/search/filter/?theme_id=17&page_num={{page}}&page_size=12\n战争::https://comic.mkzcdn.com/search/filter/?theme_id=18&page_num={{page}}&page_size=12\n古风::https://comic.mkzcdn.com/search/filter/?theme_id=19&page_num={{page}}&page_size=12\n穿越::https://comic.mkzcdn.com/search/filter/?theme_id=20&page_num={{page}}&page_size=12\n竞技::https://comic.mkzcdn.com/search/filter/?theme_id=21&page_num={{page}}&page_size=12\n百合::https://comic.mkzcdn.com/search/filter/?theme_id=22&page_num={{page}}&page_size=12\n励志::https://comic.mkzcdn.com/search/filter/?theme_id=23&page_num={{page}}&page_size=12\n同人::https://comic.mkzcdn.com/search/filter/?theme_id=24&page_num={{page}}&page_size=12\n真人::https://comic.mkzcdn.com/search/filter/?theme_id=26&page_num={{page}}&page_size=12\n少年::https://comic.mkzcdn.com/search/filter/?audience=1&page_num={{page}}&page_size=12\n少女::https://comic.mkzcdn.com/search/filter/?audience=2&page_num={{page}}&page_size=12\n青年::https://comic.mkzcdn.com/search/filter/?audience=3&page_num={{page}}&page_size=12\n少儿::https://comic.mkzcdn.com/search/filter/?audience=4&page_num={{page}}&page_size=12\n独家::https://comic.mkzcdn.com/search/filter/?copyright=1&page_num={{page}}&page_size=12\n合作::https://comic.mkzcdn.com/search/filter/?copyright=2&page_num={{page}}&page_size=12\n免费::https://comic.mkzcdn.com/search/filter/?is_free=1&page_num={{page}}&page_size=12\n付费::https://comic.mkzcdn.com/search/filter/?is_fee=1&page_num={{page}}&page_size=12\nVIP::https://comic.mkzcdn.com/search/filter/?is_vip=1&page_num={{page}}&page_size=12", + "header": "", + "lastUpdateTime": 1676877861667, + "loginUrl": "", + "respondTime": 1983, + "ruleBookInfo": { + "intro": "$.data.content", + "lastChapter": "$.data.chapter_title", + "name": "$.data.title", + "tocUrl": "https://comic.mkzhan.com/chapter/?comic_id={{$.data.comic_id}}\n@js:\nz=result\na={\n \"body\": \"oauth_id=402&oauth_data=%7B%22unionid%22%3A%223227d2ee22e30fcd847e78da026ee6da%22%7D\",\n \"method\": \"POST\"\n}\nb=java.ajax(\"https://member.mkzcdn.com/login/oauth/,\"+JSON.stringify(a))\njava.log(b)\ndata=JSON.parse(b).data\nsign=data.sign\nuid=data.uid\njava.put(\"sign\",sign)\njava.put(\"uid\",uid)\nz" + }, + "ruleContent": { + "content": "@js:\nlet list = JSON.parse(result).data.page.map((i)=>{\n\treturn ''\n\t})\nString(list).replace(new RegExp(\",\",\"gi\"),\"\")" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "$.author_title", + "bookList": "$.data.list.*", + "bookUrl": "https://comic.mkzhan.com/comic/info/?comic_id={{$.comic_id}}", + "coverUrl": "$.cover@js:result.replace(/\\.(jpg|JPG).*/g,\".$1!cover-400\")", + "lastChapter": "$.chapter_title", + "name": "@js:\njava.put(\"comic_id\",result.comic_id)\nresult.title" + }, + "ruleToc": { + "chapterList": "$.data.*", + "chapterName": "$.title", + "chapterUrl": "\nhttps://comic.mkzhan.com/chapter/content/v1/?chapter_id={{$.chapter_id}}&comic_id={{java.get(\"comic_id\")}}&format=1&quality=1&sign={{java.get(\"sign\")}}&type=1&uid={{java.get(\"uid\")}}", + "isVip": "$.is_vip", + "updateTime": "" + }, + "searchUrl": "https://comic.mkzhan.com/search/keyword/?keyword={{key}}&page_num={{page}}&page_size=20", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "✐ 新漫画", + "bookSourceType": 2, + "bookSourceUrl": "https://xapi.xinmanhua.net#♤Haxc", + "bookUrlPattern": "", + "customOrder": 2, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "全部::https://xapi.xinmanhua.net/search/catalog?keyword=all&ischarge=2&isfinish=2&order=0&page={{page}}\n治愈::https://xapi.xinmanhua.net/search/catalog?keyword=治愈&page={{page}}\n日常::https://xapi.xinmanhua.net/search/catalog?keyword=日常&page={{page}}\n都市::https://xapi.xinmanhua.net/search/catalog?keyword=都市&page={{page}}\n悬疑::https://xapi.xinmanhua.net/search/catalog?keyword=悬疑&page={{page}}\n科幻::https://xapi.xinmanhua.net/search/catalog?keyword=科幻&page={{page}}\n奇幻::https://xapi.xinmanhua.net/search/catalog?keyword=奇幻&page={{page}}\n热血::https://xapi.xinmanhua.net/search/catalog?keyword=热血&page={{page}}\n武侠::https://xapi.xinmanhua.net/search/catalog?keyword=武侠&page={{page}}\n古风::https://xapi.xinmanhua.net/search/catalog?keyword=古风&page={{page}}\n恋爱::https://xapi.xinmanhua.net/search/catalog?keyword=恋爱&page={{page}}\n少年::https://xapi.xinmanhua.net/search/catalog?keyword=少年&page={{page}}\n日漫::https://xapi.xinmanhua.net/search/catalog?keyword=日漫&page={{page}}\n原创::https://xapi.xinmanhua.net/search/catalog?keyword=原创&page={{page}}", + "header": "{\n \"User-Agent\": \"okhttp/3.10.0\"\n}", + "lastUpdateTime": 1728717432248, + "loginUrl": "", + "respondTime": 1517, + "ruleBookInfo": { + "intro": "@JSon:$.data.attributes.intro&&$.data.attributes.description" + }, + "ruleContent": { + "content": "$.data[*].attributes.url\n@js:list=result.split('\\n');\nlist.map(x=>'').join('\\n')", + "imageStyle": "FULL", + "webJs": "" + }, + "ruleExplore": {}, + "ruleSearch": { + "author": "@JSon:$.attributes.author", + "bookList": "@JSon:$.data.*", + "bookUrl": "https://xapi.xinmanhua.net/worksinfos/{$.attributes.wid}?include=chapters.digests", + "coverUrl": "@JSon:$.attributes.logo_detail", + "kind": "@JSon:$.attributes.workscate", + "lastChapter": "@JSon:$.attributes.lastctitle", + "name": "@JSon:$.attributes.title" + }, + "ruleToc": { + "chapterList": "$.included.*", + "chapterName": "$.attributes.title&&$.attributes.intro", + "chapterUrl": "https://xapi.xinmanhua.net/chapters/{$.id}/qualitywebs" + }, + "searchUrl": "https://xapi.xinmanhua.net/search/selfdefine,{\n \"method\": \"POST\",\n \"body\": \"keyword={{key}}&page={{page}}\"\n}", + "weight": 57 + }, + { + "bookSourceComment": "haxc", + "bookSourceGroup": "", + "bookSourceName": "🌐仙漫漫画", + "bookSourceType": 2, + "bookSourceUrl": "https://www.xianman123.com", + "customOrder": 4, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "连载::https://www.xianman123.com/f-1-0-0-1-0-0-{{page}}.html\n完结::https://www.xianman123.com/f-1-0-0-2-0-0-{{page}}.html\n国漫::https://www.xianman123.com/f-1-0-1-0-0-0-{{page}}.html\n日漫::https://www.xianman123.com/f-1-0-2-0-0-0-{{page}}.html\n更新::https://www.xianman123.com/f-1-0-0-0-0-0-{{page}}.html\n排行::https://www.xianman123.com/f-1-0-0-0-0-1-{{page}}.html", + "lastUpdateTime": 1728715383989, + "respondTime": 1984, + "ruleBookInfo": { + "author": ".subtitle@a@text", + "init": "", + "intro": ".content@text", + "kind": ".ticai@a@text", + "lastChapter": "#detail-list-select-1@li.0@a@text", + "name": "h1@text" + }, + "ruleContent": { + "content": "@js:\r\ntry{\r\neval(result.match(/(eval\\([\\s\\S]+?)<\\/script/)[1]);\nhost=result.match(/imgDomain = '(.*?)';/)[1];\r\npicdata.map(a=>\r\n'').join(\"\\n\")\r\n}catch(e){}", + "imageStyle": "FULL" + }, + "ruleExplore": { + "bookList": "" + }, + "ruleSearch": { + "bookList": ".mh-list@li", + "bookUrl": "a@href", + "coverUrl": "img@src", + "lastChapter": "p@a@text", + "name": "h2@text" + }, + "ruleToc": { + "chapterList": "-#detail-list-select-1@li@a", + "chapterName": "textNodes", + "chapterUrl": "href", + "updateTime": "span@text" + }, + "searchUrl": "https://www.xianmanwang.com/index/index/search.html?keyboard={{key}}", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🌐 咪咕动漫", + "bookSourceType": 0, + "bookSourceUrl": "http://m.migudm.cn", + "bookUrlPattern": "", + "customOrder": 5, + "enabled": true, + "enabledCookieJar": true, + "enabledExplore": true, + "exploreUrl": "[{\"title\":\"人气\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"月票\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=5\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"更新\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=1\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"完结\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&statusId=7000\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"连载\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&statusId=7001\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"女生\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&consumerId=180001\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"少年\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&consumerId=180002\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"男生\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&consumerId=180000\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"情感\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15003\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"热血\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15017\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"搞笑\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=45000\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"玄幻\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15021\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"都市\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15024\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"校园\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15012\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"悬疑\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15005\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"古风\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15039\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"科幻\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15004\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"运动\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15002\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"萌系\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15025\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"恐怖\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15013\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"武侠\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15020\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"怀旧\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15043\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"资讯\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15044\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"亲子\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&themeId=15042\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"大陆\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&areaId=5100\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"日本\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&areaId=5102\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"韩国\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&areaId=5104\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"港台\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&areaId=5101\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"欧美\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&areaId=5103\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}},\n{\"title\":\"其他\",\"url\":\"http://m.migudm.cn/api/listOpus.html?opusType=2&pageNo={{page}}&pageSize=20&orderType=3&areaId=5105\",\"style\":{\"layout_flexGrow\":1,\"layout_flexBasisPercent\":0.2}}]", + "header": "{\n \"User-Agent\": \"Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/16D57 UCBrowser/12.3.0.1138 Mobile AliApp(TUnionSDK/0.1.20.3)\"\n}", + "lastUpdateTime": 1728715400103, + "loginUrl": "", + "respondTime": 312267, + "ruleBookInfo": { + "author": "data.opusDetail.authorName", + "init": "", + "intro": "data.opusDetail.opusDesc&&data.opusDetail.updateTimeresult.replace(/(.*)\\s(.*)/,'  更新时间:$2\\n‎\\n$1')", + "kind": "data.opusDetail.themeList..themeName&&data.opusDetail.statusValue", + "lastChapter": "data.opusDetail.lastItemTitle", + "name": "data.opusDetail.opusName", + "wordCount": "data.opusDetail.updateTime" + }, + "ruleContent": { + "content": "$.data.jpgList.[*]url\n@js:\nlist=result.split(\"\\n\");\nhtml='';\nfor(i in list){\nhtml+='\\n'\n}\nhtml", + "imageStyle": "FULL" + }, + "ruleExplore": { + "bookList": "" + }, + "ruleSearch": { + "author": "authorName", + "bookList": "data.comicResult.opusDetailList||data.opusList", + "bookUrl": "http://m.migudm.cn/api/opusDetail.html?hwOpusId={$.hwOpusId}&opusType={$.opusType}&order=0", + "checkKeyWord": "战魂纪", + "coverUrl": "opusUrl", + "intro": "opusDesc", + "kind": "themeList..themeName&&statusValue", + "lastChapter": "lastItemName&&updateTime\nresult.replace(/(.*)\\s/,'$1 • ')", + "name": "opusName", + "wordCount": "updateTime" + }, + "ruleToc": { + "chapterList": "$.data.itemList", + "chapterName": "isFee&&itemName@js:result.replace(/^0\\s/g,'').replace(/^1\\s/g,'✿·')", + "chapterUrl": "http://m.migudm.cn/api/webQueryWatchOpusInfo.html?hwOpusId={$.hwOpusId}&index={$.index}&opusType={$.opusType}", + "isVolume": "" + }, + "searchUrl": "http://m.migudm.cn/api/searchOpus.html?hintKey={{key}}&pageNo={{page}}&pageSize=20&opusType=2&actionSource=02&inputType=01", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🌐看漫画网", + "bookSourceType": 2, + "bookSourceUrl": "https://m.kanman.com", + "customOrder": 6, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "人气::https://m.kanman.com/api/getsortlist/?comic_sort=&orderby=click&search_type=&search_key=&page={{page}}&size=48\n更新::https://m.kanman.com/api/getsortlist/?comic_sort=&orderby=date&search_type=&search_key=&page={{page}}&size=48\n评分::https://m.kanman.com/api/getsortlist/?comic_sort=&orderby=score&search_type=&search_key=&page={{page}}&size=48\n收藏::https://m.kanman.com/api/getsortlist/?comic_sort=&orderby=shoucang&search_type=&search_key=&page={{page}}&size=48\n热血::https://m.kanman.com/api/getsortlist/?comic_sort=rexue&orderby=&search_type=&search_key=&page={{page}}&size=48\n机战::https://m.kanman.com/api/getsortlist/?comic_sort=jizhan&orderby=&search_type=&search_key=&page={{page}}&size=48\n运动::https://m.kanman.com/api/getsortlist/?comic_sort=yundong&orderby=&search_type=&search_key=&page={{page}}&size=48\n推理::https://m.kanman.com/api/getsortlist/?comic_sort=tuili&orderby=&search_type=&search_key=&page={{page}}&size=48\n冒险::https://m.kanman.com/api/getsortlist/?comic_sort=maoxian&orderby=&search_type=&search_key=&page={{page}}&size=48\n搞笑::https://m.kanman.com/api/getsortlist/?comic_sort=gaoxiao&orderby=&search_type=&search_key=&page={{page}}&size=48\n战争::https://m.kanman.com/api/getsortlist/?comic_sort=zhanzhen&orderby=&search_type=&search_key=&page={{page}}&size=48\n神魔::https://m.kanman.com/api/getsortlist/?comic_sort=shenmo&orderby=&search_type=&search_key=&page={{page}}&size=48\n竞技::https://m.kanman.com/api/getsortlist/?comic_sort=jingji&orderby=&search_type=&search_key=&page={{page}}&size=48\n悬疑::https://m.kanman.com/api/getsortlist/?comic_sort=xuanyi&orderby=&search_type=&search_key=&page={{page}}&size=48\n社会::https://m.kanman.com/api/getsortlist/?comic_sort=shehui&orderby=&search_type=&search_key=&page={{page}}&size=48\n恋爱::https://m.kanman.com/api/getsortlist/?comic_sort=lianai&orderby=&search_type=&search_key=&page={{page}}&size=48\n宠物::https://m.kanman.com/api/getsortlist/?comic_sort=chongwu&orderby=&search_type=&search_key=&page={{page}}&size=48\n吸血::https://m.kanman.com/api/getsortlist/?comic_sort=xixue&orderby=&search_type=&search_key=&page={{page}}&size=48\n萝莉::https://m.kanman.com/api/getsortlist/?comic_sort=luoli&orderby=&search_type=&search_key=&page={{page}}&size=48\n后宫::https://m.kanman.com/api/getsortlist/?comic_sort=hougong&orderby=&search_type=&search_key=&page={{page}}&size=48\n御姐::https://m.kanman.com/api/getsortlist/?comic_sort=yujie&orderby=&search_type=&search_key=&page={{page}}&size=48\n霸总::https://m.kanman.com/api/getsortlist/?comic_sort=bazong&orderby=&search_type=&search_key=&page={{page}}&size=48\n玄幻::https://m.kanman.com/api/getsortlist/?comic_sort=xuanhuan&orderby=&search_type=&search_key=&page={{page}}&size=48\n古风::https://m.kanman.com/api/getsortlist/?comic_sort=gufeng&orderby=&search_type=&search_key=&page={{page}}&size=48\n历史::https://m.kanman.com/api/getsortlist/?comic_sort=lishi&orderby=&search_type=&search_key=&page={{page}}&size=48\n漫改::https://m.kanman.com/api/getsortlist/?comic_sort=mangai&orderby=&search_type=&search_key=&page={{page}}&size=48\n游戏::https://m.kanman.com/api/getsortlist/?comic_sort=youxi&orderby=&search_type=&search_key=&page={{page}}&size=48\n穿越::https://m.kanman.com/api/getsortlist/?comic_sort=chuanyue&orderby=&search_type=&search_key=&page={{page}}&size=48\n恐怖::https://m.kanman.com/api/getsortlist/?comic_sort=kongbu&orderby=&search_type=&search_key=&page={{page}}&size=48\n真人::https://m.kanman.com/api/getsortlist/?comic_sort=zhenren&orderby=&search_type=&search_key=&page={{page}}&size=48\n科幻::https://m.kanman.com/api/getsortlist/?comic_sort=kehuan&orderby=&search_type=&search_key=&page={{page}}&size=48\n都市::https://m.kanman.com/api/getsortlist/?comic_sort=dushi&orderby=&search_type=&search_key=&page={{page}}&size=48\n武侠::https://m.kanman.com/api/getsortlist/?comic_sort=wuxia&orderby=&search_type=&search_key=&page={{page}}&size=48\n修真::https://m.kanman.com/api/getsortlist/?comic_sort=xiuzhen&orderby=&search_type=&search_key=&page={{page}}&size=48\n生活::https://m.kanman.com/api/getsortlist/?comic_sort=shenghuo&orderby=&search_type=&search_key=&page={{page}}&size=48\n动作::https://m.kanman.com/api/getsortlist/?comic_sort=dongzuo&orderby=&search_type=&search_key=&page={{page}}&size=48", + "lastUpdateTime": 1728715415244, + "respondTime": 649, + "ruleBookInfo": { + "author": "[property=\"og:novel:author\"]@content", + "init": "", + "kind": "[property=\"og:novel:category\"]@content", + "lastChapter": ".comic-update-status@text##.*-\\d+ ", + "name": "[property=\"og:novel:book_name\"]@content", + "tocUrl": "https://m.kanman.com/api/getchapterlist?product_id=1&productname=kmh&platformname=wap&comic_id=@get:{bid}@href", + "wordCount": "" + }, + "ruleContent": { + "content": "$.data.current_chapter.chapter_img_list@js:\nheaders={\"headers\":{\"Referer\":baseUrl}};\nresult.split(\"\\n\").map(x=>'').join(\"\\n\")", + "imageStyle": "FULL" + }, + "ruleExplore": { + "bookList": "" + }, + "ruleSearch": { + "author": "$.cartoon_author_list_name||$.comic_author", + "bookList": "$.data.data||$.data", + "bookUrl": "https://m.kanman.com/{{$.comic_id}}/", + "coverUrl": "https://image.yqmh.com/mh/{{$.comic_id}}.jpg-300x400.webp", + "intro": "$.cartoon_desc", + "kind": "$.comic_type##[a-zA-Z]|\\|", + "lastChapter": "$.latest_cartoon_topic_name||$.last_chapter_name", + "name": "$.comic_name@put:{bid:$.comic_id}" + }, + "ruleToc": { + "chapterList": "-$.data.[*]", + "chapterName": "$.chapter_name", + "chapterUrl": "https://m.kanman.com/api/getchapterinfov2?product_id=1&productname=kmh&platformname=wap&comic_id=@get:{bid}&chapter_newid={{$.chapter_newid}}&isWebp=1&quality=low" + }, + "searchUrl": "https://m.kanman.com/api/serachcomic/?product_id=1&productname=kmh&platformname=wap&serachKey={{key}}&topNumber=10", + "weight": 0 + }, + { + "bookSourceComment": "", + "bookSourceGroup": "", + "bookSourceName": "🌐腾讯漫画", + "bookSourceType": 2, + "bookSourceUrl": "https://m.ac.qq.com", + "bookUrlPattern": "", + "customOrder": 12, + "enabled": true, + "enabledCookieJar": false, + "enabledExplore": true, + "exploreUrl": "条漫::https://m.ac.qq.com/category/listAll?type=tm&rank=upt&pageSize=30&page={{page}}\n独家::https://m.ac.qq.com/category/listAll?type=dj&rank=upt&pageSize=30&page={{page}}\n完结::https://m.ac.qq.com/category/listAll?type=wj&rank=upt&pageSize=30&page={{page}}\n日漫::https://m.ac.qq.com/category/listAll?type=rm&rank=upt&pageSize=30&page={{page}}\n恐怖::https://m.ac.qq.com/category/listAll?type=kb&rank=pgv&pageSize=30&page={{page}}\n妖怪::https://m.ac.qq.com/category/listAll?type=yg&rank=pgv&pageSize=30&page={{page}}\n恋爱::https://m.ac.qq.com/category/listAll?type=na&rank=pgv&pageSize=30&page={{page}}\n玄幻::https://m.ac.qq.com/category/listAll?type=xh&rank=pgv&pageSize=30&page={{page}}\n热血::https://m.ac.qq.com/category/listAll?type=rx&rank=pgv&pageSize=30&page={{page}}\n悬疑::https://m.ac.qq.com/category/listAll?type=xy&rank=pgv&pageSize=30&page={{page}}\n彩虹::https://m.ac.qq.com/category/listAll?type=dm&rank=pgv&pageSize=30&page={{page}}\n少女::https://m.ac.qq.com/category/listAll?type=sv&rank=pgv&pageSize=30&page={{page}}\n韩漫::https://m.ac.qq.com/category/listAll?type=hm&rank=pgv&pageSize=30&page={{page}}\n科幻::https://m.ac.qq.com/category/listAll?type=kh&rank=pgv&pageSize=30&page={{page}}\n逗比::https://m.ac.qq.com/category/listAll?type=db&rank=pgv&pageSize=30&page={{page}}\n校园::https://m.ac.qq.com/category/listAll?type=qcxy&rank=pgv&pageSize=30&page={{page}}\n都市::https://m.ac.qq.com/category/listAll?type=ds&rank=pgv&pageSize=30&page={{page}}\n治愈::https://m.ac.qq.com/category/listAll?type=zy&rank=pgv&pageSize=30&page={{page}}\n飙升榜::https://m.ac.qq.com/rank/index?type=rise&pageSize=10&page={{page}}\n畅销榜::https://m.ac.qq.com/rank/index?type=pay&pageSize=10&page={{page}}\n新作榜::https://m.ac.qq.com/rank/index?type=new&pageSize=10&page={{page}}\n真香榜::https://m.ac.qq.com/rank/index?type=hot&pageSize=10&page={{page}}", + "lastUpdateTime": 1728715427432, + "loginUrl": "", + "respondTime": 1470, + "ruleBookInfo": { + "author": "class.head-info-author@text##作者:", + "init": "", + "intro": "class.head-info-desc@text", + "lastChapter": "class.mod-chapter-title@tag.span.0@text##,", + "name": "class.head-title-tags@h1@text" + }, + "ruleContent": { + "content": "@js:\ndata=result.match(/data:\\s*'(.*?)'/)[1];\nnonce=result.match(/ + +""" + +IMAGE_HTML = """ + + + +""" + + +class FakeResponse: + def __init__(self, url: str, text: str = "", content: bytes = b"video-bytes" * 80, content_type: str = "video/mp4") -> None: + self.url = url + self.text = text + self.content = content + self.headers = {"content-type": content_type} + + def raise_for_status(self) -> None: + return None + + +class FakeClient: + def __init__(self, *args, **kwargs) -> None: + self.requests: list[str] = [] + + def __enter__(self) -> "FakeClient": + return self + + def __exit__(self, *args) -> None: + return None + + def get(self, url: str, *args, **kwargs) -> FakeResponse: + self.requests.append(url) + if "note" in url: + return FakeResponse("https://www.douyin.com/note/7321234567890123457", IMAGE_HTML) + if "v.douyin.com" in url: + return FakeResponse("https://www.douyin.com/video/7321234567890123456", VIDEO_HTML) + if url.endswith(".jpg") or url.endswith(".webp"): + return FakeResponse(url, content=b"image-bytes" * 80, content_type="image/jpeg") + return FakeResponse(url, VIDEO_HTML) + + +class DouyinPluginTests(unittest.TestCase): + def test_can_handle_douyin_urls(self) -> None: + plugin = DouyinPlugin() + self.assertTrue(plugin.can_handle("https://v.douyin.com/abc123/")) + self.assertTrue(plugin.can_handle("https://www.douyin.com/video/7321234567890123456")) + self.assertTrue(plugin.can_handle("https://www.douyin.com/note/7321234567890123457")) + self.assertFalse(plugin.can_handle("https://x.com/test/status/1")) + + @patch("plugins.douyin.plugin.httpx.Client", FakeClient) + def test_parse_video_from_render_data(self) -> None: + plugin = DouyinPlugin() + resource = plugin.parse("https://v.douyin.com/mock/")[0] + self.assertEqual(resource.id, "7321234567890123456") + self.assertEqual(resource.title, "test video") + self.assertEqual(resource.author, "sakura") + self.assertEqual(resource.files[0]["type"], "video") + self.assertIn("douyinvod.com/video.mp4", resource.files[0]["url"]) + + @patch("plugins.douyin.plugin.httpx.Client", FakeClient) + def test_parse_image_note(self) -> None: + plugin = DouyinPlugin() + resource = plugin.parse("https://www.douyin.com/note/7321234567890123457")[0] + self.assertEqual(resource.id, "7321234567890123457") + self.assertEqual(resource.title, "test images") + self.assertEqual([item["type"] for item in resource.files], ["image", "image"]) + + @patch("plugins.douyin.plugin.httpx.Client", FakeClient) + def test_download_video_entry(self) -> None: + plugin = DouyinPlugin() + resource = plugin.parse("https://v.douyin.com/mock/")[0] + with tempfile.TemporaryDirectory() as tmp: + paths = plugin.download(resource, Path(tmp)) + self.assertEqual(len(paths), 1) + self.assertTrue(paths[0].is_file()) + self.assertEqual(paths[0].suffix, ".mp4") + + def test_plugin_registration(self) -> None: + root = Path(__file__).resolve().parents[1] + if str(root) not in sys.path: + sys.path.insert(0, str(root)) + manager = PluginManager([root / "plugins"]) + plugin = manager.load_plugin(root / "plugins" / "douyin" / "plugin.py") + self.assertIsNotNone(plugin) + assert plugin is not None + self.assertIs(manager.get_plugin_for_url("https://www.douyin.com/video/7321234567890123456"), plugin) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_library_artist_repair.py b/tests/test_library_artist_repair.py new file mode 100644 index 0000000..c712fc0 --- /dev/null +++ b/tests/test_library_artist_repair.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +import json +import tempfile +from pathlib import Path +from unittest import TestCase + +from pixiv_app.core.library import PixivLibrary + + +class PixivLibraryArtistRepairTest(TestCase): + def test_setup_repairs_missing_artwork_artist_from_metadata(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "library.db" + library = PixivLibrary(db_path) + try: + with library.connection: + library.connection.execute( + """ + INSERT INTO artworks (artwork_id, artist_id, type, title, metadata_json, updated_at) + VALUES (?, NULL, 'illust', 'work', ?, CURRENT_TIMESTAMP) + """, + (123, json.dumps({"userId": 456, "userName": "Alice"})), + ) + finally: + library.close() + + repaired = PixivLibrary(db_path) + try: + row = repaired.connection.execute( + "SELECT artist_id FROM artworks WHERE artwork_id = 123" + ).fetchone() + artist = repaired.connection.execute( + "SELECT name FROM artists WHERE artist_id = 456" + ).fetchone() + finally: + repaired.close() + + self.assertEqual(row["artist_id"], 456) + self.assertEqual(artist["name"], "Alice") diff --git a/tests/test_local_gallery_creation.py b/tests/test_local_gallery_creation.py new file mode 100644 index 0000000..09e666a --- /dev/null +++ b/tests/test_local_gallery_creation.py @@ -0,0 +1,74 @@ +from __future__ import annotations + +import json +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +from PIL import Image + +from pixiv_app.core.library import PixivLibrary +from pixiv_app.core.gallery_service import LocalGalleryService +from pixiv_app.core.thumbnails import ThumbnailCache +from pixiv_app.gui.app import SakuraDownloaderGUI + + +class _HealthResponse: + status = 200 + + def __enter__(self) -> "_HealthResponse": + return self + + def __exit__(self, *args) -> None: + return None + + def read(self, _limit: int) -> bytes: + return json.dumps({"status": "ok"}).encode("utf-8") + + +class LocalGalleryCreationTests(unittest.TestCase): + def test_gallery_readiness_uses_health_endpoint(self) -> None: + app = object.__new__(SakuraDownloaderGUI) + with patch("pixiv_app.gui.app.urllib.request.urlopen", return_value=_HealthResponse()) as mocked: + self.assertTrue(app.is_gallery_wall_ready("http://127.0.0.1:8765")) + self.assertEqual(mocked.call_args.args[0], "http://127.0.0.1:8765/api/health") + + def test_thumbnail_cache_creates_local_parent_directories(self) -> None: + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + db_path = root / "data" / "library.db" + cache_dir = root / "data" / "runtime" / "thumbnails" + source = root / "source.png" + Image.new("RGB", (640, 480), "#f4aac6").save(source) + + library = PixivLibrary(db_path) + try: + library.connection.execute( + "INSERT INTO artworks (artwork_id, type, title, created_at, updated_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)", + (1001, "illust", "local thumbnail"), + ) + library.connection.execute( + "INSERT INTO artwork_files (artwork_id, page_index, file_path, source_url, status, updated_at) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)", + (1001, 0, str(source), "", "downloaded"), + ) + library.connection.commit() + finally: + library.close() + + results = ThumbnailCache(db_path=db_path, cache_dir=cache_dir).ensure_for_artwork(1001) + + self.assertEqual(len(results), 1) + self.assertTrue(Path(results[0].thumbnail_path).is_file()) + self.assertTrue(db_path.is_file()) + + def test_gallery_service_creates_database_immediately(self) -> None: + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + db_path = root / "data" / "library.db" + LocalGalleryService(db_path=db_path, thumbnail_dir=root / "data" / "thumbs") + self.assertTrue(db_path.is_file()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_ocr_gallery_search.py b/tests/test_ocr_gallery_search.py new file mode 100644 index 0000000..9d7c63a --- /dev/null +++ b/tests/test_ocr_gallery_search.py @@ -0,0 +1,183 @@ +from __future__ import annotations + +import tempfile +from pathlib import Path +from unittest import TestCase + +from fastapi import FastAPI +from fastapi.testclient import TestClient +from sqlalchemy import create_engine, select +from sqlalchemy.orm import sessionmaker + +from sakura_cloud.api.v1.router import api_router +from sakura_cloud.core.config import settings +from sakura_cloud.core.security import create_access_token +from sakura_cloud.db.base import Base +from sakura_cloud.db.session import get_db +from sakura_cloud.models.artwork import Artwork +from sakura_cloud.models.ocr import ArtworkOcrText +from sakura_cloud.models.user import User, UserRole +from sakura_cloud.services.ocr import run_ocr_for_artwork + + +class OcrGallerySearchTest(TestCase): + def setUp(self) -> None: + self.temp_dir = tempfile.TemporaryDirectory() + self.root = Path(self.temp_dir.name) + self.db_path = self.root / "cloud.db" + self.image_path = self.root / "image.jpg" + self.text_path = self.root / "note.txt" + self.image_path.write_bytes(b"fake image") + self.text_path.write_text("not image", encoding="utf-8") + + self.engine = create_engine(f"sqlite:///{self.db_path}", connect_args={"check_same_thread": False}) + self.SessionLocal = sessionmaker(bind=self.engine, autoflush=False, autocommit=False) + Base.metadata.create_all(self.engine) + + self.old_ocr_enabled = settings.ocr_enabled + settings.ocr_enabled = True + + self.app = FastAPI() + self.app.include_router(api_router, prefix=settings.api_prefix) + self.app.dependency_overrides[get_db] = self._override_db + self.client = TestClient(self.app) + + self.user_a_id = "user-a" + self.user_b_id = "user-b" + self.art_a_id = "art-a" + self.art_b_id = "art-b" + self.text_art_id = "text-art" + with self.SessionLocal() as db: + db.add_all( + [ + User( + id=self.user_a_id, + username="alice", + email="alice@example.test", + password_hash="unused", + role=UserRole.USER, + ), + User( + id=self.user_b_id, + username="bob", + email="bob@example.test", + password_hash="unused", + role=UserRole.USER, + ), + Artwork( + id=self.art_a_id, + user_id=self.user_a_id, + source_platform="pixiv", + source_id="100", + content_type="illust", + title="Sakura", + file_path=str(self.image_path), + file_size=10, + mime_type="image/jpeg", + tags=["flower"], + artwork_metadata={}, + ), + Artwork( + id=self.art_b_id, + user_id=self.user_b_id, + source_platform="pixiv", + source_id="200", + content_type="illust", + title="Other", + file_path=str(self.image_path), + file_size=10, + mime_type="image/jpeg", + tags=[], + artwork_metadata={}, + ), + Artwork( + id=self.text_art_id, + user_id=self.user_a_id, + source_platform="pixiv", + content_type="illust", + title="Text file", + file_path=str(self.text_path), + file_size=8, + mime_type="text/plain", + tags=[], + artwork_metadata={}, + ), + ] + ) + db.commit() + self.auth_a = {"Authorization": f"Bearer {create_access_token(self.user_a_id)}"} + self.auth_b = {"Authorization": f"Bearer {create_access_token(self.user_b_id)}"} + + def tearDown(self) -> None: + self.client.close() + self.engine.dispose() + settings.ocr_enabled = self.old_ocr_enabled + self.temp_dir.cleanup() + + def _override_db(self): + db = self.SessionLocal() + try: + yield db + finally: + db.close() + + def test_ocr_result_is_saved_and_searchable_by_scope(self) -> None: + with self.SessionLocal() as db: + artwork = db.get(Artwork, self.art_a_id) + self.assertIsNotNone(artwork) + row = run_ocr_for_artwork( + db, + artwork, + extractor=lambda _path: ("舰娘 远征记录", 0.92, "ch"), + ) + self.assertEqual(row.status, "done") + self.assertIn("舰娘", row.ocr_text) + + ocr_search = self.client.get("/api/v1/gallery?query=舰娘&search_scope=ocr", headers=self.auth_a) + self.assertEqual(ocr_search.status_code, 200) + items = ocr_search.json()["data"]["items"] + self.assertEqual([item["id"] for item in items], [self.art_a_id]) + self.assertEqual(items[0]["matched_by"], "ocr") + + title_search = self.client.get("/api/v1/gallery?query=舰娘&search_scope=title", headers=self.auth_a) + self.assertEqual(title_search.status_code, 200) + self.assertEqual(title_search.json()["data"]["items"], []) + + def test_ocr_data_is_isolated_by_user(self) -> None: + with self.SessionLocal() as db: + artwork = db.get(Artwork, self.art_a_id) + run_ocr_for_artwork(db, artwork, extractor=lambda _path: ("舰娘", 0.9, "ch")) + + other_user = self.client.get("/api/v1/gallery?query=舰娘&search_scope=ocr", headers=self.auth_b) + self.assertEqual(other_user.status_code, 200) + self.assertEqual(other_user.json()["data"]["items"], []) + self.assertEqual(self.client.get(f"/api/v1/gallery/{self.art_a_id}/ocr", headers=self.auth_b).status_code, 404) + + def test_non_image_file_is_skipped(self) -> None: + with self.SessionLocal() as db: + artwork = db.get(Artwork, self.text_art_id) + row = run_ocr_for_artwork(db, artwork, extractor=lambda _path: ("should not run", 1.0, "ch")) + self.assertEqual(row.status, "failed") + self.assertEqual(row.error, "unsupported_file_type") + + stored = db.scalar(select(ArtworkOcrText).where(ArtworkOcrText.artwork_id == self.text_art_id)) + self.assertIsNotNone(stored) + self.assertEqual(stored.ocr_text, "") + + def test_ocr_failure_does_not_block_gallery_create(self) -> None: + settings.ocr_enabled = False + response = self.client.post( + "/api/v1/gallery", + headers=self.auth_a, + json={ + "source_platform": "pixiv", + "content_type": "illust", + "title": "New upload", + "file_path": str(self.image_path), + "file_size": 10, + "mime_type": "image/jpeg", + "tags": [], + "metadata": {}, + }, + ) + self.assertEqual(response.status_code, 201) diff --git a/tests/test_online_search_engine.py b/tests/test_online_search_engine.py new file mode 100644 index 0000000..d7f2479 --- /dev/null +++ b/tests/test_online_search_engine.py @@ -0,0 +1,231 @@ +from __future__ import annotations + +import unittest +from unittest.mock import Mock, patch + +import requests + +from sakura_cloud.services.online_search_engine import ( + OnlineSearchEngine, + RuleConfig, + RuleManager, + build_search_request, + build_search_url, + parse_comic, + parse_novel, + resolve_resource_url, + title_match_score, +) + + +class OnlineSearchEngineTest(unittest.TestCase): + def test_resolve_resource_url_handles_relative_and_scheme_relative(self) -> None: + self.assertEqual( + resolve_resource_url("https://example.com/books/index.html", "../read/1.html"), + "https://example.com/read/1.html", + ) + self.assertEqual( + resolve_resource_url("https://example.com", "//cdn.example.com/a.webp"), + "https://cdn.example.com/a.webp", + ) + + def test_build_search_url_replaces_common_legado_placeholders(self) -> None: + keyword = "十宗罪" + template = "/search?q={{key}}&k={keyword}&s=%s&x=searchKey" + result = build_search_url(template, keyword) + self.assertNotIn("{{key}}", result) + self.assertNotIn("{keyword}", result) + self.assertNotIn("%s", result) + self.assertNotIn("searchKey", result) + self.assertIn("%E5%8D%81%E5%AE%97%E7%BD%AA", result) + + def test_build_search_request_supports_post_body(self) -> None: + url, data = build_search_request("POST /search@keyword={{key}}&page=1", "魔法", "https://novel.example.com") + self.assertEqual(url, "https://novel.example.com/search") + self.assertEqual(data, {"keyword": "%E9%AD%94%E6%B3%95", "page": "1"}) + + def test_title_match_score_uses_noise_words_and_fuzzy_match(self) -> None: + self.assertGreater(title_match_score("十宗罪 最新章节 免费阅读", "十宗罪"), 0.75) + self.assertGreater(title_match_score("千分之一的魔法", "千分之一魔法"), 0.55) + + def test_rule_manager_can_disable_rules(self) -> None: + manager = RuleManager([RuleConfig(rule_id="a", name="A", base_url="https://a.example", search_url="/s?q={{key}}")]) + self.assertEqual(len(manager.get_rules("all")), 1) + manager.disable_rule("a") + self.assertEqual(manager.get_rules("all"), []) + + def test_search_rule_parses_json_payload_without_network(self) -> None: + response = requests.Response() + response.status_code = 200 + response._content = ( + b'{"data":[{"bookName":"Sakura Magic","bookUrl":"/book/1","author":"Nana","cover":"/c.jpg"}]}' + ) + response.headers["content-type"] = "application/json; charset=utf-8" + session = Mock() + session.request.return_value = response + manager = RuleManager( + [ + RuleConfig( + rule_id="json", + name="JSON Source", + base_url="https://novel.example.com", + resource_type="novel", + search_url="/search?q={{key}}", + rule_search={ + "bookList": "$.data[*]", + "name": "$.bookName", + "bookUrl": "$.bookUrl", + "author": "$.author", + "cover": "$.cover", + }, + ) + ] + ) + engine = OnlineSearchEngine(manager, session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + rows = engine.search_resource("Sakura", "novel") + self.assertEqual(len(rows), 1) + self.assertEqual(rows[0].url, "https://novel.example.com/book/1") + self.assertEqual(rows[0].cover, "https://novel.example.com/c.jpg") + + def test_parse_novel_public_page_without_cookie(self) -> None: + html = """ + +

神秘复苏

第一章\n论坛里的故事。\n这里是正文。
+ """ + response = requests.Response() + response.status_code = 200 + response._content = html.encode("utf-8") + response.headers["content-type"] = "text/html; charset=utf-8" + session = Mock() + session.request.return_value = response + engine = OnlineSearchEngine(session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + parsed = engine.parse_novel("https://novel.example.com/read/1.html") + self.assertEqual(parsed.title, "神秘复苏") + self.assertIn("论坛里的故事", parsed.chapters[0].content or "") + self.assertEqual(parsed.cover, "https://novel.example.com/cover.jpg") + + def test_parse_comic_reads_lazy_loaded_images(self) -> None: + html = """ +

魔法漫画

+
+ + + """ + response = requests.Response() + response.status_code = 200 + response._content = html.encode("utf-8") + response.headers["content-type"] = "text/html; charset=utf-8" + session = Mock() + session.request.return_value = response + engine = OnlineSearchEngine(session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + parsed = engine.parse_comic("https://manga.example.com/c/1") + self.assertEqual(parsed.title, "魔法漫画") + self.assertEqual(parsed.chapters[0].images, ("https://manga.example.com/manga/001.webp",)) + + def test_http_444_is_reported_as_site_rejected(self) -> None: + response = requests.Response() + response.status_code = 444 + response._content = b"" + session = Mock() + session.request.return_value = response + engine = OnlineSearchEngine(session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + with self.assertRaisesRegex(Exception, "拒绝访问"): + engine.parse_comic("https://manga.example.com/c/1") + + def test_popular_manga_titles_can_be_found_from_html_cards(self) -> None: + html = """ +
    +
  • +
  • 火影忍者
  • +
  • +
+ """ + response = requests.Response() + response.status_code = 200 + response._content = html.encode("utf-8") + response.headers["content-type"] = "text/html; charset=utf-8" + session = Mock() + session.request.return_value = response + manager = RuleManager( + [RuleConfig(rule_id="manga-html", name="漫画源", base_url="https://manga.example.com", resource_type="manga", search_url="/search?q={{key}}")] + ) + engine = OnlineSearchEngine(manager, session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + one = engine.search_resource("一人之下", "manga") + naruto = engine.search_resource("火影忍者", "manga") + fox = engine.search_resource("狐妖小红娘", "manga") + self.assertEqual(one[0].url, "https://manga.example.com/comic/yirenzhixia") + self.assertEqual(naruto[0].cover, "https://manga.example.com/cover/naruto.jpg") + self.assertEqual(fox[0].title, "狐妖小红娘在线阅读") + + def test_popular_novel_titles_can_be_found_from_json(self) -> None: + response = requests.Response() + response.status_code = 200 + response._content = ( + '{"data":[{"bookName":"龙族","id":"dragon-raja","author":"江南"},' + '{"bookName":"乐可","bookUrl":"/book/leke","author":"匿名"}]}' + ).encode("utf-8") + response.headers["content-type"] = "application/json; charset=utf-8" + session = Mock() + session.request.return_value = response + manager = RuleManager( + [RuleConfig(rule_id="novel-json", name="小说源", base_url="https://novel.example.com", resource_type="novel", search_url="/api/search?key={{keyword}}")] + ) + engine = OnlineSearchEngine(manager, session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + dragon = engine.search_resource("龙族", "novel") + leke = engine.search_resource("乐可", "novel") + self.assertEqual(dragon[0].url, "https://novel.example.com/book/dragon-raja/") + self.assertEqual(leke[0].url, "https://novel.example.com/book/leke") + + def test_manga_script_images_can_be_viewed(self) -> None: + html = """ +

一人之下 第1话

+ + + """ + response = requests.Response() + response.status_code = 200 + response._content = html.encode("utf-8") + response.headers["content-type"] = "text/html; charset=utf-8" + session = Mock() + session.request.return_value = response + engine = OnlineSearchEngine(session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + parsed = engine.parse_comic("https://manga.example.com/comic/yirenzhixia/1") + self.assertEqual(parsed.chapters[0].images[0], "https://manga.example.com/pages/001.webp") + self.assertEqual(parsed.chapters[0].images[1], "https://cdn.example.com/pages/002.avif") + + def test_novel_body_can_be_read_for_dragon_and_leke_like_pages(self) -> None: + html = """ +

龙族

+

第一章 卡塞尔之门。

路明非站在雨里,故事从这里开始。

+ 下一章 + + """ + response = requests.Response() + response.status_code = 200 + response._content = html.encode("utf-8") + response.headers["content-type"] = "text/html; charset=utf-8" + session = Mock() + session.request.return_value = response + engine = OnlineSearchEngine(session=session) + with patch("sakura_cloud.services.online_search_engine.assert_public_url"): + parsed = engine.parse_novel("https://novel.example.com/read/1.html") + self.assertEqual(parsed.title, "龙族") + self.assertIn("路明非", parsed.chapters[0].content or "") + self.assertEqual(parsed.chapters[1].url, "https://novel.example.com/read/2.html") + + +class TopLevelFunctionTest(unittest.TestCase): + def test_top_level_parse_functions_are_available(self) -> None: + self.assertTrue(callable(parse_novel)) + self.assertTrue(callable(parse_comic)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_online_search_service.py b/tests/test_online_search_service.py new file mode 100644 index 0000000..3f21f1c --- /dev/null +++ b/tests/test_online_search_service.py @@ -0,0 +1,78 @@ +from __future__ import annotations + +import unittest +from unittest.mock import patch + +from sakura_cloud.services.online_search import _assert_public_url, search_sources + + +class OnlineSearchServiceTest(unittest.TestCase): + def test_novel_json_rule_search(self) -> None: + sources = [ + { + "bookSourceName": "Novel JSON", + "bookSourceUrl": "https://novel.example.com", + "bookSourceType": 0, + "searchUrl": "/search?q={{key}}", + "ruleSearch": { + "bookList": "$.data[*]", + "name": "$.name", + "bookUrl": "$.url", + "author": "$.author", + }, + } + ] + body = '{"data":[{"name":"十宗罪","url":"/book/1","author":"蜘蛛"}]}' + with patch("sakura_cloud.services.online_search._assert_public_url"), patch( + "sakura_cloud.services.online_search._fetch_text", + return_value=body, + ): + rows = search_sources(sources, "十宗罪", mode="novel") + self.assertEqual(len(rows), 1) + self.assertEqual(rows[0].title, "十宗罪") + self.assertEqual(rows[0].detail_url, "https://novel.example.com/book/1") + + def test_manga_json_fallback_search(self) -> None: + sources = [ + { + "bookSourceName": "Manga JSON", + "bookSourceUrl": "https://manga.example.com", + "bookSourceType": 2, + "searchUrl": "/search?q={{key}}", + "ruleSearch": {"bookList": "$.missing[*]"}, + } + ] + body = '{"data":[{"comic_name":"樱花漫画","comic_url":"/comic/1","cover_url":"/cover.jpg"}]}' + with patch("sakura_cloud.services.online_search._assert_public_url"), patch( + "sakura_cloud.services.online_search._fetch_text", + return_value=body, + ): + rows = search_sources(sources, "樱花", mode="manga") + self.assertEqual(len(rows), 1) + self.assertEqual(rows[0].cover_url, "https://manga.example.com/cover.jpg") + + def test_manga_html_card_fallback_search(self) -> None: + sources = [ + { + "bookSourceName": "Manga HTML", + "bookSourceUrl": "https://manga.example.com", + "bookSourceType": 2, + "searchUrl": "/search?q={{key}}", + } + ] + body = '' + with patch("sakura_cloud.services.online_search._assert_public_url"), patch( + "sakura_cloud.services.online_search._fetch_text", + return_value=body, + ): + rows = search_sources(sources, "魔法", mode="manga") + self.assertEqual(len(rows), 1) + self.assertEqual(rows[0].detail_url, "https://manga.example.com/manga/1") + + def test_private_url_is_rejected(self) -> None: + with self.assertRaises(Exception): + _assert_public_url("http://127.0.0.1:8000/search") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_origin_follow_sync.py b/tests/test_origin_follow_sync.py new file mode 100644 index 0000000..a3409df --- /dev/null +++ b/tests/test_origin_follow_sync.py @@ -0,0 +1,100 @@ +from __future__ import annotations + +import tempfile +import unittest +from unittest.mock import patch +import os +from pathlib import Path + +from pixiv_app.core.auth import SessionStore +from pixiv_app.core.gallery_service import LocalGalleryService +from pixiv_app.core.library import PixivLibrary +from pixiv_app.core.origin_follow_sync import OriginFollow, OriginFollowSyncResult, OriginFollowSyncService +from pixiv_app.core.paths import app_session_file + + +class OriginFollowSyncLocalTest(unittest.TestCase): + def test_pixiv_origin_follow_upsert_is_idempotent(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "library.db" + service = OriginFollowSyncService(db_path=str(db_path)) + result = OriginFollowSyncResult(platform="pixiv") + follows = [ + OriginFollow( + platform="pixiv", + external_id="12345", + name="Sakura Artist", + handle="sakura_artist", + avatar_url="https://i.pximg.net/user-profile/img.jpg", + profile_url="https://www.pixiv.net/users/12345", + ) + ] + + service._upsert_pixiv_local(follows, result) + second = OriginFollowSyncResult(platform="pixiv") + service._upsert_pixiv_local(follows, second) + + self.assertEqual(result.created, 1) + self.assertEqual(second.skipped, 1) + library = PixivLibrary(db_path) + try: + rows = library.connection.execute("SELECT artist_id, enabled FROM followed_artists").fetchall() + self.assertEqual(len(rows), 1) + self.assertEqual(int(rows[0]["artist_id"]), 12345) + self.assertEqual(int(rows[0]["enabled"]), 1) + finally: + library.close() + + def test_gallery_sync_reads_shared_app_session_cookie(self) -> None: + old_data_dir = os.environ.get("SAKURA_APP_DATA_DIR") + try: + with tempfile.TemporaryDirectory() as tmp: + os.environ["SAKURA_APP_DATA_DIR"] = tmp + db_path = Path(tmp) / "library.db" + SessionStore(app_session_file()).save({"cookie": "PHPSESSID=fake"}) + library = PixivLibrary(db_path) + try: + library.set_followed_artist(12345, True, "Sakura Artist") + finally: + library.close() + + service = LocalGalleryService(db_path=db_path) + with patch("pixiv_app.core.gallery_service.fetch_user_work_ids", return_value=(12345, [])) as mocked: + result = service.sync_followed_artists(download=False) + self.assertEqual(result["artists"][0]["artist_id"], 12345) + self.assertEqual(mocked.call_args.kwargs["cookie"], "PHPSESSID=fake") + finally: + if old_data_dir is None: + os.environ.pop("SAKURA_APP_DATA_DIR", None) + else: + os.environ["SAKURA_APP_DATA_DIR"] = old_data_dir + + def test_gallery_sync_keeps_new_pixiv_work_local(self) -> None: + old_data_dir = os.environ.get("SAKURA_APP_DATA_DIR") + try: + with tempfile.TemporaryDirectory() as tmp: + os.environ["SAKURA_APP_DATA_DIR"] = tmp + db_path = Path(tmp) / "library.db" + SessionStore(app_session_file()).save({"cookie": "PHPSESSID=fake"}) + library = PixivLibrary(db_path) + try: + library.set_followed_artist(12345, True, "Sakura Artist") + finally: + library.close() + + service = LocalGalleryService(db_path=db_path) + with patch("pixiv_app.core.gallery_service.fetch_user_work_ids", return_value=(12345, [67890])): + result = service.sync_followed_artists(download=False) + + self.assertEqual(result["artists"][0]["new_count"], 1) + self.assertEqual(result["artists"][0]["alerts"], 0) + self.assertFalse(result["artists"][0]["cloud_synced"]) + finally: + if old_data_dir is None: + os.environ.pop("SAKURA_APP_DATA_DIR", None) + else: + os.environ["SAKURA_APP_DATA_DIR"] = old_data_dir + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_plugin_manager.py b/tests/test_plugin_manager.py new file mode 100644 index 0000000..874016f --- /dev/null +++ b/tests/test_plugin_manager.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +import sys +import unittest +from pathlib import Path + +from pixiv_app.core.plugin.manager import PluginManager + + +class PluginManagerTests(unittest.TestCase): + def test_discover_and_load_pixiv(self) -> None: + root = Path(__file__).resolve().parents[1] + plugins_dir = root / "plugins" + plugin_py = plugins_dir / "pixiv" / "plugin.py" + if not plugin_py.is_file(): + self.skipTest("bundled plugins/pixiv not present") + + if str(root) not in sys.path: + sys.path.insert(0, str(root)) + + mgr = PluginManager([plugins_dir]) + paths = mgr.discover() + self.assertTrue(any(p.parent.name == "pixiv" for p in paths)) + + plugin = mgr.load_plugin(plugin_py) + self.assertIsNotNone(plugin) + assert plugin is not None + self.assertTrue(plugin.can_handle("https://www.pixiv.net/artworks/12345")) + mgr.unload_plugin(plugin.name) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_push_notifications.py b/tests/test_push_notifications.py new file mode 100644 index 0000000..0663c63 --- /dev/null +++ b/tests/test_push_notifications.py @@ -0,0 +1,169 @@ +from __future__ import annotations + +import tempfile +from pathlib import Path +from unittest import TestCase + +from fastapi import FastAPI +from fastapi.testclient import TestClient +from sqlalchemy import create_engine, select +from sqlalchemy.orm import sessionmaker + +from sakura_cloud.api.v1.router import api_router +from sakura_cloud.core.config import settings +from sakura_cloud.core.security import create_access_token +from sakura_cloud.db.base import Base +from sakura_cloud.db.session import get_db +from sakura_cloud.models.follow import UpdateAlert +from sakura_cloud.models.push import PushDeviceToken +from sakura_cloud.models.user import User, UserRole +from sakura_cloud.services.push import send_update_alert_push + + +class PushNotificationTest(TestCase): + def setUp(self) -> None: + self.temp_dir = tempfile.TemporaryDirectory() + self.db_path = Path(self.temp_dir.name) / "cloud.db" + self.engine = create_engine(f"sqlite:///{self.db_path}", connect_args={"check_same_thread": False}) + self.SessionLocal = sessionmaker(bind=self.engine, autoflush=False, autocommit=False) + Base.metadata.create_all(self.engine) + + self.app = FastAPI() + self.app.include_router(api_router, prefix=settings.api_prefix) + self.app.dependency_overrides[get_db] = self._override_db + self.client = TestClient(self.app) + + self.user_a_id = "user-a" + self.user_b_id = "user-b" + with self.SessionLocal() as db: + db.add_all( + [ + User( + id=self.user_a_id, + username="alice", + email="alice@example.test", + password_hash="unused", + role=UserRole.USER, + ), + User( + id=self.user_b_id, + username="bob", + email="bob@example.test", + password_hash="unused", + role=UserRole.USER, + ), + ] + ) + db.commit() + self.auth_a = {"Authorization": f"Bearer {create_access_token(self.user_a_id)}"} + self.auth_b = {"Authorization": f"Bearer {create_access_token(self.user_b_id)}"} + + def tearDown(self) -> None: + self.client.close() + self.engine.dispose() + self.temp_dir.cleanup() + + def _override_db(self): + db = self.SessionLocal() + try: + yield db + finally: + db.close() + + def test_push_endpoints_require_login(self) -> None: + cases = [ + ("GET", "/api/v1/push/devices", None), + ( + "POST", + "/api/v1/push/register-token", + {"device_id": "android-test", "platform": "android", "fcm_token": "token_1234567890:abcdef"}, + ), + ("POST", "/api/v1/push/unregister-token", {"device_id": "android-test"}), + ] + for method, path, body in cases: + with self.subTest(method=method, path=path): + response = self.client.request(method, path, json=body) + self.assertEqual(response.status_code, 401) + + def test_register_update_and_unregister_token(self) -> None: + payload = { + "device_id": "android-test", + "platform": "android", + "fcm_token": "token_1234567890:abcdef", + } + created = self.client.post("/api/v1/push/register-token", headers=self.auth_a, json=payload) + self.assertEqual(created.status_code, 201) + self.assertEqual(created.json()["data"]["device_id"], "android-test") + + updated = self.client.post( + "/api/v1/push/register-token", + headers=self.auth_a, + json={**payload, "fcm_token": "token_abcdef1234567890:updated"}, + ) + self.assertEqual(updated.status_code, 201) + + devices = self.client.get("/api/v1/push/devices", headers=self.auth_a) + self.assertEqual(devices.status_code, 200) + self.assertEqual(len(devices.json()["data"]), 1) + self.assertTrue(devices.json()["data"][0]["is_active"]) + + unregistered = self.client.post( + "/api/v1/push/unregister-token", + headers=self.auth_a, + json={"device_id": "android-test"}, + ) + self.assertEqual(unregistered.status_code, 200) + self.assertTrue(unregistered.json()["data"]["ok"]) + + devices_after = self.client.get("/api/v1/push/devices", headers=self.auth_a).json()["data"] + self.assertFalse(devices_after[0]["is_active"]) + + def test_users_cannot_list_or_unregister_other_users_devices(self) -> None: + self.client.post( + "/api/v1/push/register-token", + headers=self.auth_b, + json={ + "device_id": "android-b", + "platform": "android", + "fcm_token": "token_1234567890:bbbbbb", + }, + ) + + devices_a = self.client.get("/api/v1/push/devices", headers=self.auth_a) + self.assertEqual(devices_a.status_code, 200) + self.assertEqual(devices_a.json()["data"], []) + + self.client.post("/api/v1/push/unregister-token", headers=self.auth_a, json={"device_id": "android-b"}) + devices_b = self.client.get("/api/v1/push/devices", headers=self.auth_b).json()["data"] + self.assertEqual(len(devices_b), 1) + self.assertTrue(devices_b[0]["is_active"]) + + def test_invalid_token_is_disabled_after_push_failure(self) -> None: + with self.SessionLocal() as db: + token = PushDeviceToken( + user_id=self.user_a_id, + device_id="android-bad", + platform="android", + fcm_token="token_1234567890:invalid", + is_active=True, + ) + alert = UpdateAlert( + user_id=self.user_a_id, + platform="pixiv", + creator_id="42", + title="New Work", + body="body", + content_type="illust", + payload_json={"creator_name": "Sakura"}, + ) + db.add_all([token, alert]) + db.commit() + db.refresh(alert) + + summary = send_update_alert_push(db, alert, sender=lambda *_args: False) + self.assertEqual(summary.attempted, 1) + self.assertEqual(summary.disabled, 1) + + stored = db.scalar(select(PushDeviceToken).where(PushDeviceToken.device_id == "android-bad")) + self.assertIsNotNone(stored) + self.assertFalse(stored.is_active) diff --git a/tests/test_sakura_cloud_security.py b/tests/test_sakura_cloud_security.py new file mode 100644 index 0000000..9e03161 --- /dev/null +++ b/tests/test_sakura_cloud_security.py @@ -0,0 +1,301 @@ +from __future__ import annotations + +import tempfile +from pathlib import Path +from unittest import TestCase + +from fastapi import APIRouter, FastAPI +from fastapi.testclient import TestClient +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from sakura_cloud.api.deps import AdminUser +from sakura_cloud.api.v1.router import api_router +from sakura_cloud.core.config import settings +from sakura_cloud.core.safety import is_allowed_external_url +from sakura_cloud.core.security import create_access_token +from sakura_cloud.db.base import Base +from sakura_cloud.db.session import get_db +from sakura_cloud.models import Artwork, CreatorFollow, FileBlob, Novel, User, UserFile +from sakura_cloud.models.user import UserRole + + +class SakuraCloudSecurityTest(TestCase): + def setUp(self) -> None: + self.temp_dir = tempfile.TemporaryDirectory() + self.root = Path(self.temp_dir.name) + self.db_path = self.root / "cloud.db" + self.engine = create_engine(f"sqlite:///{self.db_path}", connect_args={"check_same_thread": False}) + self.SessionLocal = sessionmaker(bind=self.engine, autoflush=False, autocommit=False) + Base.metadata.create_all(self.engine) + + self.old_storage_root = settings.storage_root + self.old_max_upload_size_mb = settings.max_upload_size_mb + settings.storage_root = str(self.root / "storage") + settings.max_upload_size_mb = 1 + + admin_router = APIRouter() + + @admin_router.get("/admin/probe") + def admin_probe(_: AdminUser) -> dict[str, bool]: + return {"ok": True} + + self.app = FastAPI() + self.app.include_router(api_router, prefix=settings.api_prefix) + self.app.include_router(admin_router, prefix=settings.api_prefix) + self.app.dependency_overrides[get_db] = self._override_db + self.client = TestClient(self.app) + + self.user_a_id = "user-a" + self.user_b_id = "user-b" + self.admin_id = "admin" + self._seed_data() + self.auth_a = {"Authorization": f"Bearer {create_access_token(self.user_a_id)}"} + self.auth_b = {"Authorization": f"Bearer {create_access_token(self.user_b_id)}"} + self.auth_admin = {"Authorization": f"Bearer {create_access_token(self.admin_id)}"} + + def tearDown(self) -> None: + self.client.close() + self.engine.dispose() + settings.storage_root = self.old_storage_root + settings.max_upload_size_mb = self.old_max_upload_size_mb + self.temp_dir.cleanup() + + def _override_db(self): + db = self.SessionLocal() + try: + yield db + finally: + db.close() + + def _seed_data(self) -> None: + storage_file = self.root / "b-file.txt" + storage_file.write_text("secret", encoding="utf-8") + with self.SessionLocal() as db: + user_a = User( + id=self.user_a_id, + username="alice", + email="alice@example.test", + password_hash="unused", + role=UserRole.USER, + ) + user_b = User( + id=self.user_b_id, + username="bob", + email="bob@example.test", + password_hash="unused", + role=UserRole.USER, + ) + admin = User( + id=self.admin_id, + username="admin", + email="admin@example.test", + password_hash="unused", + role=UserRole.ADMIN, + ) + blob = FileBlob( + sha256="b" * 64, + storage_path=str(storage_file), + file_size=storage_file.stat().st_size, + mime_type="text/plain", + ) + user_file = UserFile(id="file-b", user_id=self.user_b_id, file_sha256=blob.sha256, original_name="b-file.txt") + artwork = Artwork( + id="art-b", + user_id=self.user_b_id, + file_id=user_file.id, + source_platform="pixiv", + content_type="illust", + title="bob artwork", + file_path=str(storage_file), + file_size=storage_file.stat().st_size, + tags=[], + artwork_metadata={}, + ) + novel = Novel( + id="novel-b", + user_id=self.user_b_id, + file_id=user_file.id, + source_platform="pixiv", + title="bob novel", + file_path=str(storage_file), + file_size=storage_file.stat().st_size, + tags=[], + ) + series_chapter = Novel( + id="novel-series-a", + user_id=self.user_a_id, + source_platform="biquge", + title="sakura series", + series_id="series-a", + series_title="Sakura Series", + chapter_title="Chapter One", + chapter_order=1, + file_path=str(storage_file), + file_size=storage_file.stat().st_size, + tags=[], + ) + follow = CreatorFollow( + id="follow-b", + user_id=self.user_b_id, + platform="pixiv", + creator_id="creator-b", + creator_name="Bob Creator", + enabled=True, + metadata_json={}, + ) + db.add_all([user_a, user_b, admin, blob, user_file, artwork, novel, series_chapter, follow]) + db.commit() + + def test_protected_resource_groups_return_401_without_login(self) -> None: + cases = [ + ("GET", "/api/v1/gallery"), + ("GET", "/api/v1/gallery/feed"), + ("GET", "/api/v1/gallery/stat"), + ("POST", "/api/v1/gallery"), + ("GET", "/api/v1/novels"), + ("GET", "/api/v1/novels/recent"), + ("GET", "/api/v1/files/file-b/content"), + ("POST", "/api/v1/upload/init"), + ("GET", "/api/v1/upload/status/unknown"), + ("GET", "/api/v1/follows"), + ("POST", "/api/v1/follows"), + ("POST", "/api/v1/follows/bulk-upsert"), + ("GET", "/api/v1/follows/alerts"), + ("GET", "/api/v1/push/devices"), + ("POST", "/api/v1/push/register-token"), + ("POST", "/api/v1/push/unregister-token"), + ] + for method, path in cases: + with self.subTest(method=method, path=path): + response = self.client.request(method, path, json={}) + self.assertEqual(response.status_code, 401) + + def test_user_a_cannot_access_user_b_gallery_novel_file_or_follow_data(self) -> None: + gallery = self.client.get("/api/v1/gallery", headers=self.auth_a) + self.assertEqual(gallery.status_code, 200) + self.assertEqual(gallery.json()["data"]["items"], []) + + self.assertEqual(self.client.get("/api/v1/gallery/art-b", headers=self.auth_a).status_code, 404) + + novels = self.client.get("/api/v1/novels", headers=self.auth_a) + self.assertEqual(novels.status_code, 200) + self.assertNotIn("novel-b", {item["id"] for item in novels.json()["data"]["items"]}) + + self.assertEqual(self.client.get("/api/v1/novels/novel-b", headers=self.auth_a).status_code, 404) + self.assertEqual(self.client.get("/api/v1/novels/novel-b/content", headers=self.auth_a).status_code, 404) + self.assertEqual(self.client.get("/api/v1/files/file-b/content", headers=self.auth_a).status_code, 404) + self.assertEqual(self.client.get("/api/v1/files/file-b/thumbnail", headers=self.auth_a).status_code, 404) + + follows = self.client.get("/api/v1/follows", headers=self.auth_a) + self.assertEqual(follows.status_code, 200) + self.assertEqual(follows.json()["data"], []) + self.assertEqual(self.client.delete("/api/v1/follows/follow-b", headers=self.auth_a).status_code, 404) + + self.assertEqual(self.client.get("/api/v1/gallery/art-b", headers=self.auth_b).status_code, 200) + self.assertEqual(self.client.get("/api/v1/novels/novel-b", headers=self.auth_b).status_code, 200) + self.assertEqual(self.client.get("/api/v1/files/file-b/content", headers=self.auth_b).status_code, 200) + + def test_follow_bulk_upsert_uses_current_user_and_deduplicates(self) -> None: + payload = { + "items": [ + { + "platform": "pixiv", + "creator_id": "origin-1", + "creator_name": "Origin One", + "creator_handle": "origin_one", + "profile_url": "https://www.pixiv.net/users/origin-1", + "metadata": {"user_id": "user-b"}, + }, + { + "platform": "pixiv", + "creator_id": "origin-1", + "creator_name": "Origin One Duplicate", + }, + { + "platform": "x", + "creator_id": "sakura", + "creator_handle": "sakura", + "profile_url": "https://x.com/sakura", + }, + ] + } + response = self.client.post("/api/v1/follows/bulk-upsert", headers=self.auth_a, json=payload) + self.assertEqual(response.status_code, 200) + data = response.json()["data"] + self.assertEqual(data["created"], 2) + self.assertEqual(data["updated"], 0) + self.assertEqual(data["skipped"], 1) + + follows_a = self.client.get("/api/v1/follows", headers=self.auth_a).json()["data"] + follows_b = self.client.get("/api/v1/follows", headers=self.auth_b).json()["data"] + self.assertEqual({item["creator_id"] for item in follows_a}, {"origin-1", "sakura"}) + self.assertEqual({item["creator_id"] for item in follows_b}, {"creator-b"}) + + def test_novel_series_summary_defers_chapter_loading(self) -> None: + summary = self.client.get("/api/v1/novels/series?include_chapters=false", headers=self.auth_a) + self.assertEqual(summary.status_code, 200) + item = summary.json()["data"][0] + self.assertEqual(item["series_id"], "series-a") + self.assertEqual(item["chapter_count"], 1) + self.assertEqual(item["chapters"], []) + + detail = self.client.get("/api/v1/novels/series/series-a", headers=self.auth_a) + self.assertEqual(detail.status_code, 200) + self.assertEqual(len(detail.json()["data"]["chapters"]), 1) + self.assertEqual(self.client.get("/api/v1/novels/series/series-a", headers=self.auth_b).status_code, 404) + + def test_batch_delete_novels_by_series_id(self) -> None: + response = self.client.post( + "/api/v1/novels/batch-delete", + headers=self.auth_a, + json={"ids": [], "series_ids": ["series-a"]}, + ) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json()["data"]["deleted"], 1) + self.assertEqual(self.client.get("/api/v1/novels/series/series-a", headers=self.auth_a).status_code, 404) + + def test_upload_init_rejects_large_path_traversal_and_disallowed_mime(self) -> None: + base_payload = { + "sha256": "a" * 64, + "file_name": "safe.png", + "file_size": 10, + "mime_type": "image/png", + "resource_type": "image", + } + too_large = {**base_payload, "file_size": 2 * 1024 * 1024} + traversal = {**base_payload, "file_name": "../secret.png"} + windows_traversal = {**base_payload, "file_name": r"..\\secret.png"} + bad_mime = {**base_payload, "mime_type": "application/x-msdownload"} + + self.assertEqual(self.client.post("/api/v1/upload/init", headers=self.auth_a, json=too_large).status_code, 413) + self.assertEqual(self.client.post("/api/v1/upload/init", headers=self.auth_a, json=traversal).status_code, 400) + self.assertEqual(self.client.post("/api/v1/upload/init", headers=self.auth_a, json=windows_traversal).status_code, 400) + self.assertEqual(self.client.post("/api/v1/upload/init", headers=self.auth_a, json=bad_mime).status_code, 415) + + ok = self.client.post("/api/v1/upload/init", headers=self.auth_a, json=base_payload) + self.assertEqual(ok.status_code, 200) + self.assertFalse(ok.json()["data"]["exists"]) + + def test_external_url_collection_allows_only_whitelisted_public_domains(self) -> None: + self.assertTrue(is_allowed_external_url("https://www.pixiv.net/artworks/123")) + self.assertTrue(is_allowed_external_url("https://i.pximg.net/img-original/example.jpg")) + + blocked = [ + "http://127.0.0.1:8000/internal", + "http://localhost/admin", + "http://10.0.0.5/private", + "http://172.16.0.1/private", + "http://192.168.1.1/private", + "http://169.254.169.254/latest/meta-data", + "file:///etc/passwd", + "https://evil.example.test/image.jpg", + ] + for url in blocked: + with self.subTest(url=url): + self.assertFalse(is_allowed_external_url(url)) + + def test_admin_dependency_requires_admin_role(self) -> None: + self.assertEqual(self.client.get("/api/v1/admin/probe").status_code, 401) + self.assertEqual(self.client.get("/api/v1/admin/probe", headers=self.auth_a).status_code, 403) + self.assertEqual(self.client.get("/api/v1/admin/probe", headers=self.auth_admin).status_code, 200) diff --git a/tests/test_taobao_plugin.py b/tests/test_taobao_plugin.py new file mode 100644 index 0000000..bb18977 --- /dev/null +++ b/tests/test_taobao_plugin.py @@ -0,0 +1,124 @@ +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +from pixiv_app.core.plugin.base import PluginParseError +from pixiv_app.core.plugin.manager import PluginManager +from pixiv_app.core import paths as app_paths +from plugins.taobao.plugin import TaobaoPlugin, _extract_item_id + + +PRODUCT_HTML = """ + + + + + + +""" + + +class FakeResponse: + def __init__(self, url: str, *, text: str = "", content: bytes = b"media" * 100, content_type: str = "image/jpeg") -> None: + self.url = url + self.text = text + self.content = content + self.headers = {"content-type": content_type} + + def raise_for_status(self) -> None: + return None + + +class FakeClient: + def __init__(self, *args, **kwargs) -> None: + self.headers = kwargs.get("headers", {}) + + def __enter__(self) -> "FakeClient": + return self + + def __exit__(self, *args) -> None: + return None + + def get(self, url: str, *args, **kwargs) -> FakeResponse: + if "item.taobao.com" in url: + return FakeResponse("https://item.taobao.com/item.htm?id=123456789012", text=PRODUCT_HTML, content_type="text/html") + content_type = "video/mp4" if url.endswith(".mp4") else "image/jpeg" + return FakeResponse(url, content_type=content_type) + + +class TaobaoPluginTests(unittest.TestCase): + def test_url_recognition_is_strict(self) -> None: + plugin = TaobaoPlugin() + self.assertTrue(plugin.can_handle("https://item.taobao.com/item.htm?id=123456789012")) + self.assertTrue(plugin.can_handle("https://detail.tmall.com/item.htm?id=123456789012")) + self.assertTrue(plugin.can_handle("https://m.tb.cn/h.example")) + self.assertFalse(plugin.can_handle("https://example.com/item.htm?id=123456789012")) + self.assertFalse(plugin.can_handle("https://taobao.com.evil.example/item.htm?id=1")) + + def test_item_id_extraction(self) -> None: + self.assertEqual(_extract_item_id("https://item.taobao.com/item.htm?id=123456789012"), "123456789012") + + def test_cookie_is_injected_from_local_file(self) -> None: + plugin = TaobaoPlugin() + with tempfile.TemporaryDirectory() as directory: + cookie_file = Path(directory) / "cookies.txt" + cookie_file.write_text(".taobao.com\tTRUE\t/\tTRUE\t0\ttest_cookie\ttest_value\n", encoding="utf-8") + plugin.config.cookie_file = str(cookie_file) + self.assertIn("test_cookie=test_value", plugin.get_headers().get("Cookie", "")) + + @patch("plugins.taobao.plugin.httpx.Client", FakeClient) + def test_parse_product_metadata_and_media(self) -> None: + resource = TaobaoPlugin().parse("https://item.taobao.com/item.htm?id=123456789012")[0] + self.assertEqual(resource.id, "123456789012") + self.assertEqual(resource.title, "测试商品") + self.assertEqual(resource.author, "樱花小店") + self.assertEqual(resource.metadata["price"], "99.00") + self.assertGreaterEqual(len(resource.files), 4) + self.assertIn("video", {item["type"] for item in resource.files}) + + @patch("plugins.taobao.plugin.httpx.Client", FakeClient) + def test_download_entry(self) -> None: + plugin = TaobaoPlugin() + resource = plugin.parse("https://item.taobao.com/item.htm?id=123456789012")[0] + with tempfile.TemporaryDirectory() as directory: + paths = plugin.download(resource, Path(directory)) + self.assertEqual(len(paths), len(resource.files)) + self.assertTrue(all(path.is_file() for path in paths)) + + def test_login_challenge_is_reported(self) -> None: + plugin = TaobaoPlugin() + with patch.object(plugin, "_fetch_page", return_value=("https://item.taobao.com/item.htm?id=123456789012", "请登录并完成滑动验证")): + with self.assertRaisesRegex(PluginParseError, "登录或安全验证"): + plugin.parse("https://item.taobao.com/item.htm?id=123456789012") + + def test_plugin_registration(self) -> None: + root = Path(__file__).resolve().parents[1] + manager = PluginManager([root / "plugins"]) + plugin = manager.load_plugin(root / "plugins" / "taobao" / "plugin.py") + self.assertIsNotNone(plugin) + assert plugin is not None + self.assertIs(manager.get_plugin_for_url("https://detail.tmall.com/item.htm?id=123456789012"), plugin) + + def test_external_plugin_root_is_scanned(self) -> None: + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + builtin = root / "bundle" / "plugins" + external = root / "dist" / "plugins" + user = root / "user" / "plugins" + with ( + patch.object(app_paths, "plugins_root", return_value=builtin), + patch.object(app_paths, "external_plugins_root", return_value=external), + patch.object(app_paths, "user_plugins_root", return_value=user), + ): + self.assertEqual(app_paths.plugin_roots(), [builtin, external, user]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_x_cookie_import.py b/tests/test_x_cookie_import.py new file mode 100644 index 0000000..2f7ef65 --- /dev/null +++ b/tests/test_x_cookie_import.py @@ -0,0 +1,44 @@ +from __future__ import annotations + +import unittest + +from pixiv_app.core.cookie_import import cookies_to_playwright, parse_cookie_text + + +class XCookieImportTests(unittest.TestCase): + def test_netscape_x_cookie_export_is_playwright_compatible(self) -> None: + text = """# Netscape HTTP Cookie File +.x.com\tTRUE\t/\tTRUE\t1819195756\tguest_id\tv1%3Aexample +x.com\tFALSE\t/\tFALSE\t0\tlang\tzh-CN +.x.com\tTRUE\t/\tTRUE\t1809869781\tauth_token\tsecret-auth-token +.x.com\tTRUE\t/\tTRUE\t1809869782\tct0\tsecret-csrf-token +""" + + cookies = parse_cookie_text(text) + playwright_cookies = cookies_to_playwright(cookies, ("x.com", "twitter.com")) + names = {item["name"] for item in playwright_cookies} + + self.assertIn("auth_token", names) + self.assertIn("ct0", names) + self.assertIn("guest_id", names) + self.assertIn("lang", names) + lang_cookie = next(item for item in playwright_cookies if item["name"] == "lang") + self.assertNotIn("expires", lang_cookie) + + def test_x_cookie_conversion_drops_invalid_items_and_session_expiry(self) -> None: + prepared = cookies_to_playwright( + [ + {"name": "", "value": "bad", "domain": ".x.com"}, + {"name": "lang", "value": "zh-CN", "domain": "x.com", "path": "/", "expires": 0}, + {"name": "ct0", "value": "token", "domain": ".x.com", "path": "/", "expires": "1809869782"}, + ], + ("x.com",), + ) + + self.assertEqual({item["name"] for item in prepared}, {"lang", "ct0"}) + self.assertNotIn("expires", next(item for item in prepared if item["name"] == "lang")) + self.assertEqual(next(item for item in prepared if item["name"] == "ct0")["expires"], 1809869782) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/code_statistics.py b/tools/code_statistics.py new file mode 100644 index 0000000..348c459 --- /dev/null +++ b/tools/code_statistics.py @@ -0,0 +1,228 @@ +#!/usr/bin/env python3 +""" +代码统计与文档生成工具 +功能: +1. 统计项目代码数量 +2. 生成目录结构 +3. 将所有代码保存到docx文档 +""" + +import os +import re +from docx import Document +from docx.shared import Pt, RGBColor, Cm +from docx.enum.text import WD_PARAGRAPH_ALIGNMENT + + +class CodeStatistics: + """代码统计类""" + + def __init__(self): + self.project_root = '.' + self.exclude_dirs = ('.venv', '.git', '.idea', '__pycache__', 'reports', 'models') + self.exclude_files = ('generate_code_doc.py',) + + def analyze(self): + """执行完整分析""" + print("🔍 正在分析项目代码...") + + # 获取项目结构 + structure = self._get_structure() + + # 统计信息 + stats = self._calculate_stats(structure) + + # 生成目录树 + tree = self._generate_tree(structure) + + # 生成文档 + self._generate_document(structure, stats, tree) + + return stats + + def _get_structure(self) -> dict: + """获取项目结构""" + structure = {} + for root, dirs, files in os.walk(self.project_root): + dirs[:] = [d for d in dirs if d not in self.exclude_dirs] + for f in files: + if f.endswith('.py') and f not in self.exclude_files: + rel_path = os.path.relpath(root, self.project_root) + if rel_path not in structure: + structure[rel_path] = [] + structure[rel_path].append(f) + return structure + + def _calculate_stats(self, structure: dict) -> dict: + """计算统计信息""" + stats = { + 'files': 0, + 'lines': 0, + 'functions': 0, + 'comments': 0, + 'empty_lines': 0, + 'size': 0, + 'file_details': [] + } + + for directory, files in structure.items(): + for filename in files: + filepath = os.path.join(self.project_root, directory, filename) if directory != '.' else filename + try: + with open(filepath, 'r', encoding='utf-8', errors='ignore') as fp: + content = fp.read() + + lines = content.splitlines() + func_count = len(re.findall(r'^\s*(def|class)\s+', content, re.M)) + comment_lines = sum(1 for line in lines if line.strip().startswith('#') or + (line.strip().startswith('"""') or line.strip().startswith("'''"))) + empty_lines = sum(1 for line in lines if line.strip() == '') + + stats['files'] += 1 + stats['lines'] += len(lines) + stats['functions'] += func_count + stats['comments'] += comment_lines + stats['empty_lines'] += empty_lines + stats['size'] += len(content.encode('utf-8')) + stats['file_details'].append({ + 'name': filename, + 'dir': directory, + 'lines': len(lines), + 'size': len(content.encode('utf-8')) + }) + except Exception as e: + print(f"⚠️ 跳过文件: {filename} - {e}") + + return stats + + def _generate_tree(self, structure: dict) -> str: + """生成目录树""" + tree = ["📁 SQL注入检测工具"] + for directory, files in sorted(structure.items()): + dir_name = directory if directory != '.' else '根目录' + tree.append(f"├── 📂 {dir_name}") + for filename in sorted(files): + tree.append(f"│ └── 📄 {filename}") + return '\n'.join(tree) + + def _generate_document(self, structure: dict, stats: dict, tree: str): + """生成docx文档""" + print("📝 正在生成文档...") + + doc = Document() + + # 设置页面边距 + for section in doc.sections: + section.top_margin = Cm(2.54) + section.bottom_margin = Cm(2.54) + section.left_margin = Cm(2.54) + section.right_margin = Cm(2.54) + + # 标题页 + title = doc.add_heading('SQL注入检测工具', level=1) + title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER + title.runs[0].font.size = Pt(24) + title.runs[0].font.color.rgb = RGBColor(0, 51, 102) + + subtitle = doc.add_heading('项目代码文档', level=2) + subtitle.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER + subtitle.runs[0].font.size = Pt(16) + subtitle.runs[0].font.color.rgb = RGBColor(68, 68, 68) + + # 目录页 + doc.add_page_break() + doc.add_heading('📋 目录', level=1) + idx = 1 + for directory, files in sorted(structure.items()): + dir_name = directory if directory != '.' else '根目录' + doc.add_paragraph(f"{idx}. {dir_name}") + jdx = 1 + for filename in sorted(files): + doc.add_paragraph(f" {idx}.{jdx} {filename}") + jdx += 1 + idx += 1 + + # 目录结构页 + doc.add_page_break() + doc.add_heading('🌳 项目目录结构', level=1) + tree_para = doc.add_paragraph(tree) + tree_para.runs[0].font.name = 'Consolas' + tree_para.runs[0].font.size = Pt(10) + + # 统计页 + doc.add_page_break() + doc.add_heading('📊 项目统计', level=1) + + stats_table = doc.add_table(rows=7, cols=2) + stats_table.style = 'Table Grid' + stats_data = [ + ('统计项', '数值'), + ('Python文件数', str(stats['files'])), + ('代码总行数', str(stats['lines'])), + ('有效代码行', str(stats['lines'] - stats['empty_lines'])), + ('函数/类数量', str(stats['functions'])), + ('注释行数', str(stats['comments'])), + ('总大小', f"{stats['size'] / 1024:.1f} KB") + ] + for row, (label, value) in enumerate(stats_data): + stats_table.rows[row].cells[0].text = label + stats_table.rows[row].cells[1].text = value + + # 文件排行 + doc.add_heading('📁 文件大小排行', level=2) + sorted_files = sorted(stats['file_details'], key=lambda x: x['size'], reverse=True)[:10] + rank_table = doc.add_table(rows=11, cols=3) + rank_table.style = 'Table Grid' + rank_table.rows[0].cells[0].text = '排名' + rank_table.rows[0].cells[1].text = '文件' + rank_table.rows[0].cells[2].text = '大小(KB)' + for i, file_info in enumerate(sorted_files, 1): + rank_table.rows[i].cells[0].text = str(i) + rank_table.rows[i].cells[1].text = file_info['name'] + rank_table.rows[i].cells[2].text = f"{file_info['size'] / 1024:.1f}" + + # 代码内容 + for directory, files in sorted(structure.items()): + doc.add_page_break() + dir_name = directory if directory != '.' else '根目录' + doc.add_heading(f"📂 {dir_name}", level=1) + + for filename in sorted(files): + filepath = os.path.join(self.project_root, directory, filename) if directory != '.' else filename + try: + with open(filepath, 'r', encoding='utf-8', errors='ignore') as fp: + content = fp.read() + + doc.add_heading(f"📄 {filename}", level=2) + doc.add_paragraph("-" * 80) + code_para = doc.add_paragraph() + code_run = code_para.add_run(content) + code_run.font.name = 'Consolas' + code_run.font.size = Pt(9) + doc.add_paragraph() + except Exception as e: + doc.add_paragraph(f"⚠️ 无法读取: {filename} - {e}") + + doc.save('下载器项目代码文档.docx') + print("✅ 文档生成完成") + + +def main(): + analyzer = CodeStatistics() + stats = analyzer.analyze() + + print("\n" + "=" * 60) + print("📊 项目代码统计结果") + print("=" * 60) + print(f"📄 Python文件数: {stats['files']}") + print(f"📝 代码总行数: {stats['lines']}") + print(f"🔧 函数/类数量: {stats['functions']}") + print(f"📝 注释行数: {stats['comments']}") + print(f"📦 总大小: {stats['size'] / 1024:.1f} KB") + print("=" * 60) + print(f"📄 文档已保存: 下载器项目代码文档.docx") + print("=" * 60) + + +if __name__ == '__main__': + main() diff --git a/tools/probe_gongzicp.py b/tools/probe_gongzicp.py new file mode 100644 index 0000000..14d2ad9 --- /dev/null +++ b/tools/probe_gongzicp.py @@ -0,0 +1,51 @@ +"""Probe gongzicp.com HTML/API structure.""" +from __future__ import annotations + +import json +import re +import requests + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com" + + +def fetch(path: str) -> str: + url = path if path.startswith("http") else f"{BASE}{path}" + r = requests.get(url, headers={"User-Agent": UA, "Referer": BASE + "/"}, timeout=45) + print(f"GET {url} -> {r.status_code} len={len(r.text)}") + return r.text + + +def main() -> None: + html = fetch("/novel-429598.html") + # API hints + for line in html.splitlines(): + lower = line.lower() + if any(k in lower for k in ("api", "chapter", "catalog", "novelid", "read")): + if len(line.strip()) < 300: + print(line.strip()[:200]) + + # chapter links + chapters = re.findall(r'href="([^"]*chapter[^"]*)"', html, re.I) + print("chapter hrefs sample:", chapters[:10]) + chapters2 = re.findall(r'href="(/read/\d+[^"]*)"', html) + print("read hrefs:", chapters2[:10]) + chapters3 = re.findall(r'novel-\d+', html) + print("novel ids in page:", list(set(chapters3))[:5]) + print("html preview:", html[:2000]) + + # try search API guesses + for api in [ + "/webapi/search/novel?keyword=测试", + "/api/search?keyword=测试", + "/search?keyword=测试", + ]: + try: + t = fetch(api) + print(t[:500]) + except Exception as exc: + print(api, exc) + + +if __name__ == "__main__": + main() diff --git a/tools/probe_gongzicp10.py b/tools/probe_gongzicp10.py new file mode 100644 index 0000000..475c7cf --- /dev/null +++ b/tools/probe_gongzicp10.py @@ -0,0 +1,41 @@ +import json +import requests + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com/webapi" +headers = { + "User-Agent": UA, + "Referer": "https://www.gongzicp.com/novel-429598.html", + "Accept": "application/json, text/plain, */*", +} + +paths = [ + "/novel/novelInfo?id=429598", + "/novel/chapterGetList?nid=429598", +] +for p in paths: + r = requests.get(BASE + p, headers=headers, timeout=30) + print(p, r.status_code) + data = r.json() + print(json.dumps(data, ensure_ascii=False)[:1500]) + +# chapter content - need cid from chapter list +clist = requests.get(BASE + "/novel/chapterGetList?nid=429598", headers=headers, timeout=30).json() +items = clist.get("data", {}).get("list") or [] +print("chapters", len(items)) +if items: + cid = items[0].get("id") + r = requests.get( + BASE + f"/novel/chapterGetInfo?nid=429598&cid={cid}", + headers=headers, + timeout=30, + ) + print("chapter info", r.status_code, json.dumps(r.json(), ensure_ascii=False)[:800]) + +# search +r = requests.get( + BASE + "/novel/searchNovel?keyword=天官&page=1&module=all", + headers={**headers, "Referer": "https://www.gongzicp.com/"}, + timeout=30, +) +print("search", r.status_code, r.text[:800]) diff --git a/tools/probe_gongzicp11.py b/tools/probe_gongzicp11.py new file mode 100644 index 0000000..f75419b --- /dev/null +++ b/tools/probe_gongzicp11.py @@ -0,0 +1,76 @@ +"""Probe gongzicp search and chapter content.""" +import base64 +import json +import re +import requests +from Crypto.Cipher import AES +from Crypto.Util.Padding import unpad + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com" +WEBAPI = BASE + "/webapi" + +def js_to_string(n: int, base: int) -> str: + digits = "0123456789abcdefghijklmnopqrstuvwxyz" + s = "" + while n: + s = digits[n % base] + s + n //= base + return s or "0" + + +def make_aes(): + e = "iGzsYn" + js_to_string(int("165455", 14), 32) + iv_str = "$h$b3!" + e + t = base64.b64decode("dTBMUnJidSRFbg==").decode() + key_str = t + js_to_string(int("4d5a6c8", 14), 36) + "A" + return key_str.encode("utf-8"), iv_str.encode("utf-8") + + +def decrypt_content(content: str) -> str: + if not content: + return "" + key, iv = make_aes() + raw = base64.b64decode(content) + cipher = AES.new(key, AES.MODE_CBC, iv) + return unpad(cipher.decrypt(raw), 16).decode("utf-8") + + +headers = { + "User-Agent": UA, + "Referer": BASE + "/", + "Accept": "application/json, text/plain, */*", +} + +# Find SearchH5 bundle +html = requests.get(BASE, headers=headers, timeout=30).text +for m in re.finditer(r"SearchH5\.[a-f0-9]+\.js", html): + print("SearchH5:", m.group()) + +# Try search endpoints from JS +search_paths = [ + "/novel/searchNovel?keyword=天官&page=1", + "/novel/searchNovel?keyword=天官&page=1&module=all", + "/novel/search?keyword=天官&page=1", + "/search/novel?keyword=天官&page=1", +] +for p in search_paths: + r = requests.get(WEBAPI + p, headers=headers, timeout=30) + print(p, r.status_code, r.text[:200]) + +# Try novel with more words - browse from homepage API +for p in [ + "/novel/getNovelListByType?type_id=75&page=1&num=5", + "/novel/getNovelList?type_id=75&page=1&num=5", + "/novel/novelList?type_id=75&page=1&num=5", +]: + r = requests.get(WEBAPI + p, headers=headers, timeout=30) + print(p, r.status_code, r.text[:300]) + +# Test another novel id from author list +r = requests.get( + WEBAPI + "/novel/getSimpleAuthorNovelList?page=1&num=5&aid=239902", + headers={**headers, "Referer": f"{BASE}/novel-429598.html"}, + timeout=30, +) +print("author novels", r.text[:500]) diff --git a/tools/probe_gongzicp12.py b/tools/probe_gongzicp12.py new file mode 100644 index 0000000..26312d0 --- /dev/null +++ b/tools/probe_gongzicp12.py @@ -0,0 +1,37 @@ +import json +import requests + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com" +WEBAPI = BASE + "/webapi" +headers = { + "User-Agent": UA, + "Referer": BASE + "/", + "Accept": "application/json, text/plain, */*", +} + +s = requests.Session() +s.headers.update(headers) +init = s.get(WEBAPI + "/system/init", timeout=30).json() +print("init", init) + +# chapter with nid param +for nid, cid in [(429598, 2377724), (100000, 1)]: + r = s.get( + WEBAPI + f"/novel/chapterGetInfo?nid={nid}&cid={cid}", + headers={"Referer": f"{BASE}/read-{nid}-{cid}.html"}, + timeout=30, + ) + data = r.json() + if data.get("code") == 200: + ci = data.get("data", {}).get("chapterInfo", {}) + print(nid, cid, "content len", len(ci.get("content") or ""), "number", ci.get("number")) + +# try search with session cookies +r = s.get( + WEBAPI + "/novel/searchNovel", + params={"keyword": "天官", "page": 1, "num": 10}, + headers={"Referer": f"{BASE}/search.html"}, + timeout=30, +) +print("search", r.status_code, r.text[:500]) diff --git a/tools/probe_gongzicp13.py b/tools/probe_gongzicp13.py new file mode 100644 index 0000000..3d91cfd --- /dev/null +++ b/tools/probe_gongzicp13.py @@ -0,0 +1,13 @@ +import re +import requests + +html = requests.get("https://www.gongzicp.com/", timeout=30).text +for m in re.finditer(r"/assets/[^\"']*Search[^\"']*\.js", html): + print("bundle", m.group()) + +for m in re.finditer(r"/assets/index\.[a-f0-9]+\.js", html): + jsurl = "https://www.gongzicp.com" + m.group() + t = requests.get(jsurl, timeout=60).text + for m2 in re.finditer(r".{0,60}searchNovel.{0,100}", t): + print(m2.group()[:220]) + print("---") diff --git a/tools/probe_gongzicp14.py b/tools/probe_gongzicp14.py new file mode 100644 index 0000000..6e92f5f --- /dev/null +++ b/tools/probe_gongzicp14.py @@ -0,0 +1,28 @@ +import json +import requests + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com" +WEBAPI = BASE + "/webapi" +headers = { + "User-Agent": UA, + "Referer": BASE + "/", + "Accept": "application/json, text/plain, */*", +} + +paths = [ + "/home/typeList?tid=75&page=1&num=10", + "/home/getTypeList?tid=75&page=1&num=10", + "/novel/getTypeNovelList?type_id=75&page=1&num=10", + "/novel/novelListByType?type_id=75&page=1&num=10", + "/rank/getRankList?type=1&page=1&num=10", + "/novel/getRankNovelList?rank_type=1&page=1&num=10", +] +for p in paths: + try: + r = requests.get(WEBAPI + p, headers=headers, timeout=30) + text = r.text + if '"code":200' in text or '"code": 200' in text: + print("OK", p, text[:400]) + except Exception as ex: + print("ERR", p, ex) diff --git a/tools/probe_gongzicp15.py b/tools/probe_gongzicp15.py new file mode 100644 index 0000000..3898887 --- /dev/null +++ b/tools/probe_gongzicp15.py @@ -0,0 +1,20 @@ +import re +import requests + +html = requests.get("https://www.gongzicp.com/", timeout=30).text +for m in re.finditer(r"/assets/[A-Za-z0-9_.-]+\.js", html): + name = m.group() + if "Home" in name or "Type" in name or "Search" in name: + print(name) + +# grep all js from index.eef719ce for webapi paths +t = requests.get("https://www.gongzicp.com/assets/index.eef719ce.js", timeout=60).text +for m in re.finditer(r"/[a-zA-Z]+/[a-zA-Z]+[a-zA-Z0-9]*", t): + s = m.group() + if "novel" in s.lower() or "search" in s.lower() or "home" in s.lower() or "type" in s.lower(): + if len(s) < 40: + pass +for m in re.finditer(r"url:\s*[\"']/[^\"']+[\"']", t): + s = m.group() + if any(k in s for k in ("novel", "search", "home", "type", "rank")): + print(s) diff --git a/tools/probe_gongzicp16.py b/tools/probe_gongzicp16.py new file mode 100644 index 0000000..94843ca --- /dev/null +++ b/tools/probe_gongzicp16.py @@ -0,0 +1,16 @@ +import re +import requests + +t = requests.get("https://www.gongzicp.com/assets/index.eef719ce.js", timeout=60).text +# find webapi base +for m in re.finditer(r"webapi[^\"']{0,40}", t): + print(m.group()[:80]) + +# find all strings containing GetList or search +seen = set() +for m in re.finditer(r"[\"'][/][a-zA-Z]+/[a-zA-Z][a-zA-Z0-9]*[\"']", t): + s = m.group().strip("'\"") + if any(k in s.lower() for k in ("novel", "search", "home", "rank", "type")): + if s not in seen: + seen.add(s) + print(s) diff --git a/tools/probe_gongzicp17.py b/tools/probe_gongzicp17.py new file mode 100644 index 0000000..e8b8ba8 --- /dev/null +++ b/tools/probe_gongzicp17.py @@ -0,0 +1,8 @@ +import re +import requests + +t = requests.get("https://www.gongzicp.com/assets/index.eef719ce.js", timeout=60).text +for m in re.finditer(r"[\"']\./[A-Za-z0-9_.-]+\.js[\"']", t): + name = m.group() + if "Search" in name or "Home" in name or "Type" in name or "novel" in name.lower(): + print(name) diff --git a/tools/probe_gongzicp18.py b/tools/probe_gongzicp18.py new file mode 100644 index 0000000..9f9dce3 --- /dev/null +++ b/tools/probe_gongzicp18.py @@ -0,0 +1,16 @@ +import re +import requests + +chunks = [ + "SearchH5.1c778303.js", + "TypeH5.e1133e1d.js", + "HomeH5.2bbaaa98.js", + "NovelListH5.a284bf69.js", +] +for chunk in chunks: + t = requests.get(f"https://www.gongzicp.com/assets/{chunk}", timeout=60).text + print("===", chunk, "len", len(t)) + for m in re.finditer(r"url:\s*[\"'][/][^\"']+[\"']", t): + print(m.group()) + for m in re.finditer(r"[\"']/novel/[a-zA-Z]+[\"']", t): + print(m.group()) diff --git a/tools/probe_gongzicp19.py b/tools/probe_gongzicp19.py new file mode 100644 index 0000000..3385661 --- /dev/null +++ b/tools/probe_gongzicp19.py @@ -0,0 +1,21 @@ +import json +import requests + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com" +WEBAPI = BASE + "/webapi" +headers = { + "User-Agent": UA, + "Referer": BASE + "/", + "Accept": "application/json, text/plain, */*", +} + +def get(path, **params): + r = requests.get(WEBAPI + path, params=params, headers=headers, timeout=30) + print(path, params, r.status_code, r.text[:500]) + return r + +get("/search/novels", keyword="天官", page=1, num=5) +get("/novel/novelList", type_id=75, page=1, num=5) +get("/novel/typeGetList") +get("/novel/typeGetInfo", type_id=75) diff --git a/tools/probe_gongzicp2.py b/tools/probe_gongzicp2.py new file mode 100644 index 0000000..044a235 --- /dev/null +++ b/tools/probe_gongzicp2.py @@ -0,0 +1,57 @@ +"""Probe gongzicp webapi endpoints.""" +from __future__ import annotations + +import re +import requests + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com" +SESSION = requests.Session() +SESSION.headers.update( + { + "User-Agent": UA, + "Referer": BASE + "/", + "Accept": "application/json, text/plain, */*", + "Origin": BASE, + } +) + + +def get(path: str, **kwargs) -> requests.Response: + url = path if path.startswith("http") else f"{BASE}{path}" + return SESSION.get(url, timeout=45, **kwargs) + + +def main() -> None: + r = get("/novel-429598.html") + m = re.search(r'/assets/index\.([a-f0-9]+)\.js', r.text) + if not m: + print("no js bundle") + return + js_name = f"/assets/index.{m.group(1)}.js" + js = get(js_name).text + print(f"js len {len(js)}") + apis = sorted(set(re.findall(r"/webapi/[a-zA-Z0-9_/]+", js))) + print("webapi paths:", apis[:40]) + print("total apis", len(apis)) + + # try novel detail + for path in [ + "/webapi/novel/429598", + "/webapi/novel/detail?novel_id=429598", + "/webapi/novel/detail?novelId=429598", + "/webapi/novel/info?novel_id=429598", + "/webapi/chapter/list?novel_id=429598", + "/webapi/chapter/list?novelId=429598", + "/webapi/novel/chapter/list?novel_id=429598", + "/webapi/search/novel?keyword=天官", + ]: + try: + resp = get(path) + print(path, resp.status_code, resp.text[:300]) + except Exception as exc: + print(path, exc) + + +if __name__ == "__main__": + main() diff --git a/tools/probe_gongzicp20.py b/tools/probe_gongzicp20.py new file mode 100644 index 0000000..8b5df31 --- /dev/null +++ b/tools/probe_gongzicp20.py @@ -0,0 +1,47 @@ +import base64 +import json +import requests +from Crypto.Cipher import AES +from Crypto.Util.Padding import unpad + +UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0" +BASE = "https://www.gongzicp.com" +WEBAPI = BASE + "/webapi" +headers = { + "User-Agent": UA, + "Referer": f"{BASE}/novel-2058540.html", + "Accept": "application/json, text/plain, */*", +} +nid = 2058540 + +def js_to_string(n, base): + digits = "0123456789abcdefghijklmnopqrstuvwxyz" + s = "" + while n: + s = digits[n % base] + s + n //= base + return s or "0" + +def decrypt_content(content): + e = "iGzsYn" + js_to_string(int("165455", 14), 32) + iv_str = "$h$b3!" + e + t = base64.b64decode("dTBMUnJidSRFbg==").decode() + key_str = t + js_to_string(int("4d5a6c8", 14), 36) + "A" + key, iv = key_str.encode(), iv_str.encode() + raw = base64.b64decode(content) + cipher = AES.new(key, AES.MODE_CBC, iv) + return unpad(cipher.decrypt(raw), 16).decode("utf-8") + +info = requests.get(WEBAPI + f"/novel/novelInfo?id={nid}", headers=headers, timeout=30).json() +print("novel", info["data"]["novel_name"], info["data"]["novel_wordnumber"]) + +clist = requests.get(WEBAPI + f"/novel/chapterGetList?nid={nid}", headers=headers, timeout=30).json() +chapters = clist["data"]["list"] +print("chapters", len(chapters)) +for ch in chapters[:3]: + cid = ch["id"] + r = requests.get(WEBAPI + f"/novel/chapterGetInfo?nid={nid}&cid={cid}", headers=headers, timeout=30) + ci = r.json()["data"]["chapterInfo"] + enc = ci.get("content") or "" + text = decrypt_content(enc) if enc else "" + print(ch["name"][:40], "enc", len(enc), "text", len(text), text[:80].replace("\n", " ")) diff --git a/tools/probe_gongzicp21.py b/tools/probe_gongzicp21.py new file mode 100644 index 0000000..7127ed0 --- /dev/null +++ b/tools/probe_gongzicp21.py @@ -0,0 +1,7 @@ +import re +import requests + +t = requests.get("https://www.gongzicp.com/assets/index.eef719ce.js", timeout=60).text +for m in re.finditer(r".{0,80}search/novels.{0,120}", t): + print(m.group()[:250]) + print("---") diff --git a/tools/probe_gongzicp22.py b/tools/probe_gongzicp22.py new file mode 100644 index 0000000..6d0ede4 --- /dev/null +++ b/tools/probe_gongzicp22.py @@ -0,0 +1,26 @@ +import re +import requests + +for chunk in ["CpListData.97f05acb.js", "CpListData.b8c8c8c8.js"]: + try: + t = requests.get(f"https://www.gongzicp.com/assets/{chunk}", timeout=30).text + print(chunk, "found", len(t)) + for m in re.finditer(r".{0,100}search.{0,100}", t, re.I): + if "novel" in m.group().lower(): + print(m.group()[:220]) + except Exception as ex: + print(chunk, ex) + +# list assets matching CpListData +html = requests.get("https://www.gongzicp.com/", timeout=30).text +for m in re.finditer(r"CpListData\.[a-f0-9]+\.js", html): + print(m.group()) + +t = requests.get("https://www.gongzicp.com/assets/index.eef719ce.js", timeout=60).text +for m in re.finditer(r"CpListData\.[a-f0-9]+\.js", t): + chunk = m.group() + print("loading", chunk) + ct = requests.get(f"https://www.gongzicp.com/assets/{chunk}", timeout=30).text + for m2 in re.finditer(r"params:\{[^}]{0,200}\}", ct): + if "search" in m2.group().lower() or "word" in m2.group().lower(): + print(m2.group()) diff --git a/tools/probe_gongzicp3.py b/tools/probe_gongzicp3.py new file mode 100644 index 0000000..9590fbb --- /dev/null +++ b/tools/probe_gongzicp3.py @@ -0,0 +1,19 @@ +import re +import requests + +js = requests.get( + "https://www.gongzicp.com/assets/index.eef719ce.js", + headers={"User-Agent": "Mozilla/5.0"}, + timeout=90, +).text +for needle in ["webapi", "novel", "chapter", "search", "read"]: + idx = 0 + found = 0 + while found < 15: + i = js.find(needle, idx) + if i < 0: + break + snippet = js[max(0, i - 40) : i + 80] + print("---", needle, snippet.replace("\n", " ")) + idx = i + len(needle) + found += 1 diff --git a/tools/probe_gongzicp4.py b/tools/probe_gongzicp4.py new file mode 100644 index 0000000..1dd2ad4 --- /dev/null +++ b/tools/probe_gongzicp4.py @@ -0,0 +1,39 @@ +import re +import requests + +js = requests.get( + "https://www.gongzicp.com/assets/index.eef719ce.js", + headers={"User-Agent": "Mozilla/5.0"}, + timeout=90, +).text +for needle in ["apiv2", "getNovel", "searchNovel", "chapter", "readChapter"]: + idx = 0 + found = 0 + while found < 8: + i = js.find(needle, idx) + if i < 0: + break + snippet = js[max(0, i - 30) : i + 100] + print("---", needle, snippet.replace("\n", " ")) + idx = i + len(needle) + found += 1 + +API = "https://api1.gongzicp.com/apiv2" +headers = { + "User-Agent": "Mozilla/5.0", + "Referer": "https://www.gongzicp.com/", + "Origin": "https://www.gongzicp.com", +} +paths = [ + "/novel/info?novel_id=429598", + "/novel/info?novelId=429598", + "/novel/detail?novel_id=429598", + "/novel/chapter/list?novel_id=429598", + "/chapter/list?novel_id=429598", + "/search/novel?keyword=天官", + "/searchNovel?keyword=天官", + "/novel/search?keyword=天官", +] +for p in paths: + r = requests.get(API + p, headers=headers, timeout=30) + print(p, r.status_code, r.text[:400]) diff --git a/tools/probe_gongzicp5.py b/tools/probe_gongzicp5.py new file mode 100644 index 0000000..54031aa --- /dev/null +++ b/tools/probe_gongzicp5.py @@ -0,0 +1,46 @@ +import base64 +import re +import requests + +auth = "Basic 6ZmI5aSn5a6dOmNwMTIzNDU2" +print("auth decode", base64.b64decode("6ZmI5aSn5a6dOmNwMTIzNDU2")) + +js = requests.get( + "https://www.gongzicp.com/assets/index.eef719ce.js", + timeout=90, + headers={"User-Agent": "Mozilla/5.0"}, +).text + +# find function Fg or getNovels definition area +for m in re.finditer(r"getNovels[^}]{0,500}", js): + s = m.group(0) + if "apiv2" in s or "novel" in s.lower(): + print(s[:400]) + print("---") + +# search url patterns with apiv2 +urls = sorted(set(re.findall(r"apiv2/[a-zA-Z0-9_/]+", js))) +print("apiv2 paths", urls[:50]) + +API = "https://api1.gongzicp.com/apiv2" +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0", + "Referer": "https://www.gongzicp.com/", + "Origin": "https://www.gongzicp.com", + "Authorization": auth, +} +tests = [ + "/novel/info?novelid=429598", + "/novel/info?nid=429598", + "/novel/info?id=429598", + "/novel/getInfo?novelid=429598", + "/novel/chapterList?novelid=429598", + "/novel/chapterList?nid=429598", + "/chapter/content?chapterid=1&novelid=429598", + "/read/chapter?chapter_id=1&novel_id=429598", + "/searchNovel?keyword=天官&page=1", + "/novel/searchNovel?keyword=天官", +] +for p in tests: + r = requests.get(API + p, headers=headers, timeout=30) + print(p, r.status_code, r.text[:500]) diff --git a/tools/probe_gongzicp6.py b/tools/probe_gongzicp6.py new file mode 100644 index 0000000..fcdd4af --- /dev/null +++ b/tools/probe_gongzicp6.py @@ -0,0 +1,30 @@ +import re +import requests + +js = requests.get( + "https://www.gongzicp.com/assets/index.eef719ce.js", + timeout=90, + headers={"User-Agent": "Mozilla/5.0"}, +).text +for pat in [r"novel/[a-zA-Z]+", r"chapter/[a-zA-Z]+", r"search[A-Za-z]+", r"read/[a-zA-Z]+"]: + found = sorted(set(re.findall(pat, js))) + print(pat, found[:30]) + +# find Fg assignment +idx = js.find("function Fg") +if idx < 0: + idx = js.find("Fg=") +print("Fg at", idx) +if idx >= 0: + print(js[idx:idx+800]) + +# download InfoH5 chunk +chunk = requests.get( + "https://www.gongzicp.com/assets/InfoH5.37080396.js", + timeout=60, + headers={"User-Agent": "Mozilla/5.0"}, +).text +print("InfoH5 len", len(chunk)) +for pat in [r"apiv2[^\"']{0,60}", r"/novel[^\"']{0,60}", r"chapter[^\"']{0,40}"]: + m = re.findall(pat, chunk) + print(pat, m[:15]) diff --git a/tools/probe_gongzicp7.py b/tools/probe_gongzicp7.py new file mode 100644 index 0000000..0fd0cd1 --- /dev/null +++ b/tools/probe_gongzicp7.py @@ -0,0 +1,39 @@ +import json +import requests + +AUTH = "Basic 6ZmI5aSn5a6dOmNwMTIzNDU2" +API = "https://api1.gongzicp.com/apiv2" +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0", + "Referer": "https://www.gongzicp.com/", + "Origin": "https://www.gongzicp.com", + "Authorization": AUTH, + "Content-Type": "application/json", +} + +def post(path, body): + r = requests.post(API + path, headers=headers, json=body, timeout=30) + print(path, body, r.status_code, r.text[:600]) + + +post("/novel/novelInfo", {"novelid": 429598}) +post("/novel/novelInfo", {"novelId": 429598}) +post("/novel/novelInfo", {"nid": 429598}) +post("/novel/novelInfo", {"id": 429598}) +post("/novel/chapterGetList", {"novelid": 429598}) +post("/novel/chapterGetList", {"nid": 429598}) +post("/novel/chapterGetInfo", {"novelid": 429598, "chapterid": 1}) +post("/novel/chapterGetInfo", {"nid": 429598, "cid": 1}) + +# search from SearchH5 +chunk = requests.get( + "https://www.gongzicp.com/assets/SearchH5.1c778303.js", + timeout=60, + headers={"User-Agent": "Mozilla/5.0"}, +).text +import re +paths = sorted(set(re.findall(r"/novel/[a-zA-Z]+", chunk))) +print("search paths", paths) + +post("/novel/searchNovel", {"keyword": "天官", "page": 1, "tab": "all"}) +post("/novel/searchNovel", {"word": "天官", "module": "all"}) diff --git a/tools/probe_gongzicp8.py b/tools/probe_gongzicp8.py new file mode 100644 index 0000000..e84a5d2 --- /dev/null +++ b/tools/probe_gongzicp8.py @@ -0,0 +1,33 @@ +import json +import requests + +AUTH = "Basic 6ZmI5aSn5a6dOmNwMTIzNDU2" +API = "https://api1.gongzicp.com/apiv2" +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0", + "Referer": "https://www.gongzicp.com/", + "Origin": "https://www.gongzicp.com", + "Authorization": AUTH, + "Content-Type": "application/json", +} + +def call(path, params): + body = {"url": path, "params": params} + r = requests.post(API + path, headers=headers, json=params, timeout=30) + print("POST path", path, params, r.status_code, r.text[:500]) + r2 = requests.post(API, headers=headers, json=body, timeout=30) + print("POST root wrap", body, r2.status_code, r2.text[:500]) + + +call("/novel/chapterGetList", {"nid": 429598}) +call("/novel/chapterGetInfo", {"nid": 429598, "cid": "1"}) +call("/novel/novelInfo", {"nid": 429598}) + +# try webapi gateway +WEB = "https://www.gongzicp.com/webapi" +for body in [ + {"url": "/novel/chapterGetList", "params": {"nid": 429598}}, + {"nid": 429598}, +]: + r = requests.post(WEB, headers=headers, json=body, timeout=30) + print("webapi", body, r.status_code, r.text[:500]) diff --git a/tools/probe_gongzicp9.py b/tools/probe_gongzicp9.py new file mode 100644 index 0000000..ce734d6 --- /dev/null +++ b/tools/probe_gongzicp9.py @@ -0,0 +1,41 @@ +import json +import requests + +AUTH = "Basic 6ZmI5aSn5a6dOmNwMTIzNDU2" +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0", + "Referer": "https://www.gongzicp.com/", + "Origin": "https://www.gongzicp.com", + "Authorization": AUTH, + "Content-Type": "application/json", +} + +body = {"url": "/novel/chapterGetList", "params": {"nid": 429598}} +for endpoint in [ + "https://www.gongzicp.com/webapi", + "https://www.gongzicp.com/webapi/", + "https://www.gongzicp.com/webapi/novel/chapterGetList", + "https://api1.gongzicp.com/apiv2/novel/chapterGetList", +]: + r = requests.post(endpoint, headers=headers, json=body, timeout=30) + print(endpoint, r.status_code, len(r.text), r.text[:800]) + +# GET webapi with query? +r = requests.get( + "https://www.gongzicp.com/webapi", + headers=headers, + params={"url": "/novel/chapterGetList", "nid": 429598}, + timeout=30, +) +print("GET webapi", r.status_code, r.text[:500]) + +# try apiv2 with form data +import urllib.parse +data = urllib.parse.urlencode({"nid": "429598"}) +r = requests.post( + "https://api1.gongzicp.com/apiv2/novel/chapterGetList", + headers={**headers, "Content-Type": "application/x-www-form-urlencoded"}, + data=data, + timeout=30, +) +print("form", r.status_code, r.text[:500]) diff --git a/tools/probe_gongzicp_playwright.py b/tools/probe_gongzicp_playwright.py new file mode 100644 index 0000000..e8280d5 --- /dev/null +++ b/tools/probe_gongzicp_playwright.py @@ -0,0 +1,45 @@ +"""Capture gongzicp API calls via Playwright.""" +from __future__ import annotations + +import json +from playwright.sync_api import sync_playwright + +from pixiv_app.core.playwright_runtime import configure_playwright_runtime_env, preferred_chromium_executable + + +def main() -> None: + configure_playwright_runtime_env() + calls: list[dict] = [] + + with sync_playwright() as p: + browser = p.chromium.launch( + headless=True, + executable_path=preferred_chromium_executable(), + ) + context = browser.new_context( + user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0", + ) + page = context.new_page() + + def on_request(request): + url = request.url + if "gongzicp" in url and ("apiv2" in url or "webapi" in url): + post = request.post_data or "" + calls.append( + { + "url": url, + "method": request.method, + "post": post[:2000], + } + ) + + page.on("request", on_request) + page.goto("https://www.gongzicp.com/novel-429598.html", wait_until="networkidle", timeout=120000) + page.wait_for_timeout(5000) + browser.close() + + print(json.dumps(calls, ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/tools/test_changpei_plugin.py b/tools/test_changpei_plugin.py new file mode 100644 index 0000000..75840a0 --- /dev/null +++ b/tools/test_changpei_plugin.py @@ -0,0 +1,21 @@ +"""Quick smoke test for changpei plugin.""" +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from pixiv_app.core.plugin.manager import PluginManager + +manager = PluginManager([ROOT / "plugins"]) +plugin = manager.load_plugin(ROOT / "plugins" / "changpei" / "plugin.py") +assert plugin is not None +print("loaded", plugin.name, plugin.version) + +outcome = plugin.resolve_target("2058540") +resources = outcome["resources"] +print("chapters", len(resources), outcome["book_title"]) + +chapter = resources[1] +paths = plugin.download(chapter, ROOT / ".tmp_changpei_test") +print("saved", paths[0], paths[0].stat().st_size) diff --git a/产品说明文档.md b/产品说明文档.md new file mode 100644 index 0000000..108c24f --- /dev/null +++ b/产品说明文档.md @@ -0,0 +1,161 @@ +# 樱云下载器(Sakura Downloader)产品说明文档 + +版本:V1.0 +更新日期:2026-11-05 +文档类型:产品说明 + +--- + +## 1. 产品概述 + +### 1.1 产品名称 + +樱云下载器(Sakura Downloader) + +### 1.2 产品定位 + +一款面向个人创作者与插画/漫画/小说爱好者的**个人媒体采集、本地图库管理与跨设备云同步系统**。 + +产品覆盖从「内容采集 → 本地整理 → 云端存储 → 多端阅读」的完整链路,解决用户在 Pixiv、X(Twitter)等平台收藏作品后"散落各处、难以管理、换设备看不了"的问题。 + +### 1.3 一句话描述 + +桌面端是"采集与整理中枢",云端是"数据与账号中心",手机端是"个人阅读器"。 + +--- + +## 2. 产品架构 + +| 端 | 载体 | 角色 | +|----|------|------| +| Windows 桌面端 | `sakuradownloader.exe` | 登录、抓取、下载、本地图库管理、云同步 | +| Sakura Cloud 云端 | FastAPI 服务 | 账号体系、媒体存储、关注与提醒数据 | +| Android 移动端 | `app-release.apk` | 跨设备浏览、阅读、收藏、提醒查看 | + +数据流向: + +``` +Pixiv / X 平台 + │ (桌面端抓取下载) + ▼ +本地图库(SQLite + 本地文件) + │ (云同步上传) + ▼ +Sakura Cloud(媒体 + 账号 + 关注/提醒数据) + │ (同账号拉取) + ▼ +Android 客户端(图片 / 漫画 / 小说 / 收藏 / 提醒) +``` + +--- + +## 3. 核心功能 + +### 3.1 桌面端(采集与整理中枢) + +#### 3.1.1 账号登录 +- 支持 **Cookie 登录**(推荐,稳定)与 **账号密码登录** 两种方式。 +- 支持导入 `cookies.txt` / `json` 格式的会话文件,可一键检测与保存会话。 + +#### 3.1.2 内容抓取下载 +通过「平台 + 模式 + 目标」三项配置发起抓取: + +| 模式 | 目标输入 | +|------|----------| +| 作者全部作品 | 作者 ID 或任意该作者的作品链接(自动反查作者) | +| 作品 / 漫画 | 作品 ID 或作品链接 | +| 文章 / 小说 | 小说 ID、小说链接或小说系列链接 | +| 关键词 | 关键词(先搜索后下载) | + +下载内容自动进入本地图库并写入数据库。 + +#### 3.1.3 缩略图墙(本地图库) +程序在本机启动本地服务(默认 `http://127.0.0.1:8765`,端口占用时自动切换 8766/8767),在浏览器中提供: +- 本地图片/漫画浏览(默认不含小说) +- 标题、作者、简介搜索;按标签筛选 +- 作者主页、收藏功能 +- 「关注同步」页:维护 Pixiv 关注作者、导入 Cookie、一键同步关注作者新作品 +- 「X 关注」页:手动添加 X 用户名并同步时间线新动态 + +#### 3.1.4 云同步 +- 勾选启用 Sakura Cloud 同步,登录/注册云端账号后自动保存凭证。 +- 登录成功后,新下载内容**自动上传云端**,手机端同账号即可查看。 +- 关注作者的新作品会写入云端 follow / alert 记录,作为提醒数据来源。 + +### 3.2 云端(数据与账号中心) +- 账号注册/登录(JWT 鉴权)、媒体存储与访问。 +- 关注(follow)与更新 alert 数据管理。 +- **每日提醒摘要**:每 5 分钟检查一次,对已开启提醒的用户,将当日未读更新汇总为一条应用内"每日摘要提醒",同一天不重复生成。 + +### 3.3 Android 端(个人阅读器) + +| 页签 | 功能 | +|------|------| +| 发现 | 最近浏览的图片/漫画/小说、更新提醒摘要展示 | +| 图片 | 云端插画列表、详情、收藏、下载、编辑、删除 | +| 漫画 | 按漫画分组进入,整组连续浏览、整组下载 | +| 书架 | 三列彩色方块,系列小说按书名归档,章节选择,按阅读记录排序 | +| 我的 | 用户资料、云端空间占用、收藏、每日提醒设置、退出登录 | + +#### 小说阅读器 +- 自动分页、进度记忆、收藏/取消收藏 +- 当前页翻译、原文/译文切换 +- 字体大小、行高、背景色、章节切换等阅读设置 +- 系列小说章节列表直接切换 + +#### 每日更新提醒 +- 开关位于「我的」页,可设置提醒时间(默认时区 Asia/Shanghai)。 +- 形式为**云端汇总的应用内提醒**,展示在「发现」页"更新提醒"区域,不是系统级推送通知。 +- 生效前提:桌面端已维护关注作者并启用云同步,双端使用同一账号,且关注作者有新内容进入云端。 + +--- + +## 4. 技术栈 + +| 模块 | 技术 | +|------|------| +| 桌面端 | Python 3.8+、CustomTkinter、SQLite、Playwright(X 插件) | +| 云端 | FastAPI、PostgreSQL、JWT、Docker Compose、Alembic | +| 移动端 | Flutter、Riverpod、Dio | + +--- + +## 5. 分发成品 + +| 文件 | 说明 | 大小(约) | +|------|------|------------| +| `releases/sakuradownloader.exe` | Windows 桌面端,双击即用 | ~68 MB | +| `releases/app-release.apk` | Android 客户端(调试签名,适合自测) | ~54 MB | + +--- + +## 6. 用户价值 + +1. **集中管理**:Pixiv / X 的插画、漫画、小说统一采集入库,告别散落收藏。 +2. **本地资产**:下载内容落在本地图库,带搜索、标签、作者、收藏体系。 +3. **多端同步**:桌面采集,手机阅读,进度、收藏、提醒全端同步。 +4. **追更提醒**:关注作者有新作,每日定时汇总提醒,不再手动刷动态。 +5. **离线阅读**:小说自动分页、进度记忆,适合长期追更连载。 + +--- + +## 7. 已知限制 + +- Android APK 当前为**调试签名**,仅供个人测试与自用,不宜正式上架分发。 +- 「账号密码登录」在遇到 Pixiv 额外验证、二步验证或风控时成功率较低,推荐 Cookie 登录。 +- 每日提醒为云端应用内摘要提醒,非手机系统级推送,不会弹系统通知。 +- 提醒数据链路目前主要落在 Pixiv 侧;X 关注已有同步入口,判新依赖作者时间线,稳定性相对更好,但整体仍建议以"Pixiv / X 关注作者更新摘要"的预期使用。 +- 使用 X 插件前需安装 Playwright Chromium(见 `项目文件/BUILD.md`)。 +- 会话与图库数据默认保存在 `%LOCALAPPDATA%\PixivDownloader`(打包版)。 + +--- + +## 8. 配套文档 + +| 文档 | 面向读者 | +|------|----------| +| [使用说明-桌面版与安卓版.md](使用说明-桌面版与安卓版.md) | 终端用户:登录、下载、云同步、手机端操作 | +| [docs/项目功能交付书.md](docs/项目功能交付书.md) | 产品 / 验收:功能模块、架构、API、代码索引 | +| [docs/项目文档.md](docs/项目文档.md) | 开发者:环境、目录、构建、部署 | +| [项目文件/BUILD.md](项目文件/BUILD.md) | 开发者:EXE / APK 打包细节与排错 | +| [README.md](README.md) | 项目总览与快速开始 | diff --git a/使用说明-桌面版与安卓版.md b/使用说明-桌面版与安卓版.md new file mode 100644 index 0000000..8ad4b5d --- /dev/null +++ b/使用说明-桌面版与安卓版.md @@ -0,0 +1,491 @@ +# 樱云同步使用说明(桌面版与安卓版) + +更新时间:2026-06-05 + +## 1. 成品文件位置 + +建议直接使用 `releases/` 目录下的成品: + +- `releases/sakuradownloader.exe` +- `releases/app-release.apk` +- `使用说明-桌面版与安卓版.md`(本文件,位于项目根目录) + +对应原始产物来源如下: + +- Windows EXE 来源:`dist/sakuradownloader.exe` +- Android APK 来源:`sakura_mobile/build/app/outputs/flutter-apk/app-release.apk` + +说明: + +- `releases/app-release.apk` 与 `sakura_mobile/build/app/outputs/flutter-apk/app-release.apk` 为同一构建产物;重新打包后请运行 `scripts\copy_release_artifacts.bat` 同步到 `releases/`。 +- 当前 Android `release` 包仍然是调试签名,适合自测和个人使用,不建议直接用于正式分发或上架。 + +## 2. 这个项目现在能做什么 + +这个项目目前可以分成两部分来理解: + +- 桌面端 `sakuradownloader.exe` + 负责登录、抓取、下载、整理本地图库、打开缩略图墙、关注作者,以及把内容同步到云端。 +- 安卓端 `app-release.apk` + 负责登录同一个云端账号后,在手机上浏览图片、漫画、小说、收藏、阅读进度和提醒。 + +一句话版本: + +- 桌面端更像“采集和整理中枢” +- 安卓端更像“个人阅读器” + +## 3. Windows 桌面版使用方法 + +### 3.1 启动 + +双击 `releases/sakuradownloader.exe` 即可启动。 + +如果你只是想直接用成品,优先用 `releases/` 里的 EXE,不需要再去 `dist` 里找。 + +### 3.2 登录 Pixiv + +桌面端支持两种方式: + +- `Cookie 登录` +- `账号密码登录` + +当前项目里更推荐 `Cookie 登录`,也是代码里明确提示的稳定方案。 + +推荐流程: + +1. 在已经登录 Pixiv 的浏览器里,用 `Get cookies.txt` 之类的插件导出 `txt` 或 `json` +2. 打开桌面端 +3. 在“登录方式”卡片里保持 `Cookie 登录` +4. 点击 `导入 cookies.txt / json` +5. 导入后可点击 `检测会话` +6. 确认没问题后点击 `保存会话` + +补充说明: + +- `账号密码登录` 仍可尝试,但 Pixiv 额外验证、二步验证或风控时更容易失败。 +- 如果后续抓取突然失效,优先重新导入一次最新 Cookie。 + +### 3.3 下载 Pixiv 内容 + +桌面端主界面有“抓取目标”卡片,核心是三项: + +- `平台` +- `模式` +- `目标` + +Pixiv 常用模式: + +- `作者全部作品` + 目标里可以填作者 ID,也可以直接贴任意该作者作品链接,程序会自动反查作者。 +- `作品 / 漫画` + 目标里填作品 ID 或作品链接。 +- `文章 / 小说` + 目标里填小说 ID、小说链接,或者小说系列链接。 +- `关键词` + 目标里填关键词,程序先搜索再下载。 + +常见输入例子: + +- 作者 ID +- Pixiv 作品链接 +- Pixiv 小说链接 +- 小说系列链接 + +操作步骤: + +1. 选择 `平台 = Pixiv` +2. 选择合适的 `模式` +3. 在 `目标` 输入框里填 ID 或链接 +4. 点击 `开始抓取` +5. 等待右侧日志和进度完成 + +下载后的内容会进入本地图库,同时写入本地数据库,后续缩略图墙、收藏和作者视图都会用到这些数据。 + +### 3.4 打开缩略图墙 + +桌面主界面有一个明显按钮: + +- `打开缩略图墙` + +点它后,程序会在本机启动一个本地服务,并用浏览器打开本地图库页面。 + +默认本地地址通常是: + +- `http://127.0.0.1:8765` + +如果端口被占用,程序会自动尝试 `8766` 或 `8767`。 + +缩略图墙适合做这些事: + +- 浏览本地图片和漫画 +- 默认不显示小说 +- 搜索标题、作者、简介 +- 按标签筛选 +- 查看作者 +- 收藏作品 +- 进入“关注同步”页维护关注作者 +- 进入“X 关注”页维护 X 作者 + +### 3.5 在缩略图墙里关注作者 + +“每日提醒”要先有“关注作者”这一步。 + +当前仓库里,明确可见的关注入口在桌面端缩略图墙。 + +有两种常用方式: + +- 方式一:在作品详情里点 `关注作者` +- 方式二:进入 `关注同步` 页,手动填写 `作者 ID`,再点 `添加关注` + +`关注同步` 页面里还能做两件事: + +- 导入 `Pixiv Cookie` +- 点击 `同步关注作者` + +注意: + +- 当前这套“关注同步”页面还是围绕 Pixiv 作者 ID 和 Pixiv Cookie 做的。 +- 另外已经新增了 `X 关注` 区域,可以手动添加 X 用户名并做同步。 + +### 3.6 云同步 + +如果你想让手机端看到桌面端内容,必须用到云同步。 + +桌面端左侧有 `云同步` 卡片,使用方法如下: + +1. 勾选 `启用 Sakura Cloud 同步` +2. 点击 `账号管理` +3. 在弹窗里登录或注册云端账号 +4. 登录成功后,云同步凭证会自动保存 +5. 回到主界面后可以点 `测试连接` + +项目当前代码里写死的默认云端地址是: + +- `http://api.kunkunxiaomao.top:8000` + +登录成功并开启云同步后: + +- 新下载完成的 Pixiv 内容会自动上传到 Sakura Cloud +- 手机端登录同一个账号后就能刷新看到内容 + +## 4. Android 安卓版使用方法 + +### 4.1 安装 + +安装 `releases/` 目录下的 APK: + +- `releases/app-release.apk` + +如果系统拦截安装,按 Android 的常规流程允许“未知来源应用安装”即可。 + +再次提醒: + +- 当前 APK 是调试签名,不适合正式上架,只适合自测和个人使用。 + +### 4.2 登录 + +安卓端打开后会先进入登录/注册页。 + +使用方法: + +1. 用和桌面端云同步相同的账号登录 +2. 如果没有账号,可以先注册 +3. 注册成功后不会自动登录,需要回到登录页手动登录一次 + +只有登录同一个云端账号,桌面端同步上去的图片、漫画、小说、收藏和提醒才能在手机上看到。 + +### 4.3 安卓端底部导航 + +当前底部共有 5 个页签: + +- `发现` +- `图片` +- `漫画` +- `书架` +- `我的` + +各页作用如下。 + +### 4.4 发现页 + +`发现` 页更像首页和动态页,主要展示: + +- 最近浏览的图片 +- 最近浏览的漫画 +- 最近阅读的小说 +- 更新提醒 + +如果你开启了“每日提醒”,提醒摘要会出现在这里的 `更新提醒` 区域。 + +### 4.5 图片页 + +`图片` 页展示云端同步过来的插画内容。 + +可以做的操作包括: + +- 看缩略图列表 +- 点进详情 +- 收藏 / 取消收藏 +- 下载到本地 +- 编辑图片信息 +- 删除图片 + +### 4.6 漫画页 + +`漫画` 页现在是按漫画分组进入,不再是单张图散着看。 + +实际体验上会更像: + +- 先看到一组漫画入口 +- 点进去后连续浏览整组内容 +- 支持整组下载 + +这也是当前项目里已经调整过的重点之一。 + +### 4.7 书架页 + +`书架` 页现在是三列彩色方块风格。 + +特点: + +- 固定三列 +- 系列小说会优先按书名归档 +- 单篇小说也能单独展示 +- 点击系列后会先弹出章节选择 +- 阅读记录会影响排序,最近看的更靠前 + +### 4.8 小说阅读页 + +小说阅读器已经改成更适合长期阅读的样子,主要能力有: + +- 自动分页 +- 记住当前阅读进度 +- 收藏 / 取消收藏 +- 当前页翻译 +- 原文 / 译文切换 +- 底部阅读设置面板 +- 系列章节列表切换 + +阅读页右上角常用功能: + +- 收藏 +- 翻译当前页 +- 删除小说 + +底部阅读设置面板支持: + +- `字体大小` +- `行高` +- `背景色` +- `章节切换` + +如果是系列小说,底部面板里会直接列出章节,点一下就能切换。 + +### 4.9 我的页 + +`我的` 页负责这些内容: + +- 用户资料 +- 云端空间占用 +- 我的收藏 +- 收藏图片 +- 收藏小说 +- 每日更新提醒 +- 退出登录 + +其中“每日更新提醒”就在这里设置开关和时间。 + +## 5. 每日提醒功能的真实使用链路 + +这一节我按当前仓库里的真实实现来写,不按想象写。 + +### 5.1 先说结论 + +当前“每日提醒”不是手机本地闹钟,也不是系统级推送通知。 + +它更准确的工作方式是: + +- 云端保存你关注作者产生的更新 alert +- 云端到点后把这些未读 alert 汇总成一条“每日摘要提醒” +- 手机端在 `发现` 页的 `更新提醒` 区域把它显示出来 + +也就是说: + +- 它是“云端摘要提醒” +- 不是“手机本地定时弹通知” + +### 5.2 开关位置 + +安卓端位置: + +1. 登录后进入 `我的` +2. 找到 `每日更新提醒` +3. 打开开关 +4. 点 `提醒时间 HH:MM` 选择时间 +5. 需要看最近提醒或已关注作者时,点 `打开提醒中心` + +当前支持保存的字段包括: + +- 是否开启 +- 小时 +- 分钟 +- 时区 + +默认时区是: + +- `Asia/Shanghai` + +### 5.3 提醒真正依赖什么 + +要让提醒真正有内容,至少要满足下面这些条件: + +1. 你已经在桌面端维护了“关注作者” +2. 桌面端已经登录并启用了云同步 +3. 手机端和桌面端用的是同一个云端账号 +4. 关注作者确实有新内容进入了云端 alert 流程 +5. 到了你设定的提醒时间 + +如果只是把手机上的提醒开关打开,但桌面端没有把作者更新同步进云端,那么手机端不会凭空出现提醒。 + +### 5.4 推荐使用步骤 + +按当前代码,最稳妥的使用顺序建议这样走: + +1. 在桌面端登录 Pixiv,优先用 Cookie +2. 在桌面端启用 `云同步`,并登录 Sakura Cloud 账号 +3. 打开 `缩略图墙` +4. 在作品详情里点 `关注作者`,或者在 `关注同步` 页面手动 `添加关注` +5. 如果要同步 X,就到 `X 关注` 区域添加用户名后再点 `同步 X 关注` +6. 让关注作者的新作品经过桌面端下载,并同步到云端 +7. 在手机端登录同一个云端账号 +8. 到 `我的` 页打开 `每日更新提醒`,或者直接进入 `提醒中心` +9. 设定提醒时间 +10. 到点后去 `发现` 页查看 `更新提醒` + +### 5.5 当前代码里已经确认到的事实 + +我在仓库里确认到了下面几件事: + +- 手机端“我的”页明确有 `每日更新提醒` 开关和时间选择 +- 手机端“发现”页明确会展示 `更新提醒` +- 云端会每 5 分钟检查一次提醒摘要 +- 云端启动时也会先跑一次提醒摘要检查 +- 云端只会给已开启提醒的用户生成摘要 +- 同一天内已经发过一次摘要,就不会重复生成同一天第二条摘要 + +摘要标题格式类似: + +- `每日 09:00 更新提醒` + +摘要正文内容类似: + +- 你关注的作者有多少条新动态 +- 这些动态来自哪些平台 +- 还会尽量带上最近几个作者名字 + +### 5.6 一个很重要的现实限制 + +虽然手机端文案写的是: + +- `Pixiv / X 博主` + +现在仓库里已经同时接通了 `Pixiv` 和 `X` 的关注管理: + +- 桌面端既有 Pixiv 风格的作者关注,也有 X 关注同步入口 +- 桌面端向云端写入 follow / alert 的代码路径也已经覆盖 Pixiv 和 X + +换句话说: + +- 后端模型和提醒摘要对 `Pixiv / X` 是通用设计 +- 但当前仓库里我真正看到的、已经接上的前端入口和 alert 创建链路,主要落在 Pixiv + +所以更稳妥的理解是: + +- 现在请把“每日提醒”当成 `Pixiv / X 关注作者更新摘要` 来使用 +- X 的判新依赖作者时间线,稳定性通常比纯媒体页更好 + +### 5.7 关于“同步关注作者”按钮的补充说明 + +缩略图墙里的 `同步关注作者` 按钮,当前明确作用是: + +- 用已保存的 Pixiv Cookie 去拉取关注作者的新作品 +- 把新作品下载到本地图库 +- `同步 X 关注` 会按时间线检查 X 作者的新动态,并把结果写到云端 follow / alert + +当前我能明确确认的提醒写入路径,是: + +- 桌面端 Pixiv / X 内容在云同步过程中 +- 如果作者已经在本地关注列表里 +- 程序就会向云端写入 follow 和 update alert + +因此更稳妥的使用建议是: + +- 把 `同步关注作者` 当作“本地补新”功能 +- 把 `同步 X 关注` 当作“X 作者新动态补新”功能 +- 把“真正用于手机提醒的数据来源”理解为“已经被桌面端同步进云端的更新记录” + +## 6. 常见问题 + +### 6.1 桌面端登录失败怎么办 + +优先做这几件事: + +1. 改用 `Cookie 登录` +2. 重新导出最新 cookies.txt +3. 重新导入后再点 `检测会话` + +账号密码登录不是不能用,但稳定性不如 Cookie。 + +### 6.2 手机端登录了还是没内容 + +通常优先排查下面几点: + +1. 桌面端是否启用了 `云同步` +2. 桌面端是否真的登录了云端账号 +3. 下载是否已经完成 +4. 下载完成后是否已经同步到云端 +5. 手机和桌面是否是同一个账号 + +### 6.3 每日提醒开了却看不到提醒 + +按顺序检查: + +1. 是否已经在桌面端关注了作者 +2. 是否有新作品真的进入云端 +3. 是否已经超过你设置的提醒时间 +4. 手机端是否登录同一账号 +5. 是否去 `发现` 页看 `更新提醒` + +再强调一次: + +- 这不是手机本地闹钟 +- 开关打开后不会立刻弹系统通知 +- 它是云端汇总后的应用内提醒 + +### 6.4 安卓安装被拦截 + +这是常见现象,尤其是未上架、未正式签名的测试包。 + +当前处理方式通常是: + +- 允许未知来源安装 +- 再重新安装一次 + +### 6.5 Android 包能不能直接拿去正式发给别人 + +不建议。 + +原因很直接: + +- 当前 `app-release.apk` 仍是调试签名 +- 更适合自测或小范围个人使用 +- 如果要正式分发,最好重新做正式 keystore 和正式签名 + +## 7. 当前版本的整理结果 + +分发成品时,建议使用 `releases/` 目录: + +- `releases/sakuradownloader.exe` +- `releases/app-release.apk` + +用户手册位于项目根目录:`使用说明-桌面版与安卓版.md`