mirror of
https://github.com/wukko/cobalt.git
synced 2025-01-12 11:52:12 +01:00
api: move url extraction to url module
This commit is contained in:
parent
5c9ecb2781
commit
ae91f8b120
2 changed files with 30 additions and 19 deletions
|
@ -1,32 +1,17 @@
|
||||||
import { services } from "./config.js";
|
|
||||||
|
|
||||||
import { apiJSON } from "./sub/utils.js";
|
import { apiJSON } from "./sub/utils.js";
|
||||||
import { errorUnsupported } from "./sub/errors.js";
|
import { errorUnsupported } from "./sub/errors.js";
|
||||||
import loc from "../localization/manager.js";
|
import loc from "../localization/manager.js";
|
||||||
import match from "./processing/match.js";
|
import match from "./processing/match.js";
|
||||||
import { getHostIfValid } from "./processing/url.js";
|
import { extract } from "./processing/url.js";
|
||||||
|
|
||||||
export async function getJSON(url, lang, obj) {
|
export async function getJSON(url, lang, obj) {
|
||||||
try {
|
try {
|
||||||
const host = getHostIfValid(url);
|
const parsed = extract(url);
|
||||||
|
if (parsed === null) {
|
||||||
if (!host || !services[host].enabled) {
|
|
||||||
return apiJSON(0, { t: errorUnsupported(lang) });
|
return apiJSON(0, { t: errorUnsupported(lang) });
|
||||||
}
|
}
|
||||||
|
|
||||||
let patternMatch;
|
return await match(parsed.host, parsed.patternMatch, url, lang, obj)
|
||||||
for (const pattern of services[host].patterns) {
|
|
||||||
patternMatch = pattern.match(
|
|
||||||
url.pathname.substring(1) + url.search
|
|
||||||
);
|
|
||||||
if (patternMatch) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!patternMatch) {
|
|
||||||
return apiJSON(0, { t: errorUnsupported(lang) });
|
|
||||||
}
|
|
||||||
|
|
||||||
return await match(host, patternMatch, url, lang, obj)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') })
|
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') })
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,3 +125,29 @@ export function getHostIfValid(url) {
|
||||||
|
|
||||||
return host.sld;
|
return host.sld;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function extract(url) {
|
||||||
|
const host = getHostIfValid(url);
|
||||||
|
|
||||||
|
if (!host || !services[host].enabled) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let patternMatch;
|
||||||
|
for (const pattern of services[host].patterns) {
|
||||||
|
patternMatch = pattern.match(
|
||||||
|
url.pathname.substring(1) + url.search
|
||||||
|
);
|
||||||
|
|
||||||
|
if (patternMatch) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!patternMatch) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { host, patternMatch };
|
||||||
|
}
|
Loading…
Reference in a new issue