cobalt/src/modules/api.js

37 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-07-08 19:17:56 +01:00
import UrlPattern from "url-pattern";
import { services as patterns } from "./config.js";
2022-07-17 12:21:51 +01:00
import { cleanURL, apiJSON } from "./sub/utils.js";
2022-07-08 19:17:56 +01:00
import { errorUnsupported } from "./sub/errors.js";
import loc from "../localization/manager.js";
2022-10-09 18:44:00 +01:00
import match from "./processing/match.js";
import hostOverrides from "./processing/hostOverrides.js";
2022-07-08 19:17:56 +01:00
export async function getJSON(originalURL, lang, obj) {
2022-07-08 19:17:56 +01:00
try {
let patternMatch, url = decodeURIComponent(originalURL),
hostname = new URL(url).hostname.split('.'),
host = hostname[hostname.length - 2];
if (!url.startsWith('https://')) return apiJSON(0, { t: errorUnsupported(lang) });
let overrides = hostOverrides(host, url);
host = overrides.host;
url = overrides.url;
if (!(host && host.length < 20 && host in patterns && patterns[host]["enabled"])) return apiJSON(0, { t: errorUnsupported(lang) });
let pathToMatch = cleanURL(url, host).split(`.${patterns[host]['tld'] ? patterns[host]['tld'] : "com"}/`)[1].replace('.', '');
for (let i in patterns[host]["patterns"]) {
patternMatch = new UrlPattern(patterns[host]["patterns"][i]).match(pathToMatch);
if (patternMatch) break
}
if (!patternMatch) return apiJSON(0, { t: errorUnsupported(lang) });
return await match(host, patternMatch, url, lang, obj)
2022-07-08 19:17:56 +01:00
} catch (e) {
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') })
2022-07-08 19:17:56 +01:00
}
}