-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch_parse.js
More file actions
68 lines (60 loc) · 2.16 KB
/
test_batch_parse.js
File metadata and controls
68 lines (60 loc) · 2.16 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
const { execSync } = require('child_process');
const path = require('path');
async function queryArtist(artistName, limit) {
return new Promise((resolve) => {
try {
const scriptPath = path.join(__dirname, 'scripts/music/hot_songs.js');
const cmd = `node "${scriptPath}" --artist "${artistName}" -n ${limit} --json`;
const output = execSync(cmd, {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe']
});
try {
// 改进的 JSON 解析逻辑
const lines = output.split('\n');
let jsonLines = [];
let inJson = false;
let braceCount = 0;
for (const line of lines) {
if (!inJson && line.includes('{')) {
inJson = true;
}
if (inJson) {
jsonLines.push(line);
braceCount += (line.match(/\{/g) || []).length;
braceCount -= (line.match(/\}/g) || []).length;
if (braceCount === 0 && jsonLines.length > 0) {
break;
}
}
}
if (jsonLines.length > 0) {
const jsonStr = jsonLines.join('\n');
const data = JSON.parse(jsonStr);
resolve(data);
} else {
resolve(null);
}
} catch (e) {
console.error('解析错误:', e.message);
resolve(null);
}
} catch (e) {
console.error('执行错误:', e.message);
resolve(null);
}
});
}
// 测试
(async () => {
const result = await queryArtist('周深', 10);
if (result && result.songs) {
console.log(`✅ 成功找到 ${result.songs.length} 首歌曲`);
console.log('前3首:');
result.songs.slice(0, 3).forEach((s, i) => {
console.log(` ${i+1}. ${s.name}`);
});
} else {
console.log('❌ 未找到歌曲');
}
})();