from pathlib import Path from server.services.downloads import DownloadManager, _plain_text from server.services.dramabox import api from server.services.dramabox.client import USER_AGENT, client from server.services.dramabox.covers import covers from server.services.dramabox import playlist as db_playlist ROOT = Path(__file__).resolve().parents[3] 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: name = title.strip() desc = "" language = client.language country = "" tags = "" count = "" try: detail = api.book_detail(str(drama_id)) book = detail.get("book") if isinstance(detail, dict) else None if isinstance(book, dict): fetched = str(book.get("bookName") or "").strip() if fetched: name = fetched desc = _plain_text(book.get("introduction") or "") language = str(book.get("simpleLanguage") or language) tags = "、".join(str(item) for item in (book.get("typeTwoNames") or []) if item) count = str(book.get("chapterCount") or "") profile = client.profile or {} country = str(profile.get("country") or "") except Exception: pass lines = [ f"ID: {drama_id}", f"名称: {name}", f"简介: {desc}", f"地区: {country or '—'}", f"语言: {language or '—'}", f"分类: {tags or '—'}", f"集数: {count 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 _auth(): return client.token, None downloads = DownloadManager( state_dir=ROOT / "cache" / "dramabox" / "downloads", default_directory=str((Path.cwd() / "download" / "dramabox").resolve()), load_episodes=_load_episodes, write_intro=_write_intro, read_cover=_read_cover, current_auth=_auth, headers={"User-Agent": USER_AGENT, "Referer": "https://www.dramaboxapp.com/", "Origin": "https://www.dramaboxapp.com"}, )