Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: newgrounds support #639

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/modules/processing/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -193,6 +193,13 @@ 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')
Expand Down
86 changes: 86 additions & 0 deletions src/modules/processing/services/newgrounds.js
hyperdefined marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { genericUserAgent } from "../../config.js";
import { cleanString } from "../../sub/utils.js";

export default async function(obj) {
// handle video downloads
if (obj.type == 'portal') {
let req = await fetch(`https://www.newgrounds.com/${obj.type}/video/${obj.id}`, {
hyperdefined marked this conversation as resolved.
Show resolved Hide resolved
headers: {
'User-Agent': genericUserAgent,
'X-Requested-With': 'XMLHttpRequest',
}
})
.then(request => request.text())
.catch(() => {});

if (!req) return { error: 'ErrorCouldntFetch' };

let json;
try {
json = JSON.parse(req);
} catch { return { error: 'ErrorEmptyDownload' }; }
const highestQuality = Object.keys(json.sources)[0];
const video = json.sources[highestQuality][0].src;
if (!json.sources[highestQuality][0].type.includes('mp4')) {
return { error: 'ErrorCouldntFetch' };
}

let fileMetadata = {
title: cleanString(decodeURIComponent(json.title)),
artist: cleanString(decodeURIComponent(json.author)),
}

return {
urls: video,
filenameAttributes: {
service: "newgrounds",
id: obj.id,
title: fileMetadata.title,
author: fileMetadata.artist,
extension: 'mp4'
},
fileMetadata,
}
}

// handle audio downloads
if (obj.type == 'audio') {
let req = await fetch(`https://www.newgrounds.com/audio/listen/${obj.id}`, {
headers: {
'User-Agent': genericUserAgent,
}
})
.then(request => request.text())
.catch(() => {});

if (!req) return { error: 'ErrorCouldntFetch' };

const titleMatch = req.match(/"name"\s*:\s*"([^"]+)"/);
const artistMatch = req.match(/"artist"\s*:\s*"([^"]+)"/);
const urlMatch = req.match(/"filename"\s*:\s*"([^"]+)"/);

if (!titleMatch || !artistMatch || !urlMatch) {
return { error: 'ErrorCouldntFetch' };
}

const title = titleMatch[1];
const artist = artistMatch[1];
const url = urlMatch[1].replace(/\\\//g, '/');
let fileMetadata = {
title: cleanString(decodeURIComponent(title.trim())),
artist: cleanString(decodeURIComponent(artist.trim())),
}

return {
urls: url,
filenameAttributes: {
service: "newgrounds",
id: obj.id,
title: fileMetadata.title,
author: fileMetadata.artist,
},
fileMetadata,
isAudioOnly: true
}
}
}
hyperdefined marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions src/modules/processing/servicesConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
"alias": "loom videos",
"patterns": ["share/:id"],
"enabled": true
},
"newgrounds": {
"alias": "newgrounds.com",
"patterns": [":type/:method/:id"],
"enabled": true
}
}
}
3 changes: 3 additions & 0 deletions src/modules/processing/servicesPatternTesters.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ export const testers = {

"youtube": (patternMatch) =>
patternMatch.id?.length <= 11,

"newgrounds": (patternMatch) =>
patternMatch.id?.length <= 6,
hyperdefined marked this conversation as resolved.
Show resolved Hide resolved
}
37 changes: 37 additions & 0 deletions src/util/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1160,5 +1160,42 @@
"code": 400,
"status": "error"
}
}],
"newgrounds": [{
"name": "regular video",
"url": "https://www.newgrounds.com/portal/view/938050",
"params": {},
"expected": {
"code": 200,
"status": "stream"
}
}, {
"name": "regular video (audio only)",
"url": "https://www.newgrounds.com/portal/view/938050",
"params": {
"isAudioOnly": true
},
"expected": {
"code": 200,
"status": "stream"
}
}, {
"name": "regular video (muted)",
"url": "https://www.newgrounds.com/portal/view/938050",
"params": {
"isAudioMuted": true
},
"expected": {
"code": 200,
"status": "stream"
}
}, {
"name": "regular music",
"url": "https://www.newgrounds.com/audio/listen/500476",
"params": {},
"expected": {
"code": 200,
"status": "stream"
}
}]
}