mirror of
https://github.com/imputnet/cobalt.git
synced 2025-07-18 11:18:28 +00:00
cleanup: move processing into different functions
This commit is contained in:
parent
3e83e1ec36
commit
2de4cd6a30
@ -1,86 +1,95 @@
|
|||||||
import { genericUserAgent } from "../../config.js";
|
import { genericUserAgent } from "../../config.js";
|
||||||
import { cleanString } from "../../sub/utils.js";
|
import { cleanString } from "../../sub/utils.js";
|
||||||
|
|
||||||
|
async function video(obj) {
|
||||||
|
let req = await fetch(`https://www.newgrounds.com/portal/video/${obj.id}`, {
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function music(obj) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default async function(obj) {
|
export default async function(obj) {
|
||||||
// handle video downloads
|
// handle video downloads
|
||||||
if (obj.type == 'portal') {
|
if (obj.type == 'portal') {
|
||||||
let req = await fetch(`https://www.newgrounds.com/portal/video/${obj.id}`, {
|
return video(obj);
|
||||||
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
|
// handle audio downloads
|
||||||
if (obj.type == 'audio') {
|
if (obj.type == 'audio') {
|
||||||
let req = await fetch(`https://www.newgrounds.com/audio/listen/${obj.id}`, {
|
return music(obj);
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return { error: 'ErrorUnsupported' };
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user