added album and playlist support

This commit is contained in:
uh wot 2021-01-26 12:55:28 +01:00
parent 8b50a3682b
commit b9212f3b98
Signed by: uhwot
GPG Key ID: CB2454984587B781
2 changed files with 61 additions and 22 deletions

4
dist/worker.js vendored

File diff suppressed because one or more lines are too long

View File

@ -49,14 +49,7 @@ const format_string_to_num = {
'misc': '0',
}
async function track(request) {
const url = new URL(request.url)
const id = url.pathname.split('/')[2]
if (id === "") {
return new Response("Track ID needs to be specified", {status: 400, headers: {'content-type': "text/plain"}})
}
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")
@ -69,14 +62,12 @@ async function track(request) {
await KV.put("access_token", access_token, {expirationTtl: Number(json.expires)})
}
const response = await fetch(`https://api.deezer.com/track/${id}?access_token=${access_token}`)
const json = await response.json()
if (json.error !== undefined) {
return new Response(JSON.stringify(json.error), {status: 403, headers: {'content-type': "application/json"}})
}
const url = new URL(request.url)
const id = url.pathname.split('/')[2]
const md5_origin = json.md5_origin
const media_version = json.media_version
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) {
@ -96,21 +87,69 @@ async function track(request) {
}
}
switch (type) {
case 'track':
return await track(id, format, access_token)
case 'album':
case 'playlist':
return await m3u(type, id, format, access_token)
}
}
async function track(id, format, access_token) {
const response = await fetch(`https://api.deezer.com/track/${id}?access_token=${access_token}`)
const json = await response.json()
if (json.error !== undefined) {
return new Response(JSON.stringify(json.error), {status: 403, headers: {'content-type': "application/json"}})
}
result = await track_url(json, format)
if (typeof result === 'object') {
return result
}
return new Response(null, {status: 302, headers: {'location': result}})
}
async function track_url(json, format) {
const id = json.id
const md5_origin = json.md5_origin
const media_version = json.media_version
if (json['filesize_' + format] === '0') {
return new Response("Format unavailable", {status: 403, headers: {'content-type': "text/plain"}})
}
format = format_string_to_num[format]
const cdn_url = await url_gen(md5_origin, format, id, media_version)
return await url_gen(md5_origin, format, id, media_version)
}
return new Response(null, {status: 302, headers: {'location': cdn_url}})
async function m3u(type, id, format, access_token) {
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"}})
}
var list = "#EXTM3U\n"
for (const track of json.tracks.data) {
const result = await track_url(track, format)
if (typeof result === 'object') {
return result
}
list += `#EXTINF:${track.duration},${track.title}\n${result}\n`
}
return new Response(list, {status: 200, headers: {'content-type': 'audio/mpegurl'}})
}
async function handleRequest(request) {
const r = new Router()
// Replace with the appropriate paths and handlers
r.get('/track/.*', () => track(request))
r.get('/track/.*', () => handler('track', request))
r.get('/album/.*', () => handler('album', request))
r.get('/playlist/.*', () => handler('playlist', request))
const resp = await r.route(request)
return resp