吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 727|回复: 6
收起左侧

[Python 原创] 自动化获取搜狗流行词库与萌娘百科词库,适配Rime【小狼毫】

[复制链接]
Tr07 发表于 2025-4-6 18:07
本帖最后由 Tr07 于 2025-4-7 18:55 编辑

[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import requests
import subprocess
import os
from datetime import datetime
from bs4 import BeautifulSoup
 
# 全局配置
CONFIG = {
    # GitHub 配置
    "github": {
        "enable": True# 是否启用 萌娘百科词库 更新任务
        "api_url": "https://api.github.com/repos/outloudvi/mw2fcitx/releases/latest"# GitHub 地址
        "target_file": "moegirl.dict.yaml"  # 目标文件名
    },
    # 搜狗词库配置
    "sogou": {
        "enable": True# 是否启用搜狗更新任务
        "list_url": "https://pinyin.sogou.com/dict/search/search_list/%CD%F8%C2%E7%C1%F7%D0%D0%D0%C2%B4%CA/normal"# 搜狗词库列表页
        "download_url": "https://pinyin.sogou.com/d/dict/download_cell.php?id=4&name=网络流行新词&f=detail"# 搜狗词库下载地址
        "converter_path": "D:\\Tools\\publish\\深蓝词库转换.exe"# 转换器路径
    },
    # HTTP 请求头
    "headers": {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    },
    # 文件路径配置
    "version_file": "D:\\Cache\\Weasel\\dict.version"# 版本文件路径
    "dict_dir": "D:\\Applications\\Weasel\\Rime\\cn_dicts\\"# 词库目录
    "temp_dir": "D:\\Cache\\Weasel\\"# 临时目录
    "log_dir": "D:\\Cache\\Weasel\\"  # 日志目录
}
 
class UpdateLogger:
    """
    日志记录器,用于记录更新任务的日志。
    """
    def __init__(self):
        self.logs = []  # 日志列表
        self.start_time = datetime.now()  # 任务开始时间
         
    def add(self, message, task=""):
        """
        添加日志条目。
 
        :param message: 日志信息
        :param task: 任务名称(可选)
        """
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        entry = f"[{timestamp}] {'['+task+'] ' if task else ''}{message}"
        self.logs.append(entry)
         
    def save(self):
        """
        保存日志到文件。
 
        :return: 日志文件路径
        """
        os.makedirs(CONFIG["log_dir"], exist_ok=True)
        log_file = f"{CONFIG['log_dir']}update_{self.start_time.strftime('%Y%m%d')}.log"
        with open(log_file, "w", encoding="utf-8") as f:
            f.write("\n".join(self.logs))
        return log_file
 
def update_version_file(target_prefix, new_version):
    """
    更新版本文件中指定前缀的版本号。
     
    :param target_prefix: 要匹配的前缀(如 "[moegirl_dict]:")
    :param new_version: 新的版本号
    """
    try:
        # 读取现有文件内容
        lines = []
        try:
            with open(CONFIG["version_file"], "r") as f:
                lines = f.readlines()
        except FileNotFoundError:
            pass  # 文件不存在时,初始化为空列表
 
        # 查找目标行并更新,或添加新行
        found = False
        for i, line in enumerate(lines):
            if line.startswith(target_prefix):
                lines[i] = f"{target_prefix}{new_version}\n"
                found = True
                break
 
        if not found:
            lines.append(f"{target_prefix}{new_version}\n")
 
        # 写回文件
        with open(CONFIG["version_file"], "w") as f:
            f.writelines(lines)
        return True
    except Exception as e:
        return False
 
def github_task(logger):
    """
    处理 GitHub 词库更新任务。
 
    :param logger: 日志记录器实例
    """
    cfg = CONFIG["github"]
    try:
        # 获取本地版本、
        local_version = "0"
        try:
            with open(CONFIG["version_file"], "r") as f:
                for line in f:
                    if line.startswith("[moegirl_dict]:"):
                        local_version = line[len("[moegirl_dict]:"):].strip()
                        break
        except FileNotFoundError:
            logger.add("初始化本地版本", "GitHub")
 
        response = requests.get(cfg["api_url"], headers=CONFIG["headers"], timeout=10)
        response.raise_for_status()
        release_data = response.json()
        remote_version = release_data["tag_name"]
        logger.add(f"本地版本: {local_version} → 远程版本: {remote_version}", "GitHub")
 
        if remote_version > local_version:
            # 查找目标文件
            asset = next((a for a in release_data["assets"]
                        if a["name"] == cfg["target_file"]), None)
            if asset:
                # 下载文件
                os.makedirs(CONFIG["dict_dir"], exist_ok=True)
                save_path = os.path.join(CONFIG["dict_dir"], cfg["target_file"])
                response = requests.get(asset["browser_download_url"])
                response.raise_for_status()
                with open(save_path, "wb") as f:
                    f.write(response.content)
                 
                # 更新版本记录
                with open(CONFIG["version_file"], "w") as f:
                    f.write("[moegirl_dict]:" + remote_version)
                if update_version_file("[moegirl_dict]:", remote_version):
                    logger.add(f"成功更新到版本 {remote_version}", "GitHub")
                else:
                    logger.add("更新版本文件失败", "GitHub")
                return True
            logger.add("未找到目标文件", "GitHub")
            return False
        logger.add("当前已是最新版本", "GitHub")
        return True
    except Exception as e:
        logger.add(f"更新失败: {str(e)}", "GitHub")
        return False
 
def sogou_task(logger):
    """
    处理搜狗词库更新任务。
 
    :param logger: 日志记录器实例
    """
    cfg = CONFIG["sogou"]
    try:
        # 获取网页更新时间
        response = requests.get(cfg["list_url"], headers=CONFIG["headers"], timeout=10)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, 'html.parser')
         
        target = soup.find('a', string="网络流行新词")
        if not target:
            logger.add("未找到目标词库", "Sogou")
            return False
             
        update_div = target.find_parent('div',class_='dict_detail_block').find('div', class_='show_title', string="更新时间:").find_next_sibling('div', class_='show_content')
        if not update_div:
            logger.add("未找到更新时间", "Sogou")
            return False
 
        web_time = datetime.strptime(update_div.get_text(strip=True), "%Y-%m-%d %H:%M:%S")
         
        # 比较本地时间
        local_time = datetime.min
        try:
            with open(CONFIG["version_file"], 'r') as f:
                for line in f:
                    if line.startswith("[Sogou_dict]:"):
                        local_time = datetime.strptime(line[len("[Sogou_dict]:"):].strip(), "%Y-%m-%d %H:%M:%S")
                        break
        except FileNotFoundError:
            logger.add("初始化本地版本", "GitHub")
         
        if web_time <= local_time:
            logger.add("当前已是最新版本", "Sogou")
            return True
             
        # 执行下载和转换
        logger.add(f"本地版本: {local_time.strftime('%Y-%m-%d %H:%M:%S')} → 远程版本: {web_time.strftime('%Y-%m-%d %H:%M:%S')}", "Sogou")
        scel_path =CONFIG["temp_dir"]+"网络流行新词.scel"
        temp_txt_path = CONFIG["temp_dir"]+"temp.txt"
 
        response = requests.get(cfg["download_url"], headers=CONFIG["headers"])
        response.raise_for_status()
        with open(scel_path, 'wb') as f:
            f.write(response.content)
             
        subprocess.run([
            cfg["converter_path"],
            "-i:scel", scel_path,
            "-o:rime", temp_txt_path,
            "-ct:pinyin",
            "-os:windows"
        ], check=True)
         
        # 处理文本内容
        with open(temp_txt_path, 'r', encoding='utf-8') as f:
            content = [line.rstrip()[:-1] + "\n" for line in f if line.strip() and not line.startswith(("#", "name", "version", "sort"))]
         
        header = [
            "---\n",
            "name: sougou\n",
            f'version: "{datetime.now().strftime("%Y-%m-%d")}"\n',
            "sort: by_weight\n\n"
            "...\n"
        ]
        output_path = CONFIG["dict_dir"]+"sougou.dict.yaml"
        os.makedirs(os.path.dirname(output_path), exist_ok=True)
        with open(output_path, 'w', encoding='utf-8') as f:
            f.writelines(header + content)
 
        #清理临时文件
        os.remove(temp_txt_path)
        os.remove(scel_path)
        # 更新本地时间
        if update_version_file("[Sogou_dict]:", web_time.strftime("%Y-%m-%d %H:%M:%S")):
            logger.add(f"成功更新到版本 {web_time.strftime('%Y-%m-%d %H:%M:%S')}", "Sogou")
        else:
            logger.add("更新版本文件失败", "Sogou")
        return True
    except Exception as e:
        logger.add(f"更新失败: {str(e)}", "Sogou")
        return False
 
