added tagging

This commit is contained in:
uh wot 2021-05-25 21:00:16 +02:00
parent a07c256fe2
commit 6d1447e126
Signed by: uhwot
GPG Key ID: CB2454984587B781
3 changed files with 46 additions and 5 deletions

4
dist/worker.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
const Router = require('./router')
const aesjs = require('aes-js');
const ID3Writer = require('browser-id3-writer');
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
@ -83,16 +84,18 @@ async function handler(type, request) {
}
}
tagging = (url.searchParams.get('t') === 'true') && ((format === '128') || (format === '320'))
switch (type) {
case 'track':
return await track(id, format, access_token)
return await track(id, format, access_token, tagging)
case 'album':
case 'playlist':
return await m3u8(type, id, format, access_token)
}
}
async function track(id, format, access_token) {
async function track(id, format, access_token, tagging) {
const response = await fetch(`https://api.deezer.com/track/${id}?access_token=${access_token}`)
const json = await response.json()
if (json.error !== undefined) {
@ -103,8 +106,45 @@ async function track(id, format, access_token) {
if (typeof result === 'object') {
return result
}
return new Response(null, {status: 302, headers: {'location': result}})
if (!tagging) {
return new Response(null, {status: 302, headers: {'location': result}})
} else {
const track = await fetch(result)
const trackBuffer = await track.arrayBuffer()
const writer = new ID3Writer(trackBuffer);
writer.setFrame('TIT2', json.title)
.setFrame('TALB', json.album.title)
if (json.contributors !== undefined) {
contr_list = [];
for (const c of json.contributors) {
contr_list.push(c.name)
}
writer.setFrame('TPE1', contr_list)
}
if (json.release_date !== undefined) {
writer.setFrame('TYER', json.release_date.split('-')[0])
}
if (json.album.cover_xl !== undefined) {
const cover = await fetch(json.album.cover_xl)
const coverBuffer = await cover.arrayBuffer()
writer.setFrame('APIC', {
type: 3,
data: coverBuffer,
description: 'cover'
});
}
writer.addTag();
return new Response(writer.arrayBuffer, {status: 200, headers: {'content-type': 'audio/mpeg'}})
}
}
async function track_url(json, format) {

View File

@ -14,6 +14,7 @@
},
"dependencies": {
"aes-js": "^3.1.2",
"browser-id3-writer": "^4.4.0",
"serverless-cloudflare-workers": "^1.2.0"
}
}