fix(账号): 批量导入跳过已存在的重复账号
- 导入前按用户名(大小写不敏感)查库去重,同批文本内重复也跳过 - 返回消息区分格式跳过与重复跳过数量
This commit is contained in:
@@ -31,23 +31,36 @@ def cookie_account_ids_query(db: Session):
|
||||
).distinct()
|
||||
|
||||
|
||||
def parse_and_build_accounts(text: str, default_tag: str = "") -> tuple[list[Account], int]:
|
||||
def parse_and_build_accounts(
|
||||
db: Session,
|
||||
text: str,
|
||||
default_tag: str = "",
|
||||
) -> tuple[list[Account], int, int]:
|
||||
"""
|
||||
解析批量导入文本,构建 Account ORM 对象列表。
|
||||
解析批量导入文本,构建 Account ORM 对象列表,并过滤已存在的重复账号。
|
||||
|
||||
Args:
|
||||
db: 数据库会话,用于查询已有账号
|
||||
text: 批量导入文本,每行格式:用户名|密码|邮箱|邮箱密码|标签(可选)
|
||||
default_tag: 统一标签兜底。某行未单独写标签时使用该值,
|
||||
行内第5列标签优先。
|
||||
|
||||
Returns:
|
||||
(accounts, skipped_count)
|
||||
(accounts, skipped_count, duplicated_count)
|
||||
skipped_count 为格式非法的行数;duplicated_count 为库中已存在或同批重复被跳过的行数。
|
||||
"""
|
||||
from core.douyu.email_verifier import get_email_config_for_account
|
||||
|
||||
default_tag = (default_tag or "").strip()
|
||||
# 库中已有用户名(大小写不敏感),用于导入去重
|
||||
existing_usernames = {
|
||||
row[0].lower()
|
||||
for row in db.query(Account.username).filter(Account.username != "").all()
|
||||
}
|
||||
accounts = []
|
||||
skipped = 0
|
||||
duplicated = 0
|
||||
seen_in_batch: set[str] = set()
|
||||
for line in text.strip().split('\n'):
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
@@ -68,6 +81,12 @@ def parse_and_build_accounts(text: str, default_tag: str = "") -> tuple[list[Acc
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
username_key = username.lower()
|
||||
if username_key in existing_usernames or username_key in seen_in_batch:
|
||||
duplicated += 1
|
||||
continue
|
||||
seen_in_batch.add(username_key)
|
||||
|
||||
email_cfg = get_email_config_for_account(email)
|
||||
accounts.append(Account(
|
||||
username=username,
|
||||
@@ -79,4 +98,4 @@ def parse_and_build_accounts(text: str, default_tag: str = "") -> tuple[list[Acc
|
||||
email_imap_ssl=email_cfg.get('ssl', True),
|
||||
tag=tag,
|
||||
))
|
||||
return accounts, skipped
|
||||
return accounts, skipped, duplicated
|
||||
|
||||
Reference in New Issue
Block a user