95 lines
3.2 KiB
Python
95 lines
3.2 KiB
Python
import os
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
class Config:
|
|
"""配置类"""
|
|
|
|
def __init__(self):
|
|
self.base_url = "https://cursorapi.nosqli.com"
|
|
self.api_endpoints = {
|
|
"activate": f"{self.base_url}/admin/api.member/activate",
|
|
"status": f"{self.base_url}/admin/api.member/status",
|
|
"get_unused": f"{self.base_url}/admin/api.account/getUnused",
|
|
"heartbeat": f"{self.base_url}/admin/api.account/heartbeat"
|
|
}
|
|
|
|
# macOS配置目录
|
|
self.config_dir = Path(os.path.expanduser("~")) / ".cursor_pro"
|
|
self.config_file = self.config_dir / "config.json"
|
|
self.member_file = self.config_dir / "member.json"
|
|
self.load_config()
|
|
|
|
def load_config(self):
|
|
"""加载配置"""
|
|
try:
|
|
self.config_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
if not self.config_file.exists():
|
|
self.save_default_config()
|
|
|
|
with open(self.config_file, "r", encoding="utf-8") as f:
|
|
config = json.load(f)
|
|
self.api_token = config.get("api_token", "")
|
|
|
|
except Exception as e:
|
|
logging.error(f"加载配置失败: {str(e)}")
|
|
self.api_token = ""
|
|
|
|
def save_member_info(self, info: dict):
|
|
"""保存会员信息"""
|
|
try:
|
|
with open(self.member_file, "w", encoding="utf-8") as f:
|
|
json.dump(info, f, indent=2, ensure_ascii=False)
|
|
logging.info("会员信息已保存")
|
|
except Exception as e:
|
|
logging.error(f"保存会员信息失败: {str(e)}")
|
|
|
|
def load_member_info(self) -> dict:
|
|
"""读取会员信息"""
|
|
try:
|
|
if self.member_file.exists():
|
|
with open(self.member_file, "r", encoding="utf-8") as f:
|
|
info = json.load(f)
|
|
logging.info(f"已读取会员信息: 到期时间 {info.get('expire_time', '')}")
|
|
return info
|
|
except Exception as e:
|
|
logging.error(f"读取会员信息失败: {str(e)}")
|
|
return {
|
|
"expire_time": "",
|
|
"days": 0,
|
|
"new_days": 0
|
|
}
|
|
|
|
def save_default_config(self):
|
|
"""保存默认配置"""
|
|
config = {
|
|
"api_token": ""
|
|
}
|
|
with open(self.config_file, "w", encoding="utf-8") as f:
|
|
json.dump(config, f, indent=2, ensure_ascii=False)
|
|
|
|
def save_config(self, api_token: str):
|
|
"""保存新的配置"""
|
|
config = {
|
|
"api_token": api_token
|
|
}
|
|
with open(self.config_file, "w", encoding="utf-8") as f:
|
|
json.dump(config, f, indent=2, ensure_ascii=False)
|
|
self.api_token = api_token
|
|
logging.info("配置已更新")
|
|
|
|
def get_api_url(self, endpoint_name: str) -> str:
|
|
"""获取API端点URL
|
|
|
|
Args:
|
|
endpoint_name: 端点名称
|
|
|
|
Returns:
|
|
str: 完整的API URL
|
|
"""
|
|
url = self.api_endpoints.get(endpoint_name, "")
|
|
if not url:
|
|
logging.error(f"未找到API端点: {endpoint_name}")
|
|
return url |