From 756221a4b1955bd526d0c43aa484fea7cfe61e18 Mon Sep 17 00:00:00 2001 From: uh wot Date: Mon, 16 Aug 2021 18:03:20 +0200 Subject: [PATCH] edited regex on router stuff --- dist/worker.js | 2 +- index.js | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/dist/worker.js b/dist/worker.js index 0ed5167..e8d88e9 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={10:function(){return{"./index_bg.js":{__wbindgen_object_drop_ref:function(t){return e[9].exports.e(t)},__wbg_buffer_9e184d6f785de5ed:function(t){return e[9].exports.b(t)},__wbg_newwithbyteoffsetandlength_e57ad1f2ce812c03:function(t,r,n){return e[9].exports.c(t,r,n)},__wbindgen_throw:function(t,r){return e[9].exports.f(t,r)},__wbindgen_memory:function(){return e[9].exports.d()}}}}};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:[10]}[t]||[]).forEach((function(t){var r=n[t];if(r)e.push(r);else{var a,s=i[t](),u=fetch(o.p+""+{10:"1b2a941e0e6bd21031c4"}[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 r=new e;r.get("/track/.*",()=>o("track",t)),r.get("/album/.*",()=>o("album",t)),r.get("/playlist/.*",()=>o("playlist",t));return await r.route(t)}(t.request))});const i={aac_96:"8",64:"10",128:"1",320:"3",ra1:"13",ra2:"14",ra3:"15",sbc_256:"12",misc:"0"};async function o(e,o){if(access_token=await KV.get("access_token"),null===access_token){const t=await fetch("https://connect.deezer.com/oauth/access_token.php?grant_type=client_credentials&client_id=447462&client_secret=a83bf7f38ad2f137e444727cfc3775cf&output=json"),e=await t.json();if(void 0!==e.error)return new Response("Couldn't get access token from Deezer",{status:500,headers:{"content-type":"text/plain"}});access_token=e.access_token,await KV.put("access_token",access_token,{expirationTtl:parseInt(e.expires)})}const a=new URL(o.url),s=a.pathname.split("/")[2];if(""===s)return new Response("ID needs to be specified",{status:400,headers:{"content-type":"text/plain"}});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,e){case"track":return await async function(e,o,a,s){const u=await fetch(`https://api.deezer.com/track/${e}?access_token=${a}`),f=await u.json();if(void 0!==f.error)return new Response(JSON.stringify(f.error),{status:403,headers:{"content-type":"application/json"}});const c=await r.e(1).then(r.bind(null,8));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)}(f,o,c.stream_url),"object"==typeof result)return result;const h=await fetch(result);if(200!==h.status)return new Response("Couldn't get track stream",{status:403,headers:{"content-type":"text/plain"}});let l;if(s){if(l=new n(t.alloc(0)),l.padding=0,l.setFrame("TIT2",f.title).setFrame("TALB",f.album.title).setFrame("TPE2",f.artist.name),void 0!==f.contributors){contr_list=[];for(const t of f.contributors)contr_list.push(t.name);l.setFrame("TPE1",contr_list)}if(void 0!==f.track_position&&l.setFrame("TRCK",f.track_position),void 0!==f.disk_number&&l.setFrame("TPOS",f.disk_number),""!==f.isrc&&l.setFrame("TSRC",f.isrc),void 0!==f.bpm&&l.setFrame("TBPM",f.bpm),void 0!==f.release_date){const t=f.release_date.split("-");l.setFrame("TYER",t[0]),l.setFrame("TDAT",t[2]+t[1])}if(""!==f.md5_image){const t=`https://cdns-images.dzcdn.net/images/cover/${f.md5_image}/1000x1000-000000-80-0-0.jpg`,e=await fetch(t),r=await e.arrayBuffer();l.setFrame("APIC",{type:3,data:r,description:"cover"})}l.addTag()}let{readable:p,writable:g}=new TransformStream;const d=g.getWriter();l&&d.write(l.arrayBuffer);f.alternative&&(e=f.alternative.id.toString());const y=new c.Cipher(e),w=parseInt(h.headers.get("Content-Length"));return 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||(e=n.decrypt_chunk(e)),await t.write(e),o+=2048}await i.cancel(),await t.close()}(d,h.body,w,y),new Response(p,{status:200,headers:{"content-type":"audio/mpeg"}})}(s,format,access_token,u);case"album":case"playlist":return await async function(t,e,r,n,i,o){const a=await fetch(`https://api.deezer.com/${t}/${e}?access_token=${n}&limit=-1`),s=await a.json();if(void 0!==s.error)return new Response(JSON.stringify(s.error),{status:403,headers:{"content-type":"application/json"}});let u="#EXTM3U\n";for(const t of s.tracks.data){t.id<0&&(r="misc");let e=`https://${o}/track/${t.id}?f=${r}&t=${+i}`;u+=`#EXTINF:${t.duration},${t.title}\n${e}\n`}return new Response(u,{status:200,headers:{"content-type":"audio/mpegurl"}})}(e,s,format,access_token,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={10:function(){return{"./index_bg.js":{__wbindgen_object_drop_ref:function(t){return e[9].exports.e(t)},__wbg_buffer_9e184d6f785de5ed:function(t){return e[9].exports.b(t)},__wbg_newwithbyteoffsetandlength_e57ad1f2ce812c03:function(t,r,n){return e[9].exports.c(t,r,n)},__wbindgen_throw:function(t,r){return e[9].exports.f(t,r)},__wbindgen_memory:function(){return e[9].exports.d()}}}}};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:[10]}[t]||[]).forEach((function(t){var r=n[t];if(r)e.push(r);else{var a,s=i[t](),u=fetch(o.p+""+{10:"1b2a941e0e6bd21031c4"}[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 r=new e;r.get("/track/-?\\d+",()=>o("track",t)),r.get("/album/\\d+",()=>o("album",t)),r.get("/playlist/\\d+",()=>o("playlist",t));return await r.route(t)}(t.request))});const i={aac_96:"8",64:"10",128:"1",320:"3",ra1:"13",ra2:"14",ra3:"15",sbc_256:"12",misc:"0"};async function o(e,o){if(access_token=await KV.get("access_token"),null===access_token){const t=await fetch("https://connect.deezer.com/oauth/access_token.php?grant_type=client_credentials&client_id=447462&client_secret=a83bf7f38ad2f137e444727cfc3775cf&output=json"),e=await t.json();if(void 0!==e.error)return new Response("Couldn't get access token from Deezer",{status:500,headers:{"content-type":"text/plain"}});access_token=e.access_token,await KV.put("access_token",access_token,{expirationTtl:parseInt(e.expires)})}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,e){case"track":return await async function(e,o,a,s){const u=await fetch(`https://api.deezer.com/track/${e}?access_token=${a}`),f=await u.json();if(void 0!==f.error)return new Response(JSON.stringify(f.error),{status:403,headers:{"content-type":"application/json"}});const c=await r.e(1).then(r.bind(null,8));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)}(f,o,c.stream_url),"object"==typeof result)return result;const h=await fetch(result);if(200!==h.status)return new Response("Couldn't get track stream",{status:403,headers:{"content-type":"text/plain"}});let l;if(s){if(l=new n(t.alloc(0)),l.padding=0,l.setFrame("TIT2",f.title).setFrame("TALB",f.album.title).setFrame("TPE2",f.artist.name),void 0!==f.contributors){contr_list=[];for(const t of f.contributors)contr_list.push(t.name);l.setFrame("TPE1",contr_list)}if(void 0!==f.track_position&&l.setFrame("TRCK",f.track_position),void 0!==f.disk_number&&l.setFrame("TPOS",f.disk_number),""!==f.isrc&&l.setFrame("TSRC",f.isrc),void 0!==f.bpm&&l.setFrame("TBPM",f.bpm),void 0!==f.release_date){const t=f.release_date.split("-");l.setFrame("TYER",t[0]),l.setFrame("TDAT",t[2]+t[1])}if(""!==f.md5_image){const t=`https://cdns-images.dzcdn.net/images/cover/${f.md5_image}/1000x1000-000000-80-0-0.jpg`,e=await fetch(t),r=await e.arrayBuffer();l.setFrame("APIC",{type:3,data:r,description:"cover"})}l.addTag()}let{readable:p,writable:g}=new TransformStream;const d=g.getWriter();l&&d.write(l.arrayBuffer);f.alternative&&(e=f.alternative.id.toString());const y=new c.Cipher(e),w=parseInt(h.headers.get("Content-Length"));return 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||(e=n.decrypt_chunk(e)),await t.write(e),o+=2048}await i.cancel(),await t.close()}(d,h.body,w,y),new Response(p,{status:200,headers:{"content-type":"audio/mpeg"}})}(s,format,access_token,u);case"album":case"playlist":return await async function(t,e,r,n,i,o){const a=await fetch(`https://api.deezer.com/${t}/${e}?access_token=${n}&limit=-1`),s=await a.json();if(void 0!==s.error)return new Response(JSON.stringify(s.error),{status:403,headers:{"content-type":"application/json"}});let u="#EXTM3U\n";for(const t of s.tracks.data){t.id<0&&(r="misc");let e=`https://${o}/track/${t.id}?f=${r}&t=${+i}`;u+=`#EXTINF:${t.duration},${t.title}\n${e}\n`}return new Response(u,{status:200,headers:{"content-type":"audio/mpegurl"}})}(e,s,format,access_token,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 7e5e1c6..23c9d33 100644 --- a/index.js +++ b/index.js @@ -33,10 +33,6 @@ async function handler(type, request) { const url = new URL(request.url) const id = url.pathname.split('/')[2] - if (id === "") { - return new Response('ID needs to be specified', { status: 400, headers: { 'content-type': 'text/plain' } }) - } - format = url.searchParams.get('f') if (format === null) { format = '320' @@ -237,9 +233,9 @@ async function m3u8(type, id, format, access_token, tagging, host) { async function handleRequest(request) { const r = new Router() - r.get('/track/.*', () => handler('track', request)) - r.get('/album/.*', () => handler('album', request)) - r.get('/playlist/.*', () => handler('playlist', request)) + r.get('/track/-?\\d+', () => handler('track', request)) + r.get('/album/\\d+', () => handler('album', request)) + r.get('/playlist/\\d+', () => handler('playlist', request)) const resp = await r.route(request) return resp