下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1133
这个框架提供了完整的社交平台卡片消息生成和发送功能。包含基础类、各平台具体实现、客户端接口和使用示例。您可以根据需要扩展更多平台支持或添加更复杂的功能。
abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, Any
import xml.etree.ElementTree as ET
@dataclass
class CardContent:
title: str
description: str
image_url: str
action_url: str
extra_data: Dict[str, Any] = None
class BaseCardGenerator(ABC):
def init(self, platform_name: str):
self.platform = platform_name
@abstractmethod
def generate_xml(self, content: CardContent) -> str:
"""生成平台特定的XML卡片消息"""
pass
@abstractmethod
def validate_content(self, content: CardContent) -> bool:
"""验证内容是否符合平台规范"""
pass
def _create_base_xml(self) -> ET.Element:
"""创建基础XML结构"""
root = ET.Element("card")
root.set("version", "1.0")
root.set("platform", self.platform)
return root
.base import BaseCardGenerator, CardContent
import xml.etree.ElementTree as ET
class DouyinCardGenerator(BaseCardGenerator):
def init(self):
super().init("douyin")
def validate_content(self, content: CardContent) -> bool:
if not content.title or len(content.title) > 20:
return False
if not content.image_url.startswith("https://"):
return False
return True
def generate_xml(self, content: CardContent) -> str:
if not self.validate_content(content):
raise ValueError("Invalid content for Douyin card")
root = self._create_base_xml()
# 添加抖音特定元素
title_elem = ET.SubElement(root, "title")
title_elem.text = content.title
desc_elem = ET.SubElement(root, "description")
desc_elem.text = content.description
image_elem = ET.SubElement(root, "image")
image_elem.set("url", content.image_url)
action_elem = ET.SubElement(root, "action")
action_elem.set("type", "open_url")
action_elem.set("url", content.action_url)
if content.extra_data:
extra_elem = ET.SubElement(root, "extra")
for key, value in content.extra_data.items():
item_elem = ET.SubElement(extra_elem, "item")
item_elem.set("key", key)
item_elem.text = str(value)
return ET.tostring(root, encoding="unicode")
.base import BaseCardGenerator, CardContent
import xml.etree.ElementTree as ET
class KuaishouCardGenerator(BaseCardGenerator):
def init(self):
super().init("kuaishou")
def validate_content(self, content: CardContent) -> bool:
if not content.title or len(content.title) > 30:
return False
if not content.image_url:
return False
return True
def generate_xml(self, content: CardContent) -> str:
if not self.validate_content(content):
raise ValueError("Invalid content for Kuaishou card")
root = self._create_base_xml()
# 快手卡片结构略有不同
header = ET.SubElement(root, "header")
ET.SubElement(header, "title").text = content.title
body = ET.SubElement(root, "body")
ET.SubElement(body, "text").text = content.description
ET.SubElement(body, "image").set("url", content.image_url)
footer = ET.SubElement(root, "footer")
action = ET.SubElement(footer, "action")
action.set("type", "web")
action.set("url", content.action_url)
return ET.tostring(root, encoding="unicode")