175 lines
5.8 KiB
Python
175 lines
5.8 KiB
Python
import json
|
|
import re
|
|
|
|
from server.services.dramabox.client import SITE, USER_AGENT, WEBFIC, client
|
|
from server.services.dramabox.locales import film_path, resolve
|
|
from server.services.http import http
|
|
|
|
_NEXT_DATA = re.compile(r'<script id="__NEXT_DATA__"[^>]*>(.*?)</script>', re.S)
|
|
|
|
|
|
def home_index(language: str | None = None) -> list[dict]:
|
|
data = _webfic("POST", "/webfic/home/index", {}, language)
|
|
return data if isinstance(data, list) else []
|
|
|
|
|
|
_ALL_LABELS: dict[str, str] = {}
|
|
|
|
|
|
def all_label(language: str | None = None) -> str:
|
|
locale = resolve(language or client.language)["id"]
|
|
cached = _ALL_LABELS.get(locale)
|
|
if cached:
|
|
return cached
|
|
url = f"https://infocdn.dramaboxdb.com/seo/dramaboxapp/locales/{locale}/common.json"
|
|
label = ""
|
|
try:
|
|
response = http.send(
|
|
"GET",
|
|
url,
|
|
headers={"User-Agent": USER_AGENT, "Referer": f"{SITE}/", "Accept": "application/json"},
|
|
timeout=20.0,
|
|
)
|
|
response.raise_for_status()
|
|
payload = response.json()
|
|
browse_text = payload.get("browse") if isinstance(payload, dict) else None
|
|
if isinstance(browse_text, dict):
|
|
label = str(browse_text.get("all") or "").strip()
|
|
except Exception:
|
|
label = ""
|
|
if not label:
|
|
label = "All"
|
|
_ALL_LABELS[locale] = label
|
|
return label
|
|
|
|
|
|
def browse(page: int, type_id: int, language: str | None = None, page_size: int = 18) -> dict:
|
|
data = _webfic(
|
|
"POST",
|
|
"/webfic/home/browse",
|
|
{"pageNo": page, "pageSize": page_size, "typeTwoId": int(type_id)},
|
|
language,
|
|
)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def home_more(name: str, page: int = 1, page_size: int = 18, language: str | None = None) -> dict:
|
|
data = _webfic(
|
|
"POST",
|
|
"/webfic/home/more",
|
|
{"name": name, "pageNum": page, "pageSize": page_size},
|
|
language,
|
|
)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def search_books(keyword: str, page: int = 1, page_size: int = 20, language: str | None = None) -> dict:
|
|
data = _webfic(
|
|
"POST",
|
|
"/webfic/book/search",
|
|
{"keyword": keyword, "pageNo": page, "pageSize": page_size},
|
|
language,
|
|
)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def book_detail(book_id: str, language: str | None = None) -> dict:
|
|
data = _webfic("POST", "/webfic/book/detail", {"bookId": str(book_id)}, language)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def film(book_id: str, language: str | None = None) -> dict:
|
|
locale = resolve(language or client.language)["id"]
|
|
book_id = str(book_id or "").strip()
|
|
if not book_id:
|
|
raise RuntimeError("没有这部剧")
|
|
path = film_path(locale, book_id)
|
|
payload = _next_data(path) or _html_data(path)
|
|
if not payload:
|
|
raise RuntimeError("没有读到这部剧")
|
|
return payload
|
|
|
|
|
|
def cdn_headers(language: str | None = None, book_id: str = "") -> dict:
|
|
locale = resolve(language or client.language)["id"]
|
|
referer = SITE + (film_path(locale, book_id) if book_id else "/")
|
|
return {
|
|
"User-Agent": USER_AGENT,
|
|
"Referer": referer,
|
|
"Origin": SITE,
|
|
"Accept": "*/*",
|
|
}
|
|
|
|
|
|
def _webfic(method: str, path: str, body: dict, language: str | None) -> object:
|
|
locale = resolve(language or client.language)["id"]
|
|
headers = {
|
|
"User-Agent": USER_AGENT,
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
"pline": "DRAMABOX",
|
|
"language": locale,
|
|
"Referer": f"{SITE}/",
|
|
"Origin": SITE,
|
|
}
|
|
url = WEBFIC + path
|
|
content = json.dumps(body, ensure_ascii=False, separators=(",", ":")).encode("utf-8") if method != "GET" else None
|
|
try:
|
|
response = http.send(method, url, headers=headers, content=content, timeout=30.0)
|
|
response.raise_for_status()
|
|
payload = response.json()
|
|
except Exception as exc:
|
|
raise RuntimeError("DramaBox 接口请求失败") from exc
|
|
if not isinstance(payload, dict):
|
|
raise RuntimeError("接口没有返回数据")
|
|
if payload.get("success") is False or payload.get("status") not in (0, None, "0"):
|
|
raise RuntimeError(str(payload.get("message") or "请求失败"))
|
|
return payload.get("data")
|
|
|
|
|
|
def _next_data(path: str) -> dict:
|
|
build = (client.build_id or "").strip()
|
|
if not build:
|
|
return {}
|
|
url = f"{SITE}/_next/data/{build}{path}.json"
|
|
try:
|
|
response = http.send("GET", url, headers=_site_headers(path), timeout=30.0)
|
|
response.raise_for_status()
|
|
payload = response.json()
|
|
except Exception:
|
|
return {}
|
|
props = payload.get("pageProps") if isinstance(payload, dict) else None
|
|
return props if isinstance(props, dict) else {}
|
|
|
|
|
|
def _html_data(path: str) -> dict:
|
|
url = SITE + path
|
|
try:
|
|
response = http.send("GET", url, headers=_site_headers(path), timeout=30.0)
|
|
response.raise_for_status()
|
|
text = response.text
|
|
except Exception as exc:
|
|
raise RuntimeError("DramaBox 页面读取失败") from exc
|
|
match = _NEXT_DATA.search(text)
|
|
if not match:
|
|
raise RuntimeError("没有读到这部剧")
|
|
try:
|
|
payload = json.loads(match.group(1))
|
|
except json.JSONDecodeError as exc:
|
|
raise RuntimeError("这部剧的数据无效") from exc
|
|
build = str(payload.get("buildId") or "").strip()
|
|
if build and build != client.build_id:
|
|
client.build_id = build
|
|
client.save()
|
|
props = payload.get("props", {}).get("pageProps") if isinstance(payload, dict) else None
|
|
return props if isinstance(props, dict) else {}
|
|
|
|
|
|
def _site_headers(path: str) -> dict:
|
|
return {
|
|
"User-Agent": USER_AGENT,
|
|
"Accept": "text/html,application/json",
|
|
"Referer": SITE + (path if path.startswith("/") else f"/{path}"),
|
|
"Origin": SITE,
|
|
}
|