UNPKG

simple-netease-cloud-music

Version:
8 lines (7 loc) 7.75 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> */ import e from"http";import t from"crypto";function o(e,t){return Object.prototype.hasOwnProperty.call(e,t)}var i,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,h,d,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=""),h=decodeURIComponent(u),d=decodeURIComponent(c),o(n,h)?Array.isArray(n[h])?n[h].push(d):n[h]=[n[h],d]:n[h]=d}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,o,i){return t=t||"&",o=o||"=",null===e&&(e=void 0),"object"==typeof e?Object.keys(e).map((function(i){var r=encodeURIComponent(n(i))+o;return Array.isArray(e[i])?e[i].map((function(e){return r+encodeURIComponent(n(e))})).join(t):r+encodeURIComponent(n(e[i]))})).join(t):i?encodeURIComponent(n(i))+o+encodeURIComponent(n(e)):""},l=(function(e,t){t.decode=t.parse=r,t.encode=t.stringify=a}(i={exports:{}},i.exports),i.exports),s=(l.decode,l.parse,l.encode,l.stringify,Symbol("neteaseAESECB")),p=Symbol("getHttpOption"),u=Symbol("getRandomHex"),c=Symbol("makeRequest"),h=function(){function o(e){void 0===e&&(e={}),this.cookie="",e.cookie&&(this.cookie=e.cookie)}return o.prototype[s]=function(e){var o=Buffer.from("7246674226682325323F5E6544673A51","hex").toString("utf8"),i=t.createCipheriv("aes-128-ecb",o,""),r=i.update(JSON.stringify(e),"utf8","hex")+i.final("hex");return l.stringify({eparams:r.toUpperCase()})},o.prototype[p]=function(e,t,o){var i,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":(i=["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"],i[Math.floor(Math.random()*i.length)])}};return"POST"===e&&(a.headers["Content-Type"]="application/x-www-form-urlencoded",o&&(a.headers["Content-Length"]=o)),a},o.prototype[u]=function(e){var o=e%2,i=t.randomFillSync(Buffer.alloc((e+o)/2)).toString("hex");return o?i.slice(1):i},o.prototype[c]=function(t,o){return new Promise((function(i,r){var n=e.request(t,(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 i(JSON.parse(t))}catch(e){return i(t)}}))}));n.on("error",(function(e){console.error("problem with request: "+e.message)})),o&&n.write(o),n.end()}))},o.prototype.search=function(e,t,o){void 0===t&&(t=1),void 0===o&&(o=3);var i={method:"POST",params:{s:e,type:1,limit:o,total:!0,offset:t-1},url:"https://music.163.com/api/cloudsearch/pc"},r=this[s](i),n=this[p]("POST","/api/linux/forward",Buffer.byteLength(r));return this[c](n,r)},o.prototype.artist=function(e,t){void 0===t&&(t=50);var o={method:"GET",params:{id:e,ext:!0,top:t},url:"https://music.163.com/api/v1/artist/"+e},i=this[s](o),r=this[p]("POST","/api/linux/forward",Buffer.byteLength(i));return this[c](r,i)},o.prototype.playlist=function(e,t){void 0===t&&(t=1e3);var o={method:"POST",params:{id:e,n:t},url:"https://music.163.com/api/v3/playlist/detail"},i=this[s](o),r=this[p]("POST","/api/linux/forward",Buffer.byteLength(i));return this[c](r,i)},o.prototype._playlist=function(e,t){void 0===t&&(t=1e3);var o={method:"POST",params:{id:e,n:t},url:"/api/v2/playlist/detail"};o.url+="?"+l.stringify(o.params);var i=this[p](o.method,o.url);return this[c](i)},o.prototype.album=function(e){var t={method:"GET",params:{id:e},url:"https://music.163.com/api/v1/album/"+e},o=this[s](t),i=this[p]("POST","/api/linux/forward",Buffer.byteLength(o));return this[c](i,o)},o.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"},o=this[s](t),i=this[p]("POST","/api/linux/forward",Buffer.byteLength(o));return this[c](i,o)},o.prototype.url=function(e,t){void 0===t&&(t=320);var o={method:"POST",params:{ids:Array.isArray(e)?e:[e],br:1e3*t},url:"https://music.163.com/api/song/enhance/player/url"},i=this[s](o),r=this[p]("POST","/api/linux/forward",Buffer.byteLength(i));return this[c](r,i)},o.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"},o=this[s](t),i=this[p]("POST","/api/linux/forward",Buffer.byteLength(o));return this[c](i,o)},o.prototype.picture=function(e,o){void 0===o&&(o=300);return Promise.resolve({url:"https://p3.music.126.net/"+function(e){e=String(e);var o,i,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(o=n.join(""),i=Buffer.from(o).toString("binary"),t.createHash("md5").update(i).digest("base64")).replace(/\//g,"_").replace(/\+/g,"-")}(e)+"/"+e+".jpg?param="+o+"y"+o})},o}();export default h;