From 760980032a4aa258083cef03afd7a92a1b1aa1ce Mon Sep 17 00:00:00 2001 From: Steve Berdy <86739818+steveberdy@users.noreply.github.com> Date: Thu, 18 Jan 2024 10:13:03 -0500 Subject: [PATCH] support for bandcamp --- README.md | 1 + package.json | 1 + src/modules/processing/match.js | 7 +++++ src/modules/processing/services/bandcamp.js | 29 +++++++++++++++++++ src/modules/processing/servicesConfig.json | 6 ++++ .../processing/servicesPatternTesters.js | 9 ++++-- src/test/tests.json | 9 ++++++ 7 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/modules/processing/services/bandcamp.js diff --git a/README.md b/README.md index 16998478..8658f77e 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ this list is not final and keeps expanding over time. if support for a service y | service | video + audio | only audio | only video | metadata | rich file names | | :-------- | :-----------: | :--------: | :--------: | :------: | :-------------: | +| bandcamp | ➖ | ✅ | ➖ | ✅ | ✅ | | bilibili.com | ✅ | ✅ | ✅ | ➖ | ➖ | | instagram posts & stories | ✅ | ✅ | ✅ | ➖ | ➖ | | instagram reels | ✅ | ✅ | ✅ | ➖ | ➖ | diff --git a/package.json b/package.json index c5bb65d3..0b3fc694 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "express-rate-limit": "^6.3.0", "ffmpeg-static": "^5.1.0", "hls-parser": "^0.10.7", + "libxmljs": "^1.0.11", "nanoid": "^4.0.2", "node-cache": "^5.1.2", "psl": "1.9.0", diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js index 0d5980ca..23fd8263 100644 --- a/src/modules/processing/match.js +++ b/src/modules/processing/match.js @@ -24,6 +24,7 @@ import pinterest from "./services/pinterest.js"; import streamable from "./services/streamable.js"; import twitch from "./services/twitch.js"; import rutube from "./services/rutube.js"; +import bandcamp from "./services/bandcamp.js"; export default async function(host, patternMatch, url, lang, obj) { assert(url instanceof URL); @@ -158,6 +159,12 @@ export default async function(host, patternMatch, url, lang, obj) { isAudioOnly: isAudioOnly }); break; + case "bandcamp": + r = await bandcamp({ + url, + name: patternMatch.name + }); + break; default: return apiJSON(0, { t: errorUnsupported(lang) }); } diff --git a/src/modules/processing/services/bandcamp.js b/src/modules/processing/services/bandcamp.js new file mode 100644 index 00000000..8a9597c6 --- /dev/null +++ b/src/modules/processing/services/bandcamp.js @@ -0,0 +1,29 @@ +import libxmljs from "libxmljs"; + +export default async function(obj) { + const htmlContent = await fetch(obj.url.href).then((r) => { + return r.status === 200 ? r.text() : false + }).catch(() => { return false }); + const htmlDoc = await libxmljs.parseHtmlAsync(htmlContent) + .then((d) => { return d }); + const rawData = htmlDoc.find("//script[@data-tralbum]")[0]; + const json = JSON.parse(rawData.getAttribute("data-tralbum").value()); + + let fileMetadata = { + title: json.current.title, + artist: json.artist + } + + return { + urls: json.trackinfo[0].file["mp3-128"], + filenameAttributes: { + service: "bandcamp", + id: json.trackinfo[0].id, + title: fileMetadata.title, + artist: fileMetadata.artist, + extension: "mp3" + }, + isMp3: true, + fileMetadata + } +} diff --git a/src/modules/processing/servicesConfig.json b/src/modules/processing/servicesConfig.json index 9d903fcb..cb5a8e32 100644 --- a/src/modules/processing/servicesConfig.json +++ b/src/modules/processing/servicesConfig.json @@ -106,6 +106,12 @@ "tld": "ru", "patterns": ["video/:id", "play/embed/:id"], "enabled": true + }, + "bandcamp": { + "alias": "bandcamp music", + "patterns": ["track/:name"], + "subdomains": "*", + "enabled": true } } } diff --git a/src/modules/processing/servicesPatternTesters.js b/src/modules/processing/servicesPatternTesters.js index 970e8f40..46583079 100644 --- a/src/modules/processing/servicesPatternTesters.js +++ b/src/modules/processing/servicesPatternTesters.js @@ -1,11 +1,14 @@ export const testers = { + "bandcamp": (patternMatch) => + patternMatch.name?.length >= 0, + "bilibili": (patternMatch) => patternMatch.id?.length <= 12, "instagram": (patternMatch) => patternMatch.postId?.length <= 12 || (patternMatch.username?.length <= 30 && patternMatch.storyId?.length <= 24), - + "ok": (patternMatch) => patternMatch.id?.length <= 16, @@ -19,12 +22,12 @@ export const testers = { patternMatch.id?.length === 32, "soundcloud": (patternMatch) => - (patternMatch.author?.length <= 255 && patternMatch.song?.length <= 255) + (patternMatch.author?.length <= 255 && patternMatch.song?.length <= 255) || patternMatch.shortLink?.length <= 32, "streamable": (patternMatch) => patternMatch.id?.length === 6, - + "tiktok": (patternMatch) => patternMatch.postId?.length <= 21 || patternMatch.id?.length <= 13, diff --git a/src/test/tests.json b/src/test/tests.json index d3170858..0e06945e 100644 --- a/src/test/tests.json +++ b/src/test/tests.json @@ -1171,5 +1171,14 @@ "code": 200, "status": "stream" } + }], + "bandcamp": [{ + "name": "regular track", + "url": "https://home96.bandcamp.com/track/5", + "params": {}, + "expected": { + "code": 200, + "status": "stream" + } }] }