201 lines
7.2 KiB
Python
201 lines
7.2 KiB
Python
"""
|
|
平台配置管理
|
|
包含各平台的特定配置信息。
|
|
"""
|
|
|
|
from typing import Dict, Any
|
|
from ..core.models import PlatformType, PlatformConfig, XIAOHONGSHU_CONFIG, DOUYIN_CONFIG
|
|
|
|
|
|
class PlatformConfigs:
|
|
"""平台配置管理类"""
|
|
|
|
def __init__(self):
|
|
self._configs: Dict[PlatformType, PlatformConfig] = {
|
|
PlatformType.XIAOHONGSHU: XIAOHONGSHU_CONFIG,
|
|
PlatformType.DOUYIN: DOUYIN_CONFIG
|
|
}
|
|
self._custom_configs: Dict[PlatformType, PlatformConfig] = {}
|
|
|
|
def get_config(self, platform: PlatformType) -> PlatformConfig:
|
|
"""获取平台配置"""
|
|
return self._custom_configs.get(platform, self._configs.get(platform))
|
|
|
|
def set_config(self, platform: PlatformType, config: PlatformConfig):
|
|
"""设置平台配置"""
|
|
self._custom_configs[platform] = config
|
|
|
|
def update_config(self, platform: PlatformType, updates: Dict[str, Any]):
|
|
"""更新平台配置"""
|
|
current_config = self.get_config(platform)
|
|
if current_config:
|
|
# 创建新的配置对象
|
|
updated_config = PlatformConfig(
|
|
name=updates.get("name", current_config.name),
|
|
login_url=updates.get("login_url", current_config.login_url),
|
|
supported_content_types=updates.get("supported_content_types", current_config.supported_content_types),
|
|
max_file_size=updates.get("max_file_size", current_config.max_file_size),
|
|
max_duration=updates.get("max_duration", current_config.max_duration),
|
|
supported_formats=updates.get("supported_formats", current_config.supported_formats),
|
|
extra_config={**current_config.extra_config, **updates.get("extra_config", {})}
|
|
)
|
|
self.set_config(platform, updated_config)
|
|
|
|
def get_all_configs(self) -> Dict[PlatformType, PlatformConfig]:
|
|
"""获取所有平台配置"""
|
|
return {**self._configs, **self._custom_configs}
|
|
|
|
def reset_config(self, platform: PlatformType):
|
|
"""重置平台配置为默认值"""
|
|
if platform in self._custom_configs:
|
|
del self._custom_configs[platform]
|
|
|
|
def load_from_dict(self, config_dict: Dict[str, Any]):
|
|
"""从字典加载配置"""
|
|
for platform_name, config_data in config_dict.items():
|
|
try:
|
|
platform = PlatformType(platform_name)
|
|
config = PlatformConfig.from_dict(config_data)
|
|
self.set_config(platform, config)
|
|
except ValueError:
|
|
continue # 忽略未知的平台名称
|
|
|
|
|
|
# 全局平台配置实例
|
|
platform_configs = PlatformConfigs()
|
|
|
|
|
|
def get_platform_config(platform: PlatformType) -> PlatformConfig:
|
|
"""获取平台配置的便捷函数"""
|
|
return platform_configs.get_config(platform)
|
|
|
|
|
|
# 平台特定的选择器配置
|
|
PLATFORM_SELECTORS = {
|
|
PlatformType.XIAOHONGSHU: {
|
|
"login": {
|
|
"qr_code": "[data-testid='qr-code']",
|
|
"qr_code_container": ".qr-code-container",
|
|
"login_success_indicators": [
|
|
"[data-testid='avatar']",
|
|
".creator-center-avatar",
|
|
".user-info",
|
|
".user-avatar"
|
|
]
|
|
},
|
|
"publish": {
|
|
"image_note": {
|
|
"title_input": "input[placeholder*='标题']",
|
|
"content_input": "textarea[placeholder*='内容']",
|
|
"image_upload": "[data-testid='image-upload']",
|
|
"tag_input": "input[placeholder*='标签']",
|
|
"publish_button": "[data-testid='publish-btn']",
|
|
"success_indicator": ".publish-success",
|
|
"upload_progress": ".upload-progress"
|
|
},
|
|
"video_note": {
|
|
"title_input": "input[placeholder*='标题']",
|
|
"content_input": "textarea[placeholder*='内容']",
|
|
"video_upload": "[data-testid='video-upload']",
|
|
"tag_input": "input[placeholder*='标签']",
|
|
"publish_button": "[data-testid='publish-btn']",
|
|
"success_indicator": ".publish-success",
|
|
"upload_progress": ".video-upload-progress"
|
|
}
|
|
}
|
|
},
|
|
PlatformType.DOUYIN: {
|
|
"login": {
|
|
"qr_code": ".login-qr-code",
|
|
"qr_code_container": ".qr-container",
|
|
"login_success_indicators": [
|
|
".user-avatar",
|
|
".creator-workspace",
|
|
".publish-btn",
|
|
".user-info"
|
|
]
|
|
},
|
|
"publish": {
|
|
"video": {
|
|
"title_input": "input[placeholder*='标题']",
|
|
"description_input": "textarea[placeholder*='描述']",
|
|
"video_upload": "[data-testid='video-upload']",
|
|
"tag_input": "input[placeholder*='#']",
|
|
"publish_button": "[data-testid='publish-btn']",
|
|
"success_indicator": ".upload-success",
|
|
"upload_progress": ".upload-progress-bar"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
def get_selectors(platform: PlatformType, page_type: str, content_type: str = None) -> Dict[str, str]:
|
|
"""获取平台选择器配置"""
|
|
platform_selectors = PLATFORM_SELECTORS.get(platform, {})
|
|
page_selectors = platform_selectors.get(page_type, {})
|
|
|
|
if content_type and page_type == "publish":
|
|
content_selectors = page_selectors.get(content_type, {})
|
|
return content_selectors
|
|
|
|
return page_selectors
|
|
|
|
|
|
# 平台特定的等待时间配置(秒)
|
|
PLATFORM_WAIT_TIMES = {
|
|
PlatformType.XIAOHONGSHU: {
|
|
"login": {
|
|
"qr_code_load": 10,
|
|
"login_success": 5,
|
|
"login_timeout": 300 # 5分钟
|
|
},
|
|
"publish": {
|
|
"page_load": 10,
|
|
"image_upload": 30,
|
|
"video_upload": 120,
|
|
"publish_success": 10,
|
|
"operation_delay": (1, 3)
|
|
}
|
|
},
|
|
PlatformType.DOUYIN: {
|
|
"login": {
|
|
"qr_code_load": 10,
|
|
"login_success": 5,
|
|
"login_timeout": 300 # 5分钟
|
|
},
|
|
"publish": {
|
|
"page_load": 10,
|
|
"video_upload": 180,
|
|
"publish_success": 15,
|
|
"operation_delay": (2, 4)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
def get_wait_times(platform: PlatformType, operation: str) -> Dict[str, Any]:
|
|
"""获取平台等待时间配置"""
|
|
return PLATFORM_WAIT_TIMES.get(platform, {}).get(operation, {})
|
|
|
|
|
|
# 平台特定的URL配置
|
|
PLATFORM_URLS = {
|
|
PlatformType.XIAOHONGSHU: {
|
|
"login": "https://creator.xiaohongshu.com/",
|
|
"image_note": "https://creator.xiaohongshu.com/publish/publish",
|
|
"video_note": "https://creator.xiaohongshu.com/publish/video",
|
|
"home": "https://creator.xiaohongshu.com/creator-center"
|
|
},
|
|
PlatformType.DOUYIN: {
|
|
"login": "https://creator.douyin.com/creator-micro/home",
|
|
"video_upload": "https://creator.douyin.com/creator-micro/content/upload",
|
|
"home": "https://creator.douyin.com/creator-micro/home",
|
|
"content_manage": "https://creator.douyin.com/creator-micro/content/manage"
|
|
}
|
|
}
|
|
|
|
|
|
def get_platform_url(platform: PlatformType, page_type: str) -> str:
|
|
"""获取平台URL配置"""
|
|
return PLATFORM_URLS.get(platform, {}).get(page_type, "") |