UNPKG

simple-netease-cloud-music

Version:
7 lines 7.86 kB
/** * @file simple-netease-cloud-music v0.5.2 * @copyright Copyright (c) Surmon. All rights reserved. * @license Released under the MIT License. * @author Surmon <https://github.com/surmon-china> */ "use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=e(require("http")),i=e(require("crypto"));function o(e,t){return Object.prototype.hasOwnProperty.call(e,t)}var r=function(e,t,i,r){t=t||"&",i=i||"=";var n={};if("string"!=typeof e||0===e.length)return n;var a=/\+/g;e=e.split(t);var l=1e3;r&&"number"==typeof r.maxKeys&&(l=r.maxKeys);var s=e.length;l>0&&s>l&&(s=l);for(var p=0;p<s;++p){var u,c,d,h,f=e[p].replace(a,"%20"),m=f.indexOf(i);m>=0?(u=f.substr(0,m),c=f.substr(m+1)):(u=f,c=""),d=decodeURIComponent(u),h=decodeURIComponent(c),o(n,d)?Array.isArray(n[d])?n[d].push(h):n[d]=[n[d],h]:n[d]=h}return n},n=function(e){switch(typeof e){case"string":return e;case"boolean":return e?"true":"false";case"number":return isFinite(e)?e:"";default:return""}},a=function(e,t,i,o){return t=t||"&",i=i||"=",null===e&&(e=void 0),"object"==typeof e?Object.keys(e).map((function(o){var r=encodeURIComponent(n(o))+i;return Array.isArray(e[o])?e[o].map((function(e){return r+encodeURIComponent(n(e))})).join(t):r+encodeURIComponent(n(e[o]))})).join(t):o?encodeURIComponent(n(o))+i+encodeURIComponent(n(e)):""},l=function(e,t){return e(t={exports:{}},t.exports),t.exports}((function(e,t){t.decode=t.parse=r,t.encode=t.stringify=a})),s=(l.decode,l.parse,l.encode,l.stringify,Symbol("neteaseAESECB")),p=Symbol("getHttpOption"),u=Symbol("getRandomHex"),c=Symbol("makeRequest"),d=function(){function e(e){void 0===e&&(e={}),this.cookie="",e.cookie&&(this.cookie=e.cookie)}return e.prototype[s]=function(e){var t=Buffer.from("7246674226682325323F5E6544673A51","hex").toString("utf8"),o=i.createCipheriv("aes-128-ecb",t,""),r=o.update(JSON.stringify(e),"utf8","hex")+o.final("hex");return l.stringify({eparams:r.toUpperCase()})},e.prototype[p]=function(e,t,i){var o,r,n,a={port:80,path:t,method:e,hostname:"music.163.com",headers:{referer:"https://music.163.com/",cookie:this.cookie||(r=this[u](128),n=["os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true","MUSIC_U="+r+"; buildver=1506310743; resolution=1920x1080; mobilename=MI5; osver=7.0.1; channel=coolapk; os=android; appver=4.2.0","osver=%E7%89%88%E6%9C%AC%2010.13.3%EF%BC%88%E7%89%88%E5%8F%B7%2017D47%EF%BC%89; os=osx; appver=1.5.9; MUSIC_U="+r+"; channel=netease;"],n[Math.floor(Math.random()*n.length)]),"user-agent":(o=["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36","Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1","Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1","Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36","Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36","Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89;GameHelper","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4","Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:46.0) Gecko/20100101 Firefox/46.0","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:46.0) Gecko/20100101 Firefox/46.0","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)","Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)","Mozilla/5.0 (Windows NT 6.3; Win64, x64; Trident/7.0; rv:11.0) like Gecko","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/13.10586","Mozilla/5.0 (iPad; CPU OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1"],o[Math.floor(Math.random()*o.length)])}};return"POST"===e&&(a.headers["Content-Type"]="application/x-www-form-urlencoded",i&&(a.headers["Content-Length"]=i)),a},e.prototype[u]=function(e){var t=e%2,o=i.randomFillSync(Buffer.alloc((e+t)/2)).toString("hex");return t?o.slice(1):o},e.prototype[c]=function(e,i){return new Promise((function(o,r){var n=t.request(e,(function(e){e.setEncoding("utf8");var t="";e.statusCode&&e.statusCode>=400&&r("Request to "+e.url+" failed with HTTP "+e.statusCode),e.on("data",(function(e){return t+=e.toString()})),e.on("end",(function(){if(!t)return r("remote result empty");try{return o(JSON.parse(t))}catch(e){return o(t)}}))}));n.on("error",(function(e){console.error("problem with request: "+e.message)})),i&&n.write(i),n.end()}))},e.prototype.search=function(e,t,i){void 0===t&&(t=1),void 0===i&&(i=3);var o={method:"POST",params:{s:e,type:1,limit:i,total:!0,offset:t-1},url:"https://music.163.com/api/cloudsearch/pc"},r=this[s](o),n=this[p]("POST","/api/linux/forward",Buffer.byteLength(r));return this[c](n,r)},e.prototype.artist=function(e,t){void 0===t&&(t=50);var i={method:"GET",params:{id:e,ext:!0,top:t},url:"https://music.163.com/api/v1/artist/"+e},o=this[s](i),r=this[p]("POST","/api/linux/forward",Buffer.byteLength(o));return this[c](r,o)},e.prototype.playlist=function(e,t){void 0===t&&(t=1e3);var i={method:"POST",params:{id:e,n:t},url:"https://music.163.com/api/v3/playlist/detail"},o=this[s](i),r=this[p]("POST","/api/linux/forward",Buffer.byteLength(o));return this[c](r,o)},e.prototype._playlist=function(e,t){void 0===t&&(t=1e3);var i={method:"POST",params:{id:e,n:t},url:"/api/v2/playlist/detail"};i.url+="?"+l.stringify(i.params);var o=this[p](i.method,i.url);return this[c](o)},e.prototype.album=function(e){var t={method:"GET",params:{id:e},url:"https://music.163.com/api/v1/album/"+e},i=this[s](t),o=this[p]("POST","/api/linux/forward",Buffer.byteLength(i));return this[c](o,i)},e.prototype.song=function(e){var t={method:"POST",params:{c:"["+(Array.isArray(e)?e:[e]).map((function(e){return"{id: "+e+"}"})).join(",")+"]"},url:"https://music.163.com/api/v3/song/detail"},i=this[s](t),o=this[p]("POST","/api/linux/forward",Buffer.byteLength(i));return this[c](o,i)},e.prototype.url=function(e,t){void 0===t&&(t=320);var i={method:"POST",params:{ids:Array.isArray(e)?e:[e],br:1e3*t},url:"https://music.163.com/api/song/enhance/player/url"},o=this[s](i),r=this[p]("POST","/api/linux/forward",Buffer.byteLength(o));return this[c](r,o)},e.prototype.lyric=function(e){var t={method:"POST",params:{id:e,os:"linux",lv:-1,kv:-1,tv:-1},url:"https://music.163.com/api/song/lyric"},i=this[s](t),o=this[p]("POST","/api/linux/forward",Buffer.byteLength(i));return this[c](o,i)},e.prototype.picture=function(e,t){void 0===t&&(t=300);return Promise.resolve({url:"https://p3.music.126.net/"+function(e){e=String(e);var t,o,r="3go8&$8*3*3h0k(2)2".split(""),n=e.split("").map((function(e,t){return String.fromCharCode(e.charCodeAt(0)^r[t%r.length].charCodeAt(0))}));return(t=n.join(""),o=Buffer.from(t).toString("binary"),i.createHash("md5").update(o).digest("base64")).replace(/\//g,"_").replace(/\+/g,"-")}(e)+"/"+e+".jpg?param="+t+"y"+t})},e}();module.exports=d;