Created
May 22, 2026 08:21
-
-
Save muxueqz/f91b4ceba7a93c12ace36f0c407fe31a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| import sys | |
| import subprocess | |
| import requests | |
| # 配置常量 | |
| REPO = "muxueqz/fcitx5-pinyin-sougou-dict" | |
| FILE_NAME = "sougou.dict" | |
| # Fcitx5 用户词库标准路径 | |
| TARGET_DIR = os.path.expanduser("~/.local/share/fcitx5/pinyin/dictionaries/") | |
| TARGET_PATH = os.path.join(TARGET_DIR, FILE_NAME) | |
| def update_dict(): | |
| # 确保目标目录存在 | |
| os.makedirs(TARGET_DIR, exist_ok=True) | |
| # 1. 请求 GitHub API 获取最新 Release 信息 | |
| api_url = f"https://api.github.com/repos/{REPO}/releases/latest" | |
| print(f"正在获取最新 Release 信息...") | |
| try: | |
| response = requests.get(api_url, timeout=15) | |
| response.raise_for_status() | |
| release_data = response.json() | |
| except Exception as e: | |
| print(f"无法访问 GitHub API: {e}", file=sys.stderr) | |
| return False | |
| # 2. 解析出 sougou.dict 的下载链接 | |
| download_url = None | |
| for asset in release_data.get("assets", []): | |
| if asset.get("name") == FILE_NAME: | |
| download_url = asset.get("browser_download_url") | |
| break | |
| if not download_url: | |
| print(f"错误:在最新的 Release 中未找到 {FILE_NAME} 文件。", file=sys.stderr) | |
| return False | |
| # 3. 流式下载文件(避免大文件占用过多内存) | |
| print(f"成功找到下载链接: {download_url}") | |
| print(f"正在下载并保存至: {TARGET_PATH} ...") | |
| try: | |
| with requests.get(download_url, stream=True, timeout=30) as r: | |
| r.raise_for_status() | |
| with open(TARGET_PATH, "wb") as f: | |
| for chunk in r.iter_content(chunk_size=8192): | |
| if chunk: | |
| f.write(chunk) | |
| print("词库下载完成!") | |
| except Exception as e: | |
| print(f"下载文件时出错: {e}", file=sys.stderr) | |
| return False | |
| # 4. 重启 Fcitx5 重新加载词库 | |
| print("正在通知 Fcitx5 重新加载词库...") | |
| try: | |
| # 使用 fcitx5-remote -r 让输入法热重载词库 | |
| subprocess.run(["fcitx5-remote", "-r"], check=True) | |
| print("Fcitx5 词库热重载成功!") | |
| return True | |
| except FileNotFoundError: | |
| print("警告:未找到 fcitx5-remote 命令,请确保 Fcitx5 已正常安装并在运行。", file=sys.stderr) | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"重载 Fcitx5 失败: {e}", file=sys.stderr) | |
| return False | |
| if __name__ == "__main__": | |
| success = update_dict() | |
| sys.exit(0 if success else 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment