Files
auto_cursor/import_emails.py
huangzhenpc cc2a3a34e3 x
2025-03-27 10:20:06 +08:00

61 lines
2.2 KiB
Python

import asyncio
import aiosqlite
from loguru import logger
from core.config import Config
async def import_emails(config: Config, file_path: str):
"""导入邮箱账号到数据库"""
DEFAULT_CLIENT_ID = "9e5f94bc-e8a4-4e73-b8be-63364c29d753"
async with aiosqlite.connect(config.database_config.path) as db:
# 创建表
await db.execute('''
CREATE TABLE IF NOT EXISTS email_accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
client_id TEXT NOT NULL,
refresh_token TEXT NOT NULL,
in_use BOOLEAN DEFAULT 0,
cursor_password TEXT,
cursor_cookie TEXT,
cursor_token TEXT,
sold BOOLEAN DEFAULT 0,
status TEXT DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# 读取文件并导入数据
count = 0
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
if line.strip():
try:
email, password, client_id, refresh_token = line.strip().split('----')
await db.execute('''
INSERT INTO email_accounts (
email, password, client_id, refresh_token, status
) VALUES (?, ?, ?, ?, 'pending')
''', (email, password, client_id, refresh_token))
count += 1
except aiosqlite.IntegrityError:
logger.warning(f"重复的邮箱: {email}")
except ValueError:
logger.error(f"无效的数据行: {line.strip()}")
await db.commit()
logger.success(f"成功导入 {count} 个邮箱账号")
async def main():
config = Config.from_yaml()
await import_emails(config, "email.txt")
if __name__ == "__main__":
asyncio.run(main())