2023-12-09 11:00:54 +00:00
|
|
|
import { services } from "./config.js";
|
2022-07-08 19:17:56 +01:00
|
|
|
|
2023-12-09 11:00:54 +00:00
|
|
|
import { 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-12-14 22:51:24 +00:00
|
|
|
import { getHostIfValid, normalizeURL } from "./processing/url.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-12-09 11:00:54 +00:00
|
|
|
const url = normalizeURL(decodeURIComponent(originalURL));
|
2023-12-14 22:51:24 +00:00
|
|
|
const host = getHostIfValid(url);
|
2023-02-09 14:45:17 +00:00
|
|
|
|
2023-12-14 22:51:24 +00:00
|
|
|
if (!host || !services[host].enabled) {
|
2023-12-09 11:00:54 +00:00
|
|
|
return apiJSON(0, { t: errorUnsupported(lang) });
|
|
|
|
}
|
2023-08-20 11:11:16 +01:00
|
|
|
|
2023-12-09 11:00:54 +00:00
|
|
|
let patternMatch;
|
|
|
|
for (const pattern of services[host].patterns) {
|
|
|
|
patternMatch = pattern.match(
|
|
|
|
url.pathname.substring(1) + url.search
|
|
|
|
);
|
|
|
|
if (patternMatch) break;
|
|
|
|
}
|
2023-02-13 13:44:58 +00:00
|
|
|
|
2023-12-09 11:00:54 +00:00
|
|
|
if (!patternMatch) {
|
|
|
|
return apiJSON(0, { t: errorUnsupported(lang) });
|
2023-02-09 14:45:17 +00:00
|
|
|
}
|
2023-02-13 13:44:58 +00:00
|
|
|
|
2023-12-14 23:04:05 +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
|
|
|
}
|