feat: 将工具更名为 VideosDownTools,并完善黄豆登录与清单
This commit is contained in:
@@ -1,41 +1,70 @@
|
||||
import json
|
||||
import re
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote, urljoin
|
||||
|
||||
from server.services.huangdou import api
|
||||
from server.services.huangdou.client import client
|
||||
from server.services.huangdou.coins import (
|
||||
PAID_TYPES,
|
||||
VIP_TYPES,
|
||||
already_bought,
|
||||
buy_method,
|
||||
episode_kind,
|
||||
episode_note,
|
||||
keep_episode,
|
||||
price_of,
|
||||
public_lock_reason,
|
||||
)
|
||||
from server.services.http import http
|
||||
from server.services.huangdou.crypto import USER_AGENT
|
||||
from server.services.playlist import is_direct_media, looks_like_segment
|
||||
|
||||
CACHE_DIR = Path(__file__).resolve().parents[3] / "cache" / "huangdou" / "playlists"
|
||||
CDN_HEADERS = {"User-Agent": USER_AGENT, "Referer": "https://pmihlvee.cc/"}
|
||||
_cache_lock = threading.Lock()
|
||||
|
||||
|
||||
def _cdn_headers() -> dict:
|
||||
return {"User-Agent": USER_AGENT, "Referer": client.host.rstrip("/") + "/"}
|
||||
|
||||
|
||||
def load_episodes(drama_id: str) -> list[dict]:
|
||||
drama_id = str(drama_id or "").strip()
|
||||
if not drama_id:
|
||||
raise RuntimeError("没有这部剧")
|
||||
cached = _read_cache(drama_id)
|
||||
fresh = _fetch_episodes(drama_id)
|
||||
merged, save = _merge(cached, fresh)
|
||||
if save or (not cached and merged):
|
||||
_write_cache(drama_id, merged)
|
||||
detail = api.drama_detail(drama_id, identity=_viewer_identity())
|
||||
rows = detail.get("episodes") if isinstance(detail, dict) else None
|
||||
if not isinstance(rows, list) or not rows:
|
||||
raise RuntimeError("没有读到分集")
|
||||
fresh = _menu_rows(rows)
|
||||
if not fresh:
|
||||
raise RuntimeError("没有读到分集")
|
||||
merged = _keep_sources(_read_cache(drama_id), fresh)
|
||||
_write_cache(drama_id, merged)
|
||||
return merged
|
||||
|
||||
|
||||
def episode_src(drama_id: str, cid: int) -> dict:
|
||||
for ep in load_episodes(drama_id):
|
||||
if int(ep.get("cid") or 0) == int(cid):
|
||||
return ep
|
||||
return {}
|
||||
drama_id = str(drama_id or "").strip()
|
||||
row = _cached_row(drama_id, cid)
|
||||
if row is None:
|
||||
row = _cached_row(drama_id, cid, refresh=True)
|
||||
if not row:
|
||||
return {}
|
||||
if not row.get("playable") or row.get("src"):
|
||||
return row
|
||||
played = _play_rows(drama_id, [dict(row)], allow_buy=False)
|
||||
updated = played[0] if played else row
|
||||
_store_row(drama_id, updated)
|
||||
return updated
|
||||
|
||||
|
||||
def playback_playlist(src: str) -> str:
|
||||
if is_direct_media(src):
|
||||
raise RuntimeError("这一集是整段视频")
|
||||
text = http.read_text(src, headers=CDN_HEADERS)
|
||||
text = http.read_text(src, headers=_cdn_headers())
|
||||
variant_rel = next(
|
||||
(line.strip() for line in text.splitlines() if line.strip() and not line.startswith("#")),
|
||||
"",
|
||||
@@ -45,7 +74,7 @@ def playback_playlist(src: str) -> str:
|
||||
if looks_like_segment(variant_rel):
|
||||
return _rewrite_playlist(text, src)
|
||||
variant_url = urljoin(src, variant_rel)
|
||||
return _rewrite_playlist(http.read_text(variant_url, headers=CDN_HEADERS), variant_url)
|
||||
return _rewrite_playlist(http.read_text(variant_url, headers=_cdn_headers()), variant_url)
|
||||
|
||||
|
||||
def decode_key(value: str) -> bytes:
|
||||
@@ -122,8 +151,11 @@ def apply_hls_key(text: str, source_dir: Path, key: str) -> str:
|
||||
return "\n".join(result) + "\n"
|
||||
|
||||
|
||||
def _fetch_episodes(drama_id: str) -> list[dict]:
|
||||
detail = api.drama_detail(drama_id)
|
||||
def load_for_download(drama_id: str, *, allow_paid: bool, account_vip: bool) -> list[dict]:
|
||||
drama_id = str(drama_id or "").strip()
|
||||
if not drama_id:
|
||||
raise RuntimeError("没有这部剧")
|
||||
detail = api.drama_detail(drama_id, identity=_viewer_identity())
|
||||
rows = detail.get("episodes") if isinstance(detail, dict) else None
|
||||
if not isinstance(rows, list) or not rows:
|
||||
raise RuntimeError("没有读到分集")
|
||||
@@ -131,36 +163,89 @@ def _fetch_episodes(drama_id: str) -> list[dict]:
|
||||
for item in rows:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
seq = _int(item.get("seq"))
|
||||
if seq <= 0:
|
||||
if not keep_episode(item, allow_paid=allow_paid, account_vip=account_vip):
|
||||
continue
|
||||
kind = str(item.get("type") or "free").strip() or "free"
|
||||
skeleton.append({
|
||||
"cid": seq,
|
||||
"idx": seq,
|
||||
"title": str(item.get("name") or f"第{seq}集").strip() or f"第{seq}集",
|
||||
"src": "",
|
||||
"type": kind,
|
||||
"price": _int(item.get("price")),
|
||||
"is_buy": item.get("is_buy") is True or item.get("is_buy") is None,
|
||||
"cover": str(item.get("cover") or ""),
|
||||
"duration": _int(item.get("duration")),
|
||||
"methods": item.get("methods") if isinstance(item.get("methods"), list) else [],
|
||||
"is_preview": False,
|
||||
"preview_seconds": 0,
|
||||
"preview_m3u8": "",
|
||||
"hls_key": "",
|
||||
"src_kind": "",
|
||||
"locked_reason": "",
|
||||
})
|
||||
row = _skeleton_row(item, account_vip=account_vip)
|
||||
if row:
|
||||
skeleton.append(row)
|
||||
return _play_rows(drama_id, skeleton, allow_buy=allow_paid)
|
||||
|
||||
|
||||
def _viewer_identity() -> str:
|
||||
return "account" if client.account_token else "guest"
|
||||
|
||||
|
||||
def _menu_rows(rows: list) -> list[dict]:
|
||||
from server.services.huangdou.login import account_is_vip
|
||||
|
||||
vip = account_is_vip()
|
||||
skeleton = []
|
||||
for item in rows:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
row = _skeleton_row(item, account_vip=vip)
|
||||
if row:
|
||||
skeleton.append(row)
|
||||
return skeleton
|
||||
|
||||
|
||||
def _skeleton_row(item: dict, *, account_vip: bool) -> dict | None:
|
||||
seq = _int(item.get("seq"))
|
||||
if seq <= 0:
|
||||
return None
|
||||
kind = str(item.get("type") or "free").strip() or "free"
|
||||
playable, reason, points = episode_note(item, account_vip=account_vip)
|
||||
return {
|
||||
"cid": seq,
|
||||
"idx": seq,
|
||||
"title": str(item.get("name") or f"第{seq}集").strip() or f"第{seq}集",
|
||||
"src": "",
|
||||
"type": kind,
|
||||
"price": price_of(item),
|
||||
"points": points,
|
||||
"playable": playable,
|
||||
"needs_coin": points > 0,
|
||||
"is_buy": item.get("is_buy"),
|
||||
"cover": str(item.get("cover") or ""),
|
||||
"duration": _int(item.get("duration")),
|
||||
"methods": item.get("methods") if isinstance(item.get("methods"), list) else [],
|
||||
"is_preview": False,
|
||||
"preview_seconds": 0,
|
||||
"preview_m3u8": "",
|
||||
"hls_key": "",
|
||||
"src_kind": "",
|
||||
"locked_reason": reason,
|
||||
}
|
||||
|
||||
|
||||
def _play_rows(drama_id: str, skeleton: list[dict], *, allow_buy: bool) -> list[dict]:
|
||||
if not skeleton:
|
||||
raise RuntimeError("没有读到分集")
|
||||
return []
|
||||
|
||||
def play_one(row: dict) -> dict:
|
||||
if row.get("needs_coin") and not already_bought(row):
|
||||
if not allow_buy or not client.account_token:
|
||||
row["src"] = ""
|
||||
row["playable"] = False
|
||||
row["locked_reason"] = public_lock_reason(row, row.get("locked_reason") or "")
|
||||
return row
|
||||
try:
|
||||
api.drama_buy(drama_id, int(row["cid"]), buy_method(row))
|
||||
except RuntimeError as exc:
|
||||
row["src"] = ""
|
||||
row["playable"] = False
|
||||
row["locked_reason"] = public_lock_reason(row, str(exc))
|
||||
return row
|
||||
row["is_buy"] = True
|
||||
row["needs_coin"] = False
|
||||
row["points"] = 0
|
||||
identity = "account" if _uses_account(row) else "guest"
|
||||
try:
|
||||
played = api.drama_play(drama_id, int(row["cid"]))
|
||||
played = api.drama_play(drama_id, int(row["cid"]), identity=identity)
|
||||
except RuntimeError as exc:
|
||||
row["locked_reason"] = str(exc) or "这一集暂时不能下载"
|
||||
row["src"] = ""
|
||||
row["playable"] = False
|
||||
row["locked_reason"] = public_lock_reason(row, str(exc))
|
||||
return row
|
||||
preview = str(played.get("preview_m3u8") or "").strip()
|
||||
row["hls_key"] = str(played.get("hls_key") or "").strip()
|
||||
@@ -171,10 +256,13 @@ def _fetch_episodes(drama_id: str) -> list[dict]:
|
||||
row["src"] = src
|
||||
row["src_kind"] = kind
|
||||
if src:
|
||||
row["playable"] = True
|
||||
if kind == "file" or row["is_preview"]:
|
||||
row["is_preview"] = True
|
||||
row["locked_reason"] = "当前账号只能下到试看"
|
||||
row["locked_reason"] = ""
|
||||
row["points"] = 0
|
||||
else:
|
||||
row["playable"] = False
|
||||
row["locked_reason"] = _lock_reason(row)
|
||||
return row
|
||||
|
||||
@@ -190,41 +278,66 @@ def _fetch_episodes(drama_id: str) -> list[dict]:
|
||||
return updated
|
||||
|
||||
|
||||
def _uses_account(row: dict) -> bool:
|
||||
if not client.account_token:
|
||||
return False
|
||||
kind = episode_kind(row)
|
||||
if kind in VIP_TYPES:
|
||||
return True
|
||||
if kind in PAID_TYPES or _int(row.get("price")) > 0:
|
||||
return bool(already_bought(row) or row.get("needs_coin"))
|
||||
return False
|
||||
|
||||
|
||||
def _lock_reason(row: dict) -> str:
|
||||
kind = str(row.get("type") or "free")
|
||||
if kind in {"vip", "VIP"}:
|
||||
if _int(row.get("points") or row.get("price")) > 0 and not already_bought(row):
|
||||
return public_lock_reason(row, "")
|
||||
kind = episode_kind(row)
|
||||
if kind in VIP_TYPES:
|
||||
return "需要 VIP"
|
||||
if kind in {"coin", "points", "point", "pay"}:
|
||||
return "需要积分或付费"
|
||||
if not row.get("is_buy"):
|
||||
return "尚未购买"
|
||||
return "没有可下载的地址"
|
||||
return "没有可播放的地址"
|
||||
|
||||
|
||||
def _merge(cached: list[dict], fresh: list[dict]) -> tuple[list[dict], bool]:
|
||||
by_cid: dict[int, dict] = {}
|
||||
for source in (cached, fresh):
|
||||
for ep in source:
|
||||
cid = _int(ep.get("cid"))
|
||||
if cid <= 0:
|
||||
continue
|
||||
current = by_cid.get(cid)
|
||||
if current is None:
|
||||
by_cid[cid] = dict(ep)
|
||||
by_cid[cid]["cid"] = cid
|
||||
by_cid[cid]["idx"] = _int(ep.get("idx") or cid)
|
||||
continue
|
||||
for key, value in ep.items():
|
||||
if key == "src":
|
||||
if value:
|
||||
current["src"] = value
|
||||
continue
|
||||
if value not in (None, "", [], {}):
|
||||
current[key] = value
|
||||
merged = sorted(by_cid.values(), key=lambda item: (int(item.get("idx") or 0), int(item["cid"])))
|
||||
filled_fresh = sum(1 for ep in fresh if ep.get("src"))
|
||||
filled_cached = sum(1 for ep in cached if ep.get("src"))
|
||||
return merged, filled_fresh >= filled_cached and filled_fresh > 0
|
||||
def _cached_row(drama_id: str, cid: int, *, refresh: bool = False) -> dict | None:
|
||||
rows = load_episodes(drama_id) if refresh else _read_cache(drama_id)
|
||||
for ep in rows:
|
||||
if int(ep.get("cid") or 0) == int(cid):
|
||||
return ep
|
||||
return None
|
||||
|
||||
|
||||
def _keep_sources(cached: list[dict], fresh: list[dict]) -> list[dict]:
|
||||
previous = {int(ep.get("cid") or 0): ep for ep in cached if int(ep.get("cid") or 0) > 0}
|
||||
merged = []
|
||||
for row in fresh:
|
||||
old = previous.get(int(row["cid"]))
|
||||
if old and old.get("is_preview"):
|
||||
row["is_preview"] = True
|
||||
if row.get("playable") and old and old.get("src") and not old.get("is_preview"):
|
||||
row["src"] = old.get("src") or ""
|
||||
row["src_kind"] = old.get("src_kind") or ""
|
||||
row["hls_key"] = old.get("hls_key") or ""
|
||||
row["preview_seconds"] = old.get("preview_seconds") or 0
|
||||
row["preview_m3u8"] = old.get("preview_m3u8") or ""
|
||||
merged.append(row)
|
||||
return merged
|
||||
|
||||
|
||||
def _store_row(drama_id: str, row: dict) -> None:
|
||||
with _cache_lock:
|
||||
rows = _read_cache(drama_id)
|
||||
cid = int(row.get("cid") or 0)
|
||||
replaced = False
|
||||
updated = []
|
||||
for item in rows:
|
||||
if int(item.get("cid") or 0) == cid:
|
||||
updated.append(row)
|
||||
replaced = True
|
||||
else:
|
||||
updated.append(item)
|
||||
if not replaced:
|
||||
updated.append(row)
|
||||
_write_cache(drama_id, updated)
|
||||
|
||||
|
||||
def _read_cache(drama_id: str) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user