LiuShen revised this gist 5 months ago. Go to revision
3 files changed, 217 insertions
USAGE.md(file created)
| @@ -0,0 +1,27 @@ | |||
| 1 | + | 整理好表情包为如下格式: | |
| 2 | + | ||
| 3 | + | ```markdown | |
| 4 | + | . 📂 owo | |
| 5 | + | └── 📂 bilibili/ | |
| 6 | + | │ ├── 📄 bilibili-doge.png | |
| 7 | + | │ ├── 📄 bilibili-大佬.png | |
| 8 | + | │ ├── 📄 ………… | |
| 9 | + | └── 📂 blobcat/ | |
| 10 | + | │ ├── 📄 blobcat0_0.png | |
| 11 | + | │ ├── 📄 blobcatalt.png | |
| 12 | + | │ ├── 📄 blobcatangry.png | |
| 13 | + | │ ├── 📄 ………… | |
| 14 | + | └── 📂 linedog/ | |
| 15 | + | │ ├── 📄 linedog-不要.gif | |
| 16 | + | │ ├── 📄 linedog-写不完了.gif | |
| 17 | + | │ ├── 📄 linedog-准备.gif | |
| 18 | + | │ ├── 📄 ………… | |
| 19 | + | └── 📂 liushen/ | |
| 20 | + | │ ├── 📄 liushen-angry.png | |
| 21 | + | │ ├── 📄 liushen-chowdown.png | |
| 22 | + | │ ├── 📄 liushen-cold.png | |
| 23 | + | │ ├── 📄 ………… | |
| 24 | + | ├── 📄 main.py | |
| 25 | + | ``` | |
| 26 | + | ||
| 27 | + | 执行脚本即可,注意修改脚本中的文件夹列表和映射关系 | |
generate-owo-artalk.py(file created)
| @@ -0,0 +1,87 @@ | |||
| 1 | + | import os | |
| 2 | + | import json | |
| 3 | + | ||
| 4 | + | def generate_artalk_emojis(base_url, emoji_folders, output_path='.json/artalk-emoji.json', origin=None): | |
| 5 | + | """ | |
| 6 | + | 生成 Artalk 表情包 JSON 文件 | |
| 7 | + | ||
| 8 | + | 参数: | |
| 9 | + | base_url: 基础 BASE URL | |
| 10 | + | emoji_folders: 包含表情包的文件夹列表 | |
| 11 | + | output_path: 输出 JSON 文件的路径 | |
| 12 | + | origin: 原始表情包数据,包含网址到名称的映射关系 | |
| 13 | + | """ | |
| 14 | + | result = [] | |
| 15 | + | ||
| 16 | + | # 创建网址到名称的映射字典 | |
| 17 | + | url_to_key_map = {} | |
| 18 | + | if origin: | |
| 19 | + | for group in origin: | |
| 20 | + | for item in group.get("items", []): | |
| 21 | + | if "val" in item and "key" in item: | |
| 22 | + | # 去除可能存在的反引号 | |
| 23 | + | val = item["val"].replace("`", "").strip() | |
| 24 | + | url_to_key_map[val] = item["key"] | |
| 25 | + | ||
| 26 | + | for folder in emoji_folders: | |
| 27 | + | if not os.path.isdir(folder): | |
| 28 | + | print(f"警告: {folder} 不是一个文件夹,已跳过") | |
| 29 | + | continue | |
| 30 | + | ||
| 31 | + | emoji_group = { | |
| 32 | + | "name": folder, | |
| 33 | + | "type": "image", | |
| 34 | + | "items": [] | |
| 35 | + | } | |
| 36 | + | ||
| 37 | + | # 获取文件夹中的所有文件 | |
| 38 | + | files = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))] | |
| 39 | + | ||
| 40 | + | for file in files: | |
| 41 | + | # 获取文件名(不带扩展名) | |
| 42 | + | filename_without_ext = os.path.splitext(file)[0] | |
| 43 | + | ||
| 44 | + | # 构建完整URL | |
| 45 | + | full_url = f"{base_url}/{folder}/{file}" | |
| 46 | + | ||
| 47 | + | # 检查是否在原始数据中存在该URL,如果存在则使用原始key | |
| 48 | + | key = url_to_key_map.get(full_url, filename_without_ext) | |
| 49 | + | ||
| 50 | + | # 创建表情项 | |
| 51 | + | emoji_item = { | |
| 52 | + | "key": key, | |
| 53 | + | "val": full_url | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | emoji_group["items"].append(emoji_item) | |
| 57 | + | ||
| 58 | + | if emoji_group["items"]: # 只添加非空的表情组 | |
| 59 | + | result.append(emoji_group) | |
| 60 | + | ||
| 61 | + | # 确保输出目录存在 | |
| 62 | + | os.makedirs(os.path.dirname(output_path), exist_ok=True) | |
| 63 | + | ||
| 64 | + | # 写入 JSON 文件 | |
| 65 | + | with open(output_path, 'w', encoding='utf-8') as f: | |
| 66 | + | json.dump(result, f, ensure_ascii=False, indent=2) | |
| 67 | + | ||
| 68 | + | print(f"表情包 JSON 文件已生成: {output_path}") | |
| 69 | + | ||
| 70 | + | if __name__ == "__main__": | |
| 71 | + | base_url = "https://jsd.example.com/gh/willow-god/owo/" | |
| 72 | + | folders = ["liushen", "blobcat", "linedog", "bilibili"] | |
| 73 | + | origin_json = ".json/artalk-emoji.json" | |
| 74 | + | output = ".json/artalk-emoji.json" | |
| 75 | + | ||
| 76 | + | # 加载原始表情包数据(如果存在) | |
| 77 | + | origin_data = None | |
| 78 | + | try: | |
| 79 | + | with open(origin_json, 'r', encoding='utf-8') as f: | |
| 80 | + | origin_data = json.load(f) | |
| 81 | + | print(f"已加载原始表情包数据: {origin_json}") | |
| 82 | + | except FileNotFoundError: | |
| 83 | + | print(f"未找到原始表情包数据文件: {origin_json}") | |
| 84 | + | except json.JSONDecodeError: | |
| 85 | + | print(f"原始表情包数据文件格式错误: {origin_json}") | |
| 86 | + | ||
| 87 | + | generate_artalk_emojis(base_url, folders, output, origin_data) | |
generate-owo-twikoo.py(file created)
| @@ -0,0 +1,103 @@ | |||
| 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) | |