From fdd713786570319a09f3a58908af256a7de86e62 Mon Sep 17 00:00:00 2001 From: Elijah <106784368+be195@users.noreply.github.com> Date: Thu, 13 Apr 2023 23:38:49 +0300 Subject: [PATCH] soundcloud: download original audio file whenever possible --- src/modules/processing/services/soundcloud.js | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/modules/processing/services/soundcloud.js b/src/modules/processing/services/soundcloud.js index aef27bd5..2a4aaf91 100644 --- a/src/modules/processing/services/soundcloud.js +++ b/src/modules/processing/services/soundcloud.js @@ -54,6 +54,24 @@ export default async function(obj) { let clientId = await findClientID(); if (!clientId) return { error: 'ErrorSoundCloudNoClientId' }; + const audioFilename = `soundcloud_${json.id}`; + const fileMetadata = { + title: json.title, + artist: json.user.username, + }; + + if (json.downloadable) { + const downloadUrl = 'https://api-v2.soundcloud.com/tracks/' + json.id + '/download?client_id=' + clientId; + const redirectUri = await fetch(downloadUrl).then(async (r) => { return (await r.json()).redirectUri }) + .catch(() => { return false }); + if (redirectUri) + return { + urls: redirectUri, + audioFilename, + fileMetadata + }; + } + let fileUrlBase = json.media.transcodings[0]["url"].replace("/hls", "/progressive"), fileUrl = `${fileUrlBase}${fileUrlBase.includes("?") ? "&" : "?"}client_id=${clientId}&track_authorization=${json.track_authorization}`; if (fileUrl.substring(0, 54) !== "https://api-v2.soundcloud.com/media/soundcloud:tracks:") return { error: 'ErrorEmptyDownload' }; @@ -65,10 +83,7 @@ export default async function(obj) { return { urls: file, - audioFilename: `soundcloud_${json.id}`, - fileMetadata: { - title: json.title, - artist: json.user.username, - } + audioFilename, + fileMetadata } }