def main():
    """
    主函数,执行自动更新任务。
    """
    logger = UpdateLogger()
    logger.add("开始自动更新任务")
     
    if CONFIG["github"]["enable"]:
        github_task(logger)
         
    if CONFIG["sogou"]["enable"]:
        sogou_task(logger)
         
    logger.add("所有任务执行完成")
    log_path = logger.save()
    print(f"日志已保存至: {log_path}")
 
if __name__ == "__main__":
    main()


附一份自动拉取雾凇词库的PowerShell脚本:
[PowerShell] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# # 设置文件写入编码为 UTF-8(无 BOM)
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
 
$repoPath = "D:\Applications\Weasel\Rime"
$logPath = "D:\Cache\Weasel\GitSync_$(Get-Date -Format 'yyyyMM').log"
 
try {
 
    Set-Location $repoPath
 
    git pull 2>&1 | Tee-Object -FilePath $logPath -Append
 
    $localCommit = git rev-parse HEAD
    $remoteCommit = git rev-parse origin/main
    if ($localCommit -ne $remoteCommit) {
        "[INFO] $(Get-Date): 检测到本地有新提交,开始推送..." | Out-File $logPath -Append
        git push origin main 2>&1 | Tee-Object -FilePath $logPath -Append
        if ($LASTEXITCODE -ne 0) { throw "推送失败" }} else { "[INFO] $(Get-Date): 无新提交可推送" | Out-File $logPath -Append}
 
} catch {
    # 错误日志记录
    "[ERROR] $(Get-Date): $($_.Exception.Message)" | Out-File $logPath -Append
    exit 1
}


