320/flac now epically bacc?!?!?!?1?1
This commit is contained in:
parent
90f3d6bf4e
commit
a92876194a
5 changed files with 61 additions and 42 deletions
78
index.js
78
index.js
|
|
@ -10,6 +10,7 @@ const format_string_to_num = {
|
|||
'64': '10',
|
||||
'128': '1',
|
||||
'320': '3',
|
||||
'flac': '9',
|
||||
'mp4_ra1': '13',
|
||||
'mp4_ra2': '14',
|
||||
'mp4_ra3': '15',
|
||||
|
|
@ -21,18 +22,6 @@ const format_string_to_num = {
|
|||
}
|
||||
|
||||
async function handler(type, request) {
|
||||
access_token = await KV.get('access_token')
|
||||
if (access_token === null) {
|
||||
const response = await fetch('https://connect.deezer.com/oauth/access_token.php?grant_type=client_credentials&client_id=447462&client_secret=a83bf7f38ad2f137e444727cfc3775cf&output=json')
|
||||
const json = await response.json()
|
||||
if (json.error !== undefined) {
|
||||
return new Response("Couldn't get access token from Deezer", { status: 500, headers: { 'content-type': 'text/plain' } })
|
||||
}
|
||||
|
||||
access_token = json.access_token
|
||||
await KV.put('access_token', access_token, { expirationTtl: parseInt(json.expires) })
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
const id = url.pathname.split('/')[2]
|
||||
|
||||
|
|
@ -51,19 +40,19 @@ async function handler(type, request) {
|
|||
}
|
||||
|
||||
let tagging = url.searchParams.get('t')
|
||||
tagging = tagging === 'true' || tagging === '1'
|
||||
tagging = (tagging === 'true' || tagging === '1') && ['misc', '128', '320'].includes(format)
|
||||
|
||||
switch (type) {
|
||||
case 'track':
|
||||
return await track(id, format, access_token, tagging)
|
||||
return await track(id, format, tagging)
|
||||
case 'album':
|
||||
case 'playlist':
|
||||
return await m3u8(type, id, format, access_token, tagging, url.host)
|
||||
return await m3u8(type, id, format, tagging, url.host)
|
||||
}
|
||||
}
|
||||
|
||||
async function track(id, format, access_token, tagging) {
|
||||
const response = await fetch(`https://api.deezer.com/track/${id}?access_token=${access_token}`)
|
||||
async function track(id, format, tagging) {
|
||||
const response = await fetch(`https://api.deezer.com/streaming_url.php?access_token=${ACCESS_TOKEN}&track_id=${id}`)
|
||||
const json = await response.json()
|
||||
if (json.error !== undefined) {
|
||||
return new Response(JSON.stringify(json.error), { status: 403, headers: { 'content-type': "application/json" } })
|
||||
|
|
@ -71,14 +60,29 @@ async function track(id, format, access_token, tagging) {
|
|||
|
||||
const wasm = await import('./pkg')
|
||||
|
||||
result = await track_url(json, format, wasm.stream_url)
|
||||
if (typeof result === 'object') {
|
||||
return result
|
||||
encrypted = !['320', 'flac'].includes(format)
|
||||
if (!encrypted) { // server-side stream url
|
||||
// TODO: handle alternatives
|
||||
result = json['url_' + format]
|
||||
|
||||
if (result === undefined) {
|
||||
return new Response('Format unavailable', { status: 403, headers: { 'content-type': 'text/plain' } })
|
||||
}
|
||||
|
||||
result = wasm.decrypt_stream_url(result)
|
||||
} else { // legacy stream url
|
||||
result = await legacy_track_url(json, format, wasm.legacy_stream_url)
|
||||
if (typeof result === 'object') {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
const track = await fetch(result)
|
||||
if (track.status !== 200) {
|
||||
return new Response("Couldn't get track stream", { status: 403, headers: { 'content-type': 'text/plain' } })
|
||||
let track
|
||||
if (tagging || encrypted) {
|
||||
track = await fetch(result)
|
||||
if (track.status !== 200) {
|
||||
return new Response("Couldn't get track stream", { status: 403, headers: { 'content-type': 'text/plain' } })
|
||||
}
|
||||
}
|
||||
|
||||
let id3
|
||||
|
|
@ -136,9 +140,12 @@ async function track(id, format, access_token, tagging) {
|
|||
}
|
||||
|
||||
let { readable, writable } = new TransformStream()
|
||||
const writer = writable.getWriter()
|
||||
let writer
|
||||
if (tagging || encrypted) {
|
||||
writer = writable.getWriter()
|
||||
}
|
||||
|
||||
if (id3) {
|
||||
if (tagging) {
|
||||
writer.write(id3.arrayBuffer)
|
||||
}
|
||||
|
||||
|
|
@ -146,12 +153,17 @@ async function track(id, format, access_token, tagging) {
|
|||
if (json.alternative) {
|
||||
id = json.alternative.id.toString()
|
||||
}
|
||||
|
||||
const cipher = new wasm.Cipher(id)
|
||||
|
||||
const length = parseInt(track.headers.get('Content-Length'))
|
||||
|
||||
pipeDecryptedStream(writer, track.body, length, cipher)
|
||||
if (encrypted) {
|
||||
const cipher = new wasm.Cipher(id)
|
||||
const length = parseInt(track.headers.get('Content-Length'))
|
||||
pipeDecryptedStream(writer, track.body, length, cipher)
|
||||
} else if (tagging) {
|
||||
writer.releaseLock()
|
||||
track.body.pipeTo(writable)
|
||||
} else {
|
||||
return new Response(null, { status: 302, headers: { 'location': result } })
|
||||
}
|
||||
|
||||
return new Response(readable, { status: 200, headers: { 'content-type': 'audio/mpeg' } })
|
||||
}
|
||||
|
|
@ -191,7 +203,7 @@ async function pipeDecryptedStream(writer, body, length, cipher) {
|
|||
await writer.close()
|
||||
}
|
||||
|
||||
function track_url(json, format, url_func) {
|
||||
function legacy_track_url(json, format, url_func) {
|
||||
// needed if track has fallback, like https://www.deezer.com/track/11835714
|
||||
if (json.alternative) {
|
||||
json = json.alternative
|
||||
|
|
@ -214,8 +226,8 @@ function track_url(json, format, url_func) {
|
|||
return url_func(md5_origin, format, id.toString(), media_version)
|
||||
}
|
||||
|
||||
async function m3u8(type, id, format, access_token, tagging, host) {
|
||||
const response = await fetch(`https://api.deezer.com/${type}/${id}?access_token=${access_token}&limit=-1`)
|
||||
async function m3u8(type, id, format, tagging, host) {
|
||||
const response = await fetch(`https://api.deezer.com/${type}/${id}?access_token=${ACCESS_TOKEN}&limit=-1`)
|
||||
const json = await response.json()
|
||||
if (json.error !== undefined) {
|
||||
return new Response(JSON.stringify(json.error), { status: 403, headers: { 'content-type': "application/json" } })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue