From 749c00a691b923d2b91b411038acf023522e1784 Mon Sep 17 00:00:00 2001 From: wukko Date: Wed, 23 Aug 2023 00:49:02 +0600 Subject: [PATCH] soundcloud: got rid of html parsing --- src/modules/processing/hostOverrides.js | 7 --- src/modules/processing/services/soundcloud.js | 44 +++++++++---------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/modules/processing/hostOverrides.js b/src/modules/processing/hostOverrides.js index 7fc7b67..211cd2a 100644 --- a/src/modules/processing/hostOverrides.js +++ b/src/modules/processing/hostOverrides.js @@ -9,12 +9,6 @@ export default function (inHost, inURL) { url = `https://youtube.com/watch?v=${url.replace("https://youtu.be/", "")}`; } break; - case "goo": - if (url.startsWith("https://soundcloud.app.goo.gl/")) { - host = "soundcloud"; - url = `https://soundcloud.com/${url.replace("https://soundcloud.app.goo.gl/", "").split('/')[0]}` - } - break; case "vxtwitter": case "x": if (url.startsWith("https://x.com/")) { @@ -33,7 +27,6 @@ export default function (inHost, inURL) { } break; } - return { host: host, url: url diff --git a/src/modules/processing/services/soundcloud.js b/src/modules/processing/services/soundcloud.js index 2edaad2..6f4d82d 100644 --- a/src/modules/processing/services/soundcloud.js +++ b/src/modules/processing/services/soundcloud.js @@ -35,31 +35,31 @@ async function findClientID() { } export default async function(obj) { - let html; - if (!obj.author && !obj.song && obj.shortLink) { - html = await fetch(`https://on.soundcloud.com/${obj.shortLink}/`).then((r) => { - return r.status === 404 ? false : r.text() - }).catch(() => { return false }); - } - if (obj.author && obj.song) { - html = await fetch( - `https://soundcloud.com/${obj.author}/${obj.song}${obj.accessKey ? `/s-${obj.accessKey}` : ''}` - ).then((r) => { - return r.text() - }).catch(() => { return false }); - } - if (!html) return { error: 'ErrorCouldntFetch' }; - if (!(html.includes('')[0]); - if (!json["media"]["transcodings"]) return { error: 'ErrorEmptyDownload' }; - let clientId = await findClientID(); if (!clientId) return { error: 'ErrorSoundCloudNoClientId' }; - let fileUrlBase = json.media.transcodings.filter((v) => { if (v["format"]["mime_type"].startsWith("audio/ogg")) return true })[0]["url"], + let link; + if (obj.shortLink && !obj.author && !obj.song) { + link = await fetch(`https://on.soundcloud.com/${obj.shortLink}/`, { redirect: "manual" }).then((r) => { + if (r.status === 302 && r.headers.get("location").startsWith("https://soundcloud.com/")) { + return r.headers.get("location").split('?', 1)[0] + } + return false + }).catch(() => { return false }); + } + if (!link && obj.author && obj.song) { + link = `https://soundcloud.com/${obj.author}/${obj.song}${obj.accessKey ? `/s-${obj.accessKey}` : ''}` + } + if (!link) return { error: 'ErrorCouldntFetch' }; + + let json = await fetch(`https://api-v2.soundcloud.com/resolve?url=${link}&client_id=${clientId}`).then((r) => { + return r.status === 200 ? r.json() : false + }).catch(() => { return false }); + if (!json) return { error: 'ErrorCouldntFetch' }; + + if (!json["media"]["transcodings"]) return { error: 'ErrorEmptyDownload' }; + + let fileUrlBase = json.media.transcodings.filter((v) => { if (v["preset"] === "opus_0_0") return true })[0]["url"], 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' };