fix: 黄豆按站点方式保持登录并自动切换播放线路
This commit is contained in:
@@ -53,12 +53,13 @@ def episode_src(drama_id: str, cid: int) -> dict:
|
||||
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
|
||||
stale = not row.get("lines_ready")
|
||||
if (row.get("playable") and stale) or row.get("locked_reason") == "登录已失效":
|
||||
played = _play_rows(drama_id, [dict(row)], allow_buy=False)
|
||||
updated = played[0] if played else row
|
||||
_store_row(drama_id, updated)
|
||||
return updated
|
||||
return row
|
||||
|
||||
|
||||
def playback_playlist(src: str) -> str:
|
||||
@@ -102,86 +103,97 @@ def store_fallback_key(source_dir: Path, key: str) -> None:
|
||||
(source_dir / "hls.key").write_bytes(raw)
|
||||
|
||||
|
||||
def line_options(played: dict) -> list[dict]:
|
||||
found = []
|
||||
seen: set[str] = set()
|
||||
def _preview_url(url: str) -> bool:
|
||||
path = (url or "").split("?", 1)[0].lower()
|
||||
return path.endswith("preview.mp4") or "/preview." in path
|
||||
|
||||
|
||||
def _line_urls(played: dict, *, allow_preview: bool) -> list[str]:
|
||||
urls: list[str] = []
|
||||
|
||||
def add(value) -> None:
|
||||
url = str(value or "").strip()
|
||||
if not url or url in urls:
|
||||
return
|
||||
if not allow_preview and _preview_url(url):
|
||||
return
|
||||
urls.append(url)
|
||||
|
||||
lines = played.get("lines") if isinstance(played.get("lines"), list) else []
|
||||
for line in lines:
|
||||
if not isinstance(line, dict):
|
||||
continue
|
||||
name = str(line.get("name") or "").strip()
|
||||
url = str(line.get("url") or "").strip()
|
||||
if not name or not url or name in seen:
|
||||
continue
|
||||
seen.add(name)
|
||||
found.append({"name": name, "label": name})
|
||||
return found
|
||||
if isinstance(line, dict):
|
||||
add(line.get("url"))
|
||||
add(played.get("m3u8"))
|
||||
if allow_preview:
|
||||
add(played.get("preview_m3u8"))
|
||||
return urls
|
||||
|
||||
|
||||
def available_lines(host: str = "") -> list[dict]:
|
||||
if host:
|
||||
client.set_host(host)
|
||||
rows = _guest_rows(lambda: api.drama_list(page=1, page_size=3))
|
||||
last_error = "没有读到线路"
|
||||
for item in rows:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
drama_id = str(item.get("id") or "").strip()
|
||||
if not drama_id:
|
||||
continue
|
||||
try:
|
||||
played = _guest_rows(lambda current=drama_id: api.drama_play(current, 1))
|
||||
except RuntimeError as exc:
|
||||
last_error = str(exc) or last_error
|
||||
continue
|
||||
if not isinstance(played, dict):
|
||||
continue
|
||||
lines = line_options(played)
|
||||
if lines:
|
||||
return lines
|
||||
raise RuntimeError(last_error)
|
||||
|
||||
|
||||
def _guest_rows(load):
|
||||
def _open_src(url: str) -> tuple[str, str] | None:
|
||||
if is_direct_media(url):
|
||||
return None if _preview_url(url) else (url, "file")
|
||||
try:
|
||||
if not client.token:
|
||||
api.login_device()
|
||||
return load()
|
||||
except TokenExpired:
|
||||
api.login_device()
|
||||
return load()
|
||||
text = http.read_text(url, headers=_cdn_headers())
|
||||
except Exception:
|
||||
return None
|
||||
if "#EXTM3U" not in text.upper():
|
||||
return None
|
||||
return url, "hls"
|
||||
|
||||
|
||||
def _choose_src(played: dict) -> tuple[str, str]:
|
||||
selected = (client.play_line or "").strip()
|
||||
lines = played.get("lines") if isinstance(played.get("lines"), list) else []
|
||||
named: list[tuple[str, str]] = []
|
||||
for line in lines:
|
||||
if not isinstance(line, dict):
|
||||
continue
|
||||
name = str(line.get("name") or "").strip()
|
||||
url = str(line.get("url") or "").strip()
|
||||
if url:
|
||||
named.append((name, url))
|
||||
ordered = []
|
||||
if selected:
|
||||
ordered.extend(url for name, url in named if name == selected)
|
||||
ordered.extend(url for _name, url in named)
|
||||
m3u8 = str(played.get("m3u8") or "").strip()
|
||||
if m3u8:
|
||||
ordered.append(m3u8)
|
||||
preview = str(played.get("preview_m3u8") or "").strip()
|
||||
if preview:
|
||||
ordered.append(preview)
|
||||
for url in ordered:
|
||||
if url and not is_direct_media(url):
|
||||
urls = _line_urls(played, allow_preview=False) or _line_urls(played, allow_preview=True)
|
||||
for url in urls:
|
||||
if not is_direct_media(url):
|
||||
return url, "hls"
|
||||
for url in ordered:
|
||||
if is_direct_media(url):
|
||||
for url in urls:
|
||||
if is_direct_media(url) and not _preview_url(url):
|
||||
return url, "file"
|
||||
return "", ""
|
||||
|
||||
|
||||
def _ordered_line_urls(played: dict, src: str) -> list[str]:
|
||||
urls = _line_urls(played, allow_preview=False)
|
||||
chosen = (src or "").strip()
|
||||
if chosen and not _preview_url(chosen):
|
||||
urls = [chosen, *[url for url in urls if url != chosen]]
|
||||
return urls
|
||||
|
||||
|
||||
def pick_line(row: dict, line: int) -> str:
|
||||
urls: list[str] = []
|
||||
for item in row.get("line_urls") or []:
|
||||
text = str(item or "").strip()
|
||||
if text and text not in urls and not _preview_url(text):
|
||||
urls.append(text)
|
||||
src = str(row.get("src") or "").strip()
|
||||
if src and not _preview_url(src) and src not in urls:
|
||||
urls.insert(0, src)
|
||||
if line < 0:
|
||||
line = 0
|
||||
if not urls:
|
||||
return src if line == 0 else ""
|
||||
if line >= len(urls):
|
||||
return ""
|
||||
return urls[line]
|
||||
|
||||
|
||||
def _resolve_src(played: dict, *, allow_preview: bool) -> tuple[str, str]:
|
||||
for url in _line_urls(played, allow_preview=False):
|
||||
opened = _open_src(url)
|
||||
if opened:
|
||||
return opened
|
||||
if not allow_preview:
|
||||
return "", ""
|
||||
for url in _line_urls(played, allow_preview=True):
|
||||
if _preview_url(url) and is_direct_media(url):
|
||||
return url, "file"
|
||||
opened = _open_src(url)
|
||||
if opened:
|
||||
return opened
|
||||
return "", ""
|
||||
|
||||
|
||||
def apply_hls_key(text: str, source_dir: Path, key: str) -> str:
|
||||
secret = (key or "").strip()
|
||||
if not secret:
|
||||
@@ -265,6 +277,8 @@ def _skeleton_row(item: dict, *, account_vip: bool) -> dict | None:
|
||||
"duration": _int(item.get("duration")),
|
||||
"methods": item.get("methods") if isinstance(item.get("methods"), list) else [],
|
||||
"is_preview": False,
|
||||
"line_urls": [],
|
||||
"lines_ready": False,
|
||||
"preview_seconds": 0,
|
||||
"preview_m3u8": "",
|
||||
"hls_key": "",
|
||||
@@ -282,6 +296,7 @@ def _play_rows(drama_id: str, skeleton: list[dict], *, allow_buy: bool) -> list[
|
||||
if not allow_buy or not client.account_token:
|
||||
row["src"] = ""
|
||||
row["playable"] = False
|
||||
row["lines_ready"] = True
|
||||
row["locked_reason"] = public_lock_reason(row, row.get("locked_reason") or "")
|
||||
return row
|
||||
try:
|
||||
@@ -289,36 +304,47 @@ def _play_rows(drama_id: str, skeleton: list[dict], *, allow_buy: bool) -> list[
|
||||
except RuntimeError as exc:
|
||||
row["src"] = ""
|
||||
row["playable"] = False
|
||||
row["lines_ready"] = True
|
||||
row["locked_reason"] = public_lock_reason(row, str(exc))
|
||||
return row
|
||||
row["is_buy"] = True
|
||||
row["needs_coin"] = False
|
||||
row["points"] = 0
|
||||
vip_episode = episode_kind(row) in VIP_TYPES
|
||||
identity = "account" if _uses_account(row) else "guest"
|
||||
try:
|
||||
played = api.drama_play(drama_id, int(row["cid"]), identity=identity)
|
||||
except TokenExpired:
|
||||
row["src"] = ""
|
||||
row["playable"] = False
|
||||
row["is_preview"] = False
|
||||
row["lines_ready"] = False
|
||||
row["locked_reason"] = "登录已失效"
|
||||
return row
|
||||
except RuntimeError as exc:
|
||||
row["src"] = ""
|
||||
row["playable"] = False
|
||||
row["lines_ready"] = True
|
||||
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()
|
||||
row["is_preview"] = played.get("is_preview") is True
|
||||
row["preview_seconds"] = _int(played.get("preview_seconds"))
|
||||
row["preview_m3u8"] = preview
|
||||
src, kind = _choose_src(played)
|
||||
src, kind = _resolve_src(played, allow_preview=not vip_episode)
|
||||
row["src"] = src
|
||||
row["src_kind"] = kind
|
||||
row["line_urls"] = _ordered_line_urls(played, src)
|
||||
row["lines_ready"] = True
|
||||
if src:
|
||||
row["playable"] = True
|
||||
if kind == "file" or row["is_preview"]:
|
||||
row["is_preview"] = True
|
||||
row["is_preview"] = (not vip_episode) and (kind == "file" and _preview_url(src) or played.get("is_preview") is True and _preview_url(src))
|
||||
row["locked_reason"] = ""
|
||||
row["points"] = 0
|
||||
else:
|
||||
row["playable"] = False
|
||||
row["locked_reason"] = _lock_reason(row)
|
||||
row["is_preview"] = False
|
||||
row["locked_reason"] = _vip_lock_reason(row) if vip_episode else _lock_reason(row)
|
||||
return row
|
||||
|
||||
updated = []
|
||||
@@ -333,6 +359,20 @@ def _play_rows(drama_id: str, skeleton: list[dict], *, allow_buy: bool) -> list[
|
||||
return updated
|
||||
|
||||
|
||||
def _vip_lock_reason(row: dict) -> str:
|
||||
if not client.account_token:
|
||||
return "需要 VIP"
|
||||
try:
|
||||
api.user_info(identity="account")
|
||||
except TokenExpired:
|
||||
client.account_token = ""
|
||||
client.save()
|
||||
return "登录已失效"
|
||||
except RuntimeError:
|
||||
return "这一集暂时不能播放"
|
||||
return "这一集暂时不能播放"
|
||||
|
||||
|
||||
def _uses_account(row: dict) -> bool:
|
||||
if not client.account_token:
|
||||
return False
|
||||
@@ -366,12 +406,14 @@ def _keep_sources(cached: list[dict], fresh: list[dict]) -> list[dict]:
|
||||
merged = []
|
||||
for row in fresh:
|
||||
old = previous.get(int(row["cid"]))
|
||||
if old and old.get("is_preview"):
|
||||
if old and old.get("is_preview") and episode_kind(row) not in VIP_TYPES:
|
||||
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["line_urls"] = old.get("line_urls") or []
|
||||
row["lines_ready"] = bool(old.get("lines_ready"))
|
||||
row["preview_seconds"] = old.get("preview_seconds") or 0
|
||||
row["preview_m3u8"] = old.get("preview_m3u8") or ""
|
||||
merged.append(row)
|
||||
|
||||
Reference in New Issue
Block a user