Skip to content

Commit

Permalink
🥺 查找歌曲
Browse files Browse the repository at this point in the history
  • Loading branch information
金伸瓯 committed Aug 13, 2020
1 parent 1c40a3b commit 0229cd9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
4 changes: 2 additions & 2 deletions routes/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = {
if (result.code === 200) {
return res.send({
result: 100,
data: dataHandle.song(result.recommend),
data: dataHandle.song(result.data.dailySongs),
})
}
return res.send({
Expand Down Expand Up @@ -130,7 +130,7 @@ module.exports = {
data: dataHandle.playlist(result.recommend || result.result || []),
})
case 'qq':
result = await request('recommend/playlist');
result = await request('recommend/playlist/u');
return res.send({
result: 100,
data: dataHandle.playlist(result.data.list || []),
Expand Down
53 changes: 53 additions & 0 deletions routes/song.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const port = require('../bin/config').port;

module.exports = {
async ['/']({ req, res, request, dataHandle, platform }) {
const { id } = req.query;
Expand Down Expand Up @@ -66,4 +68,55 @@ module.exports = {
})
}
},

// 查找,根据关键词从其他平台获取播放链接
async ['/find']({ req, res, request, platform }) {
const { list = [] } = req.query;

const reqFunction = async ({ id, key, duration }) => {
try {
const _p = {
163: 'qq',
qq: '163'
}[platform] || 'qq';
const queryRes = await request({
url: 'search',
data: {
key,
pageNo: 1,
pageSize: 5,
_p,
},
domain: `http://127.0.0.1:${port}`,
});
const findSong = (queryRes.data.list || []).find((item) => {
if (duration) {
return (item.duration <= duration + 3) && (item.duration >= duration - 3)
}
return true;
});
if (!findSong) {
return [id, ''];
}
const urlRes = await request({
url: 'url',
data: { id: findSong.id, _p: findSong.platform },
domain: `http://127.0.0.1:${port}`,
})
return [id, urlRes.data.url];
} catch {
return [id, ''];
}
}
Promise.all(list.map((obj) => reqFunction(obj)))
.then((resArr) => {
const data = {};
resArr.forEach(([id, url]) => url && (data[id] = url));

res.send({
result: 100,
data,
})
})
}
};
2 changes: 1 addition & 1 deletion util/dataHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class dataHandle {
desc: description || undefined,
}),

handle163Singer: ({ id, name, picUrl, img1v1Url, alias, company, trans, briefDesc, introduction }) => ({
handle163Singer: ({ id, name, picUrl, img1v1Url, alias = [], company, trans, briefDesc, introduction }) => ({
id,
name,
picUrl: picUrl || img1v1Url || 'http://p3.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg',
Expand Down
8 changes: 5 additions & 3 deletions util/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class Request {
}
obj.method = obj.method || 'get';

let { url, data, trueUrl } = obj;
let { url, data, trueUrl, domain } = obj;

url = `${this.domain}/${url}`;
url = `${domain || this.domain}/${url}`;

trueUrl && (url = trueUrl);

delete this.req.headers['content-type'];
delete this.req.headers['Content-Type'];
if (obj.method === 'get') {
obj.url = StringHelper.changeUrlQuery(data, url);
delete obj.data;
Expand All @@ -42,7 +44,7 @@ class Request {
if (err.message.indexOf('timeout') > -1) {
return {};
}
this.res.send(err.response.data);
this.res.send(err.message);
}
}
}
Expand Down

0 comments on commit 0229cd9

Please sign in to comment.