diff --git a/dist/worker.js b/dist/worker.js index 6595041..fd07b48 100644 --- a/dist/worker.js +++ b/dist/worker.js @@ -1,4 +1,4 @@ -!function(t){self.webpackChunk=function(e,n){for(var i in n)t[i]=n[i];for(;e.length;)r[e.pop()]=1};var e={},r={0:1},n={};var i={11:function(){return{"./index_bg.js":{__wbindgen_throw:function(t,r){return e[10].exports.b(t,r)}}}}};function o(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,o),n.l=!0,n.exports}o.e=function(t){var e=[];return e.push(Promise.resolve().then((function(){r[t]||importScripts(o.p+""+t+".worker.js")}))),({1:[11]}[t]||[]).forEach((function(t){var r=n[t];if(r)e.push(r);else{var a,s=i[t](),u=fetch(o.p+""+{11:"4371fde8bf684fe55fb6"}[t]+".module.wasm");if(s instanceof Promise&&"function"==typeof WebAssembly.compileStreaming)a=Promise.all([WebAssembly.compileStreaming(u),s]).then((function(t){return WebAssembly.instantiate(t[0],t[1])}));else if("function"==typeof WebAssembly.instantiateStreaming)a=WebAssembly.instantiateStreaming(u,s);else{a=u.then((function(t){return t.arrayBuffer()})).then((function(t){return WebAssembly.instantiate(t,s)}))}e.push(n[t]=a.then((function(e){return o.w[t]=(e.instance||e).exports})))}})),Promise.all(e)},o.m=t,o.c=e,o.d=function(t,e,r){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)o.d(r,n,function(e){return t[e]}.bind(null,n));return r},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o.w={},o(o.s=0)}([function(t,e,r){(function(t){const e=r(6),n=r(7);addEventListener("fetch",t=>{t.respondWith(async function(t){const n=new e;n.get("/",()=>async function(){return new Response(r(8),{status:200,headers:{"content-type":"text/html"}})}()),n.get("/track/-?\\d+",()=>o("track",t)),n.get("/album/\\d+",()=>o("album",t)),n.get("/playlist/\\d+",()=>o("playlist",t));return await n.route(t)}(t.request))});const i={aac_96:"8",64:"10",128:"1",320:"3",flac:"9",mp4_ra1:"13",mp4_ra2:"14",mp4_ra3:"15",mhm1_ra1:"16",mhm1_ra2:"17",mhm1:"18",sbc_256:"12",misc:"0"};async function o(e,o){const a=new URL(o.url),s=a.pathname.split("/")[2];if(format=a.searchParams.get("f"),null===format)format="320";else if(format=format.toLowerCase(),void 0===i[format]){if(index=Object.values(i).indexOf(format),-1===index)return new Response("Invalid format",{status:400,headers:{"content-type":"text/plain"}});format=Object.keys(i)[index]}let u=a.searchParams.get("t");switch(u=("true"===u||"1"===u)&&["misc","128","320"].includes(format),e){case"track":return await async function(e,o,a){const s=await fetch(`https://api.deezer.com/streaming_url.php?access_token=${ACCESS_TOKEN}&track_id=${e}`),u=await s.json();if(void 0!==u.error)return new Response(JSON.stringify(u.error),{status:403,headers:{"content-type":"application/json"}});const f=await r.e(1).then(r.bind(null,9));if(encrypted=!["320","flac"].includes(o),encrypted){if(result=await function(t,e,r){t.alternative&&(t=t.alternative);const n=t.id,o=t.md5_origin,a=t.media_version;n<0&&(e="misc");if(0==t["filesize_"+e])return new Response("Format unavailable",{status:403,headers:{"content-type":"text/plain"}});return r(o,e=i[e],n.toString(),a)}(u,o,f.legacy_stream_url),"object"==typeof result)return result}else{if(result=u["url_"+o],void 0===result)return new Response("Format unavailable",{status:403,headers:{"content-type":"text/plain"}});result=f.decrypt_stream_url(result)}let h,c;if((a||encrypted)&&(h=await fetch(result),200!==h.status))return new Response("Couldn't get track stream",{status:403,headers:{"content-type":"text/plain"}});if(a){if(c=new n(t.alloc(0)),c.padding=0,c.setFrame("TIT2",u.title).setFrame("TALB",u.album.title).setFrame("TPE2",u.artist.name),void 0!==u.contributors){contr_list=[];for(const t of u.contributors)contr_list.push(t.name);c.setFrame("TPE1",contr_list)}if(void 0!==u.track_position&&c.setFrame("TRCK",u.track_position),void 0!==u.disk_number&&c.setFrame("TPOS",u.disk_number),""!==u.isrc&&c.setFrame("TSRC",u.isrc),void 0!==u.bpm&&c.setFrame("TBPM",u.bpm),void 0!==u.release_date){const t=u.release_date.split("-");c.setFrame("TYER",t[0]),c.setFrame("TDAT",t[2]+t[1])}if(""!==u.md5_image){const t=`https://cdns-images.dzcdn.net/images/cover/${u.md5_image}/1000x1000-000000-80-0-0.jpg`,e=await fetch(t),r=await e.arrayBuffer();c.setFrame("APIC",{type:3,data:r,description:"cover"})}c.addTag()}let l,{readable:p,writable:g}=new TransformStream;(a||encrypted)&&(l=g.getWriter());a&&l.write(c.arrayBuffer);u.alternative&&(e=u.alternative.id.toString());if(encrypted){const t=new f.Cipher(e),r=parseInt(h.headers.get("Content-Length"));!async function(t,e,r,n){const i=e.getReader({mode:"byob"});let o=0,a=!1;for(;!a;){let e;a=o+2048>r,e=a?new Int8Array(r-o):new Int8Array(2048);let s=0;for(;s!==e.length;){let t=(await i.read(new Int8Array(e.length-s))).value;e.set(t,s),s+=t.length}o%6144!=0||a||n.decrypt_chunk(e),await t.write(e),o+=2048}await i.cancel(),await t.close()}(l,h.body,r,t)}else{if(!a)return new Response(null,{status:302,headers:{location:result}});l.releaseLock(),h.body.pipeTo(g)}return new Response(p,{status:200,headers:{"content-type":"audio/mpeg"}})}(s,format,u);case"album":case"playlist":return await async function(t,e,r,n,i){const o=await fetch(`https://api.deezer.com/${t}/${e}?access_token=${ACCESS_TOKEN}&limit=-1`),a=await o.json();if(void 0!==a.error)return new Response(JSON.stringify(a.error),{status:403,headers:{"content-type":"application/json"}});let s="#EXTM3U\n";for(const t of a.tracks.data){t.id<0&&(r="misc");let e=`https://${i}/track/${t.id}?f=${r}&t=${+n}`;s+=`#EXTINF:${t.duration},${t.title}\n${e}\n`}return new Response(s,{status:200,headers:{"content-type":"audio/mpegurl"}})}(e,s,format,u,a.host)}}}).call(this,r(1).Buffer)},function(t,e,r){"use strict";(function(t){ +!function(t){self.webpackChunk=function(e,n){for(var i in n)t[i]=n[i];for(;e.length;)r[e.pop()]=1};var e={},r={0:1},n={};var i={11:function(){return{"./index_bg.js":{__wbindgen_throw:function(t,r){return e[10].exports.b(t,r)}}}}};function o(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,o),n.l=!0,n.exports}o.e=function(t){var e=[];return e.push(Promise.resolve().then((function(){r[t]||importScripts(o.p+""+t+".worker.js")}))),({1:[11]}[t]||[]).forEach((function(t){var r=n[t];if(r)e.push(r);else{var a,s=i[t](),u=fetch(o.p+""+{11:"4371fde8bf684fe55fb6"}[t]+".module.wasm");if(s instanceof Promise&&"function"==typeof WebAssembly.compileStreaming)a=Promise.all([WebAssembly.compileStreaming(u),s]).then((function(t){return WebAssembly.instantiate(t[0],t[1])}));else if("function"==typeof WebAssembly.instantiateStreaming)a=WebAssembly.instantiateStreaming(u,s);else{a=u.then((function(t){return t.arrayBuffer()})).then((function(t){return WebAssembly.instantiate(t,s)}))}e.push(n[t]=a.then((function(e){return o.w[t]=(e.instance||e).exports})))}})),Promise.all(e)},o.m=t,o.c=e,o.d=function(t,e,r){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)o.d(r,n,function(e){return t[e]}.bind(null,n));return r},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o.w={},o(o.s=0)}([function(t,e,r){(function(t){const e=r(6),n=r(7);addEventListener("fetch",t=>{t.respondWith(async function(t){const n=new e;n.get("/",()=>async function(){return new Response(r(8),{status:200,headers:{"content-type":"text/html"}})}()),n.get("/track/-?\\d+",()=>o("track",t)),n.get("/album/\\d+",()=>o("album",t)),n.get("/playlist/\\d+",()=>o("playlist",t));return await n.route(t)}(t.request))});const i={aac_96:"8",64:"10",128:"1",320:"3",flac:"9",mp4_ra1:"13",mp4_ra2:"14",mp4_ra3:"15",mhm1_ra1:"16",mhm1_ra2:"17",mhm1:"18",sbc_256:"12",misc:"0"};async function o(e,o){const a=new URL(o.url),s=a.pathname.split("/")[2];if(format=a.searchParams.get("f"),null===format)format="320";else if(format=format.toLowerCase(),void 0===i[format]){if(index=Object.values(i).indexOf(format),-1===index)return new Response("Invalid format",{status:400,headers:{"content-type":"text/plain"}});format=Object.keys(i)[index]}let u=a.searchParams.get("t");switch(u=("true"===u||"1"===u)&&["misc","128","320"].includes(format),e){case"track":return await async function(e,o,a){const s=await fetch(`https://api.deezer.com/streaming_url.php?access_token=${ACCESS_TOKEN}&track_id=${e}`),u=await s.json();if(void 0!==u.error)return new Response(JSON.stringify(u.error),{status:403,headers:{"content-type":"application/json"}});u.id<0&&(o="misc");const f=await r.e(1).then(r.bind(null,9));if(encrypted=!["320","flac"].includes(o),encrypted){if(result=await function(t,e,r){t.alternative&&(t=t.alternative);const n=t.id,o=t.md5_origin,a=t.media_version;if(0==t["filesize_"+e])return new Response("Format unavailable",{status:403,headers:{"content-type":"text/plain"}});return r(o,e=i[e],n.toString(),a)}(u,o,f.legacy_stream_url),"object"==typeof result)return result}else{if(result=u["url_"+o],void 0===result)return new Response("Format unavailable",{status:403,headers:{"content-type":"text/plain"}});result=f.decrypt_stream_url(result)}let h,c;if((a||encrypted)&&(h=await fetch(result),200!==h.status))return new Response("Couldn't get track stream",{status:403,headers:{"content-type":"text/plain"}});if(a){if(c=new n(t.alloc(0)),c.padding=0,c.setFrame("TIT2",u.title).setFrame("TALB",u.album.title).setFrame("TPE2",u.artist.name),void 0!==u.contributors){contr_list=[];for(const t of u.contributors)contr_list.push(t.name);c.setFrame("TPE1",contr_list)}if(void 0!==u.track_position&&c.setFrame("TRCK",u.track_position),void 0!==u.disk_number&&c.setFrame("TPOS",u.disk_number),""!==u.isrc&&c.setFrame("TSRC",u.isrc),void 0!==u.bpm&&c.setFrame("TBPM",u.bpm),void 0!==u.release_date){const t=u.release_date.split("-");c.setFrame("TYER",t[0]),c.setFrame("TDAT",t[2]+t[1])}if(""!==u.md5_image){const t=`https://cdns-images.dzcdn.net/images/cover/${u.md5_image}/1000x1000-000000-80-0-0.jpg`,e=await fetch(t),r=await e.arrayBuffer();c.setFrame("APIC",{type:3,data:r,description:"cover"})}c.addTag()}let l,{readable:p,writable:g}=new TransformStream;(a||encrypted)&&(l=g.getWriter());a&&l.write(c.arrayBuffer);u.alternative&&(e=u.alternative.id.toString());if(encrypted){const t=new f.Cipher(e),r=parseInt(h.headers.get("Content-Length"));!async function(t,e,r,n){const i=e.getReader({mode:"byob"});let o=0,a=!1;for(;!a;){let e;a=o+2048>r,e=a?new Int8Array(r-o):new Int8Array(2048);let s=0;for(;s!==e.length;){let t=(await i.read(new Int8Array(e.length-s))).value;e.set(t,s),s+=t.length}o%6144!=0||a||n.decrypt_chunk(e),await t.write(e),o+=2048}await i.cancel(),await t.close()}(l,h.body,r,t)}else{if(!a)return new Response(null,{status:302,headers:{location:result}});l.releaseLock(),h.body.pipeTo(g)}return new Response(p,{status:200,headers:{"content-type":"audio/mpeg"}})}(s,format,u);case"album":case"playlist":return await async function(t,e,r,n,i){const o=await fetch(`https://api.deezer.com/${t}/${e}?access_token=${ACCESS_TOKEN}&limit=-1`),a=await o.json();if(void 0!==a.error)return new Response(JSON.stringify(a.error),{status:403,headers:{"content-type":"application/json"}});let s="#EXTM3U\n";for(const t of a.tracks.data){t.id<0&&(r="misc");let e=`https://${i}/track/${t.id}?f=${r}&t=${+n}`;s+=`#EXTINF:${t.duration},${t.title}\n${e}\n`}return new Response(s,{status:200,headers:{"content-type":"audio/mpegurl"}})}(e,s,format,u,a.host)}}}).call(this,r(1).Buffer)},function(t,e,r){"use strict";(function(t){ /*! * The buffer module from node.js, for the browser. * diff --git a/index.js b/index.js index 446f3b2..e600aea 100644 --- a/index.js +++ b/index.js @@ -58,6 +58,10 @@ async function track(id, format, tagging) { return new Response(JSON.stringify(json.error), { status: 403, headers: { 'content-type': "application/json" } }) } + if (json.id < 0) { // user-uploaded track + format = 'misc' + } + const wasm = await import('./pkg') encrypted = !['320', 'flac'].includes(format) @@ -213,10 +217,6 @@ function legacy_track_url(json, format, url_func) { const md5_origin = json.md5_origin const media_version = json.media_version - if (id < 0) { // user-uploaded track - format = 'misc' - } - if (json['filesize_' + format] == false) { return new Response('Format unavailable', { status: 403, headers: { 'content-type': 'text/plain' } }) }