虎牙支持先导入账号密码再登录
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""虎牙基础业务服务。"""
|
||||
|
||||
import csv
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
@@ -93,6 +94,76 @@ def parse_huya_cookie_line(line: str) -> dict | None:
|
||||
}
|
||||
|
||||
|
||||
def split_huya_password_line(line: str) -> tuple[str, str, str] | None:
|
||||
"""拆分虎牙账号密码行,返回账号、密码、预置 Cookie。"""
|
||||
raw = (line or "").strip()
|
||||
if not raw or raw.startswith("#"):
|
||||
return None
|
||||
|
||||
if "----" in raw:
|
||||
parts = [part.strip() for part in raw.split("----")]
|
||||
elif "|" in raw:
|
||||
parts = [part.strip() for part in raw.split("|")]
|
||||
elif "\t" in raw:
|
||||
parts = [part.strip() for part in raw.split("\t")]
|
||||
elif "," in raw:
|
||||
parts = [part.strip() for part in next(csv.reader([raw]))]
|
||||
else:
|
||||
parts = raw.split()
|
||||
|
||||
if len(parts) < 2:
|
||||
return None
|
||||
|
||||
username = parts[0].strip()
|
||||
password = parts[1].strip()
|
||||
cookie = "----".join(part.strip() for part in parts[2:] if part.strip())
|
||||
if not username or not password:
|
||||
return None
|
||||
return username, password, cookie
|
||||
|
||||
|
||||
def import_huya_password_accounts(db: Session, text: str, tag: str = "") -> tuple[int, int]:
|
||||
"""导入虎牙账号密码,返回 (导入/更新数, 跳过数)。"""
|
||||
created_or_updated = 0
|
||||
skipped = 0
|
||||
tag = (tag or "").strip()
|
||||
|
||||
for line in (text or "").splitlines():
|
||||
parsed = split_huya_password_line(line)
|
||||
if not parsed:
|
||||
if line.strip():
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
username, password, cookie = parsed
|
||||
account = db.query(HuyaAccount).filter(HuyaAccount.username == username).first()
|
||||
if account is None:
|
||||
account = HuyaAccount(
|
||||
uid="",
|
||||
yyuid="",
|
||||
username=username,
|
||||
account_password=password,
|
||||
cookie=normalize_huya_cookie(cookie) if cookie else "",
|
||||
tag=tag,
|
||||
status="password_imported",
|
||||
)
|
||||
db.add(account)
|
||||
else:
|
||||
account.account_password = password
|
||||
if cookie:
|
||||
account.cookie = normalize_huya_cookie(cookie)
|
||||
if tag:
|
||||
account.tag = tag
|
||||
if not account.cookie:
|
||||
account.status = "password_imported"
|
||||
account.updated_at = datetime.now(timezone.utc)
|
||||
created_or_updated += 1
|
||||
|
||||
if created_or_updated:
|
||||
db.commit()
|
||||
return created_or_updated, skipped
|
||||
|
||||
|
||||
def _upsert_huya_account(db: Session, parsed: dict, tag: str = "", status: str | None = None) -> HuyaAccount:
|
||||
"""按 uid/yyuid 新增或更新虎牙账号。"""
|
||||
account = None
|
||||
@@ -125,6 +196,33 @@ def _upsert_huya_account(db: Session, parsed: dict, tag: str = "", status: str |
|
||||
return account
|
||||
|
||||
|
||||
def save_huya_login_cookie_to_account(
|
||||
db: Session,
|
||||
account: HuyaAccount,
|
||||
cookie: str,
|
||||
tag: str = "",
|
||||
username_hint: str = "",
|
||||
) -> HuyaAccount:
|
||||
"""把登录成功后的 Cookie 回填到指定虎牙账号。"""
|
||||
line = f"{username_hint}----{cookie}" if username_hint else cookie
|
||||
parsed = parse_huya_cookie_line(line)
|
||||
if not parsed:
|
||||
raise ValueError("登录成功但 Cookie 中没有识别到虎牙 uid")
|
||||
|
||||
account.uid = parsed["uid"] or account.uid
|
||||
account.yyuid = parsed["yyuid"] or account.yyuid
|
||||
account.username = parsed["username"] or account.username
|
||||
account.cookie = parsed["cookie"]
|
||||
account.game_phone = parsed["game_phone"] or account.game_phone
|
||||
if tag:
|
||||
account.tag = tag
|
||||
account.status = "login_success"
|
||||
account.updated_at = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
db.refresh(account)
|
||||
return account
|
||||
|
||||
|
||||
def upsert_huya_cookie(db: Session, cookie: str, tag: str = "", username_hint: str = "") -> HuyaAccount:
|
||||
"""保存单条登录得到的虎牙 Cookie。"""
|
||||
line = f"{username_hint}----{cookie}" if username_hint else cookie
|
||||
|
||||
Reference in New Issue
Block a user