Skip to content

Commit

Permalink
feat: support downloading daily recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
LynMoe committed Mar 28, 2021
1 parent ac9567f commit 342d22b
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 38 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- 为本地文件夹生成播放列表
- 本地化 NeteaseCloudMusicApi / 无依赖
- 可同步播放器端对歌单的修改
- 支持日推 / 历史日推下载

### Usage

Expand Down Expand Up @@ -84,12 +85,15 @@ module.exports = {
// 下载的歌曲数量 (前 N 首)
downloadSubArtistTopNum: 30,
// 附加的歌手
extraArtist: [
456788
],
extraArtist: [],
// 排除的歌手
excludeArtist: [],

// 下载当日日推
downloadRecommendation: false,
// 下载历史日推 (仅限黑胶 VIP )
downloadHistoryRecommendation: false,

// 下载音质
bitRate: 999000,

Expand All @@ -101,7 +105,8 @@ module.exports = {
album: '[Album] ',
artistTopN: '[Artist Top $] ',
playlist: '[Playlist] ',
userDir: '[Dir] '
userDir: '[Dir] ',
recommendation: '[Recommendation] '
}
}
```
Expand Down
48 changes: 47 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ if (!fs.existsSync(path.resolve(__root, '.azusa/'))) {
trackIds.push(parseInt(track.id, 10))
}

// 处理播放器歌单变动
const playlistName = config('prefix', []).playlist + playlistInfo.name
const needSync = (playlistInfo.creator.userId === api._uid) && config('syncPlaylist', []).includes(playlistId)

Expand Down Expand Up @@ -207,6 +208,51 @@ if (!fs.existsSync(path.resolve(__root, '.azusa/'))) {
}
}

// Generate for history 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)) {
Expand All @@ -229,7 +275,7 @@ if (!fs.existsSync(path.resolve(__root, '.azusa/'))) {
}

logger.info('Download list:')
playlistList.forEach((item) => logger.info(' ' + item.name))
playlistList.forEach((item) => logger.info(' ' + item.name + ` (${item.trackIds.length})`))
logger.initBar(Object.keys(unfoundTrackList).length)
}

Expand Down
8 changes: 7 additions & 1 deletion config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ module.exports = {
// 排除的歌手
excludeArtist: [],

// 下载当日日推
downloadRecommendation: false,
// 下载历史日推 (仅限黑胶 VIP )
downloadHistoryRecommendation: false,

// 下载音质
bitRate: 999000,

Expand All @@ -44,6 +49,7 @@ module.exports = {
album: '[Album] ',
artistTopN: '[Artist Top $] ',
playlist: '[Playlist] ',
userDir: '[Dir] '
userDir: '[Dir] ',
recommendation: '[Recommendation] '
}
}
55 changes: 28 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azusa",
"version": "1.0.3",
"version": "1.1.0",
"description": "For and by music lover",
"main": "app.js",
"scripts": {
Expand All @@ -21,15 +21,15 @@
},
"homepage": "https://github.com/kawaiilab/azusa#readme",
"dependencies": {
"NeteaseCloudMusicApi": "^4.0.3",
"NeteaseCloudMusicApi": "^4.0.9",
"colors": "^1.4.0",
"dotenv": "^8.2.0",
"fast-array-diff": "^1.0.0",
"fs": "0.0.1-security",
"metaflac-js2": "^1.0.7",
"node-id3": "^0.1.17",
"node-id3": "^0.2.2",
"node-object-hash": "^2.0.0",
"nodejs-file-downloader": "^4.1.1",
"nodejs-file-downloader": "^4.4.0",
"os": "^0.1.1",
"p-queue": "^6.4.0",
"p-retry": "^4.2.0",
Expand Down
55 changes: 54 additions & 1 deletion source/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,59 @@ module.exports = {
return result.data
},

async getUserRecommendation () {
let detail = await NeteaseCloudMusicApi.recommend_songs({
cookie: this._cookie
})

detail = detail.body.data
logger.debug(detail)

const trackList = []
const content = Array.from(detail.dailySongs)
content.forEach(v => trackList.push(v))

return trackList
},

async getUserHistoryRecommendation () {
let result = await NeteaseCloudMusicApi.history_recommend_songs({
cookie: this._cookie
})
result = result.body.data
logger.debug(result)

if (result.dates) {
const trackList = {}
const dates = Array.from(result.dates)

while (dates.length) {
const date = dates.pop()
let detail = await NeteaseCloudMusicApi.history_recommend_songs_detail({
date: date,
cookie: this._cookie
})

detail = detail.body.data
logger.debug(detail)

const content = Array.from(detail.songs)
trackList[date] = []
content.forEach(v => trackList[date].push(v))
}

return {
dates: Array.from(result.dates),
tracks: trackList
}
} else {
return {
dates: [],
tracks: {}
}
}
},

async getPlaylistInfo (playlistId) {
let result = await NeteaseCloudMusicApi.playlist_detail({
id: playlistId,
Expand Down Expand Up @@ -128,7 +181,7 @@ module.exports = {
br: config('bitRate', 999000),
cookie: this._cookie
})
result = result.body
result = JSON.parse(result.body.toString())

const trackUrl = result.data[0]
if (!trackUrl || !trackUrl.url) {
Expand Down

0 comments on commit 342d22b

Please sign in to comment.