feat: 补全短剧全量清单并接入 DramaBox 官网登录
This commit is contained in:
@@ -13,6 +13,36 @@ def home_index(language: str | None = None) -> list[dict]:
|
||||
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",
|
||||
|
||||
@@ -16,7 +16,7 @@ def enabled_cache() -> bool:
|
||||
|
||||
|
||||
def cache_path() -> Path:
|
||||
return ROOT / "cache" / "dramabox" / f"catalog-{client.language}.json"
|
||||
return ROOT / "cache" / "dramabox" / f"catalog-browse-{client.language}.json"
|
||||
|
||||
|
||||
async def stream(refresh: bool = False):
|
||||
@@ -41,31 +41,24 @@ async def stream(refresh: bool = False):
|
||||
_write_cache(path, collected)
|
||||
|
||||
|
||||
PAGE_SIZE = 60
|
||||
|
||||
|
||||
async def _fetch():
|
||||
home = await asyncio.to_thread(api.home_index)
|
||||
first = await asyncio.to_thread(api.browse, 1, 0)
|
||||
language = client.language
|
||||
first = await asyncio.to_thread(api.browse, 1, 0, language, PAGE_SIZE)
|
||||
all_name = await asyncio.to_thread(api.all_label, language)
|
||||
types = [item for item in (first.get("types") or []) if isinstance(item, dict)]
|
||||
jobs: list[tuple[str, str, str, object]] = []
|
||||
sections = []
|
||||
for column in home:
|
||||
if not isinstance(column, dict):
|
||||
continue
|
||||
name = str(column.get("name") or "").strip()
|
||||
if not name:
|
||||
continue
|
||||
key = f"home-{column.get('id') or name}"
|
||||
sections.append({"key": key, "name": name, "note": "", "loading": True})
|
||||
jobs.append((key, name, "home", column))
|
||||
sections.append({"key": "browse-0", "name": "全部", "note": "", "loading": True})
|
||||
jobs.append(("browse-0", "全部", "browse", {"id": 0, "seed": first}))
|
||||
jobs: list[tuple[str, str, int]] = [("browse-0", all_name, 0)]
|
||||
sections = [{"key": "browse-0", "name": all_name, "note": "", "loading": True}]
|
||||
for item in types:
|
||||
type_id = _int(item.get("id"))
|
||||
name = str(item.get("name") or "").strip()
|
||||
if type_id <= 0 or not name:
|
||||
if type_id <= 0 or not name or name.lower() == "all":
|
||||
continue
|
||||
key = f"browse-{type_id}"
|
||||
sections.append({"key": key, "name": name, "note": "", "loading": True})
|
||||
jobs.append((key, name, "browse", {"id": type_id}))
|
||||
jobs.append((key, name, type_id))
|
||||
if not sections:
|
||||
raise RuntimeError("没有获取到分类")
|
||||
yield {"type": "outline", "sections": sections}
|
||||
@@ -75,14 +68,11 @@ async def _fetch():
|
||||
limit = asyncio.Semaphore(6)
|
||||
|
||||
async def run(job):
|
||||
key, name, kind, payload = job
|
||||
key, name, type_id = job
|
||||
|
||||
def work():
|
||||
try:
|
||||
if kind == "home":
|
||||
items = _home_items(payload)
|
||||
else:
|
||||
items = _browse_items(payload)
|
||||
items = _browse_items(type_id)
|
||||
loop.call_soon_threadsafe(queue.put_nowait, _page(key, name, items, done=True))
|
||||
except Exception as exc:
|
||||
loop.call_soon_threadsafe(
|
||||
@@ -118,74 +108,58 @@ def search(keyword: str) -> list[dict]:
|
||||
return []
|
||||
items = []
|
||||
seen: set[str] = set()
|
||||
for page in range(1, 4):
|
||||
payload = api.search_books(text, page=page, page_size=20)
|
||||
page = 1
|
||||
page_size = 60
|
||||
previous: tuple[str, ...] | None = None
|
||||
while True:
|
||||
payload = api.search_books(text, page=page, page_size=page_size)
|
||||
rows = payload.get("records") if isinstance(payload, dict) else None
|
||||
if not isinstance(rows, list) or not rows:
|
||||
break
|
||||
signature = tuple(str(item.get("bookId") or "") for item in rows if isinstance(item, dict))
|
||||
if signature == previous:
|
||||
break
|
||||
previous = signature
|
||||
for item in rows:
|
||||
drama = _drama(item)
|
||||
if not drama or drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
items.append(drama)
|
||||
if len(rows) < 20:
|
||||
total = _int(payload.get("total")) if isinstance(payload, dict) else 0
|
||||
if len(rows) < page_size or (total and page * page_size >= total):
|
||||
break
|
||||
page += 1
|
||||
return items
|
||||
|
||||
|
||||
def _home_items(column: dict) -> list[dict]:
|
||||
def _browse_items(type_id: int) -> list[dict]:
|
||||
items = []
|
||||
seen: set[str] = set()
|
||||
for item in column.get("items") or []:
|
||||
drama = _drama(item)
|
||||
if not drama or drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
items.append(drama)
|
||||
name = str(column.get("name") or "").strip()
|
||||
if name:
|
||||
for page in range(1, 3):
|
||||
payload = api.home_more(name, page=page, page_size=18)
|
||||
block = payload.get("data") if isinstance(payload, dict) else None
|
||||
rows = block.get("items") if isinstance(block, dict) else None
|
||||
if not isinstance(rows, list) or not rows:
|
||||
break
|
||||
for item in rows:
|
||||
drama = _drama(item)
|
||||
if not drama or drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
items.append(drama)
|
||||
pages = _int(payload.get("pages")) if isinstance(payload, dict) else 0
|
||||
if page >= pages:
|
||||
break
|
||||
return items
|
||||
|
||||
|
||||
def _browse_items(payload: dict) -> list[dict]:
|
||||
type_id = _int(payload.get("id"))
|
||||
items = []
|
||||
seen: set[str] = set()
|
||||
seed = payload.get("seed") if isinstance(payload.get("seed"), dict) else None
|
||||
pages = 3 if type_id == 0 else 1
|
||||
for page in range(1, pages + 1):
|
||||
if page == 1 and seed is not None:
|
||||
data = seed
|
||||
else:
|
||||
data = api.browse(page, type_id)
|
||||
page = 1
|
||||
total_pages = 1
|
||||
previous: tuple[str, ...] | None = None
|
||||
while page <= total_pages:
|
||||
data = api.browse(page, type_id, page_size=PAGE_SIZE)
|
||||
rows = data.get("bookList") if isinstance(data, dict) else None
|
||||
reported = _int(data.get("pages")) if isinstance(data, dict) else 0
|
||||
if reported > 0:
|
||||
total_pages = reported
|
||||
if not isinstance(rows, list) or not rows:
|
||||
break
|
||||
signature = tuple(str(item.get("bookId") or "") for item in rows if isinstance(item, dict))
|
||||
if signature == previous:
|
||||
break
|
||||
previous = signature
|
||||
for item in rows:
|
||||
drama = _drama(item)
|
||||
if not drama or drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
items.append(drama)
|
||||
total_pages = _int(data.get("pages")) if isinstance(data, dict) else 0
|
||||
if page >= total_pages:
|
||||
if len(rows) < PAGE_SIZE or page >= total_pages:
|
||||
break
|
||||
page += 1
|
||||
return items
|
||||
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ USER_AGENT = (
|
||||
class DramaBoxClient:
|
||||
def __init__(self) -> None:
|
||||
self.token = ""
|
||||
self.web_token = ""
|
||||
self.language = DEFAULT_LANGUAGE
|
||||
self.build_id = ""
|
||||
self.profile: dict = {}
|
||||
@@ -34,6 +35,7 @@ class DramaBoxClient:
|
||||
if not isinstance(data, dict):
|
||||
return
|
||||
self.token = str(data.get("token") or "")
|
||||
self.web_token = str(data.get("web_token") or "")
|
||||
language = str(data.get("language") or "").strip()
|
||||
if language:
|
||||
try:
|
||||
@@ -49,6 +51,7 @@ class DramaBoxClient:
|
||||
STATE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
payload = {
|
||||
"token": self.token,
|
||||
"web_token": self.web_token,
|
||||
"language": self.language,
|
||||
"build_id": self.build_id,
|
||||
"profile": self.profile,
|
||||
@@ -62,6 +65,7 @@ class DramaBoxClient:
|
||||
self.language = locale["id"]
|
||||
if not self.token:
|
||||
self.token = secrets.token_hex(16)
|
||||
self.web_token = ""
|
||||
self.profile = {
|
||||
"id": locale["id"],
|
||||
"username": locale["id"],
|
||||
@@ -85,6 +89,7 @@ class DramaBoxClient:
|
||||
|
||||
def clear(self) -> None:
|
||||
self.token = ""
|
||||
self.web_token = ""
|
||||
self.profile = {}
|
||||
self.save()
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ from server.services.dramabox import playlist as db_playlist
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
|
||||
|
||||
def _load_episodes(_token, _device, drama_id: str) -> list[dict]:
|
||||
return db_playlist.load_episodes(str(drama_id))
|
||||
def _load_episodes(_token, _device, drama_id: str, locale: str = "") -> list[dict]:
|
||||
return db_playlist.load_episodes(str(drama_id), locale or client.language)
|
||||
|
||||
|
||||
def _write_intro(folder: Path, drama_id: str, title: str, _token: str, _device) -> None:
|
||||
|
||||
@@ -10,6 +10,65 @@ def enter(language: str = "") -> dict:
|
||||
return client.enter(language or client.language or DEFAULT_LANGUAGE)
|
||||
|
||||
|
||||
def apply_account(language: str, user: dict) -> dict:
|
||||
if not isinstance(user, dict):
|
||||
raise RuntimeError("还没有登录成功")
|
||||
web_token = str(user.get("token") or "").strip()
|
||||
if not web_token or not _logged_in(user.get("isLogin")):
|
||||
raise RuntimeError("还没有登录成功")
|
||||
locale = client.enter(language or client.language or DEFAULT_LANGUAGE)["profile"]
|
||||
client.web_token = web_token
|
||||
profile = _account_profile(locale, user)
|
||||
client.profile = profile
|
||||
client.save()
|
||||
return {"token": client.token, "profile": profile}
|
||||
|
||||
|
||||
def _logged_in(value) -> bool:
|
||||
if value is True:
|
||||
return True
|
||||
if isinstance(value, (int, float)) and int(value) == 1:
|
||||
return True
|
||||
text = str(value or "").strip().lower()
|
||||
return text in {"1", "true", "yes"}
|
||||
|
||||
|
||||
def _account_profile(locale: dict, user: dict) -> dict:
|
||||
nickname = str(user.get("nickname") or user.get("name") or user.get("email") or locale.get("country") or "").strip()
|
||||
email = str(user.get("email") or "").strip()
|
||||
avatar = str(user.get("avatar") or user.get("userLogo") or user.get("avatarNumUrl") or "").strip()
|
||||
if not avatar.startswith(("http://", "https://")):
|
||||
avatar = ""
|
||||
vip = _logged_in(user.get("isVip"))
|
||||
return {
|
||||
"id": user.get("uid") or user.get("id") or "",
|
||||
"username": email or nickname,
|
||||
"nickname": nickname or "DramaBox",
|
||||
"email": email,
|
||||
"phone": "",
|
||||
"badge": "VIP" if vip else "",
|
||||
"vipday": 1 if vip else 0,
|
||||
"score": _number(user.get("coins")),
|
||||
"watched": 0,
|
||||
"rewards": _number(user.get("bonus")),
|
||||
"login_day": 0,
|
||||
"country": locale.get("country") or "",
|
||||
"language": locale.get("language") or "",
|
||||
"native": locale.get("native") or "",
|
||||
"locale": locale.get("locale") or locale.get("id") or "",
|
||||
"loginType": str(user.get("loginType") or ""),
|
||||
"has_login": "1",
|
||||
"avatarNumUrl": avatar,
|
||||
}
|
||||
|
||||
|
||||
def _number(value) -> int:
|
||||
try:
|
||||
return int(float(str(value or "0")))
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
|
||||
def current(token: str) -> dict:
|
||||
token = (token or "").strip()
|
||||
if not token:
|
||||
|
||||
@@ -11,15 +11,16 @@ from server.services.playlist import is_direct_media, looks_like_segment
|
||||
CACHE_DIR = Path(__file__).resolve().parents[3] / "cache" / "dramabox" / "playlists"
|
||||
|
||||
|
||||
def load_episodes(drama_id: str) -> list[dict]:
|
||||
def load_episodes(drama_id: str, language: str = "") -> list[dict]:
|
||||
drama_id = str(drama_id or "").strip()
|
||||
language = (language or client.language).strip()
|
||||
if not drama_id:
|
||||
raise RuntimeError("没有这部剧")
|
||||
cached = _read_cache(drama_id)
|
||||
fresh = _fetch_episodes(drama_id)
|
||||
cached = _read_cache(drama_id, language)
|
||||
fresh = _fetch_episodes(drama_id, language)
|
||||
merged, save = _merge(cached, fresh)
|
||||
if save or (not cached and merged):
|
||||
_write_cache(drama_id, merged)
|
||||
_write_cache(drama_id, language, merged)
|
||||
return merged
|
||||
|
||||
|
||||
@@ -47,8 +48,8 @@ def playback_playlist(src: str, drama_id: str = "") -> str:
|
||||
return _rewrite_playlist(http.read_text(variant_url, headers=headers), variant_url)
|
||||
|
||||
|
||||
def _fetch_episodes(drama_id: str) -> list[dict]:
|
||||
data = api.film(drama_id)
|
||||
def _fetch_episodes(drama_id: str, language: str = "") -> list[dict]:
|
||||
data = api.film(drama_id, language or None)
|
||||
rows = data.get("chapterList") if isinstance(data, dict) else None
|
||||
if not isinstance(rows, list) or not rows:
|
||||
raise RuntimeError("没有读到分集")
|
||||
@@ -123,12 +124,12 @@ def _merge(cached: list[dict], fresh: list[dict]) -> tuple[list[dict], bool]:
|
||||
return merged, filled_fresh >= filled_cached and filled_fresh > 0
|
||||
|
||||
|
||||
def _cache_file(drama_id: str) -> Path:
|
||||
return CACHE_DIR / client.language / f"{drama_id}.json"
|
||||
def _cache_file(drama_id: str, language: str = "") -> Path:
|
||||
return CACHE_DIR / (language or client.language) / f"{drama_id}.json"
|
||||
|
||||
|
||||
def _read_cache(drama_id: str) -> list[dict]:
|
||||
path = _cache_file(drama_id)
|
||||
def _read_cache(drama_id: str, language: str = "") -> list[dict]:
|
||||
path = _cache_file(drama_id, language)
|
||||
if not path.is_file():
|
||||
return []
|
||||
try:
|
||||
@@ -139,8 +140,8 @@ def _read_cache(drama_id: str) -> list[dict]:
|
||||
return [row for row in rows if isinstance(row, dict)] if isinstance(rows, list) else []
|
||||
|
||||
|
||||
def _write_cache(drama_id: str, episodes: list[dict]) -> None:
|
||||
path = _cache_file(drama_id)
|
||||
def _write_cache(drama_id: str, language: str, episodes: list[dict]) -> None:
|
||||
path = _cache_file(drama_id, language)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
temporary = path.with_suffix(".json.tmp")
|
||||
temporary.write_text(json.dumps({"episodes": episodes}, ensure_ascii=False), encoding="utf-8")
|
||||
|
||||
Reference in New Issue
Block a user