forked from citarreikee/connectMeTrae
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_cleanup_script.js
More file actions
267 lines (230 loc) · 8.94 KB
/
data_cleanup_script.js
File metadata and controls
267 lines (230 loc) · 8.94 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
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
258
259
260
261
262
263
264
265
266
267
const fs = require('fs');
const path = require('path');
// 数据清理和修复脚本
class DataCleanupScript {
constructor() {
this.personsFile = 'd:\\Joestar\\jojorn\\connectMeTrae\\data\\persons.json';
this.tagsFile = 'd:\\Joestar\\jojorn\\connectMeTrae\\data\\tags.json';
this.iconFolder = 'd:\\Joestar\\jojorn\\connectMeTrae\\icon';
this.report = {
profileToDescription: 0,
duplicateFieldsRemoved: 0,
avatarsCleared: 0,
avatarsAdded: 0,
invalidContactsRemoved: 0,
tagConnectionsFixed: 0
};
}
// 读取JSON文件
readJsonFile(filePath) {
try {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
return null;
}
}
// 写入JSON文件
writeJsonFile(filePath, data) {
try {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
return true;
} catch (error) {
console.error(`Error writing file ${filePath}:`, error);
return false;
}
}
// 获取icon文件夹中的头像文件
getAvailableAvatars() {
try {
const files = fs.readdirSync(this.iconFolder);
const avatarMap = {};
files.forEach(file => {
const ext = path.extname(file).toLowerCase();
if (['.jpg', '.jpeg', '.png', '.gif', '.webp', '.jfif'].includes(ext)) {
const nameWithoutExt = path.basename(file, ext);
avatarMap[nameWithoutExt] = path.join(this.iconFolder, file);
}
});
return avatarMap;
} catch (error) {
console.error('Error reading icon folder:', error);
return {};
}
}
// 将图片文件转换为base64
imageToBase64(imagePath) {
try {
const imageBuffer = fs.readFileSync(imagePath);
const ext = path.extname(imagePath).toLowerCase();
let mimeType = 'image/jpeg';
switch (ext) {
case '.png': mimeType = 'image/png'; break;
case '.gif': mimeType = 'image/gif'; break;
case '.webp': mimeType = 'image/webp'; break;
case '.jfif': mimeType = 'image/jpeg'; break;
default: mimeType = 'image/jpeg';
}
return `data:${mimeType};base64,${imageBuffer.toString('base64')}`;
} catch (error) {
console.error(`Error converting image ${imagePath} to base64:`, error);
return null;
}
}
// 1. 统一字段名称:将"profile"改为"description"
fixProfileToDescription(persons) {
persons.forEach(person => {
if (person.profile && !person.description) {
person.description = person.profile;
delete person.profile;
this.report.profileToDescription++;
}
});
}
// 2. 清理重复字段:删除同时存在"profile"和"description"时的"profile"字段
removeDuplicateFields(persons) {
persons.forEach(person => {
if (person.profile && person.description) {
delete person.profile;
this.report.duplicateFieldsRemoved++;
}
});
}
// 3. 清空所有头像数据
clearAvatarData(persons) {
persons.forEach(person => {
if (person.avatar) {
person.avatar = '';
this.report.avatarsCleared++;
}
});
}
// 4. 根据icon文件夹添加头像
addAvatarsFromIcons(persons) {
const availableAvatars = this.getAvailableAvatars();
persons.forEach(person => {
if (person.name && availableAvatars[person.name]) {
const base64Avatar = this.imageToBase64(availableAvatars[person.name]);
if (base64Avatar) {
person.avatar = base64Avatar;
this.report.avatarsAdded++;
}
}
});
}
// 5. 清理无效联系人
removeInvalidContacts(persons) {
const validPersons = persons.filter(person => {
const hasValidName = person.name &&
person.name.trim() !== '' &&
person.name !== '????' &&
!person.name.includes('????');
if (!hasValidName) {
this.report.invalidContactsRemoved++;
return false;
}
return true;
});
return validPersons;
}
// 6. 修复标签连接关系
fixTagConnections(persons, tags) {
// 重新计算每个标签的连接数
const tagConnectionCounts = {};
// 初始化计数
tags.forEach(tag => {
tagConnectionCounts[tag._id] = 0;
});
// 统计实际连接数
persons.forEach(person => {
if (person.tags && Array.isArray(person.tags)) {
person.tags.forEach(tag => {
if (tagConnectionCounts.hasOwnProperty(tag._id)) {
tagConnectionCounts[tag._id]++;
}
});
}
// 检查companies和organizations中的标签
['companies', 'organizations'].forEach(field => {
if (person[field] && Array.isArray(person[field])) {
person[field].forEach(item => {
if (tagConnectionCounts.hasOwnProperty(item._id)) {
tagConnectionCounts[item._id]++;
}
});
}
});
});
// 更新标签的连接数
tags.forEach(tag => {
const newCount = tagConnectionCounts[tag._id] || 0;
if (tag.connections !== newCount) {
tag.connections = newCount;
this.report.tagConnectionsFixed++;
}
});
}
// 执行完整的数据清理
async executeCleanup() {
console.log('开始数据清理和修复...');
// 读取数据文件
const persons = this.readJsonFile(this.personsFile);
const tags = this.readJsonFile(this.tagsFile);
if (!persons || !tags) {
console.error('无法读取数据文件');
return false;
}
console.log(`读取到 ${persons.length} 个联系人和 ${tags.length} 个标签`);
// 执行清理步骤
console.log('1. 统一字段名称:profile -> description');
this.fixProfileToDescription(persons);
console.log('2. 清理重复字段');
this.removeDuplicateFields(persons);
console.log('3. 清空头像数据');
this.clearAvatarData(persons);
console.log('4. 从icon文件夹添加头像');
this.addAvatarsFromIcons(persons);
console.log('5. 清理无效联系人');
const cleanedPersons = this.removeInvalidContacts(persons);
console.log('6. 修复标签连接关系');
this.fixTagConnections(cleanedPersons, tags);
// 保存清理后的数据
console.log('保存清理后的数据...');
const personsSuccess = this.writeJsonFile(this.personsFile, cleanedPersons);
const tagsSuccess = this.writeJsonFile(this.tagsFile, tags);
if (personsSuccess && tagsSuccess) {
console.log('数据清理完成!');
this.printReport();
return true;
} else {
console.error('保存数据时出错');
return false;
}
}
// 打印修复报告
printReport() {
console.log('\n=== 数据修复报告 ===');
console.log(`profile字段改为description: ${this.report.profileToDescription} 个`);
console.log(`删除重复字段: ${this.report.duplicateFieldsRemoved} 个`);
console.log(`清空头像数据: ${this.report.avatarsCleared} 个`);
console.log(`添加新头像: ${this.report.avatarsAdded} 个`);
console.log(`删除无效联系人: ${this.report.invalidContactsRemoved} 个`);
console.log(`修复标签连接: ${this.report.tagConnectionsFixed} 个`);
console.log('==================\n');
}
}
// 执行脚本
if (require.main === module) {
const cleanup = new DataCleanupScript();
cleanup.executeCleanup().then(success => {
if (success) {
console.log('数据清理脚本执行成功!');
process.exit(0);
} else {
console.error('数据清理脚本执行失败!');
process.exit(1);
}
});
}
module.exports = DataCleanupScript;