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";
|
2022-07-24 11:54:05 +01:00
|
|
|
import loc from "../localization/manager.js";
|
2022-10-09 18:44:00 +01:00
|
|
|
import match from "./processing/match.js";
|
2023-08-20 11:11:16 +01:00
|
|
|
import hostOverrides from "./processing/hostOverrides.js";
|
2022-07-08 19:17:56 +01:00
|
|
|
|
2022-08-23 15:43:56 +01:00
|
|
|
export async function getJSON(originalURL, lang, obj) {
|
2022-07-08 19:17:56 +01:00
|
|
|
try {
|
2023-03-15 16:18:31 +00:00
|
|
|
let patternMatch, url = decodeURIComponent(originalURL),
|
|
|
|
hostname = new URL(url).hostname.split('.'),
|
|
|
|
host = hostname[hostname.length - 2];
|
2023-08-13 19:09:50 +01:00
|
|
|
|
2023-03-15 16:18:31 +00:00
|
|
|
if (!url.startsWith('https://')) return apiJSON(0, { t: errorUnsupported(lang) });
|
2023-02-09 14:45:17 +00:00
|
|
|
|
2023-08-20 11:11:16 +01:00
|
|
|
let overrides = hostOverrides(host, url);
|
|
|
|
host = overrides.host;
|
|
|
|
url = overrides.url;
|
|
|
|
|
2023-02-13 13:44:58 +00:00
|
|
|
if (!(host && host.length < 20 && host in patterns && patterns[host]["enabled"])) return apiJSON(0, { t: errorUnsupported(lang) });
|
|
|
|
|
2023-06-05 07:43:04 +01:00
|
|
|
let pathToMatch = cleanURL(url, host).split(`.${patterns[host]['tld'] ? patterns[host]['tld'] : "com"}/`)[1].replace('.', '');
|
2023-02-09 14:45:17 +00:00
|
|
|
for (let i in patterns[host]["patterns"]) {
|
2023-06-05 07:43:04 +01:00
|
|
|
patternMatch = new UrlPattern(patterns[host]["patterns"][i]).match(pathToMatch);
|
2023-03-15 16:18:31 +00:00
|
|
|
if (patternMatch) break
|
2023-02-09 14:45:17 +00:00
|
|
|
}
|
2023-02-13 13:44:58 +00:00
|
|
|
if (!patternMatch) return apiJSON(0, { t: errorUnsupported(lang) });
|
|
|
|
|
2023-03-15 16:18:31 +00:00
|
|
|
return await match(host, patternMatch, url, lang, obj)
|
2022-07-08 19:17:56 +01:00
|
|
|
} catch (e) {
|
2023-03-15 16:18:31 +00:00
|
|
|
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') })
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
2022-08-01 16:48:37 +01:00
|
|
|
}
|