-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
126 lines (116 loc) · 3.63 KB
/
index.js
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
const qqMusic = require("qq-music-api")
const inquirer = require('inquirer')
const fs = require('node:fs')
const { exit } = require("node:process")
async function pauseAndExit(code) {
try {
await inquirer.prompt([
{
type: 'input',
name: 'ans',
message: '程序结束. 按 Enter 退出.'
}
])
} finally {
exit(code)
}
}
async function loadConfig() {
try {
if (!fs.existsSync("config.json")) {
fs.writeFileSync("config.json", JSON.stringify({
cookies: "", dirid: 201
}, null, 4))
console.log("配置文件不存在, 已创建默认配置文件")
await pauseAndExit(0)
} else {
let config = JSON.parse(fs.readFileSync("config.json", 'utf8'))
if (!config.cookies || !config.dirid) {
console.error("配置文件未填写或内容缺失, 请检查后重试")
await pauseAndExit(1)
}
return config
}
} catch (error) {
console.error("处理配置文件时发生错误: ", error)
await pauseAndExit(1)
}
}
async function loadSongList() {
try {
let answers = await inquirer.prompt([{
type: 'input',
name: 'current_file',
message: '音乐列表文件路径: '
}])
let current_file = answers.current_file.replace(/^"|"$/g, '')
let fileContents = fs.readFileSync(current_file, 'utf8')
return fileContents.split('\n');
} catch (error) {
console.error("读取音乐列表文件时发生错误: ", error)
await pauseAndExit(1)
}
}
async function getSongs(fullName) {
try {
var songs = []
var result = await qqMusic.api("search", { key: fullName, pageSize: 10 })
var songList = result.list
for (var i = 0; i < songList.length; i++) {
var song = songList[i]
var name = song.songname
var singers = song.singer.map(singer => singer.name).join("/")
var mid = song.songmid
var fullLine = `${name} - ${singers}`
songs.push({ name: fullLine, value: mid })
}
if (songs.length == 0) {
return [{ name: "未找到此歌曲", value: 0 }]
}
songs.push(new inquirer.Separator())
songs.push({ name: "没有我想要的歌曲", value: 0 })
return songs
} catch (error) {
console.log(error)
return [{ name: "搜索时发生错误", value: 0 }]
}
}
async function chooseSong(song, dirid) {
var fullName = song
var choices = await getSongs(fullName)
var answers = await inquirer.prompt([
{
type: 'list',
message: `请指定: ${fullName}`,
name: 'ans',
choices: choices,
pageSize: 15
}
])
if (answers.ans == 0) {
console.log("未找到此歌曲")
return
}
try {
res = await qqMusic.api('songlist/add', { mid: answers.ans, dirid: dirid })
if (res !== undefined) {
console.log(res)
}
} catch (error) {
console.error('发生错误: ', error)
return
}
}
async function main() {
let config = await loadConfig()
let songList = await loadSongList()
songList = songList.reverse()
qqMusic.setCookie(config.cookies)
for (let i = 0; i < songList.length; i++) {
const song = songList[i]
await chooseSong(song, config.dirid)
fs.writeFileSync("lastLeft.txt", songList.slice(i + 1).join('\n'))
}
await pauseAndExit(0)
}
main()