Files
Koneko_api_for_QZ-Music/Koneko_网易云音乐_v0.0.2.js

680 lines
23 KiB
JavaScript
Raw Normal View History

/**
* @name 网易云音乐 - Koneko
* @description 聚合音源插件: 官方搜索 + 多API音源容灾 + 完整Cookie功能
* @version 0.0.2
* @author Miao-moe
*
* 环境变量:
* ceru_key - 聆澜API密钥可选
* cookie - 网易云Cookie用于搜索增强每日推荐私人FM我喜欢的音乐
* playlist_url - 网易云个人主页链接用于获取个人歌单
*/
'use strict'
var https = require('https')
var http = require('http')
var crypto = require('crypto')
var env = global.env || {}
var CERU_KEY = env.ceru_key || ''
var WY_COOKIE = env.cookie || ''
var PLAYLIST_URL = env.playlist_url || ''
var HEADERS_COMMON = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
function mapBr(q) {
if (q === '128k' || q === 'standard') return '128000'
if (q === '320k' || q === 'exhigh') return '320000'
if (q === '999k' || q === 'lossless') return '999000'
if (q === 'hires') return '999000'
return '320000'
}
function httpGet(url, headers, timeout) {
return new Promise(function(resolve, reject) {
var mod = url.indexOf('https') === 0 ? https : http
var req = mod.get(url, { headers: headers || {}, timeout: timeout || 8000 }, function(res) {
var data = ''
res.on('data', function(chunk) { data += chunk })
res.on('end', function() {
try { resolve(JSON.parse(data)) } catch(e) { resolve(data) }
})
})
req.on('error', function(err) { reject(err) })
req.on('timeout', function() { req.destroy(); reject(new Error('timeout')) })
})
}
function httpPost(url, body, headers, timeout) {
return new Promise(function(resolve, reject) {
var mod = url.indexOf('https') === 0 ? https : http
var postData = typeof body === 'string' ? body : JSON.stringify(body)
var opts = {
method: 'POST',
headers: {},
timeout: timeout || 10000
}
if (headers) {
for (var k in headers) { opts.headers[k] = headers[k] }
}
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded'
opts.headers['Content-Length'] = Buffer.byteLength(postData)
var req = mod.request(url, opts, function(res) {
var data = ''
res.on('data', function(chunk) { data += chunk })
res.on('end', function() {
try { resolve(JSON.parse(data)) } catch(e) { resolve(data) }
})
})
req.on('error', function(err) { reject(err) })
req.on('timeout', function() { req.destroy(); reject(new Error('timeout')) })
req.write(postData)
req.end()
})
}
var EAPI_KEY = 'e82ckenh8dichen8'
function aesEncryptEcb(text, key) {
var cipher = crypto.createCipheriv('aes-128-ecb', key, '')
cipher.setAutoPadding(true)
var encrypted = cipher.update(text, 'utf8', 'hex')
encrypted += cipher.final('hex')
return encrypted
}
function eapiEncrypt(url, text) {
var message = 'nobody' + url + 'use' + text + 'md5forencrypt'
var digest = crypto.createHash('md5').update(message).digest('hex')
var data = url + '-36cd479b6b5-' + text + '-36cd479b6b5-' + digest
return aesEncryptEcb(data, EAPI_KEY)
}
function eapiRequest(url, params) {
var text = JSON.stringify(params)
var enc = eapiEncrypt(url, text)
var body = 'params=' + encodeURIComponent(enc)
var headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G960U) AppleWebKit/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'https://music.163.com',
'Cookie': WY_COOKIE || 'os=android; appver=8.9.0;'
}
return httpPost('https://music.163.com' + url, body, headers, 15000)
}
var WEAPI_IV = '0102030405060708'
var WEAPI_PRESET_KEY = '0CoJUm6Qyw8W8jud'
var WEAPI_RSA_KEY = '010001'
var WEAPI_RSA_MODULUS = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
var WEAPI_BASE62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
function aesEncryptCbc(text, key, iv) {
var cipher = crypto.createCipheriv('aes-128-cbc', key, iv)
cipher.setAutoPadding(true)
var encrypted = cipher.update(text, 'utf8', 'base64')
encrypted += cipher.final('base64')
return encrypted
}
function rsaEncrypt(text, exponent, modulus) {
var reversed = text.split('').reverse().join('')
var bigInt = BigInt('0x' + Buffer.from(reversed).toString('hex'))
var exp = BigInt('0x' + exponent)
var mod = BigInt('0x' + modulus)
var result = bigInt ** exp % mod
var hex = result.toString(16).padStart(256, '0')
return hex
}
function weapiEncrypt(text) {
var secretKey = ''
for (var i = 0; i < 16; i++) {
secretKey += WEAPI_BASE62.charAt(Math.floor(Math.random() * 62))
}
var firstEnc = aesEncryptCbc(text, WEAPI_PRESET_KEY, WEAPI_IV)
var secondEnc = aesEncryptCbc(firstEnc, secretKey, WEAPI_IV)
var rsa = rsaEncrypt(secretKey, WEAPI_RSA_KEY, WEAPI_RSA_MODULUS)
return {
params: secondEnc,
encSecKey: rsa
}
}
function weapiRequest(url, params) {
var text = JSON.stringify(params)
var enc = weapiEncrypt(text)
var body = 'params=' + encodeURIComponent(enc.params) + '&encSecKey=' + encodeURIComponent(enc.encSecKey)
var headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G960U) AppleWebKit/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'https://music.163.com',
'Cookie': WY_COOKIE || 'os=android; appver=8.9.0;'
}
return httpPost('https://music.163.com' + url, body, headers, 15000)
}
function formatPlayTime(ms) {
if (!ms || isNaN(ms)) return '--/--'
var totalSec = Math.floor(ms / 1000)
var m = Math.floor(totalSec / 60)
var s = totalSec % 60
return m + ':' + (s < 10 ? '0' : '') + s
}
function wySearch(keyword, page, limit) {
if (!page) page = 1
if (!limit) limit = 30
var offset = (page - 1) * limit
var url = 'https://music.163.com/api/search/get/web?csrf_token=&hlposttag=&s=' + encodeURIComponent(keyword) + '&type=1&offset=' + offset + '&total=true&limit=' + limit
return httpGet(url, {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Referer': 'https://music.163.com',
'Cookie': WY_COOKIE || 'os=pc; appver=2.10.0;'
}, 10000).then(function(res) {
if (!res || !res.result || !res.result.songs) {
return { list: [], allPage: 1, limit: limit, total: 0, source: 'wy' }
}
var list = []
for (var i = 0; i < res.result.songs.length; i++) {
var s = res.result.songs[i]
var artists = []
if (s.artists) {
for (var j = 0; j < s.artists.length; j++) {
artists.push(s.artists[j].name)
}
}
var album = s.album ? s.album.name : ''
var albumId = s.album ? String(s.album.id) : ''
var pic = ''
if (s.album && s.album.picId) {
var picIdStr = String(s.album.picId)
var picIdB64 = Buffer.from(picIdStr).toString('base64').replace(/=/g, '')
pic = 'https://p2.music.126.net/' + picIdB64 + '/' + picIdStr + '.jpg'
}
list.push({
id: s.id ? String(s.id) : '',
name: s.name || '',
artists: artists.join('、'),
albumName: album,
albumId: albumId,
source: 'wy',
pic: pic,
mPic: pic,
sPic: pic,
interval: formatPlayTime(s.duration),
qualities: {}
})
}
var total = res.result.songCount || 0
var allPage = Math.ceil(total / limit)
return { list: list, allPage: allPage, limit: limit, total: total, source: 'wy' }
}).catch(function(e) {
return { list: [], allPage: 1, limit: 30, total: 0, source: 'wy' }
})
}
function wyTipSearch(keyword) {
var url = 'https://music.163.com/api/search/get/web?csrf_token=&hlposttag=&s=' + encodeURIComponent(keyword) + '&type=1&offset=0&total=true&limit=10'
return httpGet(url, {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Referer': 'https://music.163.com',
'Cookie': WY_COOKIE || 'os=pc; appver=2.10.0;'
}, 10000).then(function(res) {
if (!res || !res.result || !res.result.songs) return []
var tips = []
for (var i = 0; i < res.result.songs.length; i++) {
var s = res.result.songs[i]
tips.push(s.name + ' - ' + (s.artists && s.artists[0] ? s.artists[0].name : '未知'))
}
return tips
}).catch(function(e) { return [] })
}
function wyHotSearch() {
return weapiRequest('/weapi/search/hot', { type: 1111 }).then(function(res) {
if (!res || !res.result || !res.result.hots) return []
var hots = []
for (var i = 0; i < res.result.hots.length; i++) {
hots.push(res.result.hots[i].first)
}
return hots
}).catch(function(e) { return [] })
}
function wyOfficialUrl(songId, quality) {
var br = mapBr(quality)
var params = {
ids: '[' + songId + ']',
br: br,
csrf_token: ''
}
return weapiRequest('/weapi/song/enhance/player/url', params).then(function(res) {
if (res && res.data && res.data[0] && res.data[0].url) {
return { url: res.data[0].url, platform: 'wy' }
}
throw new Error('no url')
})
}
function ceruGetUrl(songId, quality) {
if (!CERU_KEY) return Promise.reject(new Error('no key'))
var br = mapBr(quality)
var url = 'https://ceruapi.lol/meting-api-0/?server=netease&type=url&id=' + encodeURIComponent(songId) + '&auth=' + encodeURIComponent(CERU_KEY) + '&br=' + br
return httpGet(url, HEADERS_COMMON, 10000).then(function(res) {
if (res && res.url) return { url: res.url, platform: 'wy' }
throw new Error('no url')
})
}
function bbGetUrl(songId, quality) {
var br = mapBr(quality)
var url = 'https://api.bbdcz.cn/music/netease/url?id=' + encodeURIComponent(songId) + '&br=' + br
return httpGet(url, HEADERS_COMMON, 10000).then(function(res) {
if (res && res.data && res.data.url) return { url: res.data.url, platform: 'wy' }
if (res && res.url) return { url: res.url, platform: 'wy' }
throw new Error('no url')
})
}
function lxGetUrl(songId, quality) {
var br = mapBr(quality)
var url = 'https://lxmusicapi.onrender.com/url/wy/' + encodeURIComponent(songId) + '/' + br
return httpGet(url, HEADERS_COMMON, 15000).then(function(res) {
if (res && res.url) return { url: res.url, platform: 'wy' }
throw new Error('no url')
})
}
function ymcGetUrl(songId, quality) {
var br = mapBr(quality)
var url = 'https://api.ymusic.icu/netease/song?id=' + encodeURIComponent(songId) + '&quality=' + br
return httpGet(url, HEADERS_COMMON, 10000).then(function(res) {
if (res && res.data && res.data.url) return { url: res.data.url, platform: 'wy' }
if (res && res.url) return { url: res.url, platform: 'wy' }
throw new Error('no url')
})
}
function unmsGetUrl(songId, quality) {
var br = mapBr(quality)
var url = 'https://unms.zeabur.app/netease/url?id=' + encodeURIComponent(songId) + '&br=' + br
return httpGet(url, HEADERS_COMMON, 10000).then(function(res) {
if (res && res.url) return { url: res.url, platform: 'wy' }
throw new Error('no url')
})
}
function xhGetUrl(songId, quality) {
var br = mapBr(quality)
var url = 'https://music-api.gdstudio.xyz/api.php?types=url&source=netease&id=' + encodeURIComponent(songId) + '&br=' + br
return httpGet(url, HEADERS_COMMON, 10000).then(function(res) {
if (res && res.url) return { url: res.url, platform: 'wy' }
if (res && res.data && res.data.url) return { url: res.data.url, platform: 'wy' }
throw new Error('no url')
})
}
function nxGetUrl(songId, quality) {
var br = mapBr(quality)
var url = 'https://music.nxinxz.com/kgqq/wy.php?id=' + encodeURIComponent(songId) + '&level=' + br + '&type=mp3'
return httpGet(url, HEADERS_COMMON, 10000).then(function(res) {
if (res && res.url) return { url: res.url, platform: 'wy' }
if (res && res.data && res.data.url) return { url: res.data.url, platform: 'wy' }
throw new Error('no url')
})
}
function getUrl(songId, quality) {
var apis = []
apis.push({ name: 'ceru', fn: ceruGetUrl })
apis.push({ name: 'bb', fn: bbGetUrl })
apis.push({ name: 'lx', fn: lxGetUrl })
apis.push({ name: 'ymc', fn: ymcGetUrl })
apis.push({ name: 'unms', fn: unmsGetUrl })
apis.push({ name: 'xh', fn: xhGetUrl })
apis.push({ name: 'nx', fn: nxGetUrl })
apis.push({ name: 'official', fn: wyOfficialUrl })
var promises = []
for (var i = 0; i < apis.length; i++) {
var api = apis[i]
promises.push(
api.fn(songId, quality).then(function(result) {
return { status: 'fulfilled', value: result }
}).catch(function(err) {
return { status: 'rejected', reason: err }
})
)
}
return new Promise(function(resolve, reject) {
Promise.all(promises).then(function(results) {
for (var i = 0; i < results.length; i++) {
if (results[i].status === 'fulfilled') {
resolve(results[i].value)
return
}
}
reject(new Error('all apis failed'))
}).catch(function(e) {
reject(e)
})
})
}
function wyUserPlaylist() {
var uid = ''
var match = PLAYLIST_URL.match(/id=(\d+)/)
if (match) uid = match[1]
if (!uid) {
match = PLAYLIST_URL.match(/(\d+)/)
if (match) uid = match[1]
}
if (!uid) return Promise.resolve({ playlists: [] })
var params = { uid: uid, limit: 1000, offset: 0 }
return weapiRequest('/weapi/user/playlist', params).then(function(res) {
if (!res || !res.playlist) return { playlists: [] }
var list = []
for (var i = 0; i < res.playlist.length; i++) {
var p = res.playlist[i]
list.push({
id: String(p.id),
name: p.name,
picUrl: p.coverImgUrl,
trackCount: p.trackCount,
description: p.description || ''
})
}
return { playlists: list }
}).catch(function(e) { return { playlists: [] } })
}
function wyDailyRecommend() {
return weapiRequest('/weapi/v1/discovery/recommend/songs', {}).then(function(res) {
if (!res || !res.data || !res.data.dailySongs) return { songs: [] }
var songs = []
for (var i = 0; i < res.data.dailySongs.length; i++) {
var s = res.data.dailySongs[i]
var artists = []
if (s.ar) {
for (var j = 0; j < s.ar.length; j++) {
artists.push(s.ar[j].name)
}
}
songs.push({
id: String(s.id),
name: s.name,
artist: artists.join('/'),
album: s.al ? s.al.name : '',
picUrl: s.al ? s.al.picUrl : '',
duration: s.dt || 0,
platform: 'wy'
})
}
return { songs: songs }
}).catch(function(e) { return { songs: [] } })
}
function wyPersonalFm() {
return weapiRequest('/weapi/v1/radio/get', {}).then(function(res) {
if (!res || !res.data) return { songs: [] }
var songs = []
for (var i = 0; i < res.data.length; i++) {
var s = res.data[i]
var artists = []
if (s.ar) {
for (var j = 0; j < s.ar.length; j++) {
artists.push(s.ar[j].name)
}
}
songs.push({
id: String(s.id),
name: s.name,
artist: artists.join('/'),
album: s.al ? s.al.name : '',
picUrl: s.al ? s.al.picUrl : '',
duration: s.dt || 0,
platform: 'wy'
})
}
return { songs: songs }
}).catch(function(e) { return { songs: [] } })
}
function wyMyLikedSongs() {
return weapiRequest('/weapi/song/like/get', {}).then(function(res) {
if (!res || !res.data || !res.data.checkPoint) return { songs: [] }
var ids = res.data.checkPoint
if (!ids || ids.length === 0) return { songs: [] }
var idStr = ''
for (var i = 0; i < ids.length; i++) {
if (i > 0) idStr += ','
idStr += ids[i]
}
var params = { ids: '[' + idStr + ']', csrf_token: '' }
return weapiRequest('/weapi/v3/song/detail', params).then(function(detail) {
if (!detail || !detail.songs) return { songs: [] }
var songs = []
for (var i = 0; i < detail.songs.length; i++) {
var s = detail.songs[i]
var artists = []
if (s.ar) {
for (var j = 0; j < s.ar.length; j++) {
artists.push(s.ar[j].name)
}
}
songs.push({
id: String(s.id),
name: s.name,
artist: artists.join('/'),
album: s.al ? s.al.name : '',
picUrl: s.al ? s.al.picUrl : '',
duration: s.dt || 0,
platform: 'wy'
})
}
return { songs: songs }
})
}).catch(function(e) { return { songs: [] } })
}
var leaderboard = { getList: function() { return Promise.resolve([]) } }
var songList = {
getListDetail: function(id, page, limit) {
var params = { id: id, n: 100000, csrf_token: '' }
return weapiRequest('/weapi/v3/playlist/detail', params).then(function(res) {
if (!res || !res.playlist || !res.playlist.tracks) {
return { list: [], page: page || 1, limit: limit || 30, total: 0, source: 'wy', info: { name: '', img: '', desc: '', author: '' } }
}
var tracks = res.playlist.tracks
var list = []
for (var i = 0; i < tracks.length; i++) {
var s = tracks[i]
var artists = []
if (s.ar) {
for (var j = 0; j < s.ar.length; j++) {
artists.push(s.ar[j].name)
}
}
list.push({
id: String(s.id),
name: s.name || '',
artists: artists.join('、'),
source: 'wy',
pic: s.al ? s.al.picUrl : '',
mPic: s.al ? s.al.picUrl : '',
sPic: s.al ? s.al.picUrl : '',
albumName: s.al ? s.al.name : '',
albumId: s.al ? String(s.al.id) : '',
interval: formatPlayTime(s.dt),
qualities: {}
})
}
return {
list: list,
page: page || 1,
limit: limit || 30,
total: list.length,
source: 'wy',
info: {
name: res.playlist.name || '',
img: res.playlist.coverImgUrl || '',
desc: res.playlist.description || '',
author: res.playlist.creator ? res.playlist.creator.nickname : ''
}
}
}).catch(function(e) {
return { list: [], page: page || 1, limit: limit || 30, total: 0, source: 'wy', info: { name: '', img: '', desc: '', author: '' } }
})
}
}
var singer = { getInfo: function(id) { return Promise.resolve(null) } }
var album = {
getListDetail: function(id) {
var params = { id: id, csrf_token: '' }
return weapiRequest('/weapi/v1/album/' + id, params).then(function(res) {
if (!res || !res.album || !res.songs) {
return { list: [], page: 1, limit: 1000, total: 0, source: 'wy', info: { name: '', img: '', desc: '', author: '' } }
}
var list = []
for (var i = 0; i < res.songs.length; i++) {
var s = res.songs[i]
var artists = []
if (s.ar) {
for (var j = 0; j < s.ar.length; j++) {
artists.push(s.ar[j].name)
}
}
list.push({
id: String(s.id),
name: s.name || '',
artists: artists.join('、'),
source: 'wy',
pic: res.album.picUrl || '',
mPic: res.album.picUrl || '',
sPic: res.album.picUrl || '',
albumName: res.album.name || '',
albumId: String(res.album.id || ''),
interval: formatPlayTime(s.dt),
qualities: {}
})
}
return {
list: list,
page: 1,
limit: 1000,
total: list.length,
source: 'wy',
info: {
name: res.album.name || '',
img: res.album.picUrl || '',
desc: res.album.description || '',
author: res.album.artist ? res.album.artist.name : ''
}
}
}).catch(function(e) {
return { list: [], page: 1, limit: 1000, total: 0, source: 'wy', info: { name: '', img: '', desc: '', author: '' } }
})
},
search: function(str, page, limit) { return Promise.resolve([]) }
}
function getLyric(id) {
var params = { id: id, lv: -1, tv: -1, rv: -1, kv: -1, csrf_token: '' }
return weapiRequest('/weapi/song/lyric', params).then(function(res) {
if (!res) return ''
return {
lrc: res.lrc ? res.lrc.lyric || '' : '',
krc: res.krc ? res.krc.lyric || '' : '',
translate: res.tlyric ? res.tlyric.lyric || '' : ''
}
}).catch(function(e) { return '' })
}
function getPic(songId) { return Promise.resolve('') }
function musicDetail(id) { return Promise.resolve(null) }
function musicInfo(id) { return Promise.resolve(null) }
var musicSearch = {
search: function(keyword, page, limit) {
return wySearch(keyword, page, limit).catch(function(e) {
return { list: [], allPage: 1, limit: 30, total: 0, source: 'wy' }
})
},
tipSearch: function(keyword) {
return wyTipSearch(keyword).catch(function(e) { return [] })
},
hotSearch: function() {
return wyHotSearch().catch(function(e) { return [] })
}
}
var tipSearch = {
getList: function(str) {
return wyTipSearch(str).catch(function(e) { return [] })
}
}
var hotSearch = {
getList: function() {
return wyHotSearch().catch(function(e) { return [] })
}
}
var pluginInfo = {
info: { id: 'koneko_wy', name: '网易云音乐 - Koneko', version: '0.0.2', description: '网易云音乐聚合音源插件,官方搜索+多API音源自动测速容灾切换支持Cookie功能' },
env: [
{ key: 'ceru_key', name: '聆澜API Key', description: '聆澜音源API密钥留空则跳过聆澜音源' },
{ key: 'playlist_url', name: '个人主页链接', description: '网易云音乐个人主页链接,用于获取个人歌单' },
{ key: 'cookie', name: 'Cookie', description: '网易云音乐Cookie用于每日推荐/私人FM/我喜欢的音乐/歌单/专辑/歌词等' }
],
ext: [
{ name: '个人歌单', description: '通过分享链接获取个人歌单', entry: 'plugin.userPlaylist()', type: 'playlists' },
{ name: '每日推荐', description: '获取每日推荐歌曲', entry: 'plugin.dailyRecommend()', type: 'songs' },
{ name: '私人FM', description: '获取私人FM歌曲', entry: 'plugin.personalFm()', type: 'songs' },
{ name: '我喜欢的音乐', description: '获取我喜欢的音乐列表', entry: 'plugin.myLikedSongs()', type: 'songs' }
],
quality: [
{ name: '标准音质', ui: '标', id: 'standard' },
{ name: '高品音质', ui: 'HQ', id: 'exhigh' },
{ name: '无损音质', ui: 'SQ', id: 'lossless' },
{ name: 'Hi-Res', ui: 'HR', id: 'hires' },
{ name: '高清环绕声', ui: 'DB', id: 'jyeffect' },
{ name: '沉浸环绕声', ui: 'SK', id: 'sky' },
{ name: '超清母带', ui: 'MT', id: 'jymaster' }
],
supportFunc: []
}
module.exports = {
musicSearch: musicSearch,
tipSearch: tipSearch,
leaderboard: leaderboard,
songList: songList,
hotSearch: hotSearch,
singer: singer,
album: album,
getLyric: getLyric,
getPic: getPic,
getUrl: getUrl,
musicDetail: musicDetail,
musicInfo: musicInfo,
pluginInfo: pluginInfo,
userPlaylist: function() {
return wyUserPlaylist().catch(function(e) { return { playlists: [] } })
},
dailyRecommend: function() {
return wyDailyRecommend().catch(function(e) { return { songs: [] } })
},
personalFm: function() {
return wyPersonalFm().catch(function(e) { return { songs: [] } })
},
myLikedSongs: function() {
return wyMyLikedSongs().catch(function(e) { return { songs: [] } })
}
}