diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js index 3e38c4db..72d5b9a0 100644 --- a/src/modules/processing/match.js +++ b/src/modules/processing/match.js @@ -25,7 +25,7 @@ import twitch from "./services/twitch.js"; import rutube from "./services/rutube.js"; import dailymotion from "./services/dailymotion.js"; import loom from "./services/loom.js"; - +import newgrounds from "./services/newgrounds.js"; let freebind; export default async function(host, patternMatch, lang, obj) { @@ -193,12 +193,21 @@ export default async function(host, patternMatch, lang, obj) { id: patternMatch.id }); break; + case "newgrounds": + r = await newgrounds({ + type: patternMatch.type, + method: patternMatch.method, + id: patternMatch.id, + }); + break; default: return createResponse("error", { t: loc(lang, 'ErrorUnsupported') }); } + console.log(r) + if (r.isAudioOnly) isAudioOnly = true; let isAudioMuted = isAudioOnly ? false : obj.isAudioMuted; diff --git a/src/modules/processing/matchActionDecider.js b/src/modules/processing/matchActionDecider.js index 74f0f8c7..10167326 100644 --- a/src/modules/processing/matchActionDecider.js +++ b/src/modules/processing/matchActionDecider.js @@ -100,6 +100,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di case "video": switch (host) { case "bilibili": + case "newgrounds": params = { type: "render" }; break; case "youtube": @@ -136,7 +137,6 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di case "pinterest": case "streamable": case "loom": - responseType = "redirect"; break; } break; diff --git a/src/modules/processing/services/newgrounds.js b/src/modules/processing/services/newgrounds.js new file mode 100644 index 00000000..fbbc98c4 --- /dev/null +++ b/src/modules/processing/services/newgrounds.js @@ -0,0 +1,52 @@ +import { genericUserAgent } from "../../config.js"; +import { cleanString } from "../../sub/utils.js"; + +export default async function(obj) { + let req; + // handle video downloads + if (obj.type == 'portal') { + req = await fetch(`https://www.newgrounds.com/${obj.type}/video/${obj.id}`, { + headers: { + 'User-Agent': genericUserAgent, + 'X-Requested-With': 'XMLHttpRequest', + Accept: 'application/json, text/javascript, */*; q=0.01' + } + }) + .then(request => request.text()) + .catch(() => {}); + + if (!req) return { error: 'ErrorEmptyDownload' }; + } + + // handle audio downloads + if (obj.type == 'audio') { + req = await fetch(`https://www.newgrounds.com/audio/listen/${obj.id}`, { + headers: { + 'User-Agent': genericUserAgent, + } + }) + .then(request => request.text()) + .catch(() => {}); + + if (!req) return { error: 'ErrorEmptyDownload' }; + + const title = req.match(/"name"\s*:\s*"([^"]+)"/)?.[1]; + const artist = req.match(/"artist"\s*:\s*"([^"]+)"/)?.[1]; + let fileMetadata = { + title: cleanString(decodeURIComponent(title.trim())), + artist: cleanString(decodeURIComponent(artist.trim())), + } + + const url = req.match(/"filename"\s*:\s*"([^"]+)"/)?.[1]?.replace(/\\\//g, '/'); + + return { + urls: url, + filenameAttributes: { + service: "newgrounds", + id: obj.id, + title: fileMetadata.title, + author: fileMetadata.artist + }, + } + } + } \ No newline at end of file diff --git a/src/modules/processing/servicesConfig.json b/src/modules/processing/servicesConfig.json index d727b9a5..310625e8 100644 --- a/src/modules/processing/servicesConfig.json +++ b/src/modules/processing/servicesConfig.json @@ -118,6 +118,11 @@ "alias": "loom videos", "patterns": ["share/:id"], "enabled": true + }, + "newgrounds": { + "alias": "newgrounds.com", + "patterns": [":type/:method/:id"], + "enabled": true } } } diff --git a/src/modules/processing/servicesPatternTesters.js b/src/modules/processing/servicesPatternTesters.js index ddeea31f..3a5d698e 100644 --- a/src/modules/processing/servicesPatternTesters.js +++ b/src/modules/processing/servicesPatternTesters.js @@ -58,4 +58,7 @@ export const testers = { "youtube": (patternMatch) => patternMatch.id?.length <= 11, + + "newgrounds": (patternMatch) => + patternMatch.id?.length <= 6, }