-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e69fe08
commit 05e74c7
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |