fix: restore v0.1.0 search APIs - KG showtype=14, WY eapi, KW cover web_albumpic_short
This commit is contained in:
@@ -141,17 +141,23 @@ function formatPlayCount(count) {
|
||||
return String(count);
|
||||
}
|
||||
|
||||
// ===== 搜索实现(使用老式搜索API,无需weapi加密) =====
|
||||
// ===== 搜索实现(使用eapi搜索,与v0.1.0一致) =====
|
||||
function musicSearch(str, page, limit) {
|
||||
var url = 'https://music.163.com/api/search/get/web?s=' + encodeURIComponent(str) + '&type=1&offset=' + (limit * (page - 1)) + '&total=true&limit=' + limit;
|
||||
return requestUrl(url, 'GET', {
|
||||
var form = eapi('/api/search/song/list/page', {
|
||||
keyword: str,
|
||||
needCorrect: '1',
|
||||
channel: 'typing',
|
||||
offset: limit * (page - 1),
|
||||
scene: 'normal',
|
||||
total: page == 1,
|
||||
limit: limit
|
||||
});
|
||||
return requestUrl('http://interface3.music.163.com/eapi/search/song/list/page', 'POST', {
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
|
||||
'Referer': 'https://music.163.com',
|
||||
'Origin': 'https://music.163.com',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Cookie': WY_COOKIE
|
||||
}).then(function(res) {
|
||||
if (typeof res.body === 'string') {
|
||||
try { res.body = JSON.parse(res.body); } catch (e) { return null; }
|
||||
}
|
||||
}, querystring.stringify(form)).then(function(res) {
|
||||
return res.body;
|
||||
});
|
||||
}
|
||||
@@ -161,27 +167,24 @@ function handleSearchResult(rawList) {
|
||||
var list = [];
|
||||
for (var i = 0; i < rawList.length; i++) {
|
||||
var item = rawList[i];
|
||||
if (!item.baseInfo || !item.baseInfo.simpleSongData) continue;
|
||||
var s = item.baseInfo.simpleSongData;
|
||||
var qualities = {};
|
||||
if (item.l && item.l.size) qualities['128k'] = sizeFormate(item.l.size);
|
||||
if (item.h && item.h.size) qualities['320k'] = sizeFormate(item.h.size);
|
||||
if (item.sq && item.sq.size) qualities['flac'] = sizeFormate(item.sq.size);
|
||||
if (item.hr && item.hr.size) qualities['hires'] = sizeFormate(item.hr.size);
|
||||
var picUrl = item.album && item.album.picUrl ? item.album.picUrl : '';
|
||||
var singerName = '';
|
||||
if (item.artists && item.artists.length) {
|
||||
var ns = [];
|
||||
for (var j = 0; j < item.artists.length; j++) ns.push(item.artists[j].name);
|
||||
singerName = ns.join('、');
|
||||
}
|
||||
if (s.l && s.l.size) qualities['128k'] = sizeFormate(s.l.size);
|
||||
if (s.m && s.m.size) qualities['128k'] = sizeFormate(s.m.size);
|
||||
if (s.h && s.h.size) qualities['320k'] = sizeFormate(s.h.size);
|
||||
if (s.sq && s.sq.size) qualities['flac'] = sizeFormate(s.sq.size);
|
||||
if (s.hr && s.hr.size) qualities['hires'] = sizeFormate(s.hr.size);
|
||||
var picUrl = s.al && s.al.picUrl ? s.al.picUrl : '';
|
||||
list.push({
|
||||
id: String(item.id),
|
||||
name: item.name || '',
|
||||
artists: singerName,
|
||||
id: String(s.id),
|
||||
name: s.name || '',
|
||||
artists: formatSingerName(s.ar, 'name'),
|
||||
source: 'wy',
|
||||
pic: picUrl, mPic: picUrl, sPic: picUrl,
|
||||
albumName: item.album && item.album.name ? item.album.name : '',
|
||||
albumId: String(item.album && item.album.id ? item.album.id : ''),
|
||||
interval: String(formatPlayTime((item.duration || 0) / 1000) || '--/--'),
|
||||
albumName: s.al && s.al.name ? s.al.name : '',
|
||||
albumId: String(s.al && s.al.id ? s.al.id : ''),
|
||||
interval: String(formatPlayTime((s.dt || 0) / 1000) || '--/--'),
|
||||
qualities: qualities
|
||||
});
|
||||
}
|
||||
@@ -194,11 +197,16 @@ function search(str, page, limit, retryNum) {
|
||||
if (!limit) limit = 30;
|
||||
return musicSearch(str, page, limit).then(function(result) {
|
||||
if (!result || result.code !== 200) return search(str, page, limit, retryNum);
|
||||
var songs = (result.result && result.result.songs) ? result.result.songs : [];
|
||||
var list = handleSearchResult(songs);
|
||||
var list = handleSearchResult(result.data && result.data.resources ? result.data.resources : []);
|
||||
if (!list || list.length === 0) return search(str, page, limit, retryNum);
|
||||
var total = result.result && result.result.songCount ? result.result.songCount : 0;
|
||||
return { list: list, allPage: Math.ceil(total / limit), limit: limit, total: total, source: 'wy' };
|
||||
var total = result.data && result.data.totalCount ? result.data.totalCount : 0;
|
||||
return {
|
||||
list: list,
|
||||
allPage: Math.ceil(total / limit),
|
||||
limit: limit,
|
||||
total: total,
|
||||
source: 'wy'
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -115,12 +115,16 @@ function handleSearchResult(rawList) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// 封面:用 ALBUMID 拼 kuwo 封面 URL
|
||||
// 封面:web_albumpic_short 拼完整URL,fallback 用 ALBUMID,再 fallback 用 artistpic
|
||||
var picUrl = '';
|
||||
if (info.ALBUMID) {
|
||||
if (info.web_albumpic_short) {
|
||||
picUrl = 'https://img2.kuwo.cn/star/albumcover/' + info.web_albumpic_short;
|
||||
} else if (info.ALBUMID) {
|
||||
picUrl = 'https://img2.kuwo.cn/star/albumcover/300/' + info.ALBUMID + '.jpg';
|
||||
} else if (info.web_artistpic_short) {
|
||||
picUrl = 'https://img2.kuwo.cn/star/albumcover/' + info.web_artistpic_short;
|
||||
} else {
|
||||
picUrl = 'http://artistpicserver.kuwo.cn/pic.web?corp=kuwo&type=rid_pic&pictype=500&size=500&rid=' + songId;
|
||||
picUrl = 'http://artistpicserver.kuwo.cn/pic.web?corp=kuwo&type=rid_pic&pictype=500&size=500&rid=MUSIC_' + songId;
|
||||
}
|
||||
var artistStr = stripEm(info.ARTIST || '').replace(/&/g, '、');
|
||||
var duration = parseInt(info.DURATION || '0', 10);
|
||||
|
||||
@@ -79,9 +79,9 @@ function stripEm(s) {
|
||||
}
|
||||
|
||||
function musicSearch(str, page, limit) {
|
||||
var url = 'http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=' + encodeURIComponent(str) + '&page=' + page + '&pagesize=' + limit;
|
||||
var url = 'http://mobilecdn.kugou.com/api/v3/search/song?showtype=14&highlight=em&pagesize=' + limit + '&page=' + page + '&keyword=' + encodeURIComponent(str);
|
||||
return requestUrl(url, 'GET', { 'User-Agent': 'Mozilla/5.0 (Linux; Android 10)' }).then(function(res) {
|
||||
if (res.body && res.body.data && res.body.data.info) return res.body.data;
|
||||
if (res.body && res.body.data && res.body.data.info) return res.body.data.info;
|
||||
throw new Error('kg search failed');
|
||||
});
|
||||
}
|
||||
@@ -95,17 +95,16 @@ function handleSearchResult(rawList) {
|
||||
if (item.filesize) qualities['128k'] = sizeFormate(item.filesize);
|
||||
if (item['320filesize']) qualities['320k'] = sizeFormate(item['320filesize']);
|
||||
if (item.sqfilesize) qualities['flac'] = sizeFormate(item.sqfilesize);
|
||||
// 封面:优先 imgurl(带 {size} 占位符),其次 album_img,再次 union_cover
|
||||
// 封面:imgurl 带 {size} 占位符
|
||||
var picUrl = '';
|
||||
if (item.imgurl && typeof item.imgurl === 'string') {
|
||||
if (item.imgurl && typeof item.imgurl === 'string' && item.imgurl.indexOf('http') === 0) {
|
||||
picUrl = item.imgurl.replace('{size}', '400');
|
||||
if (picUrl && picUrl.indexOf('http') !== 0 && picUrl.indexOf('https') !== 0) picUrl = 'https://' + picUrl;
|
||||
}
|
||||
if (!picUrl && item.album_img) picUrl = item.album_img;
|
||||
if (!picUrl && item.trans_param && item.trans_param.union_cover) picUrl = item.trans_param.union_cover;
|
||||
if (!picUrl && item.img) picUrl = item.img;
|
||||
list.push({
|
||||
id: String(item.hash || ''),
|
||||
name: stripEm(item.songname || item.song_name || item.filename || ''),
|
||||
id: String(item.hash || item.audio_id || ''),
|
||||
name: stripEm(item.songname || item.song_name || ''),
|
||||
artists: stripEm(item.singername || item.singer_name || ''),
|
||||
source: 'kg',
|
||||
pic: picUrl,
|
||||
@@ -124,11 +123,9 @@ function search(str, page, limit, retryNum) {
|
||||
if (retryNum === undefined) retryNum = 0;
|
||||
if (++retryNum > 3) return Promise.reject(new Error('try max num'));
|
||||
if (!limit) limit = 30;
|
||||
return musicSearch(str, page, limit).then(function(data) {
|
||||
var rawList = data.lists || [];
|
||||
var total = data.total || rawList.length * 10;
|
||||
return musicSearch(str, page, limit).then(function(rawList) {
|
||||
var list = handleSearchResult(rawList);
|
||||
return { list: list, allPage: Math.ceil(total / limit), limit: limit, total: total, source: 'kg' };
|
||||
return { list: list, allPage: page + 1, limit: limit, total: list.length * 10, source: 'kg' };
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user