2022-03-13 14:40:38 +08:00
|
|
|
import { pathCase } from 'change-case'
|
|
|
|
import cookieParser from 'cookie-parser'
|
|
|
|
import express, { Request, Response } from 'express'
|
|
|
|
import logger from './logger'
|
2022-03-19 17:03:29 +08:00
|
|
|
import {
|
|
|
|
setCache,
|
|
|
|
getCacheForExpress,
|
|
|
|
cacheAudio,
|
|
|
|
getAudioCache,
|
|
|
|
} from './cache'
|
|
|
|
import fileUpload from 'express-fileupload'
|
2022-03-13 14:40:38 +08:00
|
|
|
|
2022-03-17 14:45:04 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
const neteaseApi = require('NeteaseCloudMusicApi') as (params: any) => any[]
|
2022-03-13 14:40:38 +08:00
|
|
|
|
|
|
|
const app = express()
|
|
|
|
app.use(cookieParser())
|
2022-03-19 17:03:29 +08:00
|
|
|
app.use(fileUpload())
|
2022-03-13 14:40:38 +08:00
|
|
|
|
|
|
|
Object.entries(neteaseApi).forEach(([name, handler]) => {
|
2022-03-17 14:45:04 +08:00
|
|
|
if (['serveNcmApi', 'getModulesDefinitions'].includes(name)) return
|
2022-03-13 14:40:38 +08:00
|
|
|
|
2022-03-19 17:03:29 +08:00
|
|
|
name = pathCase(name)
|
|
|
|
|
2022-03-13 14:40:38 +08:00
|
|
|
const wrappedHandler = async (req: Request, res: Response) => {
|
|
|
|
logger.info(`[server] Handling request: ${req.path}`)
|
2022-03-17 14:45:04 +08:00
|
|
|
|
|
|
|
// Get from cache
|
2022-03-19 17:03:29 +08:00
|
|
|
const cache = await getCacheForExpress(name, req)
|
|
|
|
if (cache) return res.json(cache)
|
2022-03-17 14:45:04 +08:00
|
|
|
|
|
|
|
// Request netease api
|
2022-03-13 14:40:38 +08:00
|
|
|
try {
|
|
|
|
const result = await handler({
|
|
|
|
...req.query,
|
|
|
|
cookie: `MUSIC_U=${req.cookies['MUSIC_U']}`,
|
|
|
|
})
|
2022-03-19 17:03:29 +08:00
|
|
|
|
|
|
|
setCache(name, result.body, req.query)
|
|
|
|
return res.send(result.body)
|
2022-03-13 14:40:38 +08:00
|
|
|
} catch (error) {
|
2022-03-19 17:03:29 +08:00
|
|
|
return res.status(500).send(error)
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:03:29 +08:00
|
|
|
app.get(`/netease/${name}`, wrappedHandler)
|
|
|
|
app.post(`/netease/${name}`, wrappedHandler)
|
2022-03-13 14:40:38 +08:00
|
|
|
})
|
|
|
|
|
2022-03-19 17:03:29 +08:00
|
|
|
// Cache audio
|
|
|
|
app.get(
|
|
|
|
'/yesplaymusic/audio/:filename',
|
|
|
|
async (req: Request, res: Response) => {
|
|
|
|
getAudioCache(req.params.filename, res)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
app.post('/yesplaymusic/audio/:id', async (req: Request, res: Response) => {
|
|
|
|
const id = Number(req.params.id)
|
|
|
|
const { url } = req.query
|
|
|
|
if (isNaN(id)) {
|
|
|
|
return res.status(400).send({ error: 'Invalid param id' })
|
|
|
|
}
|
|
|
|
if (!url) {
|
|
|
|
return res.status(400).send({ error: 'Invalid query url' })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.files || Object.keys(req.files).length === 0 || !req.files.file) {
|
|
|
|
return res.status(400).send('No audio were uploaded.')
|
|
|
|
}
|
|
|
|
if ('length' in req.files.file) {
|
|
|
|
return res.status(400).send('Only can upload one audio at a time.')
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await cacheAudio(req.files.file.data, {
|
|
|
|
id: id,
|
|
|
|
source: 'netease',
|
|
|
|
})
|
|
|
|
res.status(200).send('Audio cached!')
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).send({ error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const port = Number(process.env['ELECTRON_DEV_NETEASE_API_PORT'] ?? 3000)
|
2022-03-13 14:40:38 +08:00
|
|
|
app.listen(port, () => {
|
|
|
|
logger.info(`[server] API server listening on port ${port}`)
|
|
|
|
})
|