-
Notifications
You must be signed in to change notification settings - Fork 79
/
interaction.js
681 lines (674 loc) · 24.6 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
const vscode = require('vscode')
const crypto = require('crypto')
const quickPick = vscode.window.createQuickPick()
quickPick.canSelectMany = false
quickPick.matchOnDescription = true
quickPick.matchOnDetail = true
quickPick.onDidAccept(() => {
const item = quickPick.selectedItems[0]
if (typeof item.action === 'function' && !quickPick.busy) {
quickPick.busy = true
item.action()
}
})
const selector = (items, title) => {
quickPick.busy = false
quickPick.value = ''
quickPick.items = items.filter(item => item)
quickPick.placeholder = title
quickPick.show()
}
const utility = {
extract: (item, keys = ['id', 'name']) =>
Object.keys(item).filter(key => keys.includes(key)).reduce((result, key) => Object.assign(result, { [key]: item[key] }), {}),
format: {
song: (song, source) => {
const item = utility.extract(song, ['id', 'name', 'listen'])
const album = (song.al || song.album)
item.album = utility.extract(album)
item.artists = (song.ar || song.artists).map(artist => utility.extract(artist))
item.cover = utility.stringify.uri(album.pic || album.picId) + '.jpg'
item.source = source
return item
},
program: (program, source) => {
const item = utility.extract(program, ['id', 'name', 'duration'])
item.album = utility.extract(program.radio)
item.artists = [{ id: 0, name: program.dj.nickname }]
item.create = program.createTime
item.listen = program.listenerCount
item.cover = program.coverUrl
item.source = source
return item
},
color: value => {
value = value || ''
if (value[0] === '#') {
const collen = (value.length - 1) / 3;
const fact = [17, 1, 0.062272][collen - 1];
return [
Math.round(parseInt(value.substr(1, collen), 16) * fact),
Math.round(parseInt(value.substr(1 + collen, collen), 16) * fact),
Math.round(parseInt(value.substr(1 + 2 * collen, collen), 16) * fact)
]
} else {
return (value.split('(')[1] || '').split(')')[0].split(',').map(x => +x)
}
}
},
lift: {
song: (song, index, flags, action) => {
song.label = song.name
song.action = action
if (index) {
const indicate = controller.current(song) ? '\u2006♬' : (index[0] + 1).toString()
song.label = (new Array(Math.max(index[1].toString().length, 2) - indicate.length + 1)).join('\u2007') + indicate + ' ' + song.label
}
const listen = `${utility.stringify.number(song.listen)}次播放`
song.description = flags.program
? [utility.stringify.date(song.create), listen, `(${utility.stringify.duration(song.duration)})`].join(' ')
: [flags.artist === false ? null : utility.stringify.artist(song), flags.album === false ? null : song.album.name].filter(item => item).join(' - ') +
(flags.listen ? ' ' + listen : '') + ((song.source || {}).type === 'intelligence' ? ' 〖荐〗' : '')
return song
}
},
stringify: {
uri: id => {
if (!id) return null
id = id.toString().trim()
const key = '3go8&$8*3*3h0k(2)2'
const string = Array.from(Array(id.length).keys()).map(index => String.fromCharCode(id.charCodeAt(index) ^ key.charCodeAt(index % key.length))).join('')
const result = crypto.createHash('md5').update(string).digest('base64').replace(/\//g, '_').replace(/\+/g, '-')
return `http://p1.music.126.net/${result}/${id}`
},
date: timestamp => {
if (!timestamp) return ''
const date = new Date(timestamp)
const year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate()
return `${year}.${month}.${day}`
},
interval: timestamp => {
const date = new Date(timestamp)
const 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) + '年'
},
number: value => {
if (value / 100000 >= 1)
return parseInt(value / 10000) + '万'
else
return value
},
duration: value => {
value = value / 1000
const pad = number => ('0' + number).slice(-2)
const minute = Math.floor(value / 60)
const second = Math.floor(value - minute * 60)
return `${minute}:${pad(second)}`
},
artist: item => item.artists.map(artist => artist.name).join(' / '),
song: item => `${utility.stringify.artist(item)} - ${item.name}`
},
check: {
logged: (pass, failed) => {
if (!api.user.account()) {
vscode.window.showWarningMessage('请先登录')
if (typeof failed === 'function') failed()
}
else {
if (typeof pass === 'function') pass()
}
}
},
indicate: {
expand: fill => `${fill ? '$(chevron-down)' : '$(chevron-right)'} `,
collect: done => `${done ? '〖 $(check) 已收藏 〗' : '〖 $(plus) 收藏 〗'}`
},
indent: {
expand: item => Object.assign(item, { label: `\u2003\u2005 ${item.label}` })
}
}
const interaction = {
utility,
user: {
playlist: id => api.user.playlist(id).then(data => {
id = id || data.playlist[0].creator.userId
const show = (playlist, creator) => ({
label: api.user.favorite(playlist.id) ? '我喜欢的音乐' : playlist.name,
description: `${(playlist.trackCount || 0)}首${creator ? ' by ' + playlist.creator.nickname : ''}`,
action: () => interaction.playlist.detail(playlist.id)
})
const users = data.playlist.filter(playlist => playlist.creator.userId === id)
const others = data.playlist.filter(playlist => playlist.creator.userId != id)
const playlists = (created, collected) => Array.prototype.concat(
[{
label: `${utility.indicate.expand(created)}创建的歌单`,
description: `(${users.length})`,
action: () => {
selector(playlists(!created, collected), '我的歌单')
quickPick.activeItems = [quickPick.items[0]]
}
}],
created ? users.map(playlist => show(playlist, false)).map(utility.indent.expand) : [],
[{
label: `${utility.indicate.expand(collected)}收藏的歌单`,
description: `(${others.length})`,
action: () => {
selector(playlists(created, !collected), '我的歌单')
quickPick.activeItems = [quickPick.items[(created ? users.length : 0) + 1]]
}
}],
collected ? others.map(playlist => show(playlist, true)).map(utility.indent.expand) : []
)
selector(playlists(true, true), '我的歌单')
quickPick.activeItems = [quickPick.items[1]]
}),
artist: () => api.user.artist().then(data => {
selector(data.data.map(artist => ({
label: artist.name,
description: `${artist.albumSize || 0}张专辑`,
action: () => interaction.artist.album(artist.id)
})), '我的歌手')
}),
album: () => api.user.album().then(data => {
selector(data.data.map(album => ({
label: album.name,
description: `${utility.stringify.artist(album)} ${album.size}首`,
action: () => interaction.album(album.id)
})), '我的专辑')
}),
djradio: () => api.user.djradio().then(data => {
selector(data.djRadios.map(djradio => ({
label: djradio.name,
description: `第${djradio.programCount}期 (${utility.stringify.date(djradio.lastProgramCreateTime)}) ${djradio.lastProgramName}`,
action: () => interaction.djradio.program(djradio.id)
})), '我的电台')
}),
record: all => api.user.record().then(data => {
const show = (array, week) =>
array.map(item => Object.assign(item.song, { listen: item.playCount }))
.map(song => utility.format.song(song, { type: 'record', week }))
.map((song, index, track) => utility.lift.song(song, [index, 100], { listen: true }, () => {
controller.add(track)
controller.play(index)
quickPick.hide()
}))
const label = week => week ? '最近一周' : '所有时间'
const record = week => Array.prototype.concat(
[{
label: `${label(week)}`,
description: ` ❘ ${label(!week)}`,
action: () => {
selector(record(!week), '听歌排行')
quickPick.activeItems = [quickPick.items[1]]
}
}],
show(data[week ? 'weekData' : 'allData'], week)
)
selector(record(!all), '听歌排行')
quickPick.activeItems = [quickPick.items[1]]
})
},
djradio: {
program: id => api.djradio.program(id).then(data => {
const radio = data.programs[0].radio
radio.dj = data.programs[0].dj
const refresh = () => {
selector(Array.prototype.concat(
[{
label: utility.indicate.collect(radio.subed),
description: `${utility.stringify.number(radio.subCount)}人收藏`,
action: () => utility.check.logged(api.djradio.subscribe(id, !radio.subed).then(result => {
if (result.code === 200) {
radio.subed = !radio.subed
radio.subCount += (radio.subed ? 1 : -1)
}
refresh()
}), refresh)
}],
data.programs.map(program => utility.format.program(program, { type: 'djradio', id, name: radio.name }))
.map((program, index, track) => utility.lift.song(program, [track.length - index - 1, track.length], { program: true }, () => {
controller.add(track)
controller.play(index)
quickPick.hide()
}))
), `${radio.name} by ${radio.dj.nickname}`)
quickPick.activeItems = [quickPick.items[1]]
}
refresh()
})
},
artist: {
song: id => api.artist.song(id).then(data => {
selector(data.hotSongs.map(song => utility.format.song(song, { type: 'artist', id, name: data.artist.name }))
.map((song, index, track) => utility.lift.song(song, [index, track.length], { artist: false }, () => {
controller.add(track)
controller.play(index)
quickPick.hide()
})), `${data.artist.name} 热门单曲`)
}),
album: id => api.artist.album(id).then(data => {
const refresh = () => {
selector(Array.prototype.concat(
[{
label: utility.indicate.collect(data.artist.followed),
description: `${utility.stringify.number(data.artist.fansNum)}人收藏`,
action: () => utility.check.logged(api.artist.subscribe(id, !data.artist.followed).then(result => {
if (result.code === 200) {
data.artist.followed = !data.artist.followed
data.artist.fansNum += (data.artist.followed ? 1 : -1)
}
refresh()
}), refresh)
}],
[{
label: '热门单曲',
description: `TOP 50`,
action: () => interaction.artist.song(id)
}],
data.hotAlbums.map(album => ({
label: album.name,
description: utility.stringify.date(album.publishTime),
action: () => interaction.album(album.id)
}))
), data.artist.name)
quickPick.activeItems = [quickPick.items[1]]
}
refresh()
})
},
album: id => api.album.detail(id).then(data => {
const refresh = () => {
selector(Array.prototype.concat(
[{
label: utility.indicate.collect(data.album.info.isSub),
description: `${utility.stringify.number(data.album.info.subCount)}人收藏`,
action: () => utility.check.logged(api.album.subscribe(id, !data.album.info.isSub).then(result => {
if (result.code === 200) {
data.album.info.isSub = !data.album.info.isSub
data.album.info.subCount += (data.album.info.isSub ? 1 : -1)
}
refresh()
}), refresh)
}],
data.songs.map(song => utility.format.song(song, { type: 'album', id, name: data.album.name }))
.map((song, index, track) => utility.lift.song(song, [index, track.length], { album: false }, () => {
controller.add(track)
controller.play(index)
quickPick.hide()
}))
), data.album.name)
quickPick.activeItems = [quickPick.items[1]]
}
refresh()
}),
toplist: () => api.toplist().then(data => {
selector(data.list.map(playlist => ({
label: playlist.name,
description: `${utility.stringify.interval(playlist.updateTime)}前更新`,
action: () => interaction.playlist.detail(playlist.id)
})), '排行榜')
}),
playlist: {
detail: id => api.playlist.detail(id).then(data => {
const self = api.user.account(data.playlist.creator.userId)
const name = api.user.favorite(id) ? '我喜欢的音乐' : data.playlist.name
const refresh = () => {
selector(Array.prototype.concat(
self
? []
: [{
label: utility.indicate.collect(data.playlist.subscribed),
description: `${utility.stringify.number(data.playlist.subscribedCount)}人收藏`,
action: () => utility.check.logged(api.playlist.subscribe(id, !data.playlist.subscribed).then(result => {
if (result.code === 200) {
data.playlist.subscribed = !data.playlist.subscribed
data.playlist.subscribedCount += (data.playlist.subscribed ? 1 : -1)
}
refresh()
}), refresh)
}],
data.playlist.tracks.map(song => utility.format.song(song, { type: 'playlist', id, name }))
.map((song, index, track) => utility.lift.song(song, [index, track.length], {}, () => {
controller.add(track)
controller.play(index, true, true)
quickPick.hide()
}))
), `${name} by ${data.playlist.creator.nickname}`)
quickPick.activeItems = [quickPick.items[self ? 0 : 1]]
}
refresh()
}),
hot: () => api.playlist.hot().then(data => {
selector(data.playlists.map(playlist => ({
label: playlist.name,
description: `by ${playlist.creator.nickname} ${utility.stringify.number(playlist.playCount)}次播放`,
action: () => interaction.playlist.detail(playlist.id)
})), '热门歌单')
}),
highquality: () => api.playlist.highquality().then(data => {
selector(data.playlists.map(playlist => ({
label: playlist.name,
description: `by ${playlist.creator.nickname} ${utility.stringify.number(playlist.playCount)}次播放`,
action: () => interaction.playlist.detail(playlist.id)
})), '精品歌单')
})
},
recommend: {
song: () => api.recommend.song().then(data => {
selector(data.recommend.map(song => utility.format.song(song, { type: 'recommend' }))
.map((song, index, track) => utility.lift.song(song, [index, track.length], {}, () => {
controller.add(track)
controller.play(index)
quickPick.hide()
})), '每日歌曲推荐')
}),
playlist: () => api.recommend.playlist().then(data => {
selector(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(song => utility.format.song(song, { type: 'radio' })), true)
controller.play(0)
})
},
new: {
song: () => api.new.song().then(data => {
selector(data.data.map(song => utility.format.song(song, { type: 'new' }))
.map(song => utility.lift.song(song, null, {}, () => {
controller.add(song)
controller.play()
quickPick.hide()
})), '新歌速递')
}),
album: () => api.new.album().then(data => {
selector(data.albums.map(album => ({
label: album.name,
description: `${utility.stringify.artist(album)} ${utility.stringify.date(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.userPoint.userId})`)})
.then(() => controller.refresh())
.catch(e => {vscode.window.showErrorMessage(`登录失败: ${e.code == 502 ? '账号或密码错误' : '未知错误'}(${e.code})`)})
}
})
})
},
clone: () => {
vscode.window.showInputBox({
placeHolder: '"MUSIC_U"的值',
prompt: '从浏览器开发者工具复制Cookie'
})
.then(value => {
if (value) {
api.refresh(`MUSIC_U=${value.trim()}`)
.then(data => {vscode.window.showInformationMessage(`登录成功: ${data.profile.nickname}(${data.userPoint.userId})`)})
.then(() => controller.refresh())
.catch(e => {vscode.window.showErrorMessage(`登录失败: Cookie 无效`)})
}
})
},
logout: () => (api.logout(), runtime.playerBar.state('dislike')),
sign: () => api.sign().then(data => {
if ([-2, 200].includes(data.code)) {
vscode.window.showInformationMessage('签到成功')
runtime.stateManager.set('signed', true)
}
}),
list: {
show: () => {
const track = controller.list()
const play = track.findIndex(song => song.play)
selector(track.map((song, index) => utility.lift.song(song, [index, track.length], {}, () => {
song.play ? (controller.pause() || controller.resume()) : controller.play(index)
quickPick.hide()
})), `播放列表 (${track.length})`)
quickPick.activeItems = [quickPick.items[play]]
},
edit: () => {
const track = controller.list()
const play = track.findIndex(song => song.play)
selector(track.map((song, index) => utility.lift.song(song, [index, track.length], {}, () => {
controller.remove(index)
if (index == play) controller.play(undefined, runtime.stateManager.get('playing'))
interaction.list.edit()
})), `编辑播放列表 (${track.length})`)
}
},
search: () => {
let hot = [], timer = 0, 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 => {
const 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()
const code = { song: 1, artist: 100, album: 10, playlist: 1000, djradio: 1009 }[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 songs = (data.result.songs || []).map(song => utility.format.song(song, { type: 'search' }))
.map(song => utility.lift.song(song, null, {}, () => {
controller.add(song)
controller.play()
quickPick.hide()
}))
const artists = (data.result.artists || []).map(artist => ({
label: artist.name,
action: () => interaction.artist.album(artist.id)
}))
const albums = (data.result.albums || []).map(album => ({
label: album.name,
description: `${album.artist.name} ${album.size}首`,
action: () => interaction.album(album.id)
}))
const playlists = (data.result.playlists || []).map(playlist => ({
label: playlist.name,
description: `${utility.stringify.number(playlist.playCount)}次播放 ${utility.stringify.number(playlist.bookCount)}次收藏`,
action: () => interaction.playlist.detail(playlist.id)
}))
const djradios = (data.result.djRadios || []).map(djradio => ({
label: djradio.name,
description: `by ${djradio.dj.nickname}`,
action: () => interaction.djradio.program(djradio.id)
}))
selector(Array.prototype.concat(
[{
label: `${utility.indicate.expand(songs.length)}单曲`,
action: type != 'song' ? () => search(text, 'song') : null
}],
songs.map(utility.indent.expand),
[{
label: `${utility.indicate.expand(artists.length)}歌手`,
action: type != 'artist' ? () => search(text, 'artist') : null
}],
artists.map(utility.indent.expand),
[{
label: `${utility.indicate.expand(albums.length)}专辑`,
action: type != 'album' ? () => search(text, 'album') : null
}],
albums.map(utility.indent.expand),
[{
label: `${utility.indicate.expand(playlists.length)}歌单`,
action: type != 'playlist' ? () => search(text, 'playlist') : null
}],
playlists.map(utility.indent.expand),
[{
label: `${utility.indicate.expand(djradios.length)}电台`,
action: type != 'djradio' ? () => search(text, 'djradio') : null
}],
djradios.map(utility.indent.expand)
), `“${text}”的${{ song: '歌曲', artist: '歌手', album: '专辑', playlist: '歌单', djradio: '电台' }[type] || ''}搜索结果`)
quickPick.activeItems = [quickPick.items[{ song: 0, artist: 1, album: 2, playlist: 3, djradio: 4 }[type] || 0]]
}
api.search.hot().then(data => {
hot = data.result.hots.map(item => ({ label: item.first })).map(operation)
selector(hot, '搜索歌曲、歌手、专辑、歌单')
autoComplete = quickPick.onDidChangeValue(() => {
clearTimeout(timer)
timer = setTimeout(suggest, 250)
})
})
},
more: () => {
const song = controller.list().find(song => song.play)
const program = song.source.type === 'djradio'
selector([
program
? null
: {
label: `专辑: ${song.album.name}`,
action: () => interaction.album(song.album.id)
},
program
? null
: {
label: `歌手: ${utility.stringify.artist(song)}`,
action: () => song.artists.length > 1 ? selector(song.artists.map(artist => ({
label: artist.name,
action: () => interaction.artist.album(artist.id)
})), '查看歌手') : interaction.artist.album(song.artists[0].id)
},
!song.source
? null
: ({
playlist: {
label: `来源: 歌单「${song.source.name}」`,
action: () => interaction.playlist.detail(song.source.id)
},
album: {
label: `来源: 专辑「${song.source.name}」`,
action: () => interaction.album(song.source.id)
},
artist: {
label: `来源: ${song.source.name}的「热门单曲」`,
action: () => interaction.artist.song(song.source.id)
},
recommend: {
label: `来源: 每日推荐`,
action: api.user.account() ? () => interaction.recommend.song() : null
},
new: {
label: `来源: 新歌速递`,
action: () => interaction.new.song()
},
record: {
label: `来源: 听歌排行`,
action: api.user.account() ? () => interaction.user.record(!song.source.week) : null
},
djradio: {
label: `电台节目: ${song.source.name}`,
action: () => interaction.djradio.program(song.source.id)
},
radio: {
label: `来源: 私人 FM`
},
search: {
label: `来源: 搜索`
},
program: {
label: `来源: 节目包含歌曲`
},
intelligence: {
label: `来源: 心动模式`
}
}[song.source.type]),
!program
? null
: {
label: '节目包含歌曲',
action: () => api.program.detail(song.id).then(data => {
selector(data.program.songs.map(song => utility.format.song(song, { type: 'program' }))
.map((song, index, track) => utility.lift.song(song, [index, track.length], {}, () => {
controller.add(track)
controller.play(index)
quickPick.hide()
})), `${song.name} 包含歌曲(${data.program.songs.length})`)
})
},
{
label: `查看评论`,
action: () => api[program ? 'program' : 'song'].comment(song.id).then(data => {
const hot = (data.hotComments || []).length
selector(Array.prototype.concat(
hot ? [{
label: '精彩评论',
}] : null,
hot ? (data.hotComments || []).map(comment => ({
label: `${hot ? '\u2003' : ''}${utility.stringify.number(comment.likedCount)} $(heart)`,
description: comment.content,
})) : null,
hot ? [{
label: '最新评论',
}] : null,
(data.comments || []).map(comment => ({
label: `${hot ? '\u2003' : ''}${utility.stringify.number(comment.likedCount)} $(heart)`,
description: comment.content,
}))
), `${hot ? '' : '最新'}评论 (${data.total})`)
})
},
program || !api.user.account()
? null
: {
label: '收藏到歌单',
action: () => api.user.playlist().then(data => data.playlist).then(playlists => {
selector(playlists.filter(playlist => playlist.creator.userId === playlists[0].creator.userId)
.map(playlist => ({
label: api.user.favorite(playlist.id) ? '我喜欢的音乐' : playlist.name,
description: `${(playlist.trackCount || 0)}首`,
action: () => api.song.collect(song.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()
})
})), '添加到歌单')
})
},
{
label: '在浏览器中打开',
action: () => vscode.env.openExternal(vscode.Uri.parse(`https://music.163.com/#/${program ? 'program' : 'song'}?id=${song.id}`)) && quickPick.hide()
}
], `正在播放: ${utility.stringify.song(song)}`)
}
}
module.exports = interaction
const api = require('./request.js')
const runtime = require('./runtime.js')
const controller = require('./controller.js')