-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
479 lines (406 loc) Β· 16 KB
/
app.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
const os = require('os')
const fs = require('fs')
const sha1 = require('sha1')
const path = require('path')
const colors = require('colors')
const rimraf = require('rimraf')
const fad = require('fast-array-diff')
const randomInt = require('random-int')
const hasher = require('node-object-hash')()
const { default: PQueue } = require('p-queue')
const api = require('./source/api')
const lyric = require('./source/lyric')
const config = require('./source/config')
const logger = require('./source/logger')
const general = require('./source/general')
const metadata = require('./source/metadata')
const that = {
downloaded: new Set(),
downloadedFormat: {}
}
const uuid = require('uuid').v4
const tmpBasePath = path.resolve(os.tmpdir(), 'Azusa/')
let __root = ''
if (fs.existsSync(path.resolve('MUSIC'))) __root = path.resolve('MUSIC')
else __root = path.resolve('../MUSIC')
if (!fs.existsSync(__root = path.resolve(__root, 'Azusa/MUSIC'))) {
fs.mkdirSync(__root, {
recursive: true
})
}
if (!fs.existsSync(path.resolve(__root, '.azusa/'))) {
fs.mkdirSync(path.resolve(__root, '.azusa/'))
}
// Get downloaded list
{
logger.info('Getting downloaded tracks...')
const dirlist = fs.readdirSync(__root)
for (const dirname of dirlist) {
if (dirname.startsWith('.')) continue
const filelist = fs.readdirSync(path.resolve(__root, dirname))
for (const filename of filelist) {
if (filename.startsWith('._')) continue
const trackId = parseInt(filename.split('.')[0], 10)
if (filename.endsWith('.mp3') || filename.endsWith('.flac')) that.downloaded.add(trackId)
if (filename.endsWith('.mp3')) that.downloadedFormat[trackId] = 'mp3'
else if (filename.endsWith('.flac')) that.downloadedFormat[trackId] = 'flac'
}
}
}
(async () => {
await new Promise((resolve, reject) => {
if (fs.existsSync(tmpBasePath)) {
rimraf(tmpBasePath, (error) => {
if (error) reject(error)
else resolve()
})
} else resolve()
})
// Login to Cloudmusic
logger.info('Logging in to Cloudmusic')
await api.login(config('phone'), config('password'), config('saveCookie', true) ? path.resolve('account.json') : '')
// Generate infomation for downloading
const playlistList = []
const trackList = {}
const unfoundTrackList = {}
{
// Generate for playlist(ζε)
{
logger.info('Requesting user\'s playlists')
const playlist = await api.getUserPlaylist()
const list = new Set()
for (const plist of playlist) list.add(plist.id)
const extraPlaylist = config('extraPlaylist', [])
extraPlaylist.forEach((item) => list.add(parseInt(item.trim(), 10)))
const excludePlaylist = config('excludePlaylist', [])
excludePlaylist.forEach((item) => list.delete(parseInt(item.trim(), 10)))
for (const playlistId of list) {
const playlistInfo = await api.getPlaylistInfo(playlistId)
const trackIds = []
for (const track of playlistInfo.trackIds) {
trackList[track.id] = {}
trackIds.push(parseInt(track.id, 10))
}
// ε€ηζζΎε¨ζεεε¨
const playlistName = config('prefix', []).playlist + playlistInfo.name
const syncConfig = config('syncPlaylist', [])
const needSync = (playlistInfo.creator.userId === api._uid) && (syncConfig.includes(playlistId) || syncConfig.includes('all'))
if (needSync) {
const filePath = path.resolve(__root, '.azusa/') + `/${playlistId}.json`
const playlistPath = path.resolve(__root, '../..', general.replaceChar(playlistName) + ' .m3u')
if (fs.existsSync(filePath) && fs.existsSync(playlistPath)) {
const oldTrackIds = JSON.parse(fs.readFileSync(filePath).toString())
const playlistData = fs.readFileSync(playlistPath).toString()
const modifiedTrackIds = playlistData.match(/[/\\](\d+)\./g).map(v => parseInt(v.match(/(\d+)/)[0], 10))
const idDiff = fad.diff(oldTrackIds, modifiedTrackIds)
await Promise.all([...(new Set(idDiff.added))].map(id => {
if (!trackIds.includes(id)) {
trackList[id] = {}
trackIds.unshift(parseInt(id, 10))
}
return api.editPlaylist('add', playlistId, id)
})).catch(console.trace)
await Promise.all([...(new Set(idDiff.removed))].map(id => {
general.removeValueFromArray(trackIds, id)
return api.editPlaylist('del', playlistId, id)
})).catch(console.trace)
}
}
playlistList.push({
saveForChange: needSync ? playlistId : null,
name: playlistName,
trackIds
})
}
}
// Generate for album(δΈθΎ)
logger.info('Requesting user\'s albums')
const list = new Set()
if (config('downloadSubAlbum', false)) {
const albumList = await api.getUserAlbum()
for (const alist of albumList) list.add(alist.id)
const extraAlbum = config('extraAlbum', [])
extraAlbum.forEach((item) => list.add(parseInt(item.trim(), 10)))
const excludeAlbum = config('excludeAlbum', [])
excludeAlbum.forEach((item) => list.delete(parseInt(item.trim(), 10)))
}
for (const albumId of list) {
const albumInfo = await api.getAlbumInfo(albumId)
const trackIds = []
for (const track of albumInfo.songs) {
trackList[track.id] = {}
trackIds.push(parseInt(track.id, 10))
}
playlistList.push({
name: config('prefix', []).album + albumInfo.album.name,
trackIds
})
}
{
// Generate for artist top songs(ζζηι¨)
logger.info('Requesting user\'s atrists\' songs')
const list = new Set()
const nameMap = {}
if (config('downloadSubAlbum', false)) {
const artistList = await api.getUserArtist()
for (const artist of artistList) {
list.add(artist.id)
nameMap[artist.id] = artist.name
}
const extraArtist = config('extraArtist', [])
extraArtist.forEach((item) => list.add(parseInt(item.trim(), 10)))
const excludeArtist = config('excludeArtist', [])
excludeArtist.forEach((item) => list.delete(parseInt(item.trim(), 10)))
}
for (const artistId of list) {
const artistTop = await api.getArtistTop(artistId)
const trackIds = []
for (const track of artistTop.songs.splice(0, config('downloadSubArtistTopNum', 30))) {
trackList[track.id] = {}
trackIds.push(parseInt(track.id, 10))
}
playlistList.push({
name: config('prefix', []).artistTopN.replace('$', config('downloadSubArtistTopNum', 30)) + nameMap[artistId],
trackIds
})
}
}
// Generate for history recommendation
logger.info('Requesting user\'s daily recommendation')
if (config('downloadRecommendation', false)) {
const data = await api.getUserRecommendation()
const trackIds = []
for (const track of data) {
trackList[track.id] = metadata.generateTrackMetadata(track)
trackIds.push(parseInt(track.id, 10))
}
let month = (new Date()).getMonth() + 1
month = (month < 10) ? `0${month}` : `${month}`
let day = (new Date()).getDate()
day = (day < 10) ? `0${day}` : `${day}`
const date = `${(new Date()).getFullYear()}-${month}-${day}`
playlistList.push({
name: config('prefix', []).recommendation + date,
trackIds
})
}
// Generate for history recommendation
if (config('downloadHistoryRecommendation', false)) {
const historyData = await api.getUserHistoryRecommendation()
for (const date of historyData.dates) {
const trackIds = []
for (const track of historyData.tracks[date]) {
trackList[track.id] = metadata.generateTrackMetadata(track)
trackIds.push(parseInt(track.id, 10))
}
playlistList.push({
name: config('prefix', []).recommendation + date,
trackIds
})
}
}
// Remove downloaded tracks
for (let trackId in trackList) {
trackId = parseInt(trackId, 10)
if (that.downloaded.has(trackId)) {
trackList[trackId].done = true
trackList[trackId].format = that.downloadedFormat[trackId]
} else unfoundTrackList[trackId] = trackList[trackId]
}
for (const trackId of that.downloaded) {
if (!trackList[trackId]) {
const realPath = path.resolve(__root, sha1(trackId).substr(0, 2))
fs.unlink(path.join(realPath, trackId + '.lrc'), () => {
fs.unlink(path.join(realPath, trackId + '.flac'), (error) => {
if (error) {
fs.unlink(path.join(realPath, trackId + '.mp3'), () => {})
}
})
})
}
}
logger.info('Download list:')
playlistList.forEach((item) => logger.info(' ' + item.name + ` (${item.trackIds.length})`))
logger.initBar(Object.keys(unfoundTrackList).length)
}
// Track processing
const trackDownloadQueue = new PQueue({ concurrency: 3 })
const trackProcessQueue = new PQueue({ concurrency: 1 })
const trackCopyQueue = new PQueue({ concurrency: 1 })
for (let trackId in unfoundTrackList) {
trackId = parseInt(trackId, 10)
let trackInfo = trackList[trackId]
trackDownloadQueue.add(async () => {
const tmpPath = path.resolve(tmpBasePath, uuid() + '/')
const realPath = path.resolve(__root, sha1(trackId).substr(0, 2))
const savePath = tmpPath
logger.debug('Requesting details of track', trackId)
if (!trackInfo.title) {
trackInfo = metadata.generateTrackMetadata(await api.getTrackInfo(trackId))
}
logger.info(`[Track: ${colors.italic(trackInfo.title)}] ${colors.yellow('Downloading...')}`)
// Download files
let filetype = 'flac'
await new Promise((resolve) => {
fs.access(savePath, fs.constants.F_OK | fs.constants.W_OK, (error) => {
if (error) {
fs.mkdir(savePath, {
recursive: true
}, () => {
resolve()
})
} else resolve()
})
}).catch(console.trace)
{
logger.debug('Requesting URL of track', trackInfo.title, trackId)
const trackUrl = await api.getTrackUrl(trackId)
if (!trackUrl) {
logger.info(`[Track: ${colors.italic(trackInfo.title)}] ${colors.red('Not available due to copyright issue!')}`)
logger._bar.tick(1)
return
}
if (trackUrl.endsWith('mp3')) filetype = 'mp3'
await general.downloadFile(trackInfo, trackUrl, savePath)
if (trackInfo.albumImg) {
try {
await general.downloadFile(trackInfo, trackInfo.albumImg + '?param=640y640', savePath)
} catch (error) {
logger.warn(error)
trackInfo.albumImg = ''
}
}
}
logger.info(`[Track: ${colors.italic(trackInfo.title)}] ${colors.blue('Downloaded!')}`)
trackProcessQueue.add(async () => {
logger.info(`[Track: ${colors.italic(trackInfo.title)}] ${colors.yellow('Processing...')}`)
// Metadata processing
const trackPath = path.resolve(savePath, trackId + '.' + filetype)
{
const coverPath = path.resolve(savePath, trackId + '.jpg')
metadata.writeMetadata(trackInfo, trackPath, coverPath)
}
// Lyric processing
await new Promise((resolve) => {
fs.access(realPath, fs.constants.F_OK | fs.constants.W_OK, (error) => {
if (error) {
fs.mkdir(realPath, {
recursive: true
}, () => {
resolve()
})
} else resolve()
})
}).catch(console.trace)
{
logger.debug('Requesting lyric of track', trackInfo.name, trackId)
const lyricData = await api.getLyric(trackId)
if (!lyricData.lrc || !lyricData.lrc.lyric) {
logger.debug('No lyric for track', trackInfo.name)
} else {
const lyricStr = lyric.generateLyric(trackId, lyricData)
await new Promise((resolve) => {
fs.writeFile(path.resolve(realPath, trackId + '.lrc'), lyricStr, (error) => {
if (error) throw error
resolve()
})
}).catch(console.trace)
}
}
logger.info(`[Track: ${colors.italic(trackInfo.title)}] ${colors.blue('Processed!')}`)
trackCopyQueue.add(async () => {
logger.info(`[Track: ${colors.italic(trackInfo.title)}] ${colors.yellow('Moving...')}`)
return new Promise((resolve) => {
const destPath = path.resolve(realPath, trackId + '.' + filetype)
fs.copyFile(trackPath, destPath + '.tmp', (error) => {
if (error) throw error
fs.rename(destPath + '.tmp', destPath, (error) => {
if (error) throw error
fs.unlink(trackPath, () => {
logger.debug(`[Track: ${colors.italic(trackInfo.title)}] Moved!`)
that.downloaded.add(trackId)
trackList[trackId].done = true
trackList[trackId].format = filetype
logger._bar.tick(1)
logger.info(`[Track: ${colors.italic(trackInfo.title)}] ${colors.green('Success!')}`)
resolve()
})
})
})
}).catch(console.trace)
})
})
})
}
// Generate playlist file
const playlistWriteList = []
{
const filelist = fs.readdirSync(path.resolve(__root, '../'))
for (const name of filelist) {
if (name !== 'MUSIC') fs.unlinkSync(path.resolve(__root, '../', name))
}
}
{
const filelist = fs.readdirSync(path.resolve(__root, '../../'))
for (const name of filelist) {
if (name.endsWith(' .m3u')) fs.unlinkSync(path.resolve(__root, '../../', name))
}
}
{
const filelist = fs.readdirSync(path.resolve(__root, '.azusa/'))
for (const name of filelist) {
fs.unlinkSync(path.resolve(__root, '.azusa/', name))
}
}
// Process user dir
if (config('generatePlaylistFile', true)) {
general.genUserPlaylistFile(__root)
}
for (const playlistInfo of playlistList) {
let objHash = null
const needSave = playlistInfo.saveForChange
playlistWriteList.push(() => {
const playlistPath = path.resolve(__root, needSave ? '../..' : '..', general.replaceChar(playlistInfo.name) + (needSave ? ' .m3u' : '.m3u'))
const trackPathList = []
const trackIdList = []
for (const trackId of playlistInfo.trackIds) {
if (trackList[trackId].done) {
const trackFilename = trackId + '.' + trackList[trackId].format
const trackPath = path.join(needSave ? 'Azusa/MUSIC' : 'MUSIC', sha1(trackId).substr(0, 2), trackFilename)
trackPathList.push(trackPath)
trackIdList.push(trackId)
}
}
if (trackPathList.length === 0) return
if (objHash !== (objHash = hasher.hash(trackPathList))) {
const filecontent = '#EXTM3U\n\n' + trackPathList.join('\n')
return new Promise((resolve) => {
setTimeout(() => {
fs.writeFile(playlistPath, filecontent, (error) => {
if (error) throw error
if (needSave) {
fs.writeFile(path.resolve(__root, '.azusa/') + `/${needSave}.json`, JSON.stringify(trackIdList), (error) => {
if (error) throw error
resolve()
})
} else {
resolve()
}
})
}, randomInt(100, 1000))
}).catch(console.trace)
}
})
}
const intervalId = setInterval(() => {
Promise.all(playlistWriteList.map((fn) => fn ? fn() : Promise.resolve()))
}, 5000)
await trackDownloadQueue.onIdle()
await trackCopyQueue.onIdle()
clearInterval(intervalId)
await Promise.all(playlistWriteList.map((fn) => fn ? fn() : Promise.resolve())).then(() => {
setTimeout(() => {
rimraf(tmpBasePath, () => {})
}, 1200)
})
})().catch(console.trace)