|
1 | | -from writer import process_file |
| 1 | +import re |
| 2 | +import os |
| 3 | +from writer import process_file_plus |
2 | 4 | from helper import get_md_files |
| 5 | +from helper import read_json |
3 | 6 | from content_snippet import get_file_description |
4 | 7 | from searcher import search_in_file |
5 | | -import re |
6 | | -import json |
7 | | -import os |
8 | | - |
9 | 8 |
|
10 | | -# 读取JSON文件 |
11 | | -def read_json(file_path): |
12 | | - with open(file_path, 'r', encoding='utf-8') as file: |
13 | | - return json.load(file) |
14 | 9 |
|
15 | | - |
16 | | -# 生成描述信息,仅针对当前脚本的relatedscripts |
17 | | -def generate_description(current_script, all_scripts, code): |
| 10 | +# 生成描述信息,仅针对当前脚本的gro |
| 11 | +def generate_description(current_group, all_scripts, lang): |
18 | 12 | descriptions = [] |
19 | | - # 获取当前脚本的 relatedscripts 值作为分类名 |
20 | | - relatedscripts_category = current_script.get('group') |
21 | | - # 如果没有 relatedscripts,返回空描述 |
22 | | - if not relatedscripts_category: |
| 13 | + # 如果没有 current_group,返回空描述 |
| 14 | + if not current_group: |
23 | 15 | return "无相关脚本。\n\n" |
24 | 16 | # 添加分类名到描述中 |
25 | | - descriptions.append(f'<img height="6px" width="100%" src="https://media.chatgptautorefresh.com/images/separators/gradient-aqua.png?latest">\n\n> ### 🔍你可能在找{relatedscripts_category}\n>') |
26 | | - # 遍历所有脚本,查找具有相同 relatedscripts 值的脚本 |
27 | | - for script in all_scripts: |
28 | | - script_relatedscripts = script.get('group') |
| 17 | + descriptions.append(f'<img height="6px" width="100%" src="https://media.chatgptautorefresh.com/images/separators/gradient-aqua.png?latest">\n\n> ### 🔍你可能在找{current_group}\n>') |
| 18 | + # id升序 |
| 19 | + sorted_scripts = sorted(all_scripts, key=lambda x: x['greasyfork_id'] if x.get('greasyfork_id') is not None else 0) |
| 20 | + # 遍历所有脚本,查找具有相同 group 值的脚本 |
| 21 | + for script in sorted_scripts: |
| 22 | + script_group = script.get('group') |
29 | 23 | # 如果脚本的 relatedscripts 与当前脚本相同,就将其添加到描述中 |
30 | | - if script_relatedscripts == relatedscripts_category: |
31 | | - greasyfork_id = script.get('greasyfork_id', '未知ID') |
| 24 | + if script_group == current_group: |
| 25 | + greasyfork_id = script.get('greasyfork_id') |
32 | 26 | full_path = script.get('directory') + "/" + script.get('js_name') |
33 | | - results = search_in_file(full_path, code) |
| 27 | + results = search_in_file(full_path, lang) |
34 | 28 | name = results.name_matches[0] |
35 | 29 | description = results.description_matches[0] |
36 | | - link = f"[**{name}**](https://greasyfork.org/scripts/{greasyfork_id})" |
37 | | - descriptions.append(f"> - {link}: {description}") |
| 30 | + # 导入失败的脚本没有id直接使用github |
| 31 | + if greasyfork_id in (None, 0): |
| 32 | + item = f"[**{name}**](https://github.com/ChinaGodMan/UserScripts/tree/main/{script.get('directory')}#readme)" |
| 33 | + else: |
| 34 | + item = f"[**{name}**](https://greasyfork.org/scripts/{greasyfork_id})" |
| 35 | + |
| 36 | + descriptions.append(f"> - {item}: {description}") |
38 | 37 | return "\n".join(descriptions) + "\n" |
39 | 38 |
|
40 | 39 |
|
| 40 | +def process_script(script, scripts, start_tag, end_tag, group): |
| 41 | + # group 可以从script.get('group')获取,这里直接使用直接传递的 |
| 42 | + script_directory = script.get('directory', '') |
| 43 | + cnfile_path = os.path.join(script_directory, "README.md") |
| 44 | + descriptions = generate_description(group, scripts, "zh-CN") |
| 45 | + olddescriptions = get_file_description(cnfile_path, start_tag, end_tag) |
| 46 | + if olddescriptions is None: |
| 47 | + olddescriptions = "ggg" |
| 48 | + if olddescriptions + "\n" == descriptions: |
| 49 | + return |
| 50 | + else: |
| 51 | + print(f"----[\033[94m{script.get('name', '')}\033[0m--\033[95m{group}\033[0m]\033[92m 内容变化,执行替换\033[0m") |
| 52 | + md_files = get_md_files(script_directory) |
| 53 | + for file_name in md_files: |
| 54 | + file_path = os.path.join(script_directory, file_name) |
| 55 | + match = re.match(r'README_([a-zA-Z\-]+)\.md', file_name) |
| 56 | + lang_code = match.group(1) if match else "zh-CN" |
| 57 | + descriptions = generate_description(group, scripts, lang_code) |
| 58 | + process_file_plus(file_path, descriptions, start_tag, end_tag, "<!--FOOTER-->") |
| 59 | + |
| 60 | + |
41 | 61 | def main(): |
42 | 62 | json_path = 'docs/ScriptsPath.json' |
43 | 63 | data = read_json(json_path) |
44 | 64 | scripts = data.get('scripts', []) |
| 65 | + start_tag = "<!--RELATED-->" |
| 66 | + end_tag = "<!--RELATED-END-->" |
45 | 67 | for script in scripts: |
46 | | - script_directory = script.get('directory', '') |
47 | | - start_tag = "<!--RELATED-->" |
48 | | - end_tag = "<!--RELATED-END-->" |
49 | | - cnfile_path = os.path.join(script_directory, "README.md") |
50 | | - descriptions = generate_description(script, scripts, "zh-CN") |
51 | | - olddescriptions = get_file_description(cnfile_path, start_tag, end_tag) |
52 | | - if olddescriptions + "\n" == descriptions: # 换行符添加上,就这样了能用就行 |
53 | | - continue |
54 | | - else: |
55 | | - print(f"----\033[94m[{script.get('name', '')}]\033[0m\033[92m 内容变化,执行替换\033[0m") |
56 | | - md_files = get_md_files(script_directory) |
57 | | - for file_name in md_files: |
58 | | - file_path = os.path.join(script_directory, file_name) |
59 | | - match = re.match(r'README_([a-zA-Z\-]+)\.md', file_name) |
60 | | - lang_code = match.group(1) if match else "zh-CN" |
61 | | - descriptions = generate_description(script, scripts, lang_code) |
62 | | - process_file(file_path, descriptions, start_tag, end_tag, "head") |
| 68 | + process_script(script, scripts, start_tag, end_tag, script.get('group')) |
63 | 69 |
|
64 | 70 |
|
65 | 71 | if __name__ == "__main__": |
|
0 commit comments