再附一份win计划任务:
需要改一下小狼毫,脚本的位置才能用
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2025-04-04T15:34:00.1174705</Date>
    <Author>TR07\Tr_07</Author>
    <URI>\词库更新</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2025-04-04T03:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-3643517252-4052142179-1877116032-1001</UserId>
      <LogonType>S4U</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>D:\Applications\Weasel\weasel-0.16.3\WeaselDeployer.exe</Command>
      <Arguments>/sync</Arguments>
    </Exec>
    <Exec>
      <Command>powershell.exe</Command>
      <Arguments>-ExecutionPolicy Bypass -File "D:\Applications\Weasel\Rime\sync_repo.ps1"</Arguments>
    </Exec>
    <Exec>
      <Command>D:\Applications\Weasel\weasel-0.16.3\WeaselDeployer.exe</Command>
      <Arguments>/deploy</Arguments>
    </Exec>
  </Actions>
</Task>

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

icode_isky 发表于 2025-4-6 21:37
这个可以有!感谢大佬分享代码!如果Rime可以集成,配置词库更新地址就更完美了!
seatofish 发表于 2025-4-7 09:21
crbiggun 发表于 2025-4-8 11:12
漫咖啡 发表于 2025-4-9 19:56
二樓正解,不會弄啊
 楼主| Tr07 发表于 2025-4-16 12:27
crbiggun 发表于 2025-4-8 11:12
怎么用?有没有教程

直接下载下来,修改一下路径就可以用啦
 楼主| Tr07 发表于 2025-4-16 12:32
漫咖啡 发表于 2025-4-9 19:56
二樓正解,不會弄啊

直接下载下来,修改一下路径就可以用啦
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-23 14:13

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表