| 1 | import os
|
| 2 | import json
|
| 3 |
|
| 4 | def generate_twikoo_emojis(base_url, emoji_folders, output_path='.json/twikoo-emojis.json', origin=None):
|
| 5 | """
|
| 6 | 生成 Twikoo 表情包 JSON 文件
|
| 7 |
|
| 8 | 参数:
|
| 9 | base_url: 基础 URL
|
| 10 | emoji_folders: 包含表情包的文件夹列表
|
| 11 | output_path: 输出 JSON 文件的路径
|
| 12 | origin: 原始表情包数据,包含网址到名称的映射关系
|
| 13 | """
|
| 14 | result = {}
|
| 15 |
|
| 16 | # 创建网址到名称的映射字典
|
| 17 | url_to_text_map = {}
|
| 18 | if origin:
|
| 19 | for category, data in origin.items():
|
| 20 | if "container" in data:
|
| 21 | for item in data["container"]:
|
| 22 | if "icon" in item and "text" in item:
|
| 23 | # 从 icon 中提取 URL
|
| 24 | import re
|
| 25 | url_match = re.search(r"src=['\"]([^'\"]+)['\"]", item["icon"])
|
| 26 | if url_match:
|
| 27 | url = url_match.group(1).replace("`", "").strip()
|
| 28 | url_to_text_map[url] = item["text"]
|
| 29 |
|
| 30 | for folder in emoji_folders:
|
| 31 | if not os.path.isdir(folder):
|
| 32 | print(f"警告: {folder} 不是一个文件夹,已跳过")
|
| 33 | continue
|
| 34 |
|
| 35 | # 为每个文件夹创建一个类别
|
| 36 | category_name = folder
|
| 37 | # 可以根据文件夹名称自定义类别显示名称
|
| 38 | display_names = {
|
| 39 | "blobcat": "可爱猫",
|
| 40 | "linedog": "线条狗",
|
| 41 | "bilibili": "小电视",
|
| 42 | "liushen": "流沙",
|
| 43 | # 可以添加更多映射
|
| 44 | }
|
| 45 |
|
| 46 | category_display_name = display_names.get(folder, folder)
|
| 47 |
|
| 48 | emoji_category = {
|
| 49 | "type": "image",
|
| 50 | "container": []
|
| 51 | }
|
| 52 |
|
| 53 | # 获取文件夹中的所有文件
|
| 54 | files = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
|
| 55 |
|
| 56 | for file in files:
|
| 57 | # 获取文件名(不带扩展名)
|
| 58 | filename_without_ext = os.path.splitext(file)[0]
|
| 59 |
|
| 60 | # 构建完整URL
|
| 61 | full_url = f"{base_url}/{folder}/{file}"
|
| 62 |
|
| 63 | # 检查是否在原始数据中存在该URL,如果存在则使用原始text
|
| 64 | text = url_to_text_map.get(full_url, f"{filename_without_ext}")
|
| 65 |
|
| 66 | # 创建表情项
|
| 67 | emoji_item = {
|
| 68 | "text": text,
|
| 69 | "icon": f"<img src='{full_url}'>"
|
| 70 | }
|
| 71 |
|
| 72 | emoji_category["container"].append(emoji_item)
|
| 73 |
|
| 74 | if emoji_category["container"]: # 只添加非空的表情组
|
| 75 | result[category_display_name] = emoji_category
|
| 76 |
|
| 77 | # 确保输出目录存在
|
| 78 | os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 79 |
|
| 80 | # 写入 JSON 文件
|
| 81 | with open(output_path, 'w', encoding='utf-8') as f:
|
| 82 | json.dump(result, f, ensure_ascii=False, indent=2)
|
| 83 |
|
| 84 | print(f"Twikoo 表情包 JSON 文件已生成: {output_path}")
|
| 85 |
|
| 86 | if __name__ == "__main__":
|
| 87 | base_url = "https://jsd.example.com/gh/willow-god/owo/"
|
| 88 | folders = ["liushen", "blobcat", "linedog", "bilibili"]
|
| 89 | origin_json = ".json/twikoo.json"
|
| 90 | output = ".json/twikoo-emoji.json"
|
| 91 |
|
| 92 | # 加载原始表情包数据(如果存在)
|
| 93 | origin_data = None
|
| 94 | try:
|
| 95 | with open(origin_json, 'r', encoding='utf-8') as f:
|
| 96 | origin_data = json.load(f)
|
| 97 | print(f"已加载原始表情包数据: {origin_json}")
|
| 98 | except FileNotFoundError:
|
| 99 | print(f"未找到原始表情包数据文件: {origin_json}")
|
| 100 | except json.JSONDecodeError:
|
| 101 | print(f"原始表情包数据文件格式错误: {origin_json}")
|
| 102 |
|
| 103 | generate_twikoo_emojis(base_url, folders, output, origin_data)
|