forked from nondanee/vsc-netease-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interaction.js
415 lines (402 loc) · 14 KB
/
interaction.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
const vscode = require('vscode')
const quickPick = vscode.window.createQuickPick()
quickPick.canSelectMany = false
quickPick.matchOnDescription = true
quickPick.matchOnDetail = true
quickPick.onDidAccept(() => {
quickPick.busy = true
let item = quickPick.selectedItems[0]
if (typeof item.action === 'function') item.action()
})
const fillQuickPick = (items, title) => {
quickPick.busy = false
quickPick.value = ''
quickPick.items = items
quickPick.placeholder = title
quickPick.show()
}
const alignCenter = (string, space) => {
space = space < 4 ? 4 : space
let idle = space - string.toString().length * 2 * (isNaN(string) ? 2 : 1)
let blank = Array(idle / 2 + 1).join(' ')
return blank + string + blank
}
const addIndex = (item, index, all) => {
let space = all.length.toString().length * 2
item.index = index
item.label = ` ${alignCenter(item.play ? '♬' : (index + 1), space)} ${item.label}`
return item
}
const songFormat = song => ({
id: song.id,
name: song.name,
album: (song.al || song.album).name,
artist: (song.ar || song.artists).map(artist => artist.name).join(' / ')
})
const songDisplay = item => {
item.label = item.name
item.description = `${item.artist} - ${item.album}`
return item
}
const dateFormat = timestamp => {
if (!timestamp) return ''
let date = new Date(timestamp)
let year = date.getFullYear()
let month = date.getMonth() + 1
let day = date.getDate()
return `${year}.${month}.${day}`
}
const numberReadable = number => {
if (number / 100000 >= 1)
return parseInt(number / 10000) + '万'
else
return number
}
const dateFriendly = timestamp => {
let date = new Date(timestamp)
let delta = parseInt((Date.now() - date) / 1000)
if (delta < 60)
return '刚刚'
else if (delta < 3600)
return parseInt(delta / 60) + '分钟'
else if (delta < 86400)
return parseInt(delta / 3600) + '小时'
else if (delta < 2592000)
return parseInt(delta / 86400) + '天'
else if (delta < 31536000)
return parseInt(delta / 2592000) + '月'
else
return parseInt(delta / 31536000) + '年'
}
const interaction = {
user: {
playlist: id => api.user.playlist(id).then(data => {
id = id || data.playlist[0].creator.userId
const show = (playlist, creator) => ({
label: ' ' + playlist.name,
description: `${(playlist.trackCount || 0)}首${creator ? ' by ' + playlist.creator.nickname : ''}`,
action: () => interaction.playlist.detail(playlist.id)
})
let users = data.playlist.filter(playlist => playlist.creator.userId === id)
let others = data.playlist.filter(playlist => playlist.creator.userId != id)
const playlists = (created, collected) => Array.from([]).concat(
[{
label: `${created ? '▿' : '▹'} 创建的歌单(${users.length})`,
action: () => {
fillQuickPick(playlists(!created, collected), '我的歌单')
quickPick.activeItems = [quickPick.items[0]]
}
}],
created ? users.map(playlist => show(playlist, false)) : [],
[{
label: `${collected ? '▿' : '▹'} 收藏的歌单(${others.length})`,
action: () => {
fillQuickPick(playlists(created, !collected), '我的歌单')
quickPick.activeItems = [quickPick.items[(created ? users.length : 0) + 1]]
}
}],
collected ? others.map(playlist => show(playlist, true)) : []
)
fillQuickPick(playlists(true, true), '我的歌单')
quickPick.activeItems = [quickPick.items[1]]
}),
artist: () => api.user.artist().then(data => {
fillQuickPick(data.data.map(artist => ({
label: artist.name,
description: `${artist.albumSize || 0}张专辑`,
action: () => interaction.artist.album(artist.id)
})), '我的歌手')
}),
album: () => api.user.album().then(data => {
fillQuickPick(data.data.map(album => ({
label: album.name,
description: `${album.artists.map(artist => artist.name).join(' / ')} ${album.size}首`,
action: () => interaction.album(album.id)
})), '我的专辑')
})
},
artist: {
song: id => api.artist.song(id).then(data => {
fillQuickPick(data.hotSongs.map(songFormat).map(songDisplay).map(addIndex)
.map((song, index, track) => Object.assign(song, {
description: song.album,
action: () => {
controller.add(track)
controller.play(index)
quickPick.hide()
}
})), `${data.artist.name} 热门单曲`)
}),
album: id => api.artist.album(id).then(data => {
fillQuickPick([{
label: '热门单曲',
description: 'TOP 50',
action: () => interaction.artist.song(id)
}].concat(data.hotAlbums.map(album => ({
label: album.name,
description: dateFormat(album.publishTime),
action: () => interaction.album(album.id)
}))), data.artist.name)
})
},
album: id => api.album(id).then(data => {
fillQuickPick(data.songs.map(songFormat).map(songDisplay).map(addIndex)
.map((song, index, track) => Object.assign(song, {
description: song.artist,
action: () => {
controller.add(track)
controller.play(index)
quickPick.hide()
}
})), data.album.name)
}),
toplist: () => api.toplist().then(data => {
fillQuickPick(data.list.map(playlist => ({
label: playlist.name,
description: `${dateFriendly(playlist.updateTime)}前更新`,
action: () => interaction.playlist.detail(playlist.id)
})), '排行榜')
}),
playlist: {
detail: id => api.playlist.detail(id).then(data => {
fillQuickPick(data.playlist.tracks.map(songFormat).map(songDisplay).map(addIndex)
.map((song, index, track) => Object.assign(song, {
action: () => {
controller.add(track)
controller.play(index)
quickPick.hide()
}
})), `${data.playlist.name} by ${data.playlist.creator.nickname}`)
}),
hot: () => api.playlist.hot().then(data => {
fillQuickPick(data.playlists.map(playlist => ({
label: playlist.name,
description: `by ${playlist.creator.nickname} ${numberReadable(playlist.playCount)}次播放`,
action: () => interaction.playlist.detail(playlist.id)
})), '热门歌单')
}),
highquality: () => api.playlist.highquality().then(data => {
fillQuickPick(data.playlists.map(playlist => ({
label: playlist.name,
description: `by ${playlist.creator.nickname} ${numberReadable(playlist.playCount)}次播放`,
action: () => interaction.playlist.detail(playlist.id)
})), '精品歌单')
})
},
recommend: {
song: () => api.recommend.song().then(data => {
fillQuickPick(data.recommend.map(songFormat).map(songDisplay).map(addIndex)
.map((song, index, track) => Object.assign(song, {
action: () => {
controller.add(track)
controller.play(index)
quickPick.hide()
}
})), '每日歌曲推荐')
}),
playlist: () => api.recommend.playlist().then(data => {
fillQuickPick(data.result.map(playlist => ({
label: playlist.name,
description: playlist.copywriter,
action: () => interaction.playlist.detail(playlist.id)
})), '推荐歌单')
}),
radio: () => api.recommend.radio().then(data => {
controller.add(data.data.map(songFormat), true)
controller.play(0)
})
},
new: {
song: () => api.new.song().then(data => {
fillQuickPick(data.data.map(songFormat).map(songDisplay)
.map(song => Object.assign(song, {
action: () => {
controller.add(song)
controller.play()
quickPick.hide()
}
})), '新歌速递')
}),
album: () => api.new.album().then(data => {
fillQuickPick(data.albums.map(album => ({
label: album.name,
description: `${album.artists.map(artist => artist.name).join(' / ')} ${dateFormat(album.publishTime)}`,
action: () => interaction.album(album.id)
})), '新碟上架')
})
},
login: () => {
vscode.window.showInputBox({
placeHolder: "邮箱或手机号",
prompt: "请输入网易云账号"
})
.then(account => {
vscode.window.showInputBox({
password: true,
prompt: "请输入密码"
})
.then(password => {
if (account && password) {
api.login(account, password)
.then(data => {vscode.window.showInformationMessage(`登录成功: ${data.profile.nickname}(${data.account.id})`)})
.then(() => controller.refresh())
.catch(e => {vscode.window.showErrorMessage(`登录失败: ${e.code == 502 ? '账号或密码错误' : '未知错误'}(${e.code})`)})
}
})
})
},
logout: () => api.logout(),
list: {
show: () => {
let track = controller.list().map(songDisplay).map(addIndex)
let play = track.findIndex(song => song.play)
fillQuickPick(track.map((song, index) => Object.assign(song, {
action: () => {
song.play ? (controller.pause() || controller.resume()) : controller.play(index)
quickPick.hide()
}
})), `播放列表 (${track.length})`)
quickPick.activeItems = [quickPick.items[play]]
},
edit: () => {
let track = controller.list().map(songDisplay).map(addIndex)
let play = track.findIndex(song => song.play)
fillQuickPick(track.map((song, index) => Object.assign(song, {
action: () => {
controller.remove(index)
if (index == play) controller.play()
interaction.list.edit()
}
})), `编辑播放列表 (${track.length})`)
}
},
search: () => {
let hot = []
let timer = 0
let autoComplete = {}
const operation = item => Object.assign(item, {alwaysShow: true, action: () => search(item.label)})
const suggest = () => {
const value = quickPick.value
if (!value) quickPick.items = hot
else api.search.keyword(value).then(data => {
let items = (data.result && data.result.allMatch) ? data.result.allMatch.map(item => ({label: item.keyword})) : []
if (!items.length || items[0].label != value) items.unshift({label: value})
quickPick.items = items.map(operation)
})
}
const search = (text, type) => {
autoComplete.dispose()
let code = {song: 1, artist: 100, album: 10, playlist: 1000}[type]
if (!code) api.search.suggest(text).then(data => display(text, data))
else api.search.type(text, code).then(data => display(text, data, type))
}
const display = (text, data, type) => {
const indent = item => Object.assign(item, {label: ' ' + item.label})
let songs = (data.result.songs || []).map(songFormat).map(songDisplay).map(song => Object.assign(song, {
action: () => {
controller.add(song)
controller.play()
quickPick.hide()
}
}))
let artists = (data.result.artists || []).map(artist => ({
label: artist.name,
action: () => interaction.artist.album(artist.id)
}))
let albums = (data.result.albums || []).map(album => ({
label: album.name,
description: `${album.artist.name} ${album.size}首`,
action: () => interaction.album(album.id)
}))
let playlists = (data.result.playlists || []).map(playlist => ({
id: playlist.id,
label: playlist.name,
description: `${numberReadable(playlist.playCount)}次播放 ${numberReadable(playlist.bookCount)}次收藏`,
action: () => interaction.playlist.detail(playlist.id)
}))
fillQuickPick(Array.from([]).concat(
[{
label: `${songs.length ? '▿' : '▹'} 单曲`,
action: type != 'song' ? () => search(text, 'song') : null
}],
songs.map(indent),
[{
label: `${artists.length ? '▿' : '▹'} 歌手`,
action: type != 'artist' ? () => search(text, 'artist') : null
}],
artists.map(indent),
[{
label: `${albums.length ? '▿' : '▹'} 专辑`,
action: type != 'album' ? () => search(text, 'album') : null
}],
albums.map(indent),
[{
label: `${playlists.length ? '▿' : '▹'} 歌单`,
action: type != 'playlist' ? () => search(text, 'playlist') : null
}],
playlists.map(indent)
), `“${text}”的${{song: '歌曲', artist: '歌手', album: '专辑', playlist: '歌单'}[type] || ''}搜索结果`)
quickPick.activeItems = [quickPick.items[{song: 0, artist: 1, album: 2, playlist: 3}[type] || 0]]
}
api.search.hot().then(data => {
hot = data.result.hots.map(item => ({label: item.first})).map(operation)
fillQuickPick(hot, '搜索歌曲、歌手、专辑、歌单')
autoComplete = quickPick.onDidChangeValue(() => {
clearTimeout(timer)
timer = setTimeout(suggest, 250)
})
})
},
more: () => {
let id = controller.list().find(song => song.play).id
api.song.detail(id).then(data => data.songs[0]).then(data => {
fillQuickPick([
{
label: `专辑: ${data.al.name}`,
action: () => interaction.album(data.al.id)
},
{
label: `歌手: ${data.ar.map(artist => artist.name).join(' / ')}`,
action: () => data.ar.length > 1 ? fillQuickPick(data.ar.map(artist => ({
label: artist.name,
action: () => interaction.artist.album(artist.id)
})), '查看歌手') : interaction.artist.album(data.ar[0].id)
},
{
label: '查看热门评论',
action: () => api.song.comment(id).then(data => {
fillQuickPick(data.hotComments.map(comment => ({
label: `${numberReadable(comment.likedCount)} $(heart)`,
description: comment.content,
// action: () => comment.commentId
})), `热门评论 (${data.hotComments.length})`)
})
},
runtime.stateManager.get('logged') ? {
label: "收藏到歌单",
action: () => api.user.playlist().then(data => data.playlist).then(playlists => {
fillQuickPick(playlists.filter(playlist => playlist.creator.userId === playlists[0].creator.userId)
.map(playlist => ({
label: playlist.name,
description: `${(playlist.trackCount || 0)}首`,
action: () => api.song.collect(id, playlist.id).then(data => {
if (data.code === 200) vscode.window.showInformationMessage('收藏成功')
else if (data.code === 502) vscode.window.showWarningMessage('歌曲已存在')
else vscode.window.showWarningMessage(`未知错误(${data.code})`)
quickPick.hide()
})
})), '添加到歌单')
})
} : null,
{
label: '在浏览器中打开',
action: () => vscode.env.openExternal(vscode.Uri.parse(`https://music.163.com/#/song?id=${id}`)) && quickPick.hide()
}
].filter(item => item), `正在播放: ${data.ar.map(artist => artist.name).join(' / ')} - ${data.name}`)
})
}
}
module.exports = interaction
const api = require('./request.js')
const runtime = require('./runtime.js')
const controller = require('./controller.js')