55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import axios from 'axios'
|
|
import crypto from 'crypto'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const query = getQuery(event)
|
|
const keyword = query.keyword || query.keywords
|
|
|
|
if (!keyword) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Keyword is required'
|
|
})
|
|
}
|
|
|
|
// 我们在这里使用不需要签名的通用接口或移动端旧接口
|
|
|
|
try {
|
|
const url = `http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=${encodeURIComponent(keyword as string)}&page=1&pagesize=20&showtype=1`
|
|
const response = await axios.get(url, {
|
|
headers: {
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
},
|
|
// 允许处理不严谨的 SSL 证书或忽略部分请求错误
|
|
validateStatus: () => true
|
|
})
|
|
|
|
const data = response.data
|
|
|
|
// Process results to map cover images and VIP status correctly
|
|
if (data && data.data && data.data.info) {
|
|
data.data.info = data.data.info.map((item: any) => {
|
|
return {
|
|
...item,
|
|
isVip: item.pay_type !== 0,
|
|
// Extract proper image URL from 'union_cover', '320' size or hash, and use https
|
|
pic: item.trans_param?.union_cover
|
|
? item.trans_param.union_cover.replace('{size}', '400').replace('http://', 'https://')
|
|
: item['320hash']
|
|
? `https://imge.kugou.com/stdmusic/400/${item['320hash'].slice(0,2)}/${item['320hash'].slice(2,4)}/${item['320hash']}.jpg`
|
|
: item['hash']
|
|
? `https://imge.kugou.com/stdmusic/400/${item['hash'].slice(0,2)}/${item['hash'].slice(2,4)}/${item['hash']}.jpg`
|
|
: undefined
|
|
}
|
|
})
|
|
}
|
|
|
|
return data
|
|
} catch (error) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: 'Music service unavailable'
|
|
})
|
|
}
|
|
})
|