added tagging
This commit is contained in:
parent
a07c256fe2
commit
6d1447e126
File diff suppressed because one or more lines are too long
46
index.js
46
index.js
|
@ -1,5 +1,6 @@
|
||||||
const Router = require('./router')
|
const Router = require('./router')
|
||||||
const aesjs = require('aes-js');
|
const aesjs = require('aes-js');
|
||||||
|
const ID3Writer = require('browser-id3-writer');
|
||||||
|
|
||||||
addEventListener('fetch', event => {
|
addEventListener('fetch', event => {
|
||||||
event.respondWith(handleRequest(event.request))
|
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) {
|
switch (type) {
|
||||||
case 'track':
|
case 'track':
|
||||||
return await track(id, format, access_token)
|
return await track(id, format, access_token, tagging)
|
||||||
case 'album':
|
case 'album':
|
||||||
case 'playlist':
|
case 'playlist':
|
||||||
return await m3u8(type, id, format, access_token)
|
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 response = await fetch(`https://api.deezer.com/track/${id}?access_token=${access_token}`)
|
||||||
const json = await response.json()
|
const json = await response.json()
|
||||||
if (json.error !== undefined) {
|
if (json.error !== undefined) {
|
||||||
|
@ -103,8 +106,45 @@ async function track(id, format, access_token) {
|
||||||
if (typeof result === 'object') {
|
if (typeof result === 'object') {
|
||||||
return result
|
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) {
|
async function track_url(json, format) {
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"aes-js": "^3.1.2",
|
"aes-js": "^3.1.2",
|
||||||
|
"browser-id3-writer": "^4.4.0",
|
||||||
"serverless-cloudflare-workers": "^1.2.0"
|
"serverless-cloudflare-workers": "^1.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue