新增加花字数据。

This commit is contained in:
Hommy
2026-03-31 21:24:05 +08:00
parent 3be829f494
commit 0bf3a9715a
5 changed files with 101803 additions and 43 deletions
+92777
View File
File diff suppressed because it is too large Load Diff
+44 -43
View File
@@ -6,49 +6,50 @@ from typing import List, Dict, Any, Optional
from src.utils.logger import logger
from exceptions import CustomException, CustomError
# 预定义的花字效果映射表
# 格式:{effect_name: {"resource_id": "...", "effect_id": "..."}}
TEXT_EFFECT_MAP = {
# 热门花字效果
"白字橘色发光花字": {
"resource_id": "7296357486490144036",
"effect_id": "7296357486490144036",
"name": "白字橘色发光花字",
"is_vip": False
},
"黄字白色发光花字": {
"resource_id": "7296357486490144037",
"effect_id": "7296357486490144037",
"name": "黄字白色发光花字",
"is_vip": False
},
"粉字白色发光花字": {
"resource_id": "7296357486490144038",
"effect_id": "7296357486490144038",
"name": "粉字白色发光花字",
"is_vip": False
},
"绿字白色发光花字": {
"resource_id": "7296357486490144039",
"effect_id": "7296357486490144039",
"name": "绿字白色发光花字",
"is_vip": False
},
"蓝字白色发光花字": {
"resource_id": "7296357486490144040",
"effect_id": "7296357486490144040",
"name": "蓝字白色发光花字",
"is_vip": False
},
"紫字白色发光花字": {
"resource_id": "7296357486490144041",
"effect_id": "7296357486490144041",
"name": "紫字白色发光花字",
"is_vip": False
},
# 更多花字效果可以根据需要添加
}
# 导入自动生成的花字效果映射表
try:
from .text_effect_map_generated import TEXT_EFFECT_MAP
except ImportError:
# 如果生成的文件不存在,使用默认的映射表
TEXT_EFFECT_MAP = {
# 热门花字效果
"白字橘色发光花字": {
"resource_id": "7296357486490144036",
"effect_id": "7296357486490144036",
"name": "白字橘色发光花字",
"is_vip": False
},
"黄字白色发光花字": {
"resource_id": "7296357486490144037",
"effect_id": "7296357486490144037",
"name": "黄字白色发光花字",
"is_vip": False
},
"粉字白色发光花字": {
"resource_id": "7296357486490144038",
"effect_id": "7296357486490144038",
"name": "粉字白色发光花字",
"is_vip": False
},
"绿字白色发光花字": {
"resource_id": "7296357486490144039",
"effect_id": "7296357486490144039",
"name": "绿字白色发光花字",
"is_vip": False
},
"蓝字白色发光花字": {
"resource_id": "7296357486490144040",
"effect_id": "7296357486490144040",
"name": "蓝字白色发光花字",
"is_vip": False
},
"紫字白色发光花字": {
"resource_id": "7296357486490144041",
"effect_id": "7296357486490144041",
"name": "紫字白色发光花字",
"is_vip": False
},
}
def get_text_effects(mode: int = 0) -> List[Dict[str, Any]]:
File diff suppressed because it is too large Load Diff
+198
View File
@@ -0,0 +1,198 @@
"""
测试扩展后的花字效果功能(1440 个花字)
"""
import random
from src.service.get_text_effects import TEXT_EFFECT_MAP, resolve_text_effect, get_text_effects
def test_total_count():
"""测试花字总数"""
print("=" * 80)
print("测试 1: 验证花字效果总数")
print("=" * 80)
total = len(TEXT_EFFECT_MAP)
print(f"\n✓ 当前加载的花字效果总数:{total}")
assert total == 1440, f"期望 1440 个花字,但实际有 {total}"
print(f"✓ 验证通过:共有 {total} 个花字效果")
return total
def test_resolve_by_name():
"""测试通过中文名称查找花字"""
print("\n" + "=" * 80)
print("测试 2: 通过中文名称查找花字")
print("=" * 80)
# 测试几个示例花字
test_cases = [
"红黄火焰综艺花字",
"蓝白色立体综艺描边花字",
"黄字橙光花字",
]
for effect_name in test_cases:
if effect_name in TEXT_EFFECT_MAP:
result = resolve_text_effect(effect_name)
print(f"\n✓ 找到花字:'{effect_name}'")
print(f" Effect ID: {result['effect_id']}")
print(f" Resource ID: {result['resource_id']}")
else:
print(f"\n✗ 未找到花字:'{effect_name}'")
print("\n✓ 中文名称查找测试完成")
def test_resolve_by_id():
"""测试通过 effect_id 查找花字"""
print("\n" + "=" * 80)
print("测试 3: 通过 effect_id 查找花字")
print("=" * 80)
# 获取第一个花字的 ID
first_effect_name = list(TEXT_EFFECT_MAP.keys())[0]
first_effect_id = TEXT_EFFECT_MAP[first_effect_name]["effect_id"]
result = resolve_text_effect(first_effect_id)
print(f"\n✓ 通过 ID '{first_effect_id}' 查找到:")
print(f" 名称:{first_effect_name}")
print(f" Resource ID: {result['resource_id']}")
print("\n✓ effect_id 查找测试完成")
def test_random_effects():
"""随机测试 10 个花字"""
print("\n" + "=" * 80)
print("测试 4: 随机测试 10 个花字效果")
print("=" * 80)
all_effects = list(TEXT_EFFECT_MAP.items())
random_effects = random.sample(all_effects, 10)
print("\n随机选择的 10 个花字效果:")
for i, (name, data) in enumerate(random_effects, 1):
print(f" {i}. {name} (ID: {data['effect_id']})")
# 验证可以解析
result = resolve_text_effect(name)
assert result is not None, f"无法解析花字:{name}"
print("\n✓ 随机测试通过")
def test_filter_by_mode():
"""测试按模式过滤花字"""
print("\n" + "=" * 80)
print("测试 5: 按模式过滤花字效果")
print("=" * 80)
# 测试 mode=0 (所有)
all_effects = get_text_effects(mode=0)
print(f"\n✓ Mode 0 (所有): {len(all_effects)} 个花字")
# 测试 mode=1 (VIP)
vip_effects = get_text_effects(mode=1)
print(f"✓ Mode 1 (VIP): {len(vip_effects)} 个花字")
# 测试 mode=2 (免费)
free_effects = get_text_effects(mode=2)
print(f"✓ Mode 2 (免费): {len(free_effects)} 个花字")
# 验证数量关系
assert len(all_effects) == len(vip_effects) + len(free_effects), \
"所有效果数量应该等于 VIP+免费的总和"
print("\n✓ 过滤功能测试通过")
def test_special_characters():
"""测试包含特殊字符的花字名称"""
print("\n" + "=" * 80)
print("测试 6: 包含特殊字符的花字名称")
print("=" * 80)
special_names = []
for name in TEXT_EFFECT_MAP.keys():
if any(c in name for c in ['#', '!', '-', ' ']):
special_names.append(name)
print(f"\n找到 {len(special_names)} 个包含特殊字符的花字名称:")
for name in special_names[:10]: # 只显示前 10 个
print(f" - {name}")
if len(special_names) > 10:
print(f" ... 还有 {len(special_names) - 10}")
# 测试解析其中一个
if special_names:
test_name = special_names[0]
result = resolve_text_effect(test_name)
print(f"\n✓ 测试解析 '{test_name}': 成功")
print("\n✓ 特殊字符测试通过")
def test_export_capability():
"""测试导出功能"""
print("\n" + "=" * 80)
print("测试 7: 导出花字列表到文件")
print("=" * 80)
output_file = "test_effects_list.txt"
with open(output_file, 'w', encoding='utf-8') as f:
f.write("# 花字效果列表 (共 1440 个)\n\n")
f.write("| 序号 | 花字名称 | Effect ID |\n")
f.write("|------|---------|-----------|\n")
for i, (name, data) in enumerate(TEXT_EFFECT_MAP.items(), 1):
f.write(f"| {i} | {name} | {data['effect_id']} |\n")
print(f"\n✓ 已导出花字列表到:{output_file}")
print(f" 文件大小:请查看生成的文件")
print("\n✓ 导出测试通过")
def main():
"""运行所有测试"""
print("\n" + "=" * 80)
print("开始测试扩展后的花字效果功能 (1440 个花字)")
print("=" * 80)
try:
# 运行所有测试
test_total_count()
test_resolve_by_name()
test_resolve_by_id()
test_random_effects()
test_filter_by_mode()
test_special_characters()
test_export_capability()
# 总结
print("\n" + "=" * 80)
print("✅ 所有测试通过!")
print("=" * 80)
print(f"\n📊 统计摘要:")
print(f" • 总花字数:1,440 个")
print(f" • 支持中文名称查找:是")
print(f" • 支持 effect_id 查找:是")
print(f" • 支持模式过滤:是")
print(f" • 特殊字符支持:是")
print(f" • 导出功能:是")
print("\n🎉 花字效果扩展功能运行正常!")
print("=" * 80)
except AssertionError as e:
print(f"\n❌ 测试失败:{str(e)}")
raise
except Exception as e:
print(f"\n❌ 发生错误:{str(e)}")
raise
if __name__ == "__main__":
main()
+140
View File
@@ -0,0 +1,140 @@
"""
从 data.ts 文件提取所有花字效果并生成 Python 映射表
"""
import re
import json
def extract_text_effects_from_data_ts(file_path: str) -> dict:
"""
从 data.ts 文件中提取所有花字效果
Args:
file_path: data.ts 文件路径
Returns:
花字效果映射字典 {title: {"effect_id": ..., "resource_id": ..., "is_vip": ...}}
"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 使用正则表达式匹配所有花字对象
# 匹配 common_attr 块
pattern = r'common_attr:\s*\{([^}]+(?:\{[^}]*\}[^}]*)*)\}'
text_effects = {}
for match in re.finditer(pattern, content, re.DOTALL):
block = match.group(1)
# 提取 effect_id
effect_id_match = re.search(r'effect_id:\s*["\'](\d+)["\']', block)
if not effect_id_match:
continue
effect_id = effect_id_match.group(1)
# 提取 title
title_match = re.search(r'title:\s*["\']([^"\']+)["\']', block)
if not title_match:
continue
title = title_match.group(1)
# 提取 is_vip 信息 (从 business_info.json_str 中)
is_vip = False
json_str_match = re.search(r'json_str:\s*["\']({[^}]+})["\']', block)
if json_str_match:
try:
business_info = json.loads(json_str_match.group(1).replace('\\"', '"'))
is_vip = business_info.get('is_vip', False)
except:
pass
# 添加到字典
text_effects[title] = {
"resource_id": effect_id,
"effect_id": effect_id,
"name": title,
"is_vip": is_vip
}
return text_effects
def generate_python_mapping(text_effects: dict) -> str:
"""
生成 Python 格式的映射表代码
Args:
text_effects: 花字效果字典
Returns:
Python 代码字符串
"""
code_lines = [
"# 从 data.ts 自动生成的花字效果映射表",
"# 格式:{effect_name: {\"resource_id\": \"...\", \"effect_id\": \"...\", \"name\": \"...\", \"is_vip\": ...}}",
"TEXT_EFFECT_MAP = {",
]
# 按名称排序
sorted_effects = sorted(text_effects.items(), key=lambda x: x[0])
for i, (name, data) in enumerate(sorted_effects):
is_last = i == len(sorted_effects) - 1
comma = "," if not is_last else ""
code_lines.append(f' "{name}": {{')
code_lines.append(f' "resource_id": "{data["resource_id"]}",')
code_lines.append(f' "effect_id": "{data["effect_id"]}",')
code_lines.append(f' "name": "{data["name"]}",')
code_lines.append(f' "is_vip": {data["is_vip"]}') # Use Python boolean directly
code_lines.append(f" }}{comma}")
code_lines.append("}")
return "\n".join(code_lines)
def main():
# 读取 data.ts 文件
data_file = "d:\\code\\GitHub\\capcut-mate\\data.ts"
print("=" * 60)
print("开始从 data.ts 提取花字效果...")
print("=" * 60)
# 提取花字效果
text_effects = extract_text_effects_from_data_ts(data_file)
print(f"\n成功提取 {len(text_effects)} 个花字效果")
# 显示前 20 个效果
print("\n前 20 个花字效果:")
for i, (name, data) in enumerate(list(text_effects.items())[:20]):
vip_tag = " [VIP]" if data["is_vip"] else ""
print(f" {i+1}. {name}{vip_tag} (ID: {data['effect_id']})")
# 统计 VIP 和免费数量
vip_count = sum(1 for data in text_effects.values() if data["is_vip"])
free_count = len(text_effects) - vip_count
print(f"\nVIP 效果数量:{vip_count}")
print(f"免费效果数量:{free_count}")
# 生成 Python 映射代码
print("\n生成 Python 映射代码...")
python_code = generate_python_mapping(text_effects)
# 保存到文件
output_file = "d:\\code\\GitHub\\capcut-mate\\text_effect_map_generated.py"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(python_code)
print(f"\n映射表已保存到:{output_file}")
print("=" * 60)
return text_effects
if __name__ == "__main__":
main()