Skip to content

Commit

Permalink
refactor(ncm): playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 17, 2020
1 parent 46d08e2 commit 02c34b7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion adapter/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = (router, middlewares, controller) => {

// Netease API
router.get('/nm/search/:keyword', controller.netease.search)
// router.get('/nm/playlist/:id', controller.netease.playlist)
router.get('/nm/playlist/:id', controller.netease.playlist)
// router.get('/nm/picture/:id/:height?', controller.netease.picture)
// router.get('/nm/artist/:id', controller.netease.artist)
// router.get('/nm/album/:id', controller.netease.album)
Expand Down
61 changes: 61 additions & 0 deletions src/controllers/netease/playlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// This module is intended to search songs
const winston = require('winston')
const sdk = require('NeteaseCloudMusicApi')
const Joi = require('joi')
const Cache = require('../../cache')
// validation schema
const { ValidateParams } = require('../../utils/response')
const schema = Joi.object({
id: Joi.number().min(1).max(1000000000000).required(),
s: Joi.number().min(1).max(10000).default(8),
nocache: Joi.boolean().default(false)
})

async function getPlaylistDetail (params, ctx) {
const {
id,
s
} = params
const result = await sdk.playlist_detail({
id,
s,
realIP: ctx.get('X-Real-IP')
})
if (result.status !== 200) {
ctx.body = {
status: result.status,
message: '上游错误',
data: result.body,
ts: Date.now()
}
return
}
return result.body
}

module.exports = async (ctx) => {
const params = Object.assign({}, ctx.params, ctx.query, ctx.request.body)
if (!await ValidateParams(params, schema, ctx)) { // validateParams
return
}
const data = await (params.nocache ? getPlaylistDetail(params, ctx) : Cache.remeber(
`nm:playlist:${params.id}`,
60 * 60 * 2, // 2 Hours
async () => {
return getPlaylistDetail(params, ctx)
}
))
winston.verbose(data)
if (data.code !== 200) {
ctx.status = Number.parseInt(data.code)
ctx.body = {
status: data.code,
message: '上游错误',
data: data,
ts: Date.now()
}
return
}
ctx.status = 200
ctx.body = data
}

0 comments on commit 02c34b7

Please sign in to comment.