Files
steamreg/EmailAPIClient.py

84 lines
2.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import json
from loguru import logger
class EmailAPIClient:
"""自建邮箱API客户端
用于从API获取邮箱账号
"""
def __init__(self, api_url="https://steamapi.cursorpro.com.cn/api/create_user"):
"""初始化API客户端
Args:
api_url: API服务器地址
"""
self.api_url = api_url
def create_email_account(self):
"""创建新的邮箱账号
通过调用API创建一个新的邮箱账号
Returns:
dict: 包含以下字段的字典如果失败则返回None
- id: 账号ID
- username: 用户名
- email: 邮箱地址
- password: 邮箱密码
- success: 是否成功
- message: 信息
"""
try:
logger.info(f"正在从API获取邮箱账号: {self.api_url}")
response = requests.post(self.api_url, timeout=30)
if response.status_code != 200:
logger.error(f"API请求失败状态码: {response.status_code}")
return None
try:
result = response.json()
except json.JSONDecodeError:
logger.error("API返回的数据不是有效的JSON格式")
return None
if not result.get("success", False):
logger.error(f"API返回错误: {result.get('message', '未知错误')}")
return None
logger.info(f"成功获取邮箱账号: {result.get('email')}")
return result
except Exception as e:
logger.error(f"获取邮箱账号出错: {str(e)}")
return None
def get_email_credentials(self):
"""获取邮箱凭据
获取邮箱地址和密码,并格式化为凭据字典
Returns:
dict: 包含email和password字段的字典如果失败则返回None
"""
account = self.create_email_account()
if not account:
return None
return {
'email': account.get('email'),
'password': account.get('password')
}
# 测试代码
if __name__ == "__main__":
client = EmailAPIClient()
email_data = client.get_email_credentials()
if email_data:
print(f"获取到的邮箱: {email_data['email']}")
print(f"获取到的密码: {email_data['password']}")
else:
print("获取邮箱账号失败")