-
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
46d08e2
commit 02c34b7
Showing
2 changed files
with
62 additions
and
1 deletion.
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
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,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 | ||
} |