feat: 改造配置系统,使用API获取配置,支持IMAP和临时邮箱,移除本地env依赖
Some checks failed
Remove old artifacts / remove-old-artifacts (push) Has been cancelled

This commit is contained in:
huangzhenpc
2025-02-11 12:48:54 +08:00
parent c6e50419b7
commit d28e2f35c8
3 changed files with 188 additions and 94 deletions

View File

@@ -10,7 +10,8 @@ from logger import logging
class AccountManager:
def __init__(self, api_base_url: str = None):
self.config = Config()
self.api_base_url = api_base_url or os.getenv("API_BASE_URL", "http://api.example.com")
self.api_base_url = api_base_url or "https://cursorapi.nosqli.com/admin"
self.api_token = os.getenv("API_TOKEN", "")
self.accounts_file = "accounts.json"
self._ensure_accounts_file()
@@ -62,31 +63,82 @@ class AccountManager:
"""
try:
# 构建API请求
endpoint = f"{self.api_base_url}/api/accounts"
endpoint = f"{self.api_base_url}/api.account/add"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.getenv('API_TOKEN', '')}"
"Authorization": f"Bearer {self.api_token}"
}
# 准备请求数据
data = {
"email": account_info["email"],
"password": account_info["password"],
"first_name": account_info["first_name"],
"last_name": account_info["last_name"],
"access_token": account_info["access_token"],
"refresh_token": account_info["refresh_token"],
"machine_id": account_info.get("machine_id", ""),
"user_agent": account_info.get("user_agent", ""),
"registration_time": datetime.fromisoformat(account_info["registration_time"]).strftime("%Y-%m-%d %H:%M:%S")
}
# 发送POST请求
response = requests.post(
endpoint,
json=account_info,
json=data,
headers=headers,
timeout=10
)
if response.status_code == 200:
response_data = response.json()
if response_data["code"] == 0:
logging.info(f"账号信息已同步到服务器: {account_info['email']}")
return True
elif response_data["code"] == 400:
logging.warning(f"账号已存在: {account_info['email']}")
return False
else:
logging.error(f"同步到服务器失败: {response.status_code} - {response.text}")
logging.error(f"同步到服务器失败: {response_data['msg']}")
return False
except Exception as e:
logging.error(f"同步账号信息到服务器失败: {str(e)}")
return False
def get_unused_account(self) -> Optional[Dict]:
"""
从服务器获取一个未使用的账号
Returns:
Optional[Dict]: 账号信息或None
"""
try:
endpoint = f"{self.api_base_url}/api.account/getUnused"
headers = {
"Authorization": f"Bearer {self.api_token}"
}
response = requests.get(
endpoint,
headers=headers,
timeout=10
)
response_data = response.json()
if response_data["code"] == 0:
logging.info("成功获取未使用账号")
return response_data["data"]
elif response_data["code"] == 404:
logging.warning("没有可用的未使用账号")
return None
else:
logging.error(f"获取未使用账号失败: {response_data['msg']}")
return None
except Exception as e:
logging.error(f"获取未使用账号失败: {str(e)}")
return None
def process_account(self, account_info: Dict) -> bool:
"""
处理账号信息:保存到本地并同步到服务器