readded user-uploaded track support
This commit is contained in:
parent
44e40ba9a3
commit
1a802e7654
|
@ -6,7 +6,7 @@
|
|||
{
|
||||
"label": "build",
|
||||
"type": "shell",
|
||||
"command": "wrangler build",
|
||||
"command": "NODE_OPTIONS=--openssl-legacy-provider wrangler build",
|
||||
"problemMatcher": [],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
|
@ -16,7 +16,7 @@
|
|||
{
|
||||
"label": "dev",
|
||||
"type": "shell",
|
||||
"command": "wrangler dev",
|
||||
"command": "NODE_OPTIONS=--openssl-legacy-provider wrangler dev",
|
||||
"problemMatcher": [],
|
||||
"group": {
|
||||
"kind": "test",
|
||||
|
@ -26,7 +26,7 @@
|
|||
{
|
||||
"label": "publish",
|
||||
"type": "shell",
|
||||
"command": "wrangler publish"
|
||||
"command": "NODE_OPTIONS=--openssl-legacy-provider wrangler publish"
|
||||
}
|
||||
]
|
||||
}
|
File diff suppressed because one or more lines are too long
88
index.js
88
index.js
|
@ -5,6 +5,9 @@ addEventListener('fetch', event => {
|
|||
event.respondWith(handleRequest(event.request))
|
||||
})
|
||||
|
||||
const client_id = "447462"
|
||||
const client_secret = "a83bf7f38ad2f137e444727cfc3775cf"
|
||||
|
||||
const formats = {
|
||||
aac_96: { num: '8', gw: 'AAC_96', mime: 'audio/aac' },
|
||||
64: { num: '10', gw: 'MP3_64', mime: 'audio/mpeg' },
|
||||
|
@ -63,28 +66,42 @@ async function gw_api_call(method, params) {
|
|||
}
|
||||
|
||||
async function handler(type, request) {
|
||||
license_token = await KV.get('license_token')
|
||||
checkForm = await KV.get('checkForm')
|
||||
sid = await KV.get('sid')
|
||||
|
||||
if (license_token === null) {
|
||||
const user_data = await gw_api_call('deezer.getUserData')
|
||||
if (user_data.constructor.name === 'Response') {
|
||||
return user_data
|
||||
}
|
||||
|
||||
if (user_data.USER.USER_ID === 0) {
|
||||
return new Response('Invalid arl', { status: 500, headers: { 'content-type': 'text/plain' } })
|
||||
}
|
||||
|
||||
license_token = user_data.USER.OPTIONS.license_token
|
||||
|
||||
await KV.put('license_token', license_token, { expirationTtl: 3600 })
|
||||
}
|
||||
|
||||
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')
|
||||
checkForm = await KV.get('checkForm')
|
||||
sid = await KV.get('sid')
|
||||
|
||||
if (license_token === null) {
|
||||
const user_data = await gw_api_call('deezer.getUserData')
|
||||
if (user_data.constructor.name === 'Response') {
|
||||
return user_data
|
||||
}
|
||||
|
||||
if (user_data.USER.USER_ID === 0) {
|
||||
return new Response('Invalid arl', { status: 500, headers: { 'content-type': 'text/plain' } })
|
||||
}
|
||||
|
||||
license_token = user_data.USER.OPTIONS.license_token
|
||||
|
||||
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' } })
|
||||
}
|
||||
|
||||
access_token = json.access_token
|
||||
await KV.put('access_token', access_token, { expirationTtl: parseInt(json.expires) })
|
||||
}
|
||||
}
|
||||
|
||||
format = url.searchParams.get('f')
|
||||
if (format === null) {
|
||||
format = '320'
|
||||
|
@ -115,13 +132,34 @@ async function handler(type, request) {
|
|||
}
|
||||
|
||||
async function track(id, format, tagging) {
|
||||
const json = await gw_api_call('song.getData', { 'SNG_ID': id })
|
||||
if (json.constructor.name === 'Response') {
|
||||
return json
|
||||
}
|
||||
// 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') {
|
||||
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
|
||||
format = 'misc'
|
||||
json = {
|
||||
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) {
|
||||
|
|
|
@ -7,4 +7,4 @@ kv_namespaces = [
|
|||
{ binding = "KV", id = "974c0967a84e415daa054bbbcc7f80c6", preview_id = "cfcc6491f3484cbca664913836635113" }
|
||||
]
|
||||
vars = { ARL = "amogus" }
|
||||
compatibility_date = "2021-10-14"
|
||||
compatibility_date = "2021-11-09"
|
Loading…
Reference in New Issue