fix(kg,kw): restore old search API params + field parsing from v0.0.6, fix cover and quality

This commit is contained in:
TRAE Bot
2026-07-05 22:24:42 +00:00
parent 2c5b21e869
commit b019b0e296
2 changed files with 44 additions and 38 deletions

View File

@@ -79,8 +79,7 @@ function stripEm(s) {
} }
function musicSearch(str, page, limit) { function musicSearch(str, page, limit) {
var pn = (page - 1) * limit; var url = 'http://search.kuwo.cn/r.s?client=kt&all=' + encodeURIComponent(str) + '&pn=' + (page - 1) + '&rn=' + limit + '&uid=794762570&ver=kwplayer_ar_9.2.2.1&vipver=1&show_copyright_off=1&newver=1&ft=music&cluster=0&strategy=2012&encoding=utf8&rformat=json&vermerge=1&mobi=1&issubtitle=1';
var url = 'http://search.kuwo.cn/r.s?all=' + encodeURIComponent(str) + '&ft=music&itemset=1&rformat=json&encoding=utf8&pn=' + pn + '&rn=' + limit;
return requestUrl(url, 'GET', { return requestUrl(url, 'GET', {
'User-Agent': 'Mozilla/5.0 (Linux; Android 10)', 'User-Agent': 'Mozilla/5.0 (Linux; Android 10)',
'Referer': 'http://www.kuwo.cn/' 'Referer': 'http://www.kuwo.cn/'
@@ -98,34 +97,44 @@ function handleSearchResult(rawList) {
var list = []; var list = [];
if (!rawList) return list; if (!rawList) return list;
for (var i = 0; i < rawList.length; i++) { for (var i = 0; i < rawList.length; i++) {
var item = rawList[i]; var info = rawList[i];
var rid = ''; var songId = (info.MUSICRID || '').replace('MUSIC_', '');
if (item.MUSICRID) rid = String(item.MUSICRID).replace('MUSIC_', ''); if (!songId) songId = String(info.DC_TARGETID || info.rid || info.id || '');
else if (item.DC_TARGETID) rid = String(item.DC_TARGETID); // 音质:从 N_MINFO 解析
else rid = String(item.rid || item.id || '');
var qualities = {}; var qualities = {};
var formats = String(item.FORMATS || ''); if (info.N_MINFO) {
var minfo = String(item.MINFO || ''); var parts = String(info.N_MINFO).split(';');
if (formats.indexOf('MP3128') !== -1 || minfo.indexOf('bitrate:128') !== -1) qualities['128k'] = ''; for (var j = 0; j < parts.length; j++) {
if (formats.indexOf('MP3H') !== -1 || minfo.indexOf('bitrate:320') !== -1) qualities['320k'] = ''; var m = parts[j].match(/level:(\w+),bitrate:(\d+),format:(\w+),size:([\w.]+)/);
if (formats.indexOf('ALFLAC') !== -1 || minfo.indexOf('format:flac') !== -1) qualities['flac'] = ''; if (m) {
var name = stripEm(item.SONGNAME || item.NAME || ''); if (m[2] === '20900') qualities['master'] = m[4];
var artist = stripEm(item.ARTIST || ''); else if (m[2] === '4000') qualities['hires'] = m[4];
var albumName = stripEm(item.ALBUM || ''); else if (m[2] === '2000') qualities['flac'] = m[4];
var picUrl = item.web_albumpic_short || item.albumpic || item.ALBUMPIC || ''; else if (m[2] === '320') qualities['320k'] = m[4];
if (picUrl && picUrl.indexOf('http') !== 0 && picUrl.length > 2) picUrl = 'http://img1.kuwo.cn/star/albumcover/' + picUrl; else if (m[2] === '128') qualities['128k'] = m[4];
var duration = parseInt(item.DURATION || '0', 10); }
}
}
// 封面:用 ALBUMID 拼 kuwo 封面 URL
var picUrl = '';
if (info.ALBUMID) {
picUrl = 'https://img2.kuwo.cn/star/albumcover/300/' + info.ALBUMID + '.jpg';
} else {
picUrl = 'http://artistpicserver.kuwo.cn/pic.web?corp=kuwo&type=rid_pic&pictype=500&size=500&rid=' + songId;
}
var artistStr = stripEm(info.ARTIST || '').replace(/&/g, '、');
var duration = parseInt(info.DURATION || '0', 10);
list.push({ list.push({
id: rid, id: String(songId),
name: name, name: stripEm(info.SONGNAME || ''),
artists: artist, artists: artistStr,
source: 'kw', source: 'kw',
pic: picUrl, pic: picUrl,
mPic: picUrl, mPic: picUrl,
sPic: picUrl, sPic: picUrl,
albumName: albumName, albumName: stripEm(info.ALBUM || ''),
albumId: String(item.ALBUMID || ''), albumId: String(info.ALBUMID || ''),
interval: String(formatPlayTime(duration) || '--/--'), interval: isNaN(duration) ? '--/--' : String(formatPlayTime(duration) || '--/--'),
qualities: qualities qualities: qualities
}); });
} }
@@ -141,7 +150,7 @@ function search(str, page, limit, retryNum) {
var total = parseInt(data.TOTAL || '0', 10) || rawList.length * 10; var total = parseInt(data.TOTAL || '0', 10) || rawList.length * 10;
var list = handleSearchResult(rawList); var list = handleSearchResult(rawList);
return { list: list, allPage: Math.ceil(total / limit), limit: limit, total: total, source: 'kw' }; return { list: list, allPage: Math.ceil(total / limit), limit: limit, total: total, source: 'kw' };
}); }).catch(function() { return { list: [], allPage: 1, limit: limit, total: 0, source: 'kw' }; });
} }
// ===== 播放链接 ===== // ===== 播放链接 =====

View File

@@ -79,7 +79,7 @@ function stripEm(s) {
} }
function musicSearch(str, page, limit) { function musicSearch(str, page, limit) {
var url = 'http://mobilecdn.kugou.com/api/v3/search/song?showtype=14&highlight=&pagesize=' + limit + '&page=' + page + '&keyword=' + encodeURIComponent(str); var url = 'http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=' + encodeURIComponent(str) + '&page=' + page + '&pagesize=' + limit;
return requestUrl(url, 'GET', { 'User-Agent': 'Mozilla/5.0 (Linux; Android 10)' }).then(function(res) { 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;
throw new Error('kg search failed'); throw new Error('kg search failed');
@@ -95,27 +95,24 @@ function handleSearchResult(rawList) {
if (item.filesize) qualities['128k'] = sizeFormate(item.filesize); if (item.filesize) qualities['128k'] = sizeFormate(item.filesize);
if (item['320filesize']) qualities['320k'] = sizeFormate(item['320filesize']); if (item['320filesize']) qualities['320k'] = sizeFormate(item['320filesize']);
if (item.sqfilesize) qualities['flac'] = sizeFormate(item.sqfilesize); if (item.sqfilesize) qualities['flac'] = sizeFormate(item.sqfilesize);
// 封面:用 album_id 拼 kugou 封面 URL // 封面:优先 imgurl带 {size} 占位符),其次 album_img再次 union_cover
var albumId = String(item.album_id || '');
var picUrl = ''; var picUrl = '';
if (albumId) picUrl = 'http://imge.kugou.com/stdmusic/150/' + albumId + '.jpg'; if (item.imgurl && typeof item.imgurl === 'string') {
var name = stripEm(item.songname || item.filename || ''); picUrl = item.imgurl.replace('{size}', '400');
var singerName = stripEm(item.singername || ''); if (picUrl && picUrl.indexOf('http') !== 0 && picUrl.indexOf('https') !== 0) picUrl = 'https://' + picUrl;
// filename 格式 "歌手 - 歌名",如果没有 songname 就从 filename 拆
if (!name && item.filename) {
var parts = String(item.filename).split(' - ');
name = stripEm(parts[1] || parts[0]);
} }
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;
list.push({ list.push({
id: String(item.hash || ''), id: String(item.hash || ''),
name: name, name: stripEm(item.songname || item.song_name || item.filename || ''),
artists: singerName, artists: stripEm(item.singername || item.singer_name || ''),
source: 'kg', source: 'kg',
pic: picUrl, pic: picUrl,
mPic: picUrl, mPic: picUrl,
sPic: picUrl, sPic: picUrl,
albumName: stripEm(item.album_name || ''), albumName: stripEm(item.album_name || ''),
albumId: albumId, albumId: String(item.album_id || ''),
interval: String(formatPlayTime(item.duration) || '--/--'), interval: String(formatPlayTime(item.duration) || '--/--'),
qualities: qualities qualities: qualities
}); });