Skip to content

Commit

Permalink
refactor(ncm): picture
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 19, 2020
1 parent a83e45e commit 09cece6
Showing 1 changed file with 6 additions and 31 deletions.
37 changes: 6 additions & 31 deletions src/controllers/netease/picture.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
// This module is intended to get picture url
const Joi = require('joi')
const crypto = require('crypto')
const { getPictureURL } = require('./_sdk_wrapper')
// validation schema
const { ValidateParams } = require('../../utils/response')
const schema = Joi.object({
id: Joi.string().required(),
height: Joi.string().default(300),
height: Joi.number().default(300),
})

const md5 = (data) => {
const buf = Buffer.from(data)
const str = buf.toString('binary')
return crypto.createHash('md5').update(str).digest('base64')
}

const neteasePickey = (id) => {
id = String(id)
const magic = '3go8&$8*3*3h0k(2)2'.split('')
const songId = id
.split('')
.map((item, index) =>
String.fromCharCode(
item.charCodeAt(0) ^ magic[index % magic.length].charCodeAt(0),
),
)
return md5(songId.join('')).replace(/\//g, '_').replace(/\+/g, '-')
}

const picture = (id, size = 300) => {
return Promise.resolve({
url: `https://p3.music.126.net/${neteasePickey(
id,
)}/${id}.jpg?param=${size}y${size}`,
})
}

module.exports = async (ctx) => {
const params = Object.assign({}, ctx.params, ctx.query, ctx.request.body)
if (!(await ValidateParams(params, schema, ctx))) {
// validateParams
return
}
const pictureURL = await picture(params.id, params.height)
ctx.body = pictureURL
const pictureURL = getPictureURL(params.id, params.height)
ctx.body = {
url: pictureURL,
}
}

0 comments on commit 09cece6

Please sign in to comment.