feat: 接入 DramaBox 并按语言地区切换清单
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import json
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
from server.services.dramabox.locales import DEFAULT_LANGUAGE, resolve
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
STATE_DIR = ROOT / "cache" / "dramabox"
|
||||
SESSION_PATH = STATE_DIR / "session.json"
|
||||
|
||||
SITE = "https://www.dramaboxapp.com"
|
||||
WEBFIC = "https://www.webfic.com"
|
||||
USER_AGENT = (
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
||||
"(KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
|
||||
)
|
||||
|
||||
|
||||
class DramaBoxClient:
|
||||
def __init__(self) -> None:
|
||||
self.token = ""
|
||||
self.language = DEFAULT_LANGUAGE
|
||||
self.build_id = ""
|
||||
self.profile: dict = {}
|
||||
self._load()
|
||||
|
||||
def _load(self) -> None:
|
||||
if not SESSION_PATH.is_file():
|
||||
return
|
||||
try:
|
||||
data = json.loads(SESSION_PATH.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return
|
||||
if not isinstance(data, dict):
|
||||
return
|
||||
self.token = str(data.get("token") or "")
|
||||
language = str(data.get("language") or "").strip()
|
||||
if language:
|
||||
try:
|
||||
self.language = resolve(language)["id"]
|
||||
except RuntimeError:
|
||||
self.language = DEFAULT_LANGUAGE
|
||||
self.build_id = str(data.get("build_id") or "")
|
||||
profile = data.get("profile")
|
||||
if isinstance(profile, dict):
|
||||
self.profile = profile
|
||||
|
||||
def save(self) -> None:
|
||||
STATE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
payload = {
|
||||
"token": self.token,
|
||||
"language": self.language,
|
||||
"build_id": self.build_id,
|
||||
"profile": self.profile,
|
||||
}
|
||||
temporary = SESSION_PATH.with_suffix(".json.tmp")
|
||||
temporary.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
||||
temporary.replace(SESSION_PATH)
|
||||
|
||||
def enter(self, language: str) -> dict:
|
||||
locale = resolve(language)
|
||||
self.language = locale["id"]
|
||||
if not self.token:
|
||||
self.token = secrets.token_hex(16)
|
||||
self.profile = {
|
||||
"id": locale["id"],
|
||||
"username": locale["id"],
|
||||
"nickname": locale["country"],
|
||||
"email": locale["language"],
|
||||
"phone": "",
|
||||
"badge": "",
|
||||
"vipday": 0,
|
||||
"score": 0,
|
||||
"watched": 0,
|
||||
"rewards": 0,
|
||||
"login_day": 0,
|
||||
"country": locale["country"],
|
||||
"language": locale["language"],
|
||||
"native": locale["native"],
|
||||
"locale": locale["id"],
|
||||
"avatarNumUrl": "",
|
||||
}
|
||||
self.save()
|
||||
return {"token": self.token, "profile": self.profile}
|
||||
|
||||
def clear(self) -> None:
|
||||
self.token = ""
|
||||
self.profile = {}
|
||||
self.save()
|
||||
|
||||
|
||||
client = DramaBoxClient()
|
||||
Reference in New Issue
Block a user