兼容虎牙四段账号格式

支持虎牙号、密码、手机号、验证码链接导入,并补充美国等国际手机号格式处理。
This commit is contained in:
yml2213
2026-07-06 11:45:42 +08:00
parent 72e7947a39
commit 45dd7e5a74
6 changed files with 203 additions and 25 deletions
+48 -5
View File
@@ -2,7 +2,9 @@
import csv
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from urllib.parse import urlparse
from sqlalchemy.orm import Session
@@ -94,8 +96,25 @@ def parse_huya_cookie_line(line: str) -> dict | None:
}
def split_huya_password_line(line: str) -> tuple[str, str, str] | None:
"""拆分虎牙账号密码行,返回账号、密码、预置 Cookie。"""
@dataclass
class HuyaPasswordLine:
"""虎牙账号密码导入行。"""
username: str
password: str
cookie: str = ""
phone: str = ""
sms_url: str = ""
def _looks_like_url(value: str) -> bool:
"""判断文本是否像 URL。"""
parsed = urlparse(str(value or "").strip())
return parsed.scheme in {"http", "https"} and bool(parsed.netloc)
def split_huya_password_line(line: str) -> HuyaPasswordLine | None:
"""拆分虎牙账号密码行,兼容账号----密码----手机号----验证码链接。"""
raw = (line or "").strip()
if not raw or raw.startswith("#"):
return None
@@ -116,10 +135,29 @@ def split_huya_password_line(line: str) -> tuple[str, str, str] | None:
username = parts[0].strip()
password = parts[1].strip()
cookie = "----".join(part.strip() for part in parts[2:] if part.strip())
cookie = ""
phone = ""
sms_url = ""
extra = [part.strip() for part in parts[2:] if part.strip()]
if len(extra) >= 2 and _looks_like_url(extra[1]):
phone = extra[0]
sms_url = extra[1]
cookie = "----".join(extra[2:])
elif extra and _looks_like_url(extra[-1]) and len(extra) >= 2:
phone = extra[-2]
sms_url = extra[-1]
cookie = "----".join(extra[:-2])
elif extra:
cookie = "----".join(extra)
if not username or not password:
return None
return username, password, cookie
return HuyaPasswordLine(
username=username,
password=password,
cookie=cookie,
phone=phone,
sms_url=sms_url,
)
def import_huya_password_accounts(db: Session, text: str, tag: str = "") -> tuple[int, int]:
@@ -135,7 +173,9 @@ def import_huya_password_accounts(db: Session, text: str, tag: str = "") -> tupl
skipped += 1
continue
username, password, cookie = parsed
username = parsed.username
password = parsed.password
cookie = parsed.cookie
account = db.query(HuyaAccount).filter(HuyaAccount.username == username).first()
if account is None:
account = HuyaAccount(
@@ -144,6 +184,7 @@ def import_huya_password_accounts(db: Session, text: str, tag: str = "") -> tupl
username=username,
account_password=password,
cookie=normalize_huya_cookie(cookie) if cookie else "",
game_phone=parsed.phone,
tag=tag,
status="password_imported",
)
@@ -152,6 +193,8 @@ def import_huya_password_accounts(db: Session, text: str, tag: str = "") -> tupl
account.account_password = password
if cookie:
account.cookie = normalize_huya_cookie(cookie)
if parsed.phone:
account.game_phone = parsed.phone
if tag:
account.tag = tag
if not account.cookie: