清理虎牙登录旧数据依赖并统一动态环境
This commit is contained in:
@@ -19,6 +19,7 @@ import hashlib
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
from pathlib import Path
|
||||
|
||||
@@ -28,6 +29,7 @@ ROOT = Path(__file__).resolve().parent.parent.parent
|
||||
DATA_DIR = ROOT / "data"
|
||||
PRIMARY_PROFILE_DB = DATA_DIR / "huya_device_profiles.json"
|
||||
FALLBACK_PROFILE_DB = ROOT / "evidence" / "device_profiles.json"
|
||||
ALLOW_LEGACY_PROFILE_IMPORT = os.getenv("HUYA_ALLOW_LEGACY_PROFILE_IMPORT") == "1"
|
||||
|
||||
REAL_MODELS = [
|
||||
("xiaomi", "M2102J2SC", "M2102J2SC,30,11", (1080, 2120)),
|
||||
@@ -51,6 +53,19 @@ APP_VERSION = "13.4.22"
|
||||
SDK_VERSION = "1.0.80138"
|
||||
|
||||
|
||||
def canonical_account_key(account: str) -> str:
|
||||
"""Return one stable environment key for equivalent Huya account forms.
|
||||
|
||||
The WUP protocol keeps the ``hy_`` prefix for Huya IDs, but the device
|
||||
environment must not split ``300023887`` and ``hy_300023887`` into two
|
||||
records. Phone numbers and other usernames remain unchanged.
|
||||
"""
|
||||
value = str(account or "").strip()
|
||||
if re.fullmatch(r"hy_\d+", value):
|
||||
return value[3:]
|
||||
return value
|
||||
|
||||
|
||||
def mobile_user_agent(device_info: Mapping[str, object]) -> str:
|
||||
"""根据统一设备画像生成 App WebView UA。"""
|
||||
screen = str(device_info.get("screen") or "")
|
||||
@@ -107,8 +122,14 @@ def record_login(account: str, ok: bool, message: str = "") -> None:
|
||||
"""
|
||||
import time as _time
|
||||
|
||||
key = canonical_account_key(account)
|
||||
db = _load_db()
|
||||
rec = db.get(account)
|
||||
rec = db.get(key)
|
||||
if rec is None and key != account:
|
||||
rec = db.get(account)
|
||||
if rec is not None:
|
||||
db[key] = rec
|
||||
del db[account]
|
||||
if rec is None:
|
||||
return # 尚无环境的账号 (纯 Cookie 导入) 不生成记录
|
||||
now = int(_time.time())
|
||||
@@ -123,11 +144,11 @@ def _load_db() -> dict:
|
||||
return json.loads(PRIMARY_PROFILE_DB.read_text("utf-8"))
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"读取主设备画像库失败: {exc}")
|
||||
if FALLBACK_PROFILE_DB.exists():
|
||||
if ALLOW_LEGACY_PROFILE_IMPORT and FALLBACK_PROFILE_DB.exists():
|
||||
try:
|
||||
return json.loads(FALLBACK_PROFILE_DB.read_text("utf-8"))
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"读取备用设备画像库失败: {exc}")
|
||||
logger.debug(f"读取显式迁移画像库失败: {exc}")
|
||||
return {}
|
||||
|
||||
|
||||
@@ -166,14 +187,20 @@ def _enrich_profile(profile: dict) -> tuple[dict, bool]:
|
||||
|
||||
def get_profile(account: str, force_new: bool = False) -> dict:
|
||||
"""按账号获取或创建画像(幂等:同账号复用同一套, 自动补齐缺失一致性字段)。"""
|
||||
key = canonical_account_key(account)
|
||||
db = _load_db()
|
||||
if not force_new and account in db:
|
||||
enriched, changed = _enrich_profile(db[account])
|
||||
# Migrate the old prefixed key lazily without discarding its environment.
|
||||
legacy_key = str(account or "").strip()
|
||||
if key not in db and legacy_key != key and legacy_key in db:
|
||||
db[key] = db.pop(legacy_key)
|
||||
_save_db(db)
|
||||
if not force_new and key in db:
|
||||
enriched, changed = _enrich_profile(db[key])
|
||||
if changed:
|
||||
db[account] = enriched
|
||||
db[key] = enriched
|
||||
_save_db(db)
|
||||
return enriched
|
||||
p, _ = _enrich_profile(generate_profile())
|
||||
db[account] = p
|
||||
db[key] = p
|
||||
_save_db(db)
|
||||
return p
|
||||
|
||||
Reference in New Issue
Block a user