Skip to content

Commit

Permalink
refactor(ncm): artist
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 17, 2020
1 parent e69fe08 commit 05e74c7
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/controllers/netease/artist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// This module is intended to get artist records
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()
})

async function getArtistsDetail (params, ctx) {
const {
id
} = params
const result = await sdk.artists({
id,
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 ? getArtistsDetail(params, ctx) : Cache.remeber(
`nm:playlist:${params.id}`,
60 * 60 * 2, // 2 Hours
async () => {
return getArtistsDetail(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 05e74c7

Please sign in to comment.