readded user-uploaded track support

This commit is contained in:
uh wot 2021-11-10 00:15:28 +01:00
parent 44e40ba9a3
commit 1a802e7654
Signed by: uhwot
GPG Key ID: CB2454984587B781
4 changed files with 70 additions and 32 deletions

6
.vscode/tasks.json vendored
View File

@ -6,7 +6,7 @@
{ {
"label": "build", "label": "build",
"type": "shell", "type": "shell",
"command": "wrangler build", "command": "NODE_OPTIONS=--openssl-legacy-provider wrangler build",
"problemMatcher": [], "problemMatcher": [],
"group": { "group": {
"kind": "build", "kind": "build",
@ -16,7 +16,7 @@
{ {
"label": "dev", "label": "dev",
"type": "shell", "type": "shell",
"command": "wrangler dev", "command": "NODE_OPTIONS=--openssl-legacy-provider wrangler dev",
"problemMatcher": [], "problemMatcher": [],
"group": { "group": {
"kind": "test", "kind": "test",
@ -26,7 +26,7 @@
{ {
"label": "publish", "label": "publish",
"type": "shell", "type": "shell",
"command": "wrangler publish" "command": "NODE_OPTIONS=--openssl-legacy-provider wrangler publish"
} }
] ]
} }

6
dist/worker.js vendored

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,9 @@ addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request)) event.respondWith(handleRequest(event.request))
}) })
const client_id = "447462"
const client_secret = "a83bf7f38ad2f137e444727cfc3775cf"
const formats = { const formats = {
aac_96: { num: '8', gw: 'AAC_96', mime: 'audio/aac' }, aac_96: { num: '8', gw: 'AAC_96', mime: 'audio/aac' },
64: { num: '10', gw: 'MP3_64', mime: 'audio/mpeg' }, 64: { num: '10', gw: 'MP3_64', mime: 'audio/mpeg' },
@ -63,6 +66,10 @@ async function gw_api_call(method, params) {
} }
async function handler(type, request) { async function handler(type, request) {
const url = new URL(request.url)
const id = url.pathname.split('/')[2]
if (id[0] !== '-') { // checks if not user-upped track
license_token = await KV.get('license_token') license_token = await KV.get('license_token')
checkForm = await KV.get('checkForm') checkForm = await KV.get('checkForm')
sid = await KV.get('sid') sid = await KV.get('sid')
@ -81,9 +88,19 @@ async function handler(type, request) {
await KV.put('license_token', license_token, { expirationTtl: 3600 }) await KV.put('license_token', license_token, { expirationTtl: 3600 })
} }
} else {
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=${client_id}&client_secret=${client_secret}&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' } })
}
const url = new URL(request.url) access_token = json.access_token
const id = url.pathname.split('/')[2] await KV.put('access_token', access_token, { expirationTtl: parseInt(json.expires) })
}
}
format = url.searchParams.get('f') format = url.searchParams.get('f')
if (format === null) { if (format === null) {
@ -115,13 +132,34 @@ async function handler(type, request) {
} }
async function track(id, format, tagging) { async function track(id, format, tagging) {
const json = await gw_api_call('song.getData', { 'SNG_ID': id }) // other users' user-upped tracks cannot be downloaded with the gw-light API
let json;
if (id[0] !== '-') {
json = await gw_api_call('song.getData', { 'SNG_ID': id })
if (json.constructor.name === 'Response') { if (json.constructor.name === 'Response') {
return json return json
} }
} else { // user-upped track
const response = await fetch(`https://api.deezer.com/track/${id}?access_token=${access_token}`)
json = await response.json()
if (json.error !== undefined) {
return new Response(JSON.stringify(json.error), { status: 403, headers: { 'content-type': "application/json" } })
}
if (parseInt(json.SNG_ID) < 0) { // user-uploaded track json = {
format = 'misc' SNG_ID: json.id,
FILESIZE_MISC: json.filesize_misc,
SNG_TITLE: json.title,
ALB_TITLE: json.album.title,
ART_NAME: json.artist.name,
ARTISTS: [{ART_NAME: json.artist.name}],
ISRC: '',
ALB_PICTURE: json.md5_image,
MD5_ORIGIN: json.md5_origin,
MEDIA_VERSION: json.media_version,
}
format = 'misc' // user-upped tracks always use 'misc' as format
} }
if (json['FILESIZE_' + formats[format].gw] == false) { if (json['FILESIZE_' + formats[format].gw] == false) {

View File

@ -7,4 +7,4 @@ kv_namespaces = [
{ binding = "KV", id = "974c0967a84e415daa054bbbcc7f80c6", preview_id = "cfcc6491f3484cbca664913836635113" } { binding = "KV", id = "974c0967a84e415daa054bbbcc7f80c6", preview_id = "cfcc6491f3484cbca664913836635113" }
] ]
vars = { ARL = "amogus" } vars = { ARL = "amogus" }
compatibility_date = "2021-10-14" compatibility_date = "2021-11-09"