514 lines
18 KiB
Python
514 lines
18 KiB
Python
"""
|
|
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<nid>\d+)\.html", re.I)
|
|
_READ_PATH_RE = re.compile(r"/read-(?P<nid>\d+)-(?P<cid>\d+)\.html", re.I)
|
|
_SINGLE_READ_RE = re.compile(r"/read-(?P<cid>\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"<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", "")
|
|
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 |