255 lines
7.4 KiB
Python
255 lines
7.4 KiB
Python
from server.services.huangdou.client import ApiError, TokenExpired, client
|
|
from server.services.huangdou.crypto import USER_AGENT
|
|
|
|
|
|
def device_info(identity: str = "guest") -> dict:
|
|
if identity == "account":
|
|
client._ensure_fingerprint()
|
|
info = client.device_info
|
|
else:
|
|
if not client.guest_device_id:
|
|
client.roll_guest_fingerprint()
|
|
info = client.guest_device_info
|
|
if isinstance(info, dict) and info:
|
|
return dict(info)
|
|
return {
|
|
"browserName": "Chrome",
|
|
"language": "zh-CN",
|
|
"userAgent": USER_AGENT,
|
|
"platform": "Win32",
|
|
}
|
|
|
|
|
|
def login_device(*, force_new: bool = False) -> dict:
|
|
data = client.request(
|
|
"/login/device",
|
|
{
|
|
"line_code": "china_1",
|
|
"channel_code": "",
|
|
"share_code": "",
|
|
"clipboard_text": "",
|
|
"force_new": "1" if force_new else "",
|
|
"device_info": device_info(),
|
|
},
|
|
auth=False,
|
|
)
|
|
if not isinstance(data, dict):
|
|
raise RuntimeError("设备登录没有返回数据")
|
|
token = str(data.get("token") or "").strip()
|
|
if not token:
|
|
raise RuntimeError("设备登录没有返回 token")
|
|
client.token = token
|
|
remember_login_user(data, account=False)
|
|
client._renew_blocked.discard("guest")
|
|
client.save()
|
|
return data
|
|
|
|
|
|
def login_username(username: str, password: str, channel_code: str = "") -> dict:
|
|
data = client.request(
|
|
"/login/username",
|
|
{
|
|
"line_code": "china_1",
|
|
"share_code": "",
|
|
"channel_code": channel_code,
|
|
"username": username.strip(),
|
|
"password": password,
|
|
"clipboard_text": "",
|
|
"device_info": device_info("account"),
|
|
},
|
|
auth=False,
|
|
identity="account",
|
|
)
|
|
if not isinstance(data, dict):
|
|
raise RuntimeError("账号登录没有返回数据")
|
|
token = str(data.get("token") or "").strip()
|
|
if not token:
|
|
raise RuntimeError("账号登录没有返回 token")
|
|
client.account_token = token
|
|
client.account_username = username.strip()
|
|
remember_login_user(data, account=True)
|
|
client._renew_blocked.discard("account")
|
|
client.save()
|
|
return data
|
|
|
|
|
|
def user_info(*, identity: str = "guest") -> dict:
|
|
data = client.request("/user/info", {}, auth=True, identity=identity)
|
|
if not isinstance(data, dict):
|
|
return {}
|
|
uid = str(data.get("user_id") or data.get("id") or "").strip()
|
|
if uid:
|
|
if identity == "account":
|
|
client.account_user_id = uid
|
|
else:
|
|
client.user_id = uid
|
|
client.save()
|
|
return data
|
|
|
|
|
|
def remember_login_user(data: dict, *, account: bool) -> None:
|
|
uid = ""
|
|
if isinstance(data, dict):
|
|
uid = str(data.get("user_id") or data.get("id") or "").strip()
|
|
for key in ("user", "userInfo", "user_info", "info"):
|
|
nested = data.get(key)
|
|
if isinstance(nested, dict) and not uid:
|
|
uid = str(nested.get("user_id") or nested.get("id") or "").strip()
|
|
if not uid:
|
|
return
|
|
if account:
|
|
client.account_user_id = uid
|
|
else:
|
|
client.user_id = uid
|
|
|
|
|
|
def system_info(*, identity: str = "") -> dict:
|
|
chosen = identity or ("account" if client.account_token else "guest")
|
|
data = client.request(
|
|
"/system/info",
|
|
{
|
|
"line_code": "china_1",
|
|
"channel_code": "",
|
|
"share_code": "",
|
|
"clipboard_text": "",
|
|
"device_info": device_info(chosen),
|
|
"package_name": "dfa03",
|
|
},
|
|
auth=True,
|
|
identity=chosen,
|
|
)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def remember_api_lines(info: dict | None = None) -> list[str]:
|
|
if info is None:
|
|
identities = ["account", "guest"] if client.account_token else ["guest"]
|
|
info = {}
|
|
for name in identities:
|
|
try:
|
|
info = system_info(identity=name)
|
|
break
|
|
except TokenExpired:
|
|
continue
|
|
except RuntimeError:
|
|
return list(client.api_lines)
|
|
else:
|
|
return list(client.api_lines)
|
|
if not isinstance(info, dict) or "api_lines" not in info:
|
|
return list(client.api_lines)
|
|
hosts = []
|
|
for item in info.get("api_lines") or []:
|
|
if not isinstance(item, str) or not item.startswith("https://"):
|
|
continue
|
|
host = item.rstrip("/")
|
|
if host not in hosts:
|
|
hosts.append(host)
|
|
client.api_lines = hosts
|
|
client.save()
|
|
return hosts
|
|
|
|
|
|
def nav_list() -> list[dict]:
|
|
data = client.request("/drama/navList", {"with_filters": "1"}, auth=True)
|
|
rows = _list_of(data)
|
|
return [item for item in rows if isinstance(item, dict)]
|
|
|
|
|
|
def nav_block(code: str, tab: str = "", page: int = 1) -> list[dict]:
|
|
data = client.request(
|
|
"/drama/navBlock",
|
|
{"code": str(code or ""), "tab": str(tab or ""), "page": str(page)},
|
|
auth=True,
|
|
)
|
|
rows = _list_of(data)
|
|
return [item for item in rows if isinstance(item, dict)]
|
|
|
|
|
|
def drama_list(
|
|
*,
|
|
page: int = 1,
|
|
page_size: int = 18,
|
|
keywords: str = "",
|
|
order: str = "",
|
|
cat_id: str = "",
|
|
tag_id: str = "",
|
|
source: str = "",
|
|
canvas: str = "",
|
|
pay_type: str = "",
|
|
update_status: str = "",
|
|
) -> list[dict]:
|
|
body = {"page": str(page), "page_size": str(page_size)}
|
|
if order:
|
|
body["order"] = order
|
|
if cat_id:
|
|
body["cat_id"] = cat_id
|
|
if tag_id:
|
|
body["tag_id"] = tag_id
|
|
if source:
|
|
body["source"] = source
|
|
if canvas:
|
|
body["canvas"] = canvas
|
|
if keywords:
|
|
body["keywords"] = keywords
|
|
if update_status:
|
|
body["update_status"] = update_status
|
|
if pay_type:
|
|
body["pay_type"] = pay_type
|
|
data = client.request("/drama/list", body, auth=True)
|
|
rows = _list_of(data)
|
|
return [item for item in rows if isinstance(item, dict)]
|
|
|
|
|
|
def limited_free(page: int = 1, size: int = 50) -> dict:
|
|
data = client.request("/drama/limitedFree", {"page": str(page), "size": str(size)}, auth=True)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def drama_detail(drama_id: str, *, identity: str = "guest") -> dict:
|
|
data = client.request("/drama/detail", {"id": str(drama_id)}, auth=True, identity=identity)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def drama_play(drama_id: str, seq: int, *, identity: str = "guest") -> dict:
|
|
data = client.request("/drama/play", {"id": str(drama_id), "seq": str(seq)}, auth=True, identity=identity)
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def drama_buy(drama_id: str, seq: int, method: str = "") -> dict:
|
|
body = {"id": str(drama_id), "seq": str(seq)}
|
|
if method:
|
|
body["method"] = method
|
|
data = client.request("/drama/doBuy", body, auth=True, identity="account")
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def _list_of(data) -> list:
|
|
if isinstance(data, list):
|
|
return data
|
|
if isinstance(data, dict):
|
|
rows = data.get("list")
|
|
if isinstance(rows, list):
|
|
return rows
|
|
items = data.get("items")
|
|
if isinstance(items, list):
|
|
return items
|
|
return []
|
|
|
|
|
|
__all__ = [
|
|
"ApiError",
|
|
"TokenExpired",
|
|
"client",
|
|
"drama_detail",
|
|
"drama_list",
|
|
"drama_play",
|
|
"limited_free",
|
|
"drama_buy",
|
|
"login_device",
|
|
"login_username",
|
|
"nav_block",
|
|
"nav_list",
|
|
"system_info",
|
|
"user_info",
|
|
]
|