-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathmerge.py
More file actions
137 lines (116 loc) · 4.04 KB
/
merge.py
File metadata and controls
137 lines (116 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
# coding=utf-8
# Created By Celeter
import json, shutil, datetime, os
source_type = [
{
'Type': '图片',
'Value': 0
},
{
'Type': '文字',
'Value': 1
},
{
'Type': '视频',
'Value': 2
},
{
'Type': '音频',
'Value': 3
}
]
def log(msg):
time = datetime.datetime.now()
print('[' + time.strftime('%Y.%m.%d %H:%M:%S') + '] ' + msg)
def read_file(f_path: str):
with open(f_path, encoding='utf-8') as f:
result = f.read()
return result
def write_file(f_path: str, src):
data = json.dumps(src, indent=2, ensure_ascii=False)
with open(f_path, 'w', encoding='utf-8') as f:
f.write(data)
# 创建文件路径
def create_dir(name: str):
if not os.path.exists(name):
os.makedirs(name)
# 获取文件夹的路径列表
def get_folder_list(folder_dir: str):
dir_list = os.listdir(folder_dir)
fold_list = []
for d in dir_list:
sub_dir = os.path.join(folder_dir, d)
if os.path.isdir(sub_dir):
fold_list.append(sub_dir)
return fold_list
# 获取文件的名称列表
def get_file_list(folder_dir: str):
dir_list = os.listdir(folder_dir)
f_list = []
for d in dir_list:
sub_dir = os.path.join(folder_dir, d)
if os.path.isfile(sub_dir):
f_list.append(d)
return f_list
# 创建所有要用到的文件夹
def create_folder(import_dir: str):
for src in source_type:
type_dir = os.path.join(import_dir, src.get('Type'))
create_dir(type_dir)
create_dir(os.path.join(type_dir, '失效'))
log('所需文件夹已生成完毕')
# 源移到对应文件夹
def sort(import_dir: str):
f_list = get_file_list(import_dir)
# 源分类
for file in f_list:
if file.endswith('.gitignore') or file.endswith('.sh') or file.endswith('.md') or file.endswith('manifest'):
pass
else:
f_dir = os.path.join(import_dir, file)
obj = json.loads(read_file(f_dir))
for src in source_type:
if obj.get('contentType') == src.get('Value'):
if file.find('失效') > -1 or not file.endswith('.json'):
shutil.move(f_dir, os.path.join(import_dir, src.get('Type'), '失效', file))
else:
shutil.move(f_dir, os.path.join(import_dir, src.get('Type'), file))
log('所有源已移动到对应分类文件夹')
# 合并分类中的源
def merge(import_dir: str):
source_list = []
for src in source_type:
type_dir = os.path.join(import_dir, src.get('Type'))
f_list = get_file_list(type_dir)
src_list = []
for file in f_list:
if file.endswith('.md') or file == 'sub.json':
pass
else:
fileContent = read_file(os.path.join(type_dir, file)).strip()
if fileContent.startswith("eso"):
src_list.append(fileContent.strip())
source_list.append(fileContent.strip())
else:
contentJSON = json.loads(fileContent)
src_list.append(contentJSON)
source_list.append(contentJSON)
log('{}源:{}个'.format(src.get('Type'), len(src_list)))
write_file(os.path.join(type_dir, 'sub.json'), src_list)
log('子分类文件{}写入完毕'.format(os.path.join(type_dir, 'sub.json')))
log('所有源:{}个'.format(len(source_list)))
write_file(os.path.join(import_dir, 'manifest'), source_list)
log('所有源合并完毕,已写入文件{}'.format(os.path.join(import_dir, 'manifest')))
if __name__ == '__main__':
# 工作路径
# import_path = os.environ.get('GITHUB_WORKSPACE')
import_path = "."
if import_path is None:
log('工作路径为空,请更改路径后再执行')
else:
# 创建所有要用到的文件夹
create_folder(import_path)
# 源分类
# sort(import_path)
# 源合并
merge(import_path)