Files

90 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import threading
from pathlib import Path
from server.services.downloads import DownloadManager, _plain_text
from server.services.huangdou import api
from server.services.huangdou.client import client
from server.services.huangdou.covers import covers
from server.services.huangdou.crypto import USER_AGENT
from server.services.huangdou import login as hd_login
from server.services.huangdou import playlist as hd_playlist
_pay_lock = threading.Lock()
ROOT = Path(__file__).resolve().parents[3]
def _load_episodes(_token, _device, drama_id: str, _locale: str = "") -> list[dict]:
return hd_playlist.load_for_download(
str(drama_id),
allow_paid=bool(getattr(downloads, "allow_paid", False)),
account_vip=bool(getattr(downloads, "account_vip", False)),
)
def enqueue_download(token: str, items: list[dict], download_points: bool) -> dict:
with _pay_lock:
downloads.allow_paid = bool(download_points and client.account_token)
downloads.account_vip = hd_login.account_is_vip()
try:
return downloads.enqueue(token, None, items)
finally:
downloads.allow_paid = False
downloads.account_vip = False
def _write_intro(folder: Path, drama_id: str, title: str, _token: str, _device) -> None:
name = title.strip()
desc = ""
pay_type = ""
free_episodes = ""
vip_episodes = ""
try:
detail = api.drama_detail(str(drama_id))
fetched = str(detail.get("name") or "").strip()
if fetched:
name = fetched
desc = _plain_text(detail.get("description") or "")
pay_type = _field(detail.get("pay_type"))
free_episodes = _field(detail.get("free_episodes"))
vip_episodes = _field(detail.get("vip_episodes"))
except Exception:
pass
lines = [
f"ID: {drama_id}",
f"名称: {name}",
f"简介: {desc}",
f"付费类型: {pay_type or '—'}",
f"免费集数: {free_episodes or '—'}",
f"VIP 集: {vip_episodes or '—'}",
"",
]
(folder / "简介.txt").write_text("\n".join(lines), encoding="utf-8")
def _read_cover(image: str):
digest = (image or "").rstrip("/").rsplit("/", 1)[-1].strip()
return covers.read(digest)
def _field(value) -> str:
if isinstance(value, list):
return "、".join(str(item) for item in value if item not in (None, ""))
text = str(value or "").strip()
return text
def _auth():
return client.token, None
downloads = DownloadManager(
state_dir=ROOT / "cache" / "huangdou" / "downloads",
default_directory=str((Path.cwd() / "download" / "huangdou").resolve()),
load_episodes=_load_episodes,
write_intro=_write_intro,
read_cover=_read_cover,
current_auth=_auth,
headers={"User-Agent": USER_AGENT, "Referer": "https://pmihlvee.cc/"},
)