Files
to_session/tg接口/数据库匹配钱包.py
Administrator a0720d80dc fefdwef
2025-11-12 12:54:37 +08:00

86 lines
2.6 KiB
Python

from models.tg_models import TelegramAccount
from models.tg_phone_devices import TgPhoneDevices
from models.wallet_okx import WalletAddressOkx
if __name__ == '__main__':
project_name = "alliance"
# 批量查询已登录 Telegram 的账户
logged_in_accounts = (
TelegramAccount
.select()
.where(TelegramAccount.is_logged_in_telegram == 1)
)
# 提取所有已登录账户的 ld_name
all_ld_names = [account.ld_name for account in logged_in_accounts]
# 查询session表的电话号码
tg_infos = TgPhoneDevices.select().where(
TgPhoneDevices.is_valid_session == 1
)
for tg_info in tg_infos:
if tg_info.phone not in all_ld_names:
all_ld_names.append(tg_info.phone)
# 批量查询已存在于 caps 项目中的 ld_name
existing_caps_ld_names = (
WalletAddressOkx
.select(WalletAddressOkx.ld_name)
.where(
WalletAddressOkx.project_name == project_name,
WalletAddressOkx.ld_name.in_(all_ld_names)
)
.tuples()
)
existing_caps_ld_names = set([name for (name,) in existing_caps_ld_names])
# 批量查询可用的 Okx 信息
available_okx_infos = (
WalletAddressOkx
.select()
.where(
WalletAddressOkx.tg_id == 0,
WalletAddressOkx.server_ip.is_null(),
WalletAddressOkx.ld_name.is_null(),
WalletAddressOkx.project_name.is_null()
)
)
available_okx_infos = list(available_okx_infos)
update_data = []
for account in logged_in_accounts:
if account.ld_name not in existing_caps_ld_names:
if available_okx_infos:
okx_info = available_okx_infos.pop(0)
update_data.append({
'id': okx_info.id,
'tg_id': account.id,
'server_ip': account.server_id,
'ld_name': account.ld_name,
'project_name': project_name
})
print(account)
# 将字典列表转换为模型实例列表
model_instances = []
for data in update_data:
instance = WalletAddressOkx(**data)
model_instances.append(instance)
# 批量更新
if model_instances:
with WalletAddressOkx._meta.database.atomic():
WalletAddressOkx.bulk_update(
model_instances,
fields=[
WalletAddressOkx.tg_id,
WalletAddressOkx.server_ip,
WalletAddressOkx.ld_name,
WalletAddressOkx.project_name
]
)