feat: DramaBox 登录凭据可导出为 JSON 文件
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "videosdowntools",
|
||||
"private": true,
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.2",
|
||||
"scripts": {
|
||||
"tauri": "tauri"
|
||||
},
|
||||
|
||||
@@ -15,7 +15,7 @@ import httpx
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
API = "https://git.asio.asia/api/v1/repos/nixevol/RaptDramaDump"
|
||||
ASSET = ROOT / "dist" / "VideosDownTools-windows-x64.zip"
|
||||
NOTES = "打开程序时会检查新版本,确认后自动下载并重新打开。\n应用内播放会带上单独的音轨。\n黄豆账号登录后会自动签到。"
|
||||
NOTES = "DramaBox 登录凭据可以导出成一个 JSON 文件,在另一台电脑导入后继续使用。"
|
||||
PART_SIZE = 40 * 1024 * 1024
|
||||
|
||||
|
||||
|
||||
@@ -92,6 +92,23 @@ def login_adb(body: AdbLoginBody):
|
||||
return call(lambda: db_login.import_adb(body.serial, body.language))
|
||||
|
||||
|
||||
@router.get("/login/credential")
|
||||
def export_credential():
|
||||
return call(db_login.export_credential)
|
||||
|
||||
|
||||
class CredentialBody(BaseModel):
|
||||
kind: str = ""
|
||||
version: int = 0
|
||||
app: dict = {}
|
||||
language: str = ""
|
||||
|
||||
|
||||
@router.post("/login/credential")
|
||||
def import_credential(body: CredentialBody):
|
||||
return call(lambda: db_login.import_credential(body.model_dump(), body.language))
|
||||
|
||||
|
||||
@router.post("/resume")
|
||||
def resume():
|
||||
return call(db_login.resume)
|
||||
|
||||
@@ -59,6 +59,114 @@ def import_adb(serial: str, language: str = "") -> dict:
|
||||
return {"token": client.token, "profile": client.profile}
|
||||
|
||||
|
||||
_CREDENTIAL_KIND = "dramabox-login"
|
||||
_CREDENTIAL_VERSION = 1
|
||||
_CREDENTIAL_FIELDS = (
|
||||
"token",
|
||||
"device_id",
|
||||
"android_id",
|
||||
"user_id",
|
||||
"name",
|
||||
"vip",
|
||||
"cid",
|
||||
"nchid",
|
||||
"mchid",
|
||||
"mbid",
|
||||
"language",
|
||||
"current_language",
|
||||
"country_code",
|
||||
"install_time",
|
||||
"instance_id",
|
||||
"device_score",
|
||||
"lat",
|
||||
"afid",
|
||||
"brand",
|
||||
"model",
|
||||
"manufacturer",
|
||||
"os_version",
|
||||
"build",
|
||||
"locale",
|
||||
"resolution",
|
||||
)
|
||||
|
||||
|
||||
def export_credential() -> dict:
|
||||
if not client.has_app():
|
||||
raise RuntimeError("还没有可导出的登录凭据")
|
||||
profile = client.profile if isinstance(client.profile, dict) else {}
|
||||
app = {}
|
||||
for key in _CREDENTIAL_FIELDS:
|
||||
if key == "name":
|
||||
value = str(profile.get("nickname") or profile.get("username") or client.app.get("name") or "")
|
||||
elif key == "vip":
|
||||
value = bool(profile.get("badge")) or client.app.get("vip") is True
|
||||
else:
|
||||
value = client.app.get(key)
|
||||
if value not in (None, ""):
|
||||
app[key] = value
|
||||
return {"kind": _CREDENTIAL_KIND, "version": _CREDENTIAL_VERSION, "app": app}
|
||||
|
||||
|
||||
def import_credential(payload: dict, language: str = "") -> dict:
|
||||
record = _credential_record(payload)
|
||||
try:
|
||||
locale = resolve(language or record.get("language") or client.language or DEFAULT_LANGUAGE)
|
||||
except RuntimeError:
|
||||
locale = resolve(DEFAULT_LANGUAGE)
|
||||
if not client.token:
|
||||
client.token = secrets.token_hex(16)
|
||||
client.language = locale["id"]
|
||||
client.app = {key: value for key, value in record.items() if key not in {"name", "vip"}}
|
||||
client.app["language"] = locale["id"]
|
||||
client.app["current_language"] = locale["id"]
|
||||
nickname = str(record.get("name") or record.get("user_id") or "DramaBox")
|
||||
client.profile = {
|
||||
"id": record.get("user_id") or "",
|
||||
"username": nickname,
|
||||
"nickname": nickname,
|
||||
"email": "",
|
||||
"phone": "",
|
||||
"badge": "VIP" if record.get("vip") else "",
|
||||
"vipday": 1 if record.get("vip") else 0,
|
||||
"score": 0,
|
||||
"watched": 0,
|
||||
"rewards": 0,
|
||||
"login_day": 0,
|
||||
"country": locale.get("country") or "",
|
||||
"language": locale.get("language") or "",
|
||||
"native": locale.get("native") or "",
|
||||
"locale": locale.get("id") or "",
|
||||
"loginType": "app",
|
||||
"has_login": "1",
|
||||
"app_login": "1",
|
||||
"avatarNumUrl": "",
|
||||
}
|
||||
client.save()
|
||||
from server.services.dramabox.app_api import ensure_guest
|
||||
ensure_guest()
|
||||
return {"token": client.token, "profile": client.profile}
|
||||
|
||||
|
||||
def _credential_record(payload: dict) -> dict:
|
||||
if not isinstance(payload, dict) or payload.get("kind") != _CREDENTIAL_KIND:
|
||||
raise RuntimeError("这不是 DramaBox 登录凭据文件")
|
||||
if int(payload.get("version") or 0) != _CREDENTIAL_VERSION:
|
||||
raise RuntimeError("这份凭据文件无法识别")
|
||||
app = payload.get("app")
|
||||
if not isinstance(app, dict):
|
||||
raise RuntimeError("凭据文件里没有登录信息")
|
||||
token = str(app.get("token") or "").strip()
|
||||
if token and not token.startswith("Bearer "):
|
||||
token = f"Bearer {token}"
|
||||
record = {key: app.get(key) for key in _CREDENTIAL_FIELDS}
|
||||
record["token"] = token
|
||||
record["vip"] = app.get("vip") is True or str(app.get("vip") or "").lower() in {"1", "true", "y", "yes"}
|
||||
missing = [name for name in ("token", "device_id", "android_id", "user_id") if not str(record.get(name) or "").strip()]
|
||||
if missing:
|
||||
raise RuntimeError("凭据文件不完整")
|
||||
return record
|
||||
|
||||
|
||||
def resume() -> dict:
|
||||
if not client.token or not client.profile:
|
||||
raise RuntimeError("还没有登录")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "VideosDownTools",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.2",
|
||||
"identifier": "com.videosdowntools.desktop",
|
||||
"build": {
|
||||
"beforeDevCommand": {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
from server.services.dramabox.login import _credential_record
|
||||
|
||||
|
||||
def test_credential_file_is_one_json_document() -> None:
|
||||
document = {
|
||||
"kind": "dramabox-login",
|
||||
"version": 1,
|
||||
"app": {
|
||||
"token": "raw-token",
|
||||
"device_id": "ad-1",
|
||||
"android_id": "android-1",
|
||||
"user_id": "88",
|
||||
"vip": "y",
|
||||
"name": "示例",
|
||||
},
|
||||
}
|
||||
record = _credential_record(document)
|
||||
assert record["token"] == "Bearer raw-token"
|
||||
assert record["device_id"] == "ad-1"
|
||||
assert record["android_id"] == "android-1"
|
||||
assert record["user_id"] == "88"
|
||||
assert record["vip"] is True
|
||||
assert record["name"] == "示例"
|
||||
|
||||
for broken in (
|
||||
{},
|
||||
{"kind": "other", "version": 1, "app": document["app"]},
|
||||
{"kind": "dramabox-login", "version": 2, "app": document["app"]},
|
||||
{"kind": "dramabox-login", "version": 1, "app": {"token": "Bearer x"}},
|
||||
):
|
||||
try:
|
||||
_credential_record(broken)
|
||||
except RuntimeError:
|
||||
continue
|
||||
raise AssertionError("不完整的凭据文件应被拒绝")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_credential_file_is_one_json_document()
|
||||
print("ok")
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "videosdowntools-ui",
|
||||
"private": true,
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.2",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
|
||||
@@ -153,6 +153,7 @@ const token = ref("");
|
||||
const profile = ref<Profile | null>(null);
|
||||
const dramaboxLoggedIn = computed(() => profile.value?.has_login === "1");
|
||||
const dramaboxDeviceReady = computed(() => devices.value.some((item) => item.state === "device"));
|
||||
const credentialFile = ref<HTMLInputElement | null>(null);
|
||||
const page = ref<"download" | "profile" | "progress" | "settings" | "play">("download");
|
||||
const playing = ref<{ id: string | number; title: string; image: string } | null>(null);
|
||||
const progressCount = ref(0);
|
||||
@@ -1094,6 +1095,77 @@ async function loginDramaboxAdb() {
|
||||
}
|
||||
}
|
||||
|
||||
function exportDramaboxCredential() {
|
||||
const target = token.value ? profileError : error;
|
||||
target.value = "";
|
||||
accountBusy.value = Boolean(token.value);
|
||||
busy.value = !token.value;
|
||||
fetch("/api/dramabox/login/credential")
|
||||
.then(async (response) => {
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
throw new Error(typeof payload.detail === "string" ? payload.detail : "导出失败");
|
||||
}
|
||||
const blob = new Blob([JSON.stringify(payload, null, 2)], { type: "application/json" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = "dramabox-login.json";
|
||||
link.click();
|
||||
URL.revokeObjectURL(url);
|
||||
})
|
||||
.catch((exc) => {
|
||||
target.value = exc instanceof Error ? exc.message : "导出失败";
|
||||
})
|
||||
.finally(() => {
|
||||
busy.value = false;
|
||||
accountBusy.value = false;
|
||||
});
|
||||
}
|
||||
|
||||
function pickDramaboxCredential() {
|
||||
credentialFile.value?.click();
|
||||
}
|
||||
|
||||
async function onDramaboxCredential(event: Event) {
|
||||
const input = event.target as HTMLInputElement;
|
||||
const file = input.files?.[0];
|
||||
input.value = "";
|
||||
if (!file) return;
|
||||
const inside = Boolean(token.value);
|
||||
error.value = "";
|
||||
profileError.value = "";
|
||||
if (inside) accountBusy.value = true;
|
||||
else busy.value = true;
|
||||
try {
|
||||
const text = await file.text();
|
||||
const document = JSON.parse(text);
|
||||
const response = await fetch("/api/dramabox/login/credential", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ ...document, language: dramaboxLang.value }),
|
||||
});
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
throw new Error(typeof payload.detail === "string" ? payload.detail : "导入失败");
|
||||
}
|
||||
token.value = payload.token;
|
||||
profile.value = payload.profile;
|
||||
writeSavedToken(payload.token);
|
||||
if (!inside) {
|
||||
page.value = "download";
|
||||
startLive();
|
||||
}
|
||||
} catch (exc) {
|
||||
const message = exc instanceof Error ? exc.message : "导入失败";
|
||||
if (inside) profileError.value = message;
|
||||
else error.value = message;
|
||||
} finally {
|
||||
busy.value = false;
|
||||
accountBusy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loginDramabox() {
|
||||
error.value = "";
|
||||
busy.value = true;
|
||||
@@ -1568,6 +1640,8 @@ onUnmounted(stopLive);
|
||||
<span v-if="platform === 'dramabox'" class="profile-title-actions">
|
||||
<n-tag size="small" :bordered="false" :type="dramaboxLoggedIn ? 'success' : 'default'">{{ dramaboxLoggedIn ? "已登录" : "访客" }}</n-tag>
|
||||
<n-button size="tiny" secondary :loading="accountBusy" :disabled="!dramaboxDeviceReady" @click="loginDramaboxAdb">从设备登录</n-button>
|
||||
<n-button size="tiny" secondary :loading="accountBusy" :disabled="!dramaboxLoggedIn" @click="exportDramaboxCredential">导出凭据</n-button>
|
||||
<n-button size="tiny" secondary :loading="accountBusy" @click="pickDramaboxCredential">导入凭据</n-button>
|
||||
</span>
|
||||
</div>
|
||||
<n-alert v-if="profileError" class="hint" type="error" :show-icon="false">{{ profileError }}</n-alert>
|
||||
@@ -1680,6 +1754,7 @@ onUnmounted(stopLive);
|
||||
<n-alert v-if="error" class="hint" type="error" :show-icon="false">{{ error }}</n-alert>
|
||||
<div class="login-actions">
|
||||
<n-button v-if="platform === 'dramabox'" attr-type="button" secondary :loading="busy" :disabled="!dramaboxDeviceReady" @click="loginDramaboxAdb">从设备登录</n-button>
|
||||
<n-button v-if="platform === 'dramabox'" attr-type="button" secondary :loading="busy" @click="pickDramaboxCredential">导入凭据</n-button>
|
||||
<n-button v-if="platform === 'huangdou'" attr-type="button" secondary :loading="busy" @click="loginHuangdou(true)">访客进入</n-button>
|
||||
<n-button type="primary" attr-type="submit" :loading="busy">{{ platform === "dramabox" ? "访客进入" : platform === "huangdou" ? "登录" : isolated ? "进入" : "登录" }}</n-button>
|
||||
</div>
|
||||
@@ -1688,6 +1763,7 @@ onUnmounted(stopLive);
|
||||
</div>
|
||||
</n-layout-content>
|
||||
</n-layout>
|
||||
<input ref="credentialFile" class="credential-file" type="file" accept="application/json,.json" @change="onDramaboxCredential" />
|
||||
<n-tooltip v-if="!profile" placement="left">
|
||||
<template #trigger>
|
||||
<n-button class="settings-button" secondary circle aria-label="设置" @click="openSettings">
|
||||
@@ -1992,6 +2068,7 @@ html { color-scheme: dark; }
|
||||
.card { width: 100%; max-width: 560px; }
|
||||
.login-actions { display: flex; justify-content: center; gap: 8px; margin-top: 16px; }
|
||||
.login-actions .n-button { min-width: 180px; }
|
||||
.credential-file { display: none; }
|
||||
.settings-button { position: fixed; right: 20px; bottom: 20px; z-index: 5; }
|
||||
.gear-icon { width: 18px; height: 18px; display: block; }
|
||||
.settings-modal-body { max-height: min(58vh, 520px); overflow: auto; padding-right: 4px; }
|
||||
|
||||
Reference in New Issue
Block a user